Skip to content

Commit a43f38d

Browse files
somnathb1s1na
andauthored
core/vm: BLS gas changes for pectra-devnet-5 per EIP-2537 (#13346) (#13469)
Refer to the following: ethereum/EIPs#9116 ethereum/EIPs#9098 ethereum/EIPs#9097 ethereum/EIPs#8945 Issue board: #12401 Cherry pick #13346 --------- Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
1 parent 07c6e9c commit a43f38d

15 files changed

+1238
-2284
lines changed

core/vm/contracts.go

Lines changed: 12 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,12 @@ var PrecompiledContractsPrague = map[libcommon.Address]PrecompiledContract{
141141
libcommon.BytesToAddress([]byte{0x09}): &blake2F{},
142142
libcommon.BytesToAddress([]byte{0x0a}): &pointEvaluation{},
143143
libcommon.BytesToAddress([]byte{0x0b}): &bls12381G1Add{},
144-
libcommon.BytesToAddress([]byte{0x0c}): &bls12381G1Mul{},
145-
libcommon.BytesToAddress([]byte{0x0d}): &bls12381G1MultiExp{},
146-
libcommon.BytesToAddress([]byte{0x0e}): &bls12381G2Add{},
147-
libcommon.BytesToAddress([]byte{0x0f}): &bls12381G2Mul{},
148-
libcommon.BytesToAddress([]byte{0x10}): &bls12381G2MultiExp{},
149-
libcommon.BytesToAddress([]byte{0x11}): &bls12381Pairing{},
150-
libcommon.BytesToAddress([]byte{0x12}): &bls12381MapFpToG1{},
151-
libcommon.BytesToAddress([]byte{0x13}): &bls12381MapFp2ToG2{},
144+
libcommon.BytesToAddress([]byte{0x0c}): &bls12381G1MultiExp{},
145+
libcommon.BytesToAddress([]byte{0x0d}): &bls12381G2Add{},
146+
libcommon.BytesToAddress([]byte{0x0e}): &bls12381G2MultiExp{},
147+
libcommon.BytesToAddress([]byte{0x0f}): &bls12381Pairing{},
148+
libcommon.BytesToAddress([]byte{0x10}): &bls12381MapFpToG1{},
149+
libcommon.BytesToAddress([]byte{0x11}): &bls12381MapFp2ToG2{},
152150
}
153151

154152
var (
@@ -735,45 +733,6 @@ func (c *bls12381G1Add) Run(input []byte) ([]byte, error) {
735733
return encodePointG1(p0), nil
736734
}
737735

738-
// bls12381G1Mul implements EIP-2537 G1Mul precompile.
739-
type bls12381G1Mul struct{}
740-
741-
// RequiredGas returns the gas required to execute the pre-compiled contract.
742-
func (c *bls12381G1Mul) RequiredGas(input []byte) uint64 {
743-
return params.Bls12381G1MulGas
744-
}
745-
746-
func (c *bls12381G1Mul) Run(input []byte) ([]byte, error) {
747-
// Implements EIP-2537 G1Mul precompile.
748-
// > 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).
749-
// > Output is an encoding of multiplication operation result - single G1 point (`128` bytes).
750-
if len(input) != 160 {
751-
return nil, errBLS12381InvalidInputLength
752-
}
753-
var err error
754-
var p0 *bls12381.G1Affine
755-
756-
// Decode G1 point
757-
if p0, err = decodePointG1(input[:128]); err != nil {
758-
return nil, err
759-
}
760-
761-
// Fast subgroup check
762-
if !p0.IsInSubGroup() {
763-
return nil, errBLS12381G1PointSubgroup
764-
}
765-
766-
// Decode scalar value
767-
e := new(big.Int).SetBytes(input[128:])
768-
769-
// Compute r = e * p_0
770-
r := new(bls12381.G1Affine)
771-
r.ScalarMultiplication(p0, e)
772-
773-
// Encode the G1 point into 128 bytes
774-
return encodePointG1(r), nil
775-
}
776-
777736
// bls12381G1MultiExp implements EIP-2537 G1MultiExp precompile.
778737
type bls12381G1MultiExp struct{}
779738

@@ -787,10 +746,10 @@ func (c *bls12381G1MultiExp) RequiredGas(input []byte) uint64 {
787746
}
788747
// Lookup discount value for G1 point, scalar value pair length
789748
var discount uint64
790-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
791-
discount = params.Bls12381MultiExpDiscountTable[k-1]
749+
if dLen := len(params.Bls12381MSMDiscountTableG1); k < dLen {
750+
discount = params.Bls12381MSMDiscountTableG1[k-1]
792751
} else {
793-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
752+
discount = params.Bls12381MSMDiscountTableG1[dLen-1]
794753
}
795754
// Calculate gas and return the result
796755
return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000
@@ -868,45 +827,6 @@ func (c *bls12381G2Add) Run(input []byte) ([]byte, error) {
868827
return encodePointG2(r), nil
869828
}
870829

871-
// bls12381G2Mul implements EIP-2537 G2Mul precompile.
872-
type bls12381G2Mul struct{}
873-
874-
// RequiredGas returns the gas required to execute the pre-compiled contract.
875-
func (c *bls12381G2Mul) RequiredGas(input []byte) uint64 {
876-
return params.Bls12381G2MulGas
877-
}
878-
879-
func (c *bls12381G2Mul) Run(input []byte) ([]byte, error) {
880-
// Implements EIP-2537 G2MUL precompile logic.
881-
// > 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).
882-
// > Output is an encoding of multiplication operation result - single G2 point (`256` bytes).
883-
if len(input) != 288 {
884-
return nil, errBLS12381InvalidInputLength
885-
}
886-
var err error
887-
var p0 *bls12381.G2Affine
888-
889-
// Decode G2 point
890-
if p0, err = decodePointG2(input[:256]); err != nil {
891-
return nil, err
892-
}
893-
894-
// Fast subgroup check
895-
if !p0.IsInSubGroup() {
896-
return nil, errBLS12381G2PointSubgroup
897-
}
898-
899-
// Decode scalar value
900-
e := new(big.Int).SetBytes(input[256:])
901-
902-
// Compute r = e * p_0
903-
r := new(bls12381.G2Affine)
904-
r.ScalarMultiplication(p0, e)
905-
906-
// Encode the G2 point into 256 bytes
907-
return encodePointG2(r), nil
908-
}
909-
910830
// bls12381G2MultiExp implements EIP-2537 G2MultiExp precompile.
911831
type bls12381G2MultiExp struct{}
912832

@@ -920,10 +840,10 @@ func (c *bls12381G2MultiExp) RequiredGas(input []byte) uint64 {
920840
}
921841
// Lookup discount value for G2 point, scalar value pair length
922842
var discount uint64
923-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
924-
discount = params.Bls12381MultiExpDiscountTable[k-1]
843+
if dLen := len(params.Bls12381MSMDiscountTableG2); k < dLen {
844+
discount = params.Bls12381MSMDiscountTableG2[k-1]
925845
} else {
926-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
846+
discount = params.Bls12381MSMDiscountTableG2[dLen-1]
927847
}
928848
// Calculate gas and return the result
929849
return (uint64(k) * params.Bls12381G2MulGas * discount) / 1000

core/vm/contracts_test.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ var allPrecompiles = map[libcommon.Address]PrecompiledContract{
5959
libcommon.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
6060
libcommon.BytesToAddress([]byte{9}): &blake2F{},
6161
libcommon.BytesToAddress([]byte{10}): &bls12381G1Add{},
62-
libcommon.BytesToAddress([]byte{11}): &bls12381G1Mul{},
6362
libcommon.BytesToAddress([]byte{12}): &bls12381G1MultiExp{},
6463
libcommon.BytesToAddress([]byte{13}): &bls12381G2Add{},
65-
libcommon.BytesToAddress([]byte{14}): &bls12381G2Mul{},
6664
libcommon.BytesToAddress([]byte{15}): &bls12381G2MultiExp{},
6765
libcommon.BytesToAddress([]byte{16}): &bls12381Pairing{},
6866
libcommon.BytesToAddress([]byte{17}): &bls12381MapFpToG1{},
@@ -312,10 +310,8 @@ func benchJson(name, addr string, b *testing.B) {
312310
}
313311

314312
func TestPrecompiledBLS12381G1Add(t *testing.T) { testJson("blsG1Add", "0a", t) }
315-
func TestPrecompiledBLS12381G1Mul(t *testing.T) { testJson("blsG1Mul", "0b", t) }
316313
func TestPrecompiledBLS12381G1MultiExp(t *testing.T) { testJson("blsG1MultiExp", "0c", t) }
317314
func TestPrecompiledBLS12381G2Add(t *testing.T) { testJson("blsG2Add", "0d", t) }
318-
func TestPrecompiledBLS12381G2Mul(t *testing.T) { testJson("blsG2Mul", "0e", t) }
319315
func TestPrecompiledBLS12381G2MultiExp(t *testing.T) { testJson("blsG2MultiExp", "0f", t) }
320316
func TestPrecompiledBLS12381Pairing(t *testing.T) { testJson("blsPairing", "10", t) }
321317
func TestPrecompiledBLS12381MapG1(t *testing.T) { testJson("blsMapG1", "11", t) }
@@ -326,48 +322,20 @@ func BenchmarkPrecompiledBLS12381G1Add(b *testing.B) { benchJson("blsG1Add"
326322
func BenchmarkPrecompiledBLS12381G1Mul(b *testing.B) { benchJson("blsG1Mul", "0b", b) }
327323
func BenchmarkPrecompiledBLS12381G1MultiExp(b *testing.B) { benchJson("blsG1MultiExp", "0c", b) }
328324
func BenchmarkPrecompiledBLS12381G2Add(b *testing.B) { benchJson("blsG2Add", "0d", b) }
329-
func BenchmarkPrecompiledBLS12381G2Mul(b *testing.B) { benchJson("blsG2Mul", "0e", b) }
330325
func BenchmarkPrecompiledBLS12381G2MultiExp(b *testing.B) { benchJson("blsG2MultiExp", "0f", b) }
331326
func BenchmarkPrecompiledBLS12381Pairing(b *testing.B) { benchJson("blsPairing", "10", b) }
332327
func BenchmarkPrecompiledBLS12381MapG1(b *testing.B) { benchJson("blsMapG1", "11", b) }
333328
func BenchmarkPrecompiledBLS12381MapG2(b *testing.B) { benchJson("blsMapG2", "12", b) }
334329

335330
// Failure tests
336331
func TestPrecompiledBLS12381G1AddFail(t *testing.T) { testJsonFail("blsG1Add", "0a", t) }
337-
func TestPrecompiledBLS12381G1MulFail(t *testing.T) { testJsonFail("blsG1Mul", "0b", t) }
338332
func TestPrecompiledBLS12381G1MultiExpFail(t *testing.T) { testJsonFail("blsG1MultiExp", "0c", t) }
339333
func TestPrecompiledBLS12381G2AddFail(t *testing.T) { testJsonFail("blsG2Add", "0d", t) }
340-
func TestPrecompiledBLS12381G2MulFail(t *testing.T) { testJsonFail("blsG2Mul", "0e", t) }
341334
func TestPrecompiledBLS12381G2MultiExpFail(t *testing.T) { testJsonFail("blsG2MultiExp", "0f", t) }
342335
func TestPrecompiledBLS12381PairingFail(t *testing.T) { testJsonFail("blsPairing", "10", t) }
343336
func TestPrecompiledBLS12381MapG1Fail(t *testing.T) { testJsonFail("blsMapG1", "11", t) }
344337
func TestPrecompiledBLS12381MapG2Fail(t *testing.T) { testJsonFail("blsMapG2", "12", t) }
345338

346-
// Tests from https://github.com/ethereum/EIPs/tree/master/assets/eip-2537
347-
func TestPrecompiledBLS12381G1AddEip(t *testing.T) { testJson("blsG1Add-eip", "0a", t) }
348-
func TestPrecompiledBLS12381G1MulEip(t *testing.T) { testJson("blsG1Mul-eip", "0b", t) }
349-
func TestPrecompiledBLS12381G1MultiExpEip(t *testing.T) { testJson("blsG1MultiExp-eip", "0c", t) }
350-
func TestPrecompiledBLS12381G2AddEip(t *testing.T) { testJson("blsG2Add-eip", "0d", t) }
351-
func TestPrecompiledBLS12381G2MulEip(t *testing.T) { testJson("blsG2Mul-eip", "0e", t) }
352-
func TestPrecompiledBLS12381G2MultiExpEip(t *testing.T) { testJson("blsG2MultiExp-eip", "0f", t) }
353-
func TestPrecompiledBLS12381PairingEip(t *testing.T) { testJson("blsPairing-eip", "10", t) }
354-
func TestPrecompiledBLS12381MapG1Eip(t *testing.T) { testJson("blsMapG1-eip", "11", t) }
355-
func TestPrecompiledBLS12381MapG2Eip(t *testing.T) { testJson("blsMapG2-eip", "12", t) }
356-
357-
func TestPrecompiledBLS12381G1AddFailEip(t *testing.T) { testJsonFail("blsG1Add-eip", "0a", t) }
358-
func TestPrecompiledBLS12381G1MulFailEip(t *testing.T) { testJsonFail("blsG1Mul-eip", "0b", t) }
359-
func TestPrecompiledBLS12381G1MultiExpFailEip(t *testing.T) {
360-
testJsonFail("blsG1MultiExp-eip", "0c", t)
361-
}
362-
func TestPrecompiledBLS12381G2AddFailEip(t *testing.T) { testJsonFail("blsG2Add-eip", "0d", t) }
363-
func TestPrecompiledBLS12381G2MulFailEip(t *testing.T) { testJsonFail("blsG2Mul-eip", "0e", t) }
364-
func TestPrecompiledBLS12381G2MultiExpFailEip(t *testing.T) {
365-
testJsonFail("blsG2MultiExp-eip", "0f", t)
366-
}
367-
func TestPrecompiledBLS12381PairingFailEip(t *testing.T) { testJsonFail("blsPairing-eip", "10", t) }
368-
func TestPrecompiledBLS12381MapG1FailEip(t *testing.T) { testJsonFail("blsMapG1-eip", "11", t) }
369-
func TestPrecompiledBLS12381MapG2FailEip(t *testing.T) { testJsonFail("blsMapG2-eip", "12", t) }
370-
371339
func loadJson(name string) ([]precompiledTest, error) {
372340
data, err := os.ReadFile(fmt.Sprintf("testdata/precompiles/%v.json", name))
373341
if err != nil {
@@ -439,6 +407,5 @@ func BenchmarkPrecompiledP256Verify(b *testing.B) {
439407

440408
func TestPrecompiledP256Verify(t *testing.T) {
441409
t.Parallel()
442-
443410
testJson("p256Verify", "100", t)
444411
}

0 commit comments

Comments
 (0)