|
| 1 | +import { Common, Hardfork, Mainnet } from '@ethereumjs/common' |
| 2 | +import { assert, describe, it } from 'vitest' |
| 3 | + |
| 4 | +import { type PrefixedHexString, hexToBytes } from '@ethereumjs/util' |
| 5 | +import { createEVM } from '../../src/index.ts' |
| 6 | + |
| 7 | +const MSTORE8 = '53' |
| 8 | +const RETURN = 'f3' |
| 9 | + |
| 10 | +const SETMODX = 'c0' |
| 11 | +const LOADX = 'c1' |
| 12 | +const STOREX = 'c2' |
| 13 | + |
| 14 | +const ADDMODX = 'c3' |
| 15 | +const SUBMODX = 'c4' |
| 16 | +const MULMODX = 'c5' |
| 17 | + |
| 18 | +function numToOpcode(num: number) { |
| 19 | + return num.toString(16).padStart(2, '0') |
| 20 | +} |
| 21 | + |
| 22 | +function mstore8(index: number, value: number) { |
| 23 | + return numToOpcode(value) + numToOpcode(index) + MSTORE8 |
| 24 | +} |
| 25 | + |
| 26 | +function ret(index: number, size: number) { |
| 27 | + return size + index + RETURN |
| 28 | +} |
| 29 | + |
| 30 | +function setupx(num_val_slots: number, mod_size: number, mod_offset: number) { |
| 31 | + return numToOpcode(num_val_slots) + numToOpcode(mod_size) + numToOpcode(mod_offset) + SETMODX |
| 32 | +} |
| 33 | + |
| 34 | +function storex(num_vals: number, val_offset: number, val_slot: number) { |
| 35 | + return numToOpcode(num_vals) + numToOpcode(val_offset) + numToOpcode(val_slot) + STOREX |
| 36 | +} |
| 37 | + |
| 38 | +function loadx(num_vals: number, val_idx: number, mem_offset: number) { |
| 39 | + return numToOpcode(num_vals) + numToOpcode(val_idx) + numToOpcode(mem_offset) + LOADX |
| 40 | +} |
| 41 | + |
| 42 | +function addmodx(result_slot_idx: number, x_slot_idx: number, y_slot_idx: number) { |
| 43 | + return ADDMODX + numToOpcode(result_slot_idx) + numToOpcode(x_slot_idx) + numToOpcode(y_slot_idx) |
| 44 | +} |
| 45 | + |
| 46 | +function submodx(result_slot_idx: number, x_slot_idx: number, y_slot_idx: number) { |
| 47 | + return SUBMODX + numToOpcode(result_slot_idx) + numToOpcode(x_slot_idx) + numToOpcode(y_slot_idx) |
| 48 | +} |
| 49 | + |
| 50 | +function mulmodx(result_slot_idx: number, x_slot_idx: number, y_slot_idx: number) { |
| 51 | + return MULMODX + numToOpcode(result_slot_idx) + numToOpcode(x_slot_idx) + numToOpcode(y_slot_idx) |
| 52 | +} |
| 53 | + |
| 54 | +describe('EVM -> getActiveOpcodes()', () => { |
| 55 | + it('should not expose opcodes from a follow-up HF (istanbul -> petersburg)', async () => { |
| 56 | + const common = new Common({ chain: Mainnet, eips: [6990] }) |
| 57 | + const evm = await createEVM({ common }) |
| 58 | + |
| 59 | + // create bytecode |
| 60 | + const bytecode = |
| 61 | + '0x' + |
| 62 | + mstore8(0, 7) + // store value 0x07 at index 0 in memory |
| 63 | + setupx(3, 32, 0) + |
| 64 | + mstore8(32, 3) + |
| 65 | + mstore8(64, 6) + |
| 66 | + storex(2, 32, 0) + |
| 67 | + addmodx(2, 1, 0) + |
| 68 | + mulmodx(2, 2, 1) + |
| 69 | + submodx(2, 2, 1) + |
| 70 | + loadx(1, 2, 96) + |
| 71 | + ret(96, 32) |
| 72 | + |
| 73 | + console.log(bytecode) |
| 74 | + |
| 75 | + const result = await evm.runCall({ |
| 76 | + data: hexToBytes(bytecode as PrefixedHexString), |
| 77 | + gasLimit: BigInt(0xffffff), |
| 78 | + }) |
| 79 | + |
| 80 | + console.log(result) |
| 81 | + }) |
| 82 | +}) |
0 commit comments