-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
278 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use acvm::{AcirField, FieldElement}; | ||
|
||
fn get_msb(n: u128) -> usize { | ||
let shift = std::mem::size_of::<u128>() * 8 - 1; | ||
((n >> shift) & 1_u128) as usize | ||
} | ||
|
||
pub trait BitsQueryable { | ||
fn num_bits(&self) -> usize; | ||
} | ||
|
||
impl BitsQueryable for FieldElement { | ||
fn num_bits(&self) -> usize { | ||
AcirField::num_bits(self) as usize | ||
} | ||
} | ||
|
||
impl BitsQueryable for u8 { | ||
fn num_bits(&self) -> usize { | ||
get_msb(*self as u128) | ||
} | ||
} | ||
|
||
impl BitsQueryable for u16 { | ||
fn num_bits(&self) -> usize { | ||
get_msb(*self as u128) | ||
} | ||
} | ||
|
||
impl BitsQueryable for u32 { | ||
fn num_bits(&self) -> usize { | ||
get_msb(*self as u128) | ||
} | ||
} | ||
|
||
impl BitsQueryable for u64 { | ||
fn num_bits(&self) -> usize { | ||
get_msb(*self as u128) | ||
} | ||
} | ||
|
||
impl BitsQueryable for u128 { | ||
fn num_bits(&self) -> usize { | ||
get_msb(*self) | ||
} | ||
} | ||
|
||
pub fn bits_needed_for<T: BitsQueryable>(val: &T) -> usize { | ||
let num_bits = val.num_bits(); | ||
if num_bits < 8 { | ||
8 | ||
} else if num_bits < 16 { | ||
16 | ||
} else if num_bits < 32 { | ||
32 | ||
} else if num_bits < 64 { | ||
64 | ||
} else if num_bits < 128 { | ||
128 | ||
} else { | ||
254 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.