-
Notifications
You must be signed in to change notification settings - Fork 615
fix: handle bad note lengths on compute_note_hash_and_nullifier #21271
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
Changes from 9 commits
351ae91
c0b1461
4677c6b
a5259d7
f6cfecd
5736e66
64636c5
3932efa
b894c47
834b523
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [package] | ||
| name = "note_hash_and_nullifier_contract" | ||
| authors = [""] | ||
| compiler_version = ">=0.25.0" | ||
| type = "contract" | ||
|
|
||
| [dependencies] | ||
| aztec = { path = "../../../../../aztec-nr/aztec" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| pub mod test_note; | ||
| mod test; | ||
|
|
||
| use aztec::macros::aztec; | ||
|
|
||
| /// A minimal contract used to test the macro-generated `_compute_note_hash_and_nullifier` function. | ||
| #[aztec] | ||
| pub contract NoteHashAndNullifier { | ||
| use aztec::{ | ||
| messages::{ | ||
| discovery::NoteHashAndNullifier as NoteHashAndNullifierResult, | ||
| logs::note::MAX_NOTE_PACKED_LEN, | ||
| }, | ||
| protocol::address::AztecAddress, | ||
| }; | ||
|
|
||
| #[contract_library_method] | ||
| pub unconstrained fn test_compute_note_hash_and_nullifier( | ||
| packed_note: BoundedVec<Field, MAX_NOTE_PACKED_LEN>, | ||
| owner: AztecAddress, | ||
| storage_slot: Field, | ||
| note_type_id: Field, | ||
| contract_address: AztecAddress, | ||
| randomness: Field, | ||
| note_nonce: Field, | ||
| ) -> Option<NoteHashAndNullifierResult> { | ||
| _compute_note_hash_and_nullifier( | ||
| packed_note, | ||
| owner, | ||
| storage_slot, | ||
| note_type_id, | ||
| contract_address, | ||
| randomness, | ||
| note_nonce, | ||
| ) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| use crate::{NoteHashAndNullifier, test_note::TestNote}; | ||
| use aztec::note::note_interface::{NoteHash, NoteType}; | ||
| use aztec::protocol::address::AztecAddress; | ||
|
|
||
| #[test] | ||
| unconstrained fn returns_none_for_bad_note_length() { | ||
| // TestNote has Packable N=1, but we provide 2 fields | ||
| let packed_note = BoundedVec::from_array([42, 99]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would also add a test case for empty BVec. |
||
|
|
||
| let result = NoteHashAndNullifier::test_compute_note_hash_and_nullifier( | ||
| packed_note, | ||
| AztecAddress::zero(), | ||
| 0, | ||
| TestNote::get_id(), | ||
| AztecAddress::zero(), | ||
| 0, | ||
| 0, | ||
| ); | ||
|
|
||
| assert(result.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn returns_correct_note_hash_and_nullifier() { | ||
| // TestNote has Packable N=1 | ||
| let packed_note = BoundedVec::from_array([42]); | ||
|
|
||
| let owner = AztecAddress::zero(); | ||
| let storage_slot = 0; | ||
| let randomness = 0; | ||
|
|
||
| let result = NoteHashAndNullifier::test_compute_note_hash_and_nullifier( | ||
| packed_note, | ||
| owner, | ||
| storage_slot, | ||
| TestNote::get_id(), | ||
| AztecAddress::zero(), | ||
| randomness, | ||
| 1, | ||
| ); | ||
|
|
||
| let note_hash_and_nullifier = result.unwrap(); | ||
| let note = TestNote { value: 42 }; | ||
| let expected_note_hash = note.compute_note_hash(owner, storage_slot, randomness); | ||
| assert_eq(note_hash_and_nullifier.note_hash, expected_note_hash); | ||
|
|
||
| // TestNote::compute_nullifier_unconstrained returns a hardcoded Some(2) so we can verify the | ||
| // macro-generated function propagates the inner nullifier correctly. | ||
| assert_eq(note_hash_and_nullifier.inner_nullifier.unwrap(), 2); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though you have it explained here anyway think it would be better to define a TEST_NOTE_NULLIFIER in test_note.nr and use it here. Then you can easily navigate with LSP to where the value originates. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| use aztec::{ | ||
| context::PrivateContext, | ||
| macros::notes::custom_note, | ||
| note::note_interface::NoteHash, | ||
| protocol::{ | ||
| address::AztecAddress, constants::DOM_SEP__NOTE_HASH, hash::poseidon2_hash_with_separator, | ||
| traits::Packable, | ||
| }, | ||
| }; | ||
|
|
||
| #[derive(Eq, Packable)] | ||
| #[custom_note] | ||
| pub struct TestNote { | ||
| pub value: Field, | ||
| } | ||
|
|
||
| impl NoteHash for TestNote { | ||
| fn compute_note_hash( | ||
| self, | ||
| _owner: AztecAddress, | ||
| storage_slot: Field, | ||
| randomness: Field, | ||
| ) -> Field { | ||
| let inputs = self.pack().concat([storage_slot, randomness]); | ||
| poseidon2_hash_with_separator(inputs, DOM_SEP__NOTE_HASH) | ||
| } | ||
|
|
||
| fn compute_nullifier( | ||
| _self: Self, | ||
| _context: &mut PrivateContext, | ||
| _owner: AztecAddress, | ||
| _note_hash_for_nullification: Field, | ||
| ) -> Field { | ||
| // Not used in any meaningful way | ||
| 0 | ||
| } | ||
|
|
||
| unconstrained fn compute_nullifier_unconstrained( | ||
| _self: Self, | ||
| _owner: AztecAddress, | ||
| _note_hash_for_nullification: Field, | ||
| ) -> Option<Field> { | ||
| // Returns a hardcoded value so we can verify that `_compute_note_hash_and_nullifier` propagates it correctly. | ||
| Option::some(2) | ||
| } | ||
| } |
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.
Updated the comment since now
storage_slotgoes insideHintedNote