Skip to content

Commit

Permalink
feat(avm)!: GETENVVAR + ISSTATICCALL (#8692)
Browse files Browse the repository at this point in the history
The reason I'm keeping all the oracles on the Noir side, is because I
need to pass the enum index as an immediate and Noir doesn't allow that
in any other way. That is, if you had an oracle `get_env_var(var_idx:
u8)`, when you call it like `get_env_var(12)` you would get a memory
address with the 12 in it; and not the `12` constant.
  • Loading branch information
fcarreiro authored Sep 23, 2024
1 parent 74182c4 commit 02cff0b
Show file tree
Hide file tree
Showing 47 changed files with 1,433 additions and 1,532 deletions.
31 changes: 2 additions & 29 deletions avm-transpiler/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,8 @@ pub enum AvmOpcode {
CAST_8,
CAST_16,
// Execution environment
ADDRESS,
STORAGEADDRESS,
SENDER,
FUNCTIONSELECTOR,
TRANSACTIONFEE,
CHAINID,
VERSION,
BLOCKNUMBER,
TIMESTAMP,
FEEPERL2GAS,
FEEPERDAGAS,
GETENVVAR_16,
CALLDATACOPY,
// Gas
L2GASLEFT,
DAGASLEFT,
// Control flow
JUMP_16,
JUMPI_16,
Expand Down Expand Up @@ -138,25 +125,11 @@ impl AvmOpcode {
AvmOpcode::CAST_16 => "CAST_16",

// Execution Environment
AvmOpcode::ADDRESS => "ADDRESS",
AvmOpcode::STORAGEADDRESS => "STORAGEADDRESS",
AvmOpcode::SENDER => "SENDER",
AvmOpcode::FUNCTIONSELECTOR => "FUNCTIONSELECTOR",
AvmOpcode::TRANSACTIONFEE => "TRANSACTIONFEE",
// Execution Environment - Globals
AvmOpcode::CHAINID => "CHAINID",
AvmOpcode::VERSION => "VERSION",
AvmOpcode::BLOCKNUMBER => "BLOCKNUMBER",
AvmOpcode::TIMESTAMP => "TIMESTAMP",
AvmOpcode::FEEPERL2GAS => "FEEPERL2GAS",
AvmOpcode::FEEPERDAGAS => "FEEPERDAGAS",
AvmOpcode::GETENVVAR_16 => "GETENVVAR_16",
// Execution Environment - Calldata
AvmOpcode::CALLDATACOPY => "CALLDATACOPY",

// Machine State
// Machine State - Gas
AvmOpcode::L2GASLEFT => "L2GASLEFT",
AvmOpcode::DAGASLEFT => "DAGASLEFT",
// Machine State - Internal Control Flow
AvmOpcode::JUMP_16 => "JUMP_16",
AvmOpcode::JUMPI_16 => "JUMPI_16",
Expand Down
56 changes: 38 additions & 18 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,23 @@ fn handle_getter_instruction(
destinations: &Vec<ValueOrArray>,
inputs: &Vec<ValueOrArray>,
) {
enum EnvironmentVariable {
ADDRESS,
STORAGEADDRESS,
SENDER,
FUNCTIONSELECTOR,
TRANSACTIONFEE,
CHAINID,
VERSION,
BLOCKNUMBER,
TIMESTAMP,
FEEPERL2GAS,
FEEPERDAGAS,
ISSTATICCALL,
L2GASLEFT,
DAGASLEFT,
}

// For the foreign calls we want to handle, we do not want inputs, as they are getters
assert!(inputs.is_empty());
assert!(destinations.len() == 1);
Expand All @@ -742,28 +759,31 @@ fn handle_getter_instruction(
_ => panic!("ForeignCall address destination should be a single value"),
};

let opcode = match function {
"avmOpcodeAddress" => AvmOpcode::ADDRESS,
"avmOpcodeStorageAddress" => AvmOpcode::STORAGEADDRESS,
"avmOpcodeSender" => AvmOpcode::SENDER,
"avmOpcodeFeePerL2Gas" => AvmOpcode::FEEPERL2GAS,
"avmOpcodeFeePerDaGas" => AvmOpcode::FEEPERDAGAS,
"avmOpcodeTransactionFee" => AvmOpcode::TRANSACTIONFEE,
"avmOpcodeChainId" => AvmOpcode::CHAINID,
"avmOpcodeVersion" => AvmOpcode::VERSION,
"avmOpcodeBlockNumber" => AvmOpcode::BLOCKNUMBER,
"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),
let var_idx = match function {
"avmOpcodeAddress" => EnvironmentVariable::ADDRESS,
"avmOpcodeStorageAddress" => EnvironmentVariable::STORAGEADDRESS,
"avmOpcodeSender" => EnvironmentVariable::SENDER,
"avmOpcodeFeePerL2Gas" => EnvironmentVariable::FEEPERL2GAS,
"avmOpcodeFeePerDaGas" => EnvironmentVariable::FEEPERDAGAS,
"avmOpcodeTransactionFee" => EnvironmentVariable::TRANSACTIONFEE,
"avmOpcodeChainId" => EnvironmentVariable::CHAINID,
"avmOpcodeVersion" => EnvironmentVariable::VERSION,
"avmOpcodeBlockNumber" => EnvironmentVariable::BLOCKNUMBER,
"avmOpcodeTimestamp" => EnvironmentVariable::TIMESTAMP,
"avmOpcodeL2GasLeft" => EnvironmentVariable::L2GASLEFT,
"avmOpcodeDaGasLeft" => EnvironmentVariable::DAGASLEFT,
"avmOpcodeFunctionSelector" => EnvironmentVariable::FUNCTIONSELECTOR,
"avmOpcodeIsStaticCall" => EnvironmentVariable::ISSTATICCALL,
_ => panic!("Transpiler doesn't know how to process getter {:?}", function),
};

avm_instrs.push(AvmInstruction {
opcode,
opcode: AvmOpcode::GETENVVAR_16,
indirect: Some(ALL_DIRECT),
operands: vec![AvmOperand::U32 { value: dest_offset as u32 }],
operands: vec![
AvmOperand::U8 { value: var_idx as u8 },
AvmOperand::U16 { value: dest_offset as u16 },
],
..Default::default()
});
}
Expand Down
1 change: 1 addition & 0 deletions barretenberg/cpp/pil/avm/constants_gen.pil
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace constants(256);
pol ADDRESS_SELECTOR = 1;
pol STORAGE_ADDRESS_SELECTOR = 1;
pol FUNCTION_SELECTOR_SELECTOR = 2;
pol IS_STATIC_CALL_SELECTOR = 4;
pol START_GLOBAL_VARIABLES = 29;
pol CHAIN_ID_SELECTOR = 29;
pol VERSION_SELECTOR = 30;
Expand Down
11 changes: 7 additions & 4 deletions barretenberg/cpp/pil/avm/kernel.pil
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace main(256);
pol commit sel_kernel_out;

// Kernel Outputs
//
//
// The current implementation of kernel outputs is described within https://hackmd.io/zP1oMXF6Rf-L-ZZLXWmfHg

// Global side effect counter; incremented after each side effect is produced.
pol commit side_effect_counter;

Expand Down Expand Up @@ -98,6 +98,9 @@ namespace main(256);
#[FEE_TRANSACTION_FEE_KERNEL]
sel_op_transaction_fee * (kernel_in_offset - constants.TRANSACTION_FEE_SELECTOR) = 0;

#[IS_STATIC_CALL_KERNEL]
sel_op_is_static_call * (kernel_in_offset - constants.IS_STATIC_CALL_SELECTOR) = 0;

// CONTEXT - ENVIRONMENT - GLOBALS
#[CHAIN_ID_KERNEL]
sel_op_chain_id * (kernel_in_offset - constants.CHAIN_ID_SELECTOR) = 0;
Expand All @@ -123,7 +126,7 @@ namespace main(256);
#[NOTE_HASH_KERNEL_OUTPUT]
sel_op_note_hash_exists * (kernel_out_offset - (constants.START_NOTE_HASH_EXISTS_WRITE_OFFSET + note_hash_exist_write_offset)) = 0;
sel_first * note_hash_exist_write_offset = 0;

#[EMIT_NOTE_HASH_KERNEL_OUTPUT]
sel_op_emit_note_hash * (kernel_out_offset - (constants.START_EMIT_NOTE_HASH_WRITE_OFFSET + emit_note_hash_write_offset)) = 0;
sel_first * emit_note_hash_write_offset = 0;
Expand Down Expand Up @@ -168,7 +171,7 @@ namespace main(256);
pol KERNEL_INPUT_SELECTORS = 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_timestamp
+ sel_op_fee_per_l2_gas + sel_op_fee_per_da_gas;
+ sel_op_fee_per_l2_gas + sel_op_fee_per_da_gas + sel_op_is_static_call;
// Ensure that only one kernel lookup is active when the kernel_in_offset is active
#[KERNEL_INPUT_ACTIVE_CHECK]
KERNEL_INPUT_SELECTORS * (1 - sel_q_kernel_lookup) = 0;
Expand Down
2 changes: 2 additions & 0 deletions barretenberg/cpp/pil/avm/main.pil
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace main(256);
pol commit sel_op_sender;
pol commit sel_op_function_selector;
pol commit sel_op_transaction_fee;
pol commit sel_op_is_static_call;

// CONTEXT - ENVIRONMENT - GLOBALS
pol commit sel_op_chain_id;
Expand Down Expand Up @@ -227,6 +228,7 @@ namespace main(256);
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_is_static_call * (1 - sel_op_is_static_call) = 0;

// MACHINE STATE - GAS
sel_op_l2gasleft * (1 - sel_op_l2gasleft) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
rows[i].main_sel_op_get_contract_instance);
polys.main_sel_op_internal_call.set_if_valid_index(i, rows[i].main_sel_op_internal_call);
polys.main_sel_op_internal_return.set_if_valid_index(i, rows[i].main_sel_op_internal_return);
polys.main_sel_op_is_static_call.set_if_valid_index(i, rows[i].main_sel_op_is_static_call);
polys.main_sel_op_jump.set_if_valid_index(i, rows[i].main_sel_op_jump);
polys.main_sel_op_jumpi.set_if_valid_index(i, rows[i].main_sel_op_jumpi);
polys.main_sel_op_keccak.set_if_valid_index(i, rows[i].main_sel_op_keccak);
Expand Down
Loading

0 comments on commit 02cff0b

Please sign in to comment.