Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Dec 6, 2023
1 parent 3013354 commit 74919ca
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 122 deletions.
2 changes: 1 addition & 1 deletion docs/docs/dev_docs/contracts/syntax/context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The call context contains information about the current call being made:

Another structure that is contained within the context is the Block Header object. This object is a special one as it contains all of the roots of Aztec's data trees.

#include_code block-header /yarn-project/aztec-nr/aztec/src/abi.nr rust
#include_code block-header /yarn-project/noir-protocol-circuits/src/crates/types/src/abis/block_header.nr rust

### Contract Deployment Data

Expand Down
57 changes: 0 additions & 57 deletions yarn-project/aztec-nr/aztec/src/abi.nr
Original file line number Diff line number Diff line change
Expand Up @@ -141,63 +141,6 @@ impl CallContext {
}
}

// docs:start:block-header
struct BlockHeader {
note_hash_tree_root : Field,
nullifier_tree_root : Field,
contract_tree_root : Field,
l1_to_l2_messages_tree_root : Field,
archive_root: Field,
public_data_tree_root: Field,
global_variables_hash: Field,
}
// docs:end:block-header

impl BlockHeader {
// NOTE: this order must match the order in `private_circuit_public_inputs.hpp`
pub fn serialize(self) -> [Field; BLOCK_HEADER_LENGTH] {
[
self.note_hash_tree_root,
self.nullifier_tree_root,
self.contract_tree_root,
self.l1_to_l2_messages_tree_root,
self.archive_root,
self.public_data_tree_root,
self.global_variables_hash,
]
}

pub fn deserialize(deserialized: [Field; BLOCK_HEADER_LENGTH]) -> Self {
BlockHeader {
note_hash_tree_root: deserialized[0],
nullifier_tree_root: deserialized[1],
contract_tree_root: deserialized[2],
l1_to_l2_messages_tree_root: deserialized[3],
archive_root: deserialized[4],
public_data_tree_root: deserialized[5],
global_variables_hash: deserialized[6],
}
}

pub fn empty() -> Self {
Self { note_hash_tree_root: 0, nullifier_tree_root: 0, contract_tree_root: 0, l1_to_l2_messages_tree_root: 0, archive_root: 0, public_data_tree_root: 0, global_variables_hash: 0 }
}

pub fn block_hash(self) -> Field {
// TODO(#3442): Unify the ordering in `BlockHeader::serialize` function and the ordering
// in the block hash preimage --> This requires changes in the circuits.
let inputs = [
self.global_variables_hash,
self.note_hash_tree_root,
self.nullifier_tree_root,
self.contract_tree_root,
self.l1_to_l2_messages_tree_root,
self.public_data_tree_root
];
pedersen_hash(inputs, GENERATOR_INDEX__BLOCK_HASH)
}
}

struct FunctionData {
function_selector: Field,
is_internal: bool,
Expand Down
76 changes: 40 additions & 36 deletions yarn-project/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
use dep::protocol_types::constants::{
EMPTY_NULLIFIED_COMMITMENT,
MAX_NEW_COMMITMENTS_PER_CALL,
MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
MAX_NEW_NULLIFIERS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
MAX_READ_REQUESTS_PER_CALL,
MAX_PENDING_READ_REQUESTS_PER_CALL,
NUM_FIELDS_PER_SHA256,
RETURN_VALUES_LENGTH,
use dep::protocol_types::{
abis::{
block_header::BlockHeader,
call_context::CallContext,
function_data::FunctionData,
private_circuit_public_inputs::PrivateCircuitPublicInputs,
public_circuit_public_inputs::PublicCircuitPublicInputs,
},
constants::{
EMPTY_NULLIFIED_COMMITMENT,
MAX_NEW_COMMITMENTS_PER_CALL,
MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
MAX_NEW_NULLIFIERS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
MAX_READ_REQUESTS_PER_CALL,
MAX_PENDING_READ_REQUESTS_PER_CALL,
NUM_FIELDS_PER_SHA256,
RETURN_VALUES_LENGTH,
},
contrakt::deployment_data::ContractDeploymentData,
hash::hash_args,
};

use crate::abi;
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
// use dep::std::collections::vec::Vec;

use crate::abi::{
hash_args,
CallContext,
ContractDeploymentData,
BlockHeader,
FunctionData,
PrivateCircuitPublicInputs,
PublicCircuitPublicInputs,
PrivateContextInputs,
PublicContextInputs,
};

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
// use dep::std::collections::vec::Vec;

// l1 to l2 messaging
use crate::messaging::process_l1_to_l2_message;
use crate::private_call_stack_item::PrivateCallStackItem;
Expand All @@ -54,7 +58,7 @@ use dep::std::option::Option;
// When finished, one can call .finish() to convert back to the abi
struct PrivateContext {
// docs:start:private-context
inputs: abi::PrivateContextInputs,
inputs: PrivateContextInputs,

args_hash : Field,
return_values : BoundedVec<Field, RETURN_VALUES_LENGTH>,
Expand All @@ -79,7 +83,7 @@ struct PrivateContext {
}

impl PrivateContext {
pub fn new(inputs: abi::PrivateContextInputs, args_hash: Field) -> PrivateContext {
pub fn new(inputs: PrivateContextInputs, args_hash: Field) -> PrivateContext {
PrivateContext {
inputs: inputs,

Expand Down Expand Up @@ -133,14 +137,14 @@ impl PrivateContext {
get_block_header(block_number, self)
}

pub fn finish(self) -> abi::PrivateCircuitPublicInputs {
pub fn finish(self) -> PrivateCircuitPublicInputs {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
let encrypted_logs_hash = [0; NUM_FIELDS_PER_SHA256];
let unencrypted_logs_hash = [0; NUM_FIELDS_PER_SHA256];
let encrypted_log_preimages_length = 0;
let unencrypted_log_preimages_length = 0;

let priv_circuit_pub_inputs = abi::PrivateCircuitPublicInputs {
let priv_circuit_pub_inputs = PrivateCircuitPublicInputs {
call_context: self.inputs.call_context,
args_hash: self.args_hash,
return_values: self.return_values.storage,
Expand Down Expand Up @@ -422,7 +426,7 @@ use crate::abi::{
};

struct PublicContext {
inputs: abi::PublicContextInputs,
inputs: PublicContextInputs,

args_hash : Field,
return_values : BoundedVec<Field, RETURN_VALUES_LENGTH>,
Expand All @@ -432,9 +436,9 @@ struct PublicContext {
public_call_stack: BoundedVec<Field, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL>,

new_commitments: BoundedVec<Field, MAX_NEW_COMMITMENTS_PER_CALL>,
new_nullifiers: BoundedVec<Field, crate::abi::MAX_NEW_NULLIFIERS_PER_CALL>,
new_nullifiers: BoundedVec<Field, MAX_NEW_NULLIFIERS_PER_CALL>,

new_l2_to_l1_msgs: BoundedVec<Field, crate::abi::MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,
new_l2_to_l1_msgs: BoundedVec<Field, MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,

unencrypted_logs_hash: BoundedVec<Field, NUM_FIELDS_PER_SHA256>,
unencrypted_logs_preimages_length: Field,
Expand All @@ -444,7 +448,7 @@ struct PublicContext {
}

impl PublicContext {
pub fn new(inputs: abi::PublicContextInputs, args_hash: Field) -> PublicContext {
pub fn new(inputs: PublicContextInputs, args_hash: Field) -> PublicContext {
let empty_storage_read = ContractStorageRead::empty();
let empty_storage_update = ContractStorageUpdateRequest::empty();
PublicContext {
Expand Down Expand Up @@ -507,14 +511,14 @@ impl PublicContext {
self.inputs.public_global_variables.timestamp
}

pub fn finish(self) -> abi::PublicCircuitPublicInputs {
pub fn finish(self) -> PublicCircuitPublicInputs {
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165)
let unencrypted_logs_hash = [0; NUM_FIELDS_PER_SHA256];
let unencrypted_log_preimages_length = 0;


// Compute the public call stack hashes
let pub_circuit_pub_inputs = abi::PublicCircuitPublicInputs {
let pub_circuit_pub_inputs = PublicCircuitPublicInputs {
call_context: self.inputs.call_context, // Done
args_hash: self.args_hash, // Done
contract_storage_update_requests: self.contract_storage_update_requests.storage,
Expand Down Expand Up @@ -548,7 +552,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_messages_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.block.l1_to_l2_messages_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, EMPTY_NULLIFIED_COMMITMENT)
Expand All @@ -572,7 +576,7 @@ impl PublicContext {
function_selector: Field,
args: [Field; ARGS_COUNT],
) -> [Field; RETURN_VALUES_LENGTH] {
let args_hash = abi::hash_args(args);
let args_hash = hash_args(args);
assert(args_hash == arguments::pack_arguments(args));
call_public_function_internal(
contract_address,
Expand Down
36 changes: 9 additions & 27 deletions yarn-project/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
use dep::std::hash::{pedersen_hash_with_separator, sha256};
use dep::protocol_types::constants::{
GENERATOR_INDEX__SIGNATURE_PAYLOAD,
GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET,
};

pub fn sha256_to_field<N>(bytes_to_hash: [u8; N]) -> Field {
let sha256_hashed = sha256(bytes_to_hash);

// Convert it to a field element
let mut v = 1;
let mut high = 0 as Field;
let mut low = 0 as Field;

for i in 0..16 {
high = high + (sha256_hashed[15 - i] as Field) * v;
low = low + (sha256_hashed[16 + 15 - i] as Field) * v;
v = v * 256;
use dep::protocol_types::{
constants::{
GENERATOR_INDEX__SIGNATURE_PAYLOAD,
GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET,
},
hash::{
sha256_to_field,
pedersen_hash,
}

// Abuse that a % p + b % p = (a + b) % p and that low < p
let hash_in_a_field = low + high * v;

hash_in_a_field
}
};

pub fn compute_secret_hash(secret: Field) -> Field {
// TODO(#1205) This is probably not the right index to use
pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)
}

pub fn pedersen_hash<N>(inputs: [Field; N], hash_index: u32) -> Field {
pedersen_hash_with_separator(inputs, hash_index)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::hash::pedersen_hash;

use crate::block::Block;

// docs:start:block-header
struct BlockHeader {
archive_root : Field,
block : Block,
// Private data
// This is marked in the cpp code as an enhancement
private_kernel_vk_tree_root : Field,
}
// docs:end:block-header

impl BlockHeader {
fn assert_is_zero(self) {
Expand Down Expand Up @@ -37,4 +41,18 @@ impl BlockHeader {
fn contract_tree_root(self) -> Field {
self.block.contract_tree_root
}

pub fn block_hash(self) -> Field {
// TODO(#3442): Unify the ordering in `BlockHeader::serialize` function and the ordering
// in the block hash preimage --> This requires changes in the circuits.
let inputs = [
self.global_variables_hash,
self.note_hash_tree_root,
self.nullifier_tree_root,
self.contract_tree_root,
self.l1_to_l2_messages_tree_root,
self.public_data_tree_root
];
pedersen_hash(inputs, GENERATOR_INDEX__BLOCK_HASH)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::constants::{
GENERATOR_INDEX__FUNCTION_ARGS,
};

use dep::std::hash::sha256;
use dep::std::hash::{pedersen_hash_with_separator, sha256};

pub fn sha256_to_field<N>(bytes_to_hash: [u8; N]) -> Field {
let sha256_hashed = sha256(bytes_to_hash);
Expand Down Expand Up @@ -294,4 +294,8 @@ pub fn compute_unique_siloed_commitments<N>(first_nullifier: Field, siloed_commi
}
}
unique_siloed_commitments
}

pub fn pedersen_hash<N>(inputs: [Field; N], hash_index: u32) -> Field {
pedersen_hash_with_separator(inputs, hash_index)
}

0 comments on commit 74919ca

Please sign in to comment.