-
Notifications
You must be signed in to change notification settings - Fork 433
/
big_int.go
55 lines (44 loc) · 932 Bytes
/
big_int.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
package internal
import (
"fmt"
"math/big"
)
type BigInt struct {
big.Int
}
func (b BigInt) DeepCopyInto(n *BigInt) {
n.Int.FillBytes(b.Int.Bytes())
}
func (b BigInt) Equal(n BigInt) bool {
return b.Cmp(n) == 0
}
func (b BigInt) EqualInt64(n int64) bool {
return b.Int.Cmp(big.NewInt(n)) == 0
}
func (b BigInt) Cmp(n BigInt) int {
return b.Int.Cmp(&n.Int)
}
func (b BigInt) Add(n BigInt) BigInt {
return BigInt{*big.NewInt(0).Add(&b.Int, &n.Int)}
}
func (b BigInt) Sub(n BigInt) BigInt {
return BigInt{*big.NewInt(0).Sub(&b.Int, &n.Int)}
}
func (b BigInt) String() string {
return b.Int.String()
}
func (b BigInt) MarshalJSON() ([]byte, error) {
return []byte(b.String()), nil
}
func (b *BigInt) UnmarshalJSON(p []byte) error {
if string(p) == "null" {
return nil
}
var z big.Int
_, ok := z.SetString(string(p), 10)
if !ok {
return fmt.Errorf("invalid big integer: %q", p)
}
b.Int = z
return nil
}