-
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 2 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 |
|---|---|---|
|
|
@@ -55,23 +55,27 @@ pub struct NoteHashAndNullifier { | |
| /// ```noir | ||
| /// |packed_note, owner, storage_slot, note_type_id, contract_address, randomness, note_nonce| { | ||
| /// if note_type_id == MyNoteType::get_id() { | ||
| /// assert(packed_note.len() == MY_NOTE_TYPE_SERIALIZATION_LENGTH); | ||
| /// if packed_note.len() != MY_NOTE_TYPE_SERIALIZATION_LENGTH { | ||
| /// Option::none() | ||
| /// } else { | ||
| /// let note = MyNoteType::unpack(aztec::utils::array::subarray(packed_note.storage(), 0)); | ||
| /// | ||
| /// let note = MyNoteType::unpack(aztec::utils::array::subarray(packed_note.storage(), 0)); | ||
| /// let note_hash = note.compute_note_hash(owner, storage_slot, randomness); | ||
| /// let note_hash_for_nullification = aztec::note::utils::compute_note_hash_for_nullification( | ||
| /// HintedNote { | ||
|
Contributor
Author
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. Updated the comment since now |
||
| /// note, contract_address, owner, randomness, storage_slot, | ||
| /// metadata: SettledNoteMetadata::new(note_nonce).into(), | ||
| /// }, | ||
| /// ); | ||
| /// | ||
| /// let note_hash = note.compute_note_hash(owner, storage_slot, randomness); | ||
| /// let note_hash_for_nullification = aztec::note::utils::compute_note_hash_for_nullification( | ||
| /// HintedNote{ note, contract_address, metadata: SettledNoteMetadata::new(note_nonce).into() }, | ||
| /// storage_slot | ||
| /// ); | ||
| /// let inner_nullifier = note.compute_nullifier_unconstrained(owner, note_hash_for_nullification); | ||
| /// | ||
| /// let inner_nullifier = note.compute_nullifier_unconstrained(owner, note_hash_for_nullification); | ||
| /// | ||
| /// Option::some( | ||
| /// aztec::messages::discovery::NoteHashAndNullifier { | ||
| /// note_hash, inner_nullifier | ||
| /// } | ||
| /// ) | ||
| /// Option::some( | ||
| /// aztec::messages::discovery::NoteHashAndNullifier { | ||
| /// note_hash, inner_nullifier | ||
| /// } | ||
| /// ) | ||
| /// } | ||
| /// } else if note_type_id == MyOtherNoteType::get_id() { | ||
| /// ... // Similar to above but calling MyOtherNoteType::unpack_content | ||
| /// } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| mod compute_note_hash_and_nullifier; | ||
| mod macro_id_allocation; | ||
| mod deployment_proofs; | ||
| mod note_delivery; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| use crate::{Test, test_note::TestNote}; | ||
|
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. Test contract is an old pile of mess so ideally would not add to that and just create a new test contract for this. |
||
| 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]); | ||
|
|
||
| let result = 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 = 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 always returns None | ||
| assert(note_hash_and_nullifier.inner_nullifier.is_none()); | ||
| } | ||
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.
Made
pub(crate)so we could test itThere 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.
Given that the tests are already in a contract crate this can be avoided by just having a simple wrapper function in the test contract that is exposed and just calls this.
Would say that is preferrable.
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.
Agree, that would be much better 🫡