Skip to content

Commit d8b7add

Browse files
author
sklppy88
committed
init
1 parent f7a8f6b commit d8b7add

File tree

25 files changed

+366
-710
lines changed

25 files changed

+366
-710
lines changed

docs/docs/developers/guides/smart_contracts/writing_contracts/how_to_prove_history.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sidebar_position: 4
44
tags: [contracts]
55
---
66

7-
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.
7+
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.
88

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

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

3838
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:
3939

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

4242
## Get the note from the PXE
4343

4444
Retrieve the note from the user's PXE.
4545

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

48-
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`.
48+
In this example, the user's notes are stored in a storage_slot that is specified. We look for notes located in this storage_slot and return the ones matched by the getter options.
4949

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

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

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

5856
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:
5957

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

6260
## Create a nullifier to prove inclusion of
6361

6462
You can easily nullify a note like so:
6563

66-
#include_code nullify_note noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust
64+
#include_code nullify_note noir-projects/noir-contracts/contracts/test_contract/src/main.nr rust
6765

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

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

7472
Call `prove_nullifier_inclusion` on a block header like so:
7573

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

7876
It takes the nullifier as an argument.
7977

docs/docs/developers/reference/environment_reference/sandbox-reference.md

-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ EscrowContractArtifact
133133
FPCContractArtifact
134134
FeeJuiceContractArtifact
135135
ImportTestContractArtifact
136-
InclusionProofsContractArtifact
137136
LendingContractArtifact
138137
MultiCallEntrypointContractArtifact
139138
ParentContractArtifact

noir-projects/aztec-nr/aztec/src/history/contract_inclusion.nr

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use dep::protocol_types::{
33
hash::compute_siloed_nullifier,
44
};
55

6-
trait ProveContractDeployment {
6+
pub trait ProveContractDeployment {
77
fn prove_contract_deployment(header: BlockHeader, contract_address: AztecAddress);
88
}
99

@@ -17,7 +17,7 @@ impl ProveContractDeployment for BlockHeader {
1717
}
1818
}
1919

20-
trait ProveContractNonDeployment {
20+
pub trait ProveContractNonDeployment {
2121
fn prove_contract_non_deployment(header: BlockHeader, contract_address: AztecAddress);
2222
}
2323

@@ -33,7 +33,7 @@ impl ProveContractNonDeployment for BlockHeader {
3333
}
3434
}
3535

36-
trait ProveContractInitialization {
36+
pub trait ProveContractInitialization {
3737
fn prove_contract_initialization(header: BlockHeader, contract_address: AztecAddress);
3838
}
3939

@@ -46,7 +46,7 @@ impl ProveContractInitialization for BlockHeader {
4646
}
4747
}
4848

49-
trait ProveContractNonInitialization {
49+
pub trait ProveContractNonInitialization {
5050
fn prove_contract_non_initialization(header: BlockHeader, contract_address: AztecAddress);
5151
}
5252

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// TODO (12225): the #[aztec] macro currently does not support being used in aztec-nr, hence we cannot do any variation of the below.
2+
// Replace the hacky work around in noir-contracts/counter_contract which tests the contract inclusion.
3+
4+
// use crate::history::note_inclusion::ProveNoteInclusion;
5+
// use crate::note::retrieved_note::RetrievedNote;
6+
// use crate::oracle::execution::get_contract_address;
7+
// use crate::oracle::random::random;
8+
// use crate::test::{helpers::test_environment::TestEnvironment, mocks::mock_note::MockNote};
9+
10+
// use dep::protocol_types::address::AztecAddress;
11+
12+
// use crate::macros::aztec;
13+
14+
// #[aztec]
15+
// pub contract Dummy {
16+
// use crate::macros::functions::{initializer, private};
17+
18+
// #[initializer]
19+
// #[private]
20+
// fn constructor() {
21+
// }
22+
// }
23+
24+
// unconstrained fn setup() -> AztecAddress {
25+
// let mut env = TestEnvironment::new();
26+
27+
// let initializer = Dummy::interface().constructor();
28+
// let dummy_contract = env.deploy_self("Dummy").with_private_initializer(initializer);
29+
30+
// env.advance_block_by(1);
31+
32+
// dummy_contract.to_address()
33+
34+
// AztecAddress::from_field(1)
35+
// }
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
mod contract_inclusion;
2-
mod note_inclusion;
3-
mod note_validity;
4-
mod nullifier_inclusion;
5-
mod nullifier_non_inclusion;
6-
mod public_storage;
1+
pub mod contract_inclusion;
2+
pub mod note_inclusion;
3+
pub mod note_validity;
4+
pub mod nullifier_inclusion;
5+
pub mod nullifier_non_inclusion;
6+
pub mod public_storage;
7+
mod test;

noir-projects/aztec-nr/aztec/src/history/note_inclusion.nr

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use crate::{
1010
oracle::get_membership_witness::get_note_hash_membership_witness,
1111
};
1212

13+
mod test;
14+
1315
trait ProveNoteInclusion {
1416
fn prove_note_inclusion<Note>(
1517
header: BlockHeader,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::history::{note_inclusion::ProveNoteInclusion, test};
2+
3+
#[test]
4+
unconstrained fn note_inclusion() {
5+
let (env, retrieved_note) = test::create_note_and_optionally_nullify_it(false);
6+
7+
let context = &mut env.private();
8+
9+
// docs:start:prove_note_inclusion
10+
context.historical_header.prove_note_inclusion(retrieved_note, 15);
11+
// docs:end:prove_note_inclusion
12+
}
13+
14+
#[test(should_fail_with = "not found in NOTE_HASH_TREE at block 1")]
15+
unconstrained fn note_inclusion_fails() {
16+
let (env, retrieved_note) = test::create_note_and_optionally_nullify_it(false);
17+
18+
let context = &mut env.private_at(1);
19+
context.historical_header.prove_note_inclusion(retrieved_note, 15);
20+
}

noir-projects/aztec-nr/aztec/src/history/note_validity.nr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::{
55

66
use dep::protocol_types::block_header::BlockHeader;
77

8+
mod test;
9+
810
trait ProveNoteValidity {
911
fn prove_note_validity<Note>(
1012
header: BlockHeader,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::history::note_validity::ProveNoteValidity;
2+
use crate::history::test;
3+
4+
#[test(should_fail_with = "not found in NOTE_HASH_TREE at block 1")]
5+
unconstrained fn note_not_valid_due_to_non_inclusion() {
6+
let (env, retrieved_note) = test::create_note_and_optionally_nullify_it(true);
7+
8+
let context = &mut env.private_at(1);
9+
10+
context.historical_header.prove_note_validity(retrieved_note, 15, context);
11+
}
12+
13+
#[test]
14+
unconstrained fn note_is_valid() {
15+
let (env, retrieved_note) = test::create_note_and_optionally_nullify_it(true);
16+
17+
let context = &mut env.private_at(2);
18+
19+
// docs:start:prove_note_validity
20+
context.historical_header.prove_note_validity(retrieved_note, 15, context);
21+
// docs:end:prove_note_validity
22+
}
23+
24+
#[test(should_fail_with = "Proving nullifier non-inclusion failed")]
25+
unconstrained fn note_not_valid_due_to_nullification() {
26+
let (env, retrieved_note) = test::create_note_and_optionally_nullify_it(true);
27+
28+
let context = &mut env.private();
29+
context.historical_header.prove_note_validity(retrieved_note, 15, context);
30+
}

noir-projects/aztec-nr/aztec/src/history/nullifier_inclusion.nr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use crate::{
1111
oracle::get_nullifier_membership_witness::get_nullifier_membership_witness,
1212
};
1313

14+
mod test;
15+
1416
trait ProveNullifierInclusion {
1517
fn prove_nullifier_inclusion(header: BlockHeader, nullifier: Field);
1618
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::history::{nullifier_inclusion::{ProveNoteIsNullified, ProveNullifierInclusion}, test};
2+
use crate::oracle::random::random;
3+
4+
#[test]
5+
unconstrained fn note_is_nullified() {
6+
let (env, retrieved_note) = test::create_note_and_optionally_nullify_it(true);
7+
8+
let context = &mut env.private();
9+
10+
context.historical_header.prove_note_is_nullified(retrieved_note, 15, context);
11+
}
12+
13+
#[test(should_fail_with = "Nullifier membership witness not found at block 2.")]
14+
unconstrained fn note_is_not_nullified() {
15+
let (env, retrieved_note) = test::create_note_and_optionally_nullify_it(true);
16+
17+
let context = &mut env.private_at(2);
18+
19+
context.historical_header.prove_note_is_nullified(retrieved_note, 15, context);
20+
}
21+
22+
#[test]
23+
unconstrained fn nullifier_inclusion() {
24+
let (env) = test::create_note_and_optionally_nullify_it(true);
25+
26+
let context = &mut env.private_at(2);
27+
28+
// We use the first nullifier created (because the TXe creates deterministic first nullifiers if no side-effects are emitted)
29+
// docs:start:prove_nullifier_inclusion
30+
context.historical_header.prove_nullifier_inclusion(6969 + 1);
31+
// docs:end:prove_nullifier_inclusion
32+
}
33+
34+
#[test(should_fail_with = "Nullifier membership witness not found")]
35+
unconstrained fn nullifier_inclusion_fails() {
36+
let (env) = test::create_note_and_optionally_nullify_it(true);
37+
38+
let context = &mut env.private();
39+
context.historical_header.prove_nullifier_inclusion(random());
40+
}

noir-projects/aztec-nr/aztec/src/history/nullifier_non_inclusion.nr

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use dep::protocol_types::{
1313
};
1414
use dep::protocol_types::merkle_tree::root::root_from_sibling_path;
1515

16+
mod test;
17+
1618
trait ProveNullifierNonInclusion {
1719
fn prove_nullifier_non_inclusion(header: BlockHeader, nullifier: Field);
1820
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::history::nullifier_non_inclusion::{ProveNoteNotNullified, ProveNullifierNonInclusion};
2+
use crate::oracle::random::random;
3+
4+
use crate::history::test;
5+
6+
#[test]
7+
unconstrained fn note_not_nullified() {
8+
let (env, retrieved_note) = test::create_note_and_optionally_nullify_it(true);
9+
10+
let context = &mut env.private_at(2);
11+
12+
context.historical_header.prove_note_not_nullified(retrieved_note, 15, context);
13+
}
14+
15+
#[test(should_fail_with = "Proving nullifier non-inclusion failed")]
16+
unconstrained fn note_not_nullified_fails() {
17+
let (env, retrieved_note) = test::create_note_and_optionally_nullify_it(true);
18+
19+
let context = &mut env.private();
20+
context.historical_header.prove_note_not_nullified(retrieved_note, 15, context);
21+
}
22+
23+
#[test]
24+
unconstrained fn nullifier_non_inclusion() {
25+
let (env) = test::create_note_and_optionally_nullify_it(true);
26+
27+
let context = &mut env.private_at(2);
28+
29+
context.historical_header.prove_nullifier_non_inclusion(random());
30+
}
31+
32+
#[test(should_fail_with = "Proving nullifier non-inclusion failed")]
33+
unconstrained fn nullifier_non_inclusion_fails() {
34+
let (env) = test::create_note_and_optionally_nullify_it(true);
35+
36+
let context = &mut env.private();
37+
38+
// We use the first nullifier created (because the TXe creates deterministic first nullifiers if no side-effects are emitted)
39+
context.historical_header.prove_nullifier_non_inclusion(6969 + 1);
40+
}

noir-projects/aztec-nr/aztec/src/history/public_storage.nr

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use dep::protocol_types::merkle_tree::root::root_from_sibling_path;
66

77
use crate::oracle::get_public_data_witness::get_public_data_witness;
88

9+
mod test;
10+
911
trait PublicStorageHistoricalRead {
1012
fn public_storage_historical_read(
1113
header: BlockHeader,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::{
2+
history::public_storage::PublicStorageHistoricalRead,
3+
oracle::execution::get_contract_address,
4+
test::{helpers::test_environment::TestEnvironment, mocks::mock_note::MockNote},
5+
};
6+
7+
unconstrained fn setup() -> &mut TestEnvironment {
8+
let mut env = TestEnvironment::new();
9+
let context = &mut env.public();
10+
11+
context.storage_write(15, 69);
12+
13+
env.advance_block_by(1);
14+
15+
&mut env
16+
}
17+
18+
#[test]
19+
unconstrained fn public_storage_read() {
20+
let env = setup();
21+
22+
let context = &mut env.private();
23+
24+
assert_eq(
25+
context.historical_header.public_storage_historical_read(15, get_contract_address()),
26+
69,
27+
);
28+
}
29+
30+
#[test]
31+
unconstrained fn public_storage_read_empty() {
32+
let env = setup();
33+
34+
let context = &mut env.private_at(1);
35+
36+
assert_eq(
37+
context.historical_header.public_storage_historical_read(15, get_contract_address()),
38+
0,
39+
);
40+
}

0 commit comments

Comments
 (0)