This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
uint128.go
269 lines (234 loc) · 6.82 KB
/
uint128.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
// Copyright (C) 2018 MediBloc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package util
import (
"errors"
"math/big"
"sync"
)
const (
// Uint128Bytes defines the number of bytes for Uint128 type.
Uint128Bytes = 16
// Uint128Bits defines the number of bits for Uint128 type.
Uint128Bits = 128
)
var (
// ErrUint128Overflow indicates the value is greater than uint128 maximum value 2^128.
ErrUint128Overflow = errors.New("uint128: overflow")
// ErrUint128Underflow indicates the value is smaller then uint128 minimum value 0.
ErrUint128Underflow = errors.New("uint128: underflow")
// ErrUint128InvalidBytesSize indicates the bytes size is not equal to Uint128Bytes.
ErrUint128InvalidBytesSize = errors.New("uint128: invalid bytes")
// ErrUint128InvalidString indicates the string is not valid when converted to uin128.
ErrUint128InvalidString = errors.New("uint128: invalid string to uint128")
)
// Uint128 defines uint128 type, based on big.Int.
//
// For arithmetic operations, use uint128.Int.Add()/Sub()/Mul()/Div()/etc.
// For example, u1.Add(u1.Int, u2.Int) sets u1 to u1 + u2.
type Uint128 struct {
value *big.Int
}
// Validate returns error if u is not a valid uint128, otherwise returns nil.
func (u *Uint128) Validate() error {
if u.value.Sign() < 0 {
return ErrUint128Underflow
}
if u.value.BitLen() > Uint128Bits {
return ErrUint128Overflow
}
return nil
}
// NewUint128 returns a new Uint128 struct with default value.
func NewUint128() *Uint128 {
return &Uint128{big.NewInt(0)}
}
// NewUint128FromString returns a new Uint128 struct with given value and have a check.
func NewUint128FromString(str string) (*Uint128, error) {
Big := new(big.Int)
_, success := Big.SetString(str, 10)
if !success {
return nil, ErrUint128InvalidString
}
if err := (&Uint128{Big}).Validate(); nil != err {
return nil, err
}
return &Uint128{Big}, nil
}
// NewUint128FromUint returns a new Uint128 with given value
func NewUint128FromUint(i uint64) *Uint128 {
obj := NewUint128()
obj.value.SetUint64(i)
return obj
}
// Uint128Zero zero of uint128
func Uint128Zero() *Uint128 {
return NewUint128FromUint(0)
}
// NewUint128FromInt returns a new Uint128 struct with given value and have a check.
func NewUint128FromInt(i int64) (*Uint128, error) {
obj := &Uint128{big.NewInt(i)}
if err := obj.Validate(); nil != err {
return nil, err
}
return obj, nil
}
// NewUint128FromBigInt returns a new Uint128 struct with given value and have a check.
func NewUint128FromBigInt(Big *big.Int) (*Uint128, error) {
if err := (&Uint128{Big}).Validate(); err != nil {
return nil, err
}
return &Uint128{Big}, nil
}
// BigInt returns the big int representation of x.
func (u *Uint128) BigInt() *big.Int {
return u.value
}
// ToFixedSizeBytes converts Uint128 to Big-Endian fixed size bytes.
func (u *Uint128) ToFixedSizeBytes() ([16]byte, error) {
var res [16]byte
if err := u.Validate(); err != nil {
return res, err
}
bs := u.value.Bytes()
l := len(bs)
if l == 0 {
return res, nil
}
idx := Uint128Bytes - len(bs)
if idx < Uint128Bytes {
copy(res[idx:], bs)
}
return res, nil
}
// ToFixedSizeByteSlice converts Uint128 to Big-Endian fixed size byte slice.
func (u *Uint128) ToFixedSizeByteSlice() ([]byte, error) {
bytes, err := u.ToFixedSizeBytes()
return bytes[:], err
}
// String returns the string representation of x.
func (u *Uint128) String() string {
return u.value.Text(10)
}
// FromFixedSizeBytes converts Big-Endian fixed size bytes to Uint128.
func (u *Uint128) FromFixedSizeBytes(bytes [16]byte) (*Uint128, error) {
if err := u.FromFixedSizeByteSlice(bytes[:]); err != nil {
return nil, err
}
return u, nil
}
// FromFixedSizeByteSlice converts Big-Endian fixed size bytes to Uint128.
func (u *Uint128) FromFixedSizeByteSlice(bytes []byte) error {
if len(bytes) != Uint128Bytes {
return ErrUint128InvalidBytesSize
}
i := 0
for ; i < Uint128Bytes; i++ {
if bytes[i] != 0 {
break
}
}
if i < Uint128Bytes {
u.value.SetBytes(bytes[i:])
} else {
u.value.SetUint64(0)
}
return nil
}
// NewUint128FromFixedSizeByteSlice returns a new Uint128 struct with given fixed size byte slice.
func NewUint128FromFixedSizeByteSlice(bytes []byte) (*Uint128, error) {
u := NewUint128()
err := u.FromFixedSizeByteSlice(bytes)
if err != nil {
return nil, err
}
return u, nil
}
// Add returns u + x
func (u *Uint128) Add(x *Uint128) (*Uint128, error) {
obj := &Uint128{NewUint128().value.Add(u.value, x.value)}
if err := obj.Validate(); nil != err {
return u, err
}
return obj, nil
}
// Sub returns u - x
func (u *Uint128) Sub(x *Uint128) (*Uint128, error) {
obj := &Uint128{NewUint128().value.Sub(u.value, x.value)}
if err := obj.Validate(); nil != err {
return u, err
}
return obj, nil
}
// Mul returns u * x
func (u *Uint128) Mul(x *Uint128) (*Uint128, error) {
obj := &Uint128{NewUint128().value.Mul(u.value, x.value)}
if err := obj.Validate(); nil != err {
return u, err
}
return obj, nil
}
// Div returns u / x
func (u *Uint128) Div(x *Uint128) (*Uint128, error) {
obj := &Uint128{NewUint128().value.Div(u.value, x.value)}
if err := obj.Validate(); nil != err {
return u, err
}
return obj, nil
}
// Exp returns u^x
func (u *Uint128) Exp(x *Uint128) (*Uint128, error) {
obj := &Uint128{NewUint128().value.Exp(u.value, x.value, nil)}
if err := obj.Validate(); nil != err {
return u, err
}
return obj, nil
}
// DeepCopy returns a deep copy of u
func (u *Uint128) DeepCopy() *Uint128 {
z := new(big.Int)
z.Set(u.value)
return &Uint128{z}
}
// Cmp compares u and x and returns:
//
// -1 if u < x
// 0 if u == x
// +1 if u > x
func (u *Uint128) Cmp(x *Uint128) int {
return u.value.Cmp(x.value)
}
// Bytes absolute value of u as a big-endian byte slice.
func (u *Uint128) Bytes() []byte {
return u.value.Bytes()
}
// Uint64 returns the uint64 representation of x.
// If x cannot be represented in a uint64, the result is undefined.
func (u *Uint128) Uint64() uint64 {
return u.value.Uint64()
}
var mu sync.Mutex
// MulWithRat multiply uin128 with big.Rat
func (u *Uint128) MulWithRat(x *big.Rat) (*Uint128, error) {
mu.Lock()
defer mu.Unlock()
tmp := big.NewInt(0).Mul(u.value, x.Num())
tmp = big.NewInt(0).Div(tmp, x.Denom())
obj := &Uint128{tmp}
if err := obj.Validate(); nil != err {
return u, err
}
return obj, nil
}