Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion noir-projects/aztec-nr/aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ fn extract_property_value_from_selector<let N: u32>(packed_note: [Field; N], sel
let value: [u8; 32] = packed_note[selector.index as u32].to_be_bytes();
let offset = selector.offset;
let length = selector.length;
assert(offset as u32 + length as u32 <= 32, "PropertySelector offset + length exceeds field byte width");
let mut value_field = 0 as Field;
let mut acc: Field = 1;
for i in 0..32 {
if i < length {
value_field += value[(31 + offset - i) as u32] as Field * acc;
// `value` is big-endian, so the last byte (index 31) holds the lowest value.
// offset shifts the starting point away from that last byte, and i walks
// through consecutive bytes.
value_field += value[(31 - offset - i) as u32] as Field * acc;
acc = acc * 256;
}
}
Expand Down
58 changes: 58 additions & 0 deletions noir-projects/aztec-nr/aztec/src/note/note_getter/test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,61 @@ unconstrained fn constrain_view_note_rejects_mismatched_owner() {
let _ = confirm_hinted_note(context, note, Option::some(owner), storage_slot);
});
}
#[test]
unconstrained fn extract_property_value_full_field_is_identity() {
let packed = [12345 as Field];
let selector = PropertySelector { index: 0, offset: 0, length: 32 };
let result = super::extract_property_value_from_selector(packed, selector);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import the fn instead of using super:: in each test

assert_eq(result, 12345);
}

#[test]
unconstrained fn extract_property_value_zero_length_returns_zero() {
let packed = [12345 as Field];
let selector = PropertySelector { index: 0, offset: 0, length: 0 };
let result = super::extract_property_value_from_selector(packed, selector);
assert_eq(result, 0);
}

#[test]
unconstrained fn extract_property_value_with_zero_offset() {
// 0x030201 = [..., 0x03, 0x02, 0x01]. offset=0, length=2 reads [0x01, 0x02] -> 0x0201.
let packed = [0x030201 as Field];
let selector = PropertySelector { index: 0, offset: 0, length: 2 };
let result = super::extract_property_value_from_selector(packed, selector);
assert_eq(result, 0x0201);
}

#[test]
unconstrained fn extract_property_value_with_nonzero_offset() {
// 0x0100 = [..., 0x01, 0x00]. offset=1 skips 0x00, length=1 reads 0x01.
let packed = [0x0100 as Field];
let selector = PropertySelector { index: 0, offset: 1, length: 1 };
let result = super::extract_property_value_from_selector(packed, selector);
assert_eq(result, 1);
}
Comment thread
nchamo marked this conversation as resolved.

#[test]
unconstrained fn extract_property_value_nonzero_index() {
// index=1 skips the first Field (999) and reads from the second one (0x030201).
let packed = [999 as Field, 0x030201 as Field];
Comment thread
nchamo marked this conversation as resolved.
let selector = PropertySelector { index: 1, offset: 1, length: 2 };
let result = super::extract_property_value_from_selector(packed, selector);
assert_eq(result, 0x0302);
}

#[test]
unconstrained fn extract_property_value_max_offset_reads_first_byte() {
// 256^31 = byte[0]=1, rest=0. offset=31, length=1 reads byte[0].
let packed = [Field::pow_32(256, 31)];
let selector = PropertySelector { index: 0, offset: 31, length: 1 };
let result = super::extract_property_value_from_selector(packed, selector);
assert_eq(result, 1);
}

#[test(should_fail_with = "PropertySelector offset + length exceeds field byte width")]
unconstrained fn extract_property_value_rejects_offset_plus_length_exceeding_32() {
let packed = [1 as Field];
let selector = PropertySelector { index: 0, offset: 31, length: 2 };
let _ = super::extract_property_value_from_selector(packed, selector);
}
Loading