Skip to content

Commit

Permalink
testing deserialization as well
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 5, 2024
1 parent 6729aa7 commit 5857119
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::{
Serialize,
},
};
use dep::std::cmp::Eq;

// docs:start:call-context
struct CallContext {
Expand Down Expand Up @@ -95,9 +94,11 @@ impl Deserialize<CALL_CONTEXT_LENGTH> for CallContext {
}

#[test]
fn serialization_smoke() {
fn serialization_of_empty() {
let context: CallContext = dep::std::unsafe::zeroed();
let _serialized = context.serialize();
let serialized = context.serialize();
let deserialized = CallContext::deserialize(serialized);
assert(context.eq(deserialized));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ struct FunctionData {
is_constructor : bool,
}

impl Eq for FunctionData {
fn eq(self, other: Self) -> bool {
self.selector.eq(other.selector) &
self.is_internal == other.is_internal &
self.is_private == other.is_private &
self.is_constructor == other.is_constructor
}
}

impl Serialize<FUNCTION_DATA_LENGTH> for FunctionData {
// A field is ~256 bits
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3057): Since, function data can fit into a Field,
Expand Down Expand Up @@ -53,9 +62,11 @@ impl Hash for FunctionData {
}

#[test]
fn serialization_smoke() {
fn serialization_of_empty() {
let data: FunctionData = dep::std::unsafe::zeroed();
let _serialized = data.serialize();
let serialized = data.serialize();
let deserialized = FunctionData::deserialize(serialized);
assert(data.eq(deserialized));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Empty for GlobalVariables {
}

#[test]
fn serialization_deserialization_smoke() {
fn serialization_of_empty() {
let vars: GlobalVariables = dep::std::unsafe::zeroed();
let _serialized = vars.serialize();
let _deserialized = GlobalVariables::deserialize(_serialized);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ struct PrivateCallStackItem {
is_execution_request: bool,
}

impl Eq for PrivateCallStackItem {
fn eq(self, other: Self) -> bool {
self.contract_address.eq(other.contract_address) &
self.function_data.eq(other.function_data) &
self.public_inputs.eq(other.public_inputs) &
self.is_execution_request == other.is_execution_request
}
}

impl Serialize<PRIVATE_CALL_STACK_ITEM_LENGTH> for PrivateCallStackItem {
fn serialize(self) -> [Field; PRIVATE_CALL_STACK_ITEM_LENGTH] {
let mut fields: BoundedVec<Field, PRIVATE_CALL_STACK_ITEM_LENGTH> = BoundedVec::new(0);
Expand Down Expand Up @@ -69,9 +78,11 @@ impl Hash for PrivateCallStackItem {
}

#[test]
fn serialization_smoke() {
fn serialization_of_empty() {
let item: PrivateCallStackItem = dep::std::unsafe::zeroed();
let _serialized = item.serialize();
let serialized = item.serialize();
let deserialized = PrivateCallStackItem::deserialize(serialized);
assert(item.eq(deserialized));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ struct PrivateCircuitPublicInputs {
version: Field,
}

impl Eq for PrivateCircuitPublicInputs {
fn eq(self, other: Self) -> bool {
self.call_context.eq(other.call_context) &
self.args_hash.eq(other.args_hash) &
(self.return_values == other.return_values) &
(self.read_requests == other.read_requests) &
(self.nullifier_key_validation_requests == other.nullifier_key_validation_requests) &
(self.new_commitments == other.new_commitments) &
(self.new_nullifiers == other.new_nullifiers) &
(self.private_call_stack_hashes == other.private_call_stack_hashes) &
(self.public_call_stack_hashes == other.public_call_stack_hashes) &
(self.new_l2_to_l1_msgs == other.new_l2_to_l1_msgs) &
(self.end_side_effect_counter == other.end_side_effect_counter) &
(self.encrypted_logs_hash == other.encrypted_logs_hash) &
(self.unencrypted_logs_hash == other.unencrypted_logs_hash) &
(self.encrypted_log_preimages_length == other.encrypted_log_preimages_length) &
(self.unencrypted_log_preimages_length == other.unencrypted_log_preimages_length) &
self.historical_header.eq(other.historical_header) &
self.contract_deployment_data.eq(other.contract_deployment_data) &
self.chain_id.eq(other.chain_id) &
self.version.eq(other.version)
}
}

impl Serialize<PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH> for PrivateCircuitPublicInputs {
fn serialize(self) -> [Field; PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH] {
let mut fields: BoundedVec<Field, PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH> = BoundedVec::new(0);
Expand Down Expand Up @@ -143,9 +167,11 @@ impl Hash for PrivateCircuitPublicInputs {
}

#[test]
fn serialization_smoke() {
fn serialization_of_empty() {
let pcpi: PrivateCircuitPublicInputs = dep::std::unsafe::zeroed();
let _serialized = pcpi.serialize();
let serialized = pcpi.serialize();
let deserialized = PrivateCircuitPublicInputs::deserialize(serialized);
assert(pcpi.eq(deserialized));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ struct PublicCircuitPublicInputs{
prover_address: AztecAddress,
}

impl Eq for PublicCircuitPublicInputs {
fn eq(self, other: Self) -> bool {
self.serialize() == other.serialize()
}
}

impl Serialize<PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH> for PublicCircuitPublicInputs {
fn serialize(self) -> [Field; PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH] {
let mut fields: BoundedVec<Field, PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH> = BoundedVec::new(0);
Expand Down Expand Up @@ -119,9 +125,11 @@ impl Hash for PublicCircuitPublicInputs {
}

#[test]
fn serialization_smoke() {
fn serialization_of_empty() {
let pcpi: PublicCircuitPublicInputs = dep::std::unsafe::zeroed();
let _serialized = pcpi.serialize();
let serialized = pcpi.serialize();
let deserialized = PublicCircuitPublicInputs::deserialize(serialized);
assert(pcpi.eq(deserialized));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ struct ContractDeploymentData {
}
// docs:end:contract-deployment-data

impl Eq for ContractDeploymentData {
fn eq(self, other: Self) -> bool {
self.public_key.eq(other.public_key) &
self.initialization_hash.eq(other.initialization_hash) &
self.contract_class_id.eq(other.contract_class_id) &
self.contract_address_salt.eq(other.contract_address_salt) &
self.portal_contract_address.eq(other.portal_contract_address)
}
}

impl Hash for ContractDeploymentData {
fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__CONTRACT_DEPLOYMENT_DATA)
Expand Down Expand Up @@ -67,3 +77,11 @@ impl ContractDeploymentData {
self.portal_contract_address.assert_is_zero();
}
}

#[test]
fn serialization_of_empty() {
let data: ContractDeploymentData = dep::std::unsafe::zeroed();
let serialized = data.serialize();
let deserialized = ContractDeploymentData::deserialize(serialized);
assert(data.eq(deserialized));
}
16 changes: 13 additions & 3 deletions yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ struct Header {
}
// docs:end:header

impl Eq for Header {
fn eq(self, other: Self) -> bool {
self.last_archive.eq(other.last_archive) &
(self.body_hash == other.body_hash) &
self.state.eq(other.state) &
self.global_variables.eq(other.global_variables)
}
}

impl Serialize<HEADER_LENGTH> for Header {
fn serialize(self) -> [Field; HEADER_LENGTH] {
let mut fields: BoundedVec<Field, HEADER_LENGTH> = BoundedVec::new(0);
Expand Down Expand Up @@ -92,10 +101,11 @@ impl Hash for Header {
}

#[test]
fn serialization_deserialization_smoke() {
fn serialization_of_empty() {
let header: Header = dep::std::unsafe::zeroed();
let _serialized = header.serialize();
let _deserialized = Header::deserialize(_serialized);
let serialized = header.serialize();
let deserialized = Header::deserialize(serialized);
assert(header.eq(deserialized));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Empty for PartialStateReference {
}

#[test]
fn serialization_deserialization_smoke() {
fn serialization_of_empty() {
let partial: PartialStateReference = dep::std::unsafe::zeroed();
let _serialized = partial.serialize();
let _deserialized = PartialStateReference::deserialize(_serialized);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Empty for StateReference {
}

#[test]
fn serialization_deserialization_smoke() {
fn serialization_of_empty() {
let state: StateReference = dep::std::unsafe::zeroed();
let _serialized = state.serialize();
let _deserialized = StateReference::deserialize(_serialized);
Expand Down

0 comments on commit 5857119

Please sign in to comment.