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