@@ -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+
314367var (
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+ }
0 commit comments