Skip to content

Commit

Permalink
feat: bytes_to_fields requiring only 1 generic param
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 24, 2024
1 parent 74a8ad1 commit 066a7a9
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions noir-projects/aztec-nr/aztec/src/utils/bytes.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use std::static_assert;

// Converts the input bytes into an array of fields. A Field is ~254 bits meaning that each field can store 31 bytes.
// This implies that M = ceil(N / 31).
//
// Each 31 byte chunk is converted into a Field as if the chunk was the Field's big endian representation. If the last chunk
// is less than 31 bytes long, then only the relevant bytes are conisdered.
// For example, [1, 10, 3] is encoded as [1 * 256^2 + 10 * 256 + 3]
pub fn bytes_to_fields<let N: u32, let M: u32>(input: [u8; N]) -> [Field; M] {
static_assert(N <= 31 * M, "Bytes do not fit into fields");
let mut dst = [0; M];
pub fn bytes_to_fields<let N: u32>(input: [u8; N]) -> [Field; (N + 30) / 31] {
let mut dst = [0; (N + 30) / 31];

for dst_index in 0..M {
for dst_index in 0..((N + 30) / 31) {
let mut field_value = 0;

for i in 0..31 {
Expand Down Expand Up @@ -78,7 +76,7 @@ mod test {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31,
];
let output = bytes_to_fields::<31, 1>(input);
let output = bytes_to_fields::<31>(input);

assert_eq(output[0], 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f);
}
Expand Down Expand Up @@ -139,7 +137,7 @@ mod test {
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
];
let output = bytes_to_fields::<59, 2>(input);
let output = bytes_to_fields::<59>(input);

assert_eq(output[0], 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f);
assert_eq(output[1], 0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b);
Expand All @@ -165,7 +163,7 @@ mod test {

#[test]
fn test_large_random_input_to_fields_and_back(input: [u8; 128]) {
let output = bytes_to_fields::<128, 5>(input);
let output = bytes_to_fields::<128>(input);
let input_back = fields_to_bytes::<128, 5>(output);

assert_eq(input, input_back);
Expand Down Expand Up @@ -194,7 +192,7 @@ mod test {
}

let output = fields_to_bytes::<155, 5>(input);
let input_back = bytes_to_fields::<155, 5>(output);
let input_back = bytes_to_fields::<155>(output);

assert_eq(input, input_back);
}
Expand All @@ -203,7 +201,7 @@ mod test {
fn test_too_few_destination_fields() {
// This should fail because we need 2 fields to store 32 bytes but we only provide 1.
let input = [0 as u8; 32];
let _ignored_result = bytes_to_fields::<32, 1>(input);
let _ignored_result = bytes_to_fields::<32>(input);
}

#[test(should_fail_with = "Field does not fit into remaining bytes")]
Expand Down

0 comments on commit 066a7a9

Please sign in to comment.