Skip to content

Commit 6f4cccf

Browse files
authored
core/vm, protocol_params: implement eip-2565 modexp repricing (#21607)
* core/vm, protocol_params: implement eip-2565 modexp repricing * core/vm: fix review concerns
1 parent 0703c91 commit 6f4cccf

4 files changed

Lines changed: 209 additions & 20 deletions

File tree

‎core/vm/contracts.go‎

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{
5858
common.BytesToAddress([]byte{2}): &sha256hash{},
5959
common.BytesToAddress([]byte{3}): &ripemd160hash{},
6060
common.BytesToAddress([]byte{4}): &dataCopy{},
61-
common.BytesToAddress([]byte{5}): &bigModExp{},
61+
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false},
6262
common.BytesToAddress([]byte{6}): &bn256AddByzantium{},
6363
common.BytesToAddress([]byte{7}): &bn256ScalarMulByzantium{},
6464
common.BytesToAddress([]byte{8}): &bn256PairingByzantium{},
@@ -71,7 +71,7 @@ var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
7171
common.BytesToAddress([]byte{2}): &sha256hash{},
7272
common.BytesToAddress([]byte{3}): &ripemd160hash{},
7373
common.BytesToAddress([]byte{4}): &dataCopy{},
74-
common.BytesToAddress([]byte{5}): &bigModExp{},
74+
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false},
7575
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
7676
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
7777
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
@@ -85,7 +85,7 @@ var PrecompiledContractsYoloV2 = map[common.Address]PrecompiledContract{
8585
common.BytesToAddress([]byte{2}): &sha256hash{},
8686
common.BytesToAddress([]byte{3}): &ripemd160hash{},
8787
common.BytesToAddress([]byte{4}): &dataCopy{},
88-
common.BytesToAddress([]byte{5}): &bigModExp{},
88+
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false},
8989
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
9090
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
9191
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
@@ -222,14 +222,19 @@ func (c *dataCopy) Run(in []byte) ([]byte, error) {
222222
}
223223

224224
// bigModExp implements a native big integer exponential modular operation.
225-
type bigModExp struct{}
225+
type bigModExp struct {
226+
eip2565 bool
227+
}
226228

227229
var (
228230
big0 = big.NewInt(0)
229231
big1 = big.NewInt(1)
232+
big3 = big.NewInt(3)
230233
big4 = big.NewInt(4)
234+
big7 = big.NewInt(7)
231235
big8 = big.NewInt(8)
232236
big16 = big.NewInt(16)
237+
big20 = big.NewInt(20)
233238
big32 = big.NewInt(32)
234239
big64 = big.NewInt(64)
235240
big96 = big.NewInt(96)
@@ -239,6 +244,34 @@ var (
239244
big199680 = big.NewInt(199680)
240245
)
241246

247+
// modexpMultComplexity implements bigModexp multComplexity formula, as defined in EIP-198
248+
//
249+
// def mult_complexity(x):
250+
// if x <= 64: return x ** 2
251+
// elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072
252+
// else: return x ** 2 // 16 + 480 * x - 199680
253+
//
254+
// where is x is max(length_of_MODULUS, length_of_BASE)
255+
func modexpMultComplexity(x *big.Int) *big.Int {
256+
switch {
257+
case x.Cmp(big64) <= 0:
258+
x.Mul(x, x) // x ** 2
259+
case x.Cmp(big1024) <= 0:
260+
// (x ** 2 // 4 ) + ( 96 * x - 3072)
261+
x = new(big.Int).Add(
262+
new(big.Int).Div(new(big.Int).Mul(x, x), big4),
263+
new(big.Int).Sub(new(big.Int).Mul(big96, x), big3072),
264+
)
265+
default:
266+
// (x ** 2 // 16) + (480 * x - 199680)
267+
x = new(big.Int).Add(
268+
new(big.Int).Div(new(big.Int).Mul(x, x), big16),
269+
new(big.Int).Sub(new(big.Int).Mul(big480, x), big199680),
270+
)
271+
}
272+
return x
273+
}
274+
242275
// RequiredGas returns the gas required to execute the pre-compiled contract.
243276
func (c *bigModExp) RequiredGas(input []byte) uint64 {
244277
var (
@@ -273,25 +306,36 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
273306
adjExpLen.Mul(big8, adjExpLen)
274307
}
275308
adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
276-
277309
// Calculate the gas cost of the operation
278310
gas := new(big.Int).Set(math.BigMax(modLen, baseLen))
279-
switch {
280-
case gas.Cmp(big64) <= 0:
311+
if c.eip2565 {
312+
// EIP-2565 has three changes
313+
// 1. Different multComplexity (inlined here)
314+
// in EIP-2565 (https://eips.ethereum.org/EIPS/eip-2565):
315+
//
316+
// def mult_complexity(x):
317+
// ceiling(x/8)^2
318+
//
319+
//where is x is max(length_of_MODULUS, length_of_BASE)
320+
gas = gas.Add(gas, big7)
321+
gas = gas.Div(gas, big8)
281322
gas.Mul(gas, gas)
282-
case gas.Cmp(big1024) <= 0:
283-
gas = new(big.Int).Add(
284-
new(big.Int).Div(new(big.Int).Mul(gas, gas), big4),
285-
new(big.Int).Sub(new(big.Int).Mul(big96, gas), big3072),
286-
)
287-
default:
288-
gas = new(big.Int).Add(
289-
new(big.Int).Div(new(big.Int).Mul(gas, gas), big16),
290-
new(big.Int).Sub(new(big.Int).Mul(big480, gas), big199680),
291-
)
323+
324+
gas.Mul(gas, math.BigMax(adjExpLen, big1))
325+
// 2. Different divisor (`GQUADDIVISOR`) (3)
326+
gas.Div(gas, big3)
327+
if gas.BitLen() > 64 {
328+
return math.MaxUint64
329+
}
330+
// 3. Minimum price of 200 gas
331+
if gas.Uint64() < 200 {
332+
return 200
333+
}
334+
return gas.Uint64()
292335
}
336+
gas = modexpMultComplexity(gas)
293337
gas.Mul(gas, math.BigMax(adjExpLen, big1))
294-
gas.Div(gas, new(big.Int).SetUint64(params.ModExpQuadCoeffDiv))
338+
gas.Div(gas, big20)
295339

296340
if gas.BitLen() > 64 {
297341
return math.MaxUint64

‎core/vm/contracts_test.go‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,29 @@ type precompiledFailureTest struct {
4343
Name string
4444
}
4545

46-
var allPrecompiles = PrecompiledContractsYoloV2
46+
// allPrecompiles does not map to the actual set of precompiles, as it also contains
47+
// repriced versions of precompiles at certain slots
48+
var allPrecompiles = map[common.Address]PrecompiledContract{
49+
common.BytesToAddress([]byte{1}): &ecrecover{},
50+
common.BytesToAddress([]byte{2}): &sha256hash{},
51+
common.BytesToAddress([]byte{3}): &ripemd160hash{},
52+
common.BytesToAddress([]byte{4}): &dataCopy{},
53+
common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false},
54+
common.BytesToAddress([]byte{0xf5}): &bigModExp{eip2565: true},
55+
common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
56+
common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
57+
common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
58+
common.BytesToAddress([]byte{9}): &blake2F{},
59+
common.BytesToAddress([]byte{10}): &bls12381G1Add{},
60+
common.BytesToAddress([]byte{11}): &bls12381G1Mul{},
61+
common.BytesToAddress([]byte{12}): &bls12381G1MultiExp{},
62+
common.BytesToAddress([]byte{13}): &bls12381G2Add{},
63+
common.BytesToAddress([]byte{14}): &bls12381G2Mul{},
64+
common.BytesToAddress([]byte{15}): &bls12381G2MultiExp{},
65+
common.BytesToAddress([]byte{16}): &bls12381Pairing{},
66+
common.BytesToAddress([]byte{17}): &bls12381MapG1{},
67+
common.BytesToAddress([]byte{18}): &bls12381MapG2{},
68+
}
4769

4870
// EIP-152 test vectors
4971
var blake2FMalformedInputTests = []precompiledFailureTest{
@@ -213,6 +235,9 @@ func BenchmarkPrecompiledIdentity(bench *testing.B) {
213235
func TestPrecompiledModExp(t *testing.T) { testJson("modexp", "05", t) }
214236
func BenchmarkPrecompiledModExp(b *testing.B) { benchJson("modexp", "05", b) }
215237

238+
func TestPrecompiledModExpEip2565(t *testing.T) { testJson("modexp_eip2565", "f5", t) }
239+
func BenchmarkPrecompiledModExpEip2565(b *testing.B) { benchJson("modexp_eip2565", "f5", b) }
240+
216241
// Tests the sample inputs from the elliptic curve addition EIP 213.
217242
func TestPrecompiledBn256Add(t *testing.T) { testJson("bn256Add", "06", t) }
218243
func BenchmarkPrecompiledBn256Add(b *testing.B) { benchJson("bn256Add", "06", b) }

0 commit comments

Comments
 (0)