Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 2, 2024
1 parent 9eef247 commit d80f61c
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 131 deletions.
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
Expand Up @@ -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
Expand Up @@ -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.
}
Expand All @@ -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
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], 0);
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(
_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)]
Expand All @@ -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
Expand Up @@ -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);
}
Expand All @@ -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);
}

Expand Down
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;
Expand Down

This file was deleted.

Loading

0 comments on commit d80f61c

Please sign in to comment.