Skip to content

Commit ea3be60

Browse files
authored
Merge pull request #409 from OffchainLabs/eip2537
EIP-2537
2 parents a3e8cc4 + 6a16fe5 commit ea3be60

29 files changed

+1848
-2309
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
@@ -742,44 +740,6 @@ func (c *bls12381G1Add) Run(input []byte) ([]byte, error) {
742740
return encodePointG1(p0), nil
743741
}
744742

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

@@ -793,10 +753,10 @@ func (c *bls12381G1MultiExp) RequiredGas(input []byte) uint64 {
793753
}
794754
// Lookup discount value for G1 point, scalar value pair length
795755
var discount uint64
796-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
797-
discount = params.Bls12381MultiExpDiscountTable[k-1]
756+
if dLen := len(params.Bls12381G1MultiExpDiscountTable); k < dLen {
757+
discount = params.Bls12381G1MultiExpDiscountTable[k-1]
798758
} else {
799-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
759+
discount = params.Bls12381G1MultiExpDiscountTable[dLen-1]
800760
}
801761
// Calculate gas and return the result
802762
return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000
@@ -877,44 +837,6 @@ func (c *bls12381G2Add) Run(input []byte) ([]byte, error) {
877837
return encodePointG2(r), nil
878838
}
879839

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

@@ -928,10 +850,10 @@ func (c *bls12381G2MultiExp) RequiredGas(input []byte) uint64 {
928850
}
929851
// Lookup discount value for G2 point, scalar value pair length
930852
var discount uint64
931-
if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen {
932-
discount = params.Bls12381MultiExpDiscountTable[k-1]
853+
if dLen := len(params.Bls12381G2MultiExpDiscountTable); k < dLen {
854+
discount = params.Bls12381G2MultiExpDiscountTable[k-1]
933855
} else {
934-
discount = params.Bls12381MultiExpDiscountTable[dLen-1]
856+
discount = params.Bls12381G2MultiExpDiscountTable[dLen-1]
935857
}
936858
// Calculate gas and return the result
937859
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)