Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVM: Precompile Debug Logger Improvements #2572

Merged
merged 5 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/evm/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type { Account } from '@ethereumjs/util'

const debug = createDebugLogger('evm:evm')
const debugGas = createDebugLogger('evm:gas')
const debugPrecompiles = createDebugLogger('evm:precompiles')

// very ugly way to detect if we are running in a browser
const isBrowser = new Function('try {return this===window;}catch(e){ return false;}')
Expand Down Expand Up @@ -386,9 +387,6 @@ export class EVM implements EVMInterface {

let result: ExecResult
if (message.isCompiled) {
if (this.DEBUG) {
debug(`Run precompile`)
}
result = await this.runPrecompile(
message.code as PrecompileFunc,
message.data,
Expand Down Expand Up @@ -864,6 +862,7 @@ export class EVM implements EVMInterface {
gasLimit,
_common: this._common,
_EVM: this,
_debug: this.DEBUG ? debugPrecompiles : undefined,
}

return code(opts)
Expand Down
31 changes: 29 additions & 2 deletions packages/evm/src/precompiles/01-ecrecover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
publicToAddress,
setLengthLeft,
setLengthRight,
short,
} from '@ethereumjs/util'

import { OOGResult } from '../evm'
Expand All @@ -13,8 +14,18 @@ import type { PrecompileInput } from './types'

export function precompile01(opts: PrecompileInput): ExecResult {
const gasUsed = opts._common.param('gasPrices', 'ecRecover')
if (opts._debug) {
opts._debug(
`Run ECRECOVER (0x01) precompile data=${short(opts.data)} length=${
opts.data.length
} gasLimit=${opts.gasLimit} gasUsed=${gasUsed}`
)
}

if (opts.gasLimit < gasUsed) {
if (opts._debug) {
opts._debug(`ECRECOVER (0x01) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

Expand All @@ -28,6 +39,9 @@ export function precompile01(opts: PrecompileInput): ExecResult {
// a signature in most of the cases in the cases that `v=0` or `v=1`
// However, this should throw, only 27 and 28 is allowed as input
if (vBigInt !== BigInt(27) && vBigInt !== BigInt(28)) {
if (opts._debug) {
opts._debug(`ECRECOVER (0x01) failed: v neither 27 nor 28`)
}
return {
executionGasUsed: gasUsed,
returnValue: Buffer.alloc(0),
Expand All @@ -39,16 +53,29 @@ export function precompile01(opts: PrecompileInput): ExecResult {

let publicKey
try {
if (opts._debug) {
opts._debug(
`ECRECOVER (0x01): PK recovery with msgHash=${msgHash.toString('hex')} v=${v.toString(
'hex'
)} r=${r.toString('hex')}s=${s.toString('hex')}}`
)
}
publicKey = ecrecover(msgHash, bufferToBigInt(v), r, s)
} catch (e: any) {
if (opts._debug) {
opts._debug(`ECRECOVER (0x01) failed: PK recovery failed`)
}
return {
executionGasUsed: gasUsed,
returnValue: Buffer.alloc(0),
}
}

const address = setLengthLeft(publicToAddress(publicKey), 32)
if (opts._debug) {
opts._debug(`ECRECOVER (0x01) return address=${address.toString('hex')}`)
}
return {
executionGasUsed: gasUsed,
returnValue: setLengthLeft(publicToAddress(publicKey), 32),
returnValue: address,
}
}
20 changes: 18 additions & 2 deletions packages/evm/src/precompiles/02-sha256.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toBuffer } from '@ethereumjs/util'
import { short, toBuffer } from '@ethereumjs/util'
import { sha256 } from 'ethereum-cryptography/sha256'

import { OOGResult } from '../evm'
Expand All @@ -12,12 +12,28 @@ export function precompile02(opts: PrecompileInput): ExecResult {
let gasUsed = opts._common.param('gasPrices', 'sha256')
gasUsed += opts._common.param('gasPrices', 'sha256Word') * BigInt(Math.ceil(data.length / 32))

if (opts._debug) {
opts._debug(
`Run KECCAK256 (0x02) precompile data=${short(opts.data)} length=${
opts.data.length
} gasLimit=${opts.gasLimit} gasUsed=${gasUsed}`
)
}

if (opts.gasLimit < gasUsed) {
if (opts._debug) {
opts._debug(`KECCAK256 (0x02) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

const hash = toBuffer(sha256(data))
if (opts._debug) {
opts._debug(`KECCAK256 (0x02) return hash=${hash.toString('hex')}`)
}

return {
executionGasUsed: gasUsed,
returnValue: toBuffer(sha256(data)),
returnValue: hash,
}
}
20 changes: 18 additions & 2 deletions packages/evm/src/precompiles/03-ripemd160.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setLengthLeft, toBuffer } from '@ethereumjs/util'
import { setLengthLeft, short, toBuffer } from '@ethereumjs/util'
import { ripemd160 } from 'ethereum-cryptography/ripemd160'

import { OOGResult } from '../evm'
Expand All @@ -12,12 +12,28 @@ export function precompile03(opts: PrecompileInput): ExecResult {
let gasUsed = opts._common.param('gasPrices', 'ripemd160')
gasUsed += opts._common.param('gasPrices', 'ripemd160Word') * BigInt(Math.ceil(data.length / 32))

if (opts._debug) {
opts._debug(
`Run RIPEMD160 (0x03) precompile data=${short(opts.data)} length=${
opts.data.length
} gasLimit=${opts.gasLimit} gasUsed=${gasUsed}`
)
}

if (opts.gasLimit < gasUsed) {
if (opts._debug) {
opts._debug(`RIPEMD160 (0x03) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

const hash = setLengthLeft(toBuffer(ripemd160(data)), 32)
if (opts._debug) {
opts._debug(`RIPEMD160 (0x03) return hash=${hash.toString('hex')}`)
}

return {
executionGasUsed: gasUsed,
returnValue: setLengthLeft(toBuffer(ripemd160(data)), 32),
returnValue: hash,
}
}
16 changes: 16 additions & 0 deletions packages/evm/src/precompiles/04-identity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { short } from '@ethereumjs/util'

import { OOGResult } from '../evm'

import type { ExecResult } from '../evm'
Expand All @@ -8,11 +10,25 @@ export function precompile04(opts: PrecompileInput): ExecResult {

let gasUsed = opts._common.param('gasPrices', 'identity')
gasUsed += opts._common.param('gasPrices', 'identityWord') * BigInt(Math.ceil(data.length / 32))
if (opts._debug) {
opts._debug(
`Run IDENTITY (0x04) precompile data=${short(opts.data)} length=${
opts.data.length
} gasLimit=${opts.gasLimit} gasUsed=${gasUsed}`
)
}

if (opts.gasLimit < gasUsed) {
if (opts._debug) {
opts._debug(`IDENTITY (0x04) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

if (opts._debug) {
opts._debug(`IDENTITY (0x04) return data=${short(opts.data)}`)
}

return {
executionGasUsed: gasUsed,
returnValue: data,
Expand Down
31 changes: 29 additions & 2 deletions packages/evm/src/precompiles/05-modexp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { bigIntToBuffer, bufferToBigInt, setLengthLeft, setLengthRight } from '@ethereumjs/util'
import {
bigIntToBuffer,
bufferToBigInt,
setLengthLeft,
setLengthRight,
short,
} from '@ethereumjs/util'

import { OOGResult } from '../evm'

Expand Down Expand Up @@ -110,8 +116,18 @@ export function precompile05(opts: PrecompileInput): ExecResult {
gasUsed = BigInt(200)
}
}
if (opts._debug) {
opts._debug(
`Run MODEXP (0x05) precompile data=${short(opts.data)} length=${opts.data.length} gasLimit=${
opts.gasLimit
} gasUsed=${gasUsed}`
)
}

if (opts.gasLimit < gasUsed) {
if (opts._debug) {
opts._debug(`MODEXP (0x05) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

Expand All @@ -133,6 +149,9 @@ export function precompile05(opts: PrecompileInput): ExecResult {
const maxSize = BigInt(2147483647) // @ethereumjs/util setLengthRight limitation

if (bLen > maxSize || eLen > maxSize || mLen > maxSize) {
if (opts._debug) {
opts._debug(`MODEXP (0x05) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

Expand All @@ -141,6 +160,9 @@ export function precompile05(opts: PrecompileInput): ExecResult {
const M = bufferToBigInt(setLengthRight(data.slice(Number(mStart), Number(mEnd)), Number(mLen)))

if (mEnd > maxInt) {
if (opts._debug) {
opts._debug(`MODEXP (0x05) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

Expand All @@ -151,8 +173,13 @@ export function precompile05(opts: PrecompileInput): ExecResult {
R = expmod(B, E, M)
}

const res = setLengthLeft(bigIntToBuffer(R), Number(mLen))
if (opts._debug) {
opts._debug(`MODEXP (0x05) return value=${res.toString('hex')}`)
}

return {
executionGasUsed: gasUsed,
returnValue: setLengthLeft(bigIntToBuffer(R), Number(mLen)),
returnValue: res,
}
}
21 changes: 20 additions & 1 deletion packages/evm/src/precompiles/06-ecadd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { short } from '@ethereumjs/util'

import { OOGResult } from '../evm'

import type { ExecResult } from '../evm'
Expand All @@ -9,17 +11,34 @@ export function precompile06(opts: PrecompileInput): ExecResult {
const inputData = opts.data.slice(0, 128)

const gasUsed = opts._common.param('gasPrices', 'ecAdd')
if (opts._debug) {
opts._debug(
`Run ECADD (0x06) precompile data=${short(opts.data)} length=${opts.data.length} gasLimit=${
opts.gasLimit
} gasUsed=${gasUsed}`
)
}
if (opts.gasLimit < gasUsed) {
if (opts._debug) {
opts._debug(`ECADD (0x06) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

const returnData = bn128.add(inputData)
const returnData: Buffer = bn128.add(inputData)

// check ecadd success or failure by comparing the output length
if (returnData.length !== 64) {
if (opts._debug) {
opts._debug(`ECADD (0x06) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

if (opts._debug) {
opts._debug(`ECADD (0x06) return value=${returnData.toString('hex')}`)
}

return {
executionGasUsed: gasUsed,
returnValue: returnData,
Expand Down
20 changes: 20 additions & 0 deletions packages/evm/src/precompiles/07-ecmul.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { short } from '@ethereumjs/util'

import { OOGResult } from '../evm'

import type { ExecResult } from '../evm'
Expand All @@ -8,17 +10,35 @@ const bn128 = require('rustbn.js')
export function precompile07(opts: PrecompileInput): ExecResult {
const inputData = opts.data.slice(0, 128)
const gasUsed = opts._common.param('gasPrices', 'ecMul')
if (opts._debug) {
opts._debug(
`Run ECMUL (0x07) precompile data=${short(opts.data)} length=${opts.data.length} gasLimit=${
opts.gasLimit
} gasUsed=${gasUsed}`
)
}

if (opts.gasLimit < gasUsed) {
if (opts._debug) {
opts._debug(`ECMUL (0x07) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

const returnData = bn128.mul(inputData)
// check ecmul success or failure by comparing the output length
if (returnData.length !== 64) {
if (opts._debug) {
opts._debug(`ECMUL (0x07) failed: OOG`)
}
// TODO: should this really return OOG?
holgerd77 marked this conversation as resolved.
Show resolved Hide resolved
return OOGResult(opts.gasLimit)
}

if (opts._debug) {
opts._debug(`ECMUL (0x07) return value=${returnData.toString('hex')}`)
}

return {
executionGasUsed: gasUsed,
returnValue: returnData,
Expand Down
20 changes: 20 additions & 0 deletions packages/evm/src/precompiles/08-ecpairing.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { short } from '@ethereumjs/util'

import { OOGResult } from '../evm'

import type { ExecResult } from '../evm'
Expand All @@ -12,18 +14,36 @@ export function precompile08(opts: PrecompileInput): ExecResult {
const gasUsed =
opts._common.param('gasPrices', 'ecPairing') +
inputDataSize * opts._common.param('gasPrices', 'ecPairingWord')
if (opts._debug) {
opts._debug(
`Run ECPAIRING (0x08) precompile data=${short(opts.data)} length=${
opts.data.length
} gasLimit=${opts.gasLimit} gasUsed=${gasUsed}`
)
}

if (opts.gasLimit < gasUsed) {
if (opts._debug) {
opts._debug(`ECPAIRING (0x08) failed: OOG`)
}
return OOGResult(opts.gasLimit)
}

const returnData = bn128.pairing(inputData)

// check ecpairing success or failure by comparing the output length
if (returnData.length !== 32) {
if (opts._debug) {
opts._debug(`ECPAIRING (0x08) failed: OOG`)
}
// TODO: should this really return OOG?
holgerd77 marked this conversation as resolved.
Show resolved Hide resolved
return OOGResult(opts.gasLimit)
}

if (opts._debug) {
opts._debug(`ECPAIRING (0x08) return value=${returnData.toString('hex')}`)
}

return {
executionGasUsed: gasUsed,
returnValue: returnData,
Expand Down
Loading