Skip to content
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

chore: removing leaf data type + related cleanup #3794

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions yarn-project/aztec-nr/aztec/src/history/nullifier_inclusion.nr
Original file line number Diff line number Diff line change
@@ -17,17 +17,16 @@ pub fn prove_nullifier_inclusion(
let witness = get_nullifier_membership_witness(block_number, nullifier);

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

// 4) Compute the nullifier tree leaf
let nullifier_leaf = witness.leaf_data.hash();
let nullifier_leaf = witness.leaf_preimage.hash();

// 5) Prove that the nullifier is in the nullifier tree
assert(
block_header.nullifier_tree_root == compute_merkle_root(nullifier_leaf, witness.index, witness.path),
"Proving nullifier inclusion failed"
block_header.nullifier_tree_root
== compute_merkle_root(nullifier_leaf, witness.index, witness.path), "Proving nullifier inclusion failed"
);

// --> Now we have traversed the trees all the way up to archive root and verified that the nullifier
// was not yet included in the nullifier tree.
}
}
Original file line number Diff line number Diff line change
@@ -28,25 +28,23 @@ pub fn prove_nullifier_non_inclusion(
// 3) Prove that the nullifier is not included in the nullifier tree

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

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

// 3.c) Prove that the low nullifier is pointing "over" the nullifier to prove that the nullifier is not
// included in the nullifier tree (or to 0 if the to-be-inserted nullifier is the largest of all)
assert(
full_field_greater_than(witness.leaf_data.next_value, nullifier) | (witness.leaf_data.next_index == 0),
"Proving nullifier non-inclusion failed: low_nullifier.next_value > nullifier.value check failed"
full_field_greater_than(witness.leaf_preimage.next_nullifier, nullifier)
| (witness.leaf_preimage.next_index == 0), "Proving nullifier non-inclusion failed: low_nullifier.next_value > nullifier.value check failed"
);

// --> Now we have traversed the trees all the way up to archive root and verified that the nullifier
// was not yet included in the nullifier tree.
}
@@ -60,4 +58,4 @@ pub fn prove_note_not_nullified<Note, N>(
let nullifier = compute_siloed_nullifier(note_interface, note_with_header);

prove_nullifier_non_inclusion(nullifier, block_number, context);
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,44 @@
use dep::protocol_types::{
abis::nullifier_leaf_preimage::{
NullifierLeafPreimage,
NULLIFIER_LEAF_PREIMAGE_LENGTH,
},
constants::NULLIFIER_TREE_HEIGHT,
hash::pedersen_hash,
};
use crate::utils::arr_copy_slice;

global LEAF_DATA_LENGTH: Field = 3;
// TODO: move this to constants.hpp so that it gets computed as INDEX_LENGTH + LEAF_DATA_LENGTH + NULLIFIER_TREE_HEIGHT
// INDEX_LENGTH + NULLIFIER_LEAF_PREIMAGE_LENGTH + NULLIFIER_TREE_HEIGHT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be in the constants such that it is computed in there instead of being a custom value in here. The noir constants are used to generate the other values so should be fine for handling it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it now possible to compute a constant in Noir? constants.hpp is no longer used so as far as I know we don't have a way to do this anymore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was thinking just moving it into the constants, but guess it is already based on stuff from constants so might be useless actually.

global NULLIFIER_MEMBERSHIP_WITNESS: Field = 24;

// Noir version of LeafData interface from indexed merkle tree.
// 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
struct LeafData {
value: Field,
next_index: Field,
next_value: Field,
}

impl LeafData {
fn serialize(self) -> [Field; LEAF_DATA_LENGTH] {
[self.value, self.next_index, self.next_value]
}

fn hash(self) -> Field {
// Performs the same hashing as StandardIndexedTree::encodeLeaf(...)
pedersen_hash(self.serialize(), 0)
}
}

struct NullifierMembershipWitness {
index: Field,
leaf_data: LeafData,
leaf_preimage: NullifierLeafPreimage,
path: [Field; NULLIFIER_TREE_HEIGHT],
}

impl NullifierMembershipWitness {
pub fn deserialize(fields: [Field; NULLIFIER_MEMBERSHIP_WITNESS]) -> Self {
let leaf_preimage_fields = arr_copy_slice(fields, [0; NULLIFIER_LEAF_PREIMAGE_LENGTH], 1);
Self {
index: fields[0],
leaf_preimage: NullifierLeafPreimage::deserialize(leaf_preimage_fields),
path: arr_copy_slice(fields, [0; NULLIFIER_TREE_HEIGHT], 1 + NULLIFIER_LEAF_PREIMAGE_LENGTH)
}
}
}

#[oracle(getLowNullifierMembershipWitness)]
fn get_low_nullifier_membership_witness_oracle(_block_number: u32, _nullifier: Field) -> [Field; NULLIFIER_MEMBERSHIP_WITNESS] {}
fn get_low_nullifier_membership_witness_oracle(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the formatting not run before this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was run but with an older nargo which did not wrap lines correctly. I will send a PR later in which I will fix it.

_block_number: u32,
_nullifier: Field
) -> [Field; NULLIFIER_MEMBERSHIP_WITNESS] {}

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

#[oracle(getNullifierMembershipWitness)]
@@ -54,9 +48,5 @@ fn get_nullifier_membership_witness_oracle(_block_number: u32, _nullifier: Field
// nullifier's next_value is bigger than the nullifier)
unconstrained pub fn get_nullifier_membership_witness(block_number: u32, nullifier: Field) -> NullifierMembershipWitness {
let fields = get_nullifier_membership_witness_oracle(block_number, nullifier);
NullifierMembershipWitness {
index: fields[0],
leaf_data: LeafData { value: fields[1], next_index: fields[2], next_value: fields[3] },
path: arr_copy_slice(fields, [0; NULLIFIER_TREE_HEIGHT], 1 + LEAF_DATA_LENGTH)
}
}
NullifierMembershipWitness::deserialize(fields)
}
Original file line number Diff line number Diff line change
@@ -45,11 +45,15 @@ export class NullifierLeafPreimage implements IndexedTreeLeafPreimage {
toHashInputs(): Buffer[] {
return [
Buffer.from(this.nullifier.toBuffer()),
Buffer.from(toBufferBE(this.nextIndex, 32)),
Buffer.from(this.nextNullifier.toBuffer()),
Buffer.from(toBufferBE(this.nextIndex, 32)),
];
}

toFieldArray(): Fr[] {
return this.toHashInputs().map(buf => Fr.fromBuffer(buf));
}

clone(): NullifierLeafPreimage {
return new NullifierLeafPreimage(this.nullifier, this.nextNullifier, this.nextIndex);
}
@@ -60,8 +64,8 @@ export class NullifierLeafPreimage implements IndexedTreeLeafPreimage {

static fromBuffer(buf: Buffer): NullifierLeafPreimage {
const nullifier = Fr.fromBuffer(buf.subarray(0, 32));
const nextIndex = toBigIntBE(buf.subarray(32, 64));
const nextNullifier = Fr.fromBuffer(buf.subarray(64, 96));
const nextNullifier = Fr.fromBuffer(buf.subarray(32, 64));
const nextIndex = toBigIntBE(buf.subarray(64, 96));
return new NullifierLeafPreimage(nullifier, nextNullifier, nextIndex);
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod nullifier_leaf_preimage;
mod public_data_tree_leaf;
mod append_only_tree_snapshot;
mod global_variables;

This file was deleted.

Loading