From aec2b1e93460b3d2f26e00b862935f3460261f9e Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 23 Jan 2024 13:27:08 +0000 Subject: [PATCH] WIP --- yarn-project/aztec-nr/aztec/src/abi.nr | 6 ++-- yarn-project/aztec-nr/aztec/src/context.nr | 32 +++++++++---------- .../aztec/src/history/contract_inclusion.nr | 2 +- .../aztec/src/history/note_inclusion.nr | 2 +- .../aztec/src/history/nullifier_inclusion.nr | 2 +- .../src/history/nullifier_non_inclusion.nr | 2 +- .../src/history/public_value_inclusion.nr | 2 +- .../aztec/src/oracle/get_block_header.nr | 12 +++---- .../crates/types/src/abis/global_variables.nr | 16 +++++++++- .../src/crates/types/src/constants.nr | 2 +- .../src/crates/types/src/header.nr | 12 +++++++ .../types/src/partial_state_reference.nr | 16 +++++++++- .../src/crates/types/src/state_reference.nr | 10 ++++++ 13 files changed, 83 insertions(+), 33 deletions(-) diff --git a/yarn-project/aztec-nr/aztec/src/abi.nr b/yarn-project/aztec-nr/aztec/src/abi.nr index b6f5cfa92a97..cd8ba99b3f33 100644 --- a/yarn-project/aztec-nr/aztec/src/abi.nr +++ b/yarn-project/aztec-nr/aztec/src/abi.nr @@ -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 @@ -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, } @@ -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, } diff --git a/yarn-project/aztec-nr/aztec/src/context.nr b/yarn-project/aztec-nr/aztec/src/context.nr index 6329a9d541f1..b2e413ede3a5 100644 --- a/yarn-project/aztec-nr/aztec/src/context.nr +++ b/yarn-project/aztec-nr/aztec/src/context.nr @@ -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, @@ -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, @@ -80,7 +83,7 @@ struct PrivateContext { new_l2_to_l1_msgs : BoundedVec, // docs:end:private-context - block_header: BlockHeader, + block_header: Header, // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) // encrypted_logs_preimages: Vec, @@ -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) } @@ -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) @@ -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 { @@ -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, @@ -475,7 +475,7 @@ struct PublicContext { unencrypted_logs_hash: BoundedVec, unencrypted_logs_preimages_length: Field, - block_header: BlockHeader, + block_header: Header, prover_address: AztecAddress, } @@ -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) diff --git a/yarn-project/aztec-nr/aztec/src/history/contract_inclusion.nr b/yarn-project/aztec-nr/aztec/src/history/contract_inclusion.nr index 053fb5734b0d..ae716e999860 100644 --- a/yarn-project/aztec-nr/aztec/src/history/contract_inclusion.nr +++ b/yarn-project/aztec-nr/aztec/src/history/contract_inclusion.nr @@ -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" ); diff --git a/yarn-project/aztec-nr/aztec/src/history/note_inclusion.nr b/yarn-project/aztec-nr/aztec/src/history/note_inclusion.nr index d156618d3260..4fdd444a6a2f 100644 --- a/yarn-project/aztec-nr/aztec/src/history/note_inclusion.nr +++ b/yarn-project/aztec-nr/aztec/src/history/note_inclusion.nr @@ -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. 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 43976dbd92cd..c7e6098630a0 100644 --- a/yarn-project/aztec-nr/aztec/src/history/nullifier_inclusion.nr +++ b/yarn-project/aztec-nr/aztec/src/history/nullifier_inclusion.nr @@ -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 diff --git a/yarn-project/aztec-nr/aztec/src/history/nullifier_non_inclusion.nr b/yarn-project/aztec-nr/aztec/src/history/nullifier_non_inclusion.nr index f4cb297d17ad..8f14ea909fbb 100644 --- a/yarn-project/aztec-nr/aztec/src/history/nullifier_non_inclusion.nr +++ b/yarn-project/aztec-nr/aztec/src/history/nullifier_non_inclusion.nr @@ -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" ); diff --git a/yarn-project/aztec-nr/aztec/src/history/public_value_inclusion.nr b/yarn-project/aztec-nr/aztec/src/history/public_value_inclusion.nr index 45f2c5450f66..47c8cbe6676a 100644 --- a/yarn-project/aztec-nr/aztec/src/history/public_value_inclusion.nr +++ b/yarn-project/aztec-nr/aztec/src/history/public_value_inclusion.nr @@ -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 diff --git a/yarn-project/aztec-nr/aztec/src/oracle/get_block_header.nr b/yarn-project/aztec-nr/aztec/src/oracle/get_block_header.nr index 71f30dde2bc7..74035a57fdb9 100644 --- a/yarn-project/aztec-nr/aztec/src/oracle/get_block_header.nr +++ b/yarn-project/aztec-nr/aztec/src/oracle/get_block_header.nr @@ -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::{ @@ -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 @@ -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" ); diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/global_variables.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/global_variables.nr index cc28af92252d..c8b5ba74d0bf 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/global_variables.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/global_variables.nr @@ -1,7 +1,10 @@ use dep::std::cmp::Eq; use crate::{ constants::GENERATOR_INDEX__GLOBAL_VARIABLES, - traits::Hash, + traits::{ + Empty, + Hash, + }, }; struct GlobalVariables { @@ -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, + } + } +} diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr index e041e959567c..9125e22efbae 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr @@ -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; diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr index 478494b666e1..44c443e21016 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr @@ -19,6 +19,7 @@ use crate::{ StateReference, STATE_REFERENCE_LENGTH, }, + traits::Empty, utils::{ arr_copy_slice, bounded_vec::BoundedVec, @@ -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(), + } + } +} diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/partial_state_reference.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/partial_state_reference.nr index 6e31cbf0ad4f..a1b6a45d0840 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/partial_state_reference.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/partial_state_reference.nr @@ -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, @@ -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(), + } + } +} diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/state_reference.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/state_reference.nr index f09bb51a89e1..30c039d44651 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/state_reference.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/state_reference.nr @@ -7,6 +7,7 @@ use crate::{ PartialStateReference, PARTIAL_STATE_REFERENCE_LENGTH, }, + traits::Empty, utils::{ arr_copy_slice, bounded_vec::BoundedVec, @@ -49,3 +50,12 @@ impl StateReference { } } } + +impl Empty for StateReference { + fn empty() -> Self { + Self { + l1_to_l2_message_tree: AppendOnlyTreeSnapshot::empty(), + partial: PartialStateReference::empty(), + } + } +}