Skip to content

Commit 3003a13

Browse files
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 f700ed9 commit 3003a13

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
@@ -129,14 +129,12 @@ var PrecompiledContractsPrague = PrecompiledContracts{
129129
common.BytesToAddress([]byte{0x09}): &blake2F{},
130130
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
131131
common.BytesToAddress([]byte{0x0b}): &bls12381G1Add{},
132-
common.BytesToAddress([]byte{0x0c}): &bls12381G1Mul{},
133-
common.BytesToAddress([]byte{0x0d}): &bls12381G1MultiExp{},
134-
common.BytesToAddress([]byte{0x0e}): &bls12381G2Add{},
135-
common.BytesToAddress([]byte{0x0f}): &bls12381G2Mul{},
136-
common.BytesToAddress([]byte{0x10}): &bls12381G2MultiExp{},
137-
common.BytesToAddress([]byte{0x11}): &bls12381Pairing{},
138-
common.BytesToAddress([]byte{0x12}): &bls12381MapG1{},
139-
common.BytesToAddress([]byte{0x13}): &bls12381MapG2{},
132+
common.BytesToAddress([]byte{0x0c}): &bls12381G1MultiExp{},
133+
common.BytesToAddress([]byte{0x0d}): &bls12381G2Add{},
134+
common.BytesToAddress([]byte{0x0e}): &bls12381G2MultiExp{},
135+
common.BytesToAddress([]byte{0x0f}): &bls12381Pairing{},
136+
common.BytesToAddress([]byte{0x10}): &bls12381MapG1{},
137+
common.BytesToAddress([]byte{0x11}): &bls12381MapG2{},
140138
}
141139

142140
var PrecompiledContractsBLS = PrecompiledContractsPrague
@@ -750,44 +748,6 @@ func (c *bls12381G1Add) Run(input []byte) ([]byte, error) {
750748
return encodePointG1(p0), nil
751749
}
752750

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

@@ -801,10 +761,10 @@ func (c *bls12381G1MultiExp) RequiredGas(input []byte) uint64 {
801761
}
802762
// Lookup discount value for G1 point, scalar value pair length
803763
var discount uint64
804-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
805-
discount = params.Bls12381MultiExpDiscountTable[k-1]
764+
if dLen := len(params.Bls12381G1MultiExpDiscountTable); k < dLen {
765+
discount = params.Bls12381G1MultiExpDiscountTable[k-1]
806766
} else {
807-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
767+
discount = params.Bls12381G1MultiExpDiscountTable[dLen-1]
808768
}
809769
// Calculate gas and return the result
810770
return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000
@@ -885,44 +845,6 @@ func (c *bls12381G2Add) Run(input []byte) ([]byte, error) {
885845
return encodePointG2(r), nil
886846
}
887847

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

@@ -936,10 +858,10 @@ func (c *bls12381G2MultiExp) RequiredGas(input []byte) uint64 {
936858
}
937859
// Lookup discount value for G2 point, scalar value pair length
938860
var discount uint64
939-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
940-
discount = params.Bls12381MultiExpDiscountTable[k-1]
861+
if dLen := len(params.Bls12381G2MultiExpDiscountTable); k < dLen {
862+
discount = params.Bls12381G2MultiExpDiscountTable[k-1]
941863
} else {
942-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
864+
discount = params.Bls12381G2MultiExpDiscountTable[dLen-1]
943865
}
944866
// Calculate gas and return the result
945867
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
@@ -59,14 +59,12 @@ var allPrecompiles = map[common.Address]PrecompiledContract{
5959
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
6060

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)