Skip to content

Commit a19e6a1

Browse files
authored
Add new precompile limits for Jovian (#709)
This introduces more realistic limits on accelerated precompiles for the Jovian hard fork.
1 parent 96738d2 commit a19e6a1

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

core/vm/contracts.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,29 @@ var PrecompiledContractsIsthmus = map[common.Address]PrecompiledContract{
227227
common.BytesToAddress([]byte{0x01, 0x00}): &p256VerifyFjord{},
228228
}
229229

230+
var PrecompiledContractsJovian = map[common.Address]PrecompiledContract{
231+
common.BytesToAddress([]byte{1}): &ecrecover{},
232+
common.BytesToAddress([]byte{2}): &sha256hash{},
233+
common.BytesToAddress([]byte{3}): &ripemd160hash{},
234+
common.BytesToAddress([]byte{4}): &dataCopy{},
235+
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true},
236+
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
237+
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
238+
common.BytesToAddress([]byte{8}): &bn256PairingJovian{},
239+
common.BytesToAddress([]byte{9}): &blake2F{},
240+
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
241+
common.BytesToAddress([]byte{0x0b}): &bls12381G1Add{},
242+
common.BytesToAddress([]byte{0x0c}): &bls12381G1MultiExpJovian{},
243+
common.BytesToAddress([]byte{0x0d}): &bls12381G2Add{},
244+
common.BytesToAddress([]byte{0x0e}): &bls12381G2MultiExpJovian{},
245+
common.BytesToAddress([]byte{0x0f}): &bls12381PairingJovian{},
246+
common.BytesToAddress([]byte{0x10}): &bls12381MapG1{},
247+
common.BytesToAddress([]byte{0x11}): &bls12381MapG2{},
248+
common.BytesToAddress([]byte{0x01, 0x00}): &p256VerifyFjord{},
249+
}
250+
230251
var (
252+
PrecompiledAddressesJovian []common.Address
231253
PrecompiledAddressesIsthmus []common.Address
232254
PrecompiledAddressesGranite []common.Address
233255
PrecompiledAddressesFjord []common.Address
@@ -271,11 +293,16 @@ func init() {
271293
for k := range PrecompiledContractsIsthmus {
272294
PrecompiledAddressesIsthmus = append(PrecompiledAddressesIsthmus, k)
273295
}
296+
for k := range PrecompiledContractsJovian {
297+
PrecompiledAddressesJovian = append(PrecompiledAddressesJovian, k)
298+
}
274299
}
275300

276301
func activePrecompiledContracts(rules params.Rules) PrecompiledContracts {
277302
// note: the order of these switch cases is important
278303
switch {
304+
case rules.IsOptimismJovian:
305+
return PrecompiledContractsJovian
279306
case rules.IsOptimismIsthmus:
280307
return PrecompiledContractsIsthmus
281308
case rules.IsOptimismGranite:
@@ -309,6 +336,8 @@ func ActivePrecompiledContracts(rules params.Rules) PrecompiledContracts {
309336
// ActivePrecompiles returns the precompile addresses enabled with the current configuration.
310337
func ActivePrecompiles(rules params.Rules) []common.Address {
311338
switch {
339+
case rules.IsOptimismJovian:
340+
return PrecompiledAddressesJovian
312341
case rules.IsOptimismIsthmus:
313342
return PrecompiledAddressesIsthmus
314343
case rules.IsOptimismGranite:
@@ -823,6 +852,23 @@ func (c *bn256PairingGranite) Name() string {
823852
return "BN254_PAIRING"
824853
}
825854

855+
type bn256PairingJovian struct{}
856+
857+
func (c *bn256PairingJovian) RequiredGas(input []byte) uint64 {
858+
return new(bn256PairingIstanbul).RequiredGas(input)
859+
}
860+
861+
func (c *bn256PairingJovian) Run(input []byte) ([]byte, error) {
862+
if len(input) > int(params.Bn256PairingMaxInputSizeJovian) {
863+
return nil, errBadPairingInputSize
864+
}
865+
return runBn256Pairing(input)
866+
}
867+
868+
func (c *bn256PairingJovian) Name() string {
869+
return "BN254_PAIRING"
870+
}
871+
826872
// bn256PairingIstanbul implements a pairing pre-compile for the bn256 curve
827873
// conforming to Istanbul consensus rules.
828874
type bn256PairingIstanbul struct{}
@@ -986,6 +1032,25 @@ func (c *bls12381G1MultiExpIsthmus) Name() string {
9861032
return "BLS12_G1MSM"
9871033
}
9881034

1035+
type bls12381G1MultiExpJovian struct {
1036+
}
1037+
1038+
func (c *bls12381G1MultiExpJovian) RequiredGas(input []byte) uint64 {
1039+
return new(bls12381G1MultiExp).RequiredGas(input)
1040+
}
1041+
1042+
func (c *bls12381G1MultiExpJovian) Run(input []byte) ([]byte, error) {
1043+
if len(input) > int(params.Bls12381G1MulMaxInputSizeJovian) {
1044+
return nil, errBLS12381MaxG1Size
1045+
}
1046+
1047+
return new(bls12381G1MultiExp).Run(input)
1048+
}
1049+
1050+
func (c *bls12381G1MultiExpJovian) Name() string {
1051+
return "BLS12_G1MSM"
1052+
}
1053+
9891054
// bls12381G1MultiExp implements EIP-2537 G1MultiExp precompile for Prague (no size limits).
9901055
func (c *bls12381G1Add) Name() string {
9911056
return "BLS12_G1ADD"
@@ -1115,6 +1180,25 @@ func (c *bls12381G2MultiExpIsthmus) Name() string {
11151180
return "BLS12_G2MSM"
11161181
}
11171182

1183+
type bls12381G2MultiExpJovian struct {
1184+
}
1185+
1186+
func (c *bls12381G2MultiExpJovian) RequiredGas(input []byte) uint64 {
1187+
return new(bls12381G2MultiExp).RequiredGas(input)
1188+
}
1189+
1190+
func (c *bls12381G2MultiExpJovian) Run(input []byte) ([]byte, error) {
1191+
if len(input) > int(params.Bls12381G2MulMaxInputSizeJovian) {
1192+
return nil, errBLS12381MaxG2Size
1193+
}
1194+
1195+
return new(bls12381G2MultiExp).Run(input)
1196+
}
1197+
1198+
func (c *bls12381G2MultiExpJovian) Name() string {
1199+
return "BLS12_G2MSM"
1200+
}
1201+
11181202
// bls12381G2MultiExp implements EIP-2537 G2MultiExp precompile.
11191203
type bls12381G2MultiExp struct{}
11201204

@@ -1198,6 +1282,25 @@ func (c *bls12381PairingIsthmus) Name() string {
11981282
return "BLS12_PAIRING_CHECK"
11991283
}
12001284

1285+
type bls12381PairingJovian struct {
1286+
}
1287+
1288+
func (c *bls12381PairingJovian) RequiredGas(input []byte) uint64 {
1289+
return new(bls12381Pairing).RequiredGas(input)
1290+
}
1291+
1292+
func (c *bls12381PairingJovian) Run(input []byte) ([]byte, error) {
1293+
if len(input) > int(params.Bls12381PairingMaxInputSizeJovian) {
1294+
return nil, errBLS12381MaxPairingSize
1295+
}
1296+
1297+
return new(bls12381Pairing).Run(input)
1298+
}
1299+
1300+
func (c *bls12381PairingJovian) Name() string {
1301+
return "BLS12_PAIRING_CHECK"
1302+
}
1303+
12011304
// bls12381Pairing implements EIP-2537 Pairing precompile.
12021305
type bls12381Pairing struct{}
12031306

core/vm/contracts_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ var allPrecompiles = map[common.Address]PrecompiledContract{
7474
common.BytesToAddress([]byte{0x0b}): &p256Verify{},
7575

7676
common.BytesToAddress([]byte{0x01, 0x00}): &p256VerifyFjord{},
77+
78+
common.BytesToAddress([]byte{0x2f, 0x08}): &bn256PairingJovian{},
79+
common.BytesToAddress([]byte{0x2f, 0x0e}): &bls12381PairingJovian{},
80+
common.BytesToAddress([]byte{0x2f, 0x0b}): &bls12381G1MultiExpJovian{},
81+
common.BytesToAddress([]byte{0x2f, 0x0d}): &bls12381G2MultiExpJovian{},
7782
}
7883

7984
// EIP-152 test vectors

params/protocol_params.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ const (
185185
Bls12381G2MulMaxInputSizeIsthmus uint64 = 488448 // Maximum input size for BLS12-381 G2 multiple-scalar-multiply operation
186186
Bls12381PairingMaxInputSizeIsthmus uint64 = 235008 // Maximum input size for BLS12-381 pairing check
187187

188+
Bn256PairingMaxInputSizeJovian uint64 = 81984 // bn256Pairing limit (427 pairs)
189+
Bls12381G1MulMaxInputSizeJovian uint64 = 288960 // BLS12-381 G1 MSM limit (1,806 pairs)
190+
Bls12381G2MulMaxInputSizeJovian uint64 = 278784 // BLS12-381 G2 MSM limit (968 pairs)
191+
Bls12381PairingMaxInputSizeJovian uint64 = 156672 // BLS12-381 pairing limit (408 pairs)
192+
188193
// The Refund Quotient is the cap on how much of the used gas can be refunded. Before EIP-3529,
189194
// up to half the consumed gas could be refunded. Redefined as 1/5th in EIP-3529
190195
RefundQuotient uint64 = 2

0 commit comments

Comments
 (0)