Skip to content

Commit 70175c1

Browse files
committed
refactor: make Note a marker trait
1 parent d8e08d8 commit 70175c1

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

packages/contracts/noir/common/src/erc20_note.nr

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use protocol_types::hash::poseidon2_hash_with_separator;
2-
31
// fails to compile if this file is moved to erc20 crate
42

53
pub struct Erc20Note {
@@ -8,6 +6,8 @@ pub struct Erc20Note {
86
pub randomness: Field,
97
}
108

9+
impl crate::Note for Erc20Note {}
10+
1111
impl Erc20Note {
1212
pub fn sub_and_emit_change<let N: u32>(
1313
context: &mut crate::Context,
@@ -41,13 +41,6 @@ impl crate::Serialize<6> for Erc20Note {
4141
}
4242
}
4343

44-
impl crate::Note for Erc20Note {
45-
fn hash(self) -> Field {
46-
let serialized = self.serialize();
47-
poseidon2_hash_with_separator(serialized, crate::GENERATOR_INDEX__NOTE_HASH)
48-
}
49-
}
50-
5144
impl crate::OwnedNote for Erc20Note {
5245
fn owner(self) -> crate::WaAddress {
5346
self.owner

packages/contracts/noir/common/src/lib.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod owned_note;
88

99
pub use context::{Context, Result};
1010
pub use erc20_note::{Erc20Note, Erc20NoteConsumptionInputs};
11-
pub use note::Note;
11+
pub use note::{compute_note_hash, Note};
1212
pub use owned_note::{NoteConsumptionInputs, OwnedNote};
1313
pub use protocol_types::{address::EthAddress, traits::Serialize};
1414

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
pub trait Note {
2-
fn hash(self) -> Field;
1+
use protocol_types::hash::poseidon2_hash_with_separator;
2+
3+
/// A marker trait to mark structs as notes
4+
pub trait Note: crate::Serialize<_> {
35

46
pub fn emit(self, context: &mut crate::Context) {
5-
context.push_note_hash(self.hash());
7+
context.push_note_hash(crate::compute_note_hash(self));
68
}
79
}
10+
11+
pub fn compute_note_hash<T>(note: T) -> Field
12+
where
13+
T: Note,
14+
{
15+
let serialized = note.serialize();
16+
poseidon2_hash_with_separator(serialized, crate::GENERATOR_INDEX__NOTE_HASH)
17+
}

packages/contracts/noir/common/src/owned_note.nr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ fn compute_nullifier_of_owned_note<T>(note: T, secret_key: Field) -> Field
1111
where
1212
T: OwnedNote,
1313
{
14+
// TODO(perf): pass note hash as an argument to avoid hashing twice?
1415
assert_eq(note.owner(), crate::WaAddress::from_secret_key(secret_key), "invalid secret key");
1516
poseidon2_hash_with_separator(
16-
[note.hash(), secret_key],
17+
[crate::compute_note_hash(note), secret_key],
1718
crate::GENERATOR_INDEX__NOTE_NULLIFIER,
1819
)
1920
}
@@ -32,7 +33,7 @@ where
3233
{
3334
pub fn consume(self, context: &mut crate::Context, secret_key: Field) {
3435
merkle_tree::assert_check_membership(
35-
self.note.hash(),
36+
crate::compute_note_hash(self.note),
3637
self.note_index,
3738
self.note_sibling_path,
3839
context.tree_roots().note_hash_root,

0 commit comments

Comments
 (0)