|
| 1 | +""" |
| 2 | +abstract: Tests [EIP-7939: Count leading zeros (CLZ) opcode](https://eips.ethereum.org/EIPS/eip-7939) |
| 3 | + Test cases for [EIP-7939: Count leading zeros (CLZ) opcode](https://eips.ethereum.org/EIPS/eip-7939). |
| 4 | +""" |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from ethereum_test_forks import Fork |
| 9 | +from ethereum_test_tools import Account, Alloc, CodeGasMeasure, StateTestFiller, Transaction |
| 10 | +from ethereum_test_tools.vm.opcode import Opcodes as Op |
| 11 | + |
| 12 | +REFERENCE_SPEC_GIT_PATH = "EIPS/eip-7939.md" |
| 13 | +REFERENCE_SPEC_VERSION = "1a4aed0bca3a74bc2caa37c16514098e3d072a8c" |
| 14 | + |
| 15 | +CLZ_GAS_COST = 3 |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.valid_from("Osaka") |
| 19 | +@pytest.mark.parametrize("bits", range(257)) |
| 20 | +def test_clz_filled_format_0b0111(state_test: StateTestFiller, bits: int, pre: Alloc, fork: Fork): |
| 21 | + """ |
| 22 | + Test CLZ opcode. Value is of format 0b0011..11. |
| 23 | + The CLZ(0) case is also included in this test. |
| 24 | + """ |
| 25 | + value = 2**256 - 1 >> bits |
| 26 | + contract_address = pre.deploy_contract( |
| 27 | + Op.SSTORE(0, Op.CLZ(value)), |
| 28 | + storage={"0x00": "0xdeadbeef"}, |
| 29 | + ) |
| 30 | + sender = pre.fund_eoa() |
| 31 | + |
| 32 | + tx = Transaction(to=contract_address, sender=sender, gas_limit=200_000) |
| 33 | + |
| 34 | + post = { |
| 35 | + # gas cost calculation adds the cost of PUSH (3), POP (2) and GAS (2) to the costs of CLZ |
| 36 | + # TODO: read gas costs of these opcode from fork |
| 37 | + contract_address: Account(storage={"0x00": bits}), |
| 38 | + } |
| 39 | + |
| 40 | + state_test(pre=pre, post=post, tx=tx) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.valid_from("Osaka") |
| 44 | +@pytest.mark.parametrize("bits", range(0, 256)) |
| 45 | +def test_clz_filled_format_0b0100(state_test: StateTestFiller, bits: int, pre: Alloc, fork: Fork): |
| 46 | + """Test CLZ opcode. Value is of format 0b0010..00.""" |
| 47 | + value = 1 << bits |
| 48 | + contract_address = pre.deploy_contract( |
| 49 | + Op.SSTORE(0, Op.CLZ(value)), |
| 50 | + storage={"0x00": "0xdeadbeef"}, |
| 51 | + ) |
| 52 | + sender = pre.fund_eoa() |
| 53 | + |
| 54 | + tx = Transaction(to=contract_address, sender=sender, gas_limit=200_000) |
| 55 | + |
| 56 | + post = { |
| 57 | + # gas cost calculation adds the cost of PUSH (3), POP (2) and GAS (2) to the costs of CLZ |
| 58 | + # TODO: read gas costs of these opcode from fork |
| 59 | + contract_address: Account(storage={"0x00": 255 - bits}), |
| 60 | + } |
| 61 | + |
| 62 | + state_test(pre=pre, post=post, tx=tx) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.valid_from("Osaka") |
| 66 | +def test_clz_gas(state_test: StateTestFiller, pre: Alloc, fork: Fork): |
| 67 | + """Tests the gas cost of the CLZ opcode.""" |
| 68 | + gas_cost = fork.gas_costs() |
| 69 | + contract_address = pre.deploy_contract( |
| 70 | + Op.SSTORE( |
| 71 | + 0, |
| 72 | + CodeGasMeasure(code=Op.CLZ(Op.PUSH1(1)), extra_stack_items=1, overhead_cost=0), |
| 73 | + ), |
| 74 | + storage={"0x00": "0xdeadbeef"}, |
| 75 | + ) |
| 76 | + sender = pre.fund_eoa() |
| 77 | + |
| 78 | + tx = Transaction(to=contract_address, sender=sender, gas_limit=200_000) |
| 79 | + |
| 80 | + post = { |
| 81 | + # Cost measured is CLZ + PUSH1 |
| 82 | + contract_address: Account(storage={"0x00": CLZ_GAS_COST + gas_cost.G_VERY_LOW}), |
| 83 | + } |
| 84 | + |
| 85 | + state_test(pre=pre, post=post, tx=tx) |
0 commit comments