-
Notifications
You must be signed in to change notification settings - Fork 21
feat!: Fixing the bytesize for be, le byte serialization, adding new functionality and tests #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c02e544
added more serialization and tests
5eeefa2
removed the brillig report
342d34f
remove prints
f103546
bumped nargo version
a081449
bb version
45b51b7
newer noirup
3840a5a
changed the casting to u128
2a8d736
moved the benchmarks into macros
8b0a1d0
unified the le and be logics
220ee11
Merge branch 'main' into kb/add_to_be_bytes_from_le_bytes
TomAFrench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,61 +1,79 @@ | ||
| /** | ||
| * @brief construct a BigNum instance out of an array of bytes in BIG ENDIAN format | ||
| * @description: each 120-bit limb represents 15 bytes, we require that the size of the byte array | ||
| * is precisely large enough to cover MOD_BITS | ||
| * @param x: input byte array | ||
| **/ | ||
| pub(crate) fn from_be_bytes<let N: u32, let MOD_BITS: u32, let NBytes: u32>( | ||
| x: [u8; NBytes], | ||
| use crate::utils::map::invert_array; | ||
| /// conversions between big endian and little endian byte arrays and BigNum instances | ||
| /// the byte serialization should have `(MOD_BITS + 7) / 8` bytes. | ||
| /// each 120-bit limb is represented by 15 bytes, and there are fewer bytes for covering the most significant limb | ||
| pub(crate) fn from_be_bytes<let N: u32, let MOD_BITS: u32>( | ||
| x: [u8; (MOD_BITS + 7) / 8], | ||
| ) -> [u128; N] { | ||
| let num_bits = NBytes * 8; | ||
| let num_bits = (MOD_BITS + 7) / 8 * 8; | ||
| assert(num_bits >= MOD_BITS); | ||
| assert(num_bits - MOD_BITS < 8); | ||
| let mut result: [u128; N] = [0; N]; | ||
|
|
||
| let excess_bytes = N * 15 - NBytes; | ||
| let excess_bytes = N * 15 - (MOD_BITS + 7) / 8; | ||
| let final_limb_bytes = 15 - excess_bytes; | ||
| let mut limb: u128 = 0; | ||
| let mut limb: Field = 0; | ||
| let mut k = 0; | ||
| for _j in 0..final_limb_bytes { | ||
| limb *= 256; | ||
| limb += x[k] as u128; | ||
| limb += x[k] as Field; | ||
| k += 1; | ||
| } | ||
| result[N - 1] = limb; | ||
| limb.assert_max_bit_size::<128>(); | ||
| result[N - 1] = limb as u128; | ||
|
|
||
| for i in 1..N { | ||
| let mut limb: u128 = 0; | ||
| let mut limb: Field = 0; | ||
| for _j in 0..15 { | ||
| limb *= 256; | ||
| limb += x[k] as u128; | ||
| limb += x[k] as Field; | ||
| k += 1; | ||
| } | ||
| result[N - i - 1] = limb; | ||
| limb.assert_max_bit_size::<128>(); | ||
| result[N - i - 1] = limb as u128; | ||
| } | ||
|
|
||
| let most_significant_byte: Field = x[0] as Field; | ||
|
|
||
| most_significant_byte.assert_max_bit_size::<8 - (NBytes * 8 - MOD_BITS)>(); | ||
| most_significant_byte.assert_max_bit_size::<8 - ((MOD_BITS + 7) / 8 * 8 - MOD_BITS)>(); | ||
| result | ||
| } | ||
|
|
||
| pub(crate) fn to_le_bytes<let N: u32, let MOD_BITS: u32, let NBytes: u32>( | ||
| pub(crate) fn to_be_bytes<let N: u32, let MOD_BITS: u32>( | ||
| val: [u128; N], | ||
| ) -> [u8; NBytes] { | ||
| let nbytes = (MOD_BITS / 8) + (MOD_BITS % 8 != 0) as u32; | ||
| assert(nbytes <= NBytes); | ||
|
|
||
| let mut result: [u8; NBytes] = [0; NBytes]; | ||
| ) -> [u8; (MOD_BITS + 7) / 8] { | ||
| let mut result: [u8; (MOD_BITS + 7) / 8] = [0; (MOD_BITS + 7) / 8]; | ||
| // the last limb will not have all the 15 bytes so we deal with the full limbs first | ||
| for i in 0..N - 1 { | ||
| let limb_bytes: [u8; 15] = (val[i] as Field).to_le_bytes(); | ||
| let index = N - i - 2; | ||
| let limb_bytes: [u8; 15] = (val[index] as Field).to_be_bytes(); | ||
| for j in 0..15 { | ||
| result[i * 15 + j] = limb_bytes[j]; | ||
| // we leave the space for the first byte empty, which would take (MOD_BITS+7)/8 - MOD_BITS/8 bytes | ||
| result[i * 15 + j + (MOD_BITS + 7) / 8 - (N - 1) * 15] = limb_bytes[j]; | ||
| } | ||
| } | ||
| let last_limb_bytes: [u8; 15] = (val[N - 1] as Field).to_le_bytes(); | ||
| let num_last_bytes = (NBytes - (N - 1) * 15); | ||
| for i in 0..num_last_bytes { | ||
| result[(N - 1) * 15 + i] = last_limb_bytes[i]; | ||
| // now we deal with the last limb | ||
| let last_limb_bytes: [u8; ((MOD_BITS + 7) / 8 - (N - 1) * 15)] = | ||
| (val[N - 1] as Field).to_be_bytes(); | ||
|
|
||
| for i in 0..((MOD_BITS + 7) / 8 - (N - 1) * 15) { | ||
| result[i] = last_limb_bytes[i]; | ||
| } | ||
| result | ||
| } | ||
|
|
||
| pub(crate) fn to_le_bytes<let N: u32, let MOD_BITS: u32>( | ||
| val: [u128; N], | ||
| ) -> [u8; (MOD_BITS + 7) / 8] { | ||
| let result_be: [u8; (MOD_BITS + 7) / 8] = to_be_bytes(val); | ||
| let result = invert_array(result_be); | ||
| result | ||
| } | ||
|
|
||
| pub(crate) fn from_le_bytes<let N: u32, let MOD_BITS: u32>( | ||
| x: [u8; (MOD_BITS + 7) / 8], | ||
| ) -> [u128; N] { | ||
| // make the bytes big endian | ||
| let be_x = invert_array(x); | ||
| from_be_bytes(be_x) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kashbrti should we update this repo's README:
noir-bignum/README.md
Line 49 in 873d901