Skip to content

Commit

Permalink
feat: add selector to call_context (#2626)
Browse files Browse the repository at this point in the history
Fixes #2627.
  • Loading branch information
LHerskind authored Oct 3, 2023
1 parent 10b30e0 commit 8e317be
Show file tree
Hide file tree
Showing 44 changed files with 357 additions and 185 deletions.
3 changes: 3 additions & 0 deletions circuits/cpp/src/aztec3/circuits/abis/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ TEST(abi_tests, native_read_write_call_context)
.msg_sender = 1,
.storage_contract_address = 2,
.portal_contract_address = 3,
.function_selector = { .value = 1 },
.is_delegate_call = false,
.is_static_call = false,
.is_contract_deployment = false,
Expand Down Expand Up @@ -92,6 +93,7 @@ TEST(abi_tests, native_call_context)
.msg_sender = 10,
.storage_contract_address = 11,
.portal_contract_address = 12,
.function_selector = { .value = 1 },
.is_delegate_call = false,
.is_static_call = false,
};
Expand All @@ -105,6 +107,7 @@ TEST(abi_tests, native_to_circuit_call_context)
.msg_sender = 10,
.storage_contract_address = 11,
.portal_contract_address = 12,
.function_selector = { .value = 1 },
.is_delegate_call = false,
.is_static_call = false,
};
Expand Down
32 changes: 26 additions & 6 deletions circuits/cpp/src/aztec3/circuits/abis/call_context.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "aztec3/circuits/abis/function_selector.hpp"
#include "aztec3/circuits/hash.hpp"
#include "aztec3/constants.hpp"
#include "aztec3/utils/msgpack_derived_equals.hpp"
#include "aztec3/utils/msgpack_derived_output.hpp"
Expand All @@ -23,6 +25,8 @@ template <typename NCT> struct CallContext {
address storage_contract_address = 0;
fr portal_contract_address = 0;

FunctionSelector<NCT> function_selector{};

boolean is_delegate_call = false;
boolean is_static_call = false;
boolean is_contract_deployment = false;
Expand All @@ -31,6 +35,7 @@ template <typename NCT> struct CallContext {
MSGPACK_FIELDS(msg_sender,
storage_contract_address,
portal_contract_address,
function_selector,
is_delegate_call,
is_static_call,
is_contract_deployment);
Expand All @@ -48,8 +53,13 @@ template <typename NCT> struct CallContext {
auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(builder, e); };

CallContext<CircuitTypes<Builder>> call_context = {
to_ct(msg_sender), to_ct(storage_contract_address), to_ct(portal_contract_address),
to_ct(is_delegate_call), to_ct(is_static_call), to_ct(is_contract_deployment),
to_ct(msg_sender),
to_ct(storage_contract_address),
to_ct(portal_contract_address),
function_selector.to_circuit_type(builder),
to_ct(is_delegate_call),
to_ct(is_static_call),
to_ct(is_contract_deployment),

};

Expand All @@ -59,11 +69,17 @@ template <typename NCT> struct CallContext {
template <typename Builder> CallContext<NativeTypes> to_native_type() const
{
static_assert(std::is_same<CircuitTypes<Builder>, NCT>::value);
auto to_native_type = []<typename T>(T& e) { return e.template to_native_type<Builder>(); };
auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Builder>(e); };

CallContext<NativeTypes> call_context = {
to_nt(msg_sender), to_nt(storage_contract_address), to_nt(portal_contract_address),
to_nt(is_delegate_call), to_nt(is_static_call), to_nt(is_contract_deployment),
to_nt(msg_sender),
to_nt(storage_contract_address),
to_nt(portal_contract_address),
to_native_type(function_selector),
to_nt(is_delegate_call),
to_nt(is_static_call),
to_nt(is_contract_deployment),
};

return call_context;
Expand All @@ -72,8 +88,10 @@ template <typename NCT> struct CallContext {
fr hash() const
{
std::vector<fr> const inputs = {
msg_sender.to_field(), storage_contract_address.to_field(), portal_contract_address, fr(is_delegate_call),
fr(is_static_call), fr(is_contract_deployment),
msg_sender.to_field(), storage_contract_address.to_field(),
portal_contract_address, function_selector.to_field(),
fr(is_delegate_call), fr(is_static_call),
fr(is_contract_deployment),
};

return NCT::hash(inputs, GeneratorIndex::CALL_CONTEXT);
Expand All @@ -86,6 +104,7 @@ template <typename NCT> struct CallContext {
msg_sender.to_field().assert_is_zero();
storage_contract_address.to_field().assert_is_zero();
portal_contract_address.assert_is_zero();
function_selector.to_field().assert_is_zero();
fr(is_delegate_call).assert_is_zero();
fr(is_static_call).assert_is_zero();
fr(is_contract_deployment).assert_is_zero();
Expand All @@ -98,6 +117,7 @@ template <typename NCT> struct CallContext {
msg_sender.to_field().set_public();
storage_contract_address.to_field().set_public();
portal_contract_address.set_public();
function_selector.set_public();
fr(is_delegate_call).set_public();
fr(is_static_call).set_public();
fr(is_contract_deployment).set_public();
Expand Down
2 changes: 2 additions & 0 deletions circuits/cpp/src/aztec3/circuits/abis/function_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ template <typename NCT> struct FunctionSelector {
return selector;
};

fr to_field() const { return fr(value); }

void set_public()
{
static_assert(!(std::is_same<NativeTypes, NCT>::value));
Expand Down
1 change: 1 addition & 0 deletions circuits/cpp/src/aztec3/circuits/apps/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class state_var_tests : public ::testing::Test {
CallContext<NT> const call_context{ .msg_sender = msg_sender,
.storage_contract_address = contract_address,
.portal_contract_address = 0,
.function_selector = function_data.selector,
.is_delegate_call = false,
.is_static_call = false,
.is_contract_deployment = false };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ template <typename Builder> class FunctionExecutionContext {
.msg_sender = oracle.get_this_contract_address(), // the sender is `this` contract!
.storage_contract_address = f_contract_address,
.portal_contract_address = 0, // TODO
.function_selector = f_function_data_ct.selector,
.is_delegate_call = false,
.is_static_call = false,
.is_contract_deployment = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class escrow_tests : public ::testing::Test {
.msg_sender = msg_sender,
.storage_contract_address = contract_address,
.portal_contract_address = 0,
.function_selector = function_data.selector,
.is_delegate_call = false,
.is_static_call = false,
.is_contract_deployment = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ TEST(private_to_private_function_call_tests, circuit_private_to_private_function
.msg_sender = msg_sender,
.storage_contract_address = contract_address,
.portal_contract_address = 0,
.function_selector = function_data.selector,
.is_delegate_call = false,
.is_static_call = false,
.is_contract_deployment = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ std::pair<PrivateCallData<NT>, ContractDeploymentData<NT>> create_private_call_d
.msg_sender = msg_sender,
.storage_contract_address = contract_address,
.portal_contract_address = portal_contract_address,
.function_selector = function_data.selector,
.is_delegate_call = false,
.is_static_call = false,
.is_contract_deployment = is_constructor,
Expand Down
2 changes: 2 additions & 0 deletions circuits/cpp/src/aztec3/circuits/kernel/public/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ PublicCallStackItem generate_call_stack_item(NT::fr contract_address,
.msg_sender = msg_sender,
.storage_contract_address = storage_contract_address,
.portal_contract_address = portal_contract_address,
.function_selector = function_data.selector,
.is_delegate_call = is_delegate_call,
.is_static_call = false,
.is_contract_deployment = false,
Expand Down Expand Up @@ -273,6 +274,7 @@ PublicKernelInputs<NT> get_kernel_inputs_with_previous_kernel(NT::boolean privat
.msg_sender = msg_sender,
.storage_contract_address = contract_address,
.portal_contract_address = portal_contract_address,
.function_selector = function_data.selector,
.is_delegate_call = false,
.is_static_call = false,
.is_contract_deployment = false,
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ constexpr size_t MAX_NOTES_PER_PAGE = 10;
// + 2 for EXTRA_DATA: [number_of_return_notes, contract_address]
constexpr size_t VIEW_NOTE_ORACLE_RETURN_LENGTH = MAX_NOTES_PER_PAGE * (MAX_NOTE_FIELDS_LENGTH + 1) + 2;

constexpr size_t CALL_CONTEXT_LENGTH = 6;
constexpr size_t CALL_CONTEXT_LENGTH = 7;
// Must be updated if any data is added into the block hash calculation.
constexpr size_t HISTORIC_BLOCK_DATA_LENGTH = 7;
constexpr size_t FUNCTION_DATA_LENGTH = 4;
Expand Down
8 changes: 4 additions & 4 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ library Constants {
uint256 internal constant GET_NOTE_ORACLE_RETURN_LENGTH = 23;
uint256 internal constant MAX_NOTES_PER_PAGE = 10;
uint256 internal constant VIEW_NOTE_ORACLE_RETURN_LENGTH = 212;
uint256 internal constant CALL_CONTEXT_LENGTH = 6;
uint256 internal constant CALL_CONTEXT_LENGTH = 7;
uint256 internal constant HISTORIC_BLOCK_DATA_LENGTH = 7;
uint256 internal constant FUNCTION_DATA_LENGTH = 4;
uint256 internal constant CONTRACT_DEPLOYMENT_DATA_LENGTH = 6;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 122;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 123;
uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 141;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 142;
uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674;
uint256 internal constant EMPTY_NULLIFIED_COMMITMENT = 1000000;
uint256 internal constant CALL_PRIVATE_FUNCTION_RETURN_SIZE = 128;
uint256 internal constant CALL_PRIVATE_FUNCTION_RETURN_SIZE = 129;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 87;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 112;
uint256 internal constant COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 4096;
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/acir-simulator/src/acvm/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ContractDeploymentData,
ContractStorageRead,
ContractStorageUpdateRequest,
FunctionSelector,
HistoricBlockData,
MAX_NEW_COMMITMENTS_PER_CALL,
MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
Expand Down Expand Up @@ -137,6 +138,7 @@ export function extractPrivateCircuitPublicInputs(
frToAztecAddress(witnessReader.readField()),
frToAztecAddress(witnessReader.readField()),
witnessReader.readField(),
FunctionSelector.fromField(witnessReader.readField()),
frToBoolean(witnessReader.readField()),
frToBoolean(witnessReader.readField()),
frToBoolean(witnessReader.readField()),
Expand Down Expand Up @@ -214,6 +216,7 @@ export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, ac
frToAztecAddress(witnessReader.readField()),
frToAztecAddress(witnessReader.readField()),
witnessReader.readField(),
FunctionSelector.fromField(witnessReader.readField()),
frToBoolean(witnessReader.readField()),
frToBoolean(witnessReader.readField()),
frToBoolean(witnessReader.readField()),
Expand Down
1 change: 1 addition & 0 deletions yarn-project/acir-simulator/src/acvm/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function toACVMCallContext(callContext: CallContext): ACVMField[] {
toACVMField(callContext.msgSender),
toACVMField(callContext.storageContractAddress),
toACVMField(callContext.portalContractAddress),
toACVMField(callContext.functionSelector.toField()),
toACVMField(callContext.isDelegateCall),
toACVMField(callContext.isStaticCall),
toACVMField(callContext.isContractDeployment),
Expand Down
16 changes: 12 additions & 4 deletions yarn-project/acir-simulator/src/client/client_execution_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@aztec/circuits.js';
import { computeUniqueCommitment, siloCommitment } from '@aztec/circuits.js/abis';
import { Grumpkin } from '@aztec/circuits.js/barretenberg';
import { FunctionAbi } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
Expand Down Expand Up @@ -85,6 +86,7 @@ export class ClientExecutionContext extends ViewDataOracle {
this.callContext.msgSender,
this.callContext.storageContractAddress,
this.callContext.portalContractAddress,
this.callContext.functionSelector.toField(),
this.callContext.isDelegateCall,
this.callContext.isStaticCall,
this.callContext.isContractDeployment,
Expand Down Expand Up @@ -323,7 +325,7 @@ export class ClientExecutionContext extends ViewDataOracle {
this.txContext.version,
);

const derivedCallContext = await this.deriveCallContext(targetContractAddress, false, false);
const derivedCallContext = await this.deriveCallContext(targetContractAddress, targetAbi, false, false);

const context = new ClientExecutionContext(
targetContractAddress,
Expand Down Expand Up @@ -366,7 +368,7 @@ export class ClientExecutionContext extends ViewDataOracle {
argsHash: Fr,
): Promise<PublicCallRequest> {
const targetAbi = await this.db.getFunctionABI(targetContractAddress, functionSelector);
const derivedCallContext = await this.deriveCallContext(targetContractAddress, false, false);
const derivedCallContext = await this.deriveCallContext(targetContractAddress, targetAbi, false, false);
const args = this.packedArgsCache.unpack(argsHash);
const sideEffectCounter = this.sideEffectCounter.count();
const enqueuedRequest = PublicCallRequest.from({
Expand All @@ -392,18 +394,24 @@ export class ClientExecutionContext extends ViewDataOracle {

/**
* Derives the call context for a nested execution.
* @param parentContext - The parent call context.
* @param targetContractAddress - The address of the contract being called.
* @param targetAbi - The ABI of the function being called.
* @param isDelegateCall - Whether the call is a delegate call.
* @param isStaticCall - Whether the call is a static call.
* @returns The derived call context.
*/
private async deriveCallContext(targetContractAddress: AztecAddress, isDelegateCall = false, isStaticCall = false) {
private async deriveCallContext(
targetContractAddress: AztecAddress,
targetAbi: FunctionAbi,
isDelegateCall = false,
isStaticCall = false,
) {
const portalContractAddress = await this.db.getPortalContractAddress(targetContractAddress);
return new CallContext(
this.contractAddress,
targetContractAddress,
portalContractAddress,
FunctionSelector.fromNameAndParameters(targetAbi.name, targetAbi.parameters),
isDelegateCall,
isStaticCall,
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ describe('Private Execution test suite', () => {
msgSender: parentAddress,
storageContractAddress: childAddress,
portalContractAddress: childPortalContractAddress,
functionSelector: childSelector,
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
Expand Down
1 change: 1 addition & 0 deletions yarn-project/acir-simulator/src/client/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class AcirSimulator {
msgSender,
contractAddress,
portalContractAddress,
FunctionSelector.fromNameAndParameters(entryPointABI.name, entryPointABI.parameters),
false,
false,
request.functionData.isConstructor,
Expand Down
7 changes: 7 additions & 0 deletions yarn-project/acir-simulator/src/public/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe('ACIR public execution simulator', () => {
msgSender: AztecAddress.random(),
storageContractAddress: contractAddress,
portalContractAddress: EthAddress.random(),
functionSelector: FunctionSelector.empty(),
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
Expand Down Expand Up @@ -119,6 +120,7 @@ describe('ACIR public execution simulator', () => {
msgSender: sender,
storageContractAddress: contractAddress,
portalContractAddress: EthAddress.random(),
functionSelector: FunctionSelector.empty(),
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
Expand Down Expand Up @@ -223,6 +225,7 @@ describe('ACIR public execution simulator', () => {
msgSender: AztecAddress.random(),
storageContractAddress: parentContractAddress,
portalContractAddress: EthAddress.random(),
functionSelector: FunctionSelector.empty(),
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
Expand Down Expand Up @@ -289,6 +292,7 @@ describe('ACIR public execution simulator', () => {
msgSender: AztecAddress.random(),
storageContractAddress: contractAddress,
portalContractAddress: EthAddress.random(),
functionSelector: FunctionSelector.empty(),
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
Expand Down Expand Up @@ -321,6 +325,7 @@ describe('ACIR public execution simulator', () => {
msgSender: AztecAddress.random(),
storageContractAddress: contractAddress,
portalContractAddress: EthAddress.random(),
functionSelector: FunctionSelector.empty(),
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
Expand Down Expand Up @@ -373,6 +378,7 @@ describe('ACIR public execution simulator', () => {
msgSender: AztecAddress.random(),
storageContractAddress: contractAddress,
portalContractAddress: EthAddress.random(),
functionSelector: FunctionSelector.empty(),
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
Expand Down Expand Up @@ -405,6 +411,7 @@ describe('ACIR public execution simulator', () => {
msgSender: AztecAddress.random(),
storageContractAddress: contractAddress,
portalContractAddress: EthAddress.random(),
functionSelector: FunctionSelector.empty(),
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class PublicExecutionContext extends TypedOracle {
callContext.msgSender,
callContext.storageContractAddress,
callContext.portalContractAddress,
callContext.functionSelector.toField(),
callContext.isDelegateCall,
callContext.isStaticCall,
callContext.isContractDeployment,
Expand Down Expand Up @@ -197,6 +198,7 @@ export class PublicExecutionContext extends TypedOracle {
msgSender: this.execution.contractAddress,
portalContractAddress: portalAddress,
storageContractAddress: targetContractAddress,
functionSelector,
isContractDeployment: false,
isDelegateCall: false,
isStaticCall: false,
Expand Down
Loading

0 comments on commit 8e317be

Please sign in to comment.