Skip to content

Commit 671e1fe

Browse files
committed
refactor: make Note a marker trait
1 parent d8e08d8 commit 671e1fe

File tree

6 files changed

+30
-26
lines changed

6 files changed

+30
-26
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ mod note;
77
mod owned_note;
88

99
pub use context::{Context, Result};
10-
pub use erc20_note::{Erc20Note, Erc20NoteConsumptionInputs};
11-
pub use note::Note;
10+
pub use note::{compute_note_hash, Note};
1211
pub use owned_note::{NoteConsumptionInputs, OwnedNote};
1312
pub use protocol_types::{address::EthAddress, traits::Serialize};
1413

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
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+
// TODO(security): add note type to the hash (erc20, erc721, etc.)
17+
poseidon2_hash_with_separator(serialized, crate::GENERATOR_INDEX__NOTE_HASH)
18+
}

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,

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use common::{Erc20Note, Erc20NoteConsumptionInputs};
1+
pub use common::erc20_note::{Erc20Note, Erc20NoteConsumptionInputs};
22

33
pub mod Token {
44
pub fn mint(
@@ -7,17 +7,17 @@ pub mod Token {
77
amount: common::TokenAmount,
88
randomness: Field,
99
) {
10-
common::Erc20Note { owner: to, amount, randomness }.emit(context);
10+
crate::Erc20Note { owner: to, amount, randomness }.emit(context);
1111
}
1212

1313
pub fn burn(
1414
context: &mut common::Context,
1515
from_secret_key: Field,
16-
from_note_inputs: common::Erc20NoteConsumptionInputs,
16+
from_note_inputs: crate::Erc20NoteConsumptionInputs,
1717
amount: common::TokenAmount,
1818
change_randomness: Field,
1919
) {
20-
common::Erc20Note::sub_and_emit_change(
20+
crate::Erc20Note::sub_and_emit_change(
2121
context,
2222
[from_note_inputs],
2323
amount,
@@ -29,7 +29,7 @@ pub mod Token {
2929
pub fn join<let N: u32>(
3030
context: &mut common::Context,
3131
from_secret_key: Field,
32-
notes: [common::Erc20NoteConsumptionInputs; N],
32+
notes: [crate::Erc20NoteConsumptionInputs; N],
3333
to: common::WaAddress,
3434
join_randomness: Field,
3535
) {
@@ -42,27 +42,27 @@ pub mod Token {
4242
joined_amount += notes[i].note.amount;
4343
}
4444

45-
common::Erc20Note { owner: to, amount: joined_amount, randomness: join_randomness }.emit(
45+
crate::Erc20Note { owner: to, amount: joined_amount, randomness: join_randomness }.emit(
4646
context,
4747
);
4848
}
4949

5050
pub fn transfer(
5151
context: &mut common::Context,
5252
from_secret_key: Field,
53-
from_note_inputs: common::Erc20NoteConsumptionInputs,
53+
from_note_inputs: crate::Erc20NoteConsumptionInputs,
5454
to: common::WaAddress,
5555
amount: common::TokenAmount,
5656
to_randomness: Field,
5757
change_randomness: Field,
5858
) {
59-
common::Erc20Note::sub_and_emit_change(
59+
crate::Erc20Note::sub_and_emit_change(
6060
context,
6161
[from_note_inputs],
6262
amount,
6363
change_randomness,
6464
from_secret_key,
6565
);
66-
common::Erc20Note { owner: to, amount, randomness: to_randomness }.emit(context);
66+
crate::Erc20Note { owner: to, amount, randomness: to_randomness }.emit(context);
6767
}
6868
}

packages/contracts/noir/erc20_join/src/main.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main(
55
tree_roots: pub common::TreeRoots,
66
// params
77
from_secret_key: Field,
8-
notes: [common::Erc20NoteConsumptionInputs; MAX_NOTES_TO_JOIN],
8+
notes: [erc20::Erc20NoteConsumptionInputs; MAX_NOTES_TO_JOIN],
99
to: common::WaAddress,
1010
join_randomness: Field,
1111
) -> pub common::Result<1, MAX_NOTES_TO_JOIN> {

0 commit comments

Comments
 (0)