From 06ee4512cd49c69dd41124f04cd5382e33eecf31 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 12 Dec 2023 15:40:46 +0000 Subject: [PATCH] nullifier inclusion libraryfied --- yarn-project/aztec-nr/aztec/src/history.nr | 1 + .../aztec/src/history/nullifier_inclusion.nr | 33 +++++++++++++++++++ .../inclusion_proofs_contract/src/main.nr | 24 +++----------- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/yarn-project/aztec-nr/aztec/src/history.nr b/yarn-project/aztec-nr/aztec/src/history.nr index 8f7c1f232dc..9f800d0d294 100644 --- a/yarn-project/aztec-nr/aztec/src/history.nr +++ b/yarn-project/aztec-nr/aztec/src/history.nr @@ -1,2 +1,3 @@ mod note_inclusion; +mod nullifier_inclusion; mod nullifier_non_inclusion; \ No newline at end of file diff --git a/yarn-project/aztec-nr/aztec/src/history/nullifier_inclusion.nr b/yarn-project/aztec-nr/aztec/src/history/nullifier_inclusion.nr index e69de29bb2d..f6f5185cace 100644 --- a/yarn-project/aztec-nr/aztec/src/history/nullifier_inclusion.nr +++ b/yarn-project/aztec-nr/aztec/src/history/nullifier_inclusion.nr @@ -0,0 +1,33 @@ +use dep::std::merkle::compute_merkle_root; + +use crate::{ + context::PrivateContext, + oracle::get_nullifier_membership_witness::get_nullifier_membership_witness, +}; + +pub fn prove_nullifier_inclusion( + nullifier: Field, + block_number: u32, // The block at which we'll prove that the note exists + context: PrivateContext +) { + // 1) Get block header from oracle and ensure that the block hash is included in the archive. + let block_header = context.get_block_header(block_number); + + // 2) Get the membership witness of the nullifier + 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"); + + // 4) Compute the nullifier tree leaf + let nullifier_leaf = witness.leaf_data.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" + ); + + // --> 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. +} \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/inclusion_proofs_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/inclusion_proofs_contract/src/main.nr index e17a8fca3d6..be32c2d171a 100644 --- a/yarn-project/noir-contracts/src/contracts/inclusion_proofs_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/inclusion_proofs_contract/src/main.nr @@ -39,6 +39,9 @@ contract InclusionProofs { prove_note_commitment_inclusion, prove_note_inclusion, }, + nullifier_inclusion::{ + prove_nullifier_inclusion, + }, nullifier_non_inclusion::{ prove_nullifier_non_inclusion, prove_note_not_nullified, @@ -158,26 +161,7 @@ contract InclusionProofs { nullifier: Field, block_number: u32, // The block at which we'll prove that the nullifier not exists in the tree ) { - // 1) Get block header from oracle and ensure that the block hash is included in the archive. - let block_header = context.get_block_header(block_number); - - // 2) Get the membership witness of the nullifier - 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"); - - // 4) Compute the nullifier tree leaf - let nullifier_leaf = witness.leaf_data.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" - ); - - // --> Now we have traversed the trees all the way up to blocks tree root and verified that the nullifier - // was not yet included in the nullifier tree. + prove_nullifier_inclusion(nullifier, block_number, context); } #[aztec(private)]