Skip to content

Commit

Permalink
PrivateCallStackItem::deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 5, 2024
1 parent 82f18ae commit 570270b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 31 deletions.
1 change: 0 additions & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ library Constants {
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201;
uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674;
uint256 internal constant CALL_PRIVATE_FUNCTION_RETURN_SIZE = 210;
uint256 internal constant COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
uint256 internal constant NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
uint256 internal constant PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP = 1024;
Expand Down
12 changes: 1 addition & 11 deletions yarn-project/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,12 @@ impl PrivateContext {
function_selector: FunctionSelector,
args_hash: Field
) -> [Field; RETURN_VALUES_LENGTH] {
let fields = call_private_function_internal(
let item = call_private_function_internal(
contract_address,
function_selector,
args_hash,
self.side_effect_counter,
);
let mut reader = Reader::new(fields);

let item = PrivateCallStackItem {
contract_address: AztecAddress::from_field(reader.read()),
function_data: reader.read_struct(FunctionData::deserialize),
public_inputs: reader.read_struct(PrivateCircuitPublicInputs::deserialize),
is_execution_request: reader.read() as bool,
};

reader.finish();

assert_eq(item.public_inputs.call_context.start_side_effect_counter, self.side_effect_counter);
self.side_effect_counter = item.public_inputs.end_side_effect_counter + 1;
Expand Down
17 changes: 11 additions & 6 deletions yarn-project/aztec-nr/aztec/src/oracle/call_private_function.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use dep::protocol_types::{
abis::function_selector::FunctionSelector,
abis::{
function_selector::FunctionSelector,
private_call_stack_item::PrivateCallStackItem,
},
address::AztecAddress,
constants::CALL_PRIVATE_FUNCTION_RETURN_SIZE,
constants::PRIVATE_CALL_STACK_ITEM_LENGTH,
};

#[oracle(callPrivateFunction)]
Expand All @@ -10,18 +13,20 @@ fn call_private_function_oracle(
_function_selector: FunctionSelector,
_args_hash: Field,
_start_side_effect_counter: u32
) -> [Field; CALL_PRIVATE_FUNCTION_RETURN_SIZE] {}
) -> [Field; PRIVATE_CALL_STACK_ITEM_LENGTH] {}

unconstrained pub fn call_private_function_internal(
contract_address: AztecAddress,
function_selector: FunctionSelector,
args_hash: Field,
start_side_effect_counter: u32
) -> [Field; CALL_PRIVATE_FUNCTION_RETURN_SIZE] {
call_private_function_oracle(
) -> PrivateCallStackItem {
let fields = call_private_function_oracle(
contract_address,
function_selector,
args_hash,
start_side_effect_counter
)
);

PrivateCallStackItem::deserialize(fields)
}
1 change: 0 additions & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export const CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
export const CONTRACT_STORAGE_READ_LENGTH = 2;
export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201;
export const GET_NOTES_ORACLE_RETURN_LENGTH = 674;
export const CALL_PRIVATE_FUNCTION_RETURN_SIZE = 210;
export const COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
export const NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
export const PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP = 1024;
Expand Down
15 changes: 7 additions & 8 deletions yarn-project/circuits.js/src/structs/private_call_stack_item.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { pedersenHash } from '@aztec/foundation/crypto';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader, FieldReader, serializeToBuffer, serializeToFields } from '@aztec/foundation/serialize';
import { FieldsOf } from '@aztec/foundation/types';

import { pedersenHash } from '@aztec/foundation/crypto';
import { GeneratorIndex } from '../constants.gen.js';
import { CallRequest, CallerContext } from './call_request.js';
import { FunctionData } from './function_data.js';
Expand Down Expand Up @@ -65,13 +65,12 @@ export class PrivateCallStackItem {

static fromFields(fields: Fr[] | FieldReader): PrivateCallStackItem {
const reader = FieldReader.asReader(fields);

const contractAddress = AztecAddress.fromFields(reader);
const functionData = FunctionData.fromFields(reader);
const publicInputs = PrivateCircuitPublicInputs.fromFields(reader);
const isExecutionRequest = reader.readBoolean();

return new PrivateCallStackItem(contractAddress, functionData, publicInputs, isExecutionRequest);
return new PrivateCallStackItem(
AztecAddress.fromFields(reader),
FunctionData.fromFields(reader),
PrivateCircuitPublicInputs.fromFields(reader),
reader.readBoolean(),
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ use crate::{
},
hash::pedersen_hash,
traits::{
Deserialize,
Hash,
Serialize,
},
utils::reader::Reader,
};

struct PrivateCallStackItem {
Expand All @@ -22,8 +24,8 @@ struct PrivateCallStackItem {
// getting the correct code from the tree. There is a separate `storage_contract_address`
// within a CallStackItem which varies depending on whether this is a call or delegatecall.
contract_address: AztecAddress,
public_inputs: PrivateCircuitPublicInputs,
function_data: FunctionData,
public_inputs: PrivateCircuitPublicInputs,
// Not really needed for PrivateCallStackItem.
is_execution_request: bool,
}
Expand All @@ -33,8 +35,8 @@ impl Serialize<PRIVATE_CALL_STACK_ITEM_LENGTH> for PrivateCallStackItem {
let mut fields: BoundedVec<Field, PRIVATE_CALL_STACK_ITEM_LENGTH> = BoundedVec::new(0);

fields.push(self.contract_address.to_field());
fields.extend_from_array(self.public_inputs.serialize());
fields.extend_from_array(self.function_data.serialize());
fields.extend_from_array(self.public_inputs.serialize());
fields.push(self.is_execution_request as Field);

assert_eq(fields.len(), PRIVATE_CALL_STACK_ITEM_LENGTH);
Expand All @@ -43,6 +45,23 @@ impl Serialize<PRIVATE_CALL_STACK_ITEM_LENGTH> for PrivateCallStackItem {
}
}

impl Deserialize<PRIVATE_CALL_STACK_ITEM_LENGTH> for PrivateCallStackItem {
fn deserialize(serialized: [Field; PRIVATE_CALL_STACK_ITEM_LENGTH]) -> Self {
// TODO(#4390): This should accept a reader ^ to avoid copying data.
let mut reader = Reader::new(serialized);

let item = Self {
contract_address: reader.read_struct(AztecAddress::deserialize),
function_data: reader.read_struct(FunctionData::deserialize),
public_inputs: reader.read_struct(PrivateCircuitPublicInputs::deserialize),
is_execution_request: reader.read() as bool,
};

reader.finish();
item
}
}

impl Hash for PrivateCallStackItem {
fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__CALL_STACK_ITEM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,14 @@ global FUNCTION_DATA_LENGTH: Field = 4;
global CONTRACT_DEPLOYMENT_DATA_LENGTH: Field = 6;
// Change this ONLY if you have changed the PrivateCircuitPublicInputs structure.
// In other words, if the structure/size of the public inputs of a function call changes then we should change this
// constant as well CALL_PRIVATE_FUNCTION_RETURN_SIZE
// constant as well PRIVATE_CALL_STACK_ITEM_LENGTH
global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: Field = 204;
global PRIVATE_CALL_STACK_ITEM_LENGTH: Field = 210;
global CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH: Field = 3;
global CONTRACT_STORAGE_READ_LENGTH: Field = 2;
// Change this ONLY if you have changed the PublicCircuitPublicInputs structure.
global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: Field = 201;
global GET_NOTES_ORACLE_RETURN_LENGTH: Field = 674;
global CALL_PRIVATE_FUNCTION_RETURN_SIZE: Field = 210;
global COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP: Field = 2048;
global NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP: Field = 2048;
global PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP: Field = 1024;
Expand Down

0 comments on commit 570270b

Please sign in to comment.