@@ -2,6 +2,7 @@ import { Common, Mainnet } from '@ethereumjs/common'
2
2
import { assert , describe , it } from 'vitest'
3
3
4
4
import { type PrefixedHexString , createAddressFromString , hexToBytes } from '@ethereumjs/util'
5
+ import { bigIntToBytes } from '@ethereumjs/util'
5
6
import { LOADX_BASE_COST , SETMODX_BASE_COST , STOREX_BASE_COST } from '../../src/evmmax/index.ts'
6
7
import { createEVM } from '../../src/index.ts'
7
8
@@ -31,7 +32,7 @@ function ret(index: number, size: number) {
31
32
return size + index + RETURN
32
33
}
33
34
34
- function setupx ( id : number , mod_size : number , mod_offset : number , alloc_count : number ) {
35
+ function setupx ( id : number , mod_offset : number , mod_size : number , alloc_count : number ) {
35
36
return (
36
37
PUSH1 +
37
38
numToOpcode ( alloc_count ) +
@@ -120,8 +121,66 @@ function mulmodx(
120
121
)
121
122
}
122
123
123
- describe ( 'EVM -> getActiveOpcodes()' , ( ) => {
124
- it ( 'should not expose opcodes from a follow-up HF (istanbul -> petersburg)' , async ( ) => {
124
+ describe ( 'should be able to perform modular arithmetic with evmmax opcodes' , ( ) => {
125
+ // it('1-byte modulus test', async () => {
126
+ // const common = new Common({
127
+ // chain: Mainnet,
128
+ // eips: [6690],
129
+ // params: {
130
+ // 6690: {
131
+ // setmodxGas: SETMODX_BASE_COST,
132
+ // loadxGas: LOADX_BASE_COST,
133
+ // storexGas: STOREX_BASE_COST,
134
+ // addmodxGas: 0,
135
+ // submodxGas: 0,
136
+ // mulmodxGas: 0,
137
+ // },
138
+ // },
139
+ // })
140
+ // const evm = await createEVM({ common })
141
+ // evm.events.on('step', (e) => {
142
+ // console.log(e.opcode.name)
143
+ // })
144
+
145
+ // const modulus = 8n
146
+ // const modBytes = bigIntToBytes(modulus)
147
+
148
+ // console.log('dbg100')
149
+ // console.log(addmodx(0, 1, 1, 1, 2, 1, 1))
150
+ // console.log(modBytes)
151
+ // // create bytecode
152
+ // const bytecode =
153
+ // '0x' +
154
+ // mstore8(0, 8) + // store value 0x08 at index 0 in memory
155
+ // setupx(0, 1, 0, 3) +
156
+ // mstore8(1, 3) +
157
+ // mstore8(2, 6) +
158
+ // storex(1, 1, 1) +
159
+ // storex(2, 2, 1) +
160
+ // addmodx(0, 1, 1, 1, 2, 1, 1) +
161
+ // mulmodx(0, 1, 1, 1, 2, 1, 1) +
162
+ // submodx(0, 1, 1, 1, 2, 1, 1) +
163
+ // loadx(0, 0, 1)
164
+ // // + ret(96, 32)
165
+
166
+ // const ADDR_TO_CALL = createAddressFromString('0x' + '20'.repeat(20))
167
+
168
+ // await evm.stateManager.putCode(ADDR_TO_CALL, hexToBytes(bytecode as PrefixedHexString))
169
+
170
+ // const result = await evm.runCall({
171
+ // to: ADDR_TO_CALL,
172
+ // gasLimit: BigInt(0xffffff),
173
+ // })
174
+
175
+ // // const result1 = await evm.runCall({
176
+ // // data: hexToBytes(bytecode as PrefixedHexString),
177
+ // // gasLimit: BigInt(0xffffff),
178
+ // // })
179
+
180
+ // console.log(result)
181
+ // })
182
+
183
+ it ( '2-byte modulus test' , async ( ) => {
125
184
const common = new Common ( {
126
185
chain : Mainnet ,
127
186
eips : [ 6690 ] ,
@@ -137,19 +196,24 @@ describe('EVM -> getActiveOpcodes()', () => {
137
196
} ,
138
197
} )
139
198
const evm = await createEVM ( { common } )
140
- evm . events . on ( 'step' , ( e ) => {
141
- console . log ( e . opcode . name )
142
- } )
199
+ const modulus = 500n
200
+ const modBytes = bigIntToBytes ( modulus )
201
+
202
+ // const elemByteLen = Number(fieldCtx.elemSize)
203
+ // const xBytes = padBigIntBytes(xInt, elemByteLen * 8)
204
+ // const yBytes = padBigIntBytes(yInt, elemByteLen * 8)
205
+ // const outBytes = new Uint8Array(elemByteLen * 8)
143
206
144
207
console . log ( 'dbg100' )
145
- console . log ( addmodx ( 0 , 1 , 1 , 1 , 2 , 1 , 1 ) )
208
+ console . log ( modBytes )
146
209
// create bytecode
147
210
const bytecode =
148
211
'0x' +
149
- mstore8 ( 0 , 8 ) + // store value 0x08 at index 0 in memory
150
- setupx ( 0 , 1 , 0 , 3 ) +
151
- mstore8 ( 1 , 3 ) +
152
- mstore8 ( 2 , 6 ) +
212
+ mstore8 ( 0 , modBytes [ 1 ] ) +
213
+ mstore8 ( 1 , modBytes [ 0 ] ) +
214
+ setupx ( 0 , 0 , 2 , 3 ) +
215
+ mstore8 ( 2 , 220 ) +
216
+ mstore8 ( 4 , 230 ) +
153
217
storex ( 1 , 1 , 1 ) +
154
218
storex ( 2 , 2 , 1 ) +
155
219
addmodx ( 0 , 1 , 1 , 1 , 2 , 1 , 1 ) +
0 commit comments