/
EDDSA.go
413 lines (384 loc) · 8.3 KB
/
EDDSA.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//go:build !386 && !arm
/*
* Copyright (c) 2012-2020 MIRACL UK Ltd.
*
* This file is part of MIRACL Core
* (see https://github.com/miracl/core).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* EDDSA API Functions */
package FP256BN
import "github.com/hyperledger/fabric-amcl/core"
const EDDSA_INVALID_PUBLIC_KEY int = -2
//const EDDSA_ERROR int = -3
// Transform a point multiplier to rfc7748 form
func rfc7748(r *BIG) {
lg := 0
t := NewBIGint(1)
c := CURVE_Cof_I
for c != 1 {
lg++
c /= 2
}
n := uint(8*EGS - lg + 1)
r.mod2m(n)
t.shl(n)
r.add(t)
c = r.lastbits(lg)
r.dec(c)
}
// reverse first n bytes of buff - for little endian
func eddsa_reverse(n int, buff []byte) {
for i := 0; i < n/2; i++ {
ch := buff[i]
buff[i] = buff[n-i-1]
buff[n-i-1] = ch
}
}
// dom - domain function
func dom(pp string, ph bool, cl byte) []byte {
PP := []byte(pp)
n := len(PP)
dom := make([]byte, n+2)
for i := 0; i < n; i++ {
dom[i] = PP[i]
}
if ph {
dom[n] = 1
} else {
dom[n] = 0
}
dom[n+1] = cl
return dom
}
func h(S []byte) []byte {
n := len(S)
if AESKEY <= 16 { // for ed25519?
sh := core.NewHASH512()
for i := 0; i < n; i++ {
sh.Process(S[i])
}
return sh.Hash()
} else { // for ed448?
digest := make([]byte, 2*n)
sh := core.NewSHA3(core.SHA3_SHAKE256)
for i := 0; i < n; i++ {
sh.Process(S[i])
}
sh.Shake(digest[:], 2*n)
return digest
}
}
func h2(ph bool, ctx []byte, R []byte, Q []byte, M []byte) *DBIG {
b := len(Q)
cl := 0
if ctx != nil {
cl = len(ctx)
}
if AESKEY <= 16 { // Ed25519??
sh := core.NewHASH512()
if ph || cl > 0 { // if not prehash and no context, omit dom2()
domain := dom("SigFP256BN no FP256BN collisions", ph, byte(cl))
for i := 0; i < len(domain); i++ {
sh.Process(domain[i])
}
for i := 0; i < int(cl); i++ {
sh.Process(ctx[i])
}
}
for i := 0; i < b; i++ {
sh.Process(R[i])
}
for i := 0; i < b; i++ {
sh.Process(Q[i])
}
for i := 0; i < len(M); i++ {
sh.Process(M[i])
}
h := sh.Hash()
eddsa_reverse(64, h)
return DBIG_fromBytes(h)
} else { // for ed448?
domain := dom("SigFP256BN", ph, byte(cl))
h := make([]byte, 2*b)
sh := core.NewSHA3(core.SHA3_SHAKE256)
for i := 0; i < len(domain); i++ {
sh.Process(domain[i])
}
for i := 0; i < cl; i++ {
sh.Process(ctx[i])
}
for i := 0; i < b; i++ {
sh.Process(R[i])
}
for i := 0; i < b; i++ {
sh.Process(Q[i])
}
for i := 0; i < len(M); i++ {
sh.Process(M[i])
}
sh.Shake(h, 2*b)
eddsa_reverse(2*b, h)
return DBIG_fromBytes(h)
}
}
func getR(ph bool, b int, digest []byte, ctx []byte, M []byte) *DBIG {
cl := 0
if ctx != nil {
cl = len(ctx)
}
if AESKEY <= 16 { // Ed25519??
sh := core.NewHASH512()
if ph || cl > 0 { // if not prehash and no context, omit dom2()
domain := dom("SigFP256BN no FP256BN collisions", ph, byte(cl))
for i := 0; i < len(domain); i++ {
sh.Process(domain[i])
}
for i := 0; i < cl; i++ {
sh.Process(ctx[i])
}
}
for i := b; i < 2*b; i++ {
sh.Process(digest[i])
}
for i := 0; i < len(M); i++ {
sh.Process(M[i])
}
h := sh.Hash()
eddsa_reverse(64, h)
return DBIG_fromBytes(h)
} else { // for ed448?
domain := dom("SigFP256BN", ph, byte(cl))
sh := core.NewSHA3(core.SHA3_SHAKE256)
h := make([]byte, 2*b)
for i := 0; i < len(domain); i++ {
sh.Process(domain[i])
}
for i := 0; i < cl; i++ {
sh.Process(ctx[i])
}
for i := b; i < 2*b; i++ {
sh.Process(digest[i])
}
for i := 0; i < len(M); i++ {
sh.Process(M[i])
}
sh.Shake(h, 2*b)
eddsa_reverse(2*b, h)
return DBIG_fromBytes(h)
}
}
// encode integer (little endian)
func encode_int(x *BIG, w []byte) int {
index := 0
if 8*MODBYTES == MODBITS {
index = 1 // extra byte needed for compression
}
b := int(MODBYTES) + index
w[0] = 0
x.tobytearray(w, index)
eddsa_reverse(b, w)
return b
}
// encode point
func encode(P *ECP, w []byte) {
index := 0
if 8*MODBYTES == MODBITS {
index = 1 // extra byte needed for compression
}
b := int(MODBYTES) + index
x := P.GetX()
y := P.GetY()
encode_int(y, w)
w[b-1] |= byte(x.parity() << 7)
}
// get sign
func getsign(x []byte) int {
index := 0
if 8*MODBYTES == MODBITS {
index = 1 // extra byte needed for compression
}
b := int(MODBYTES) + index
if (x[b-1] & 0x80) != 0 {
return 1
} else {
return 0
}
}
// decode integer (little endian)
func decode_int(strip_sign bool, ei []byte) *BIG {
index := 0
if 8*MODBYTES == MODBITS {
index = 1 // extra byte needed for compression
}
b := int(MODBYTES) + index
r := make([]byte, b)
for i := 0; i < b; i++ {
r[i] = ei[i]
}
eddsa_reverse(b, r)
if strip_sign {
r[0] &= 0x7f
}
return BIG_frombytearray(r, index)
}
// decode compressed point
func decode(W []byte) *ECP {
sign := getsign(W) // lsb of x
y := decode_int(true, W)
one := NewFPint(1)
hint := NewFP()
x := NewFPbig(y)
x.sqr()
d := NewFPcopy(x)
x.sub(one)
x.norm()
t := NewFPbig(NewBIGints(CURVE_B))
d.mul(t)
if CURVE_A == 1 {
d.sub(one)
}
if CURVE_A == -1 {
d.add(one)
}
d.norm()
// inverse square root trick for sqrt(x/d)
t.copy(x)
t.sqr()
x.mul(t)
x.mul(d)
if x.qr(hint) != 1 {
return NewECP()
}
d.copy(x.sqrt(hint))
x.inverse(hint)
x.mul(d)
x.mul(t)
x.reduce()
if x.redc().parity() != sign {
x.neg()
}
x.norm()
return NewECPbigs(x.redc(), y)
}
/* Calculate a public/private EC GF(p) key pair. Q=D.G mod EC(p),
* where D is the secret key and Q is the public key
* and G is fixed generator.
* RNG is a cryptographically strong RNG
* If RNG==NULL, D is provided externally
*/
func KEY_PAIR_GENERATE(RNG *core.RAND, D []byte, Q []byte) int {
res := 0
index := 0
if 8*MODBYTES == MODBITS {
index = 1 // extra byte needed for compression
}
b := int(MODBYTES) + index
G := ECP_generator()
if RNG != nil {
for i := 0; i < b; i++ {
D[i] = byte(RNG.GetByte())
}
}
digest := h(D)
// reverse bytes for little endian
eddsa_reverse(b, digest)
s := BIG_frombytearray(digest, index)
rfc7748(s)
G.Copy(G.mul(s))
encode(G, Q)
return res
}
// Generate a signature using key pair (D,Q) on message M
// Set ph=true if message has already been pre-hashed
// if ph=false, then context should be NULL for ed25519. However RFC8032 mode ed25519ctx is supported by supplying a non-NULL or non-empty context
func SIGNATURE(ph bool, D []byte, ctx []byte, M []byte, SIG []byte) int {
digest := h(D) // hash of private key
res := 0
index := 0
if 8*MODBYTES == MODBITS {
index = 1 // extra byte needed for compression
}
b := int(MODBYTES) + index
S := make([]byte, b)
Q := make([]byte, b)
KEY_PAIR_GENERATE(nil, D, Q[:])
q := NewBIGints(CURVE_Order)
if len(D) != len(Q) || len(D) != b {
res = EDDSA_INVALID_PUBLIC_KEY
}
if res == 0 {
dr := getR(ph, b, digest, ctx, M)
sr := dr.Mod(q)
R := ECP_generator().mul(sr)
encode(R, S[:])
for i := 0; i < b; i++ {
SIG[i] = S[i]
}
// reverse bytes for little endian
eddsa_reverse(b, digest)
s := BIG_frombytearray(digest, index)
RFC7748(s)
dr = h2(ph, ctx, SIG, Q, M)
sd := dr.Mod(q)
encode_int(Modadd(sr, Modmul(s, sd, q), q), S)
for i := 0; i < b; i++ {
SIG[b+i] = S[i]
}
}
return res
}
func VERIFY(ph bool, Q []byte, ctx []byte, M []byte, SIG []byte) bool {
lg := 0
index := 0
if 8*MODBYTES == MODBITS {
index = 1 // extra byte needed for compression
}
b := int(MODBYTES) + index
S := make([]byte, b)
c := CURVE_Cof_I
for c != 1 {
lg++
c /= 2
}
q := NewBIGints(CURVE_Order)
R := decode(SIG)
if R.Is_infinity() {
return false
}
for i := 0; i < b; i++ {
S[i] = SIG[b+i]
}
t := decode_int(false, S)
if Comp(t, q) >= 0 {
return false
}
du := h2(ph, ctx, SIG, Q, M)
su := du.Mod(q)
G := ECP_generator()
QD := decode(Q)
if QD.Is_infinity() {
return false
}
QD.Neg()
for i := 0; i < lg; i++ { // use cofactor 2^c
G.dbl()
QD.dbl()
R.dbl()
}
if !G.Mul2(t, QD, su).Equals(R) {
return false
}
return true
}