Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 23, 2024
1 parent 81b4087 commit aec2b1e
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 33 deletions.
6 changes: 3 additions & 3 deletions yarn-project/aztec-nr/aztec/src/abi.nr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use dep::protocol_types::{
abis::{
block_header::BlockHeader,
call_context::CallContext,
private_circuit_public_inputs::PrivateCircuitPublicInputs,
public_circuit_public_inputs::PublicCircuitPublicInputs,
},
contrakt::deployment_data::ContractDeploymentData,
hash::hash_args,
header::Header,
};

// docs:start:private-global-variables
Expand Down Expand Up @@ -41,7 +41,7 @@ impl PublicGlobalVariables {
// docs:start:private-context-inputs
struct PrivateContextInputs {
call_context : CallContext,
block_header: BlockHeader,
block_header: Header,
contract_deployment_data: ContractDeploymentData,
private_global_variables: PrivateGlobalVariables,
}
Expand All @@ -51,7 +51,7 @@ struct PrivateContextInputs {
// docs:start:public-context-inputs
struct PublicContextInputs {
call_context: CallContext,
block_header: BlockHeader,
block_header: Header,

public_global_variables: PublicGlobalVariables,
}
Expand Down
32 changes: 16 additions & 16 deletions yarn-project/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ use crate::{
};
use dep::protocol_types::{
abis::{
block_header::BlockHeader,
append_only_tree_snapshot::AppendOnlyTreeSnapshot,
call_context::CallContext,
global_variables::GlobalVariables,
function_data::FunctionData,
function_selector::FunctionSelector,
private_circuit_public_inputs::PrivateCircuitPublicInputs,
Expand Down Expand Up @@ -51,7 +52,9 @@ use dep::protocol_types::{
storage_update_request::StorageUpdateRequest,
},
hash::hash_args,
header::Header,
grumpkin_point::GrumpkinPoint,
state_reference::StateReference,
};
use dep::std::{
grumpkin_scalar::GrumpkinScalar,
Expand Down Expand Up @@ -80,7 +83,7 @@ struct PrivateContext {
new_l2_to_l1_msgs : BoundedVec<Field, MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,
// docs:end:private-context

block_header: BlockHeader,
block_header: Header,

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
// encrypted_logs_preimages: Vec<Field>,
Expand Down Expand Up @@ -137,7 +140,7 @@ impl PrivateContext {
self.inputs.call_context.function_selector
}

pub fn get_block_header(self, block_number: u32) -> BlockHeader {
pub fn get_block_header(self, block_number: u32) -> Header {
get_block_header(block_number, self)
}

Expand Down Expand Up @@ -226,7 +229,7 @@ impl PrivateContext {
)
// docs:end:context_consume_l1_to_l2_message
{
let nullifier = process_l1_to_l2_message(self.block_header.l1_to_l2_message_tree_root, self.this_address(), self.this_portal_address(), self.chain_id(), self.version(), msg_key, content, secret);
let nullifier = process_l1_to_l2_message(self.block_header.state.l1_to_l2_message_tree.root, self.this_address(), self.this_portal_address(), self.chain_id(), self.version(), msg_key, content, secret);

// Push nullifier (and the "commitment" corresponding to this can be "empty")
self.push_new_nullifier(nullifier, 0)
Expand Down Expand Up @@ -310,15 +313,12 @@ impl PrivateContext {
unencrypted_logs_hash: reader.read_array([0; NUM_FIELDS_PER_SHA256]),
encrypted_log_preimages_length: reader.read(),
unencrypted_log_preimages_length: reader.read(),
block_header: BlockHeader{
// Must match order in `private_circuit_public_inputs.hpp`
note_hash_tree_root : reader.read(),
nullifier_tree_root : reader.read(),
contract_tree_root : reader.read(),
l1_to_l2_message_tree_root : reader.read(),
archive_root : reader.read(),
public_data_tree_root: reader.read(),
global_variables_hash: reader.read(),
block_header: Header{
// Must match order in `private_circuit_public_inputs.nr`
last_archive: reader.read_struct(AppendOnlyTreeSnapshot::deserialize),
body_hash: reader.read_array([0; NUM_FIELDS_PER_SHA256]),
state: reader.read_struct(StateReference::deserialize),
global_variables: reader.read_struct(GlobalVariables::deserialize),
},
contract_deployment_data: ContractDeploymentData {
deployer_public_key: GrumpkinPoint {
Expand Down Expand Up @@ -426,7 +426,7 @@ impl PrivateContext {
new_l2_to_l1_msgs:[0; MAX_NEW_L2_TO_L1_MSGS_PER_CALL],
unencrypted_logs_hash:[0; NUM_FIELDS_PER_SHA256],
unencrypted_log_preimages_length: 0,
block_header: BlockHeader::empty(),
block_header: Header::empty(),
prover_address: AztecAddress::zero(),
},
is_execution_request: true,
Expand Down Expand Up @@ -475,7 +475,7 @@ struct PublicContext {
unencrypted_logs_hash: BoundedVec<Field, NUM_FIELDS_PER_SHA256>,
unencrypted_logs_preimages_length: Field,

block_header: BlockHeader,
block_header: Header,
prover_address: AztecAddress,
}

Expand Down Expand Up @@ -596,7 +596,7 @@ impl PublicContext {
// Note this returns self to get around an issue where mutable structs do not maintain mutations unless reassigned
pub fn consume_l1_to_l2_message(&mut self, msg_key: Field, content: Field, secret: Field) {
let this = (*self).this_address();
let nullifier = process_l1_to_l2_message(self.block_header.l1_to_l2_message_tree_root, this, self.this_portal_address(), self.chain_id(), self.version(), msg_key, content, secret);
let nullifier = process_l1_to_l2_message(self.block_header.state.l1_to_l2_message_tree.root, this, self.this_portal_address(), self.chain_id(), self.version(), msg_key, content, secret);

// Push nullifier (and the "commitment" corresponding to this can be "empty")
self.push_new_nullifier(nullifier, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn prove_contract_inclusion(

// 6) Prove that the leaf is in the contract tree
assert(
block_header.contract_tree_root
block_header.state.partial.contract_tree.root
== compute_merkle_root(contract_leaf, witness.index, witness.path), "Proving contract inclusion failed"
);

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/aztec/src/history/note_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn prove_note_commitment_inclusion(

// 3) Prove that the commitment is in the note hash tree
assert(
block_header.note_hash_tree_root
block_header.state.partial.note_hash_tree.root
== compute_merkle_root(note_commitment, witness.index, witness.path), "Proving note inclusion failed"
);
// --> Now we have traversed the trees all the way up to archive root.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn prove_nullifier_inclusion(

// 5) Prove that the nullifier is in the nullifier tree
assert(
block_header.nullifier_tree_root
block_header.state.partial.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn prove_nullifier_non_inclusion(
// 3.a) Compute the low nullifier leaf and prove that it is in the nullifier tree
let low_nullifier_leaf = witness.leaf_preimage.hash();
assert(
block_header.nullifier_tree_root
block_header.state.partial.nullifier_tree.root
== compute_merkle_root(low_nullifier_leaf, witness.index, witness.path), "Proving nullifier non-inclusion failed: Could not prove low nullifier inclusion"
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn prove_public_value_inclusion(

// 5) Prove that the leaf we validated is in the public data tree
assert(
block_header.public_data_tree_root
block_header.state.partial.public_data_tree.root
== compute_merkle_root(preimage.hash(), witness.index, witness.path), "Proving public value inclusion failed"
);
// --> Now we have traversed the trees all the way up to archive root and that way verified that a specific
Expand Down
12 changes: 6 additions & 6 deletions yarn-project/aztec-nr/aztec/src/oracle/get_block_header.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::std::merkle::compute_merkle_root;
use dep::protocol_types::{
abis::block_header::BlockHeader,
constants::HEADER_LENGTH,
header::Header,
};

use crate::{
Expand All @@ -20,15 +20,15 @@ unconstrained pub fn get_nullifier_root_block_number(nullifier_tree_root: Field)
#[oracle(getBlockHeader)]
fn get_block_header_oracle(_block_number: u32) -> [Field; HEADER_LENGTH] {}

unconstrained pub fn get_block_header_internal(block_number: u32) -> BlockHeader {
unconstrained pub fn get_block_header_internal(block_number: u32) -> Header {
let block_header = get_block_header_oracle(block_number);
BlockHeader::deserialize(block_header)
Header::deserialize(block_header)
}

pub fn get_block_header(block_number: u32, context: PrivateContext) -> BlockHeader {
pub fn get_block_header(block_number: u32, context: PrivateContext) -> Header {
// 1) Get block number corresponding to block header inside context
// Using nullifier tree root to get the block header block number because that changes in every block (every tx emits a nullifier).
let block_header_block_number = get_nullifier_root_block_number(context.block_header.nullifier_tree_root);
let block_header_block_number = get_nullifier_root_block_number(context.block_header.state.partial.nullifier_tree.root);

// 2) Check that the block header 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 archive root from the header would not "contain" the block we want to prove against
Expand All @@ -47,7 +47,7 @@ pub fn get_block_header(block_number: u32, context: PrivateContext) -> BlockHead

// 6) Check that the block is in the archive (i.e. the witness is valid)
assert(
context.block_header.archive_root
context.block_header.last_archive.root
== compute_merkle_root(block_hash, witness.index, witness.path), "Proving membership of a block in archive failed"
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use dep::std::cmp::Eq;
use crate::{
constants::GENERATOR_INDEX__GLOBAL_VARIABLES,
traits::Hash,
traits::{
Empty,
Hash,
},
};

struct GlobalVariables {
Expand Down Expand Up @@ -54,3 +57,14 @@ impl Hash for GlobalVariables {
)
}
}

impl Empty for GlobalVariables {
fn empty() -> Self {
Self {
chain_id: 0,
version: 0,
block_number: 0,
timestamp: 0,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ global CONTRACT_STORAGE_READ_LENGTH: Field = 2;
// Change this ONLY if you have changed the PublicCircuitPublicInputs structure.
global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: Field = 190;
global GET_NOTES_ORACLE_RETURN_LENGTH: Field = 674;
global CALL_PRIVATE_FUNCTION_RETURN_SIZE: Field = 195;
global CALL_PRIVATE_FUNCTION_RETURN_SIZE: Field = 206;
global PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: Field = 98;
global PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: Field = 188;
global COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP: Field = 2048;
Expand Down
12 changes: 12 additions & 0 deletions yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
StateReference,
STATE_REFERENCE_LENGTH,
},
traits::Empty,
utils::{
arr_copy_slice,
bounded_vec::BoundedVec,
Expand Down Expand Up @@ -82,3 +83,14 @@ impl Header {
], GENERATOR_INDEX__BLOCK_HASH)
}
}

impl Empty for Header {
fn empty() -> Self {
Self {
last_archive: AppendOnlyTreeSnapshot::empty(),
body_hash: [0; NUM_FIELDS_PER_SHA256],
state: StateReference::empty(),
global_variables: GlobalVariables::empty(),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot;
use crate::{
abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot,
traits::Empty,
};

struct PartialStateReference {
note_hash_tree: AppendOnlyTreeSnapshot,
Expand Down Expand Up @@ -52,3 +55,14 @@ impl PartialStateReference {
}
}
}

impl Empty for PartialStateReference {
fn empty() -> Self {
Self {
note_hash_tree: AppendOnlyTreeSnapshot::empty(),
nullifier_tree: AppendOnlyTreeSnapshot::empty(),
contract_tree: AppendOnlyTreeSnapshot::empty(),
public_data_tree: AppendOnlyTreeSnapshot::empty(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
PartialStateReference,
PARTIAL_STATE_REFERENCE_LENGTH,
},
traits::Empty,
utils::{
arr_copy_slice,
bounded_vec::BoundedVec,
Expand Down Expand Up @@ -49,3 +50,12 @@ impl StateReference {
}
}
}

impl Empty for StateReference {
fn empty() -> Self {
Self {
l1_to_l2_message_tree: AppendOnlyTreeSnapshot::empty(),
partial: PartialStateReference::empty(),
}
}
}

0 comments on commit aec2b1e

Please sign in to comment.