-
Notifications
You must be signed in to change notification settings - Fork 14
/
bn_utils.go
76 lines (58 loc) · 1.3 KB
/
bn_utils.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
package otr3
import (
"math/big"
"github.com/coyim/constbn"
)
func modExp(g, x, m *big.Int) *big.Int {
return new(big.Int).Exp(g, x, m)
}
func modExpP(g, x *big.Int) *big.Int {
return modExp(g, x, p)
}
func modExpCT(g *constbn.Int, x secretKeyValue, m *constbn.Int) *constbn.Int {
return new(constbn.Int).ExpB(g, []byte(x), m)
}
func modExpPCT(g *constbn.Int, x secretKeyValue) *constbn.Int {
return modExpCT(g, x, pct)
}
func modInverse(g, x *big.Int) *big.Int {
return new(big.Int).ModInverse(g, x)
}
func mul(l, r *big.Int) *big.Int {
return new(big.Int).Mul(l, r)
}
func sub(l, r *big.Int) *big.Int {
return new(big.Int).Sub(l, r)
}
func mulMod(l, r, m *big.Int) *big.Int {
res := mul(l, r)
res.Mod(res, m)
return res
}
// Fast division over a modular field, without using division
func divMod(l, r, m *big.Int) *big.Int {
return mulMod(l, modInverse(r, m), m)
}
func subMod(l, r, m *big.Int) *big.Int {
res := sub(l, r)
res.Mod(res, m)
return res
}
func mod(l, m *big.Int) *big.Int {
return new(big.Int).Mod(l, m)
}
func lt(l, r *big.Int) bool {
return l.Cmp(r) == -1
}
func lte(l, r *big.Int) bool {
return l.Cmp(r) != 1
}
func eq(l, r *big.Int) bool {
return l.Cmp(r) == 0
}
func gt(l, r *big.Int) bool {
return l.Cmp(r) == 1
}
func gte(l, r *big.Int) bool {
return l.Cmp(r) != -1
}