Skip to content

Commit

Permalink
PublicCircuitPublicInputs::deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 2, 2024
1 parent 4e4a2e2 commit 55e2151
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 37 deletions.
16 changes: 1 addition & 15 deletions yarn-project/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -380,21 +380,7 @@ impl PrivateContext {
let item = PublicCallStackItem {
contract_address: AztecAddress::from_field(reader.read()),
function_data: reader.read_struct(FunctionData::deserialize),
public_inputs: PublicCircuitPublicInputs {
call_context: reader.read_struct(CallContext::deserialize),
args_hash: reader.read(),
return_values: [0; RETURN_VALUES_LENGTH],
contract_storage_update_requests: [StorageUpdateRequest::empty(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL],
contract_storage_reads: [StorageRead::empty(); MAX_PUBLIC_DATA_READS_PER_CALL],
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],
unencrypted_log_preimages_length: 0,
historical_header: Header::empty(),
prover_address: AztecAddress::zero(),
},
public_inputs: reader.read_struct(PublicCircuitPublicInputs::deserialize),
is_execution_request: true,
};
reader.finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ impl Serialize<PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH> for PrivateCircuitPublicInp

impl Deserialize<PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH> for PrivateCircuitPublicInputs {
fn deserialize(serialized: [Field; PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH]) -> Self {
// TODO: This should accept a reader ^ to avoid copying data.
// TODO(#4390): This should accept a reader ^ to avoid copying data.
let mut reader = Reader::new(serialized);
Self {
let inputs = Self {
call_context: reader.read_struct(CallContext::deserialize),
args_hash: reader.read(),
return_values: reader.read_array([0; RETURN_VALUES_LENGTH]),
Expand All @@ -129,7 +129,10 @@ impl Deserialize<PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH> for PrivateCircuitPublicI
contract_deployment_data: reader.read_struct(ContractDeploymentData::deserialize),
chain_id: reader.read(),
version: reader.read(),
}
};

reader.finish();
inputs
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
use crate::constants::{
MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
MAX_NEW_NULLIFIERS_PER_CALL,
MAX_NEW_COMMITMENTS_PER_CALL,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
NUM_FIELDS_PER_SHA256,
RETURN_VALUES_LENGTH,
GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS,
PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH,
};
use crate::{
abis::{
call_context::CallContext,
side_effect::{SideEffect, SideEffectLinkedToNoteHash},
},
address::AztecAddress,
constants::{
MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
MAX_NEW_NULLIFIERS_PER_CALL,
MAX_NEW_COMMITMENTS_PER_CALL,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
NUM_FIELDS_PER_SHA256,
RETURN_VALUES_LENGTH,
GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS,
PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH,
},
contrakt::{
storage_read::StorageRead,
storage_update_request::StorageUpdateRequest,
},
hash::pedersen_hash,
header::Header,
traits::{
Hash,
Serialize,
Deserialize,
},
utils::reader::Reader,
};
use crate::traits::{Hash, Serialize, Deserialize};

struct PublicCircuitPublicInputs{
call_context: CallContext,
Expand Down Expand Up @@ -82,6 +87,31 @@ impl Serialize<PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH> for PublicCircuitPublicInput
}
}

impl Deserialize<PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH> for PublicCircuitPublicInputs {
fn deserialize(serialized: [Field; PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH]) -> Self {
// TODO(#4390): This should accept a reader ^ to avoid copying data.
let mut reader = Reader::new(serialized);
let inputs = PublicCircuitPublicInputs {
call_context: reader.read_struct(CallContext::deserialize),
args_hash: reader.read(),
return_values: reader.read_array([0; RETURN_VALUES_LENGTH]),
contract_storage_update_requests: reader.read_struct_array(StorageUpdateRequest::deserialize, [StorageUpdateRequest::empty(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL]),
contract_storage_reads: reader.read_struct_array(StorageRead::deserialize, [StorageRead::empty(); MAX_PUBLIC_DATA_READS_PER_CALL]),
public_call_stack_hashes: reader.read_array([0; MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL]),
new_commitments: reader.read_struct_array(SideEffect::deserialize, [SideEffect::empty(); MAX_NEW_COMMITMENTS_PER_CALL]),
new_nullifiers: reader.read_struct_array(SideEffectLinkedToNoteHash::deserialize, [SideEffectLinkedToNoteHash::empty(); MAX_NEW_NULLIFIERS_PER_CALL]),
new_l2_to_l1_msgs: reader.read_array([0; MAX_NEW_L2_TO_L1_MSGS_PER_CALL]),
unencrypted_logs_hash: reader.read_array([0; NUM_FIELDS_PER_SHA256]),
unencrypted_log_preimages_length: reader.read(),
historical_header: reader.read_struct(Header::deserialize),
prover_address: reader.read_struct(AztecAddress::deserialize),
};

reader.finish();
inputs
}
}

impl Hash for PublicCircuitPublicInputs {
fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ use crate::{
GENERATOR_INDEX__PUBLIC_DATA_READ,
},
hash::pedersen_hash,
traits::{
Deserialize,
Hash,
Empty,
Serialize,
},
};
use crate::traits::Empty;

struct StorageRead {
storage_slot: Field,
current_value: Field,
}

impl Eq for StorageRead {
fn eq(self, other: Self) -> bool {
(self.storage_slot == other.storage_slot) & (self.current_value == other.current_value)
}
}

impl Empty for StorageRead {
fn empty() -> Self {
Self {
Expand All @@ -21,16 +32,28 @@ impl Empty for StorageRead {
}
}

impl StorageRead {
impl Hash for StorageRead {
fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_READ)
}
}

pub fn serialize(self) -> [Field; CONTRACT_STORAGE_READ_LENGTH] {
impl Serialize<CONTRACT_STORAGE_READ_LENGTH> for StorageRead {
fn serialize(self) -> [Field; CONTRACT_STORAGE_READ_LENGTH] {
[self.storage_slot, self.current_value]
}
}

pub fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_READ)
impl Deserialize<CONTRACT_STORAGE_READ_LENGTH> for StorageRead {
fn deserialize(serialized: [Field; CONTRACT_STORAGE_READ_LENGTH]) -> Self {
Self {
storage_slot: serialized[0],
current_value: serialized[1],
}
}
}

impl StorageRead {
pub fn is_empty(self) -> bool {
self.storage_slot == 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ use crate::{
GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST,
},
hash::pedersen_hash,
traits::{
Deserialize,
Hash,
Empty,
Serialize,
},
};
use dep::std::cmp::Eq;
use crate::traits::{Hash, Empty, Serialize};

struct StorageUpdateRequest{
storage_slot : Field,
Expand Down Expand Up @@ -45,6 +50,16 @@ impl Serialize<CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH> for StorageUpdateRequest
}
}

impl Deserialize<CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH> for StorageUpdateRequest {
fn deserialize(serialized: [Field; CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH]) -> Self {
StorageUpdateRequest {
storage_slot: serialized[0],
old_value: serialized[1],
new_value: serialized[2],
}
}
}

impl StorageUpdateRequest {
pub fn is_empty(self) -> bool {
self.storage_slot == 0
Expand Down

0 comments on commit 55e2151

Please sign in to comment.