diff --git a/authwit/src/entrypoint/app.nr b/authwit/src/entrypoint/app.nr index a909fe2..16fcd8b 100644 --- a/authwit/src/entrypoint/app.nr +++ b/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/authwit/src/entrypoint/fee.nr b/authwit/src/entrypoint/fee.nr index 3825b1d..8073ab9 100644 --- a/authwit/src/entrypoint/fee.nr +++ b/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/authwit/src/entrypoint/function_call.nr b/authwit/src/entrypoint/function_call.nr index cd5a573..2f54c85 100644 --- a/authwit/src/entrypoint/function_call.nr +++ b/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/aztec/src/note/note_header.nr b/aztec/src/note/note_header.nr index 99dc9d5..b7cf2ef 100644 --- a/aztec/src/note/note_header.nr +++ b/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/aztec/src/test/helpers/utils.nr b/aztec/src/test/helpers/utils.nr index eacf34d..3888f8f 100644 --- a/aztec/src/test/helpers/utils.nr +++ b/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 } - } -}