Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Feb 4, 2025
1 parent 1a94a13 commit 93133f7
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 1 deletion.
13 changes: 13 additions & 0 deletions noir-projects/aztec-nr/aztec/src/oracle/block_header.nr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ pub fn get_block_header_at(block_number: u32, context: PrivateContext) -> BlockH
// Note: We subtract 1 because the last_archive root is the root of the archive after applying the previous block
let last_archive_block_number = historical_block_number - 1;

crate::oracle::debug_log::debug_log_format(
"historical header block nr {}",
[historical_block_number as Field],
);
crate::oracle::debug_log::debug_log_format(
"last archive block nr {}",
[last_archive_block_number as Field],
);
crate::oracle::debug_log::debug_log_format(
"getting block header block nr {}",
[block_number as Field],
);

// 2) Check that the last archive block number is more than or equal to the block number we want to prove against
// We could not perform the proof otherwise because the last archive root from the header would not "contain"
// the header we want to prove against
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// A demonstration of inclusion and non-inclusion proofs.
use dep::aztec::macros::aztec;

mod test;

#[aztec]
pub contract InclusionProofs {
use dep::aztec::encrypted_logs::log_assembly_strategies::default_aes128::note::encode_and_encrypt_note;
use dep::aztec::prelude::{AztecAddress, Map, NoteGetterOptions, PrivateSet, PublicMutable};

use dep::aztec::{
macros::{functions::{initializer, private, public}, storage::storage},
note::note_getter_options::NoteStatus,
note::{note_getter_options::NoteStatus, note_viewer_options::NoteViewerOptions},
};
// docs:start:value_note_imports
use dep::value_note::value_note::ValueNote;
Expand Down Expand Up @@ -40,6 +42,13 @@ pub contract InclusionProofs {
}
// docs:end:create_note

unconstrained fn get_note(owner: AztecAddress) -> ValueNote {
let options = NoteViewerOptions::new().set_limit(1);
let note = storage.private_values.at(owner).view_notes(options).get(0);

note
}

#[private]
fn test_note_inclusion(
owner: AztecAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
use crate::InclusionProofs;
use dep::aztec::{
oracle::execution::get_contract_address,
prelude::AztecAddress,
test::helpers::{cheatcodes, test_environment::TestEnvironment},
};

global INITIAL_VALUE: Field = 69;

pub unconstrained fn setup(
initial_value: Field,
) -> (&mut TestEnvironment, AztecAddress, AztecAddress) {
// Setup env, generate keys
let mut env = TestEnvironment::new();
let owner = env.create_account();
env.impersonate(owner);
// Deploy contract and initialize
let initializer = InclusionProofs::interface().constructor(initial_value);
let inclusion_proofs_contract =
env.deploy_self("InclusionProofs").with_public_void_initializer(initializer);
let contract_address = inclusion_proofs_contract.to_address();
env.advance_block_by(1);
(&mut env, contract_address, owner)
}

#[test]
unconstrained fn note_inclusion_and_validity() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let block_number = env.block_number();

let NOTE_VALUE = 69;
InclusionProofs::at(contract_address).create_note(owner, NOTE_VALUE).call(&mut env.private());

env.advance_block_by(2);

let current_contract_address = get_contract_address();
cheatcodes::set_contract_address(contract_address);

let note = InclusionProofs::get_note(owner);
cheatcodes::set_contract_address(current_contract_address);

assert(note.owner.eq(owner));
assert(note.value.eq(NOTE_VALUE));

InclusionProofs::at(contract_address)
.test_note_inclusion(owner, true, block_number, false)
.call(&mut env.private());
InclusionProofs::at(contract_address).test_note_inclusion(owner, false, 0, false).call(
&mut env.private(),
);

InclusionProofs::at(contract_address)
.test_note_not_nullified(owner, true, block_number, false)
.call(&mut env.private());
InclusionProofs::at(contract_address).test_note_not_nullified(owner, false, 0, false).call(
&mut env.private(),
);

InclusionProofs::at(contract_address).test_note_validity(owner, true, block_number, false).call(
&mut env.private(),
);
InclusionProofs::at(contract_address).test_note_validity(owner, false, 0, false).call(
&mut env.private(),
);
}

#[test]
unconstrained fn nullify_note_flow() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let note_creation_block_number = env.block_number();

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());

env.advance_block_by(1);

InclusionProofs::at(contract_address).nullify_note(owner).call(&mut env.private());

env.advance_block_by(1);

InclusionProofs::at(contract_address)
.test_note_inclusion(owner, true, note_creation_block_number, true)
.call(&mut env.private());

InclusionProofs::at(contract_address).test_note_inclusion(owner, false, 0, true).call(
&mut env.private(),
);

InclusionProofs::at(contract_address)
.test_note_not_nullified(owner, true, note_creation_block_number, true)
.call(&mut env.private());
InclusionProofs::at(contract_address)
.test_note_validity(owner, true, note_creation_block_number, true)
.call(&mut env.private());
}

#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
unconstrained fn validity_after_nullified() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());

env.advance_block_by(1);

let note_nullification_block_number = env.block_number();

InclusionProofs::at(contract_address).nullify_note(owner).call(&mut env.private());

env.advance_block_by(1);

InclusionProofs::at(contract_address)
.test_note_validity(owner, true, note_nullification_block_number, true)
.call(&mut env.private());
}

#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
unconstrained fn validity_after_nullified_1() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());

env.advance_block_by(1);

let note_nullification_block_number = env.block_number();

InclusionProofs::at(contract_address).nullify_note(owner).call(&mut env.private());

env.advance_block_by(1);

// TODO: env.block_number() should actually return the private context inputs block number, not the block that is currently being built !
// Change env.block_number to subtract one, then just use `env.block_number()`.
InclusionProofs::at(contract_address)
.test_note_not_nullified(owner, true, env.block_number() - 1, true)
.call(&mut env.private());
}

#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
unconstrained fn validity_after_nullified_2() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let block_number = env.block_number();

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());

env.advance_block_by(1);

InclusionProofs::at(contract_address).nullify_note(owner).call(&mut env.private());

env.advance_block_by(1);

InclusionProofs::at(contract_address).test_note_not_nullified(owner, false, 0, true).call(
&mut env.private(),
);
}

#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
unconstrained fn validity_after_nullified_3() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());

env.advance_block_by(1);

InclusionProofs::at(contract_address).nullify_note(owner).call(&mut env.private());

env.advance_block_by(1);

InclusionProofs::at(contract_address).test_note_validity(owner, false, 0, true).call(
&mut env.private(),
);
}

#[test(should_fail_with = "not found in NOTE_HASH_TREE")]
unconstrained fn note_inclusion_fail_case() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);
let random_owner = AztecAddress::from_field(dep::aztec::oracle::random::random());

let block_number = env.block_number();

env.advance_block_by(2);

InclusionProofs::at(contract_address)
.test_note_inclusion_fail_case(random_owner, true, block_number)
.call(&mut env.private());
}

#[test(should_fail_with = "not found in NOTE_HASH_TREE")]
unconstrained fn note_inclusion_fail_case_1() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);
let random_owner = AztecAddress::from_field(dep::aztec::oracle::random::random());

env.advance_block_by(2);

InclusionProofs::at(contract_address)
.test_note_inclusion_fail_case(random_owner, false, 0)
.call(&mut env.private());
}

#[test]
unconstrained fn nullifier_inclusion_block_number() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);
env.impersonate(owner);

// first nullifier
let nullifier = 6969 + 1;
let block_number = env.block_number() - 1;

InclusionProofs::at(contract_address)
.test_nullifier_inclusion(nullifier, true, block_number)
.call(&mut env.private());
}

#[test]
unconstrained fn nullifier_inclusion() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

// first nullifier
let nullifier = 6969 + 1;
env.impersonate(owner);

InclusionProofs::at(contract_address).test_nullifier_inclusion(nullifier, false, 0).call(
&mut env.private(),
);
}

#[test]
unconstrained fn push_nullifier_public() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let unsiloed_nullifier = 0xffffff;
InclusionProofs::at(contract_address).push_nullifier_public(unsiloed_nullifier).call(
&mut env.public(),
);

env.advance_block_by(1);
InclusionProofs::at(contract_address)
.test_nullifier_inclusion_from_public(unsiloed_nullifier)
.call(&mut env.public());
}

#[test(should_fail_with = "Nullifier witness not found for nullifier")]
unconstrained fn nullifier_non_existence() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

let block_number = env.block_number() - 1;
env.impersonate(owner);
let random_nullifier = dep::aztec::oracle::random::random();

InclusionProofs::at(contract_address)
.test_nullifier_inclusion(random_nullifier, true, block_number)
.call(&mut env.private());
}

#[test(should_fail_with = "Nullifier witness not found for nullifier")]
unconstrained fn nullifier_non_existence_1() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);
let random_nullifier = dep::aztec::oracle::random::random();

InclusionProofs::at(contract_address).test_nullifier_inclusion(random_nullifier, false, 0).call(
&mut env.private(),
);
}

#[test]
unconstrained fn historical_reads() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let block_number = env.block_number() - 1;

InclusionProofs::at(contract_address)
.test_storage_historical_read(INITIAL_VALUE, true, block_number)
.call(&mut env.private());
InclusionProofs::at(contract_address)
.test_storage_historical_read(INITIAL_VALUE, false, 0)
.call(&mut env.private());

InclusionProofs::at(contract_address)
.test_storage_historical_read_unset_slot(block_number)
.call(&mut env.private());
}

#[test(should_fail_with = "Assertion failed: Actual public value does not match expected")]
unconstrained fn historical_read_failure() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let block_number = env.block_number() - 1;

InclusionProofs::at(contract_address).test_storage_historical_read(0, true, block_number).call(
&mut env.private(),
);
}

#[test]
unconstrained fn test_contract_initialization() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let block_number = env.block_number() - 1;

InclusionProofs::at(contract_address)
.test_contract_inclusion(contract_address, block_number, false, true)
.call(&mut env.private());
}
2 changes: 2 additions & 0 deletions yarn-project/txe/src/oracle/txe_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ export class TXE implements TypedOracle {
txEffect.nullifiers.unshift(this.getTxRequestHash());
}

console.log('all nullifiers', txEffect.nullifiers);

txEffect.publicDataWrites = this.publicDataWrites;

const body = new Body([txEffect]);
Expand Down

0 comments on commit 93133f7

Please sign in to comment.