Skip to content

Commit beff5fa

Browse files
Shadowfiendkaralabe
authored andcommitted
params, core/vm: Istanbul EIP-1108 bn256 gas cost reduction (#19904)
* params: add IsIstanbul to config + rules IstanbulBlock, used to determine if the config IsIstanbul, is currently left nil until an actual block is chosen. * params, core/vm: implement EIP-1108 Old gas costs for elliptic curve operations are given the PreIstanbul prefix, while current gas costs retain the unprefixed names. The actual precompile implementations are the same, so they are factored out into common functions that are called by the pre-Istanbul and current precompile structs. Finally, an Istanbul precompile list is added that references the new precompile structs, which in turn reference the new gas costs. * params: fix fork ordering, add missing chain compatibility check
1 parent aa6005b commit beff5fa

4 files changed

Lines changed: 144 additions & 45 deletions

File tree

‎core/vm/contracts.go‎

Lines changed: 99 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,22 @@ var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{
5454
common.BytesToAddress([]byte{3}): &ripemd160hash{},
5555
common.BytesToAddress([]byte{4}): &dataCopy{},
5656
common.BytesToAddress([]byte{5}): &bigModExp{},
57-
common.BytesToAddress([]byte{6}): &bn256Add{},
58-
common.BytesToAddress([]byte{7}): &bn256ScalarMul{},
59-
common.BytesToAddress([]byte{8}): &bn256Pairing{},
57+
common.BytesToAddress([]byte{6}): &bn256AddByzantium{},
58+
common.BytesToAddress([]byte{7}): &bn256ScalarMulByzantium{},
59+
common.BytesToAddress([]byte{8}): &bn256PairingByzantium{},
60+
}
61+
62+
// PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
63+
// contracts used in the Istanbul release.
64+
var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
65+
common.BytesToAddress([]byte{1}): &ecrecover{},
66+
common.BytesToAddress([]byte{2}): &sha256hash{},
67+
common.BytesToAddress([]byte{3}): &ripemd160hash{},
68+
common.BytesToAddress([]byte{4}): &dataCopy{},
69+
common.BytesToAddress([]byte{5}): &bigModExp{},
70+
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
71+
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
72+
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
6073
}
6174

6275
// RunPrecompiledContract runs and evaluates the output of a precompiled contract.
@@ -271,15 +284,9 @@ func newTwistPoint(blob []byte) (*bn256.G2, error) {
271284
return p, nil
272285
}
273286

274-
// bn256Add implements a native elliptic curve point addition.
275-
type bn256Add struct{}
276-
277-
// RequiredGas returns the gas required to execute the pre-compiled contract.
278-
func (c *bn256Add) RequiredGas(input []byte) uint64 {
279-
return params.Bn256AddGas
280-
}
281-
282-
func (c *bn256Add) Run(input []byte) ([]byte, error) {
287+
// runBn256Add implements the Bn256Add precompile, referenced by both
288+
// Byzantium and Istanbul operations.
289+
func runBn256Add(input []byte) ([]byte, error) {
283290
x, err := newCurvePoint(getData(input, 0, 64))
284291
if err != nil {
285292
return nil, err
@@ -293,15 +300,35 @@ func (c *bn256Add) Run(input []byte) ([]byte, error) {
293300
return res.Marshal(), nil
294301
}
295302

296-
// bn256ScalarMul implements a native elliptic curve scalar multiplication.
297-
type bn256ScalarMul struct{}
303+
// bn256Add implements a native elliptic curve point addition conforming to
304+
// Istanbul consensus rules.
305+
type bn256AddIstanbul struct{}
306+
307+
// RequiredGas returns the gas required to execute the pre-compiled contract.
308+
func (c *bn256AddIstanbul) RequiredGas(input []byte) uint64 {
309+
return params.Bn256AddGasIstanbul
310+
}
311+
312+
func (c *bn256AddIstanbul) Run(input []byte) ([]byte, error) {
313+
return runBn256Add(input)
314+
}
315+
316+
// bn256AddByzantium implements a native elliptic curve point addition
317+
// conforming to Byzantium consensus rules.
318+
type bn256AddByzantium struct{}
298319

299320
// RequiredGas returns the gas required to execute the pre-compiled contract.
300-
func (c *bn256ScalarMul) RequiredGas(input []byte) uint64 {
301-
return params.Bn256ScalarMulGas
321+
func (c *bn256AddByzantium) RequiredGas(input []byte) uint64 {
322+
return params.Bn256AddGasByzantium
323+
}
324+
325+
func (c *bn256AddByzantium) Run(input []byte) ([]byte, error) {
326+
return runBn256Add(input)
302327
}
303328

304-
func (c *bn256ScalarMul) Run(input []byte) ([]byte, error) {
329+
// runBn256ScalarMul implements the Bn256ScalarMul precompile, referenced by
330+
// both Byzantium and Istanbul operations.
331+
func runBn256ScalarMul(input []byte) ([]byte, error) {
305332
p, err := newCurvePoint(getData(input, 0, 64))
306333
if err != nil {
307334
return nil, err
@@ -311,6 +338,32 @@ func (c *bn256ScalarMul) Run(input []byte) ([]byte, error) {
311338
return res.Marshal(), nil
312339
}
313340

341+
// bn256ScalarMulIstanbul implements a native elliptic curve scalar
342+
// multiplication conforming to Istanbul consensus rules.
343+
type bn256ScalarMulIstanbul struct{}
344+
345+
// RequiredGas returns the gas required to execute the pre-compiled contract.
346+
func (c *bn256ScalarMulIstanbul) RequiredGas(input []byte) uint64 {
347+
return params.Bn256ScalarMulGasIstanbul
348+
}
349+
350+
func (c *bn256ScalarMulIstanbul) Run(input []byte) ([]byte, error) {
351+
return runBn256ScalarMul(input)
352+
}
353+
354+
// bn256ScalarMulByzantium implements a native elliptic curve scalar
355+
// multiplication conforming to Byzantium consensus rules.
356+
type bn256ScalarMulByzantium struct{}
357+
358+
// RequiredGas returns the gas required to execute the pre-compiled contract.
359+
func (c *bn256ScalarMulByzantium) RequiredGas(input []byte) uint64 {
360+
return params.Bn256ScalarMulGasByzantium
361+
}
362+
363+
func (c *bn256ScalarMulByzantium) Run(input []byte) ([]byte, error) {
364+
return runBn256ScalarMul(input)
365+
}
366+
314367
var (
315368
// true32Byte is returned if the bn256 pairing check succeeds.
316369
true32Byte = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
@@ -322,15 +375,9 @@ var (
322375
errBadPairingInput = errors.New("bad elliptic curve pairing size")
323376
)
324377

325-
// bn256Pairing implements a pairing pre-compile for the bn256 curve
326-
type bn256Pairing struct{}
327-
328-
// RequiredGas returns the gas required to execute the pre-compiled contract.
329-
func (c *bn256Pairing) RequiredGas(input []byte) uint64 {
330-
return params.Bn256PairingBaseGas + uint64(len(input)/192)*params.Bn256PairingPerPointGas
331-
}
332-
333-
func (c *bn256Pairing) Run(input []byte) ([]byte, error) {
378+
// runBn256Pairing implements the Bn256Pairing precompile, referenced by both
379+
// Byzantium and Istanbul operations.
380+
func runBn256Pairing(input []byte) ([]byte, error) {
334381
// Handle some corner cases cheaply
335382
if len(input)%192 > 0 {
336383
return nil, errBadPairingInput
@@ -358,3 +405,29 @@ func (c *bn256Pairing) Run(input []byte) ([]byte, error) {
358405
}
359406
return false32Byte, nil
360407
}
408+
409+
// bn256PairingIstanbul implements a pairing pre-compile for the bn256 curve
410+
// conforming to Istanbul consensus rules.
411+
type bn256PairingIstanbul struct{}
412+
413+
// RequiredGas returns the gas required to execute the pre-compiled contract.
414+
func (c *bn256PairingIstanbul) RequiredGas(input []byte) uint64 {
415+
return params.Bn256PairingBaseGasIstanbul + uint64(len(input)/192)*params.Bn256PairingPerPointGasIstanbul
416+
}
417+
418+
func (c *bn256PairingIstanbul) Run(input []byte) ([]byte, error) {
419+
return runBn256Pairing(input)
420+
}
421+
422+
// bn256PairingByzantium implements a pairing pre-compile for the bn256 curve
423+
// conforming to Byzantium consensus rules.
424+
type bn256PairingByzantium struct{}
425+
426+
// RequiredGas returns the gas required to execute the pre-compiled contract.
427+
func (c *bn256PairingByzantium) RequiredGas(input []byte) uint64 {
428+
return params.Bn256PairingBaseGasByzantium + uint64(len(input)/192)*params.Bn256PairingPerPointGasByzantium
429+
}
430+
431+
func (c *bn256PairingByzantium) Run(input []byte) ([]byte, error) {
432+
return runBn256Pairing(input)
433+
}

‎core/vm/evm.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, err
4747
if evm.chainRules.IsByzantium {
4848
precompiles = PrecompiledContractsByzantium
4949
}
50+
if evm.chainRules.IsIstanbul {
51+
precompiles = PrecompiledContractsIstanbul
52+
}
5053
if p := precompiles[*contract.CodeAddr]; p != nil {
5154
return RunPrecompiledContract(p, input, contract)
5255
}
@@ -206,6 +209,9 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
206209
if evm.chainRules.IsByzantium {
207210
precompiles = PrecompiledContractsByzantium
208211
}
212+
if evm.chainRules.IsIstanbul {
213+
precompiles = PrecompiledContractsIstanbul
214+
}
209215
if precompiles[addr] == nil && evm.chainRules.IsEIP158 && value.Sign() == 0 {
210216
// Calling a non existing account, don't do anything, but ping the tracer
211217
if evm.vmConfig.Debug && evm.depth == 0 {

‎params/config.go‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var (
6565
ByzantiumBlock: big.NewInt(4370000),
6666
ConstantinopleBlock: big.NewInt(7280000),
6767
PetersburgBlock: big.NewInt(7280000),
68+
IstanbulBlock: nil,
6869
Ethash: new(EthashConfig),
6970
}
7071

@@ -102,6 +103,7 @@ var (
102103
ByzantiumBlock: big.NewInt(1700000),
103104
ConstantinopleBlock: big.NewInt(4230000),
104105
PetersburgBlock: big.NewInt(4939394),
106+
IstanbulBlock: nil,
105107
Ethash: new(EthashConfig),
106108
}
107109

@@ -139,6 +141,7 @@ var (
139141
ByzantiumBlock: big.NewInt(1035301),
140142
ConstantinopleBlock: big.NewInt(3660663),
141143
PetersburgBlock: big.NewInt(4321234),
144+
IstanbulBlock: nil,
142145
Clique: &CliqueConfig{
143146
Period: 15,
144147
Epoch: 30000,
@@ -177,6 +180,7 @@ var (
177180
ByzantiumBlock: big.NewInt(0),
178181
ConstantinopleBlock: big.NewInt(0),
179182
PetersburgBlock: big.NewInt(0),
183+
IstanbulBlock: nil,
180184
Clique: &CliqueConfig{
181185
Period: 15,
182186
Epoch: 30000,
@@ -209,16 +213,16 @@ var (
209213
//
210214
// This configuration is intentionally not using keyed fields to force anyone
211215
// adding flags to the config to also have to set these fields.
212-
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil}
216+
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, new(EthashConfig), nil}
213217

214218
// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
215219
// and accepted by the Ethereum core developers into the Clique consensus.
216220
//
217221
// This configuration is intentionally not using keyed fields to force anyone
218222
// adding flags to the config to also have to set these fields.
219-
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}}
223+
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}}
220224

221-
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil}
225+
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, new(EthashConfig), nil}
222226
TestRules = TestChainConfig.Rules(new(big.Int))
223227
)
224228

@@ -287,6 +291,7 @@ type ChainConfig struct {
287291
ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium)
288292
ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
289293
PetersburgBlock *big.Int `json:"petersburgBlock,omitempty"` // Petersburg switch block (nil = same as Constantinople)
294+
IstanbulBlock *big.Int `json:"istanbulBlock,omitempty"` // Istanbul switch block (nil = no fork, 0 = already on istanbul)
290295
EWASMBlock *big.Int `json:"ewasmBlock,omitempty"` // EWASM switch block (nil = no fork, 0 = already activated)
291296

292297
// Various consensus engines
@@ -324,7 +329,7 @@ func (c *ChainConfig) String() string {
324329
default:
325330
engine = "unknown"
326331
}
327-
return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Petersburg: %v Engine: %v}",
332+
return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Petersburg: %v Istanbul: %v Engine: %v}",
328333
c.ChainID,
329334
c.HomesteadBlock,
330335
c.DAOForkBlock,
@@ -335,6 +340,7 @@ func (c *ChainConfig) String() string {
335340
c.ByzantiumBlock,
336341
c.ConstantinopleBlock,
337342
c.PetersburgBlock,
343+
c.IstanbulBlock,
338344
engine,
339345
)
340346
}
@@ -381,6 +387,11 @@ func (c *ChainConfig) IsPetersburg(num *big.Int) bool {
381387
return isForked(c.PetersburgBlock, num) || c.PetersburgBlock == nil && isForked(c.ConstantinopleBlock, num)
382388
}
383389

390+
// IsIstanbul returns whether num is either equal to the Istanbul fork block or greater.
391+
func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
392+
return isForked(c.IstanbulBlock, num)
393+
}
394+
384395
// IsEWASM returns whether num represents a block number after the EWASM fork
385396
func (c *ChainConfig) IsEWASM(num *big.Int) bool {
386397
return isForked(c.EWASMBlock, num)
@@ -435,6 +446,9 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *Confi
435446
if isForkIncompatible(c.PetersburgBlock, newcfg.PetersburgBlock, head) {
436447
return newCompatError("Petersburg fork block", c.PetersburgBlock, newcfg.PetersburgBlock)
437448
}
449+
if isForkIncompatible(c.IstanbulBlock, newcfg.IstanbulBlock, head) {
450+
return newCompatError("Istanbul fork block", c.IstanbulBlock, newcfg.IstanbulBlock)
451+
}
438452
if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) {
439453
return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock)
440454
}
@@ -502,9 +516,9 @@ func (err *ConfigCompatError) Error() string {
502516
// Rules is a one time interface meaning that it shouldn't be used in between transition
503517
// phases.
504518
type Rules struct {
505-
ChainID *big.Int
506-
IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
507-
IsByzantium, IsConstantinople, IsPetersburg bool
519+
ChainID *big.Int
520+
IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
521+
IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
508522
}
509523

510524
// Rules ensures c's ChainID is not nil.
@@ -522,5 +536,6 @@ func (c *ChainConfig) Rules(num *big.Int) Rules {
522536
IsByzantium: c.IsByzantium(num),
523537
IsConstantinople: c.IsConstantinople(num),
524538
IsPetersburg: c.IsPetersburg(num),
539+
IsIstanbul: c.IsIstanbul(num),
525540
}
526541
}

‎params/protocol_params.go‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,23 @@ const (
9999

100100
// Precompiled contract gas prices
101101

102-
EcrecoverGas uint64 = 3000 // Elliptic curve sender recovery gas price
103-
Sha256BaseGas uint64 = 60 // Base price for a SHA256 operation
104-
Sha256PerWordGas uint64 = 12 // Per-word price for a SHA256 operation
105-
Ripemd160BaseGas uint64 = 600 // Base price for a RIPEMD160 operation
106-
Ripemd160PerWordGas uint64 = 120 // Per-word price for a RIPEMD160 operation
107-
IdentityBaseGas uint64 = 15 // Base price for a data copy operation
108-
IdentityPerWordGas uint64 = 3 // Per-work price for a data copy operation
109-
ModExpQuadCoeffDiv uint64 = 20 // Divisor for the quadratic particle of the big int modular exponentiation
110-
Bn256AddGas uint64 = 500 // Gas needed for an elliptic curve addition
111-
Bn256ScalarMulGas uint64 = 40000 // Gas needed for an elliptic curve scalar multiplication
112-
Bn256PairingBaseGas uint64 = 100000 // Base price for an elliptic curve pairing check
113-
Bn256PairingPerPointGas uint64 = 80000 // Per-point price for an elliptic curve pairing check
102+
EcrecoverGas uint64 = 3000 // Elliptic curve sender recovery gas price
103+
Sha256BaseGas uint64 = 60 // Base price for a SHA256 operation
104+
Sha256PerWordGas uint64 = 12 // Per-word price for a SHA256 operation
105+
Ripemd160BaseGas uint64 = 600 // Base price for a RIPEMD160 operation
106+
Ripemd160PerWordGas uint64 = 120 // Per-word price for a RIPEMD160 operation
107+
IdentityBaseGas uint64 = 15 // Base price for a data copy operation
108+
IdentityPerWordGas uint64 = 3 // Per-work price for a data copy operation
109+
ModExpQuadCoeffDiv uint64 = 20 // Divisor for the quadratic particle of the big int modular exponentiation
110+
111+
Bn256AddGasByzantium uint64 = 500 // Byzantium gas needed for an elliptic curve addition
112+
Bn256AddGasIstanbul uint64 = 150 // Gas needed for an elliptic curve addition
113+
Bn256ScalarMulGasByzantium uint64 = 40000 // Byzantium gas needed for an elliptic curve scalar multiplication
114+
Bn256ScalarMulGasIstanbul uint64 = 6000 // Gas needed for an elliptic curve scalar multiplication
115+
Bn256PairingBaseGasByzantium uint64 = 100000 // Byzantium base price for an elliptic curve pairing check
116+
Bn256PairingBaseGasIstanbul uint64 = 45000 // Base price for an elliptic curve pairing check
117+
Bn256PairingPerPointGasByzantium uint64 = 80000 // Byzantium per-point price for an elliptic curve pairing check
118+
Bn256PairingPerPointGasIstanbul uint64 = 34000 // Per-point price for an elliptic curve pairing check
114119
)
115120

116121
var (

0 commit comments

Comments
 (0)