Skip to content

Commit 3030cc8

Browse files
authored
chore: removing leaf data type + related cleanup (AztecProtocol#3794)
1. Removed redundant LeafData struct and used the one from protocol circuits. I had to move the type to protocol-circuit/rollup-lib to protocol-circuit/types to have it exposed. 2. Fixes AztecProtocol#3444 3. Unified the naming in NullifierLeafPreimage in TS and Noir.
1 parent 3107aad commit 3030cc8

File tree

14 files changed

+142
-139
lines changed

14 files changed

+142
-139
lines changed

yarn-project/aztec-nr/aztec/src/history/nullifier_inclusion.nr

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ pub fn prove_nullifier_inclusion(
1717
let witness = get_nullifier_membership_witness(block_number, nullifier);
1818

1919
// 3) Check that the witness we obtained matches the nullifier
20-
assert(witness.leaf_data.value == nullifier, "Nullifier does not match value in witness");
20+
assert(witness.leaf_preimage.nullifier == nullifier, "Nullifier does not match value in witness");
2121

2222
// 4) Compute the nullifier tree leaf
23-
let nullifier_leaf = witness.leaf_data.hash();
23+
let nullifier_leaf = witness.leaf_preimage.hash();
2424

2525
// 5) Prove that the nullifier is in the nullifier tree
2626
assert(
27-
block_header.nullifier_tree_root == compute_merkle_root(nullifier_leaf, witness.index, witness.path),
28-
"Proving nullifier inclusion failed"
27+
block_header.nullifier_tree_root
28+
== compute_merkle_root(nullifier_leaf, witness.index, witness.path), "Proving nullifier inclusion failed"
2929
);
30-
3130
// --> Now we have traversed the trees all the way up to archive root and verified that the nullifier
3231
// was not yet included in the nullifier tree.
33-
}
32+
}

yarn-project/aztec-nr/aztec/src/history/nullifier_non_inclusion.nr

+7-9
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,23 @@ pub fn prove_nullifier_non_inclusion(
2828
// 3) Prove that the nullifier is not included in the nullifier tree
2929

3030
// 3.a) Compute the low nullifier leaf and prove that it is in the nullifier tree
31-
let low_nullifier_leaf = witness.leaf_data.hash();
31+
let low_nullifier_leaf = witness.leaf_preimage.hash();
3232
assert(
33-
block_header.nullifier_tree_root == compute_merkle_root(low_nullifier_leaf, witness.index, witness.path),
34-
"Proving nullifier non-inclusion failed: Could not prove low nullifier inclusion"
33+
block_header.nullifier_tree_root
34+
== compute_merkle_root(low_nullifier_leaf, witness.index, witness.path), "Proving nullifier non-inclusion failed: Could not prove low nullifier inclusion"
3535
);
3636

3737
// 3.b) Prove that the low nullifier is smaller than the nullifier
3838
assert(
39-
full_field_less_than(witness.leaf_data.value, nullifier),
40-
"Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed"
39+
full_field_less_than(witness.leaf_preimage.nullifier, nullifier), "Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed"
4140
);
4241

4342
// 3.c) Prove that the low nullifier is pointing "over" the nullifier to prove that the nullifier is not
4443
// included in the nullifier tree (or to 0 if the to-be-inserted nullifier is the largest of all)
4544
assert(
46-
full_field_greater_than(witness.leaf_data.next_value, nullifier) | (witness.leaf_data.next_index == 0),
47-
"Proving nullifier non-inclusion failed: low_nullifier.next_value > nullifier.value check failed"
45+
full_field_greater_than(witness.leaf_preimage.next_nullifier, nullifier)
46+
| (witness.leaf_preimage.next_index == 0), "Proving nullifier non-inclusion failed: low_nullifier.next_value > nullifier.value check failed"
4847
);
49-
5048
// --> Now we have traversed the trees all the way up to archive root and verified that the nullifier
5149
// was not yet included in the nullifier tree.
5250
}
@@ -60,4 +58,4 @@ pub fn prove_note_not_nullified<Note, N>(
6058
let nullifier = compute_siloed_nullifier(note_interface, note_with_header);
6159

6260
prove_nullifier_non_inclusion(nullifier, block_number, context);
63-
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,44 @@
11
use dep::protocol_types::{
2+
abis::nullifier_leaf_preimage::{
3+
NullifierLeafPreimage,
4+
NULLIFIER_LEAF_PREIMAGE_LENGTH,
5+
},
26
constants::NULLIFIER_TREE_HEIGHT,
37
hash::pedersen_hash,
48
};
59
use crate::utils::arr_copy_slice;
610

7-
global LEAF_DATA_LENGTH: Field = 3;
8-
// TODO: move this to constants.hpp so that it gets computed as INDEX_LENGTH + LEAF_DATA_LENGTH + NULLIFIER_TREE_HEIGHT
11+
// INDEX_LENGTH + NULLIFIER_LEAF_PREIMAGE_LENGTH + NULLIFIER_TREE_HEIGHT
912
global NULLIFIER_MEMBERSHIP_WITNESS: Field = 24;
1013

11-
// Noir version of LeafData interface from indexed merkle tree.
12-
// TODO(#3470) replace with /mnt/user-data/jan/aztec-packages/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/nullifier_leaf_preimage.nr
13-
struct LeafData {
14-
value: Field,
15-
next_index: Field,
16-
next_value: Field,
17-
}
18-
19-
impl LeafData {
20-
fn serialize(self) -> [Field; LEAF_DATA_LENGTH] {
21-
[self.value, self.next_index, self.next_value]
22-
}
23-
24-
fn hash(self) -> Field {
25-
// Performs the same hashing as StandardIndexedTree::encodeLeaf(...)
26-
pedersen_hash(self.serialize(), 0)
27-
}
28-
}
29-
3014
struct NullifierMembershipWitness {
3115
index: Field,
32-
leaf_data: LeafData,
16+
leaf_preimage: NullifierLeafPreimage,
3317
path: [Field; NULLIFIER_TREE_HEIGHT],
3418
}
3519

20+
impl NullifierMembershipWitness {
21+
pub fn deserialize(fields: [Field; NULLIFIER_MEMBERSHIP_WITNESS]) -> Self {
22+
let leaf_preimage_fields = arr_copy_slice(fields, [0; NULLIFIER_LEAF_PREIMAGE_LENGTH], 1);
23+
Self {
24+
index: fields[0],
25+
leaf_preimage: NullifierLeafPreimage::deserialize(leaf_preimage_fields),
26+
path: arr_copy_slice(fields, [0; NULLIFIER_TREE_HEIGHT], 1 + NULLIFIER_LEAF_PREIMAGE_LENGTH)
27+
}
28+
}
29+
}
30+
3631
#[oracle(getLowNullifierMembershipWitness)]
37-
fn get_low_nullifier_membership_witness_oracle(_block_number: u32, _nullifier: Field) -> [Field; NULLIFIER_MEMBERSHIP_WITNESS] {}
32+
fn get_low_nullifier_membership_witness_oracle(
33+
_block_number: u32,
34+
_nullifier: Field
35+
) -> [Field; NULLIFIER_MEMBERSHIP_WITNESS] {}
3836

3937
// Nullifier here refers to the nullifier we are looking to get non-inclusion proof for (by proving that a lower
4038
// nullifier's next_value is bigger than the nullifier)
4139
unconstrained pub fn get_low_nullifier_membership_witness(block_number: u32, nullifier: Field) -> NullifierMembershipWitness {
4240
let fields = get_low_nullifier_membership_witness_oracle(block_number, nullifier);
43-
NullifierMembershipWitness {
44-
index: fields[0],
45-
leaf_data: LeafData { value: fields[1], next_index: fields[2], next_value: fields[3] },
46-
path: arr_copy_slice(fields, [0; NULLIFIER_TREE_HEIGHT], 1 + LEAF_DATA_LENGTH)
47-
}
41+
NullifierMembershipWitness::deserialize(fields)
4842
}
4943

5044
#[oracle(getNullifierMembershipWitness)]
@@ -54,9 +48,5 @@ fn get_nullifier_membership_witness_oracle(_block_number: u32, _nullifier: Field
5448
// nullifier's next_value is bigger than the nullifier)
5549
unconstrained pub fn get_nullifier_membership_witness(block_number: u32, nullifier: Field) -> NullifierMembershipWitness {
5650
let fields = get_nullifier_membership_witness_oracle(block_number, nullifier);
57-
NullifierMembershipWitness {
58-
index: fields[0],
59-
leaf_data: LeafData { value: fields[1], next_index: fields[2], next_value: fields[3] },
60-
path: arr_copy_slice(fields, [0; NULLIFIER_TREE_HEIGHT], 1 + LEAF_DATA_LENGTH)
61-
}
62-
}
51+
NullifierMembershipWitness::deserialize(fields)
52+
}

yarn-project/circuits.js/src/structs/rollup/nullifier_leaf/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ export class NullifierLeafPreimage implements IndexedTreeLeafPreimage {
4545
toHashInputs(): Buffer[] {
4646
return [
4747
Buffer.from(this.nullifier.toBuffer()),
48-
Buffer.from(toBufferBE(this.nextIndex, 32)),
4948
Buffer.from(this.nextNullifier.toBuffer()),
49+
Buffer.from(toBufferBE(this.nextIndex, 32)),
5050
];
5151
}
5252

53+
toFieldArray(): Fr[] {
54+
return this.toHashInputs().map(buf => Fr.fromBuffer(buf));
55+
}
56+
5357
clone(): NullifierLeafPreimage {
5458
return new NullifierLeafPreimage(this.nullifier, this.nextNullifier, this.nextIndex);
5559
}
@@ -60,8 +64,8 @@ export class NullifierLeafPreimage implements IndexedTreeLeafPreimage {
6064

6165
static fromBuffer(buf: Buffer): NullifierLeafPreimage {
6266
const nullifier = Fr.fromBuffer(buf.subarray(0, 32));
63-
const nextIndex = toBigIntBE(buf.subarray(32, 64));
64-
const nextNullifier = Fr.fromBuffer(buf.subarray(64, 96));
67+
const nextNullifier = Fr.fromBuffer(buf.subarray(32, 64));
68+
const nextIndex = toBigIntBE(buf.subarray(64, 96));
6569
return new NullifierLeafPreimage(nullifier, nextNullifier, nextIndex);
6670
}
6771

yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis.nr

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
mod nullifier_leaf_preimage;
21
mod public_data_tree_leaf;
32
mod append_only_tree_snapshot;
43
mod global_variables;

yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/abis/nullifier_leaf_preimage.nr

-27
This file was deleted.

0 commit comments

Comments
 (0)