Skip to content

Commit

Permalink
Implement BLOBHASH opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Feb 18, 2024
1 parent 2d8184e commit 1bcf799
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 7 deletions.
6 changes: 3 additions & 3 deletions evm_arithmetization/src/cpu/kernel/asm/core/exception.asm
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ min_stack_len_for_opcode:
BYTES 0 // 0x46, CHAINID
BYTES 0 // 0x47, SELFBALANCE
BYTES 0 // 0x48, BASEFEE
BYTES 0 // 0x49, invalid
BYTES 1 // 0x49, BLOBHASH
BYTES 0 // 0x4a, BLOBBASEFEE
%rep 5 // 0x4b-0x4f, invalid
BYTES 0
Expand Down Expand Up @@ -371,11 +371,11 @@ gas_cost_for_opcode:
BYTES 0
%endrep

%rep 25 //0x30-0x48, only syscalls
%rep 26 //0x30-0x49, only syscalls
BYTES 0
%endrep

%rep 7 // 0x49-0x4f, invalid
%rep 6 // 0x4a-0x4f, invalid
BYTES 0
%endrep

Expand Down
2 changes: 1 addition & 1 deletion evm_arithmetization/src/cpu/kernel/asm/core/syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ global syscall_jumptable:
JUMPTABLE sys_chainid
JUMPTABLE sys_selfbalance
JUMPTABLE sys_basefee
JUMPTABLE panic // 0x49 is invalid
JUMPTABLE sys_blobhash
JUMPTABLE sys_blobbasefee
%rep 5
JUMPTABLE panic // 0x4b-0x4f are invalid opcodes
Expand Down
33 changes: 33 additions & 0 deletions evm_arithmetization/src/cpu/kernel/asm/memory/metadata.asm
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,39 @@ global sys_basefee:
SWAP1
EXIT_KERNEL

global sys_blobhash:
// stack: kexit_info, index
%charge_gas_const(@GAS_HASH_OPCODE)
// stack: kexit_info, index
%blobhash
// stack: blobhash, kexit_info
SWAP1
EXIT_KERNEL

%macro blobhash
// stack: kexit_info, index
SWAP1
// stack: index, kexit_info
%mload_global_metadata(@GLOBAL_METADATA_BLOB_VERSIONED_HASHES_RLP_LEN)
DUP2
GT
// stack: index > len, index, kexit_info
%jumpi(%%index_too_big)
PUSH @SEGMENT_TXN_BLOB_VERSIONED_HASHES
%build_kernel_address
// stack: read_addr, kexit_info
MLOAD_GENERAL
%jump(%%end)
%%index_too_big:
// The index is larger than the list, just push 0.
// stack: index, kexit_info
POP
PUSH 0
// stack: 0, kexit_info
%%end:
// stack: blobhash, kexit_info
%endmacro

global sys_blobbasefee:
// stack: kexit_info
%charge_gas_const(@GAS_BASE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) const INVALID_OPCODES_USER: U256 = u256_from_set_index_ranges(&[
0x0c..=0x0f,
0x1e..=0x1f,
0x21..=0x2f,
0x49..=0x4f,
0x4a..=0x4f,
0x5c..=0x5e,
0xa5..=0xef,
0xf6..=0xf9,
Expand Down
3 changes: 2 additions & 1 deletion evm_arithmetization/src/cpu/kernel/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ const EC_CONSTANTS: [(&str, [u8; 32]); 20] = [
),
];

const GAS_CONSTANTS: [(&str, u16); 36] = [
const GAS_CONSTANTS: [(&str, u16); 37] = [
("GAS_ZERO", 0),
("GAS_JUMPDEST", 1),
("GAS_BASE", 2),
Expand Down Expand Up @@ -252,6 +252,7 @@ const GAS_CONSTANTS: [(&str, u16); 36] = [
("GAS_KECCAK256WORD", 6),
("GAS_COPY", 3),
("GAS_BLOCKHASH", 20),
("GAS_HASH_OPCODE", 3),
];

const REFUND_CONSTANTS: [(&str, u16); 2] = [("REFUND_SCLEAR", 4_800), ("MAX_REFUND_QUOTIENT", 5)];
Expand Down
1 change: 1 addition & 0 deletions evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ impl<'a, F: Field> Interpreter<'a, F> {
0x46 => self.run_syscall(opcode, 0, true), // "CHAINID",
0x47 => self.run_syscall(opcode, 0, true), // SELFABALANCE,
0x48 => self.run_syscall(opcode, 0, true), // "BASEFEE",
0x49 => self.run_syscall(opcode, 1, false), // "BLOBHASH",
0x4a => self.run_syscall(opcode, 0, true), // "BLOBBASEFEE",
0x50 => self.run_pop(), // "POP",
0x51 => self.run_syscall(opcode, 1, false), // "MLOAD",
Expand Down
3 changes: 2 additions & 1 deletion evm_arithmetization/src/witness/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ use crate::{arithmetic, logic};
pub(crate) enum Operation {
Iszero,
Not,
Syscall(u8, usize, bool), // (syscall number, minimum stack length, increases stack length)
/// (syscall number, minimum stack length, increases stack length)
Syscall(u8, usize, bool),
Eq,
BinaryLogic(logic::Op),
BinaryArithmetic(arithmetic::BinaryOperator),
Expand Down
1 change: 1 addition & 0 deletions evm_arithmetization/src/witness/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub(crate) fn decode(registers: RegistersState, opcode: u8) -> Result<Operation,
(0x46, _) => Ok(Operation::Syscall(opcode, 0, true)), // CHAINID
(0x47, _) => Ok(Operation::Syscall(opcode, 0, true)), // SELFBALANCE
(0x48, _) => Ok(Operation::Syscall(opcode, 0, true)), // BASEFEE
(0x49, _) => Ok(Operation::Syscall(opcode, 1, false)), // BLOBHASH
(0x4a, _) => Ok(Operation::Syscall(opcode, 0, true)), // BLOBBASEFEE
(0x50, _) => Ok(Operation::Pop),
(0x51, _) => Ok(Operation::Syscall(opcode, 1, false)), // MLOAD
Expand Down

0 comments on commit 1bcf799

Please sign in to comment.