Skip to content

Commit 379f5a8

Browse files
s1naMariusVanDerWijden
authored andcommitted
core/vm: implement EIP-2537 spec updates (#30978)
Reference: - Remove MUL precompiles: ethereum/EIPs#8945 - Pricing change for pairing operation: ethereum/EIPs#9098 - Pricing change for add, mapping and mul operations: ethereum/EIPs#9097 - Pricing change for MSM operations: ethereum/EIPs#9116 --------- Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de>
1 parent 7728b31 commit 379f5a8

25 files changed

+1251
-2329
lines changed

core/vm/contracts.go

Lines changed: 12 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,12 @@ var PrecompiledContractsPrague = map[common.Address]PrecompiledContract{
132132
common.BytesToAddress([]byte{0x09}): &blake2F{},
133133
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
134134
common.BytesToAddress([]byte{0x0b}): &bls12381G1Add{},
135-
common.BytesToAddress([]byte{0x0c}): &bls12381G1Mul{},
136-
common.BytesToAddress([]byte{0x0d}): &bls12381G1MultiExp{},
137-
common.BytesToAddress([]byte{0x0e}): &bls12381G2Add{},
138-
common.BytesToAddress([]byte{0x0f}): &bls12381G2Mul{},
139-
common.BytesToAddress([]byte{0x10}): &bls12381G2MultiExp{},
140-
common.BytesToAddress([]byte{0x11}): &bls12381Pairing{},
141-
common.BytesToAddress([]byte{0x12}): &bls12381MapG1{},
142-
common.BytesToAddress([]byte{0x13}): &bls12381MapG2{},
135+
common.BytesToAddress([]byte{0x0c}): &bls12381G1MultiExp{},
136+
common.BytesToAddress([]byte{0x0d}): &bls12381G2Add{},
137+
common.BytesToAddress([]byte{0x0e}): &bls12381G2MultiExp{},
138+
common.BytesToAddress([]byte{0x0f}): &bls12381Pairing{},
139+
common.BytesToAddress([]byte{0x10}): &bls12381MapG1{},
140+
common.BytesToAddress([]byte{0x11}): &bls12381MapG2{},
143141
}
144142

145143
var PrecompiledContractsBLS = PrecompiledContractsPrague
@@ -745,44 +743,6 @@ func (c *bls12381G1Add) Run(input []byte) ([]byte, error) {
745743
return encodePointG1(p0), nil
746744
}
747745

748-
// bls12381G1Mul implements EIP-2537 G1Mul precompile.
749-
type bls12381G1Mul struct{}
750-
751-
// RequiredGas returns the gas required to execute the pre-compiled contract.
752-
func (c *bls12381G1Mul) RequiredGas(input []byte) uint64 {
753-
return params.Bls12381G1MulGas
754-
}
755-
756-
func (c *bls12381G1Mul) Run(input []byte) ([]byte, error) {
757-
// Implements EIP-2537 G1Mul precompile.
758-
// > G1 multiplication call expects `160` bytes as an input that is interpreted as byte concatenation of encoding of G1 point (`128` bytes) and encoding of a scalar value (`32` bytes).
759-
// > Output is an encoding of multiplication operation result - single G1 point (`128` bytes).
760-
if len(input) != 160 {
761-
return nil, errBLS12381InvalidInputLength
762-
}
763-
var err error
764-
var p0 *bls12381.G1Affine
765-
766-
// Decode G1 point
767-
if p0, err = decodePointG1(input[:128]); err != nil {
768-
return nil, err
769-
}
770-
// 'point is on curve' check already done,
771-
// Here we need to apply subgroup checks.
772-
if !p0.IsInSubGroup() {
773-
return nil, errBLS12381G1PointSubgroup
774-
}
775-
// Decode scalar value
776-
e := new(big.Int).SetBytes(input[128:])
777-
778-
// Compute r = e * p_0
779-
r := new(bls12381.G1Affine)
780-
r.ScalarMultiplication(p0, e)
781-
782-
// Encode the G1 point into 128 bytes
783-
return encodePointG1(r), nil
784-
}
785-
786746
// bls12381G1MultiExp implements EIP-2537 G1MultiExp precompile.
787747
type bls12381G1MultiExp struct{}
788748

@@ -796,10 +756,10 @@ func (c *bls12381G1MultiExp) RequiredGas(input []byte) uint64 {
796756
}
797757
// Lookup discount value for G1 point, scalar value pair length
798758
var discount uint64
799-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
800-
discount = params.Bls12381MultiExpDiscountTable[k-1]
759+
if dLen := len(params.Bls12381G1MultiExpDiscountTable); k < dLen {
760+
discount = params.Bls12381G1MultiExpDiscountTable[k-1]
801761
} else {
802-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
762+
discount = params.Bls12381G1MultiExpDiscountTable[dLen-1]
803763
}
804764
// Calculate gas and return the result
805765
return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000
@@ -880,44 +840,6 @@ func (c *bls12381G2Add) Run(input []byte) ([]byte, error) {
880840
return encodePointG2(r), nil
881841
}
882842

883-
// bls12381G2Mul implements EIP-2537 G2Mul precompile.
884-
type bls12381G2Mul struct{}
885-
886-
// RequiredGas returns the gas required to execute the pre-compiled contract.
887-
func (c *bls12381G2Mul) RequiredGas(input []byte) uint64 {
888-
return params.Bls12381G2MulGas
889-
}
890-
891-
func (c *bls12381G2Mul) Run(input []byte) ([]byte, error) {
892-
// Implements EIP-2537 G2MUL precompile logic.
893-
// > G2 multiplication call expects `288` bytes as an input that is interpreted as byte concatenation of encoding of G2 point (`256` bytes) and encoding of a scalar value (`32` bytes).
894-
// > Output is an encoding of multiplication operation result - single G2 point (`256` bytes).
895-
if len(input) != 288 {
896-
return nil, errBLS12381InvalidInputLength
897-
}
898-
var err error
899-
var p0 *bls12381.G2Affine
900-
901-
// Decode G2 point
902-
if p0, err = decodePointG2(input[:256]); err != nil {
903-
return nil, err
904-
}
905-
// 'point is on curve' check already done,
906-
// Here we need to apply subgroup checks.
907-
if !p0.IsInSubGroup() {
908-
return nil, errBLS12381G2PointSubgroup
909-
}
910-
// Decode scalar value
911-
e := new(big.Int).SetBytes(input[256:])
912-
913-
// Compute r = e * p_0
914-
r := new(bls12381.G2Affine)
915-
r.ScalarMultiplication(p0, e)
916-
917-
// Encode the G2 point into 256 bytes
918-
return encodePointG2(r), nil
919-
}
920-
921843
// bls12381G2MultiExp implements EIP-2537 G2MultiExp precompile.
922844
type bls12381G2MultiExp struct{}
923845

@@ -931,10 +853,10 @@ func (c *bls12381G2MultiExp) RequiredGas(input []byte) uint64 {
931853
}
932854
// Lookup discount value for G2 point, scalar value pair length
933855
var discount uint64
934-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
935-
discount = params.Bls12381MultiExpDiscountTable[k-1]
856+
if dLen := len(params.Bls12381G2MultiExpDiscountTable); k < dLen {
857+
discount = params.Bls12381G2MultiExpDiscountTable[k-1]
936858
} else {
937-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
859+
discount = params.Bls12381G2MultiExpDiscountTable[dLen-1]
938860
}
939861
// Calculate gas and return the result
940862
return (uint64(k) * params.Bls12381G2MulGas * discount) / 1000

core/vm/contracts_test.go

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,12 @@ var allPrecompiles = map[common.Address]PrecompiledContract{
6161
common.BytesToAddress([]byte{0x01, 0x00}): &p256Verify{},
6262

6363
common.BytesToAddress([]byte{0x0f, 0x0a}): &bls12381G1Add{},
64-
common.BytesToAddress([]byte{0x0f, 0x0b}): &bls12381G1Mul{},
65-
common.BytesToAddress([]byte{0x0f, 0x0c}): &bls12381G1MultiExp{},
66-
common.BytesToAddress([]byte{0x0f, 0x0d}): &bls12381G2Add{},
67-
common.BytesToAddress([]byte{0x0f, 0x0e}): &bls12381G2Mul{},
68-
common.BytesToAddress([]byte{0x0f, 0x0f}): &bls12381G2MultiExp{},
69-
common.BytesToAddress([]byte{0x0f, 0x10}): &bls12381Pairing{},
70-
common.BytesToAddress([]byte{0x0f, 0x11}): &bls12381MapG1{},
71-
common.BytesToAddress([]byte{0x0f, 0x12}): &bls12381MapG2{},
64+
common.BytesToAddress([]byte{0x0f, 0x0b}): &bls12381G1MultiExp{},
65+
common.BytesToAddress([]byte{0x0f, 0x0c}): &bls12381G2Add{},
66+
common.BytesToAddress([]byte{0x0f, 0x0d}): &bls12381G2MultiExp{},
67+
common.BytesToAddress([]byte{0x0f, 0x0e}): &bls12381Pairing{},
68+
common.BytesToAddress([]byte{0x0f, 0x0f}): &bls12381MapG1{},
69+
common.BytesToAddress([]byte{0x0f, 0x10}): &bls12381MapG2{},
7270
}
7371

7472
// EIP-152 test vectors
@@ -308,38 +306,36 @@ func benchJson(name, addr string, b *testing.B) {
308306

309307
func TestPrecompiledBLS12381G1Add(t *testing.T) { testJson("blsG1Add", "f0a", t) }
310308
func TestPrecompiledBLS12381G1Mul(t *testing.T) { testJson("blsG1Mul", "f0b", t) }
311-
func TestPrecompiledBLS12381G1MultiExp(t *testing.T) { testJson("blsG1MultiExp", "f0c", t) }
312-
func TestPrecompiledBLS12381G2Add(t *testing.T) { testJson("blsG2Add", "f0d", t) }
313-
func TestPrecompiledBLS12381G2Mul(t *testing.T) { testJson("blsG2Mul", "f0e", t) }
314-
func TestPrecompiledBLS12381G2MultiExp(t *testing.T) { testJson("blsG2MultiExp", "f0f", t) }
315-
func TestPrecompiledBLS12381Pairing(t *testing.T) { testJson("blsPairing", "f10", t) }
316-
func TestPrecompiledBLS12381MapG1(t *testing.T) { testJson("blsMapG1", "f11", t) }
317-
func TestPrecompiledBLS12381MapG2(t *testing.T) { testJson("blsMapG2", "f12", t) }
309+
func TestPrecompiledBLS12381G1MultiExp(t *testing.T) { testJson("blsG1MultiExp", "f0b", t) }
310+
func TestPrecompiledBLS12381G2Add(t *testing.T) { testJson("blsG2Add", "f0c", t) }
311+
func TestPrecompiledBLS12381G2Mul(t *testing.T) { testJson("blsG2Mul", "f0d", t) }
312+
func TestPrecompiledBLS12381G2MultiExp(t *testing.T) { testJson("blsG2MultiExp", "f0d", t) }
313+
func TestPrecompiledBLS12381Pairing(t *testing.T) { testJson("blsPairing", "f0e", t) }
314+
func TestPrecompiledBLS12381MapG1(t *testing.T) { testJson("blsMapG1", "f0f", t) }
315+
func TestPrecompiledBLS12381MapG2(t *testing.T) { testJson("blsMapG2", "f10", t) }
318316

319317
func TestPrecompiledPointEvaluation(t *testing.T) { testJson("pointEvaluation", "0a", t) }
320318

321319
func BenchmarkPrecompiledPointEvaluation(b *testing.B) { benchJson("pointEvaluation", "0a", b) }
322320

323321
func BenchmarkPrecompiledBLS12381G1Add(b *testing.B) { benchJson("blsG1Add", "f0a", b) }
324-
func BenchmarkPrecompiledBLS12381G1Mul(b *testing.B) { benchJson("blsG1Mul", "f0b", b) }
325-
func BenchmarkPrecompiledBLS12381G1MultiExp(b *testing.B) { benchJson("blsG1MultiExp", "f0c", b) }
326-
func BenchmarkPrecompiledBLS12381G2Add(b *testing.B) { benchJson("blsG2Add", "f0d", b) }
327-
func BenchmarkPrecompiledBLS12381G2Mul(b *testing.B) { benchJson("blsG2Mul", "f0e", b) }
328-
func BenchmarkPrecompiledBLS12381G2MultiExp(b *testing.B) { benchJson("blsG2MultiExp", "f0f", b) }
329-
func BenchmarkPrecompiledBLS12381Pairing(b *testing.B) { benchJson("blsPairing", "f10", b) }
330-
func BenchmarkPrecompiledBLS12381MapG1(b *testing.B) { benchJson("blsMapG1", "f11", b) }
331-
func BenchmarkPrecompiledBLS12381MapG2(b *testing.B) { benchJson("blsMapG2", "f12", b) }
322+
func BenchmarkPrecompiledBLS12381G1MultiExp(b *testing.B) { benchJson("blsG1MultiExp", "f0b", b) }
323+
func BenchmarkPrecompiledBLS12381G2Add(b *testing.B) { benchJson("blsG2Add", "f0c", b) }
324+
func BenchmarkPrecompiledBLS12381G2MultiExp(b *testing.B) { benchJson("blsG2MultiExp", "f0d", b) }
325+
func BenchmarkPrecompiledBLS12381Pairing(b *testing.B) { benchJson("blsPairing", "f0e", b) }
326+
func BenchmarkPrecompiledBLS12381MapG1(b *testing.B) { benchJson("blsMapG1", "f0f", b) }
327+
func BenchmarkPrecompiledBLS12381MapG2(b *testing.B) { benchJson("blsMapG2", "f10", b) }
332328

333329
// Failure tests
334330
func TestPrecompiledBLS12381G1AddFail(t *testing.T) { testJsonFail("blsG1Add", "f0a", t) }
335331
func TestPrecompiledBLS12381G1MulFail(t *testing.T) { testJsonFail("blsG1Mul", "f0b", t) }
336-
func TestPrecompiledBLS12381G1MultiExpFail(t *testing.T) { testJsonFail("blsG1MultiExp", "f0c", t) }
337-
func TestPrecompiledBLS12381G2AddFail(t *testing.T) { testJsonFail("blsG2Add", "f0d", t) }
338-
func TestPrecompiledBLS12381G2MulFail(t *testing.T) { testJsonFail("blsG2Mul", "f0e", t) }
339-
func TestPrecompiledBLS12381G2MultiExpFail(t *testing.T) { testJsonFail("blsG2MultiExp", "f0f", t) }
340-
func TestPrecompiledBLS12381PairingFail(t *testing.T) { testJsonFail("blsPairing", "f10", t) }
341-
func TestPrecompiledBLS12381MapG1Fail(t *testing.T) { testJsonFail("blsMapG1", "f11", t) }
342-
func TestPrecompiledBLS12381MapG2Fail(t *testing.T) { testJsonFail("blsMapG2", "f12", t) }
332+
func TestPrecompiledBLS12381G1MultiExpFail(t *testing.T) { testJsonFail("blsG1MultiExp", "f0b", t) }
333+
func TestPrecompiledBLS12381G2AddFail(t *testing.T) { testJsonFail("blsG2Add", "f0c", t) }
334+
func TestPrecompiledBLS12381G2MulFail(t *testing.T) { testJsonFail("blsG2Mul", "f0d", t) }
335+
func TestPrecompiledBLS12381G2MultiExpFail(t *testing.T) { testJsonFail("blsG2MultiExp", "f0d", t) }
336+
func TestPrecompiledBLS12381PairingFail(t *testing.T) { testJsonFail("blsPairing", "f0e", t) }
337+
func TestPrecompiledBLS12381MapG1Fail(t *testing.T) { testJsonFail("blsMapG1", "f0f", t) }
338+
func TestPrecompiledBLS12381MapG2Fail(t *testing.T) { testJsonFail("blsMapG2", "f10", t) }
343339

344340
func loadJson(name string) ([]precompiledTest, error) {
345341
data, err := os.ReadFile(fmt.Sprintf("testdata/precompiles/%v.json", name))

0 commit comments

Comments
 (0)