Skip to content

Commit 4da6647

Browse files
authored
Several EVM Precompile Clean-Ups & Renamings (#3655)
* Rename EVM bn254 precompiles (EC based name too generic, not consistent with bls12) * Additional EC* precompile renaming * Small fix * Bring precompileEntries array in the correct order * Even more ordering * Some precompile name DRYing * Undo confused-state hex resorting * Another confusion: keep the SHA256 name * Rename ec*Gas params to bn254*Gas * Lower-case bls12381*Gas param names
1 parent 47051db commit 4da6647

32 files changed

+255
-190
lines changed

packages/common/test/data/paramsTest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const paramsTest: ParamsDict = {
1313
*/
1414
609: {
1515
// gasPrices
16-
ecAddGas: 500, // Gas costs for curve addition precompile
16+
bn254AddGas: 500, // Gas costs for curve addition precompile
1717
// pow
1818
minerReward: '3000000000000000000', // the amount a miner get rewarded for mining a block
1919
},
@@ -38,13 +38,13 @@ export const paramsTest: ParamsDict = {
3838
*/
3939
1679: {
4040
// gasPrices
41-
ecAddGas: 150, // Gas costs for curve addition precompile
41+
bn254AddGas: 150, // Gas costs for curve addition precompile
4242
},
4343
/**
4444
* BLS12-381 precompiles
4545
*/
4646
2537: {
4747
// gasPrices
48-
Bls12381G1AddGas: 500, // Gas cost of a single BLS12-381 G1 addition precompile-call
48+
bls12381G1AddGas: 500, // Gas cost of a single BLS12-381 G1 addition precompile-call
4949
},
5050
}

packages/common/test/params.spec.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,42 @@ describe('[Common]: Parameter instantiation / params option / Updates', () => {
88
it('Param option', () => {
99
const c = new Common({ chain: Mainnet, params: paramsTest })
1010
let msg = 'Should also work with parameters passed with params option'
11-
assert.equal(c.param('ecAddGas'), BigInt(150), msg)
11+
assert.equal(c.param('bn254AddGas'), BigInt(150), msg)
1212

1313
const params = {
1414
1679: {
15-
ecAddGas: 250,
15+
bn254AddGas: 250,
1616
},
1717
}
1818
c.updateParams(params)
1919
msg = 'Should update parameter on updateParams() and properly rebuild cache'
20-
assert.equal(c.param('ecAddGas'), BigInt(250), msg)
20+
assert.equal(c.param('bn254AddGas'), BigInt(250), msg)
2121

2222
c.resetParams(params)
2323
msg = 'Should reset all parameters on resetParams() and properly rebuild cache'
24-
assert.equal(c.param('ecAddGas'), BigInt(250), msg)
24+
assert.equal(c.param('bn254AddGas'), BigInt(250), msg)
2525
assert.throws(() => {
26-
c.param('ecMulGas'), BigInt(250)
26+
c.param('bn254MulGas'), BigInt(250)
2727
})
2828

2929
msg = 'Should not side-manipulate the original params file during updating internally'
30-
assert.equal(paramsTest['1679']['ecAddGas'], 150)
30+
assert.equal(paramsTest['1679']['bn254AddGas'], 150)
3131
})
3232
})
3333

3434
describe('[Common]: Parameter access for param(), paramByHardfork()', () => {
3535
it('Basic usage', () => {
3636
const c = new Common({ chain: Mainnet, params: paramsTest, eips: [2537] })
3737
let msg = 'Should return correct value when HF directly provided'
38-
assert.equal(c.paramByHardfork('ecAddGas', 'byzantium'), BigInt(500), msg)
38+
assert.equal(c.paramByHardfork('bn254AddGas', 'byzantium'), BigInt(500), msg)
3939

4040
msg = 'Should return correct value for HF set in class'
4141
c.setHardfork(Hardfork.Byzantium)
42-
assert.equal(c.param('ecAddGas'), BigInt(500), msg)
42+
assert.equal(c.param('bn254AddGas'), BigInt(500), msg)
4343
c.setHardfork(Hardfork.Istanbul)
44-
assert.equal(c.param('ecAddGas'), BigInt(150), msg)
44+
assert.equal(c.param('bn254AddGas'), BigInt(150), msg)
4545
c.setHardfork(Hardfork.MuirGlacier)
46-
assert.equal(c.param('ecAddGas'), BigInt(150), msg)
46+
assert.equal(c.param('bn254AddGas'), BigInt(150), msg)
4747

4848
assert.throws(() => {
4949
c.paramByHardfork('notExistingValue', 'byzantium')
@@ -55,7 +55,7 @@ describe('[Common]: Parameter access for param(), paramByHardfork()', () => {
5555
// To run please manually add an "ecAdd" entry with value 12345 to EIP2537 config
5656
// and uncomment the test
5757
msg = 'EIP config should take precedence over HF config'
58-
assert.equal(c.param('ecAddGas'), 12345, msg)
58+
assert.equal(c.param('bn254AddGas'), 12345, msg)
5959
*/
6060
})
6161

@@ -64,7 +64,7 @@ describe('[Common]: Parameter access for param(), paramByHardfork()', () => {
6464

6565
c.setHardfork(Hardfork.Byzantium)
6666
assert.equal(
67-
c.param('ecAddGas'),
67+
c.param('bn254AddGas'),
6868
BigInt(500),
6969
'Should return correct value for HF set in class',
7070
)
@@ -123,12 +123,12 @@ describe('[Common]: Parameter access for param(), paramByHardfork()', () => {
123123

124124
const UNSUPPORTED_EIP = 1000000
125125
const f = function () {
126-
c.paramByEIP('Bls12381G1AddGas', UNSUPPORTED_EIP)
126+
c.paramByEIP('bls12381G1AddGas', UNSUPPORTED_EIP)
127127
}
128128
let msg = 'Should throw for using paramByEIP() with an unsupported EIP'
129129
assert.throws(f, /not supported$/, undefined, msg)
130130

131-
msg = 'Should return Bls12381G1AddGas gas price for EIP2537'
132-
assert.equal(c.paramByEIP('Bls12381G1AddGas', 2537), BigInt(500), msg)
131+
msg = 'Should return bls12381G1AddGas gas price for EIP2537'
132+
assert.equal(c.paramByEIP('bls12381G1AddGas', 2537), BigInt(500), msg)
133133
})
134134
})

packages/evm/src/params.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ export const paramsEVM: ParamsDict = {
137137
609: {
138138
// gasPrices
139139
modexpGquaddivisorGas: 20, // Gquaddivisor from modexp precompile for gas calculation
140-
ecAddGas: 500, // Gas costs for curve addition precompile
141-
ecMulGas: 40000, // Gas costs for curve multiplication precompile
142-
ecPairingGas: 100000, // Base gas costs for curve pairing precompile
143-
ecPairingWordGas: 80000, // Gas costs regarding curve pairing precompile input length
140+
bn254AddGas: 500, // Gas costs for curve addition precompile
141+
bn254MulGas: 40000, // Gas costs for curve multiplication precompile
142+
bn254PairingGas: 100000, // Base gas costs for curve pairing precompile
143+
bn254PairingWordGas: 80000, // Gas costs regarding curve pairing precompile input length
144144
revertGas: 0, // Base fee of the REVERT opcode
145145
staticcallGas: 700, // Base fee of the STATICCALL opcode
146146
returndatasizeGas: 2, // Base fee of the RETURNDATASIZE opcode
@@ -183,10 +183,10 @@ export const paramsEVM: ParamsDict = {
183183
1679: {
184184
// gasPrices
185185
blake2RoundGas: 1, // Gas cost per round for the Blake2 F precompile
186-
ecAddGas: 150, // Gas costs for curve addition precompile
187-
ecMulGas: 6000, // Gas costs for curve multiplication precompile
188-
ecPairingGas: 45000, // Base gas costs for curve pairing precompile
189-
ecPairingWordGas: 34000, // Gas costs regarding curve pairing precompile input length
186+
bn254AddGas: 150, // Gas costs for curve addition precompile
187+
bn254MulGas: 6000, // Gas costs for curve multiplication precompile
188+
bn254PairingGas: 45000, // Base gas costs for curve pairing precompile
189+
bn254PairingWordGas: 34000, // Gas costs regarding curve pairing precompile input length
190190
sstoreSentryEIP2200Gas: 2300, // Minimum gas required to be present for an SSTORE call, not consumed
191191
sstoreNoopEIP2200Gas: 800, // Once per SSTORE operation if the value doesn't change
192192
sstoreDirtyEIP2200Gas: 800, // Once per SSTORE operation if a dirty value is changed
@@ -234,14 +234,14 @@ export const paramsEVM: ParamsDict = {
234234
*/
235235
2537: {
236236
// gasPrices
237-
Bls12381G1AddGas: 500, // Gas cost of a single BLS12-381 G1 addition precompile-call
238-
Bls12381G1MulGas: 12000, // Gas cost of a single BLS12-381 G1 multiplication precompile-call
239-
Bls12381G2AddGas: 800, // Gas cost of a single BLS12-381 G2 addition precompile-call
240-
Bls12381G2MulGas: 45000, // Gas cost of a single BLS12-381 G2 multiplication precompile-call
241-
Bls12381PairingBaseGas: 65000, // Base gas cost of BLS12-381 pairing check
242-
Bls12381PairingPerPairGas: 43000, // Per-pair gas cost of BLS12-381 pairing check
243-
Bls12381MapG1Gas: 5500, // Gas cost of BLS12-381 map field element to G1
244-
Bls12381MapG2Gas: 75000, // Gas cost of BLS12-381 map field element to G2
237+
bls12381G1AddGas: 500, // Gas cost of a single BLS12-381 G1 addition precompile-call
238+
bls12381G1MulGas: 12000, // Gas cost of a single BLS12-381 G1 multiplication precompile-call
239+
bls12381G2AddGas: 800, // Gas cost of a single BLS12-381 G2 addition precompile-call
240+
bls12381G2MulGas: 45000, // Gas cost of a single BLS12-381 G2 multiplication precompile-call
241+
bls12381PairingBaseGas: 65000, // Base gas cost of BLS12-381 pairing check
242+
bls12381PairingPerPairGas: 43000, // Per-pair gas cost of BLS12-381 pairing check
243+
bls12381MapG1Gas: 5500, // Gas cost of BLS12-381 map field element to G1
244+
bls12381MapG2Gas: 75000, // Gas cost of BLS12-381 map field element to G2
245245
},
246246
/**
247247
. * Gas cost increases for state access opcodes

packages/evm/src/precompiles/01-ecrecover.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ import { OOGResult } from '../evm.js'
1313

1414
import { gasLimitCheck } from './util.js'
1515

16+
import { getPrecompileName } from './index.js'
17+
1618
import type { ExecResult } from '../types.js'
1719
import type { PrecompileInput } from './types.js'
1820

1921
export function precompile01(opts: PrecompileInput): ExecResult {
22+
const pName = getPrecompileName('01')
2023
const ecrecoverFunction = opts.common.customCrypto.ecrecover ?? ecrecover
2124
const gasUsed = opts.common.param('ecRecoverGas')
22-
if (!gasLimitCheck(opts, gasUsed, 'ECRECOVER (0x01)')) {
25+
if (!gasLimitCheck(opts, gasUsed, pName)) {
2326
return OOGResult(opts.gasLimit)
2427
}
2528

@@ -34,7 +37,7 @@ export function precompile01(opts: PrecompileInput): ExecResult {
3437
// However, this should throw, only 27 and 28 is allowed as input
3538
if (vBigInt !== BIGINT_27 && vBigInt !== BIGINT_28) {
3639
if (opts._debug !== undefined) {
37-
opts._debug(`ECRECOVER (0x01) failed: v neither 27 nor 28`)
40+
opts._debug(`${pName} failed: v neither 27 nor 28`)
3841
}
3942
return {
4043
executionGasUsed: gasUsed,
@@ -49,15 +52,15 @@ export function precompile01(opts: PrecompileInput): ExecResult {
4952
try {
5053
if (opts._debug !== undefined) {
5154
opts._debug(
52-
`ECRECOVER (0x01): PK recovery with msgHash=${bytesToHex(msgHash)} v=${bytesToHex(
55+
`${pName}: PK recovery with msgHash=${bytesToHex(msgHash)} v=${bytesToHex(
5356
v,
5457
)} r=${bytesToHex(r)}s=${bytesToHex(s)}}`,
5558
)
5659
}
5760
publicKey = ecrecoverFunction(msgHash, bytesToBigInt(v), r, s)
5861
} catch (e: any) {
5962
if (opts._debug !== undefined) {
60-
opts._debug(`ECRECOVER (0x01) failed: PK recovery failed`)
63+
opts._debug(`${pName} failed: PK recovery failed`)
6164
}
6265
return {
6366
executionGasUsed: gasUsed,
@@ -66,7 +69,7 @@ export function precompile01(opts: PrecompileInput): ExecResult {
6669
}
6770
const address = setLengthLeft(publicToAddress(publicKey), 32)
6871
if (opts._debug !== undefined) {
69-
opts._debug(`ECRECOVER (0x01) return address=${bytesToHex(address)}`)
72+
opts._debug(`${pName} return address=${bytesToHex(address)}`)
7073
}
7174
return {
7275
executionGasUsed: gasUsed,

packages/evm/src/precompiles/02-sha256.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ import { OOGResult } from '../evm.js'
55

66
import { gasLimitCheck } from './util.js'
77

8+
import { getPrecompileName } from './index.js'
9+
810
import type { ExecResult } from '../types.js'
911
import type { PrecompileInput } from './types.js'
1012

1113
export function precompile02(opts: PrecompileInput): ExecResult {
14+
const pName = getPrecompileName('02')
1215
const data = opts.data
1316
const sha256Function = opts.common.customCrypto.sha256 ?? sha256
1417
let gasUsed = opts.common.param('sha256Gas')
1518
gasUsed += opts.common.param('sha256WordGas') * BigInt(Math.ceil(data.length / 32))
1619

17-
if (!gasLimitCheck(opts, gasUsed, 'KECCAK256 (0x02)')) {
20+
if (!gasLimitCheck(opts, gasUsed, pName)) {
1821
return OOGResult(opts.gasLimit)
1922
}
2023

2124
const hash = sha256Function(data)
2225
if (opts._debug !== undefined) {
23-
opts._debug(`KECCAK256 (0x02) return hash=${bytesToHex(hash)}`)
26+
opts._debug(`${pName} return hash=${bytesToHex(hash)}`)
2427
}
2528

2629
return {

packages/evm/src/precompiles/03-ripemd160.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ import { OOGResult } from '../evm.js'
55

66
import { gasLimitCheck } from './util.js'
77

8+
import { getPrecompileName } from './index.js'
9+
810
import type { ExecResult } from '../types.js'
911
import type { PrecompileInput } from './types.js'
1012

1113
export function precompile03(opts: PrecompileInput): ExecResult {
14+
const pName = getPrecompileName('03')
1215
const data = opts.data
1316

1417
let gasUsed = opts.common.param('ripemd160Gas')
1518
gasUsed += opts.common.param('ripemd160WordGas') * BigInt(Math.ceil(data.length / 32))
1619

17-
if (!gasLimitCheck(opts, gasUsed, 'RIPEMD160 (0x03)')) {
20+
if (!gasLimitCheck(opts, gasUsed, pName)) {
1821
return OOGResult(opts.gasLimit)
1922
}
2023

2124
const hash = setLengthLeft(ripemd160(data), 32)
2225
if (opts._debug !== undefined) {
23-
opts._debug(`RIPEMD160 (0x03) return hash=${bytesToHex(hash)}`)
26+
opts._debug(`${pName} return hash=${bytesToHex(hash)}`)
2427
}
2528

2629
return {

packages/evm/src/precompiles/04-identity.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ import { OOGResult } from '../evm.js'
44

55
import { gasLimitCheck } from './util.js'
66

7+
import { getPrecompileName } from './index.js'
8+
79
import type { ExecResult } from '../types.js'
810
import type { PrecompileInput } from './types.js'
911

1012
export function precompile04(opts: PrecompileInput): ExecResult {
13+
const pName = getPrecompileName('04')
1114
const data = opts.data
1215

1316
let gasUsed = opts.common.param('identityGas')
1417
gasUsed += opts.common.param('identityWordGas') * BigInt(Math.ceil(data.length / 32))
15-
if (!gasLimitCheck(opts, gasUsed, 'IDENTITY (0x04)')) {
18+
if (!gasLimitCheck(opts, gasUsed, pName)) {
1619
return OOGResult(opts.gasLimit)
1720
}
1821

1922
if (opts._debug !== undefined) {
20-
opts._debug(`IDENTITY (0x04) return data=${short(opts.data)}`)
23+
opts._debug(`${pName} return data=${short(opts.data)}`)
2124
}
2225

2326
return {

packages/evm/src/precompiles/05-modexp.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { OOGResult } from '../evm.js'
1818

1919
import { gasLimitCheck } from './util.js'
2020

21+
import { getPrecompileName } from './index.js'
22+
2123
import type { ExecResult } from '../types.js'
2224
import type { PrecompileInput } from './types.js'
2325

@@ -104,6 +106,7 @@ export function expMod(a: bigint, power: bigint, modulo: bigint) {
104106
}
105107

106108
export function precompile05(opts: PrecompileInput): ExecResult {
109+
const pName = getPrecompileName('05')
107110
const data = opts.data.length < 96 ? setLengthRight(opts.data, 96) : opts.data
108111

109112
let adjustedELen = getAdjustedExponentLength(data)
@@ -137,7 +140,7 @@ export function precompile05(opts: PrecompileInput): ExecResult {
137140
gasUsed = BIGINT_200
138141
}
139142
}
140-
if (!gasLimitCheck(opts, gasUsed, 'MODEXP (0x05)')) {
143+
if (!gasLimitCheck(opts, gasUsed, pName)) {
141144
return OOGResult(opts.gasLimit)
142145
}
143146

@@ -150,14 +153,14 @@ export function precompile05(opts: PrecompileInput): ExecResult {
150153

151154
if (bLen > maxSize || eLen > maxSize || mLen > maxSize) {
152155
if (opts._debug !== undefined) {
153-
opts._debug(`MODEXP (0x05) failed: OOG`)
156+
opts._debug(`${pName} failed: OOG`)
154157
}
155158
return OOGResult(opts.gasLimit)
156159
}
157160

158161
if (mEnd > maxInt) {
159162
if (opts._debug !== undefined) {
160-
opts._debug(`MODEXP (0x05) failed: OOG`)
163+
opts._debug(`${pName} failed: OOG`)
161164
}
162165
return OOGResult(opts.gasLimit)
163166
}
@@ -180,7 +183,7 @@ export function precompile05(opts: PrecompileInput): ExecResult {
180183

181184
const res = setLengthLeft(R, Number(mLen))
182185
if (opts._debug !== undefined) {
183-
opts._debug(`MODEXP (0x05) return value=${bytesToHex(res)}`)
186+
opts._debug(`${pName} return value=${bytesToHex(res)}`)
184187
}
185188

186189
return {

packages/evm/src/precompiles/06-ecadd.ts renamed to packages/evm/src/precompiles/06-bn254-add.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import { EvmErrorResult, OOGResult } from '../evm.js'
44

55
import { gasLimitCheck } from './util.js'
66

7+
import { getPrecompileName } from './index.js'
8+
79
import type { EVM } from '../evm.js'
810
import type { ExecResult } from '../types.js'
911
import type { PrecompileInput } from './types.js'
1012

1113
export function precompile06(opts: PrecompileInput): ExecResult {
12-
const gasUsed = opts.common.param('ecAddGas')
13-
if (!gasLimitCheck(opts, gasUsed, 'ECADD (0x06)')) {
14+
const pName = getPrecompileName('06')
15+
const gasUsed = opts.common.param('bn254AddGas')
16+
if (!gasLimitCheck(opts, gasUsed, pName)) {
1417
return OOGResult(opts.gasLimit)
1518
}
1619

@@ -23,21 +26,21 @@ export function precompile06(opts: PrecompileInput): ExecResult {
2326
returnData = (opts._EVM as EVM)['_bn254'].add(input)
2427
} catch (e: any) {
2528
if (opts._debug !== undefined) {
26-
opts._debug(`ECADD (0x06) failed: ${e.message}`)
29+
opts._debug(`${pName} failed: ${e.message}`)
2730
}
2831
return EvmErrorResult(e, opts.gasLimit)
2932
}
3033

3134
// check ecadd success or failure by comparing the output length
3235
if (returnData.length !== 64) {
3336
if (opts._debug !== undefined) {
34-
opts._debug(`ECADD (0x06) failed: OOG`)
37+
opts._debug(`${pName} failed: OOG`)
3538
}
3639
return OOGResult(opts.gasLimit)
3740
}
3841

3942
if (opts._debug !== undefined) {
40-
opts._debug(`ECADD (0x06) return value=${bytesToHex(returnData)}`)
43+
opts._debug(`${pName} return value=${bytesToHex(returnData)}`)
4144
}
4245

4346
return {

0 commit comments

Comments
 (0)