-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigint.go
143 lines (119 loc) · 2.83 KB
/
bigint.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
// Package bigint implements some helper functions for big.Int
package bigint
import (
"math/big"
)
var zero = big.NewInt(0)
const (
cmpLt int = -1
cmpEq int = 0
cmpGt int = 1
cmpNil int = -42
)
// Add returns the result of adding the values of params x and y.
// Notes:
// - If x == nil && y == nil, returns nil.
// - If x != nil && y == nil, returns x.
// - If x == nil && y != nil, returns y.
func Add(x, y *big.Int) *big.Int {
if x == nil {
if y != nil {
return y
}
return nil
} else if y == nil {
if x != nil {
return x
}
return nil
}
return newBigInt().Add(x, y)
}
// Sub returns the result of subtracting the value of param y from the value of param x.
// Notes:
// - If x == nil, returns nil.
// - If x != nil && y == nil, returns x.
func Sub(x, y *big.Int) *big.Int {
if x == nil {
return nil
} else if x != nil && y == nil {
return x
}
return newBigInt().Sub(x, y)
}
// Mul returns the result of multiplying the values of params x and y.
// Notes:
// - If x == nil, returns nil.
// - If x != nil && y == nil, returns x.
func Mul(x, y *big.Int) *big.Int {
if x == nil {
return nil
} else if x != nil && y == nil {
return x
}
return newBigInt().Mul(x, y)
}
// Div returns the result of dividing the value of x by the value of y.
// Notes:
// - If x == nil, returns nil.
// - If x != nil && y == nil, returns x.
func Div(x, y *big.Int) *big.Int {
if x == nil {
return nil
} else if x != nil && y == nil {
return x
}
return newBigInt().Quo(x, y)
}
// Cmp wraps the Int.Cmp() function, returning the same values as the function it wraps:
// - -1 if x < y
// - 0 if x == y
// - 1 if x > y
// Note: returns -2 if either x or y == nil.
func Cmp(x, y *big.Int) int {
if x == nil || y == nil {
return cmpNil
}
return x.Cmp(y)
}
// Eq returns true if x == y.
// Returns false if either x or y == nil.
func Eq(x, y *big.Int) bool {
return Cmp(x, y) == cmpEq
}
// Lt returns true if x < y.
// Returns false if either x or y == nil.
func Lt(x, y *big.Int) bool {
return Cmp(x, y) == cmpLt
}
// Lte returns true if x <= y.
// Returns false if either x or y == nil.
func Lte(x, y *big.Int) bool {
return Lt(x, y) || Eq(x, y)
}
// Gt returns true if x > y.
// Returns false if either x or y == nil.
func Gt(x, y *big.Int) bool {
return Cmp(x, y) == cmpGt
}
// Gte returns true if x >= y.
// Returns false if either x or y == nil.
func Gte(x, y *big.Int) bool {
return Gt(x, y) || Eq(x, y)
}
// IsZero returns true if n == 0.
// Returns nil if n == nil.
func IsZero(n *big.Int) bool {
return Eq(zero, n)
}
// FromInt64 returns a new *big.Int with its value set to n.
func FromInt64(n int64) *big.Int {
return newBigInt().SetInt64(n)
}
// FromUint64 returns a new *big.Int with its value set to n.
func FromUint64(n uint64) *big.Int {
return newBigInt().SetUint64(n)
}
func newBigInt() *big.Int {
return new(big.Int)
}