From 05b70b00b502744f6653f48809e286032bff8534 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Tue, 19 Dec 2023 00:28:36 +0100 Subject: [PATCH] evm: ensure modexp right-pads input data --- packages/evm/src/precompiles/05-modexp.ts | 2 +- .../evm/test/precompiles/05-modexp.spec.ts | 37 +++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/evm/src/precompiles/05-modexp.ts b/packages/evm/src/precompiles/05-modexp.ts index c291b9aa7e..85a15572ba 100644 --- a/packages/evm/src/precompiles/05-modexp.ts +++ b/packages/evm/src/precompiles/05-modexp.ts @@ -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) { diff --git a/packages/evm/test/precompiles/05-modexp.spec.ts b/packages/evm/test/precompiles/05-modexp.spec.ts index 36065ba170..10cea3a801 100644 --- a/packages/evm/test/precompiles/05-modexp.spec.ts +++ b/packages/evm/test/precompiles/05-modexp.spec.ts @@ -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) + }) })