-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
big_math.go
69 lines (56 loc) · 1.71 KB
/
big_math.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
// Package bigmath compensates for awkward big.Int API. Can cause an extra allocation or two.
package bigmath
import (
"math/big"
)
// I returns a new big.Int.
func I() *big.Int { return new(big.Int) }
// Add performs addition with the given values.
func Add(addend1, addend2 *big.Int) *big.Int { return I().Add(addend1, addend2) }
// Div performs division with the given values.
func Div(dividend, divisor *big.Int) *big.Int { return I().Div(dividend, divisor) }
// Equal compares the given values.
func Equal(left, right *big.Int) bool { return left.Cmp(right) == 0 }
// Exp performs modular eponentiation with the given values.
func Exp(base, exponent, modulus *big.Int) *big.Int {
return I().Exp(base, exponent, modulus)
}
// Mul performs multiplication with the given values.
func Mul(multiplicand, multiplier *big.Int) *big.Int {
return I().Mul(multiplicand, multiplier)
}
// Mod performs modulus with the given values.
func Mod(dividend, divisor *big.Int) *big.Int { return I().Mod(dividend, divisor) }
// Sub performs subtraction with the given values.
func Sub(minuend, subtrahend *big.Int) *big.Int { return I().Sub(minuend, subtrahend) }
// Max returns the maximum of the two given values.
func Max(x, y *big.Int) *big.Int {
if x.Cmp(y) == 1 {
return x
}
return y
}
// Min returns the min of the two given values.
func Min(x, y *big.Int) *big.Int {
if x.Cmp(y) == -1 {
return x
}
return y
}
// Accumulate returns the sum of the given slice.
func Accumulate(s []*big.Int) (r *big.Int) {
r = big.NewInt(0)
for _, e := range s {
r.Add(r, e)
}
return
}
// nolint
var (
Zero = big.NewInt(0)
One = big.NewInt(1)
Two = big.NewInt(2)
Three = big.NewInt(3)
Four = big.NewInt(4)
Seven = big.NewInt(7)
)