/
arith_generic.go
203 lines (174 loc) · 4.3 KB
/
arith_generic.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots.
//go:build purego || (!amd64 && !arm64)
// +build purego !amd64,!arm64
package p751
import (
"math/bits"
"github.com/cloudflare/circl/dh/sidh/internal/common"
)
// Compute z = x + y (mod p).
func addP751(z, x, y *common.Fp) {
var carry uint64
// z=x+y % P751
for i := 0; i < FpWords; i++ {
z[i], carry = bits.Add64(x[i], y[i], carry)
}
// z = z - P751x2
carry = 0
for i := 0; i < FpWords; i++ {
z[i], carry = bits.Sub64(z[i], P751x2[i], carry)
}
// if z<0 add P751x2 back
mask := uint64(0 - carry)
carry = 0
for i := 0; i < FpWords; i++ {
z[i], carry = bits.Add64(z[i], P751x2[i]&mask, carry)
}
}
// Compute z = x - y (mod p).
func subP751(z, x, y *common.Fp) {
var borrow uint64
for i := 0; i < FpWords; i++ {
z[i], borrow = bits.Sub64(x[i], y[i], borrow)
}
mask := uint64(0 - borrow)
borrow = 0
for i := 0; i < FpWords; i++ {
z[i], borrow = bits.Add64(z[i], P751x2[i]&mask, borrow)
}
}
// If choice = 0, leave x unchanged. If choice = 1, sets x to y.
// If choice is neither 0 nor 1 then behaviour is undefined.
// This function executes in constant time.
func cmovP751(x, y *common.Fp, choice uint8) {
mask := 0 - uint64(choice)
for i := 0; i < FpWords; i++ {
x[i] ^= mask & (x[i] ^ y[i])
}
}
// Conditionally swaps bits in x and y in constant time.
// mask indicates bits to be swapped (set bits are swapped)
// For details see "Hackers Delight, 2.20"
//
// Implementation doesn't actually depend on a prime field.
func cswapP751(x, y *common.Fp, mask uint8) {
var tmp, mask64 uint64
mask64 = 0 - uint64(mask)
for i := 0; i < FpWords; i++ {
tmp = mask64 & (x[i] ^ y[i])
x[i] = tmp ^ x[i]
y[i] = tmp ^ y[i]
}
}
// Perform Montgomery reduction: set z = x R^{-1} (mod 2*p)
// with R=2^(FpWords*64). Destroys the input value.
func rdcP751(z *common.Fp, x *common.FpX2) {
var carry, t, u, v uint64
var hi, lo uint64
var count int
count = P751p1Zeros
for i := 0; i < FpWords; i++ {
for j := 0; j < i; j++ {
if j < (i - count + 1) {
hi, lo = bits.Mul64(z[j], P751p1[i-j])
v, carry = bits.Add64(lo, v, 0)
u, carry = bits.Add64(hi, u, carry)
t += carry
}
}
v, carry = bits.Add64(v, x[i], 0)
u, carry = bits.Add64(u, 0, carry)
t += carry
z[i] = v
v = u
u = t
t = 0
}
for i := FpWords; i < 2*FpWords-1; i++ {
if count > 0 {
count--
}
for j := i - FpWords + 1; j < FpWords; j++ {
if j < (FpWords - count) {
hi, lo = bits.Mul64(z[j], P751p1[i-j])
v, carry = bits.Add64(lo, v, 0)
u, carry = bits.Add64(hi, u, carry)
t += carry
}
}
v, carry = bits.Add64(v, x[i], 0)
u, carry = bits.Add64(u, 0, carry)
t += carry
z[i-FpWords] = v
v = u
u = t
t = 0
}
v, _ = bits.Add64(v, x[2*FpWords-1], 0)
z[FpWords-1] = v
}
// Compute z = x * y.
func mulP751(z *common.FpX2, x, y *common.Fp) {
var u, v, t uint64
var hi, lo uint64
var carry uint64
for i := uint64(0); i < FpWords; i++ {
for j := uint64(0); j <= i; j++ {
hi, lo = bits.Mul64(x[j], y[i-j])
v, carry = bits.Add64(lo, v, 0)
u, carry = bits.Add64(hi, u, carry)
t += carry
}
z[i] = v
v = u
u = t
t = 0
}
for i := FpWords; i < (2*FpWords)-1; i++ {
for j := i - FpWords + 1; j < FpWords; j++ {
hi, lo = bits.Mul64(x[j], y[i-j])
v, carry = bits.Add64(lo, v, 0)
u, carry = bits.Add64(hi, u, carry)
t += carry
}
z[i] = v
v = u
u = t
t = 0
}
z[2*FpWords-1] = v
}
// Compute z = x + y, without reducing mod p.
func adlP751(z, x, y *common.FpX2) {
var carry uint64
for i := 0; i < 2*FpWords; i++ {
z[i], carry = bits.Add64(x[i], y[i], carry)
}
}
// Reduce a field element in [0, 2*p) to one in [0,p).
func modP751(x *common.Fp) {
var borrow, mask uint64
for i := 0; i < FpWords; i++ {
x[i], borrow = bits.Sub64(x[i], P751[i], borrow)
}
// Sets all bits if borrow = 1
mask = 0 - borrow
borrow = 0
for i := 0; i < FpWords; i++ {
x[i], borrow = bits.Add64(x[i], P751[i]&mask, borrow)
}
}
// Compute z = x - y, without reducing mod p.
func sulP751(z, x, y *common.FpX2) {
var borrow, mask uint64
for i := 0; i < 2*FpWords; i++ {
z[i], borrow = bits.Sub64(x[i], y[i], borrow)
}
// Sets all bits if borrow = 1
mask = 0 - borrow
borrow = 0
for i := FpWords; i < 2*FpWords; i++ {
z[i], borrow = bits.Add64(z[i], P751[i-FpWords]&mask, borrow)
}
}