Skip to content

Commit 5e98c97

Browse files
authored
core/vm: implement EIP 7823 - Set upper bounds for MODEXP (#31818)
1 parent 5346b8f commit 5e98c97

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

core/vm/contracts.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var PrecompiledContractsByzantium = PrecompiledContracts{
6666
common.BytesToAddress([]byte{0x2}): &sha256hash{},
6767
common.BytesToAddress([]byte{0x3}): &ripemd160hash{},
6868
common.BytesToAddress([]byte{0x4}): &dataCopy{},
69-
common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false},
69+
common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false, eip7823: false},
7070
common.BytesToAddress([]byte{0x6}): &bn256AddByzantium{},
7171
common.BytesToAddress([]byte{0x7}): &bn256ScalarMulByzantium{},
7272
common.BytesToAddress([]byte{0x8}): &bn256PairingByzantium{},
@@ -79,7 +79,7 @@ var PrecompiledContractsIstanbul = PrecompiledContracts{
7979
common.BytesToAddress([]byte{0x2}): &sha256hash{},
8080
common.BytesToAddress([]byte{0x3}): &ripemd160hash{},
8181
common.BytesToAddress([]byte{0x4}): &dataCopy{},
82-
common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false},
82+
common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: false, eip7823: false},
8383
common.BytesToAddress([]byte{0x6}): &bn256AddIstanbul{},
8484
common.BytesToAddress([]byte{0x7}): &bn256ScalarMulIstanbul{},
8585
common.BytesToAddress([]byte{0x8}): &bn256PairingIstanbul{},
@@ -93,7 +93,7 @@ var PrecompiledContractsBerlin = PrecompiledContracts{
9393
common.BytesToAddress([]byte{0x2}): &sha256hash{},
9494
common.BytesToAddress([]byte{0x3}): &ripemd160hash{},
9595
common.BytesToAddress([]byte{0x4}): &dataCopy{},
96-
common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: true},
96+
common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: true, eip7823: false},
9797
common.BytesToAddress([]byte{0x6}): &bn256AddIstanbul{},
9898
common.BytesToAddress([]byte{0x7}): &bn256ScalarMulIstanbul{},
9999
common.BytesToAddress([]byte{0x8}): &bn256PairingIstanbul{},
@@ -107,7 +107,7 @@ var PrecompiledContractsCancun = PrecompiledContracts{
107107
common.BytesToAddress([]byte{0x2}): &sha256hash{},
108108
common.BytesToAddress([]byte{0x3}): &ripemd160hash{},
109109
common.BytesToAddress([]byte{0x4}): &dataCopy{},
110-
common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: true},
110+
common.BytesToAddress([]byte{0x5}): &bigModExp{eip2565: true, eip7823: false},
111111
common.BytesToAddress([]byte{0x6}): &bn256AddIstanbul{},
112112
common.BytesToAddress([]byte{0x7}): &bn256ScalarMulIstanbul{},
113113
common.BytesToAddress([]byte{0x8}): &bn256PairingIstanbul{},
@@ -122,7 +122,7 @@ var PrecompiledContractsPrague = PrecompiledContracts{
122122
common.BytesToAddress([]byte{0x02}): &sha256hash{},
123123
common.BytesToAddress([]byte{0x03}): &ripemd160hash{},
124124
common.BytesToAddress([]byte{0x04}): &dataCopy{},
125-
common.BytesToAddress([]byte{0x05}): &bigModExp{eip2565: true},
125+
common.BytesToAddress([]byte{0x05}): &bigModExp{eip2565: true, eip7823: false},
126126
common.BytesToAddress([]byte{0x06}): &bn256AddIstanbul{},
127127
common.BytesToAddress([]byte{0x07}): &bn256ScalarMulIstanbul{},
128128
common.BytesToAddress([]byte{0x08}): &bn256PairingIstanbul{},
@@ -141,6 +141,28 @@ var PrecompiledContractsBLS = PrecompiledContractsPrague
141141

142142
var PrecompiledContractsVerkle = PrecompiledContractsBerlin
143143

144+
// PrecompiledContractsOsaka contains the set of pre-compiled Ethereum
145+
// contracts used in the Osaka release.
146+
var PrecompiledContractsOsaka = PrecompiledContracts{
147+
common.BytesToAddress([]byte{0x01}): &ecrecover{},
148+
common.BytesToAddress([]byte{0x02}): &sha256hash{},
149+
common.BytesToAddress([]byte{0x03}): &ripemd160hash{},
150+
common.BytesToAddress([]byte{0x04}): &dataCopy{},
151+
common.BytesToAddress([]byte{0x05}): &bigModExp{eip2565: true, eip7823: true},
152+
common.BytesToAddress([]byte{0x06}): &bn256AddIstanbul{},
153+
common.BytesToAddress([]byte{0x07}): &bn256ScalarMulIstanbul{},
154+
common.BytesToAddress([]byte{0x08}): &bn256PairingIstanbul{},
155+
common.BytesToAddress([]byte{0x09}): &blake2F{},
156+
common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{},
157+
common.BytesToAddress([]byte{0x0b}): &bls12381G1Add{},
158+
common.BytesToAddress([]byte{0x0c}): &bls12381G1MultiExp{},
159+
common.BytesToAddress([]byte{0x0d}): &bls12381G2Add{},
160+
common.BytesToAddress([]byte{0x0e}): &bls12381G2MultiExp{},
161+
common.BytesToAddress([]byte{0x0f}): &bls12381Pairing{},
162+
common.BytesToAddress([]byte{0x10}): &bls12381MapG1{},
163+
common.BytesToAddress([]byte{0x11}): &bls12381MapG2{},
164+
}
165+
144166
var (
145167
PrecompiledAddressesPrague []common.Address
146168
PrecompiledAddressesCancun []common.Address
@@ -175,6 +197,8 @@ func activePrecompiledContracts(rules params.Rules) PrecompiledContracts {
175197
switch {
176198
case rules.IsVerkle:
177199
return PrecompiledContractsVerkle
200+
case rules.IsOsaka:
201+
return PrecompiledContractsOsaka
178202
case rules.IsPrague:
179203
return PrecompiledContractsPrague
180204
case rules.IsCancun:
@@ -317,6 +341,7 @@ func (c *dataCopy) Run(in []byte) ([]byte, error) {
317341
// bigModExp implements a native big integer exponential modular operation.
318342
type bigModExp struct {
319343
eip2565 bool
344+
eip7823 bool
320345
}
321346

322347
var (
@@ -456,6 +481,10 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
456481
if baseLen == 0 && modLen == 0 {
457482
return []byte{}, nil
458483
}
484+
// enforce size cap for inputs
485+
if c.eip7823 && max(baseLen, expLen, modLen) > 1024 {
486+
return nil, fmt.Errorf("one or more of base/exponent/modulus length exceeded 1024 bytes")
487+
}
459488
// Retrieve the operands and execute the exponentiation
460489
var (
461490
base = new(big.Int).SetBytes(getData(input, 0, baseLen))

0 commit comments

Comments
 (0)