Skip to content

Commit

Permalink
feat: function selector opcode in AVM (#7244)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 authored Jul 1, 2024
1 parent 868606e commit dde47e9
Show file tree
Hide file tree
Showing 45 changed files with 1,133 additions and 1,033 deletions.
12 changes: 6 additions & 6 deletions avm-transpiler/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ pub enum AvmOpcode {
ADDRESS,
STORAGEADDRESS,
SENDER,
FEEPERL2GAS,
FEEPERDAGAS,
FUNCTIONSELECTOR,
TRANSACTIONFEE,
CONTRACTCALLDEPTH,
CHAINID,
VERSION,
BLOCKNUMBER,
TIMESTAMP,
COINBASE,
FEEPERL2GAS,
FEEPERDAGAS,
BLOCKL2GASLIMIT,
BLOCKDAGASLIMIT,
CALLDATACOPY,
Expand Down Expand Up @@ -106,16 +106,16 @@ impl AvmOpcode {
AvmOpcode::ADDRESS => "ADDRESS",
AvmOpcode::STORAGEADDRESS => "STORAGEADDRESS",
AvmOpcode::SENDER => "SENDER",
AvmOpcode::FEEPERL2GAS => "FEEPERL2GAS",
AvmOpcode::FEEPERDAGAS => "FEEPERDAGAS",
AvmOpcode::FUNCTIONSELECTOR => "FUNCTIONSELECTOR",
AvmOpcode::TRANSACTIONFEE => "TRANSACTIONFEE",
AvmOpcode::CONTRACTCALLDEPTH => "CONTRACTCALLDEPTH",
// Execution Environment - Globals
AvmOpcode::CHAINID => "CHAINID",
AvmOpcode::VERSION => "VERSION",
AvmOpcode::BLOCKNUMBER => "BLOCKNUMBER",
AvmOpcode::TIMESTAMP => "TIMESTAMP",
AvmOpcode::COINBASE => "COINBASE",
AvmOpcode::FEEPERL2GAS => "FEEPERL2GAS",
AvmOpcode::FEEPERDAGAS => "FEEPERDAGAS",
AvmOpcode::BLOCKL2GASLIMIT => "BLOCKL2GASLIMIT",
AvmOpcode::BLOCKDAGASLIMIT => "BLOCKDAGASLIMIT",
// Execution Environment - Calldata
Expand Down
9 changes: 4 additions & 5 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,9 @@ fn handle_external_call(
ValueOrArray::HeapVector(HeapVector { pointer, size }) => (pointer.0 as u32, size.0 as u32),
_ => panic!("Call instruction's args input should be a HeapVector input"),
};
let temporary_function_selector_offset = match &inputs[4] {
let function_selector_offset = match &inputs[4] {
ValueOrArray::MemoryAddress(offset) => offset.to_usize() as u32,
_ => panic!(
"Call instruction's temporary function selector input should be a basic MemoryAddress",
),
_ => panic!("Call instruction's function selector input should be a basic MemoryAddress",),
};

let ret_offset_maybe = destinations[0];
Expand Down Expand Up @@ -351,7 +349,7 @@ fn handle_external_call(
AvmOperand::U32 { value: ret_offset },
AvmOperand::U32 { value: ret_size },
AvmOperand::U32 { value: success_offset },
AvmOperand::U32 { value: temporary_function_selector_offset },
AvmOperand::U32 { value: function_selector_offset },
],
..Default::default()
});
Expand Down Expand Up @@ -650,6 +648,7 @@ fn handle_getter_instruction(
"avmOpcodeTimestamp" => AvmOpcode::TIMESTAMP,
"avmOpcodeL2GasLeft" => AvmOpcode::L2GASLEFT,
"avmOpcodeDaGasLeft" => AvmOpcode::DAGASLEFT,
"avmOpcodeFunctionSelector" => AvmOpcode::FUNCTIONSELECTOR,
// "callStackDepth" => AvmOpcode::CallStackDepth,
_ => panic!("Transpiler doesn't know how to process ForeignCall function {:?}", function),
};
Expand Down
22 changes: 12 additions & 10 deletions barretenberg/cpp/pil/avm/constants.pil
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@

// NOTE: the constants in this file line up to the indexes of values in the
// `PublicKernelInputs.nr` object
// `PublicCircuitPublicInputs` object
namespace constants(256);
// From Public Context Inputs
// From PublicCircuitPublicInputs's CallContext member
pol SENDER_SELECTOR = 0;
// "address" actually does not exist in PublicCircuitPublicInputs,
// so this is just an alias to "storage address" for now
pol ADDRESS_SELECTOR = 1;
pol STORAGE_ADDRESS_SELECTOR = 2;
pol STORAGE_ADDRESS_SELECTOR = 1;
pol FUNCTION_SELECTOR_SELECTOR = 2;

// NOTE: constant expression evaluation does not seem to be supported yet in pil
// pol START_GLOBAL_VARIABLES = CALL_CONTEXT_LENGTH + HEADER_LENGTH = 6 + 23 = 29

// From PublicCircuitPublicInputs's GlobalVariables member
// Global Variables
pol CHAIN_ID_SELECTOR = 29;
pol VERSION_SELECTOR = 30;
pol BLOCK_NUMBER_SELECTOR = 31;
pol TIMESTAMP_SELECTOR = 32;
pol COINBASE_SELECTOR = 33;

pol END_GLOBAL_VARIABLES = 29 + 8; // We only use the first 5 of 8 global variables for now

// Gas
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/6715): This has since moved into the global variables
// Global Variables - fees
pol FEE_PER_DA_GAS_SELECTOR = 35;
pol FEE_PER_L2_GAS_SELECTOR = 36;

pol START_SIDE_EFFECT_COUNTER = 37;
pol END_GLOBAL_VARIABLES = 29 + 8; // We only use the first 5 of 8 global variables for now

// Top-level PublicCircuitPublicInputs members
pol START_SIDE_EFFECT_COUNTER = 37;
pol TRANSACTION_FEE_SELECTOR = 40;

// Other AVM specific constants
pol INTERNAL_CALL_SPACE_ID = 255;

// Lengths of kernel output vectors
// (vectors in PublicCircuitPublicInputs to be processed by kernel)
// Read requests
pol MAX_NULLIFIER_READ_REQUESTS_PER_CALL = 32;
pol MAX_NOTE_HASH_READ_REQUESTS_PER_CALL = 32;
pol MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL = 32;
pol MAX_PUBLIC_DATA_READS_PER_CALL = 32;

// Emitting Data
pol MAX_NEW_NOTE_HASHES_PER_CALL = 16;
pol MAX_NEW_NULLIIFIERS_PER_CALL = 16;
Expand Down
55 changes: 30 additions & 25 deletions barretenberg/cpp/pil/avm/main.pil
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ namespace main(256);
// Kernel lookup selector opcodes
pol commit sel_q_kernel_lookup;

// CALL CONTEXT
pol commit sel_op_sender;
// CONTEXT - ENVIRONMENT
pol commit sel_op_address;
pol commit sel_op_storage_address;

// FEES
pol commit sel_op_fee_per_l2_gas;
pol commit sel_op_fee_per_da_gas;
pol commit sel_op_sender;
pol commit sel_op_function_selector;
pol commit sel_op_transaction_fee;
// GLOBALS

// CONTEXT - ENVIRONMENT - GLOBALS
pol commit sel_op_chain_id;
pol commit sel_op_version;
pol commit sel_op_block_number;
pol commit sel_op_coinbase;
pol commit sel_op_timestamp;
// CONTEXT - ENVIRONMENT - GLOBALS - FEES
pol commit sel_op_fee_per_l2_gas;
pol commit sel_op_fee_per_da_gas;

// MACHINE STATE - GAS
// CONTEXT - MACHINE STATE - GAS
pol commit sel_op_l2gasleft;
pol commit sel_op_dagasleft;

Expand Down Expand Up @@ -262,17 +262,18 @@ namespace main(256);
// Relations on type constraints
// TODO: Very likely, we can remove these constraints as the selectors should be derived during
// opcode decomposition.
sel_op_sender * (1 - sel_op_sender) = 0;
sel_op_address * (1 - sel_op_address) = 0;
sel_op_storage_address * (1 - sel_op_storage_address) = 0;
sel_op_sender * (1 - sel_op_sender) = 0;
sel_op_function_selector * (1 - sel_op_function_selector) = 0;
sel_op_transaction_fee * (1 - sel_op_transaction_fee) = 0;
sel_op_chain_id * (1 - sel_op_chain_id) = 0;
sel_op_version * (1 - sel_op_version) = 0;
sel_op_block_number * (1 - sel_op_block_number) = 0;
sel_op_coinbase * (1 - sel_op_coinbase) = 0;
sel_op_timestamp * (1 - sel_op_timestamp) = 0;
sel_op_fee_per_l2_gas * (1 - sel_op_fee_per_l2_gas) = 0;
sel_op_fee_per_da_gas * (1 - sel_op_fee_per_da_gas) = 0;
sel_op_transaction_fee * (1 - sel_op_transaction_fee) = 0;

// MACHINE STATE - GAS
sel_op_l2gasleft * (1 - sel_op_l2gasleft) = 0;
Expand Down Expand Up @@ -410,8 +411,9 @@ namespace main(256);

//===== KERNEL LOOKUPS =======================================================
pol KERNEL_INPUT_SELECTORS = (
sel_op_sender + sel_op_address + sel_op_storage_address + sel_op_chain_id + sel_op_version + sel_op_block_number + sel_op_coinbase +
sel_op_timestamp + sel_op_fee_per_l2_gas + sel_op_fee_per_da_gas + sel_op_transaction_fee
sel_op_address + sel_op_storage_address + sel_op_sender + sel_op_function_selector + sel_op_transaction_fee +
sel_op_chain_id + sel_op_version + sel_op_block_number + sel_op_coinbase + sel_op_timestamp +
sel_op_fee_per_l2_gas + sel_op_fee_per_da_gas
);
// Ensure that only one kernel lookup is active when the kernel_in_offset is active
#[KERNEL_INPUT_ACTIVE_CHECK]
Expand Down Expand Up @@ -568,27 +570,23 @@ namespace main(256);
// We can lookup into a fixed index of this polynomial by including constraints that force the value
// of kernel_in_offset to the value relevant to the given opcode that is active

// CALL CONTEXT
#[SENDER_KERNEL]
sel_op_sender * (kernel.kernel_in_offset - constants.SENDER_SELECTOR) = 0;

// CONTEXT - ENVIRONMENT
#[ADDRESS_KERNEL]
sel_op_address * (kernel.kernel_in_offset - constants.ADDRESS_SELECTOR) = 0;

#[STORAGE_ADDRESS_KERNEL]
sel_op_storage_address * (kernel.kernel_in_offset - constants.STORAGE_ADDRESS_SELECTOR) = 0;

// FEES
#[FEE_DA_GAS_KERNEL]
sel_op_fee_per_da_gas * (kernel.kernel_in_offset - constants.FEE_PER_DA_GAS_SELECTOR) = 0;
#[SENDER_KERNEL]
sel_op_sender * (kernel.kernel_in_offset - constants.SENDER_SELECTOR) = 0;

#[FEE_L2_GAS_KERNEL]
sel_op_fee_per_l2_gas * (kernel.kernel_in_offset - constants.FEE_PER_L2_GAS_SELECTOR) = 0;
#[FUNCTION_SELECTOR_KERNEL]
sel_op_function_selector * (kernel.kernel_in_offset - constants.FUNCTION_SELECTOR_SELECTOR) = 0;

#[FEE_TRANSACTION_FEE_KERNEL]
sel_op_transaction_fee * (kernel.kernel_in_offset - constants.TRANSACTION_FEE_SELECTOR) = 0;

// GLOBALS
// CONTEXT - ENVIRONMENT - GLOBALS
#[CHAIN_ID_KERNEL]
sel_op_chain_id * (kernel.kernel_in_offset - constants.CHAIN_ID_SELECTOR) = 0;

Expand All @@ -598,11 +596,18 @@ namespace main(256);
#[BLOCK_NUMBER_KERNEL]
sel_op_block_number * (kernel.kernel_in_offset - constants.BLOCK_NUMBER_SELECTOR) = 0;

#[TIMESTAMP_KERNEL]
sel_op_timestamp * (kernel.kernel_in_offset - constants.TIMESTAMP_SELECTOR) = 0;

#[COINBASE_KERNEL]
sel_op_coinbase * (kernel.kernel_in_offset - constants.COINBASE_SELECTOR) = 0;

#[TIMESTAMP_KERNEL]
sel_op_timestamp * (kernel.kernel_in_offset - constants.TIMESTAMP_SELECTOR) = 0;
// CONTEXT - ENVIRONMENT - GLOBALS - FEES
#[FEE_DA_GAS_KERNEL]
sel_op_fee_per_da_gas * (kernel.kernel_in_offset - constants.FEE_PER_DA_GAS_SELECTOR) = 0;

#[FEE_L2_GAS_KERNEL]
sel_op_fee_per_l2_gas * (kernel.kernel_in_offset - constants.FEE_PER_L2_GAS_SELECTOR) = 0;

// OUTPUTS LOOKUPS
// Constrain the value of kernel_out_offset to be the correct offset for the operation being performed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
[[maybe_unused]] auto main_sel_op_fdiv = View(new_term.main_sel_op_fdiv); \
[[maybe_unused]] auto main_sel_op_fee_per_da_gas = View(new_term.main_sel_op_fee_per_da_gas); \
[[maybe_unused]] auto main_sel_op_fee_per_l2_gas = View(new_term.main_sel_op_fee_per_l2_gas); \
[[maybe_unused]] auto main_sel_op_function_selector = View(new_term.main_sel_op_function_selector); \
[[maybe_unused]] auto main_sel_op_get_contract_instance = View(new_term.main_sel_op_get_contract_instance); \
[[maybe_unused]] auto main_sel_op_halt = View(new_term.main_sel_op_halt); \
[[maybe_unused]] auto main_sel_op_internal_call = View(new_term.main_sel_op_internal_call); \
Expand Down
Loading

0 comments on commit dde47e9

Please sign in to comment.