-
Notifications
You must be signed in to change notification settings - Fork 599
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
Merged
nchamo
merged 10 commits into
merge-train/fairies
from
fix/handle-bad-note-lengths-compute-note-hash-and-nullifier
Mar 11, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
351ae91
fix: return Option::none() instead of panicking on packed note length…
nchamo c0b1461
chore: nargo fmt
nchamo 4677c6b
build: new test contract for compute_note_hash_and_nullifier and add …
nchamo a5259d7
test: verify inner nullifier propagation in compute_note_hash_and_nul…
nchamo f6cfecd
refactor: remove unnecessary dummy storage from test contract
nchamo 5736e66
refactor: move tests into contract package for CI compatibility
nchamo 64636c5
fix: remove unused TestNote import in contract module
nchamo 3932efa
chore: improve comments in test note and warning message
nchamo b894c47
docs: fix inaccuracies in ComputeNoteHashAndNullifier doc comment
nchamo 834b523
address pr review
nchamo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...tracts/contracts/test/note_hash_and_nullifier/note_hash_and_nullifier_contract/Nargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" } |
37 changes: 37 additions & 0 deletions
37
...racts/contracts/test/note_hash_and_nullifier/note_hash_and_nullifier_contract/src/main.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
...racts/contracts/test/note_hash_and_nullifier/note_hash_and_nullifier_contract/src/test.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use crate::{NoteHashAndNullifier, test_note::{TEST_NOTE_NULLIFIER, 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); | ||
|
|
||
| assert_eq(note_hash_and_nullifier.inner_nullifier.unwrap(), TEST_NOTE_NULLIFIER); | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn returns_none_for_empty_packed_note() { | ||
| let packed_note = BoundedVec::new(); | ||
|
|
||
| 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()); | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
.../contracts/test/note_hash_and_nullifier/note_hash_and_nullifier_contract/src/test_note.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| 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, | ||
| } | ||
|
|
||
| pub global TEST_NOTE_NULLIFIER: Field = 2; | ||
|
|
||
| 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(TEST_NOTE_NULLIFIER) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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