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

Include EIP-2537 precompiles into Prague #9560

Merged
merged 13 commits into from
Mar 18, 2024
20 changes: 17 additions & 3 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,17 @@ var PrecompiledContractsNapoli = map[libcommon.Address]PrecompiledContract{
libcommon.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
}

// PrecompiledContractsBLS contains the set of pre-compiled Ethereum
// contracts specified in EIP-2537. These are exported for testing purposes.
var PrecompiledContractsBLS = map[libcommon.Address]PrecompiledContract{
var PrecompiledContractsPrague = map[libcommon.Address]PrecompiledContract{
libcommon.BytesToAddress([]byte{0x01}): &ecrecover{},
libcommon.BytesToAddress([]byte{0x02}): &sha256hash{},
libcommon.BytesToAddress([]byte{0x03}): &ripemd160hash{},
libcommon.BytesToAddress([]byte{0x04}): &dataCopy{},
libcommon.BytesToAddress([]byte{0x05}): &bigModExp{eip2565: true},
libcommon.BytesToAddress([]byte{0x06}): &bn256AddIstanbul{},
libcommon.BytesToAddress([]byte{0x07}): &bn256ScalarMulIstanbul{},
libcommon.BytesToAddress([]byte{0x08}): &bn256PairingIstanbul{},
libcommon.BytesToAddress([]byte{0x09}): &blake2F{},
libcommon.BytesToAddress([]byte{0x0a}): &pointEvaluation{},
libcommon.BytesToAddress([]byte{0x0c}): &bls12381G1Add{},
somnathb1 marked this conversation as resolved.
Show resolved Hide resolved
libcommon.BytesToAddress([]byte{0x0d}): &bls12381G1Mul{},
libcommon.BytesToAddress([]byte{0x0e}): &bls12381G1MultiExp{},
Expand All @@ -140,6 +148,7 @@ var PrecompiledContractsBLS = map[libcommon.Address]PrecompiledContract{
}

var (
PrecompiledAddressesPrague []libcommon.Address
PrecompiledAddressesNapoli []libcommon.Address
PrecompiledAddressesCancun []libcommon.Address
PrecompiledAddressesBerlin []libcommon.Address
Expand Down Expand Up @@ -167,11 +176,16 @@ func init() {
for k := range PrecompiledContractsNapoli {
PrecompiledAddressesNapoli = append(PrecompiledAddressesNapoli, k)
}
for k := range PrecompiledContractsPrague {
PrecompiledAddressesPrague = append(PrecompiledAddressesPrague, k)
}
}

// ActivePrecompiles returns the precompiles enabled with the current configuration.
func ActivePrecompiles(rules *chain.Rules) []libcommon.Address {
switch {
case rules.IsPrague:
return PrecompiledAddressesPrague
case rules.IsNapoli:
return PrecompiledAddressesNapoli
case rules.IsCancun:
Expand Down
2 changes: 2 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var emptyCodeHash = crypto.Keccak256Hash(nil)
func (evm *EVM) precompile(addr libcommon.Address) (PrecompiledContract, bool) {
var precompiles map[libcommon.Address]PrecompiledContract
switch {
case evm.chainRules.IsPrague:
precompiles = PrecompiledContractsPrague
case evm.chainRules.IsNapoli:
precompiles = PrecompiledContractsNapoli
case evm.chainRules.IsCancun:
Expand Down
208 changes: 104 additions & 104 deletions core/vm/testdata/precompiles/blsG1Add.json

Large diffs are not rendered by default.

208 changes: 104 additions & 104 deletions core/vm/testdata/precompiles/blsG2Add.json

Large diffs are not rendered by default.

208 changes: 104 additions & 104 deletions core/vm/testdata/precompiles/blsG2Mul.json

Large diffs are not rendered by default.

206 changes: 103 additions & 103 deletions core/vm/testdata/precompiles/blsG2MultiExp.json

Large diffs are not rendered by default.

200 changes: 100 additions & 100 deletions core/vm/testdata/precompiles/blsMapG2.json

Large diffs are not rendered by default.

200 changes: 100 additions & 100 deletions core/vm/testdata/precompiles/blsPairing.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions crypto/bls12381/g1.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (g *G1) IsAffine(p *PointG1) bool {
return p[2].isOne()
}

// Add adds two G1 points p1, p2 and assigns the result to point at first argument.
// Affine calculates affine form of given G1 point.
func (g *G1) Affine(p *PointG1) *PointG1 {
if g.IsZero(p) {
return p
Expand All @@ -248,7 +248,7 @@ func (g *G1) Affine(p *PointG1) *PointG1 {

// Add adds two G1 points p1, p2 and assigns the result to point at first argument.
func (g *G1) Add(r, p1, p2 *PointG1) *PointG1 {
// http://www.hyperelliptic.org/EFD/gp/auto-shortw-jacobian-0.html#addition-add-2007-bl
// www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
if g.IsZero(p1) {
return r.Set(p2)
}
Expand Down Expand Up @@ -296,7 +296,7 @@ func (g *G1) Add(r, p1, p2 *PointG1) *PointG1 {

// Double doubles a G1 point p and assigns the result to the point at first argument.
func (g *G1) Double(r, p *PointG1) *PointG1 {
// http://www.hyperelliptic.org/EFD/gp/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
if g.IsZero(p) {
return r.Set(p)
}
Expand Down
3 changes: 2 additions & 1 deletion crypto/bls12381/g1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ func BenchmarkG1Add(t *testing.B) {
}

func BenchmarkG1Mul(t *testing.B) {
worstCaseScalar, _ := new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
g1 := NewG1()
a, e, c := g1.rand(), q, PointG1{}
a, e, c := g1.rand(), worstCaseScalar, PointG1{}
t.ResetTimer()
for i := 0; i < t.N; i++ {
g1.MulScalar(&c, a, e)
Expand Down
6 changes: 3 additions & 3 deletions crypto/bls12381/g2.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (g *G2) FromBytes(in []byte) (*PointG2, error) {
return p, nil
}

// DecodePoint given encoded (x, y) coordinates in 256 bytes returns a valid G1 Point.
// DecodePoint given encoded (x, y) coordinates in 256 bytes returns a valid G2 Point.
func (g *G2) DecodePoint(in []byte) (*PointG2, error) {
if len(in) != 256 {
return nil, errors.New("invalid g2 point length")
Expand Down Expand Up @@ -269,7 +269,7 @@ func (g *G2) Affine(p *PointG2) *PointG2 {

// Add adds two G2 points p1, p2 and assigns the result to point at first argument.
func (g *G2) Add(r, p1, p2 *PointG2) *PointG2 {
// http://www.hyperelliptic.org/EFD/gp/auto-shortw-jacobian-0.html#addition-add-2007-bl
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
if g.IsZero(p1) {
return r.Set(p2)
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func (g *G2) Add(r, p1, p2 *PointG2) *PointG2 {

// Double doubles a G2 point p and assigns the result to the point at first argument.
func (g *G2) Double(r, p *PointG2) *PointG2 {
// http://www.hyperelliptic.org/EFD/gp/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
if g.IsZero(p) {
return r.Set(p)
}
Expand Down
3 changes: 2 additions & 1 deletion crypto/bls12381/g2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ func BenchmarkG2Add(t *testing.B) {
}

func BenchmarkG2Mul(t *testing.B) {
worstCaseScalar, _ := new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
somnathb1 marked this conversation as resolved.
Show resolved Hide resolved
g2 := NewG2()
a, e, c := g2.rand(), q, PointG2{}
a, e, c := g2.rand(), worstCaseScalar, PointG2{}
t.ResetTimer()
for i := 0; i < t.N; i++ {
g2.MulScalar(&c, a, e)
Expand Down
16 changes: 8 additions & 8 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ const (
Bn256PairingPerPointGasByzantium uint64 = 80000 // Byzantium per-point price for an elliptic curve pairing check
Bn256PairingPerPointGasIstanbul uint64 = 34000 // Per-point price for an elliptic curve pairing check

Bls12381G1AddGas uint64 = 600 // Price for BLS12-381 elliptic curve G1 point addition
Bls12381G1MulGas uint64 = 12000 // Price for BLS12-381 elliptic curve G1 point scalar multiplication
Bls12381G2AddGas uint64 = 4500 // Price for BLS12-381 elliptic curve G2 point addition
Bls12381G2MulGas uint64 = 55000 // Price for BLS12-381 elliptic curve G2 point scalar multiplication
Bls12381PairingBaseGas uint64 = 115000 // Base gas price for BLS12-381 elliptic curve pairing check
Bls12381PairingPerPairGas uint64 = 23000 // Per-point pair gas price for BLS12-381 elliptic curve pairing check
Bls12381MapG1Gas uint64 = 5500 // Gas price for BLS12-381 mapping field element to G1 operation
Bls12381MapG2Gas uint64 = 110000 // Gas price for BLS12-381 mapping field element to G2 operation
Bls12381G1AddGas uint64 = 500 // Price for BLS12-381 elliptic curve G1 point addition
Bls12381G1MulGas uint64 = 12000 // Price for BLS12-381 elliptic curve G1 point scalar multiplication
Bls12381G2AddGas uint64 = 800 // Price for BLS12-381 elliptic curve G2 point addition
Bls12381G2MulGas uint64 = 45000 // Price for BLS12-381 elliptic curve G2 point scalar multiplication
Bls12381PairingBaseGas uint64 = 65000 // Base gas price for BLS12-381 elliptic curve pairing check
Bls12381PairingPerPairGas uint64 = 43000 // Per-point pair gas price for BLS12-381 elliptic curve pairing check
Bls12381MapG1Gas uint64 = 5500 // Gas price for BLS12-381 mapping field element to G1 operation
somnathb1 marked this conversation as resolved.
Show resolved Hide resolved
Bls12381MapG2Gas uint64 = 75000 // Gas price for BLS12-381 mapping field element to G2 operation

// The Refund Quotient is the cap on how much of the used gas can be refunded. Before EIP-3529,
// up to half the consumed gas could be refunded. Redefined as 1/5th in EIP-3529
Expand Down
2 changes: 1 addition & 1 deletion tests/fuzzers/bls12381/precompile_fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func checkInput(id byte, inputLen int) bool {
// other values are reserved for future use.
func fuzz(id byte, data []byte) int {
// Even on bad input, it should not crash, so we still test the gas calc
precompile := vm.PrecompiledContractsBLS[libcommon.BytesToAddress([]byte{id})]
precompile := vm.PrecompiledContractsPrague[libcommon.BytesToAddress([]byte{id})]
gas := precompile.RequiredGas(data)
if !checkInput(id, len(data)) {
return 0
Expand Down
Loading