Skip to content

Commit

Permalink
enough fields check
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Sep 18, 2024
1 parent 697f403 commit 12b6dc9
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions noir-projects/aztec-nr/aztec/src/utils/bytes.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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).
pub fn bytes_to_fields<let N: u32, let M: u32>(input: [u8; N]) -> [Field; M] {
assert(N <= 31 * M, "Bytes do not fit into fields");
let mut dst = [0; M];

for field_index in 0..M {
Expand Down Expand Up @@ -171,6 +172,13 @@ mod test {
assert_eq(input, input_back);
}

#[test(should_fail_with="Bytes do not fit into fields")]
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);
}

#[test(should_fail_with="Field does not fit into remaining bytes")]
fn test_too_few_destination_bytes() {
// We should get an error here because first field gets converted to 31 bytes and the second field needs
Expand Down

0 comments on commit 12b6dc9

Please sign in to comment.