@@ -28,13 +28,9 @@ import (
28
28
29
29
var (
30
30
// MaxUint256 is the maximum value that can be represented by a uint256
31
- MaxUint256 = big .NewInt (0 ).Add (
32
- big .NewInt (0 ).Exp (big .NewInt (2 ), big .NewInt (256 ), nil ),
33
- big .NewInt (- 1 ))
31
+ MaxUint256 = new (big.Int ).Sub (new (big.Int ).Lsh (common .Big1 , 256 ), common .Big1 )
34
32
// MaxInt256 is the maximum value that can be represented by a int256
35
- MaxInt256 = big .NewInt (0 ).Add (
36
- big .NewInt (0 ).Exp (big .NewInt (2 ), big .NewInt (255 ), nil ),
37
- big .NewInt (- 1 ))
33
+ MaxInt256 = new (big.Int ).Sub (new (big.Int ).Lsh (common .Big1 , 255 ), common .Big1 )
38
34
)
39
35
40
36
// ReadInteger reads the integer based on its kind and returns the appropriate value
@@ -57,17 +53,17 @@ func ReadInteger(typ byte, kind reflect.Kind, b []byte) interface{} {
57
53
case reflect .Int64 :
58
54
return int64 (binary .BigEndian .Uint64 (b [len (b )- 8 :]))
59
55
default :
60
- // the only case lefts for integer is int256/uint256.
61
- // big.SetBytes can't tell if a number is negative, positive on itself.
62
- // On EVM, if the returned number > max int256, it is negative.
56
+ // the only case left for integer is int256/uint256.
63
57
ret := new (big.Int ).SetBytes (b )
64
58
if typ == UintTy {
65
59
return ret
66
60
}
67
-
68
- if ret .Cmp (MaxInt256 ) > 0 {
69
- ret .Add (MaxUint256 , big .NewInt (0 ).Neg (ret ))
70
- ret .Add (ret , big .NewInt (1 ))
61
+ // big.SetBytes can't tell if a number is negative or positive in itself.
62
+ // On EVM, if the returned number > max int256, it is negative.
63
+ // A number is > max int256 if the bit at position 255 is set.
64
+ if ret .Bit (255 ) == 1 {
65
+ ret .Add (MaxUint256 , new (big.Int ).Neg (ret ))
66
+ ret .Add (ret , common .Big1 )
71
67
ret .Neg (ret )
72
68
}
73
69
return ret
0 commit comments