diff --git a/address-note/src/address_note.nr b/address-note/src/address_note.nr index e644b55..ebacf94 100644 --- a/address-note/src/address_note.nr +++ b/address-note/src/address_note.nr @@ -2,22 +2,10 @@ use dep::aztec::log::emit_encrypted_log; // docs:end:encrypted_import use dep::aztec::{ - protocol_types::{ - address::AztecAddress, - traits::Empty - }, - note::{ - note_header::NoteHeader, - note_interface::NoteInterface, - utils::compute_note_hash_for_consumption, - }, - oracle::{ - rand::rand, - nullifier_key::get_nullifier_secret_key, - get_public_key::get_public_key, - }, - hash::pedersen_hash, - context::PrivateContext + protocol_types::{address::AztecAddress, traits::Empty}, + note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption}, + oracle::{rand::rand, nullifier_key::get_nullifier_secret_key, get_public_key::get_public_key}, + hash::pedersen_hash, context::PrivateContext }; global ADDRESS_NOTE_LEN: Field = 3; @@ -88,22 +76,24 @@ impl NoteInterface for AddressNote { context, (*context).this_address(), slot, + Self::get_note_type_id(), encryption_pub_key, self.serialize_content(), ); // docs:end:encrypted } + + fn get_note_type_id() -> Field { + // TODO(#4519): autogenerate + // python -c "print(int(''.join(str(ord(c)) for c in 'AddressNote')))" + 6510010011410111511578111116101 + } } impl AddressNote { pub fn new(address: AztecAddress, owner: AztecAddress) -> Self { let randomness = rand(); - AddressNote { - address, - owner, - randomness, - header: NoteHeader::empty(), - } + AddressNote { address, owner, randomness, header: NoteHeader::empty() } } -// docs:end:address_note_def + // docs:end:address_note_def } diff --git a/authwit/src/account.nr b/authwit/src/account.nr index 790aee2..dc343ea 100644 --- a/authwit/src/account.nr +++ b/authwit/src/account.nr @@ -1,7 +1,7 @@ use dep::aztec::context::{PrivateContext, PublicContext, Context}; use dep::aztec::state_vars::{map::Map, public_state::PublicState}; -use crate::entrypoint::EntrypointPayload; +use crate::entrypoint::{app::AppPayload, fee::FeePayload}; use crate::auth::IS_VALID_SELECTOR; struct AccountActions { @@ -11,57 +11,79 @@ struct AccountActions { } impl AccountActions { - pub fn init(context: Context, approved_action_storage_slot: Field, is_valid_impl: fn(&mut PrivateContext, Field) -> bool) -> Self { - AccountActions { - context: context, - is_valid_impl: is_valid_impl, - approved_action: Map::new( - context, - approved_action_storage_slot, - |context, slot| { + pub fn init( + context: Context, + approved_action_storage_slot: Field, + is_valid_impl: fn(&mut PrivateContext, Field) -> bool + ) -> Self { + AccountActions { + context, + is_valid_impl, + approved_action: Map::new( + context, + approved_action_storage_slot, + |context, slot| { PublicState::new(context, slot) - }, - ), + } + ) + } } - } - pub fn private(context: &mut PrivateContext, approved_action_storage_slot: Field, is_valid_impl: fn(&mut PrivateContext, Field) -> bool) -> Self { - AccountActions::init(Context::private(context), approved_action_storage_slot, is_valid_impl) - } + pub fn private( + context: &mut PrivateContext, + approved_action_storage_slot: Field, + is_valid_impl: fn(&mut PrivateContext, Field) -> bool + ) -> Self { + AccountActions::init( + Context::private(context), + approved_action_storage_slot, + is_valid_impl + ) + } + + pub fn public( + context: &mut PublicContext, + approved_action_storage_slot: Field, + is_valid_impl: fn(&mut PrivateContext, Field) -> bool + ) -> Self { + AccountActions::init( + Context::public(context), + approved_action_storage_slot, + is_valid_impl + ) + } + + // docs:start:entrypoint + pub fn entrypoint(self, app_payload: AppPayload, fee_payload: FeePayload) { + let valid_fn = self.is_valid_impl; + let mut private_context = self.context.private.unwrap(); - pub fn public(context: &mut PublicContext, approved_action_storage_slot: Field, is_valid_impl: fn(&mut PrivateContext, Field) -> bool) -> Self { - AccountActions::init(Context::public(context), approved_action_storage_slot, is_valid_impl) - } - - // docs:start:entrypoint - pub fn entrypoint(self, payload: EntrypointPayload) { - let message_hash = payload.hash(); - let valid_fn = self.is_valid_impl; - let private_context = self.context.private.unwrap(); - assert(valid_fn(private_context, message_hash)); - payload.execute_calls(private_context); - } - // docs:end:entrypoint + let fee_hash = fee_payload.hash(); + assert(valid_fn(private_context, fee_hash)); + fee_payload.execute_calls(private_context); + private_context.capture_max_non_revertible_side_effect_counter(); - pub fn is_valid(self, message_hash: Field) -> Field { - let valid_fn = self.is_valid_impl; - if (valid_fn(self.context.private.unwrap(), message_hash)) { - IS_VALID_SELECTOR - } else { - 0 + let app_hash = app_payload.hash(); + assert(valid_fn(private_context, app_hash)); + app_payload.execute_calls(private_context); } - } + // docs:end:entrypoint - pub fn is_valid_public(self, message_hash: Field) -> Field { - let value = self.approved_action.at(message_hash).read(); - if (value){ - IS_VALID_SELECTOR - } else { - 0 + pub fn is_valid(self, message_hash: Field) -> Field { + let valid_fn = self.is_valid_impl; + if (valid_fn(self.context.private.unwrap(), message_hash)) { + IS_VALID_SELECTOR + } else { + 0 + } } - } - pub fn internal_set_is_valid_storage(self, message_hash: Field, value: bool) { - self.approved_action.at(message_hash).write(value); - } + pub fn is_valid_public(self, message_hash: Field) -> Field { + let value = self.approved_action.at(message_hash).read(); + if (value) { IS_VALID_SELECTOR } else { 0 } + } + + pub fn internal_set_is_valid_storage(self, message_hash: Field, value: bool) { + self.approved_action.at(message_hash).write(value); + } } diff --git a/authwit/src/auth.nr b/authwit/src/auth.nr index d9dabab..99b07bf 100644 --- a/authwit/src/auth.nr +++ b/authwit/src/auth.nr @@ -1,19 +1,8 @@ use dep::aztec::protocol_types::{ - abis::function_selector::FunctionSelector, - address::AztecAddress, - constants::{ - GENERATOR_INDEX__SIGNATURE_PAYLOAD, - }, - hash::{ - hash_args, - pedersen_hash, - }, -}; -use dep::aztec::context::{ - PrivateContext, - PublicContext, - Context, + abis::function_selector::FunctionSelector, address::AztecAddress, + constants::{GENERATOR_INDEX__SIGNATURE_PAYLOAD}, hash::{hash_args, pedersen_hash} }; +use dep::aztec::context::{PrivateContext, PublicContext, Context}; global IS_VALID_SELECTOR = 0xe86ab4ff; global IS_VALID_PUBLIC_SELECTOR = 0xf3661153; diff --git a/authwit/src/entrypoint.nr b/authwit/src/entrypoint.nr index 13407a4..d52c280 100644 --- a/authwit/src/entrypoint.nr +++ b/authwit/src/entrypoint.nr @@ -1,116 +1,3 @@ -use dep::aztec::context::PrivateContext; -use dep::aztec::protocol_types::{ - abis::function_selector::FunctionSelector, - address::AztecAddress, - constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD, - hash::pedersen_hash, - traits::{Hash, Serialize} -}; - -global ACCOUNT_MAX_CALLS: Field = 4; -// 1 (ARGS_HASH) + 1 (FUNCTION_SELECTOR) + 1 (TARGET_ADDRESS) + 1 (IS_PUBLIC) -global FUNCTION_CALL_SIZE: Field = 4; -// 3 * 32 + 1 -global FUNCTION_CALL_SIZE_IN_BYTES: Field = 97; - -struct FunctionCall { - args_hash: Field, - function_selector: FunctionSelector, - target_address: AztecAddress, - is_public: bool, -} - -impl Serialize for FunctionCall { - fn serialize(self) -> [Field; FUNCTION_CALL_SIZE] { - [self.args_hash, self.function_selector.to_field(), self.target_address.to_field(), self.is_public as Field] - } -} - -impl FunctionCall { - fn to_be_bytes(self) -> [u8; FUNCTION_CALL_SIZE_IN_BYTES] { - let mut bytes: [u8; FUNCTION_CALL_SIZE_IN_BYTES] = [0; FUNCTION_CALL_SIZE_IN_BYTES]; - let args_hash_bytes = self.args_hash.to_be_bytes(32); - for i in 0..32 { bytes[i] = args_hash_bytes[i]; } - let function_selector_bytes = self.function_selector.to_field().to_be_bytes(32); - for i in 0..32 { bytes[i + 32] = function_selector_bytes[i]; } - let target_address_bytes = self.target_address.to_field().to_be_bytes(32); - for i in 0..32 { bytes[i + 64] = target_address_bytes[i]; } - bytes[96] = self.is_public as u8; - bytes - } -} - -// FUNCTION_CALL_SIZE * ACCOUNT_MAX_CALLS + 1 -global ENTRYPOINT_PAYLOAD_SIZE: Field = 17; -// FUNCTION_CALL_SIZE_IN_BYTES * ACCOUNT_MAX_CALLS + 32 -global ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES: Field = 420; - -// Note: If you change the following struct you have to update default_entrypoint.ts -// docs:start:entrypoint-struct -struct EntrypointPayload { - function_calls: [FunctionCall; ACCOUNT_MAX_CALLS], - nonce: Field, -} -// docs:end:entrypoint-struct - -impl Serialize for EntrypointPayload { - // Serializes the entrypoint struct - fn serialize(self) -> [Field; ENTRYPOINT_PAYLOAD_SIZE] { - let mut fields: BoundedVec = BoundedVec::new(0); - for call in self.function_calls { - fields.extend_from_array(call.serialize()); - } - fields.push(self.nonce); - fields.storage - } -} - -impl Hash for EntrypointPayload { - fn hash(self) -> Field { - pedersen_hash( - self.serialize(), - GENERATOR_INDEX__SIGNATURE_PAYLOAD - ) - } -} - -impl EntrypointPayload { - // Serializes the payload as an array of bytes. Useful for hashing with sha256. - fn to_be_bytes(self) -> [u8; ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES] { - let mut bytes: [u8; ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES] = [0; ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES]; - - for i in 0..ACCOUNT_MAX_CALLS { - let item_bytes = self.function_calls[i].to_be_bytes(); - for j in 0..FUNCTION_CALL_SIZE_IN_BYTES { - bytes[i * FUNCTION_CALL_SIZE_IN_BYTES + j] = item_bytes[j]; - } - } - - let nonce_bytes = self.nonce.to_be_bytes(32); - let nonce_offset = FUNCTION_CALL_SIZE_IN_BYTES * ACCOUNT_MAX_CALLS; - for j in 0..32 { - bytes[nonce_offset + j] = nonce_bytes[j]; - } - - bytes - } - - // Executes all private and public calls - // docs:start:entrypoint-execute-calls - fn execute_calls(self, context: &mut PrivateContext) { - for call in self.function_calls { - if !call.target_address.is_zero() { - if call.is_public { - context.call_public_function_with_packed_args( - call.target_address, call.function_selector, call.args_hash - ); - } else { - let _result = context.call_private_function_with_packed_args( - call.target_address, call.function_selector, call.args_hash - ); - } - } - } - } - // docs:end:entrypoint-execute-calls -} +mod fee; +mod app; +mod function_call; diff --git a/authwit/src/entrypoint/app.nr b/authwit/src/entrypoint/app.nr new file mode 100644 index 0000000..49a22da --- /dev/null +++ b/authwit/src/entrypoint/app.nr @@ -0,0 +1,69 @@ +use dep::aztec::context::PrivateContext; +use dep::aztec::protocol_types::{constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD, hash::pedersen_hash, traits::{Hash, Serialize}}; + +use crate::entrypoint::function_call::{FunctionCall, FUNCTION_CALL_SIZE_IN_BYTES}; + +// FUNCTION_CALL_SIZE * ACCOUNT_MAX_CALLS + 1 +global APP_PAYLOAD_SIZE: Field = 17; +// FUNCTION_CALL_SIZE_IN_BYTES * ACCOUNT_MAX_CALLS + 32 +global APP_PAYLOAD_SIZE_IN_BYTES: Field = 420; + +global ACCOUNT_MAX_CALLS: Field = 4; + +// Note: If you change the following struct you have to update default_entrypoint.ts +// docs:start:app-payload-struct +struct AppPayload { + function_calls: [FunctionCall; ACCOUNT_MAX_CALLS], + nonce: Field, +} +// docs:end:app-payload-struct + +impl Serialize for AppPayload { + // Serializes the entrypoint struct + fn serialize(self) -> [Field; APP_PAYLOAD_SIZE] { + let mut fields: BoundedVec = BoundedVec::new(0); + for call in self.function_calls { + fields.extend_from_array(call.serialize()); + } + fields.push(self.nonce); + fields.storage + } +} + +impl Hash for AppPayload { + fn hash(self) -> Field { + pedersen_hash( + self.serialize(), + GENERATOR_INDEX__SIGNATURE_PAYLOAD + ) + } +} + +impl AppPayload { + // Serializes the payload as an array of bytes. Useful for hashing with sha256. + fn to_be_bytes(self) -> [u8; APP_PAYLOAD_SIZE_IN_BYTES] { + let mut bytes: BoundedVec = BoundedVec::new(0); + + for i in 0..ACCOUNT_MAX_CALLS { + bytes.extend_from_array(self.function_calls[i].to_be_bytes()); + } + bytes.extend_from_array(self.nonce.to_be_bytes(32)); + + bytes.storage + } + + // Executes all private and public calls + // docs:start:entrypoint-execute-calls + fn execute_calls(self, context: &mut PrivateContext) { + for call in self.function_calls { + if !call.target_address.is_zero() { + if call.is_public { + context.call_public_function_with_packed_args(call.target_address, call.function_selector, call.args_hash); + } else { + let _result = context.call_private_function_with_packed_args(call.target_address, call.function_selector, call.args_hash); + } + } + } + } + // docs:end:entrypoint-execute-calls +} diff --git a/authwit/src/entrypoint/fee.nr b/authwit/src/entrypoint/fee.nr new file mode 100644 index 0000000..3183302 --- /dev/null +++ b/authwit/src/entrypoint/fee.nr @@ -0,0 +1,64 @@ +use dep::aztec::context::PrivateContext; +use dep::aztec::protocol_types::{constants::GENERATOR_INDEX__FEE_PAYLOAD, hash::pedersen_hash, traits::{Hash, Serialize}}; +use crate::entrypoint::function_call::{FunctionCall}; + +// 2 * 4 (function call) + 1 +global FEE_PAYLOAD_SIZE: Field = 9; + +// 2*97 + 32 +global FEE_PAYLOAD_SIZE_IN_BYTES: Field = 226; + +global MAX_FEE_FUNCTION_CALLS = 2; + +// docs:start:fee-payload-struct +struct FeePayload { + function_calls: [FunctionCall; MAX_FEE_FUNCTION_CALLS], + nonce: Field, +} +// docs:end:fee-payload-struct + +impl Serialize for FeePayload { + // Serializes the entrypoint struct + fn serialize(self) -> [Field; FEE_PAYLOAD_SIZE] { + let mut fields: BoundedVec = BoundedVec::new(0); + for i in 0..MAX_FEE_FUNCTION_CALLS { + fields.extend_from_array(self.function_calls[i].serialize()); + } + fields.push(self.nonce); + fields.storage + } +} + +impl Hash for FeePayload { + fn hash(self) -> Field { + pedersen_hash( + self.serialize(), + GENERATOR_INDEX__FEE_PAYLOAD + ) + } +} + +impl FeePayload { + fn to_be_bytes(self) -> [u8; FEE_PAYLOAD_SIZE_IN_BYTES] { + let mut bytes: BoundedVec = BoundedVec::new(0); + + for i in 0..MAX_FEE_FUNCTION_CALLS { + bytes.extend_from_array(self.function_calls[i].to_be_bytes()); + } + bytes.extend_from_array(self.nonce.to_be_bytes(32)); + + bytes.storage + } + + fn execute_calls(self, context: &mut PrivateContext) { + for call in self.function_calls { + if !call.target_address.is_zero() { + if call.is_public { + context.call_public_function_with_packed_args(call.target_address, call.function_selector, call.args_hash); + } else { + let _result = context.call_private_function_with_packed_args(call.target_address, call.function_selector, call.args_hash); + } + } + } + } +} diff --git a/authwit/src/entrypoint/function_call.nr b/authwit/src/entrypoint/function_call.nr new file mode 100644 index 0000000..0b1f2ba --- /dev/null +++ b/authwit/src/entrypoint/function_call.nr @@ -0,0 +1,39 @@ +use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress, traits::Serialize}; + +// 1 (ARGS_HASH) + 1 (FUNCTION_SELECTOR) + 1 (TARGET_ADDRESS) + 1 (IS_PUBLIC) +global FUNCTION_CALL_SIZE: Field = 4; +// 3 * 32 + 1 +global FUNCTION_CALL_SIZE_IN_BYTES: Field = 97; + +struct FunctionCall { + args_hash: Field, + function_selector: FunctionSelector, + target_address: AztecAddress, + is_public: bool, +} + +impl Serialize for FunctionCall { + fn serialize(self) -> [Field; FUNCTION_CALL_SIZE] { + [self.args_hash, self.function_selector.to_field(), self.target_address.to_field(), self.is_public as Field] + } +} + +impl FunctionCall { + fn to_be_bytes(self) -> [u8; FUNCTION_CALL_SIZE_IN_BYTES] { + let mut bytes: [u8; FUNCTION_CALL_SIZE_IN_BYTES] = [0; FUNCTION_CALL_SIZE_IN_BYTES]; + let args_hash_bytes = self.args_hash.to_be_bytes(32); + for i in 0..32 { + bytes[i] = args_hash_bytes[i]; + } + let function_selector_bytes = self.function_selector.to_field().to_be_bytes(32); + for i in 0..32 { + bytes[i + 32] = function_selector_bytes[i]; + } + let target_address_bytes = self.target_address.to_field().to_be_bytes(32); + for i in 0..32 { + bytes[i + 64] = target_address_bytes[i]; + } + bytes[96] = self.is_public as u8; + bytes + } +} diff --git a/aztec/Nargo.toml b/aztec/Nargo.toml index 54657bf..fb8ba9b 100644 --- a/aztec/Nargo.toml +++ b/aztec/Nargo.toml @@ -5,4 +5,4 @@ compiler_version = ">=0.18.0" type = "lib" [dependencies] -protocol_types = { git="https://github.com/AztecProtocol/aztec-packages", tag="aztec-packages-v0.23.0", directory="yarn-project/noir-protocol-circuits/src/crates/types" } +protocol_types = { path = "../../noir-protocol-circuits/src/crates/types" } diff --git a/aztec/src/avm/context.nr b/aztec/src/avm/context.nr index 4e6d3f5..e247f60 100644 --- a/aztec/src/avm/context.nr +++ b/aztec/src/avm/context.nr @@ -1,7 +1,4 @@ -use dep::protocol_types::address::{ - AztecAddress, - EthAddress, -}; +use dep::protocol_types::address::{AztecAddress, EthAddress}; // Getters that will be converted by the transpiler into their // own opcodes @@ -44,7 +41,6 @@ impl AvmContext { #[oracle(timestamp)] pub fn timestamp() -> Field {} - // #[oracle(contractCallDepth)] // pub fn contract_call_depth() -> Field {} } diff --git a/aztec/src/context.nr b/aztec/src/context.nr index 8cc2c2a..37eb6bb 100644 --- a/aztec/src/context.nr +++ b/aztec/src/context.nr @@ -16,23 +16,14 @@ struct Context { impl Context { pub fn private(context: &mut PrivateContext) -> Context { - Context { - private: Option::some(context), - public: Option::none() - } + Context { private: Option::some(context), public: Option::none() } } pub fn public(context: &mut PublicContext) -> Context { - Context { - public: Option::some(context), - private: Option::none() - } + Context { public: Option::some(context), private: Option::none() } } pub fn none() -> Context { - Context { - public: Option::none(), - private: Option::none() - } + Context { public: Option::none(), private: Option::none() } } } diff --git a/aztec/src/context/avm.nr b/aztec/src/context/avm.nr index 98c578e..26634b8 100644 --- a/aztec/src/context/avm.nr +++ b/aztec/src/context/avm.nr @@ -1,7 +1,4 @@ -use dep::protocol_types::address::{ - AztecAddress, - EthAddress, -}; +use dep::protocol_types::address::{AztecAddress, EthAddress}; // Getters that will be converted by the transpiler into their // own opcodes @@ -48,7 +45,6 @@ impl AVMContext { #[oracle(timestamp)] pub fn timestamp(self) -> Field {} - // #[oracle(contractCallDepth)] // pub fn contract_call_depth(self) -> Field {} } diff --git a/aztec/src/context/inputs/private_context_inputs.nr b/aztec/src/context/inputs/private_context_inputs.nr index b96f62e..c2ed873 100644 --- a/aztec/src/context/inputs/private_context_inputs.nr +++ b/aztec/src/context/inputs/private_context_inputs.nr @@ -1,8 +1,4 @@ -use dep::protocol_types::{ - abis::call_context::CallContext, - contrakt::deployment_data::ContractDeploymentData, - header::Header, -}; +use dep::protocol_types::{abis::call_context::CallContext, contrakt::deployment_data::ContractDeploymentData, header::Header}; use crate::context::globals::private_global_variables::PrivateGlobalVariables; // PrivateContextInputs are expected to be provided to each private function diff --git a/aztec/src/context/inputs/public_context_inputs.nr b/aztec/src/context/inputs/public_context_inputs.nr index 6fefcfe..6b8aefb 100644 --- a/aztec/src/context/inputs/public_context_inputs.nr +++ b/aztec/src/context/inputs/public_context_inputs.nr @@ -1,9 +1,6 @@ use crate::context::globals::public_global_variables::PublicGlobalVariables; -use dep::protocol_types::{ - abis::call_context::CallContext, - header::Header, -}; +use dep::protocol_types::{abis::call_context::CallContext, header::Header}; // PublicContextInputs are expected to be provided to each public function // docs:start:public-context-inputs diff --git a/aztec/src/context/private_context.nr b/aztec/src/context/private_context.nr index 1fdad5a..0e3d7fa 100644 --- a/aztec/src/context/private_context.nr +++ b/aztec/src/context/private_context.nr @@ -1,53 +1,32 @@ use crate::{ - context::inputs::PrivateContextInputs, - key::nullifier_key::validate_nullifier_key_against_address, + context::inputs::PrivateContextInputs, key::nullifier_key::validate_nullifier_key_against_address, messaging::process_l1_to_l2_message, oracle::{ - arguments, - call_private_function::call_private_function_internal, - enqueue_public_function_call::enqueue_public_function_call_internal, - context::get_portal_address, - header::get_header_at, - nullifier_key::{get_nullifier_key_pair, NullifierKeyPair}, - }, + arguments, call_private_function::call_private_function_internal, + enqueue_public_function_call::enqueue_public_function_call_internal, context::get_portal_address, + header::get_header_at, nullifier_key::{get_nullifier_key_pair, NullifierKeyPair} +} }; use dep::protocol_types::{ abis::{ - call_context::CallContext, - function_data::FunctionData, - function_selector::FunctionSelector, - nullifier_key_validation_request::NullifierKeyValidationRequest, - private_call_stack_item::PrivateCallStackItem, - private_circuit_public_inputs::PrivateCircuitPublicInputs, - public_call_stack_item::PublicCallStackItem, - public_circuit_public_inputs::PublicCircuitPublicInputs, - side_effect::{SideEffect, SideEffectLinkedToNoteHash}, - }, - address::{ - AztecAddress, - EthAddress, - }, + call_context::CallContext, function_data::FunctionData, function_selector::FunctionSelector, + nullifier_key_validation_request::NullifierKeyValidationRequest, + private_call_stack_item::PrivateCallStackItem, + private_circuit_public_inputs::PrivateCircuitPublicInputs, + public_call_stack_item::PublicCallStackItem, + public_circuit_public_inputs::PublicCircuitPublicInputs, + side_effect::{SideEffect, SideEffectLinkedToNoteHash} +}, + address::{AztecAddress, EthAddress}, constants::{ - 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_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL, - NUM_FIELDS_PER_SHA256, - RETURN_VALUES_LENGTH, - }, - contrakt::{ - storage_read::StorageRead, - storage_update_request::StorageUpdateRequest, - }, - grumpkin_private_key::GrumpkinPrivateKey, - hash::hash_args, - header::Header, - utils::reader::Reader, + 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_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL, NUM_FIELDS_PER_SHA256, + RETURN_VALUES_LENGTH +}, + contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest}, + grumpkin_private_key::GrumpkinPrivateKey, hash::hash_args, header::Header, utils::reader::Reader }; use dep::std::option::Option; @@ -60,7 +39,7 @@ struct PrivateContext { inputs: PrivateContextInputs, side_effect_counter: u32, - meta_hwm: u32, + max_non_revertible_side_effect_counter: u32, args_hash : Field, return_values : BoundedVec, @@ -89,30 +68,23 @@ struct PrivateContext { impl PrivateContext { pub fn new(inputs: PrivateContextInputs, args_hash: Field) -> PrivateContext { PrivateContext { - inputs: inputs, + inputs, side_effect_counter: inputs.call_context.start_side_effect_counter, - meta_hwm: 0, - - args_hash: args_hash, + max_non_revertible_side_effect_counter: 0, + args_hash, return_values: BoundedVec::new(0), - read_requests: BoundedVec::new(SideEffect::empty()), nullifier_key_validation_requests: BoundedVec::new(NullifierKeyValidationRequest::empty()), - new_commitments: BoundedVec::new(SideEffect::empty()), new_nullifiers: BoundedVec::new(SideEffectLinkedToNoteHash::empty()), - historical_header: inputs.historical_header, - private_call_stack_hashes: BoundedVec::new(0), public_call_stack_hashes: BoundedVec::new(0), new_l2_to_l1_msgs: BoundedVec::new(0), - // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) // encrypted_logs_preimages: Vec::new(), // unencrypted_logs_preimages: Vec::new(), - - nullifier_key: Option::none(), + nullifier_key: Option::none() } } @@ -163,7 +135,7 @@ impl PrivateContext { call_context: self.inputs.call_context, args_hash: self.args_hash, return_values: self.return_values.storage, - meta_hwm: self.meta_hwm, + max_non_revertible_side_effect_counter: self.max_non_revertible_side_effect_counter, read_requests: self.read_requests.storage, nullifier_key_validation_requests: self.nullifier_key_validation_requests.storage, new_commitments: self.new_commitments.storage, @@ -172,42 +144,39 @@ impl PrivateContext { public_call_stack_hashes: self.public_call_stack_hashes.storage, new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage, end_side_effect_counter: self.side_effect_counter, - encrypted_logs_hash: encrypted_logs_hash, - unencrypted_logs_hash: unencrypted_logs_hash, - encrypted_log_preimages_length: encrypted_log_preimages_length, - unencrypted_log_preimages_length: unencrypted_log_preimages_length, + encrypted_logs_hash, + unencrypted_logs_hash, + encrypted_log_preimages_length, + unencrypted_log_preimages_length, historical_header: self.historical_header, contract_deployment_data: self.inputs.contract_deployment_data, chain_id: self.inputs.private_global_variables.chain_id, - version: self.inputs.private_global_variables.version, + version: self.inputs.private_global_variables.version }; priv_circuit_pub_inputs } + pub fn capture_max_non_revertible_side_effect_counter(&mut self) { + assert( + self.max_non_revertible_side_effect_counter == 0, "Already captured the non-revertible side effect counter" + ); + self.max_non_revertible_side_effect_counter = self.side_effect_counter; + } + pub fn push_read_request(&mut self, read_request: Field) { - let side_effect = SideEffect { - value: read_request, - counter: self.side_effect_counter, - }; + let side_effect = SideEffect { value: read_request, counter: self.side_effect_counter }; self.read_requests.push(side_effect); self.side_effect_counter = self.side_effect_counter + 1; } pub fn push_new_note_hash(&mut self, note_hash: Field) { - let side_effect = SideEffect { - value: note_hash, - counter: self.side_effect_counter, - }; + let side_effect = SideEffect { value: note_hash, counter: self.side_effect_counter }; self.new_commitments.push(side_effect); self.side_effect_counter = self.side_effect_counter + 1; } pub fn push_new_nullifier(&mut self, nullifier: Field, nullified_commitment: Field) { - let side_effect = SideEffectLinkedToNoteHash { - value: nullifier, - note_hash: nullified_commitment, - counter: self.side_effect_counter, - }; + let side_effect = SideEffectLinkedToNoteHash { value: nullifier, note_hash: nullified_commitment, counter: self.side_effect_counter }; self.new_nullifiers.push(side_effect); self.side_effect_counter = self.side_effect_counter + 1; } @@ -224,16 +193,17 @@ impl PrivateContext { let key_pair = self.nullifier_key.unwrap_unchecked(); // If MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL is larger than 1, need to update the way the key pair is cached. assert(MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL == 1); - assert(key_pair.account == account, "Cannot query nullifier key for more than one account per call"); + assert( + key_pair.account == account, "Cannot query nullifier key for more than one account per call" + ); key_pair }; key_pair.secret_key } // docs:start:context_message_portal - pub fn message_portal(&mut self, content: Field) - // docs:end:context_message_portal - { + pub fn message_portal(&mut self, content: Field) { + // docs:end:context_message_portal self.new_l2_to_l1_msgs.push(content); } @@ -241,15 +211,18 @@ impl PrivateContext { // Note this returns self to get around an issue where mutable structs do not maintain mutations unless reassigned // docs:start:context_consume_l1_to_l2_message // docs:start:consume_l1_to_l2_message - pub fn consume_l1_to_l2_message( - &mut self, - msg_key: Field, - content: Field, - secret: Field - ) - // docs:end:context_consume_l1_to_l2_message - { - let nullifier = process_l1_to_l2_message(self.historical_header.state.l1_to_l2_message_tree.root, self.this_address(), self.this_portal_address(), self.chain_id(), self.version(), msg_key, content, secret); + pub fn consume_l1_to_l2_message(&mut self, msg_key: Field, content: Field, secret: Field) { + // docs:end:context_consume_l1_to_l2_message + let nullifier = process_l1_to_l2_message( + self.historical_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) @@ -282,7 +255,7 @@ impl PrivateContext { pub fn call_private_function_no_args( &mut self, contract_address: AztecAddress, - function_selector: FunctionSelector, + function_selector: FunctionSelector ) -> [Field; RETURN_VALUES_LENGTH] { self.call_private_function_with_packed_args(contract_address, function_selector, 0) } @@ -297,7 +270,7 @@ impl PrivateContext { contract_address, function_selector, args_hash, - self.side_effect_counter, + self.side_effect_counter ); assert_eq(item.public_inputs.call_context.start_side_effect_counter, self.side_effect_counter); @@ -315,7 +288,9 @@ impl PrivateContext { assert(item.public_inputs.call_context.is_delegate_call == false); assert(item.public_inputs.call_context.is_static_call == false); assert(item.public_inputs.call_context.is_contract_deployment == false); - assert(item.public_inputs.call_context.msg_sender.eq(self.inputs.call_context.storage_contract_address)); + assert( + item.public_inputs.call_context.msg_sender.eq(self.inputs.call_context.storage_contract_address) + ); assert(item.public_inputs.call_context.storage_contract_address.eq(contract_address)); self.private_call_stack_hashes.push(item.hash()); @@ -337,7 +312,7 @@ impl PrivateContext { pub fn call_public_function_no_args( &mut self, contract_address: AztecAddress, - function_selector: FunctionSelector, + function_selector: FunctionSelector ) { self.call_public_function_with_packed_args(contract_address, function_selector, 0) } @@ -360,7 +335,7 @@ impl PrivateContext { // Note: Not using PublicCirclePublicInputs::deserialize here, because everything below args_hash is 0 and // there is no more data in fields because there is only ENQUEUE_PUBLIC_FUNCTION_CALL_RETURN_SIZE fields! let item = PublicCallStackItem { - contract_address: AztecAddress::from_field(reader.read()), + contract_address: AztecAddress::from_field(reader.read()), function_data: reader.read_struct(FunctionData::deserialize), public_inputs: PublicCircuitPublicInputs { call_context: reader.read_struct(CallContext::deserialize), @@ -371,13 +346,13 @@ impl PrivateContext { public_call_stack_hashes: [0; MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL], new_commitments: [SideEffect::empty(); MAX_NEW_COMMITMENTS_PER_CALL], new_nullifiers: [SideEffectLinkedToNoteHash::empty(); MAX_NEW_NULLIFIERS_PER_CALL], - new_l2_to_l1_msgs:[0; MAX_NEW_L2_TO_L1_MSGS_PER_CALL], - unencrypted_logs_hash:[0; NUM_FIELDS_PER_SHA256], + 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, historical_header: Header::empty(), - prover_address: AztecAddress::zero(), + prover_address: AztecAddress::zero() }, - is_execution_request: true, + is_execution_request: true }; reader.finish(); @@ -397,7 +372,9 @@ impl PrivateContext { assert(item.public_inputs.call_context.is_delegate_call == false); assert(item.public_inputs.call_context.is_static_call == false); assert(item.public_inputs.call_context.is_contract_deployment == false); - assert(item.public_inputs.call_context.msg_sender.eq(self.inputs.call_context.storage_contract_address)); + assert( + item.public_inputs.call_context.msg_sender.eq(self.inputs.call_context.storage_contract_address) + ); assert(item.public_inputs.call_context.storage_contract_address.eq(contract_address)); self.public_call_stack_hashes.push(item.hash()); diff --git a/aztec/src/context/public_context.nr b/aztec/src/context/public_context.nr index 2309a90..aec62ab 100644 --- a/aztec/src/context/public_context.nr +++ b/aztec/src/context/public_context.nr @@ -1,42 +1,24 @@ use crate::{ - context::inputs::PublicContextInputs, - messaging::process_l1_to_l2_message, - oracle::{ - arguments, - public_call::call_public_function_internal, - }, + context::inputs::PublicContextInputs, messaging::process_l1_to_l2_message, + oracle::{arguments, public_call::call_public_function_internal} }; use dep::protocol_types::{ abis::{ - global_variables::GlobalVariables, - function_selector::FunctionSelector, - private_circuit_public_inputs::PrivateCircuitPublicInputs, - public_call_stack_item::PublicCallStackItem, - public_circuit_public_inputs::PublicCircuitPublicInputs, - side_effect::{SideEffect, SideEffectLinkedToNoteHash}, - }, - address::{ - AztecAddress, - EthAddress, - }, + global_variables::GlobalVariables, function_selector::FunctionSelector, + private_circuit_public_inputs::PrivateCircuitPublicInputs, + public_call_stack_item::PublicCallStackItem, + public_circuit_public_inputs::PublicCircuitPublicInputs, + side_effect::{SideEffect, SideEffectLinkedToNoteHash} +}, + address::{AztecAddress, EthAddress}, constants::{ - MAX_NEW_COMMITMENTS_PER_CALL, - MAX_NEW_L2_TO_L1_MSGS_PER_CALL, - MAX_NEW_NULLIFIERS_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, - NUM_FIELDS_PER_SHA256, - RETURN_VALUES_LENGTH, - }, - contrakt::{ - storage_read::StorageRead, - storage_update_request::StorageUpdateRequest, - }, - hash::hash_args, - header::Header, - utils::reader::Reader, + MAX_NEW_COMMITMENTS_PER_CALL, MAX_NEW_L2_TO_L1_MSGS_PER_CALL, MAX_NEW_NULLIFIERS_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, NUM_FIELDS_PER_SHA256, + RETURN_VALUES_LENGTH +}, + contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest}, + hash::hash_args, header::Header, utils::reader::Reader }; struct PublicContext { @@ -69,29 +51,20 @@ impl PublicContext { let empty_storage_read = StorageRead::empty(); let empty_storage_update = StorageUpdateRequest::empty(); PublicContext { - inputs: inputs, + inputs, side_effect_counter: inputs.call_context.start_side_effect_counter, - - args_hash: args_hash, + args_hash, return_values: BoundedVec::new(0), - contract_storage_update_requests: BoundedVec::new(empty_storage_update), contract_storage_reads: BoundedVec::new(empty_storage_read), public_call_stack_hashes: BoundedVec::new(0), - new_commitments: BoundedVec::new(SideEffect::empty()), new_nullifiers: BoundedVec::new(SideEffectLinkedToNoteHash::empty()), - new_l2_to_l1_msgs: BoundedVec::new(0), - - unencrypted_logs_hash: BoundedVec::new(0), unencrypted_logs_preimages_length: 0, - historical_header: inputs.historical_header, - prover_address: AztecAddress::zero(), - - // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) + prover_address: AztecAddress::zero() // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) // encrypted_logs_preimages: Vec::new(), // unencrypted_logs_preimages: Vec::new(), } @@ -142,7 +115,6 @@ impl PublicContext { 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 = PublicCircuitPublicInputs { call_context: self.inputs.call_context, // Done @@ -154,19 +126,16 @@ impl PublicContext { new_nullifiers: self.new_nullifiers.storage, public_call_stack_hashes: self.public_call_stack_hashes.storage, new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage, - unencrypted_logs_hash: unencrypted_logs_hash, - unencrypted_log_preimages_length: unencrypted_log_preimages_length, + unencrypted_logs_hash, + unencrypted_log_preimages_length, historical_header: self.inputs.historical_header, - prover_address: self.prover_address, + prover_address: self.prover_address }; pub_circuit_pub_inputs } pub fn push_new_note_hash(&mut self, note_hash: Field) { - let side_effect = SideEffect { - value: note_hash, - counter: self.side_effect_counter - }; + let side_effect = SideEffect { value: note_hash, counter: self.side_effect_counter }; self.new_commitments.push(side_effect); self.side_effect_counter = self.side_effect_counter + 1; } @@ -189,7 +158,16 @@ 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.historical_header.state.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.historical_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) @@ -211,27 +189,18 @@ impl PublicContext { _self: Self, contract_address: AztecAddress, function_selector: FunctionSelector, - args: [Field; ARGS_COUNT], + args: [Field; ARGS_COUNT] ) -> [Field; RETURN_VALUES_LENGTH] { let args_hash = hash_args(args); assert(args_hash == arguments::pack_arguments(args)); - call_public_function_internal( - contract_address, - function_selector, - args_hash, - ) + call_public_function_internal(contract_address, function_selector, args_hash) } pub fn call_public_function_no_args( _self: Self, contract_address: AztecAddress, - function_selector: FunctionSelector, + function_selector: FunctionSelector ) -> [Field; RETURN_VALUES_LENGTH] { - call_public_function_internal( - contract_address, - function_selector, - 0, - ) + call_public_function_internal(contract_address, function_selector, 0) } - } diff --git a/aztec/src/hash.nr b/aztec/src/hash.nr index 0368908..d59bdd9 100644 --- a/aztec/src/hash.nr +++ b/aztec/src/hash.nr @@ -1,7 +1,4 @@ -use dep::protocol_types::{ - constants::GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, - hash::pedersen_hash, -}; +use dep::protocol_types::{constants::GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, hash::pedersen_hash}; pub fn compute_secret_hash(secret: Field) -> Field { // TODO(#1205) This is probably not the right index to use diff --git a/aztec/src/hasher.nr b/aztec/src/hasher.nr index fcfd27a..b1f1544 100644 --- a/aztec/src/hasher.nr +++ b/aztec/src/hasher.nr @@ -1,7 +1,4 @@ -use dep::protocol_types::{ - hash::hash_args, - traits::Hash, -}; +use dep::protocol_types::{hash::hash_args, traits::Hash}; struct Hasher { fields: [Field], @@ -14,7 +11,7 @@ impl Hash for Hasher { } impl Hasher { - pub fn new()-> Self { + pub fn new() -> Self { Self { fields: [] } } diff --git a/aztec/src/history/contract_inclusion.nr b/aztec/src/history/contract_inclusion.nr index 5b4469b..21137cc 100644 --- a/aztec/src/history/contract_inclusion.nr +++ b/aztec/src/history/contract_inclusion.nr @@ -1,17 +1,10 @@ use dep::protocol_types::{ - abis::{ - new_contract_data::NewContractData as ContractLeafPreimage, - }, - address::{AztecAddress, EthAddress}, - contract_class::ContractClassId, - grumpkin_point::GrumpkinPoint, + abis::{new_contract_data::NewContractData as ContractLeafPreimage}, + address::{AztecAddress, EthAddress}, contract_class::ContractClassId, grumpkin_point::GrumpkinPoint }; use dep::std::merkle::compute_merkle_root; -use crate::{ - context::PrivateContext, - oracle::get_membership_witness::get_contract_membership_witness, -}; +use crate::{context::PrivateContext, oracle::get_membership_witness::get_contract_membership_witness}; // Proves that a contract exists at block `block_number` and returns its address. // Note: This can be used to approximate a factory pattern --> a factory contract could perform this proof and that diff --git a/aztec/src/history/note_inclusion.nr b/aztec/src/history/note_inclusion.nr index b4170fd..0d47e4d 100644 --- a/aztec/src/history/note_inclusion.nr +++ b/aztec/src/history/note_inclusion.nr @@ -3,11 +3,8 @@ use dep::protocol_types::header::Header; use crate::{ context::PrivateContext, - note::{ - utils::compute_note_hash_for_consumption, - note_interface::NoteInterface, - }, - oracle::get_membership_witness::get_note_hash_membership_witness, + note::{utils::compute_note_hash_for_consumption, note_interface::NoteInterface}, + oracle::get_membership_witness::get_note_hash_membership_witness }; pub fn _note_inclusion(note: Note, header: Header) where Note: NoteInterface { diff --git a/aztec/src/history/note_validity.nr b/aztec/src/history/note_validity.nr index 916e6ff..c929f15 100644 --- a/aztec/src/history/note_validity.nr +++ b/aztec/src/history/note_validity.nr @@ -1,15 +1,10 @@ use crate::{ context::PrivateContext, history::{ - note_inclusion::prove_note_inclusion, - note_inclusion::_note_inclusion, - nullifier_non_inclusion::prove_note_not_nullified, - nullifier_non_inclusion::_nullifier_non_inclusion, - }, - note::{ - utils::compute_siloed_nullifier, - note_interface::NoteInterface, - }, + note_inclusion::prove_note_inclusion, note_inclusion::_note_inclusion, + nullifier_non_inclusion::prove_note_not_nullified, nullifier_non_inclusion::_nullifier_non_inclusion +}, + note::{utils::compute_siloed_nullifier, note_interface::NoteInterface} }; pub fn prove_note_validity(note: Note, context: &mut PrivateContext) where Note: NoteInterface { diff --git a/aztec/src/history/nullifier_inclusion.nr b/aztec/src/history/nullifier_inclusion.nr index 6a1a28d..1a7de2c 100644 --- a/aztec/src/history/nullifier_inclusion.nr +++ b/aztec/src/history/nullifier_inclusion.nr @@ -2,12 +2,8 @@ use dep::std::merkle::compute_merkle_root; use dep::protocol_types::header::Header; use crate::{ - context::PrivateContext, - oracle::get_nullifier_membership_witness::get_nullifier_membership_witness, - note::{ - utils::compute_siloed_nullifier, - note_interface::NoteInterface, - }, + context::PrivateContext, oracle::get_nullifier_membership_witness::get_nullifier_membership_witness, + note::{utils::compute_siloed_nullifier, note_interface::NoteInterface} }; fn _nullifier_inclusion(nullifier: Field, header: Header) { diff --git a/aztec/src/history/nullifier_non_inclusion.nr b/aztec/src/history/nullifier_non_inclusion.nr index d202ac3..1d17e2d 100644 --- a/aztec/src/history/nullifier_non_inclusion.nr +++ b/aztec/src/history/nullifier_non_inclusion.nr @@ -1,18 +1,8 @@ use dep::std::merkle::compute_merkle_root; -use dep::protocol_types::{ - header::Header, - utils::field::{ - full_field_less_than, - full_field_greater_than, - }, -}; +use dep::protocol_types::{header::Header, utils::field::{full_field_less_than, full_field_greater_than}}; use crate::{ - context::PrivateContext, - note::{ - utils::compute_siloed_nullifier, - note_interface::NoteInterface, - }, - oracle::get_nullifier_membership_witness::get_low_nullifier_membership_witness, + context::PrivateContext, note::{utils::compute_siloed_nullifier, note_interface::NoteInterface}, + oracle::get_nullifier_membership_witness::get_low_nullifier_membership_witness }; pub fn _nullifier_non_inclusion(nullifier: Field, header: Header) { diff --git a/aztec/src/history/public_value_inclusion.nr b/aztec/src/history/public_value_inclusion.nr index 5a5f7f1..47dbae5 100644 --- a/aztec/src/history/public_value_inclusion.nr +++ b/aztec/src/history/public_value_inclusion.nr @@ -1,20 +1,10 @@ use dep::protocol_types::{ - constants::GENERATOR_INDEX__PUBLIC_LEAF_INDEX, - hash::pedersen_hash, - address::{ - AztecAddress - }, - header::Header, - utils::field::full_field_less_than, + constants::GENERATOR_INDEX__PUBLIC_LEAF_INDEX, hash::pedersen_hash, address::{AztecAddress}, + header::Header, utils::field::full_field_less_than }; use dep::std::merkle::compute_merkle_root; -use crate::{ - context::PrivateContext, - oracle::get_public_data_witness::{ - get_public_data_witness, - }, -}; +use crate::{context::PrivateContext, oracle::get_public_data_witness::{get_public_data_witness}}; fn _public_value_inclusion( value: Field, diff --git a/aztec/src/key/nullifier_key.nr b/aztec/src/key/nullifier_key.nr index 0b93dc8..7154a19 100644 --- a/aztec/src/key/nullifier_key.nr +++ b/aztec/src/key/nullifier_key.nr @@ -1,8 +1,8 @@ -use dep::protocol_types::{ - address::AztecAddress, - grumpkin_point::GrumpkinPoint, -}; +use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint}; -pub fn validate_nullifier_key_against_address(_address: AztecAddress, _nullifier_public_key: GrumpkinPoint) { +pub fn validate_nullifier_key_against_address( + _address: AztecAddress, + _nullifier_public_key: GrumpkinPoint +) { // TODO: Nullifier public key should be part of the address. } diff --git a/aztec/src/log.nr b/aztec/src/log.nr index bc9141d..033db50 100644 --- a/aztec/src/log.nr +++ b/aztec/src/log.nr @@ -1,18 +1,22 @@ use crate::context::{PrivateContext, PublicContext}; use crate::oracle; -use dep::protocol_types::{ - address::AztecAddress, - grumpkin_point::GrumpkinPoint, -}; +use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint}; pub fn emit_encrypted_log( context: &mut PrivateContext, contract_address: AztecAddress, storage_slot: Field, + note_type_id: Field, encryption_pub_key: GrumpkinPoint, log: [Field; N] ) { - let _ = oracle::logs::emit_encrypted_log(contract_address, storage_slot, encryption_pub_key, log); + let _ = oracle::logs::emit_encrypted_log( + contract_address, + storage_slot, + note_type_id, + encryption_pub_key, + log + ); context.accumulate_encrypted_logs(log); } diff --git a/aztec/src/messaging.nr b/aztec/src/messaging.nr index c75febd..caf26c3 100644 --- a/aztec/src/messaging.nr +++ b/aztec/src/messaging.nr @@ -7,10 +7,7 @@ use crate::oracle::get_l1_to_l2_message::get_l1_to_l2_message_call; use dep::std::merkle::compute_merkle_root; -use dep::protocol_types::address::{ - AztecAddress, - EthAddress, -}; +use dep::protocol_types::address::{AztecAddress, EthAddress}; // Returns the nullifier for the message pub fn process_l1_to_l2_message( diff --git a/aztec/src/messaging/l1_to_l2_message.nr b/aztec/src/messaging/l1_to_l2_message.nr index a4dfcfa..a8f86a4 100644 --- a/aztec/src/messaging/l1_to_l2_message.nr +++ b/aztec/src/messaging/l1_to_l2_message.nr @@ -1,17 +1,7 @@ use dep::protocol_types::{ - address::{ - AztecAddress, - EthAddress, - }, - constants::{ - L1_TO_L2_MESSAGE_LENGTH, - GENERATOR_INDEX__NULLIFIER, - GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, - }, - hash::{ - pedersen_hash, - sha256_to_field, - }, + address::{AztecAddress, EthAddress}, + constants::{L1_TO_L2_MESSAGE_LENGTH, GENERATOR_INDEX__NULLIFIER, GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET}, + hash::{pedersen_hash, sha256_to_field} }; struct L1ToL2Message { @@ -39,11 +29,11 @@ impl L1ToL2Message { recipient: AztecAddress::from_field(fields[2]), version: fields[3], content: fields[4], - secret: secret, + secret, secret_hash: fields[5], deadline: fields[6] as u32, fee: fields[7] as u64, - tree_index: tree_index + tree_index } } @@ -75,14 +65,17 @@ impl L1ToL2Message { } let message_hash = sha256_to_field(hash_bytes); - message_hash + message_hash } // The nullifier of a l1 to l2 message is the hash of the message salted with the secret and tree index // docs:start:l1_to_l2_message_compute_nullifier pub fn compute_nullifier(self: Self) -> Field { let message_hash = self.hash(); - pedersen_hash([message_hash, self.secret, self.tree_index], GENERATOR_INDEX__NULLIFIER) + pedersen_hash( + [message_hash, self.secret, self.tree_index], + GENERATOR_INDEX__NULLIFIER + ) } // docs:end:l1_to_l2_message_compute_nullifier } diff --git a/aztec/src/messaging/l1_to_l2_message_getter_data.nr b/aztec/src/messaging/l1_to_l2_message_getter_data.nr index 0f72547..ae710e3 100644 --- a/aztec/src/messaging/l1_to_l2_message_getter_data.nr +++ b/aztec/src/messaging/l1_to_l2_message_getter_data.nr @@ -1,11 +1,5 @@ use crate::messaging::l1_to_l2_message::L1ToL2Message; -use dep::protocol_types::{ - constants::{ - L1_TO_L2_MSG_TREE_HEIGHT, - L1_TO_L2_MESSAGE_LENGTH, - }, - utils::arr_copy_slice, -}; +use dep::protocol_types::{constants::{L1_TO_L2_MSG_TREE_HEIGHT, L1_TO_L2_MESSAGE_LENGTH}, utils::arr_copy_slice}; struct L1ToL2MessageGetterData { message: L1ToL2Message, diff --git a/aztec/src/note/lifecycle.nr b/aztec/src/note/lifecycle.nr index 84326f9..dfad659 100644 --- a/aztec/src/note/lifecycle.nr +++ b/aztec/src/note/lifecycle.nr @@ -1,11 +1,7 @@ -use crate::context::{ - PrivateContext, - PublicContext, -}; +use crate::context::{PrivateContext, PublicContext}; use crate::note::{ - note_header::NoteHeader, - note_interface::NoteInterface, - utils::{compute_note_hash_for_insertion, compute_note_hash_for_consumption}, + note_header::NoteHeader, note_interface::NoteInterface, + utils::{compute_note_hash_for_insertion, compute_note_hash_for_consumption} }; use crate::oracle::notes::{notify_created_note, notify_nullified_note}; @@ -25,7 +21,15 @@ pub fn create_note( // TODO: Strong typing required because of https://github.com/noir-lang/noir/issues/4088 let serialized_note: [Field; N] = Note::serialize_content(*note); - assert(notify_created_note(storage_slot, serialized_note, inner_note_hash) == 0); + assert( + notify_created_note( + storage_slot, + Note::get_note_type_id(), + serialized_note, + inner_note_hash + ) + == 0 + ); context.push_new_note_hash(inner_note_hash); @@ -34,7 +38,11 @@ pub fn create_note( } } -pub fn create_note_hash_from_public(context: &mut PublicContext, storage_slot: Field, note: &mut Note) where Note: NoteInterface { +pub fn create_note_hash_from_public( + context: &mut PublicContext, + storage_slot: Field, + note: &mut Note +) where Note: NoteInterface { let contract_address = (*context).this_address(); let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true }; diff --git a/aztec/src/note/note_interface.nr b/aztec/src/note/note_interface.nr index cfe9ea7..a663051 100644 --- a/aztec/src/note/note_interface.nr +++ b/aztec/src/note/note_interface.nr @@ -18,6 +18,8 @@ trait NoteInterface { fn compute_nullifier_without_context(self) -> Field; fn broadcast(self, context: &mut PrivateContext, slot: Field) -> (); + + fn get_note_type_id() -> Field; } // docs:end:note_interface diff --git a/aztec/src/note/note_type_id.nr b/aztec/src/note/note_type_id.nr new file mode 100644 index 0000000..e69de29 diff --git a/aztec/src/note/utils.nr b/aztec/src/note/utils.nr index 81f2f2c..8aa346d 100644 --- a/aztec/src/note/utils.nr +++ b/aztec/src/note/utils.nr @@ -1,20 +1,12 @@ -use crate::{ - context::PrivateContext, - note::{ - note_header::NoteHeader, - note_interface::NoteInterface, - }, -}; +use crate::{context::PrivateContext, note::{note_header::NoteHeader, note_interface::NoteInterface}}; use dep::protocol_types::{ address::AztecAddress, constants::{ - GENERATOR_INDEX__OUTER_NULLIFIER, - GENERATOR_INDEX__UNIQUE_COMMITMENT, - GENERATOR_INDEX__SILOED_COMMITMENT, - }, - hash::pedersen_hash, - utils::arr_copy_slice, + GENERATOR_INDEX__OUTER_NULLIFIER, GENERATOR_INDEX__UNIQUE_COMMITMENT, + GENERATOR_INDEX__SILOED_COMMITMENT +}, + hash::pedersen_hash, utils::arr_copy_slice }; fn compute_siloed_hash(contract_address: AztecAddress, inner_note_hash: Field) -> Field { diff --git a/aztec/src/oracle/call_private_function.nr b/aztec/src/oracle/call_private_function.nr index 489fd38..069247c 100644 --- a/aztec/src/oracle/call_private_function.nr +++ b/aztec/src/oracle/call_private_function.nr @@ -1,10 +1,6 @@ use dep::protocol_types::{ - abis::{ - function_selector::FunctionSelector, - private_call_stack_item::PrivateCallStackItem, - }, - address::AztecAddress, - constants::PRIVATE_CALL_STACK_ITEM_LENGTH, + abis::{function_selector::FunctionSelector, private_call_stack_item::PrivateCallStackItem}, + address::AztecAddress, constants::PRIVATE_CALL_STACK_ITEM_LENGTH }; #[oracle(callPrivateFunction)] diff --git a/aztec/src/oracle/context.nr b/aztec/src/oracle/context.nr index f5fae3b..56e1b22 100644 --- a/aztec/src/oracle/context.nr +++ b/aztec/src/oracle/context.nr @@ -1,7 +1,4 @@ -use dep::protocol_types::address::{ - AztecAddress, - EthAddress, -}; +use dep::protocol_types::address::{AztecAddress, EthAddress}; #[oracle(getPortalContractAddress)] fn _get_portal_address(_contract_address: AztecAddress) -> EthAddress {} diff --git a/aztec/src/oracle/enqueue_public_function_call.nr b/aztec/src/oracle/enqueue_public_function_call.nr index d48f6b2..d5bafbc 100644 --- a/aztec/src/oracle/enqueue_public_function_call.nr +++ b/aztec/src/oracle/enqueue_public_function_call.nr @@ -1,7 +1,4 @@ -use dep::protocol_types::{ - abis::function_selector::FunctionSelector, - address::AztecAddress, -}; +use dep::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress}; // contract_address + // args_hash + diff --git a/aztec/src/oracle/get_membership_witness.nr b/aztec/src/oracle/get_membership_witness.nr index 14d5b9e..1e82b12 100644 --- a/aztec/src/oracle/get_membership_witness.nr +++ b/aztec/src/oracle/get_membership_witness.nr @@ -1,11 +1,4 @@ -use dep::protocol_types::{ - constants::{ - ARCHIVE_HEIGHT, - CONTRACT_TREE_HEIGHT, - NOTE_HASH_TREE_HEIGHT, - }, - utils::arr_copy_slice, -}; +use dep::protocol_types::{constants::{ARCHIVE_HEIGHT, CONTRACT_TREE_HEIGHT, NOTE_HASH_TREE_HEIGHT}, utils::arr_copy_slice}; global CONTRACT_TREE_ID = 0; global NOTE_HASH_TREE_ID = 2; @@ -24,9 +17,17 @@ struct MembershipWitness { } #[oracle(getMembershipWitness)] -fn get_membership_witness_oracle(_block_number: u32, _tree_id: Field, _leaf_value: Field) -> [Field; M] {} +fn get_membership_witness_oracle( + _block_number: u32, + _tree_id: Field, + _leaf_value: Field +) -> [Field; M] {} -unconstrained pub fn get_membership_witness(block_number: u32, tree_id: Field, leaf_value: Field) -> MembershipWitness { +unconstrained pub fn get_membership_witness( + block_number: u32, + tree_id: Field, + leaf_value: Field +) -> MembershipWitness { let fields: [Field; M] = get_membership_witness_oracle(block_number, tree_id, leaf_value); MembershipWitness { index: fields[0], path: arr_copy_slice(fields, [0; N], 1) } } diff --git a/aztec/src/oracle/get_nullifier_membership_witness.nr b/aztec/src/oracle/get_nullifier_membership_witness.nr index 3cf667c..22d9799 100644 --- a/aztec/src/oracle/get_nullifier_membership_witness.nr +++ b/aztec/src/oracle/get_nullifier_membership_witness.nr @@ -1,11 +1,6 @@ use dep::protocol_types::{ - abis::nullifier_leaf_preimage::{ - NullifierLeafPreimage, - NULLIFIER_LEAF_PREIMAGE_LENGTH, - }, - constants::NULLIFIER_TREE_HEIGHT, - hash::pedersen_hash, - utils::arr_copy_slice, + abis::nullifier_leaf_preimage::{NullifierLeafPreimage, NULLIFIER_LEAF_PREIMAGE_LENGTH}, + constants::NULLIFIER_TREE_HEIGHT, hash::pedersen_hash, utils::arr_copy_slice }; // INDEX_LENGTH + NULLIFIER_LEAF_PREIMAGE_LENGTH + NULLIFIER_TREE_HEIGHT @@ -23,7 +18,11 @@ impl NullifierMembershipWitness { Self { index: fields[0], leaf_preimage: NullifierLeafPreimage::deserialize(leaf_preimage_fields), - path: arr_copy_slice(fields, [0; NULLIFIER_TREE_HEIGHT], 1 + NULLIFIER_LEAF_PREIMAGE_LENGTH) + path: arr_copy_slice( + fields, + [0; NULLIFIER_TREE_HEIGHT], + 1 + NULLIFIER_LEAF_PREIMAGE_LENGTH + ) } } } @@ -42,7 +41,10 @@ unconstrained pub fn get_low_nullifier_membership_witness(block_number: u32, nul } #[oracle(getNullifierMembershipWitness)] -fn get_nullifier_membership_witness_oracle(_block_number: u32, _nullifier: Field) -> [Field; NULLIFIER_MEMBERSHIP_WITNESS] {} +fn get_nullifier_membership_witness_oracle( + _block_number: u32, + _nullifier: Field +) -> [Field; NULLIFIER_MEMBERSHIP_WITNESS] {} // Nullifier here refers to the nullifier we are looking to get non-inclusion proof for (by proving that a lower // nullifier's next_value is bigger than the nullifier) diff --git a/aztec/src/oracle/get_public_data_witness.nr b/aztec/src/oracle/get_public_data_witness.nr index 1e843f7..c7a8f53 100644 --- a/aztec/src/oracle/get_public_data_witness.nr +++ b/aztec/src/oracle/get_public_data_witness.nr @@ -1,12 +1,7 @@ use dep::protocol_types::{ - constants::PUBLIC_DATA_TREE_HEIGHT, - hash::pedersen_hash, - public_data_tree_leaf_preimage::PublicDataTreeLeafPreimage, - traits::{ - Hash, - Serialize, - }, - utils::arr_copy_slice, + constants::PUBLIC_DATA_TREE_HEIGHT, hash::pedersen_hash, + public_data_tree_leaf_preimage::PublicDataTreeLeafPreimage, traits::{Hash, Serialize}, + utils::arr_copy_slice }; global LEAF_PREIMAGE_LENGTH: Field = 4; @@ -19,7 +14,10 @@ struct PublicDataWitness { } #[oracle(getPublicDataTreeWitness)] -fn get_public_data_witness_oracle(_block_number: u32, _leaf_slot: Field) -> [Field; PUBLIC_DATA_WITNESS] {} +fn get_public_data_witness_oracle( + _block_number: u32, + _leaf_slot: Field +) -> [Field; PUBLIC_DATA_WITNESS] {} unconstrained pub fn get_public_data_witness(block_number: u32, leaf_slot: Field) -> PublicDataWitness { let fields = get_public_data_witness_oracle(block_number, leaf_slot); diff --git a/aztec/src/oracle/get_public_key.nr b/aztec/src/oracle/get_public_key.nr index 5aea451..abb7167 100644 --- a/aztec/src/oracle/get_public_key.nr +++ b/aztec/src/oracle/get_public_key.nr @@ -1,11 +1,4 @@ -use dep::protocol_types::{ - address::{ - AztecAddress, - PartialAddress, - PublicKeysHash, - }, - grumpkin_point::GrumpkinPoint, -}; +use dep::protocol_types::{address::{AztecAddress, PartialAddress, PublicKeysHash}, grumpkin_point::GrumpkinPoint}; #[oracle(getPublicKeyAndPartialAddress)] fn get_public_key_and_partial_address_oracle(_address: AztecAddress) -> [Field; 3] {} diff --git a/aztec/src/oracle/header.nr b/aztec/src/oracle/header.nr index bcb7027..14e6eb6 100644 --- a/aztec/src/oracle/header.nr +++ b/aztec/src/oracle/header.nr @@ -1,13 +1,7 @@ use dep::std::merkle::compute_merkle_root; -use dep::protocol_types::{ - constants::HEADER_LENGTH, - header::Header, -}; - -use crate::{ - context::PrivateContext, - oracle::get_membership_witness::get_archive_membership_witness, -}; +use dep::protocol_types::{constants::HEADER_LENGTH, header::Header}; + +use crate::{context::PrivateContext, oracle::get_membership_witness::get_archive_membership_witness}; #[oracle(getHeader)] fn get_header_at_oracle(_block_number: u32) -> [Field; HEADER_LENGTH] {} diff --git a/aztec/src/oracle/logs.nr b/aztec/src/oracle/logs.nr index eefed0a..1bbede4 100644 --- a/aztec/src/oracle/logs.nr +++ b/aztec/src/oracle/logs.nr @@ -1,14 +1,11 @@ -use dep::protocol_types::{ - address::AztecAddress, - constants::NUM_FIELDS_PER_SHA256, - grumpkin_point::GrumpkinPoint, -}; +use dep::protocol_types::{address::AztecAddress, constants::NUM_FIELDS_PER_SHA256, grumpkin_point::GrumpkinPoint}; // TODO: Should take encrypted data. #[oracle(emitEncryptedLog)] fn emit_encrypted_log_oracle( _contract_address: AztecAddress, _storage_slot: Field, + _note_type_id: Field, _encryption_pub_key: GrumpkinPoint, _preimage: [Field; N] ) -> Field {} @@ -16,14 +13,27 @@ fn emit_encrypted_log_oracle( unconstrained pub fn emit_encrypted_log( contract_address: AztecAddress, storage_slot: Field, + note_type_id: Field, encryption_pub_key: GrumpkinPoint, preimage: [Field; N] ) -> [Field; NUM_FIELDS_PER_SHA256] { - [emit_encrypted_log_oracle(contract_address, storage_slot, encryption_pub_key, preimage), 0] + [ + emit_encrypted_log_oracle( + contract_address, + storage_slot, + note_type_id, + encryption_pub_key, + preimage + ), 0 + ] } #[oracle(emitUnencryptedLog)] -fn emit_unencrypted_log_oracle(_contract_address: AztecAddress, _event_selector: Field, _message: T) -> Field {} +fn emit_unencrypted_log_oracle( + _contract_address: AztecAddress, + _event_selector: Field, + _message: T +) -> Field {} unconstrained pub fn emit_unencrypted_log( contract_address: AztecAddress, diff --git a/aztec/src/oracle/notes.nr b/aztec/src/oracle/notes.nr index f6378c1..42b2f35 100644 --- a/aztec/src/oracle/notes.nr +++ b/aztec/src/oracle/notes.nr @@ -10,10 +10,20 @@ use dep::protocol_types::{ }; #[oracle(notifyCreatedNote)] -fn notify_created_note_oracle(_storage_slot: Field, _serialized_note: [Field; N], _inner_note_hash: Field) -> Field {} +fn notify_created_note_oracle( + _storage_slot: Field, + _note_type_id: Field, + _serialized_note: [Field; N], + _inner_note_hash: Field +) -> Field {} -unconstrained pub fn notify_created_note(storage_slot: Field, serialized_note: [Field; N], inner_note_hash: Field) -> Field { - notify_created_note_oracle(storage_slot, serialized_note, inner_note_hash) +unconstrained pub fn notify_created_note( + storage_slot: Field, + note_type_id: Field, + serialized_note: [Field; N], + inner_note_hash: Field +) -> Field { + notify_created_note_oracle(storage_slot, note_type_id, serialized_note, inner_note_hash) } #[oracle(notifyNullifiedNote)] diff --git a/aztec/src/oracle/nullifier_key.nr b/aztec/src/oracle/nullifier_key.nr index a99b946..c3429d4 100644 --- a/aztec/src/oracle/nullifier_key.nr +++ b/aztec/src/oracle/nullifier_key.nr @@ -1,8 +1,4 @@ -use dep::protocol_types::{ - address::AztecAddress, - grumpkin_point::GrumpkinPoint, - grumpkin_private_key::GrumpkinPrivateKey, -}; +use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint, grumpkin_private_key::GrumpkinPrivateKey}; struct NullifierKeyPair { account: AztecAddress, diff --git a/aztec/src/oracle/public_call.nr b/aztec/src/oracle/public_call.nr index 0bc8197..26b29d9 100644 --- a/aztec/src/oracle/public_call.nr +++ b/aztec/src/oracle/public_call.nr @@ -1,8 +1,4 @@ -use dep::protocol_types::{ - abis::function_selector::FunctionSelector, - address::AztecAddress, - constants::RETURN_VALUES_LENGTH, -}; +use dep::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress, constants::RETURN_VALUES_LENGTH}; #[oracle(callPublicFunction)] fn call_public_function_oracle( diff --git a/aztec/src/state_vars/immutable_singleton.nr b/aztec/src/state_vars/immutable_singleton.nr index ab67674..fed2d66 100644 --- a/aztec/src/state_vars/immutable_singleton.nr +++ b/aztec/src/state_vars/immutable_singleton.nr @@ -1,18 +1,10 @@ use dep::std::option::Option; -use dep::protocol_types::{ - address::AztecAddress, - constants::{ - GENERATOR_INDEX__INITIALIZATION_NULLIFIER, - }, - hash::pedersen_hash, -}; +use dep::protocol_types::{address::AztecAddress, constants::{GENERATOR_INDEX__INITIALIZATION_NULLIFIER}, hash::pedersen_hash}; use crate::context::{PrivateContext, Context}; use crate::note::{ - lifecycle::create_note, - note_getter::{get_note, view_notes}, - note_interface::NoteInterface, - note_viewer_options::NoteViewerOptions, + lifecycle::create_note, note_getter::{get_note, view_notes}, note_interface::NoteInterface, + note_viewer_options::NoteViewerOptions }; use crate::oracle::notes::check_nullifier_exists; use crate::state_vars::storage::Storage; @@ -28,15 +20,9 @@ impl Storage for ImmutableSingleton {} impl ImmutableSingleton { // docs:start:new - pub fn new( - context: Context, - storage_slot: Field, - ) -> Self { + pub fn new(context: Context, storage_slot: Field) -> Self { assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); - Self { - context: context.private, - storage_slot, - } + Self { context: context.private, storage_slot } } // docs:end:new @@ -47,7 +33,10 @@ impl ImmutableSingleton { // This is especially dangerous for initial assignment to elements of a `Map` type (for example), because the storage slot often also identifies an actor. // e.g. the initial assignment to `my_map.at(msg.sender)` will leak: `msg.sender`, the fact that an element of `my_map` was assigned-to for the first time, and the contract_address. pub fn compute_initialization_nullifier(self) -> Field { - pedersen_hash([self.storage_slot], GENERATOR_INDEX__INITIALIZATION_NULLIFIER) + pedersen_hash( + [self.storage_slot], + GENERATOR_INDEX__INITIALIZATION_NULLIFIER + ) } // docs:start:is_initialized @@ -58,23 +47,14 @@ impl ImmutableSingleton { // docs:end:is_initialized // docs:start:initialize - pub fn initialize( - self, - note: &mut Note, - broadcast: bool, - ) where Note: NoteInterface { + pub fn initialize(self, note: &mut Note, broadcast: bool) where Note: NoteInterface { let context = self.context.unwrap(); // Nullify the storage slot. let nullifier = self.compute_initialization_nullifier(); context.push_new_nullifier(nullifier, 0); - create_note( - context, - self.storage_slot, - note, - broadcast, - ); + create_note(context, self.storage_slot, note, broadcast); } // docs:end:initialize diff --git a/aztec/src/state_vars/map.nr b/aztec/src/state_vars/map.nr index 7e38552..043c8a3 100644 --- a/aztec/src/state_vars/map.nr +++ b/aztec/src/state_vars/map.nr @@ -1,9 +1,6 @@ use crate::context::{PrivateContext, PublicContext, Context}; use dep::std::option::Option; -use dep::protocol_types::{ - hash::pedersen_hash, - traits::{ToField} -}; +use dep::protocol_types::{hash::pedersen_hash, traits::{ToField}}; use crate::state_vars::storage::Storage; // docs:start:map @@ -21,21 +18,17 @@ impl Map { pub fn new( context: Context, storage_slot: Field, - state_var_constructor: fn(Context, Field) -> V, + state_var_constructor: fn(Context, Field) -> V ) -> Self { assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); - Map { - context, - storage_slot, - state_var_constructor, - } + Map { context, storage_slot, state_var_constructor } } // docs:end:new // docs:start:at pub fn at(self, key: K) -> V where K: ToField { // TODO(#1204): use a generator index for the storage slot - let derived_storage_slot = pedersen_hash([self.storage_slot, key.to_field()],0); + let derived_storage_slot = pedersen_hash([self.storage_slot, key.to_field()], 0); let state_var_constructor = self.state_var_constructor; state_var_constructor(self.context, derived_storage_slot) diff --git a/aztec/src/state_vars/public_state.nr b/aztec/src/state_vars/public_state.nr index 081b0d5..883a2f7 100644 --- a/aztec/src/state_vars/public_state.nr +++ b/aztec/src/state_vars/public_state.nr @@ -19,13 +19,10 @@ impl PublicState { pub fn new( // Note: Passing the contexts to new(...) just to have an interface compatible with a Map. context: Context, - storage_slot: Field, + storage_slot: Field ) -> Self { assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); - PublicState { - context, - storage_slot, - } + PublicState { context, storage_slot } } // docs:end:public_state_struct_new diff --git a/aztec/src/state_vars/set.nr b/aztec/src/state_vars/set.nr index 7144bbe..2a56fb1 100644 --- a/aztec/src/state_vars/set.nr +++ b/aztec/src/state_vars/set.nr @@ -1,17 +1,14 @@ use dep::std::option::Option; use dep::protocol_types::{ constants::{MAX_NOTES_PER_PAGE, MAX_READ_REQUESTS_PER_CALL}, - abis::side_effect::{SideEffect, SideEffectLinkedToNoteHash}, + abis::side_effect::{SideEffect, SideEffectLinkedToNoteHash} }; use crate::context::{PrivateContext, PublicContext, Context}; use crate::note::{ lifecycle::{create_note, create_note_hash_from_public, destroy_note}, - note_getter::{get_notes, view_notes}, - note_getter_options::NoteGetterOptions, - note_header::NoteHeader, - note_interface::NoteInterface, - note_viewer_options::NoteViewerOptions, - utils::compute_note_hash_for_consumption, + note_getter::{get_notes, view_notes}, note_getter_options::NoteGetterOptions, + note_header::NoteHeader, note_interface::NoteInterface, note_viewer_options::NoteViewerOptions, + utils::compute_note_hash_for_consumption }; use crate::state_vars::storage::Storage; @@ -26,49 +23,40 @@ impl Storage for Set {} impl Set { // docs:start:new - pub fn new( - context: Context, - storage_slot: Field, - ) -> Self { + pub fn new(context: Context, storage_slot: Field) -> Self { assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); - Set { - context, - storage_slot, - } + Set { context, storage_slot } } // docs:end:new // docs:start:insert - pub fn insert(self, - note: &mut Note, - broadcast: bool, - ) where Note: NoteInterface { + pub fn insert(self, note: &mut Note, broadcast: bool) where Note: NoteInterface { create_note( self.context.private.unwrap(), self.storage_slot, note, - broadcast, + broadcast ); } // docs:end:insert // docs:start:insert_from_public pub fn insert_from_public(self, note: &mut Note) where Note: NoteInterface { - create_note_hash_from_public( - self.context.public.unwrap(), - self.storage_slot, - note, - ); + create_note_hash_from_public(self.context.public.unwrap(), self.storage_slot, note); } // docs:end:insert_from_public - + // DEPRECATED fn assert_contains_and_remove(_self: Self, _note: &mut Note, _nonce: Field) { - assert(false, "`assert_contains_and_remove` has been deprecated. Please call PXE.addNote() to add a note to the database. Then use Set.get_notes() and Set.remove() in your contract to verify and remove a note."); + assert( + false, "`assert_contains_and_remove` has been deprecated. Please call PXE.addNote() to add a note to the database. Then use Set.get_notes() and Set.remove() in your contract to verify and remove a note." + ); } // DEPRECATED fn assert_contains_and_remove_publicly_created(_self: Self, _note: &mut Note) { - assert(false, "`assert_contains_and_remove_publicly_created` has been deprecated. Please call PXE.addNote() to add a note to the database. Then use Set.get_notes() and Set.remove() in your contract to verify and remove a note."); + assert( + false, "`assert_contains_and_remove_publicly_created` has been deprecated. Please call PXE.addNote() to add a note to the database. Then use Set.get_notes() and Set.remove() in your contract to verify and remove a note." + ); } // docs:start:remove @@ -78,24 +66,17 @@ impl Set { let has_been_read = context.read_requests.any(|r: SideEffect| r.value == note_hash); assert(has_been_read, "Can only remove a note that has been read from the set."); - destroy_note( - context, - note, - ); + destroy_note(context, note); } // docs:end:remove // docs:start:get_notes pub fn get_notes( self, - options: NoteGetterOptions, + options: NoteGetterOptions ) -> [Option; MAX_READ_REQUESTS_PER_CALL] where Note: NoteInterface { let storage_slot = self.storage_slot; - let opt_notes = get_notes( - self.context.private.unwrap(), - storage_slot, - options, - ); + let opt_notes = get_notes(self.context.private.unwrap(), storage_slot, options); opt_notes } // docs:end:get_notes @@ -103,7 +84,7 @@ impl Set { // docs:start:view_notes unconstrained pub fn view_notes( self, - options: NoteViewerOptions, + options: NoteViewerOptions ) -> [Option; MAX_NOTES_PER_PAGE] where Note: NoteInterface { view_notes(self.storage_slot, options) } diff --git a/aztec/src/state_vars/singleton.nr b/aztec/src/state_vars/singleton.nr index 04f96e9..49d34b0 100644 --- a/aztec/src/state_vars/singleton.nr +++ b/aztec/src/state_vars/singleton.nr @@ -1,24 +1,13 @@ use dep::std::option::Option; -use dep::protocol_types::{ - address::AztecAddress, - constants::{ - GENERATOR_INDEX__INITIALIZATION_NULLIFIER, - }, - hash::pedersen_hash, -}; +use dep::protocol_types::{address::AztecAddress, constants::{GENERATOR_INDEX__INITIALIZATION_NULLIFIER}, hash::pedersen_hash}; use crate::context::{PrivateContext, PublicContext, Context}; use crate::note::{ - lifecycle::{create_note, destroy_note}, - note_getter::{get_note, view_notes}, - note_interface::NoteInterface, - note_viewer_options::NoteViewerOptions, -}; -use crate::oracle::{ - nullifier_key::get_nullifier_secret_key, - notes::check_nullifier_exists, + lifecycle::{create_note, destroy_note}, note_getter::{get_note, view_notes}, + note_interface::NoteInterface, note_viewer_options::NoteViewerOptions }; +use crate::oracle::{nullifier_key::get_nullifier_secret_key, notes::check_nullifier_exists}; use crate::state_vars::storage::Storage; // docs:start:struct @@ -32,15 +21,9 @@ impl Storage for Singleton {} impl Singleton { // docs:start:new - pub fn new( - context: Context, - storage_slot: Field, - ) -> Self { + pub fn new(context: Context, storage_slot: Field) -> Self { assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); - Self { - context: context.private, - storage_slot, - } + Self { context: context.private, storage_slot } } // docs:end:new @@ -53,7 +36,10 @@ impl Singleton { // Note: subsequent nullification of this state variable, via the `replace` method will not be leaky, if the `compute_nullifier()` method of the underlying note is designed to ensure privacy. // For example, if the `compute_nullifier()` method injects the secret key of a note owner into the computed nullifier's preimage. pub fn compute_initialization_nullifier(self) -> Field { - pedersen_hash([self.storage_slot], GENERATOR_INDEX__INITIALIZATION_NULLIFIER) + pedersen_hash( + [self.storage_slot], + GENERATOR_INDEX__INITIALIZATION_NULLIFIER + ) } // docs:start:is_initialized @@ -64,11 +50,7 @@ impl Singleton { // docs:end:is_initialized // docs:start:initialize - pub fn initialize( - self, - note: &mut Note, - broadcast: bool, - ) where Note: NoteInterface { + pub fn initialize(self, note: &mut Note, broadcast: bool) where Note: NoteInterface { let context = self.context.unwrap(); // Nullify the storage slot. @@ -80,11 +62,7 @@ impl Singleton { // docs:end:initialize // docs:start:replace - pub fn replace( - self, - new_note: &mut Note, - broadcast: bool, - ) where Note: NoteInterface { + pub fn replace(self, new_note: &mut Note, broadcast: bool) where Note: NoteInterface { let context = self.context.unwrap(); let prev_note = get_note(context, self.storage_slot); diff --git a/aztec/src/state_vars/stable_public_state.nr b/aztec/src/state_vars/stable_public_state.nr index e42b107..9612fb3 100644 --- a/aztec/src/state_vars/stable_public_state.nr +++ b/aztec/src/state_vars/stable_public_state.nr @@ -1,7 +1,5 @@ use crate::context::{Context}; -use crate::oracle::{ - storage::{storage_read, storage_write}, -}; +use crate::oracle::{storage::{storage_read, storage_write}}; use crate::history::public_value_inclusion::prove_public_value_inclusion; use dep::std::option::Option; use dep::protocol_types::traits::{Deserialize, Serialize}; @@ -21,10 +19,7 @@ impl StablePublicState { storage_slot: Field ) -> Self { assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); - Self { - context, - storage_slot, - } + Self { context, storage_slot } } // Intended to be only called once. @@ -59,10 +54,9 @@ impl StablePublicState { fields[i], self.storage_slot + i, (*private_context).this_address(), - (*private_context), + (*private_context) ) } T::deserialize(fields) } - } diff --git a/compressed-string/src/compressed_string.nr b/compressed-string/src/compressed_string.nr index 7f7945d..4900f10 100644 --- a/compressed-string/src/compressed_string.nr +++ b/compressed-string/src/compressed_string.nr @@ -1,7 +1,4 @@ -use dep::aztec::protocol_types::{ - utils::field::field_from_bytes, - traits::{Serialize, Deserialize} -}; +use dep::aztec::protocol_types::{utils::field::field_from_bytes, traits::{Serialize, Deserialize}}; use dep::std; // A Fixedsize Compressed String. @@ -22,27 +19,27 @@ impl Deserialize<1> for FieldCompressedString { } } -impl FieldCompressedString{ - pub fn is_eq(self, other: FieldCompressedString) -> bool { - self.value == other.value - } +impl FieldCompressedString { + pub fn is_eq(self, other: FieldCompressedString) -> bool { + self.value == other.value + } - pub fn from_field(input_field: Field) -> Self { - Self {value: input_field} - } + pub fn from_field(input_field: Field) -> Self { + Self { value: input_field } + } - pub fn from_string(input_string: str<31>) -> Self { - Self {value: field_from_bytes(input_string.as_bytes(), true)} - } + pub fn from_string(input_string: str<31>) -> Self { + Self { value: field_from_bytes(input_string.as_bytes(), true) } + } - pub fn to_bytes(self) -> [u8; 31] { - let mut result = [0; 31]; - let bytes = self.value.to_be_bytes(31); - for i in 0..31 { - result[i] = bytes[i]; + pub fn to_bytes(self) -> [u8; 31] { + let mut result = [0; 31]; + let bytes = self.value.to_be_bytes(31); + for i in 0..31 { + result[i] = bytes[i]; + } + result } - result - } } // The general Compressed String. @@ -54,49 +51,49 @@ struct CompressedString { } impl CompressedString { - pub fn from_string(input_string: str) -> Self { - let mut fields = [0; N]; - let byts = input_string.as_bytes(); - - let mut r_index = 0 as u32; - - for i in 0..N { - let mut temp = [0 as u8; 31]; - for j in 0..31 { - if r_index < M { - temp[j] = byts[r_index]; - r_index += 1; + pub fn from_string(input_string: str) -> Self { + let mut fields = [0; N]; + let byts = input_string.as_bytes(); + + let mut r_index = 0 as u32; + + for i in 0..N { + let mut temp = [0 as u8; 31]; + for j in 0..31 { + if r_index < M { + temp[j] = byts[r_index]; + r_index += 1; + } + } + + fields[i] = field_from_bytes(temp, true); } - } - fields[i] = field_from_bytes(temp, true); + Self { value: fields } } - Self { value: fields } - } - - pub fn to_bytes(self) -> [u8; M] { - let mut result = [0; M]; - let mut w_index = 0 as u32; - for i in 0..N { - let bytes = self.value[i].to_be_bytes(31); - for j in 0..31 { - if w_index < M { - result[w_index] = bytes[j]; - w_index += 1; + pub fn to_bytes(self) -> [u8; M] { + let mut result = [0; M]; + let mut w_index = 0 as u32; + for i in 0..N { + let bytes = self.value[i].to_be_bytes(31); + for j in 0..31 { + if w_index < M { + result[w_index] = bytes[j]; + w_index += 1; + } + } } - } + result } - result - } - pub fn serialize(self) -> [Field; N] { - self.value - } + pub fn serialize(self) -> [Field; N] { + self.value + } - pub fn deserialize(input: [Field; N]) -> Self { - Self { value: input } - } + pub fn deserialize(input: [Field; N]) -> Self { + Self { value: input } + } } #[test] diff --git a/field-note/src/field_note.nr b/field-note/src/field_note.nr index cde638d..1f54659 100644 --- a/field-note/src/field_note.nr +++ b/field-note/src/field_note.nr @@ -1,10 +1,6 @@ use dep::aztec::{ - note::{ - note_header::NoteHeader, - note_interface::NoteInterface, - }, - hash::pedersen_hash, - context::PrivateContext, + note::{note_header::NoteHeader, note_interface::NoteInterface}, hash::pedersen_hash, + context::PrivateContext }; global FIELD_NOTE_LEN: Field = 1; @@ -57,14 +53,17 @@ impl NoteInterface for FieldNote { false, "FieldNote does not support broadcast. Add it to PXE directly using the `.addNote` function." ); } + + fn get_note_type_id() -> Field { + // TODO(#4519): autogenerate + // python -c "print(int(''.join(str(ord(c)) for c in 'FieldNote')))" + 7010510110810078111116101 + } } impl FieldNote { pub fn new(value: Field) -> Self { - FieldNote { - value, - header: NoteHeader::empty(), - } + FieldNote { value, header: NoteHeader::empty() } } } diff --git a/safe-math/src/safe_u120.nr b/safe-math/src/safe_u120.nr index 4ef341b..74acd0d 100644 --- a/safe-math/src/safe_u120.nr +++ b/safe-math/src/safe_u120.nr @@ -25,7 +25,8 @@ impl Serialize for SafeU120 { impl Deserialize for SafeU120 { // This is safe when reading from storage IF only correct safeu120 was written to storage fn deserialize(fields: [Field; SAFE_U120_SERIALIZED_LEN]) -> SafeU120 { - SafeU120 { value: fields[0] as u120 } + let value = fields[0] as u120; + SafeU120 { value } } } @@ -50,9 +51,9 @@ impl SafeU120 { for i in 0..17 { assert(bytes[i] == 0, "Value too large for SafeU120"); } - Self { - value: value as u120 - } + + let value = value as u120; + Self { value } } pub fn is_zero( diff --git a/value-note/src/balance_utils.nr b/value-note/src/balance_utils.nr index 74c6c79..570f0cc 100644 --- a/value-note/src/balance_utils.nr +++ b/value-note/src/balance_utils.nr @@ -1,7 +1,4 @@ -use dep::aztec::note::{ - note_getter::view_notes, - note_viewer_options::NoteViewerOptions, -}; +use dep::aztec::note::{note_getter::view_notes, note_viewer_options::NoteViewerOptions}; use dep::aztec::state_vars::set::Set; use crate::value_note::{VALUE_NOTE_LEN, ValueNote}; diff --git a/value-note/src/value_note.nr b/value-note/src/value_note.nr index 6334a6f..f29769e 100644 --- a/value-note/src/value_note.nr +++ b/value-note/src/value_note.nr @@ -1,21 +1,8 @@ use dep::aztec::{ - protocol_types::{ - address::AztecAddress, - traits::{Deserialize, Serialize} - }, - note::{ - note_header::NoteHeader, - note_interface::NoteInterface, - utils::compute_note_hash_for_consumption, - }, - oracle::{ - rand::rand, - nullifier_key::get_nullifier_secret_key, - get_public_key::get_public_key, - }, - log::emit_encrypted_log, - hash::pedersen_hash, - context::PrivateContext, + protocol_types::{address::AztecAddress, traits::{Deserialize, Serialize}}, + note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption}, + oracle::{rand::rand, nullifier_key::get_nullifier_secret_key, get_public_key::get_public_key}, + log::emit_encrypted_log, hash::pedersen_hash, context::PrivateContext }; global VALUE_NOTE_LEN: Field = 3; // 3 plus a header. @@ -89,21 +76,23 @@ impl NoteInterface for ValueNote { context, (*context).this_address(), slot, + Self::get_note_type_id(), encryption_pub_key, self.serialize_content(), ); } + + fn get_note_type_id() -> Field { + // TODO(#4519): autogenerate + // python -c "print(int(''.join(str(ord(c)) for c in 'ValueNote')))" + 869710811710178111116101 + } } impl ValueNote { pub fn new(value: Field, owner: AztecAddress) -> Self { let randomness = rand(); let header = NoteHeader::empty(); - ValueNote { - value, - owner, - randomness, - header, - } + ValueNote { value, owner, randomness, header } } }