-
Notifications
You must be signed in to change notification settings - Fork 7
/
scalar.go
374 lines (307 loc) · 7.76 KB
/
scalar.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package ed448
import (
"errors"
)
// Scalar is a interface of a Ed448 scalar
type Scalar interface {
Equals(a Scalar) bool
EqualsMask(a Scalar) uint32
Copy() Scalar
Add(a, b Scalar)
Sub(a, b Scalar)
Mul(a, b Scalar)
Halve(a Scalar)
Invert() bool
Encode() []byte
BarretDecode(src []byte) error
Decode(src []byte)
}
type scalar [scalarWords]word
func (s *scalar) montgomeryMultiply(x, y *scalar) {
out := &scalar{}
carry := word(0x00)
for i := 0; i < scalarWords; i++ {
chain := dword(0x00)
for j := 0; j < scalarWords; j++ {
chain += dword(x[i])*dword(y[j]) + dword(out[j])
out[j] = word(chain)
chain >>= wordBits
}
saved := word(chain)
multiplicand := out[0] * montgomeryFactor
chain = dword(0x00)
for j := 0; j < scalarWords; j++ {
chain += dword(multiplicand)*dword(ScalarQ[j]) + dword(out[j])
if j > 0 {
out[j-1] = word(chain)
}
chain >>= wordBits
}
chain += dword(saved) + dword(carry)
out[scalarWords-1] = word(chain)
carry = word(chain >> wordBits)
}
out.subExtra(out, ScalarQ, carry)
copy(s[:], out[:])
}
func (s *scalar) montgomerySquare(x *scalar) {
s.montgomeryMultiply(x, x)
}
// Invert a scalar: 1/s
func (s *scalar) invert() bool {
// Fermat's little theorem, sliding window algorithm
preComp := make([]*scalar, 8)
for i := range preComp {
preComp[i] = new(scalar)
}
out := &scalar{}
scalarWindowBits := uint(3)
last := (1 << scalarWindowBits) - 1
// Precompute preCmp = [a^1, a^3, ...]
preComp[0].montgomeryMultiply(s, scalarR2)
if last > 0 {
preComp[last].montgomeryMultiply(preComp[0], preComp[0])
}
for i := 1; i <= last; i++ {
preComp[i].montgomeryMultiply(preComp[i-1], preComp[last])
}
// Sliding window
var residue, trailing, started uint
for i := scalarBits - 1; i >= int(-scalarWindowBits); i-- {
if started != 0 {
out.montgomerySquare(out)
}
var w word
if i >= 0 {
w = ScalarQ[i/wordBits]
} else {
w = 0x00
}
if i >= 0 && i < int(wordBits) {
w -= 2
}
residue = uint((word(residue) << 1) | ((w >> (uint(i) % wordBits)) & 1))
if residue>>scalarWindowBits != 0 {
trailing = residue
residue = 0
}
if trailing > 0 && (trailing&((1<<scalarWindowBits)-1)) == 0 {
if started != 0 {
out.montgomeryMultiply(out, preComp[trailing>>(scalarWindowBits+1)])
} else {
out = preComp[trailing>>(scalarWindowBits+1)].copy()
started = 1
}
trailing = 0
}
trailing <<= 1
}
// demontgomerize
one := &scalar{0x01}
out.montgomeryMultiply(out, one)
copy(s[:], out[:])
// TODO: memzero
// True is the output is not zero
return !out.equals(scalarZero)
}
func (s *scalar) equals(x *scalar) bool {
return word(s.equalsMask(x)) == decafTrue
}
func (s *scalar) equalsMask(x *scalar) uint32 {
diff := word(0x00)
for i := uintZero; i < scalarWords; i++ {
diff |= s[i] ^ x[i]
}
return uint32(isZeroMask(diff))
}
func (s *scalar) copy() *scalar {
out := &scalar{}
copy(out[:], s[:])
return out
}
func (s *scalar) set(w word) {
s[0] = w
}
// {minuend , accum} - subtrahend + (one or mor) q
// Must have carry <= 1
func (s *scalar) subExtra(minuend *scalar, subtrahend *scalar, carry word) {
out := &scalar{}
var chain sdword
for i := uintZero; i < scalarWords; i++ {
chain += sdword(minuend[i]) - sdword(subtrahend[i])
out[i] = word(chain)
chain >>= wordBits
}
borrow := chain + sdword(carry) // 0 or -1
chain = 0
for i := uintZero; i < scalarWords; i++ {
chain += sdword(out[i]) + (sdword(ScalarQ[i]) & borrow)
out[i] = word(chain)
chain >>= wordBits
}
copy(s[:], out[:])
}
func (s *scalar) add(a, b *scalar) {
var chain dword
for i := uintZero; i < scalarWords; i++ {
chain += dword(a[i]) + dword(b[i])
s[i] = word(chain)
chain >>= wordBits
}
s.subExtra(s, ScalarQ, word(chain))
}
func (s *scalar) sub(x, y *scalar) {
noExtra := word(0x00)
s.subExtra(x, y, noExtra)
}
func (s *scalar) mul(x, y *scalar) {
s.montgomeryMultiply(x, y)
s.montgomeryMultiply(s, scalarR2)
}
func (s *scalar) halve(a *scalar) {
mask := -(a[0] & 1)
var chain dword
var i uint
for i = uintZero; i < scalarWords; i++ {
chain += dword(a[i]) + dword(ScalarQ[i]&mask)
s[i] = word(chain)
chain >>= wordBits
}
for i = uintZero; i < scalarWords-1; i++ {
s[i] = s[i]>>1 | s[i+1]<<(wordBits-1)
}
s[i] = s[i]>>1 | word(chain<<(wordBits-1))
}
// Serializes an array of words into an array of bytes (little-endian)
func (s *scalar) encode(dst []byte) error {
wordBytes := wordBits / 8
if len(dst) < fieldBytes {
return errors.New("dst length smaller than fieldBytes")
}
k := uintZero
for i := uintZero; i < scalarLimbs; i++ {
for j := uintZero; j < uint(wordBytes); j++ {
b := s[i] >> (8 * j)
dst[k] = byte(b)
k++
}
}
return nil
}
func (s *scalar) decodeShort(b []byte, size uint) {
k := uintZero
for i := uintZero; i < scalarLimbs; i++ {
out := word(0)
for j := uintZero; j < 4 && k < size; j, k = j+1, k+1 {
out |= (word(b[k])) << (8 * j)
}
s[i] = out
}
}
func (s *scalar) decode(b []byte) word {
s.decodeShort(b, scalarBytes)
accum := sdword(0x00)
for i := 0; i < 14; i++ {
accum += sdword(s[i]) - sdword(ScalarQ[i])
accum >>= wordBits
}
s.mul(s, &scalar{0x01})
return word(accum)
}
// HACKY: either the param or the return
func decodeLong(s *scalar, b []byte) *scalar {
y := &scalar{}
bLen := len(b)
size := bLen - (bLen % scalarSerBytes)
if bLen == 0 {
s = scalarZero.copy()
return s
}
if size == bLen {
size -= scalarSerBytes
}
s.decodeShort(b[size:], uint(bLen-size))
if bLen == scalarBytes {
s.mul(s, &scalar{0x01})
return s
}
for size > 0 {
size -= scalarSerBytes
s.montgomeryMultiply(s, scalarR2)
y.decode(b[size:])
s.add(s, y)
}
y.destroy()
return s.copy()
}
func (s *scalar) destroy() {
copy(s[:], scalarZero[:])
}
//Exported methods
// NewScalar returns a Scalar in Ed448 with decaf
func NewScalar(in ...[]byte) Scalar {
if len(in) > 1 {
panic("too many arguments to function call")
}
if in == nil {
return &scalar{}
}
out := &scalar{}
bytes := in[0][:]
return decodeLong(out, bytes)
}
// Equals compares two scalars. Returns true if they are the same; false, otherwise.
func (s *scalar) Equals(x Scalar) bool {
return s.equals(x.(*scalar))
}
// EqualsMask compares two scalars.
func (s *scalar) EqualsMask(x Scalar) uint32 {
return s.equalsMask(x.(*scalar))
}
// Copy copies scalars.
func (s *scalar) Copy() Scalar {
out := &scalar{}
copy(out[:], s[:])
return out
}
// Add adds two scalars. The scalars may use the same memory.
func (s *scalar) Add(x, y Scalar) {
s.add(x.(*scalar), y.(*scalar))
}
// Sub subtracts two scalars. The scalars may use the same memory.
func (s *scalar) Sub(x, y Scalar) {
noExtra := word(0)
s.subExtra(x.(*scalar), y.(*scalar), noExtra)
}
// Mul multiplies two scalars. The scalars may use the same memory.
func (s *scalar) Mul(x, y Scalar) {
s.montgomeryMultiply(x.(*scalar), y.(*scalar))
s.montgomeryMultiply(s, scalarR2)
}
// Halve halfs a scalar. The scalars may used the same memory.
func (s *scalar) Halve(x Scalar) {
s.halve(x.(*scalar))
}
// Invert inverts a scalar. The scalars may used the same memory.
func (s *scalar) Invert() bool {
return s.invert()
}
// Encode serializes a scalar to wire format.
func (s *scalar) Encode() []byte {
dst := make([]byte, fieldBytes)
s.encode(dst)
return dst
}
// Decode reads a scalar from wire format or from bytes and reduces mod scalar prime.
// TODO: this will reduce with barret, change name and receiver
func (s *scalar) BarretDecode(src []byte) error {
if len(src) < fieldBytes {
return errors.New("ed448: cannot decode a scalar from a byte array with a length unequal to 56")
}
barrettDeserializeAndReduce(s[:], src, &curvePrimeOrder)
return nil
}
// Decode reads a scalar from wire format or from bytes and reduces mod scalar prime.
func (s *scalar) Decode(src []byte) {
decodeLong(s, src)
}