Skip to content

Commit df12e79

Browse files
lightclientGiulio2002spencer-tb
committed
core/vm: implement eip-7939 CLZ instruction
Co-authored-by: Giulio <[email protected]> Co-authored-by: spencer-tb <[email protected]>
1 parent 0d58def commit df12e79

File tree

6 files changed

+68
-1
lines changed

6 files changed

+68
-1
lines changed

core/vm/eips.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var activators = map[int]func(*JumpTable){
4242
4762: enable4762,
4343
7702: enable7702,
4444
7907: enable7907,
45+
7939: enable7939,
4546
}
4647

4748
// EnableEIP enables the given EIP on the config.
@@ -294,6 +295,13 @@ func opBlobBaseFee(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
294295
return nil, nil
295296
}
296297

298+
func opCLZ(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
299+
x := scope.Stack.pop()
300+
// count leading zero bits in x
301+
scope.Stack.push(new(uint256.Int).SetUint64(256 - uint64(x.BitLen())))
302+
return nil, nil
303+
}
304+
297305
// enable4844 applies EIP-4844 (BLOBHASH opcode)
298306
func enable4844(jt *JumpTable) {
299307
jt[BLOBHASH] = &operation{
@@ -304,6 +312,15 @@ func enable4844(jt *JumpTable) {
304312
}
305313
}
306314

315+
func enable7939(jt *JumpTable) {
316+
jt[CLZ] = &operation{
317+
execute: opCLZ,
318+
constantGas: GasFastestStep,
319+
minStack: minStack(1, 1),
320+
maxStack: maxStack(1, 1),
321+
}
322+
}
323+
307324
// enable7516 applies EIP-7516 (BLOBBASEFEE opcode)
308325
func enable7516(jt *JumpTable) {
309326
jt[BLOBBASEFEE] = &operation{

core/vm/instructions_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,47 @@ func TestPush(t *testing.T) {
972972
}
973973
}
974974
}
975+
976+
func TestOpCLZ(t *testing.T) {
977+
// set up once
978+
evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{})
979+
980+
tests := []struct {
981+
name string
982+
inputHex string // hexadecimal input for clarity
983+
want uint64 // expected CLZ result
984+
}{
985+
{"zero", "0x0", 256},
986+
{"one", "0x1", 255},
987+
{"all-ones (256 bits)", "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0},
988+
{"low-10-bytes ones", "0xffffffffff", 216}, // 10 bytes = 80 bits, so 256-80=176? Actually input is 0xffffffffff = 40 bits so 256-40=216
989+
}
990+
991+
for _, tc := range tests {
992+
t.Run(tc.name, func(t *testing.T) {
993+
994+
// prepare a fresh stack and PC
995+
stack := newstack()
996+
pc := uint64(0)
997+
998+
// parse input
999+
val := new(uint256.Int)
1000+
if _, err := fmt.Sscan(tc.inputHex, val); err != nil {
1001+
// fallback: try hex
1002+
val.SetFromHex(tc.inputHex)
1003+
}
1004+
1005+
stack.push(val)
1006+
opCLZ(&pc, evm.interpreter, &ScopeContext{Stack: stack})
1007+
1008+
if gotLen := stack.len(); gotLen != 1 {
1009+
t.Fatalf("stack length = %d; want 1", gotLen)
1010+
}
1011+
result := stack.pop()
1012+
1013+
if got := result.Uint64(); got != tc.want {
1014+
t.Fatalf("clz(%q) = %d; want %d", tc.inputHex, got, tc.want)
1015+
}
1016+
})
1017+
}
1018+
}

core/vm/interpreter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter {
106106
// If jump table was not initialised we set the default one.
107107
var table *JumpTable
108108
switch {
109+
case evm.chainRules.IsOsaka:
110+
table = &osakaInstructionSet
109111
case evm.chainRules.IsVerkle:
110112
// TODO replace with proper instruction set when fork is specified
111113
table = &verkleInstructionSet

core/vm/jump_table.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func newVerkleInstructionSet() JumpTable {
9595
func newOsakaInstructionSet() JumpTable {
9696
instructionSet := newPragueInstructionSet()
9797
enable7907(&instructionSet)
98+
enable7939(&instructionSet) // EIP-7939 (CLZ opcode)
9899
return validate(instructionSet)
99100
}
100101

core/vm/jump_table_export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func LookupInstructionSet(rules params.Rules) (JumpTable, error) {
2929
case rules.IsVerkle:
3030
return newCancunInstructionSet(), errors.New("verkle-fork not defined yet")
3131
case rules.IsOsaka:
32-
return newPragueInstructionSet(), errors.New("osaka-fork not defined yet")
32+
return newOsakaInstructionSet(), nil
3333
case rules.IsPrague:
3434
return newPragueInstructionSet(), nil
3535
case rules.IsCancun:

core/vm/opcodes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const (
6262
SHL OpCode = 0x1b
6363
SHR OpCode = 0x1c
6464
SAR OpCode = 0x1d
65+
CLZ OpCode = 0x1e
6566
)
6667

6768
// 0x20 range - crypto.
@@ -282,6 +283,7 @@ var opCodeToString = [256]string{
282283
SHL: "SHL",
283284
SHR: "SHR",
284285
SAR: "SAR",
286+
CLZ: "CLZ",
285287
ADDMOD: "ADDMOD",
286288
MULMOD: "MULMOD",
287289

@@ -484,6 +486,7 @@ var stringToOp = map[string]OpCode{
484486
"SHL": SHL,
485487
"SHR": SHR,
486488
"SAR": SAR,
489+
"CLZ": CLZ,
487490
"ADDMOD": ADDMOD,
488491
"MULMOD": MULMOD,
489492
"KECCAK256": KECCAK256,

0 commit comments

Comments
 (0)