@@ -30,7 +30,7 @@ import (
30
30
"github.com/ethereum/go-ethereum/crypto/bn256"
31
31
"github.com/ethereum/go-ethereum/params"
32
32
33
- //lint:ignore SA1019 Needed for precompile
33
+ big2 "github.com/holiman/big"
34
34
"golang.org/x/crypto/ripemd160"
35
35
)
36
36
@@ -266,9 +266,10 @@ var (
266
266
// modexpMultComplexity implements bigModexp multComplexity formula, as defined in EIP-198
267
267
//
268
268
// def mult_complexity(x):
269
- // if x <= 64: return x ** 2
270
- // elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072
271
- // else: return x ** 2 // 16 + 480 * x - 199680
269
+ //
270
+ // if x <= 64: return x ** 2
271
+ // elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072
272
+ // else: return x ** 2 // 16 + 480 * x - 199680
272
273
//
273
274
// where is x is max(length_of_MODULUS, length_of_BASE)
274
275
func modexpMultComplexity (x * big.Int ) * big.Int {
@@ -379,15 +380,24 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
379
380
}
380
381
// Retrieve the operands and execute the exponentiation
381
382
var (
382
- base = new (big.Int ).SetBytes (getData (input , 0 , baseLen ))
383
- exp = new (big.Int ).SetBytes (getData (input , baseLen , expLen ))
384
- mod = new (big.Int ).SetBytes (getData (input , baseLen + expLen , modLen ))
383
+ base = new (big2.Int ).SetBytes (getData (input , 0 , baseLen ))
384
+ exp = new (big2.Int ).SetBytes (getData (input , baseLen , expLen ))
385
+ mod = new (big2.Int ).SetBytes (getData (input , baseLen + expLen , modLen ))
386
+ v []byte
385
387
)
386
- if mod .BitLen () == 0 {
388
+
389
+ switch {
390
+ case mod .BitLen () == 0 :
387
391
// Modulo 0 is undefined, return zero
388
392
return common .LeftPadBytes ([]byte {}, int (modLen )), nil
393
+ case base .BitLen () == 1 : // a bit length of 1 means it's 1 (or -1).
394
+ //If base == 1, then we can just return base % mod (if mod >= 1, which it is)
395
+ v = base .Mod (base , mod ).Bytes ()
396
+ default :
397
+ v = base .Exp (base , exp , mod ).Bytes ()
389
398
}
390
- return common .LeftPadBytes (base .Exp (base , exp , mod ).Bytes (), int (modLen )), nil
399
+
400
+ return common .LeftPadBytes (v , int (modLen )), nil
391
401
}
392
402
393
403
// newCurvePoint unmarshals a binary blob into a bn256 elliptic curve point,
0 commit comments