Skip to content

Commit c47b68f

Browse files
spencer-tbgurukamath
authored andcommitted
feat(osaka): add eip-7939 count leading zeros opcode (#1275)
* feat(vm): add eip-7939 count leading zeros opcode. * Update src/ethereum/osaka/vm/instructions/bitwise.py Co-authored-by: Guruprasad Kamath <[email protected]> * code formatting --------- Co-authored-by: Guruprasad Kamath <[email protected]> Co-authored-by: Guruprasad Kamath <[email protected]>
1 parent 0a1eebf commit c47b68f

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/ethereum/osaka/vm/instructions/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Ops(enum.Enum):
6565
SHL = 0x1B
6666
SHR = 0x1C
6767
SAR = 0x1D
68+
CLZ = 0x1E
6869

6970
# Keccak Op
7071
KECCAK = 0x20
@@ -241,6 +242,7 @@ class Ops(enum.Enum):
241242
Ops.SHL: bitwise_instructions.bitwise_shl,
242243
Ops.SHR: bitwise_instructions.bitwise_shr,
243244
Ops.SAR: bitwise_instructions.bitwise_sar,
245+
Ops.CLZ: bitwise_instructions.count_leading_zeros,
244246
Ops.KECCAK: keccak_instructions.keccak,
245247
Ops.SLOAD: storage_instructions.sload,
246248
Ops.BLOCKHASH: block_instructions.block_hash,

src/ethereum/osaka/vm/instructions/bitwise.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,31 @@ def bitwise_sar(evm: Evm) -> None:
238238

239239
# PROGRAM COUNTER
240240
evm.pc += Uint(1)
241+
242+
243+
def count_leading_zeros(evm: Evm) -> None:
244+
"""
245+
Count the number of leading zero bits in a 256-bit word.
246+
247+
Pops one value from the stack and pushes the number of leading zero bits.
248+
If the input is zero, pushes 256.
249+
250+
Parameters
251+
----------
252+
evm :
253+
The current EVM frame.
254+
"""
255+
# STACK
256+
x = pop(evm.stack)
257+
258+
# GAS
259+
charge_gas(evm, GAS_VERY_LOW)
260+
261+
# OPERATION
262+
bit_length = U256(x.bit_length())
263+
result = U256(256) - bit_length
264+
265+
push(evm.stack, result)
266+
267+
# PROGRAM COUNTER
268+
evm.pc += Uint(1)

whitelist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,5 @@ eoa
475475
blockchain
476476
listdir
477477
precompiles
478+
479+
CLZ

0 commit comments

Comments
 (0)