Skip to content

Commit

Permalink
Add custom keccak256 usage to EVM keccak256 opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Dec 18, 2023
1 parent 3c3c85f commit 159abd1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/common/test/customCrypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('[Common]: Custom Crypto', () => {
let msg = 'Should initialize with custom keccak256 function and use properly (main constructor)'
assert.deepEqual(c.customCrypto.keccak256!(value), new Uint8Array([2, 1]), msg)

msg = 'Should still work on a copied instance'
assert.deepEqual(c.copy().customCrypto.keccak256!(value), new Uint8Array([2, 1]), msg)

const customChainParams = { name: 'custom', chainId: 123, networkId: 678 }
c = Common.custom(customChainParams, { customCrypto })
msg = 'Should initialize with custom keccak256 function and use properly (custom() constructor)'
Expand Down
4 changes: 2 additions & 2 deletions packages/evm/src/opcodes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,13 @@ export const handlers: Map<number, OpHandler> = new Map([
// 0x20: KECCAK256
[
0x20,
function (runState) {
function (runState, common) {
const [offset, length] = runState.stack.popN(2)
let data = new Uint8Array(0)
if (length !== BIGINT_0) {
data = runState.memory.read(Number(offset), Number(length))
}
const r = BigInt(bytesToHex(keccak256(data)))
const r = BigInt(bytesToHex((common.customCrypto.keccak256 ?? keccak256)(data)))

Check warning on line 408 in packages/evm/src/opcodes/functions.ts

View check run for this annotation

Codecov / codecov/patch

packages/evm/src/opcodes/functions.ts#L408

Added line #L408 was not covered by tests
runState.stack.push(r)
},
],
Expand Down

0 comments on commit 159abd1

Please sign in to comment.