-
Notifications
You must be signed in to change notification settings - Fork 1
/
num.go
278 lines (246 loc) · 6.46 KB
/
num.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package abi
import (
"fmt"
"math"
"math/big"
"math/bits"
)
var (
// MaxUint contains the maximum unsigned integer for each bit size.
MaxUint = map[int]*big.Int{}
// MaxInt contains the maximum signed integer for each bit size.
MaxInt = map[int]*big.Int{}
// MinInt contains the minimum signed integer for each bit size.
MinInt = map[int]*big.Int{}
)
// intX represents a signed integer of bit size between 8 and 256.
type intX struct {
size int
val *big.Int
}
// newIntX creates a new intX value.
func newIntX(bitSize int) *intX {
if bitSize < 8 || bitSize > 256 || bitSize%8 != 0 {
panic("abi: invalid bit size for intX")
}
return &intX{
size: bitSize,
val: new(big.Int),
}
}
// BitSize returns the bit size of the integer.
func (i *intX) BitSize() int {
return i.size
}
// BitLen returns the number of bits required to represent x.
func (i *intX) BitLen() int {
return signedBitLen(i.val)
}
func (i *intX) IsInt() bool {
if !i.val.IsInt64() {
return false
}
x := i.val.Int64()
if x > math.MaxInt {
return false
}
if x < math.MinInt {
return false
}
return true
}
func (i *intX) Int() (int, error) {
if !i.val.IsInt64() {
return 0, fmt.Errorf("abi: int overflow")
}
x := i.val.Int64()
if x > math.MaxInt {
return 0, fmt.Errorf("abi: int overflow")
}
if x < math.MinInt {
return 0, fmt.Errorf("abi: int overflow")
}
return int(i.val.Int64()), nil
}
func (i *intX) Int64() (int64, error) {
if !i.val.IsInt64() {
return 0, fmt.Errorf("abi: int64 overflow")
}
return i.val.Int64(), nil
}
// BigInt returns the value of the integer as a big integer.
func (i *intX) BigInt() *big.Int {
return i.val
}
// Bytes returns the value of the integer as a big-endian byte slice.
// The byte slice is zero-padded to the size of the integer. Negative
// values are two's complement encoded.
func (i *intX) Bytes() []byte {
r := make([]byte, i.size/8)
x := new(big.Int).Set(i.val).And(i.val, MaxUint[i.size])
padLeft(r, x.Bytes())
return r
}
func (i *intX) SetInt(x int) error {
if bits.Len(uint(x)) > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit int", bits.Len(uint(x)), i.size)
}
i.val.SetInt64(int64(x))
return nil
}
func (i *intX) SetInt64(x int64) error {
if bits.Len64(uint64(x)) > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit int64", bits.Len64(uint64(x)), i.size)
}
i.val.SetInt64(x)
return nil
}
// SetBigInt sets the value of the integer to x. If x is larger than the
// integer's bit size, an error is returned.
func (i *intX) SetBigInt(x *big.Int) error {
if x == nil || x.Sign() == 0 {
i.val = big.NewInt(0)
return nil
}
if signedBitLen(x) > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit signed int", signedBitLen(x), i.size)
}
i.val.Set(x)
return nil
}
// SetBytes sets the value of the integer to x. If x is larger than the
// integer's bit size, an error is returned.
func (i *intX) SetBytes(b []byte) error {
x := new(big.Int).SetBytes(b)
if x.Cmp(MaxInt[i.size]) > 0 {
// If the number is negative, we need to set it from the two's complement
// representation.
x.Add(MaxUint[i.size], new(big.Int).Neg(x))
x.Add(x, big.NewInt(1))
x.Neg(x)
}
return i.SetBigInt(x)
}
// uintX represents a unsigned integer of bit size between 8 and 256.
type uintX struct {
size int
val *big.Int
}
// newUintX creates a new uintX value.
func newUintX(bitSize int) *uintX {
if bitSize < 8 || bitSize > 256 || bitSize%8 != 0 {
panic("abi: invalid bit size for intX")
}
return &uintX{
size: bitSize,
val: new(big.Int),
}
}
func (i *uintX) Uint() (int, error) {
if !i.val.IsUint64() {
return 0, fmt.Errorf("abi: uint overflow")
}
x := i.val.Uint64()
if x > math.MaxUint {
return 0, fmt.Errorf("abi: uint overflow")
}
return int(i.val.Uint64()), nil
}
func (i *uintX) Uint64() (uint64, error) {
if !i.val.IsUint64() {
return 0, fmt.Errorf("abi: int64 overflow")
}
return i.val.Uint64(), nil
}
// BigInt returns the value of the integer as a big integer.
func (i *uintX) BigInt() *big.Int {
return i.val
}
// Bytes returns the value of the integer as a big-endian byte slice.
// The byte slice is zero-padded to the size of the integer. Negative
// values are two's complement encoded.
func (i *uintX) Bytes() []byte {
r := make([]byte, i.size/8)
padLeft(r, i.val.Bytes())
return r
}
func (i *uintX) SetUint(x uint) error {
if bits.Len(x) > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit int", bits.Len(x), i.size)
}
i.val.SetUint64(uint64(x))
return nil
}
func (i *uintX) SetUint64(x uint64) error {
if bits.Len64(x) > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit int64", bits.Len64(x), i.size)
}
i.val.SetUint64(x)
return nil
}
// SetBigInt sets the value of the integer to x. If x is larger than the
// integer's bit size, an error is returned.
func (i *uintX) SetBigInt(x *big.Int) error {
if x == nil || x.Sign() == 0 {
i.val = big.NewInt(0)
return nil
}
if x.BitLen() > i.size {
return fmt.Errorf("abi: cannot set %d-bit integer to %d-bit signed int", signedBitLen(x), i.size)
}
i.val.Set(x)
return nil
}
// SetBytes sets the value of the integer to x. If x is larger than the
// integer's bit size, an error is returned.
func (i *uintX) SetBytes(b []byte) error {
return i.SetBigInt(new(big.Int).SetBytes(b))
}
func padLeft(dst []byte, src []byte) {
copy(dst[len(dst)-len(src):], src)
}
// signedBitLen returns the number of bits required to represent x in two's
// complement representation.
func signedBitLen(x *big.Int) int {
if x == nil || x.Sign() == 0 {
return 0
}
bitLen := x.BitLen()
if x.Sign() < 0 && x.TrailingZeroBits() == uint(bitLen-1) {
// If the binary representation of the number is equal to x^2, then the
// bit length for the negative number encoded in two's complement is
// one bit shorter.
return bitLen
}
return bitLen + 1
}
func canSetInt(x int64, bitLen int) bool {
if bitLen >= 64 {
return true
}
if bitLen <= 0 {
return false
}
if x < 0 {
return x >= -1<<(bitLen-1)
}
return x < (1 << uint(bitLen-1))
}
func canSetUint(x uint64, bitLen int) bool {
if bitLen >= 64 {
return true
}
if bitLen <= 0 {
return false
}
return x < (1 << uint(bitLen))
}
func init() {
pOne := big.NewInt(1)
mOne := big.NewInt(-1)
for i := 8; i <= 256; i += 8 {
MaxUint[i] = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), uint(i)), pOne)
MaxInt[i] = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), uint(i-1)), pOne)
MinInt[i] = new(big.Int).Lsh(mOne, uint(i-1))
}
}