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

feat: add historical library tests and remove old inclusion proof contract / related tests #12215

Merged
merged 9 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
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
23 changes: 0 additions & 23 deletions .test_patterns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,6 @@ tests:
owners:
- "U04DT239VQU" # sean

# FAIL ./flakey_e2e_inclusion_proofs_contract.test.ts
# ● e2e_inclusion_proofs_contract › contract inclusion › proves public deployment of a contract
#
# Undefined argument value of type field
#
# 41 | private encodeArgument(abiType: AbiType, arg: any, name?: string) {
# 42 | if (arg === undefined || arg == null) {
# > 43 | throw new Error(`Undefined argument ${name ?? 'unnamed'} of type ${abiType.kind}`);
# | ^
# 44 | }
# 45 | switch (abiType.kind) {
# 46 | case 'field':
#
# at ArgumentEncoder.encodeArgument (../../stdlib/src/abi/encoder.ts:43:13)
# at ArgumentEncoder.encode (../../stdlib/src/abi/encoder.ts:137:12)
# at encodeArguments (../../stdlib/src/abi/encoder.ts:150:41)
# at computeInitializationHash (../../stdlib/src/contract/contract_address.ts:75:20)
# at getContractInstanceFromDeployParams (../../stdlib/src/contract/contract_instance.ts:124:9)
# at Object.getContractInstanceFromDeployParams (flakey_e2e_inclusion_proofs_contract.test.ts:275:24)
- regex: "simple flakey_e2e_inclusion_proofs_contract"
owners:
- "UKUMA5J7K" # charlie

- regex: "simple e2e_fees/private_payments"
owners:
- "U02G4KAD57Y" # phil
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
"Nargo",
"nixpkgs",
"nodebuffer",
"noinitcheck",
"noirc",
"noirup",
"nullifer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_position: 4
tags: [contracts]
---

The Aztec Protocol uses an append-only Merkle tree to store hashes of the headers of all previous blocks in the chain as its leaves. This is known as the Archive tree.
The Aztec Protocol uses an append-only Merkle tree to store hashes of the headers of all previous blocks in the chain as its leaves. This is known as the Archive tree.

This page is a quick introductory guide to creating historical proofs proofs from the archive tree.

Expand Down Expand Up @@ -37,33 +37,31 @@ Using this library, you can check that specific notes or nullifiers were part of

In general you will likely have the note you want to prove inclusion of. But if you are just experimenting you can create a note with a function like below:

#include_code create_note noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust
#include_code create_note noir-projects/noir-contracts/contracts/test_contract/src/main.nr rust

## Get the note from the PXE

Retrieve the note from the user's PXE.

#include_code get_note_from_pxe noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust
#include_code get_note_from_pxe noir-projects/noir-contracts/contracts/test_contract/src/main.nr rust

In this example, the user's notes are stored in a map called `private_values`. We retrieve this map, then select 1 note from it with the value of `1`.
In this example, we fetch notes located in the storage slot that we pass in from the function parameters. The notes also must match the criteria specified by the getter options that we declare and are able to customize.

## Prove that a note was included in a specified block

To prove that a note existed in a specified block, call `prove_note_inclusion` on the `header` as shown in this example:

#include_code prove_note_inclusion noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust

Here, if `block_number` exists as an argument, it will prove inclusion in that block. Else, it will use the current block.
#include_code prove_note_inclusion noir-projects/aztec-nr/aztec/src/history/note_inclusion/test.nr rust

This will only prove the note existed at the specific block number, not whether or not the note has been nullified. You can prove that a note existed and had not been nullified in a specified block by using `prove_note_validity` on the block header which takes the following arguments:

#include_code prove_note_validity noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust
#include_code prove_note_validity noir-projects/aztec-nr/aztec/src/history/note_validity/test.nr rust

## Create a nullifier to prove inclusion of

You can easily nullify a note like so:

#include_code nullify_note noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust
#include_code nullify_note noir-projects/noir-contracts/contracts/test_contract/src/main.nr rust

This function gets a note from the PXE and nullifies it with `remove()`.

Expand All @@ -73,7 +71,7 @@ You can then compute this nullifier with `note.compute_nullifier(&mut context)`.

Call `prove_nullifier_inclusion` on a block header like so:

#include_code prove_nullifier_inclusion noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust
#include_code prove_nullifier_inclusion noir-projects/aztec-nr/aztec/src/history/nullifier_inclusion/test.nr rust

It takes the nullifier as an argument.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ EscrowContractArtifact
FPCContractArtifact
FeeJuiceContractArtifact
ImportTestContractArtifact
InclusionProofsContractArtifact
LendingContractArtifact
MultiCallEntrypointContractArtifact
ParentContractArtifact
Expand Down
11 changes: 7 additions & 4 deletions noir-projects/aztec-nr/aztec/src/history/contract_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use dep::protocol_types::{
hash::compute_siloed_nullifier,
};

trait ProveContractDeployment {
// This is tested in `noir-projects/noir-contracts/test_contract/src/test.nr because we cannot define a contract
// from within aztec.nr (due to the contract macro).

pub trait ProveContractDeployment {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These were exposed to be used in the counter-contracts due to not being able to test this in aztec-nr.

Copy link
Contributor

Choose a reason for hiding this comment

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

They should be exposed anyway as the history library and the whole of Aztec.nr is to be consumed from contracts.

fn prove_contract_deployment(header: BlockHeader, contract_address: AztecAddress);
}

Expand All @@ -17,7 +20,7 @@ impl ProveContractDeployment for BlockHeader {
}
}

trait ProveContractNonDeployment {
pub trait ProveContractNonDeployment {
fn prove_contract_non_deployment(header: BlockHeader, contract_address: AztecAddress);
}

Expand All @@ -33,7 +36,7 @@ impl ProveContractNonDeployment for BlockHeader {
}
}

trait ProveContractInitialization {
pub trait ProveContractInitialization {
fn prove_contract_initialization(header: BlockHeader, contract_address: AztecAddress);
}

Expand All @@ -46,7 +49,7 @@ impl ProveContractInitialization for BlockHeader {
}
}

trait ProveContractNonInitialization {
pub trait ProveContractNonInitialization {
fn prove_contract_non_initialization(header: BlockHeader, contract_address: AztecAddress);
}

Expand Down
13 changes: 7 additions & 6 deletions noir-projects/aztec-nr/aztec/src/history/mod.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod contract_inclusion;
mod note_inclusion;
mod note_validity;
mod nullifier_inclusion;
mod nullifier_non_inclusion;
mod public_storage;
pub mod contract_inclusion;
pub mod note_inclusion;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exposing all these as well to remove warnings

pub mod note_validity;
pub mod nullifier_inclusion;
pub mod nullifier_non_inclusion;
pub mod public_storage;
mod test;
2 changes: 2 additions & 0 deletions noir-projects/aztec-nr/aztec/src/history/note_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::{
oracle::get_membership_witness::get_note_hash_membership_witness,
};

mod test;

trait ProveNoteInclusion {
fn prove_note_inclusion<Note>(
header: BlockHeader,
Expand Down
22 changes: 22 additions & 0 deletions noir-projects/aztec-nr/aztec/src/history/note_inclusion/test.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::history::{note_inclusion::ProveNoteInclusion, test};

// In this test, we create a note and prove its inclusion in the block of its creation.
#[test]
unconstrained fn note_inclusion() {
let (env, retrieved_note) = test::create_note();

let context = &mut env.private_at(test::NOTE_CREATED_AT);

// docs:start:prove_note_inclusion
context.historical_header.prove_note_inclusion(retrieved_note, test::NOTE_STORAGE_SLOT);
// docs:end:prove_note_inclusion
}

// In this test, we create a note and fail to prove its inclusion in the block before its creation.
#[test(should_fail_with = "not found in NOTE_HASH_TREE at block 1")]
unconstrained fn note_inclusion_fails() {
let (env, retrieved_note) = test::create_note();

let context = &mut env.private_at(test::NOTE_CREATED_AT - 1);
context.historical_header.prove_note_inclusion(retrieved_note, test::NOTE_STORAGE_SLOT);
}
2 changes: 2 additions & 0 deletions noir-projects/aztec-nr/aztec/src/history/note_validity.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::{

use dep::protocol_types::block_header::BlockHeader;

mod test;

trait ProveNoteValidity {
fn prove_note_validity<Note>(
header: BlockHeader,
Expand Down
38 changes: 38 additions & 0 deletions noir-projects/aztec-nr/aztec/src/history/note_validity/test.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::history::note_validity::ProveNoteValidity;
use crate::history::test;

// In these tests, we create a note in one block and nullify it in the next.

// In this test, we fail to prove the note's validity in the block before its creation. It fails the validity check due to
// non-inclusion of the note in the block before its creation.
#[test(should_fail_with = "not found in NOTE_HASH_TREE at block 1")]
unconstrained fn note_not_valid_due_to_non_inclusion() {
let (env, retrieved_note) = test::create_note_and_nullify_it();

let context = &mut env.private_at(test::NOTE_CREATED_AT - 1);

context.historical_header.prove_note_validity(retrieved_note, test::NOTE_STORAGE_SLOT, context);
}

// In this test, we prove the note's validity in the block at its creation, and before its nullification. It succeeds
// because it is included and not nullified at the block specified.
#[test]
unconstrained fn note_is_valid() {
let (env, retrieved_note) = test::create_note_and_nullify_it();

let context = &mut env.private_at(test::NOTE_CREATED_AT);

// docs:start:prove_note_validity
context.historical_header.prove_note_validity(retrieved_note, test::NOTE_STORAGE_SLOT, context);
// docs:end:prove_note_validity
}

// In this test, we fail to prove the note's validity in the block at its nullification. It fails the validity check due to
// its nullifier being included in the state at one block after it was created.
#[test(should_fail_with = "Proving nullifier non-inclusion failed")]
unconstrained fn note_not_valid_due_to_nullification() {
let (env, retrieved_note) = test::create_note_and_nullify_it();

let context = &mut env.private_at(test::NOTE_NULLIFIED_AT);
context.historical_header.prove_note_validity(retrieved_note, test::NOTE_STORAGE_SLOT, context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::{
oracle::get_nullifier_membership_witness::get_nullifier_membership_witness,
};

mod test;

trait ProveNullifierInclusion {
fn prove_nullifier_inclusion(header: BlockHeader, nullifier: Field);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::history::{nullifier_inclusion::{ProveNoteIsNullified, ProveNullifierInclusion}, test};
use crate::oracle::random::random;
use crate::test::helpers::test_environment::FIRST_NULLIFIER_EMITTED_IN_TXE;

// In these tests, we create a note in one block and nullify it in the next.

// In this test, we prove the presence of the note's nullifier in state at the block it was nullified in.
#[test]
unconstrained fn note_is_nullified() {
let (env, retrieved_note) = test::create_note_and_nullify_it();

let context = &mut env.private_at(test::NOTE_NULLIFIED_AT);

context.historical_header.prove_note_is_nullified(
retrieved_note,
test::NOTE_STORAGE_SLOT,
context,
);
}

// In this test, we fail to prove the presence of the note's nullifier in state at the block before it was nullified.
#[test(should_fail_with = "Nullifier membership witness not found at block 2.")]
unconstrained fn note_is_not_nullified() {
let (env, retrieved_note) = test::create_note_and_nullify_it();

let context = &mut env.private_at(test::NOTE_NULLIFIED_AT - 1);

context.historical_header.prove_note_is_nullified(
retrieved_note,
test::NOTE_STORAGE_SLOT,
context,
);
}

// In this test, we prove the inclusion of an existing nullifier in state. We use know FIRST_NULLIFIER_EMITTED_IN_TXE exists
// because the TXe creates deterministic first nullifiers if no side-effects are emitted.
#[test]
unconstrained fn nullifier_inclusion() {
let (env) = test::create_note_and_nullify_it();

let context = &mut env.private();

// docs:start:prove_nullifier_inclusion
context.historical_header.prove_nullifier_inclusion(FIRST_NULLIFIER_EMITTED_IN_TXE);
// docs:end:prove_nullifier_inclusion
}

// In this test, we fail to prove the inclusion of an arbitrary nullifier in state.
#[test(should_fail_with = "Nullifier membership witness not found")]
unconstrained fn nullifier_inclusion_fails() {
let (env) = test::create_note_and_nullify_it();

let context = &mut env.private();
context.historical_header.prove_nullifier_inclusion(random());
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use dep::protocol_types::{
};
use dep::protocol_types::merkle_tree::root::root_from_sibling_path;

mod test;

trait ProveNullifierNonInclusion {
fn prove_nullifier_non_inclusion(header: BlockHeader, nullifier: Field);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::history::nullifier_non_inclusion::{ProveNoteNotNullified, ProveNullifierNonInclusion};
use crate::oracle::random::random;

use crate::history::test;
use crate::test::helpers::test_environment::FIRST_NULLIFIER_EMITTED_IN_TXE;

// In these tests, we create a note in one block and nullify it in the next.

// In this test, we prove the absence of the note's nullifier in state at the block before it was nullified.
#[test]
unconstrained fn note_not_nullified() {
let (env, retrieved_note) = test::create_note_and_nullify_it();

let context = &mut env.private_at(test::NOTE_NULLIFIED_AT - 1);

context.historical_header.prove_note_not_nullified(
retrieved_note,
test::NOTE_STORAGE_SLOT,
context,
);
}

// In this test, we fail prove the absence of the note's nullifier in state at the block it was nullified in.
#[test(should_fail_with = "Proving nullifier non-inclusion failed")]
unconstrained fn note_not_nullified_fails() {
let (env, retrieved_note) = test::create_note_and_nullify_it();

let context = &mut env.private_at(test::NOTE_NULLIFIED_AT);
context.historical_header.prove_note_not_nullified(
retrieved_note,
test::NOTE_STORAGE_SLOT,
context,
);
}

// In this test, we prove the absence of an arbitrary nullifier in state.
#[test]
unconstrained fn nullifier_non_inclusion() {
let (env) = test::create_note_and_nullify_it();

let context = &mut env.private();

context.historical_header.prove_nullifier_non_inclusion(random());
}

// In this test, we fail to prove the absence of an existing nullifier in state. We use know FIRST_NULLIFIER_EMITTED_IN_TXE exists
// because the TXe creates deterministic first nullifiers if no side-effects are emitted.
#[test(should_fail_with = "Proving nullifier non-inclusion failed")]
unconstrained fn nullifier_non_inclusion_fails() {
let (env) = test::create_note_and_nullify_it();

let context = &mut env.private();

context.historical_header.prove_nullifier_non_inclusion(FIRST_NULLIFIER_EMITTED_IN_TXE);
}
2 changes: 2 additions & 0 deletions noir-projects/aztec-nr/aztec/src/history/public_storage.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use dep::protocol_types::merkle_tree::root::root_from_sibling_path;

use crate::oracle::get_public_data_witness::get_public_data_witness;

mod test;

trait PublicStorageHistoricalRead {
fn public_storage_historical_read(
header: BlockHeader,
Expand Down
Loading
Loading