@@ -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
227229var (
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.
243276func (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
0 commit comments