Skip to content

Commit

Permalink
evm: ensure modexp right-pads input data
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer authored and holgerd77 committed Dec 19, 2023
1 parent c5054eb commit 05b70b0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/evm/src/precompiles/05-modexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function expmod(a: bigint, power: bigint, modulo: bigint) {
}

export function precompile05(opts: PrecompileInput): ExecResult {
const data = opts.data
const data = setLengthRight(opts.data, 96)

let adjustedELen = getAdjustedExponentLength(data)
if (adjustedELen < BIGINT_1) {
Expand Down
37 changes: 25 additions & 12 deletions packages/evm/test/precompiles/05-modexp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,31 @@ describe('Precompiles: MODEXP', () => {
const addressStr = '0000000000000000000000000000000000000005'
const MODEXP = getActivePrecompiles(common).get(addressStr)!

let n = 0
for (const [input, expect] of fuzzerTests) {
n++
it(`MODEXP edge cases (issue 3168) - case ${n}`, async () => {
const result = await MODEXP({
data: hexToBytes(input),
gasLimit: BigInt(0xffff),
common,
_EVM: evm,
it('should run testdata', async () => {
let n = 0
for (const [input, expect] of fuzzerTests) {
n++
it(`MODEXP edge cases (issue 3168) - case ${n}`, async () => {
const result = await MODEXP({
data: hexToBytes(input),
gasLimit: BigInt(0xffff),
common,
_EVM: evm,
})
const oput = bytesToHex(result.returnValue)
assert.equal(oput, expect)
})
const oput = bytesToHex(result.returnValue)
assert.equal(oput, expect)
}
})

it.only('should correctly right-pad data if input length is too short', async () => {
const gas = BigInt(0xffff)
const result = await MODEXP({
data: hexToBytes('0x41'),
gasLimit: gas,
common,
_EVM: evm,
})
}
assert.ok(result.executionGasUsed === gas)
})
})

0 comments on commit 05b70b0

Please sign in to comment.