diff --git a/noir-projects/aztec-nr/authwit/src/entrypoint/app.nr b/noir-projects/aztec-nr/authwit/src/entrypoint/app.nr index a909fe2261c..16fcd8bed86 100644 --- a/noir-projects/aztec-nr/authwit/src/entrypoint/app.nr +++ b/noir-projects/aztec-nr/authwit/src/entrypoint/app.nr @@ -1,14 +1,15 @@ -use dep::aztec::prelude::PrivateContext; -use dep::aztec::protocol_types::{ - constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD, - hash::poseidon2_hash_with_separator, - traits::{Hash, Serialize}, +use dep::aztec::{ + prelude::PrivateContext, + protocol_types::{ + constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD, + hash::poseidon2_hash_with_separator, + traits::{Hash, Serialize}, + }, }; +use std::meta::derive; use crate::entrypoint::function_call::FunctionCall; -// FUNCTION_CALL_SIZE * ACCOUNT_MAX_CALLS + 1 -global APP_PAYLOAD_SIZE: u32 = 21; // FUNCTION_CALL_SIZE_IN_BYTES * ACCOUNT_MAX_CALLS + 32 global APP_PAYLOAD_SIZE_IN_BYTES: u32 = 424; @@ -16,24 +17,13 @@ global ACCOUNT_MAX_CALLS: u32 = 4; // Note: If you change the following struct you have to update default_entrypoint.ts // docs:start:app-payload-struct +#[derive(Serialize)] pub 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(); - 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 { poseidon2_hash_with_separator(self.serialize(), GENERATOR_INDEX__SIGNATURE_PAYLOAD) diff --git a/noir-projects/aztec-nr/authwit/src/entrypoint/fee.nr b/noir-projects/aztec-nr/authwit/src/entrypoint/fee.nr index 3825b1dc673..8073ab920bc 100644 --- a/noir-projects/aztec-nr/authwit/src/entrypoint/fee.nr +++ b/noir-projects/aztec-nr/authwit/src/entrypoint/fee.nr @@ -1,13 +1,13 @@ use crate::entrypoint::function_call::FunctionCall; -use dep::aztec::prelude::PrivateContext; -use dep::aztec::protocol_types::{ - constants::GENERATOR_INDEX__FEE_PAYLOAD, - hash::poseidon2_hash_with_separator, - traits::{Hash, Serialize}, +use dep::aztec::{ + prelude::PrivateContext, + protocol_types::{ + constants::GENERATOR_INDEX__FEE_PAYLOAD, + hash::poseidon2_hash_with_separator, + traits::{Hash, Serialize}, + }, }; - -// 2 * 5 (FUNCTION_CALL_SIZE) + 2 -global FEE_PAYLOAD_SIZE: u32 = 12; +use std::meta::derive; // 2 * 98 (FUNCTION_CALL_SIZE_IN_BYTES) + 32 global FEE_PAYLOAD_SIZE_IN_BYTES: u32 = 228; @@ -15,6 +15,7 @@ global FEE_PAYLOAD_SIZE_IN_BYTES: u32 = 228; global MAX_FEE_FUNCTION_CALLS: u32 = 2; // docs:start:fee-payload-struct +#[derive(Serialize)] pub struct FeePayload { function_calls: [FunctionCall; MAX_FEE_FUNCTION_CALLS], nonce: Field, @@ -22,19 +23,6 @@ pub struct FeePayload { } // 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(); - for i in 0..MAX_FEE_FUNCTION_CALLS { - fields.extend_from_array(self.function_calls[i].serialize()); - } - fields.push(self.nonce); - fields.push(self.is_fee_payer as Field); - fields.storage() - } -} - impl Hash for FeePayload { fn hash(self) -> Field { poseidon2_hash_with_separator(self.serialize(), GENERATOR_INDEX__FEE_PAYLOAD) diff --git a/noir-projects/aztec-nr/authwit/src/entrypoint/function_call.nr b/noir-projects/aztec-nr/authwit/src/entrypoint/function_call.nr index cd5a5730fb9..2f54c85076b 100644 --- a/noir-projects/aztec-nr/authwit/src/entrypoint/function_call.nr +++ b/noir-projects/aztec-nr/authwit/src/entrypoint/function_call.nr @@ -1,12 +1,12 @@ use dep::aztec::protocol_types::{ abis::function_selector::FunctionSelector, address::AztecAddress, traits::Serialize, }; +use std::meta::derive; -// 1 (ARGS_HASH) + 1 (FUNCTION_SELECTOR) + 1 (TARGET_ADDRESS) + 1 (IS_PUBLIC) + 1 (IS_STATIC) -global FUNCTION_CALL_SIZE: u32 = 5; // 3 * 32 + 2 global FUNCTION_CALL_SIZE_IN_BYTES: u32 = 98; +#[derive(Serialize)] pub struct FunctionCall { pub args_hash: Field, pub function_selector: FunctionSelector, @@ -15,18 +15,6 @@ pub struct FunctionCall { pub is_static: 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, - self.is_static 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]; diff --git a/noir-projects/aztec-nr/aztec/src/note/note_header.nr b/noir-projects/aztec-nr/aztec/src/note/note_header.nr index 99dc9d5ca1e..b7cf2efed88 100644 --- a/noir-projects/aztec-nr/aztec/src/note/note_header.nr +++ b/noir-projects/aztec-nr/aztec/src/note/note_header.nr @@ -1,7 +1,7 @@ use dep::protocol_types::{address::AztecAddress, traits::{Empty, Serialize}}; +use std::meta::derive; -global NOTE_HEADER_LENGTH: u32 = 4; - +#[derive(Eq, Serialize)] pub struct NoteHeader { pub contract_address: AztecAddress, pub nonce: Field, @@ -20,34 +20,8 @@ impl Empty for NoteHeader { } } -impl Eq for NoteHeader { - fn eq(self, other: Self) -> bool { - (self.contract_address == other.contract_address) - & (self.nonce == other.nonce) - & (self.storage_slot == other.storage_slot) - & (self.note_hash_counter == other.note_hash_counter) - } -} - impl NoteHeader { pub fn new(contract_address: AztecAddress, nonce: Field, storage_slot: Field) -> Self { NoteHeader { contract_address, nonce, storage_slot, note_hash_counter: 0 } } } - -impl Serialize for NoteHeader { - /// The following method is used by implementations of the Serialize trait for notes --> the implementation - // of the Serialize trait for notes needs to be implemented when the note is passed as an argument to a contract - // function --> in that situation the serialize method is called by aztec-nr when computing an arguments hash. - fn serialize(self) -> [Field; NOTE_HEADER_LENGTH] { - // Note: If you change this function don't forget to update implementations of Serialize trait for notes. - // (Serialize trait needs to be implemented for a note when it's passed as an argument to a contract function - // --> then it's used when computing args hash.) - [ - self.contract_address.to_field(), - self.nonce, - self.storage_slot, - self.note_hash_counter as Field, - ] - } -} diff --git a/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr b/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr index eacf34d3914..3888f8fd5bd 100644 --- a/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr +++ b/noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr @@ -1,9 +1,14 @@ use dep::protocol_types::{ address::AztecAddress, contract_instance::ContractInstance, - public_keys::{PUBLIC_KEYS_LENGTH, PublicKeys}, + public_keys::PublicKeys, traits::{Deserialize, Serialize}, }; +use std::meta::derive; + +// The following 2 lines have been added here because derivation of Serialize and Deserialize does not handle imports +use dep::protocol_types::public_keys::{IvpkM, NpkM, OvpkM, TpkM}; +use std::embedded_curve_ops::EmbeddedCurvePoint; use crate::context::call_interfaces::CallInterface; use crate::context::{PrivateContext, PublicContext}; @@ -114,36 +119,8 @@ impl Deployer { } } -// Keys length + address -global TEST_ACCOUNT_LENGTH: u32 = PUBLIC_KEYS_LENGTH + 1; - +#[derive(Deserialize, Serialize)] pub struct TestAccount { pub address: AztecAddress, pub keys: PublicKeys, } - -impl Serialize for TestAccount { - fn serialize(self) -> [Field; TEST_ACCOUNT_LENGTH] { - let mut output = [0; TEST_ACCOUNT_LENGTH]; - - output[0] = self.address.to_field(); - - for i in 0..PUBLIC_KEYS_LENGTH { - output[i + 1] = self.keys.serialize()[i]; - } - output - } -} - -impl Deserialize for TestAccount { - fn deserialize(input: [Field; TEST_ACCOUNT_LENGTH]) -> Self { - let address = AztecAddress::from_field(input[0]); - let mut key_buffer = [0; PUBLIC_KEYS_LENGTH]; - for i in 0..PUBLIC_KEYS_LENGTH { - key_buffer[i] = input[i + 1]; - } - let keys = PublicKeys::deserialize(key_buffer); - - Self { address, keys } - } -} diff --git a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/dapp_payload.nr b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/dapp_payload.nr index c4524a1fa22..4488d3c1d94 100644 --- a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/dapp_payload.nr +++ b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/dapp_payload.nr @@ -1,38 +1,28 @@ -use dep::aztec::prelude::{AztecAddress, PrivateContext}; -use dep::aztec::protocol_types::{ - constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD, - hash::poseidon2_hash_with_separator, - traits::{Hash, Serialize}, +use dep::aztec::{ + prelude::{AztecAddress, PrivateContext}, + protocol_types::{ + constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD, + hash::poseidon2_hash_with_separator, + traits::{Hash, Serialize}, + }, }; +use std::meta::derive; use dep::authwit::entrypoint::function_call::FunctionCall; global DAPP_MAX_CALLS: u32 = 1; -// FUNCTION_CALL_SIZE * DAPP_MAX_CALLS + 1 -global DAPP_PAYLOAD_SIZE: u32 = 6; // FUNCTION_CALL_SIZE_IN_BYTES * DAPP_MAX_CALLS + 32 global DAPP_PAYLOAD_SIZE_IN_BYTES: u32 = 130; // Note: If you change the following struct you have to update default_entrypoint.ts // docs:start:app-payload-struct +#[derive(Serialize)] pub struct DAppPayload { function_calls: [FunctionCall; DAPP_MAX_CALLS], nonce: Field, } // docs:end:app-payload-struct -impl Serialize for DAppPayload { - // Serializes the entrypoint struct - fn serialize(self) -> [Field; DAPP_PAYLOAD_SIZE] { - let mut fields: BoundedVec = BoundedVec::new(); - for call in self.function_calls { - fields.extend_from_array(call.serialize()); - } - fields.push(self.nonce); - fields.storage() - } -} - impl Hash for DAppPayload { fn hash(self) -> Field { poseidon2_hash_with_separator(self.serialize(), GENERATOR_INDEX__SIGNATURE_PAYLOAD) diff --git a/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr b/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr index 326d76efe14..052a577e3cb 100644 --- a/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr +++ b/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr @@ -13,7 +13,9 @@ use dep::aztec::{ state_vars::PrivateSet, }; use dep::value_note::value_note::ValueNote; +use std::meta::derive; +#[derive(Serialize)] pub struct Card { // We use u32s since u16s are unsupported strength: u32, @@ -35,12 +37,6 @@ impl ToField for Card { } } -impl Serialize<2> for Card { - fn serialize(self) -> [Field; 2] { - [self.strength as Field, self.points as Field] - } -} - // docs:start:pure_noir_testing #[test] fn test_to_from_field() { diff --git a/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/class_registered.nr b/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/class_registered.nr index 8e6edbfdb57..69e1cad09ab 100644 --- a/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/class_registered.nr +++ b/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/class_registered.nr @@ -3,7 +3,6 @@ use dep::aztec::protocol_types::{ MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS, REGISTERER_CONTRACT_CLASS_REGISTERED_MAGIC_VALUE, }, contract_class_id::ContractClassId, - traits::Serialize, }; pub struct ContractClassRegistered { @@ -14,9 +13,12 @@ pub struct ContractClassRegistered { packed_public_bytecode: [Field; MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS], } -impl Serialize for ContractClassRegistered { - fn serialize(self: Self) -> [Field; MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS + 5] { +impl ContractClassRegistered { + fn serialize_non_standard( + self: Self, + ) -> [Field; MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS + 5] { let mut packed = [0; MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS + 5]; + // Since we are not yet emitting selectors we'll use this magic value to identify events emitted by the ClassRegisterer. packed[0] = REGISTERER_CONTRACT_CLASS_REGISTERED_MAGIC_VALUE; packed[1] = self.contract_class_id.to_field(); packed[2] = self.version; diff --git a/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/private_function_broadcasted.nr b/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/private_function_broadcasted.nr index 8ffcef81f51..460a0578c32 100644 --- a/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/private_function_broadcasted.nr +++ b/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/private_function_broadcasted.nr @@ -9,19 +9,16 @@ use dep::aztec::protocol_types::{ contract_class_id::ContractClassId, traits::Serialize, }; +use std::meta::derive; +#[derive(Serialize)] pub struct InnerPrivateFunction { selector: FunctionSelector, metadata_hash: Field, vk_hash: Field, } -impl Serialize<3> for InnerPrivateFunction { - fn serialize(self: Self) -> [Field; 3] { - [self.selector.to_field(), self.metadata_hash, self.vk_hash] - } -} - +#[derive(Serialize)] pub struct PrivateFunction { selector: FunctionSelector, metadata_hash: Field, @@ -29,21 +26,6 @@ pub struct PrivateFunction { bytecode: [Field; MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS], } -impl Serialize for PrivateFunction { - fn serialize( - self: Self, - ) -> [Field; MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS + 3] { - let mut packed = [0; MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS + 3]; - packed[0] = self.selector.to_field(); - packed[1] = self.metadata_hash; - packed[2] = self.vk_hash; - for i in 0..MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS { - packed[i + 3] = self.bytecode[i]; - } - packed - } -} - // #[event] pub struct ClassPrivateFunctionBroadcasted { contract_class_id: ContractClassId, @@ -56,14 +38,15 @@ pub struct ClassPrivateFunctionBroadcasted { function: PrivateFunction, } -impl Serialize for ClassPrivateFunctionBroadcasted { - fn serialize( +impl ClassPrivateFunctionBroadcasted { + fn serialize_non_standard( self: Self, ) -> [Field; MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS + REGISTERER_PRIVATE_FUNCTION_BROADCASTED_ADDITIONAL_FIELDS] { let mut packed = [ 0; MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS + REGISTERER_PRIVATE_FUNCTION_BROADCASTED_ADDITIONAL_FIELDS ]; + // Since we are not yet emitting selectors we'll use this magic value to identify events emitted by the ClassRegisterer. packed[0] = REGISTERER_PRIVATE_FUNCTION_BROADCASTED_MAGIC_VALUE; packed[1] = self.contract_class_id.to_field(); packed[2] = self.artifact_metadata_hash; diff --git a/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/unconstrained_function_broadcasted.nr b/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/unconstrained_function_broadcasted.nr index f81da27fb8c..49995532e1f 100644 --- a/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/unconstrained_function_broadcasted.nr +++ b/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/events/unconstrained_function_broadcasted.nr @@ -9,38 +9,21 @@ use dep::aztec::protocol_types::{ contract_class_id::ContractClassId, traits::Serialize, }; +use std::meta::derive; +#[derive(Serialize)] pub struct InnerUnconstrainedFunction { selector: FunctionSelector, metadata_hash: Field, } -impl Serialize<2> for InnerUnconstrainedFunction { - fn serialize(self: Self) -> [Field; 2] { - [self.selector.to_field(), self.metadata_hash] - } -} - +#[derive(Serialize)] pub struct UnconstrainedFunction { selector: FunctionSelector, metadata_hash: Field, bytecode: [Field; MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS], } -impl Serialize for UnconstrainedFunction { - fn serialize( - self: Self, - ) -> [Field; MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS + 2] { - let mut packed = [0; MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS + 2]; - packed[0] = self.selector.to_field(); - packed[1] = self.metadata_hash; - for i in 0..MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS { - packed[i + 2] = self.bytecode[i]; - } - packed - } -} - // #[event] pub struct ClassUnconstrainedFunctionBroadcasted { contract_class_id: ContractClassId, @@ -51,14 +34,15 @@ pub struct ClassUnconstrainedFunctionBroadcasted { function: UnconstrainedFunction, } -impl Serialize for ClassUnconstrainedFunctionBroadcasted { - fn serialize( +impl ClassUnconstrainedFunctionBroadcasted { + fn serialize_non_standard( self: Self, ) -> [Field; MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS + REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_ADDITIONAL_FIELDS] { let mut packed = [ 0; MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS + REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_ADDITIONAL_FIELDS ]; + // Since we are not yet emitting selectors we'll use this magic value to identify events emitted by the ClassRegisterer. packed[0] = REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_MAGIC_VALUE; packed[1] = self.contract_class_id.to_field(); packed[2] = self.artifact_metadata_hash; diff --git a/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/main.nr b/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/main.nr index a5d99a95869..11858c7eec5 100644 --- a/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/main.nr @@ -106,7 +106,7 @@ contract ContractClassRegisterer { private_functions_root, packed_public_bytecode, }; - emit_contract_class_log(&mut context, event.serialize()); + emit_contract_class_log(&mut context, event.serialize_non_standard()); } } @@ -150,7 +150,7 @@ contract ContractClassRegisterer { function_data.metadata_hash, ], ); - emit_contract_class_log(&mut context, event.serialize()); + emit_contract_class_log(&mut context, event.serialize_non_standard()); } #[private] @@ -186,7 +186,7 @@ contract ContractClassRegisterer { function_data.metadata_hash, ], ); - emit_contract_class_log(&mut context, event.serialize()); + emit_contract_class_log(&mut context, event.serialize_non_standard()); } #[contract_library_method] diff --git a/noir-projects/noir-contracts/contracts/contract_instance_deployer_contract/src/main.nr b/noir-projects/noir-contracts/contracts/contract_instance_deployer_contract/src/main.nr index 9a242a23258..672a758ef96 100644 --- a/noir-projects/noir-contracts/contracts/contract_instance_deployer_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/contract_instance_deployer_contract/src/main.nr @@ -56,8 +56,8 @@ contract ContractInstanceDeployer { // kind: 'point' // } - impl Serialize<15> for ContractInstanceDeployed { - fn serialize(self) -> [Field; 15] { + impl ContractInstanceDeployed { + fn serialize_non_standard(self) -> [Field; 15] { [ self.DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE, self.address.to_field(), @@ -117,10 +117,7 @@ contract ContractInstanceDeployer { version: 1, }; - // This is a hack to get around there being two implementations of `Serialize`. - // Can be replaced with the below once fixed - // let payload = event.serialize(); - let payload = Serialize::<15>::serialize(event); + let payload = event.serialize_non_standard(); dep::aztec::oracle::debug_log::debug_log_format("ContractInstanceDeployed: {}", payload); let padded_log = array_concat(payload, [0; 3]);