-
Notifications
You must be signed in to change notification settings - Fork 171
/
modular_reduction.go
187 lines (139 loc) · 3.67 KB
/
modular_reduction.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
package ring
import (
"math/bits"
"github.com/tuneinsight/lattigo/v5/utils/bignum"
)
// MForm switches a to the Montgomery domain by computing
// a*2^64 mod q.
func MForm(a, q uint64, u []uint64) (r uint64) {
mhi, _ := bits.Mul64(a, u[1])
r = -(a*u[0] + mhi) * q
if r >= q {
r -= q
}
return
}
// MFormLazy switches a to the Montgomery domain by computing
// a*2^64 mod q in constant time.
// The result is between 0 and 2*q-1.
func MFormLazy(a, q uint64, u []uint64) (r uint64) {
mhi, _ := bits.Mul64(a, u[1])
r = -(a*u[0] + mhi) * q
return
}
// IMForm switches a from the Montgomery domain back to the
// standard domain by computing a*(1/2^64) mod q.
func IMForm(a, q, qInv uint64) (r uint64) {
r, _ = bits.Mul64(a*qInv, q)
r = q - r
if r >= q {
r -= q
}
return
}
// IMFormLazy switches a from the Montgomery domain back to the
// standard domain by computing a*(1/2^64) mod q in constant time.
// The result is between 0 and 2*q-1.
func IMFormLazy(a, q, qInv uint64) (r uint64) {
r, _ = bits.Mul64(a*qInv, q)
r = q - r
return
}
// MRedConstant computes the constant qInv = (q^-1) mod 2^64 required for MRed.
func MRedConstant(q uint64) (qInv uint64) {
qInv = 1
for i := 0; i < 63; i++ {
qInv *= q
q *= q
}
return
}
// MRed computes x * y * (1/2^64) mod q.
func MRed(x, y, q, qInv uint64) (r uint64) {
mhi, mlo := bits.Mul64(x, y)
hhi, _ := bits.Mul64(mlo*qInv, q)
r = mhi - hhi + q
if r >= q {
r -= q
}
return
}
// MRedLazy computes x * y * (1/2^64) mod q in constant time.
// The result is between 0 and 2*q-1.
func MRedLazy(x, y, q, qInv uint64) (r uint64) {
ahi, alo := bits.Mul64(x, y)
H, _ := bits.Mul64(alo*qInv, q)
r = ahi - H + q
return
}
// BRedConstant computes the constant for the BRed algorithm.
// Returns ((2^128)/q)/(2^64) and (2^128)/q mod 2^64.
func BRedConstant(q uint64) (constant []uint64) {
bigR := bignum.NewInt("0x100000000000000000000000000000000")
bigR.Quo(bigR, bignum.NewInt(q))
mlo := bigR.Uint64()
mhi := bigR.Rsh(bigR, 64).Uint64()
return []uint64{mhi, mlo}
}
// BRedAdd computes a mod q.
func BRedAdd(a, q uint64, u []uint64) (r uint64) {
mhi, _ := bits.Mul64(a, u[0])
r = a - mhi*q
if r >= q {
r -= q
}
return
}
// BRedAddLazy computes a mod q in constant time.
// The result is between 0 and 2*q-1.
func BRedAddLazy(x, q uint64, u []uint64) uint64 {
s0, _ := bits.Mul64(x, u[0])
return x - s0*q
}
// BRed computes x*y mod q.
func BRed(x, y, q uint64, u []uint64) (r uint64) {
var mhi, mlo, lhi, hhi, hlo, s0, carry uint64
mhi, mlo = bits.Mul64(x, y)
// computes r = mhi * uhi + (mlo * uhi + mhi * ulo)<<64 + (mlo * ulo)) >> 128
r = mhi * u[0] // r = mhi * uhi
hhi, hlo = bits.Mul64(mlo, u[0]) // mlo * uhi
r += hhi
lhi, _ = bits.Mul64(mlo, u[1]) // mlo * ulo
s0, carry = bits.Add64(hlo, lhi, 0)
r += carry
hhi, hlo = bits.Mul64(mhi, u[1]) // mhi * ulo
r += hhi
_, carry = bits.Add64(hlo, s0, 0)
r += carry
r = mlo - r*q
if r >= q {
r -= q
}
return
}
// BRedLazy computes x*y mod q in constant time.
// The result is between 0 and 2*q-1.
func BRedLazy(x, y, q uint64, u []uint64) (r uint64) {
var mhi, mlo, lhi, hhi, hlo, s0, carry uint64
mhi, mlo = bits.Mul64(x, y)
// computes r = mhi * uhi + (mlo * uhi + mhi * ulo)<<64 + (mlo * ulo)) >> 128
r = mhi * u[0] // r = mhi * uhi
hhi, hlo = bits.Mul64(mlo, u[0]) // mlo * uhi
r += hhi
lhi, _ = bits.Mul64(mlo, u[1]) // mlo * ulo
s0, carry = bits.Add64(hlo, lhi, 0)
r += carry
hhi, hlo = bits.Mul64(mhi, u[1]) // mhi * ulo
r += hhi
_, carry = bits.Add64(hlo, s0, 0)
r += carry
r = mlo - r*q
return
}
// CRed reduce returns a mod q where a is between 0 and 2*q-1.
func CRed(a, q uint64) uint64 {
if a >= q {
return a - q
}
return a
}