Skip to content

Commit 2d40d54

Browse files
committed
Debug addmodx opcode
1 parent 7ead43b commit 2d40d54

File tree

4 files changed

+81
-19
lines changed

4 files changed

+81
-19
lines changed

packages/evm/src/evmmax/util.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { bytesToHex } from '@ethereumjs/util'
2+
13
export const MASK_64 = (1n << 64n) - 1n
24

35
/**
@@ -305,6 +307,11 @@ export function madd2(a: bigint, b: bigint, c: bigint, d: bigint): [bigint, bigi
305307
}
306308

307309
export function extractEVMMAXImmediateInputs(pc: number, code: Uint8Array) {
310+
console.log('dbg300')
311+
console.log(bytesToHex(code))
312+
console.log(pc)
313+
console.log(code[pc])
314+
console.log(code[pc - 1])
308315
const out = Number(code[pc + 1])
309316
const outStride = Number(code[pc + 2])
310317
const x = Number(code[pc + 3])

packages/evm/src/opcodes/functions.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -996,41 +996,62 @@ export const handlers: Map<number, OpHandler> = new Map([
996996
function (runState, _common) {
997997
const [id, modOffset, modSize, allocCount] = runState.stack.popN(4)
998998
const modulus = runState.memory.read(Number(modOffset), Number(modSize))
999+
// console.log('dbg600')
1000+
// console.log(modulus)
9991001
runState.evmmaxState.allocAndSetActive(Number(id), modulus, allocCount)
10001002
},
10011003
],
1002-
// 0xc1: STOREX
1004+
// 0xc1: LOADX
10031005
[
10041006
0xc1,
10051007
function (runState, _common) {
1006-
// TODO figure out if we need to use extend(), _store(), or or just write()
10071008
const [dest, source, count] = runState.stack.popN(3)
10081009
const copySize = Number(count) * runState.evmmaxState.getActive()?.getElemSize()
1009-
const srcBuf = runState.memory.read(Number(source), Number(count) * copySize)
1010-
runState.evmmaxState.getActive()?.store(Number(dest), Number(count), srcBuf)
1010+
const destBuf = new Uint8Array(copySize)
1011+
runState.evmmaxState.getActive()?.load(destBuf, Number(source), Number(count))
1012+
runState.memory.write(Number(dest), copySize, destBuf)
10111013
},
10121014
],
1013-
// 0xc2: LOADX
1015+
// 0xc2: STOREX
10141016
[
10151017
0xc2,
10161018
function (runState, _common) {
1019+
// TODO figure out if we need to use extend(), _store(), or or just write()
10171020
const [dest, source, count] = runState.stack.popN(3)
10181021
const copySize = Number(count) * runState.evmmaxState.getActive()?.getElemSize()
1019-
const destBuf = new Uint8Array(copySize)
1020-
runState.evmmaxState.getActive()?.load(destBuf, Number(source), Number(count))
1021-
runState.memory.write(Number(dest), copySize, destBuf)
1022+
const srcBuf = runState.memory.read(Number(source), Number(count) * copySize)
1023+
console.log('dbg400')
1024+
// console.log(dest)
1025+
// console.log(source)
1026+
// console.log(count)
1027+
// console.log(srcBuf)
1028+
// console.log(runState.memory._store)
1029+
console.log(runState.evmmaxState.getActive().scratchSpace)
1030+
runState.evmmaxState.getActive()?.store(Number(dest), Number(count), srcBuf)
1031+
console.log(runState.evmmaxState.getActive().scratchSpace)
10221032
},
10231033
],
10241034
// 0xc3: ADDMODX
10251035
[
10261036
0xc3,
10271037
function (runState, _common) {
1038+
console.log(runState.code)
10281039
const [out, outStride, x, xStride, y, yStride, count] = extractEVMMAXImmediateInputs(
10291040
runState.programCounter,
10301041
runState.code,
10311042
)
10321043
runState.programCounter += 7
1044+
console.log('dbg500')
1045+
console.log(out)
1046+
console.log(outStride)
1047+
console.log(x)
1048+
console.log(xStride)
1049+
console.log(y)
1050+
console.log(yStride)
1051+
console.log(count)
1052+
console.log(runState.evmmaxState.getActive().scratchSpace)
10331053
runState.evmmaxState.getActive().addM(out, outStride, x, xStride, y, yStride, count)
1054+
console.log(runState.evmmaxState.getActive().scratchSpace)
10341055
},
10351056
],
10361057
// 0xc4: SUBMODX

packages/evm/src/opcodes/util.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export function isPowerOfTwo(val: bigint): boolean {
210210
}
211211

212212
export function makeEVMMAXArithGasFunc(opCosts: number[]): Function {
213-
return function (runState: RunState, gas: number, common: Common) {
213+
return function (runState: RunState, gas: bigint, common: Common) {
214214
const [out, outStride, x, xStride, y, yStride, count] = extractEVMMAXImmediateInputs(
215215
runState.programCounter,
216216
runState.code,
@@ -225,7 +225,13 @@ export function makeEVMMAXArithGasFunc(opCosts: number[]): Function {
225225
trap('bad parameters')
226226
}
227227

228-
return gas + count * opCosts[runState.evmmaxState.getActive().modulus.length - 1]
228+
// console.log('dbg200')
229+
// console.log(gas)
230+
// console.log(count)
231+
// console.log(opCosts[runState.evmmaxState.getActive().modulus.length - 1])
232+
return (
233+
gas + BigInt(count) * BigInt(opCosts[runState.evmmaxState.getActive().modulus.length - 1])
234+
)
229235
}
230236
}
231237

packages/evm/test/evmmax/opcodes.spec.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
22
import { assert, describe, it } from 'vitest'
33

4-
import { type PrefixedHexString, hexToBytes } from '@ethereumjs/util'
4+
import { type PrefixedHexString, createAddressFromString, hexToBytes } from '@ethereumjs/util'
55
import { LOADX_BASE_COST, SETMODX_BASE_COST, STOREX_BASE_COST } from '../../src/evmmax/index.ts'
66
import { createEVM } from '../../src/index.ts'
77

@@ -18,6 +18,7 @@ const SUBMODX = 'c4'
1818
const MULMODX = 'c5'
1919

2020
function numToOpcode(num: number) {
21+
if (num > 255) throw 'unsupported: > 255 cannot fit in one byte'
2122
return num.toString(16).padStart(2, '0')
2223
}
2324

@@ -46,16 +47,33 @@ function setupx(id: number, mod_size: number, mod_offset: number, alloc_count: n
4647

4748
function storex(dst: number, source: number, count: number) {
4849
return (
49-
PUSH1 + numToOpcode(dst) + PUSH1 + numToOpcode(source) + PUSH1 + numToOpcode(count) + STOREX
50+
PUSH1 + numToOpcode(count) + PUSH1 + numToOpcode(source) + PUSH1 + numToOpcode(dst) + STOREX
5051
)
5152
}
5253

5354
function loadx(num_vals: number, val_idx: number, mem_offset: number) {
5455
return numToOpcode(num_vals) + numToOpcode(val_idx) + numToOpcode(mem_offset) + LOADX
5556
}
5657

57-
function addmodx(result_slot_idx: number, x_slot_idx: number, y_slot_idx: number) {
58-
return ADDMODX + numToOpcode(result_slot_idx) + numToOpcode(x_slot_idx) + numToOpcode(y_slot_idx)
58+
function addmodx(
59+
result_slot_idx: number,
60+
result_stride: number,
61+
x_slot_idx: number,
62+
x_stride: number,
63+
y_slot_idx: number,
64+
y_stride: number,
65+
count: number,
66+
) {
67+
return (
68+
ADDMODX +
69+
numToOpcode(result_slot_idx) +
70+
numToOpcode(result_stride) +
71+
numToOpcode(x_slot_idx) +
72+
numToOpcode(x_stride) +
73+
numToOpcode(y_slot_idx) +
74+
numToOpcode(y_stride) +
75+
numToOpcode(count)
76+
)
5977
}
6078

6179
function submodx(result_slot_idx: number, x_slot_idx: number, y_slot_idx: number) {
@@ -84,27 +102,37 @@ describe('EVM -> getActiveOpcodes()', () => {
84102
})
85103
const evm = await createEVM({ common })
86104

105+
console.log('dbg100')
106+
console.log(addmodx(0, 1, 1, 1, 2, 1, 1))
87107
// create bytecode
88108
const bytecode =
89109
'0x' +
90-
mstore8(0, 7) + // store value 0x07 at index 0 in memory
110+
mstore8(0, 8) + // store value 0x07 at index 0 in memory
91111
setupx(0, 1, 0, 3) +
92112
mstore8(1, 3) +
93113
mstore8(2, 6) +
94-
storex(2, 0, 0) +
95-
addmodx(2, 1, 0)
114+
storex(1, 1, 1) +
115+
storex(2, 2, 1) +
116+
addmodx(0, 1, 1, 1, 2, 1, 1)
96117
// + mulmodx(2, 2, 1)
97118
// + submodx(2, 2, 1)
98119
// + loadx(1, 2, 96)
99120
// + ret(96, 32)
100121

101-
console.log(bytecode)
122+
const ADDR_TO_CALL = createAddressFromString('0x' + '20'.repeat(20))
123+
124+
await evm.stateManager.putCode(ADDR_TO_CALL, hexToBytes(bytecode as PrefixedHexString))
102125

103126
const result = await evm.runCall({
104-
data: hexToBytes(bytecode as PrefixedHexString),
127+
to: ADDR_TO_CALL,
105128
gasLimit: BigInt(0xffffff),
106129
})
107130

131+
// const result1 = await evm.runCall({
132+
// data: hexToBytes(bytecode as PrefixedHexString),
133+
// gasLimit: BigInt(0xffffff),
134+
// })
135+
108136
console.log(result)
109137
})
110138
})

0 commit comments

Comments
 (0)