Skip to content

Commit 3c4573d

Browse files
committed
Fix bugs and continue to debug opcode tests
1 parent 3503335 commit 3c4573d

File tree

6 files changed

+261
-105
lines changed

6 files changed

+261
-105
lines changed

packages/evm/src/evmmax/fieldContext.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BIGINT_8, bigIntToBytes, bytesToBigInt, concatBytes } from '@ethereumjs/util'
1+
import { BIGINT_8, bigIntToBytes, bytesToBigInt, bytesToHex, concatBytes } from '@ethereumjs/util'
22

33
import {
44
addModBinary,
@@ -42,6 +42,8 @@ export class FieldContext {
4242
public scratchSpaceElemCount: bigint
4343
public outputWriteBuf: bigint[] | undefined
4444

45+
public modByteSize: number
46+
4547
constructor(modBytes: Uint8Array, scratchSize: bigint) {
4648
if (modBytes.length > MAX_MODULUS_SIZE) {
4749
throw new Error('modulus cannot be greater than 768 bits')
@@ -61,7 +63,13 @@ export class FieldContext {
6163

6264
const mod = bytesToBigInt(modBytes)
6365
const paddedSize = BigInt(Math.ceil(modBytes.length / 8) * 8) // Compute paddedSize as the next multiple of 8 bytes
64-
66+
this.modByteSize = modBytes.length
67+
console.log('dbg300')
68+
console.log(modBytes.length)
69+
console.log(paddedSize)
70+
console.log(mod)
71+
console.log(Math.ceil(modBytes.length / 8))
72+
console.log(modBytes.length / 8)
6573
if (isModulusBinary(mod)) {
6674
this.modulus = bytesToLimbs(modBytes)
6775
this.mulMod = mulModBinary
@@ -78,6 +86,7 @@ export class FieldContext {
7886
return
7987
}
8088

89+
console.log(modBytes)
8190
if (modBytes.at(-1)! % 2 === 0) {
8291
throw new Error('modulus cannot be even')
8392
}
@@ -133,18 +142,29 @@ export class FieldContext {
133142
const dstIdx = dst * elemSize + i * elemSize
134143
const srcBytes = from.slice(srcIdx, srcIdx + elemSize * 8)
135144
const val = bytesToLimbs(srcBytes)
145+
console.log('dbg700')
146+
console.log(count)
147+
console.log(elemSize)
148+
console.log(srcBytes)
149+
console.log(bytesToHex(srcBytes))
150+
console.log(val)
151+
console.log(this.modulus)
152+
console.log(this.scratchSpace)
136153
if (!lt(val, this.modulus)) throw new Error(`value being stored must be less than modulus`)
137154
if (this.useMontgomeryRepr) {
155+
// console.log('dbg1000')
138156
const tmp = this.scratchSpace.slice(dstIdx + elemSize)
139157
this.mulMod(tmp, val, this.r2, this.modulus, this.modInvVal)
140158
for (let i = 0; i < elemSize; i++) {
141159
this.scratchSpace[dstIdx + i] = tmp[i]
142160
}
143161
} else {
162+
// console.log('dbg1001')
144163
for (let i = 0; i < elemSize; i++) {
145164
this.scratchSpace[dstIdx + i] = val[i]
146165
}
147166
}
167+
// console.log(this.scratchSpace)
148168
}
149169
}
150170

@@ -176,13 +196,21 @@ export class FieldContext {
176196
}
177197
}
178198

199+
console.log('dbg950')
200+
console.log(res)
201+
console.log(dst)
202+
console.log(dstIdx)
203+
console.log(elemSize)
179204
// Write res[] into 'dst'
180205
for (let i = 0; i < elemSize; i++) {
181206
const limb = res[elemSize - 1 - i]
207+
console.log(limb)
182208
putUint64BE(dst, dstIdx + i * 8, limb)
209+
console.log('dbg951')
183210
}
184211
dstIdx += elemSize * 8
185212
}
213+
console.log(dst)
186214
}
187215

188216
/**

packages/evm/src/evmmax/util.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,25 @@ export const MASK_64 = (1n << 64n) - 1n
1010
* @param value bigint whose lower 64 bits are to be interpreted as big endian bytes and put into destination from offset
1111
*/
1212
export function putUint64BE(dst: Uint8Array, offset: number, value: bigint): void {
13+
console.log('dbg970')
1314
value = BigInt.asUintN(64, value)
1415
const hex = value.toString(16).padStart(16, '0')
16+
console.log(value)
17+
console.log(dst)
18+
console.log(offset)
19+
console.log(hex)
20+
let j = 0
1521
for (let i = 0; i < 8; i++) {
16-
dst[offset + i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16)
22+
const byteVal = parseInt(hex.slice(i * 2, i * 2 + 2), 16)
23+
24+
if (byteVal === 0) {
25+
continue
26+
} else {
27+
dst[offset + j] = byteVal
28+
j++
29+
}
30+
console.log(byteVal)
31+
console.log(dst)
1732
}
1833
}
1934

@@ -315,15 +330,15 @@ export function extractEVMMAXImmediateInputs(pc: number, code: Uint8Array) {
315330
const yStride = code[pc + 6]
316331
const count = code[pc + 7]
317332

318-
console.log('dbg600')
319-
console.log(pc)
320-
console.log(out)
321-
console.log(outStride)
322-
console.log(x)
323-
console.log(xStride)
324-
console.log(y)
325-
console.log(yStride)
326-
console.log(count)
333+
// console.log('dbg600')
334+
// console.log(pc)
335+
// console.log(out)
336+
// console.log(outStride)
337+
// console.log(x)
338+
// console.log(xStride)
339+
// console.log(y)
340+
// console.log(yStride)
341+
// console.log(count)
327342

328343
return [out, outStride, x, xStride, y, yStride, count]
329344
}

packages/evm/src/opcodes/functions.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -996,26 +996,33 @@ 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)
999+
console.log('dbg600')
1000+
console.log(modulus)
10011001
runState.evmmaxState.allocAndSetActive(Number(id), modulus, allocCount)
10021002
},
10031003
],
10041004
// 0xc1: LOADX
10051005
[
10061006
0xc1,
10071007
function (runState, _common) {
1008-
console.log('dbg900')
10091008
const [dest, source, count] = runState.stack.popN(3)
1010-
const copySize = Number(count) * runState.evmmaxState.getActive()?.getElemSize()
1009+
const copySize =
1010+
Number(count) *
1011+
runState.evmmaxState.getActive()?.getElemSize() *
1012+
runState.evmmaxState.getActive()?.modByteSize
10111013
const destBuf = new Uint8Array(copySize)
10121014
runState.evmmaxState.getActive()?.load(destBuf, Number(source), Number(count))
1015+
console.log('dbg900')
1016+
console.log(source)
1017+
console.log(runState.evmmaxState.getActive().scratchSpace)
10131018
console.log(copySize)
10141019
console.log(count)
1015-
console.log(runState.evmmaxState.getActive()?.getElemSize())
10161020
console.log(destBuf)
10171021
console.log(runState.memory._store)
1022+
1023+
// runState.memory.write(Number(dest), copySize, destBuf)
10181024
runState.memory.write(Number(dest), copySize, destBuf)
1025+
10191026
console.log(runState.memory._store)
10201027
},
10211028
],
@@ -1025,9 +1032,18 @@ export const handlers: Map<number, OpHandler> = new Map([
10251032
function (runState, _common) {
10261033
// TODO figure out if we need to use extend(), _store(), or or just write()
10271034
const [dest, source, count] = runState.stack.popN(3)
1028-
const copySize = Number(count) * runState.evmmaxState.getActive()?.getElemSize()
1035+
const copySize =
1036+
Number(count) *
1037+
runState.evmmaxState.getActive()?.getElemSize() *
1038+
runState.evmmaxState.getActive()?.modByteSize
1039+
console.log('dbg400')
1040+
// console.log(runState.memory._store)
1041+
// console.log(copySize)
1042+
console.log(runState.evmmaxState.getActive().scratchSpace)
1043+
10291044
const srcBuf = runState.memory.read(Number(source), Number(count) * copySize)
10301045
runState.evmmaxState.getActive()?.store(Number(dest), Number(count), srcBuf)
1046+
console.log(runState.evmmaxState.getActive().scratchSpace)
10311047
},
10321048
],
10331049
// 0xc3: ADDMODX
@@ -1039,7 +1055,9 @@ export const handlers: Map<number, OpHandler> = new Map([
10391055
runState.code,
10401056
)
10411057
runState.programCounter += 7
1058+
console.log(runState.evmmaxState.getActive().scratchSpace)
10421059
runState.evmmaxState.getActive().addM(out, outStride, x, xStride, y, yStride, count)
1060+
console.log(runState.evmmaxState.getActive().scratchSpace)
10431061
},
10441062
],
10451063
// 0xc4: SUBMODX
@@ -1051,7 +1069,9 @@ export const handlers: Map<number, OpHandler> = new Map([
10511069
runState.code,
10521070
)
10531071
runState.programCounter += 7
1072+
console.log(runState.evmmaxState.getActive().scratchSpace)
10541073
runState.evmmaxState.getActive().subM(out, outStride, x, xStride, y, yStride, count)
1074+
console.log(runState.evmmaxState.getActive().scratchSpace)
10551075
},
10561076
],
10571077
// 0xc5: MULMODX
@@ -1063,7 +1083,11 @@ export const handlers: Map<number, OpHandler> = new Map([
10631083
runState.code,
10641084
)
10651085
runState.programCounter += 7
1086+
1087+
console.log('dbg1100')
1088+
console.log(runState.evmmaxState.getActive().scratchSpace)
10661089
runState.evmmaxState.getActive().mulM(out, outStride, x, xStride, y, yStride, count)
1090+
console.log(runState.evmmaxState.getActive().scratchSpace)
10671091
},
10681092
],
10691093
// 0xd0: DATALOAD

packages/evm/src/opcodes/gas.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
825825
/* LOADX */
826826
0xc1,
827827
async function (runState, gas, common): Promise<bigint> {
828+
// console.log('dbg1000')
828829
const [dst, src, count] = runState.stack.peek(3)
829830

830831
if (!isUint64(src) || src >= runState.evmmaxState.getActive().getNumElems()) {
@@ -841,25 +842,42 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
841842
trap('destination of copy out of bounds')
842843
}
843844

844-
const [loadSize, overflow2] = mul64(
845+
const [overflow2, loadSize] = mul64(
845846
count,
846847
BigInt(runState.evmmaxState.getActive().getElemSize()),
847848
)
849+
850+
// console.log('dbg1001')
851+
// console.log(count)
852+
// console.log(BigInt(runState.evmmaxState.getActive().getElemSize()))
853+
// console.log(loadSize)
854+
// console.log(overflow2)
855+
848856
if (overflow2 !== 0n) {
849857
trap('overflow')
850858
}
859+
851860
const [last2, overflow3] = add64(dst, loadSize, 0n)
852-
if (overflow3 !== 0n || last2 > runState.memoryWordCount) {
861+
862+
// console.log('dbg1002')
863+
// console.log(dst)
864+
// console.log(loadSize)
865+
// console.log(last2)
866+
// console.log(overflow3)
867+
868+
if (overflow3 !== 0n || last2 > runState.memory._store.length) {
853869
trap('out of bounds destination')
854870
}
855871

856872
if (runState.evmmaxState.getActive().isModulusBinary) {
857873
return gas + loadSize * common.param('copyGas') // TODO check if this translates from go: toWordSize(storeSize) * params.copyGas
858874
} else {
875+
// console.log('dbg1500')
876+
// console.log(Number(runState.evmmaxState.getActive().getElemSize() / 8) - 1)
877+
// console.log(MULMODX_COST[Number(runState.evmmaxState.getActive().getElemSize() / 8) - 1])
859878
return (
860879
gas +
861-
count *
862-
BigInt(MULMODX_COST[Number(runState.evmmaxState.getActive().getElemSize() / 8) - 1])
880+
count * BigInt(MULMODX_COST[Number(runState.evmmaxState.getActive().getElemSize()) - 1])
863881
)
864882
}
865883
},

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import { randomBytes } from 'crypto'
22
import { bigIntToBytes, bytesToBigInt } from '@ethereumjs/util'
33
import { assert, describe, it } from 'vitest'
44

5-
import { FieldContext } from '../../src/index.js'
5+
import { FieldContext } from '../../src/index.ts'
66

7-
function padBigIntBytes(val: bigint, byteLen: number): Uint8Array {
7+
export function padBigIntBytes(val: bigint, byteLen: number): Uint8Array {
88
const raw = bigIntToBytes(val)
99
if (raw.length === byteLen) return raw
1010
const out = new Uint8Array(byteLen)
1111
out.set(raw, byteLen - raw.length)
1212
return out
1313
}
1414

15-
function randomBigInt(size: number, limit: bigint): bigint {
15+
export function randomBigInt(size: number, limit: bigint): bigint {
1616
return bytesToBigInt(randomBytes(size)) % limit
1717
}
1818

19-
function randomBinaryModulus(size: number): bigint {
20-
return 1n << BigInt(size * 8)
19+
export function randomBinaryModulus(size: number): bigint {
20+
return 1n << BigInt(size * 8 - 1)
2121
}
2222

2323
export function randomOddModulus(size: number): bigint {

0 commit comments

Comments
 (0)