-
Notifications
You must be signed in to change notification settings - Fork 4
/
group25519.cpp
2600 lines (2180 loc) · 71.5 KB
/
group25519.cpp
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017-2019, Pelayo Bernedo.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "group25519.hpp"
#include "misc.hpp"
#include "blake2.hpp"
#include <iostream>
#include <iomanip>
#include <string.h>
#include <fstream>
#include "sha2.hpp"
#include "hasopt.hpp"
namespace amber { namespace AMBER_SONAME {
static const Fe fezero = { 0 };
static const Fe feone = { 1 };
static const Edwards edzero = { fezero, feone, feone, fezero };
#if AMBER_LIMB_BITS == 32
// p = 2²⁵⁵-19
static const Fe p = { 0x3FFFFED, mask25, mask26, mask25, mask26,
mask25, mask26, mask25, mask26, mask25 };
// d = -121665/121666
static const Fe edwards_d = { 0x35978a3, 0x0d37284, 0x3156ebd, 0x06a0a0e, 0x001c029,
0x179e898, 0x3a03cbb, 0x1ce7198, 0x2e2b6ff, 0x1480db3 };
static const Fe edwards_2d = { 0x2b2f159, 0x1a6e509, 0x22add7a, 0x0d4141d, 0x0038052,
0x0f3d130, 0x3407977, 0x19ce331, 0x1c56dff, 0x0901b67 };
// sqrt(-1) = 2^(2²⁵³ - 5)
static const Fe root_minus_1 = { 0x20ea0b0, 0x186c9d2, 0x08f189d, 0x035697f, 0x0bd0c60,
0x1fbd7a7, 0x2804c9e, 0x1e16569, 0x004fc1d, 0x0ae0c92 };
// C = sqrt(-A-2)
static const Fe C = { 0x0ba81e7, 0x07ed540, 0x0afa672, 0x175a417, 0x0e978b0,
0x003b081, 0x27b91fe, 0x12885b0, 0x0b9f5ff, 0x1c36448 };
// Base point. This is 9 in Montgomery.
static const Edwards edwards_base = {
{ 0x325d51a, 0x18b5823, 0x0f6592a, 0x104a92d, 0x1a4b31d,
0x1d6dc5c, 0x27118fe, 0x07fd814, 0x13cd6e5, 0x085a4db },
{ 0x2666658, 0x1999999, 0x0cccccc, 0x1333333, 0x1999999,
0x0666666, 0x3333333, 0x0cccccc, 0x2666666, 0x1999999 },
{ 0x0000001, 0x0000000, 0x0000000, 0x0000000, 0x0000000,
0x0000000, 0x0000000, 0x0000000, 0x0000000, 0x0000000 },
{ 0x1b7dda3, 0x1a2ace9, 0x25eadbb, 0x003ba8a, 0x083c27e,
0x0abe37d, 0x1274732, 0x0ccacdd, 0x0fd78b7, 0x19e1d7c }
};
static const Fe d_minus_one_sq = { 0x0ed4d20, 0x156aa91, 0x3332635, 0x16580f0, 0x34a7928, 0x09b4eeb, 0x26997a9, 0x048299b, 0x3af66c2, 0x165a2cd };
static const Fe one_minus_d_sq = { 0x05fc176, 0x1027065, 0x2a1fc4f, 0x1c66af1, 0x0b20684, 0x070dfe4, 0x255eedf, 0x01af332, 0x28b2b3e, 0x00a41ca };
static const Fe invsqrt_a_minus_d = { 0x05d40ea, 0x03f6aa0, 0x257d339, 0x0bad20b, 0x274bc58, 0x001d840, 0x13dc8ff, 0x19442d8, 0x05cfaff, 0x1e1b224 };
static const Fe sqrt_ad_minus_one = { 0x17b2e1b, 0x1fda812, 0x297afd2, 0x060dbc2, 0x2be7638, 0x1f5d1fd, 0x27e6498, 0x11581e7, 0x3f2b834, 0x0dda4c6 };
#elif AMBER_LIMB_BITS >= 64
static const Fe p = { p0_64, mask51, mask51, mask51, mask51 };
// d = -121665/121666
static const Fe edwards_d = { 0x34dca135978a3, 0x1a8283b156ebd, 0x5e7a26001c029,
0x739c663a03cbb, 0x52036cee2b6ff };
static const Fe edwards_2d = { 0x69b9426b2f159, 0x35050762add7a, 0x3cf44c0038052,
0x6738cc7407977, 0x2406d9dc56dff };
// sqrt(-1) = 2^(2²⁵³ - 5)
static const Fe root_minus_1 = { 0x61b274a0ea0b0, 0xd5a5fc8f189d, 0x7ef5e9cbd0c60,
0x78595a6804c9e, 0x2b8324804fc1d };
// C = sqrt(-A-2)
static const Fe C = { 0x1fb5500ba81e7, 0x5d6905cafa672, 0xec204e978b0, 0x4a216c27b91fe,
0x70d9120b9f5ff };
// Base point. This is 9 in Montgomery.
static const Edwards edwards_base = {
{ 0x62d608f25d51a, 0x412a4b4f6592a, 0x75b7171a4b31d, 0x1ff60527118fe, 0x216936d3cd6e5 },
{ 0x6666666666658, 0x4cccccccccccc, 0x1999999999999, 0x3333333333333, 0x6666666666666 },
{ 0x0000001, 0x0000000, 0x0000000, 0x0000000, 0x0000000 },
{ 0x68ab3a5b7dda3, 0xeea2a5eadbb, 0x2af8df483c27e, 0x332b375274732, 0x67875f0fd78b7 }
};
static const Fe invsqrt_a_minus_d = { 0xfdaa805d40ea, 0x2eb482e57d339, 0x7610274bc58,
0x6510b613dc8ff, 0x786c8905cfaff };
static const Fe one_minus_d_sq = { 0x409c1945fc176, 0x719abc6a1fc4f, 0x1c37f90b20684,
0x6bccca55eedf, 0x29072a8b2b3e };
static const Fe d_minus_one_sq = { 0x55aaa44ed4d20, 0x59603c3332635, 0x26d3baf4a7928,
0x120a66e6997a9, 0x5968b37af66c2 };
static const Fe sqrt_ad_minus_one = { 0x7f6a0497b2e1b, 0x1836f0a97afd2, 0x7d747f6be7638,
0x456079e7e6498, 0x376931bf2b834 };
#endif
const Edwards edwards_base_point = edwards_base;
#if 0 // Used for debugging.
static void show_edwards (std::ostream &os, const char *label, const Edwards &ed)
{
os << label << ": \n";
os << " x: " << ed.x << '\n';
os << " y: " << ed.y << '\n';
os << " z: " << ed.z << '\n';
os << " t: " << ed.t << '\n';
}
#endif
// Store the point as Montgomery x with the sign bit in bit 255.
/*
u = (Z + Y)/(Z - Y)
x = X/Z
Combine the two inversions.
h = 1/(Z(Z-Y))
u = Z(Z+Y)h = Z(Z+Y)/Z/(Z-Y) = (Z+Y)/(Z-Y)
x = X(Z-Y)h = X(Z-Y)/Z/(Z-Y) = X/Z
*/
void edwards_to_mxs (uint8_t res[32], const Edwards &p)
{
Fe zmy, h;
sub (zmy, p.z, p.y);
mul (h, zmy, p.z);
invert (h, h);
mul (zmy, zmy, h);
mul (zmy, zmy, p.x);
reduce_store (res, zmy);
uint8_t sign = res[0] & 1;
add_no_reduce (zmy, p.z, p.y);
mul (zmy, zmy, p.z);
mul (zmy, zmy, h);
reduce_store (res, zmy);
res[31] |= sign << 7;
}
// Store the point as Edwards y with the sign bit in bit 255.
void edwards_to_eys (uint8_t res[32], const Edwards &p)
{
Fe inv, tmp;
invert (inv, p.z);
mul (tmp, p.x, inv);
reduce_store (res, tmp);
uint8_t sign = res[0] & 1;
mul (tmp, p.y, inv);
reduce_store (res, tmp);
res[31] |= sign << 7;
}
// Convert simultaneously to compressed Montgomery and Edwards.
/*
u = (Z + Y)/(Z - Y)
x = X/Z
Combine the two inversions.
h = 1/(Z(Z-Y))
u = Z(Z+Y)h = Z(Z+Y)/Z/(Z-Y) = (Z+Y)/(Z-Y)
w = (Z-Y)h = (Z-Y)/Z/(Z-Y) = 1/Z
x = wX
y = wY
*/
void edwards_to_eys_mxs (uint8_t ey[32], uint8_t mx[32], const Edwards &p)
{
Fe zmy, h, w;
sub (zmy, p.z, p.y);
mul (h, zmy, p.z);
invert (h, h);
mul (w, zmy, h);
mul (zmy, w, p.x);
reduce_store (ey, zmy);
uint8_t sign = ey[0] & 1;
mul (zmy, w, p.y);
reduce_store (ey, zmy);
ey[31] |= sign << 7;
add_no_reduce (zmy, p.z, p.y);
mul (zmy, zmy, p.z);
mul (zmy, zmy, h);
reduce_store (mx, zmy);
mx[31] |= sign << 7;
}
// Load to Edwards coordinates starting from the Montgomery x coordinate.
// The Montgomery x coordinate contains the parity of the Edwards x
// coordinate in the most significant bit of the last byte. This is as fast
// as unpacking the Edwards Y coordinate. See
// https://moderncrypto.org/mail-archive/curves/2015/000376.html
/* x = Cu/v C = sqrt(-1)*sqrt(A+2)
y = (u-1)/(u+1).
where v = sqrt(u³+Au²+u)
We combine the inversions and the square root into one exponential.
v² = u³+Au²+u
h = v²*(u+1)²
s = 1/sqrt(h) = h³(h⁷)^((p-5)/8) = h³(h⁷)^(2²⁵²-3) From Ed25519 paper.
s is now 1/(v(u+1))
x = C*u*(u+1)*s
1/(u+1) = s²*v²*(u+1)
y = (u-1)/(u+1) = (u-1)*(u+1)*s²*v²
*/
// Expansion of compressed Montgomery X plus sign bit of Edwards X to Edwards
// coordinates. It will return 0 on success. If the compressed point mx is
// not on the curve or if the input is mx==0 or mx==-1 then it will return a
// non zero value.
// If neg is true then select the negative value.
int mxs_to_edwards (Edwards &res, const uint8_t mx[32], bool neg)
{
Fe u, t1, t2, a, b, h, s;
enum { A = 486662 };
load (u, mx);
square (t1, u);
mul_small (t2, u, A);
add (t1, t1, t2);
add_no_reduce (t1, t1, feone);
mul (a, t1, u); // a = u(1 + Au + u²) = v²
add_no_reduce (b, u, feone); // b = u + 1
mul (h, a, b);
mul (h, h, b); // h = ab²
if (0 != invsqrt (s, h)) {
return -1;
} // s = 1/sqrt(h);
// y = (u - 1)*a*b*s²
sub (t1, u, feone);
mul (t1, t1, a);
mul (t1, t1, b);
square (t2, s);
mul (res.y, t1, t2);
// x = C*u*b*s
mul (res.x, C, u);
mul (res.x, res.x, b);
mul (res.x, res.x, s);
uint8_t check[32];
int signbit = mx[31] >> 7;
reduce_store (check, res.x);
if (neg) signbit = !signbit;
// Select the correct sign bit/
if ((check[0] & 1) != signbit) {
negate (res.x, res.x);
}
mul (res.t, res.x, res.y);
res.z = feone;
return 0;
}
inline bool is_zero_vartime (Fe &fe)
{
uint8_t bytes[32];
reduce_store (bytes, fe);
for (int i = 0; i < fecount; ++i) {
if (fe.v[i] != 0) return false;
}
return true;
}
/* -x² + y² = 1 + dx²y²
y² - 1 = x² + dx²y²
x = sqrt((y² - 1)/(1 + dy²))
*/
// Return 0 if ok. -1 on errors.
int eys_to_edwards (Edwards &res, const uint8_t ey[32], bool neg)
{
Fe u, v, y, y2, tmp1, tmp2, v4, x2;
load (y, ey);
square (y2, y);
sub (u, y2, feone); // u = y² - 1
mul (v, edwards_d, y2);
add (v, v, feone); // v = dy² + 1
// Compute sqrt(u/v) as uv³(uv⁷)^[(q-5)/8]
square (tmp1, v);
square (v4, tmp1);
mul (tmp1, tmp1, u);
mul (tmp1, tmp1, v); // tmp1 = uv³
mul (tmp2, tmp1, v4);
raise_252_3 (tmp2, tmp2); // tmp2 = sqrt(u/v)
mul (res.x, tmp1, tmp2);
// Now check if the root fits the equation x²v = u
square (x2, res.x);
mul (tmp1, x2, v);
sub (tmp2, tmp1, u);
if (!is_zero_vartime (tmp2)) {
// Check if -x² works:
add (tmp2, tmp1, u);
if (!is_zero_vartime (tmp2)) {
return -1;
}
mul (res.x, res.x, root_minus_1);
}
// Choose the correct sign value.
uint8_t bytes[32];
reduce_store (bytes, res.x);
int signbit = ey[31] >> 7;
if (neg) signbit = !signbit;
if ((bytes[0] & 1) != signbit) {
// Wrong sign bit. Select the negative value.
negate (res.x, res.x);
}
res.y = y;
mul (res.t, res.x, res.y);
res.z = feone;
return 0;
}
// Convert compressed Edwards y to compressed Montgomery x, with sign bits.
// The y=1 (identity point) it maps to u = ∞ (the identity point in
// Montgomery). However our routine maps it to u=0.
void eys_to_mxs (uint8_t mx[32], const uint8_t ey[32])
{
Fe y, t1, t2;
// u = (1+y)/(1-y)
load (y, ey);
add_no_reduce (t1, y, feone);
sub (t2, feone, y);
invert (t2, t2);
mul (t1, t1, t2);
reduce_store (mx, t1);
mx[31] |= ey[31] & 0x80;
}
// Convert compressed Montgomery x to compressed Edwards y, with sign bits.
// When u == -1 this fails and returns ey == 0.
void mxs_to_eys (uint8_t ey[32], const uint8_t mx[32])
{
// y = (u - 1)/(u + 1)
Fe x, y, tmp0, tmp1;
load (x, mx);
sub (tmp0, x, feone);
add_no_reduce (tmp1, x, feone);
invert (tmp1, tmp1);
mul (y, tmp0, tmp1);
reduce_store (ey, y);
ey[31] |= mx[31] & 0x80;
}
std::ostream & operator<< (std::ostream &os, const Edwards &rhs)
{
uint8_t ey[32];
edwards_to_eys (ey, rhs);
os << std::hex << std::setfill ('0');
int count = 0;
for (int i = 0; i < 32; ++i) {
os << std::setw(2) << unsigned(ey[i]);
if (++count == 4) {
os << ' ';
count = 0;
}
}
os << std::dec << std::setfill (' ');
return os;
}
// Precomputed values that are stored without z. z is assumed to be 1.
struct Precomputed {
// Store the values required for the addition. z is assumed to be 1.
// y + x, y - x, x*y*2*d
Fe ypx, ymx, xy2d;
};
// Precomputed values that are stored with z.
struct Summand {
// y + x, y - x, t*2*d, z*2
Fe ypx, ymx, t2d, z2;
};
// These formulae are given in RFC 8032.
// res = p + q
inline void point_add (Edwards &res, const Edwards &p, const Edwards &q)
{
Fe a, b, c, d, e, f, g, h, t;
sub (a, p.y, p.x);
sub (t, q.y, q.x);
mul (a, a, t);
add_no_reduce (b, p.x, p.y);
add_no_reduce (t, q.x, q.y);
mul (b, b, t);
mul (c, p.t, q.t);
mul (c, c, edwards_2d);
mul (d, p.z, q.z);
add_no_reduce (d, d, d);
sub (e, b, a);
sub (f, d, c);
add_no_reduce (g, d, c);
add_no_reduce (h, b, a);
mul (res.x, e, f);
mul (res.y, h, g);
mul (res.t, e, h);
mul (res.z, f, g);
}
void add (Edwards &res, const Edwards &a, const Edwards &b)
{
point_add (res, a, b);
}
inline void point_add (Edwards &res, const Edwards &p, const Precomputed &q)
{
Fe a, b, c, d, e, f, g, h;
sub (a, p.y, p.x);
mul (a, a, q.ymx);
add_no_reduce (b, p.x, p.y);
mul (b, b, q.ypx);
mul (c, p.t, q.xy2d);
add_no_reduce (d, p.z, p.z);
sub (e, b, a);
sub (f, d, c);
add_no_reduce (g, d, c);
add_no_reduce (h, b, a);
mul (res.x, e, f);
mul (res.y, h, g);
mul (res.t, e, h);
mul (res.z, f, g);
}
inline void point_add (Edwards &res, const Edwards &p, const Summand &q)
{
Fe a, b, c, d, e, f, g, h;
sub (a, p.y, p.x);
// sub (t, q.y, q.x);
mul (a, a, q.ymx);
add_no_reduce (b, p.x, p.y);
// add_no_reduce (t, q.x, q.y);
mul (b, b, q.ypx);
mul (c, p.t, q.t2d);
// mul (c, c, edwards_2d);
mul (d, p.z, q.z2);
// add_no_reduce (d, d, d);
sub (e, b, a);
sub (f, d, c);
add_no_reduce (g, d, c);
add_no_reduce (h, b, a);
mul (res.x, e, f);
mul (res.y, h, g);
mul (res.t, e, h);
mul (res.z, f, g);
}
inline void point_add (Summand &res, const Summand &p, const Edwards &q)
{
Fe a, b, c, d, e, f, g, h, t;
// sub (a, p.y, p.x);
sub (t, q.y, q.x);
mul (a, p.ymx, t);
// add_no_reduce (b, p.x, p.y);
add_no_reduce (t, q.x, q.y);
mul (b, p.ypx, t);
mul (c, p.t2d, q.t);
// mul (c, c, edwards_2d);
mul (d, p.z2, q.z);
// add_no_reduce (d, d, d);
sub (e, b, a);
sub (f, d, c);
add_no_reduce (g, d, c);
add_no_reduce (h, b, a);
Fe x, y;
mul (x, e, f);
mul (y, h, g);
add_no_reduce (res.ypx, x, y);
sub (res.ymx, y, x);
mul (res.t2d, e, h);
mul (res.t2d, res.t2d, edwards_2d);
mul (res.z2, f, g);
add_no_reduce (res.z2, res.z2, res.z2);
}
// res = p - q
inline void point_sub (Edwards &res, const Edwards &p, const Edwards &q)
{
Fe a, b, c, d, e, f, g, h, t;
sub (a, p.y, p.x);
add (t, q.y, q.x);
mul (a, a, t);
add_no_reduce (b, p.x, p.y);
sub (t, q.y, q.x);
mul (b, b, t);
negate (c, q.t);
mul (c, p.t, c);
mul (c, c, edwards_2d);
mul (d, p.z, q.z);
add_no_reduce (d, d, d);
sub (e, b, a);
sub (f, d, c);
add_no_reduce (g, d, c);
add_no_reduce (h, b, a);
mul (res.x, e, f);
mul (res.y, h, g);
mul (res.t, e, h);
mul (res.z, f, g);
}
void sub (Edwards &res, const Edwards &a, const Edwards &b)
{
point_sub (res, a, b);
}
inline void point_sub (Edwards &res, const Edwards &p, const Summand &q)
{
Fe a, b, c, d, e, f, g, h;
sub (a, p.y, p.x);
// add (t, q.y, q.x);
mul (a, a, q.ypx);
add_no_reduce (b, p.x, p.y);
// sub (t, q.y, q.x);
mul (b, b, q.ymx);
negate (c, q.t2d);
mul (c, p.t, c);
// mul (c, c, edwards_2d);
mul (d, p.z, q.z2);
// add_no_reduce (d, d, d);
sub (e, b, a);
sub (f, d, c);
add_no_reduce (g, d, c);
add_no_reduce (h, b, a);
mul (res.x, e, f);
mul (res.y, h, g);
mul (res.t, e, h);
mul (res.z, f, g);
}
inline void point_double (Edwards &res, const Edwards &p)
{
Fe a, b, c, h, e, g, f;
square (a, p.x);
square (b, p.y);
square (c, p.z);
add (c, c, c);
add (h, a, b);
add_no_reduce (e, p.x, p.y);
square (e, e);
sub (e, h, e);
sub (g, a, b);
add_no_reduce (f, c, g);
mul (res.x, e, f);
mul (res.y, g, h);
mul (res.t, e, h);
mul (res.z, f, g);
}
void pdouble (Edwards &res, const Edwards &x)
{
point_double (res, x);
}
void negate (Edwards &res, const Edwards &p)
{
// Works even if res and p point to the same memory.
negate (res.x, p.x);
res.y = p.y;
res.z = p.z;
negate (res.t, p.t);
}
// Swap a and b if flag == 1. Flag must be exactly 1 or 0. No other values.
inline void cswap (Edwards &a, Edwards &b, uint32_t flag)
{
cswap (a.x, b.x, flag);
cswap (a.y, b.y, flag);
cswap (a.z, b.z, flag);
cswap (a.t, b.t, flag);
}
// res = sP
void scalarmult (Edwards &res, const Edwards &p, const uint8_t s[32])
{
int i;
res.x = fezero;
res.y = feone;
res.z = feone;
res.t = fezero;
Edwards tmp = p;
for (i = 255; i >= 0; --i) {
uint32_t b = (s[i/8] >> (i&7)) & 1;
cswap (res, tmp, b);
point_add (tmp, tmp, res);
point_double (res, res);
cswap (res, tmp, b);
}
}
// return a == b in constant time.
inline uint32_t equal (uint32_t a, uint32_t b)
{
uint32_t diff = a ^ b;
return (diff - 1) >> 31;
}
// If flag == 1 then res = p. Flag can be either 0 or 1, no other values
// allowed.
inline void select (Edwards &res, const Edwards &p, uint32_t flag)
{
typedef decltype(p.x.v[0]) Limbint;
Limbint liflag = flag;
Limbint discard = liflag - 1; // All zero if flag == 1, all 1 if flag == 0
Limbint keep = ~discard; // All one if flag == 1, all 0 if flag == 0
for (int i = 0; i < fecount; ++i) {
res.x.v[i] = (res.x.v[i] & discard) | (p.x.v[i] & keep);
res.y.v[i] = (res.y.v[i] & discard) | (p.y.v[i] & keep);
res.z.v[i] = (res.z.v[i] & discard) | (p.z.v[i] & keep);
res.t.v[i] = (res.t.v[i] & discard) | (p.t.v[i] & keep);
}
}
// Fixed window, 4 bits at a time.
void scalarmult_fw (Edwards &res, const Edwards &p, const uint8_t s[32])
{
int i;
res.x = fezero;
res.y = feone;
res.z = feone;
res.t = fezero;
Edwards pmul[16];
pmul[0] = res;
pmul[1] = p;
point_double (pmul[2], pmul[1]);
point_add (pmul[3], pmul[2], pmul[1]);
point_double (pmul[4], pmul[2]);
point_add (pmul[5], pmul[4], pmul[1]);
point_double (pmul[6], pmul[3]);
point_add (pmul[7], pmul[6], pmul[1]);
point_double (pmul[8], pmul[4]);
point_add (pmul[9], pmul[8], pmul[1]);
point_double (pmul[10], pmul[5]);
point_add (pmul[11], pmul[10], pmul[1]);
point_double (pmul[12], pmul[6]);
point_add (pmul[13], pmul[12], pmul[1]);
point_double (pmul[14], pmul[7]);
point_add (pmul[15], pmul[14], pmul[1]);
Edwards tmp;
for (i = 31; i >= 0; --i) {
uint32_t v = s[i] >> 4;
point_double (res, res);
point_double (res, res);
point_double (res, res);
point_double (res, res);
for (int j = 0; j < 16; ++j) {
select (tmp, pmul[j], equal (j, v));
}
point_add (res, res, tmp);
v = s[i] & 0xF;
point_double (res, res);
point_double (res, res);
point_double (res, res);
point_double (res, res);
for (int j = 0; j < 16; ++j) {
select (tmp, pmul[j], equal (j, v));
}
point_add (res, res, tmp);
}
}
static
void edwards_to_precomp (Precomputed &pc, const Edwards &e)
{
Fe nx, ny, inv;
invert (inv, e.z);
mul (nx, e.x, inv);
mul (ny, e.y, inv);
add (pc.ypx, nx, ny);
sub (pc.ymx, ny, nx);
mul (pc.xy2d, nx, ny);
mul (pc.xy2d, pc.xy2d, edwards_2d);
}
static
void edwards_to_summand (Summand &s, const Edwards &e)
{
add (s.ypx, e.y, e.x);
sub (s.ymx, e.y, e.x);
mul (s.t2d, e.t, edwards_2d);
add (s.z2, e.z, e.z);
}
#if 0 // Used for debugging.
static
void summand_to_edwards (Edwards &e, const Summand &s)
{
add (e.y, s.ypx, s.ymx);
sub (e.x, s.ypx, s.ymx);
mul (e.t, e.x, e.y);
e.z = s.z2;
Fe inv;
invert (inv, e.z);
mul (e.t, e.t, inv);
}
#endif
// This file was created with
// write_base_multiples("group25519_basemult.hpp"); 30 kbytes
#if AMBER_LIMB_BITS == 32
#include "group25519_basemult_32.hpp"
#elif AMBER_LIMB_BITS >= 64
#include "group25519_basemult_64.hpp"
#endif
static
void write_coeffs (std::ostream &os, const Fe &fe)
{
os << "{ " << std::hex << std::setfill('0');
for (int i = 0; i < fecount; ++i) {
os << "0x" << std::setw(7) << fe.v[i];
if (i != 9) {
os << ", ";
}
}
os << " }" << std::setfill(' ') << std::dec;
}
void write_base_multiples (const char *name)
{
std::ofstream os(name);
os << "static const Precomputed basemult[32][8] = {\n";
Edwards p;
Precomputed pc;
uint8_t scalar[32];
for (int i = 0; i < 32; ++i) {
memset (scalar, 0, 32);
os << " { // 16^" << i*2 << "*B\n";
for (int j = 1; j < 9; ++j) {
scalar[i] = j;
scalarmult (p, edwards_base, scalar);
edwards_to_precomp (pc, p);
os << " { // " << j << "*16^" << i*2 << "*B\n";
os << " "; write_coeffs (os, pc.ypx); os << ",\n";
os << " "; write_coeffs (os, pc.ymx); os << ",\n";
os << " "; write_coeffs (os, pc.xy2d); os << '\n';
os << " }";
if (j == 8) {
os << "\n }";
if (i != 31) {
os << ",";
}
} else {
os << ",\n";
}
}
}
os << "};\n";
}
// If flag == 1 then res = p. If flag == 0 then leave res unchanged. Flag can
// be either 0 or 1, no other values allowed.
inline void select (Precomputed &res, const Precomputed &p, uint32_t flag)
{
typedef decltype(p.ypx.v[0]) Limbint;
Limbint liflag = flag;
Limbint discard = liflag - 1; // All zero if flag == 1, all 1 if flag == 0
Limbint keep = ~discard; // All one if flag == 1, all 0 if flag == 0
for (int i = 0; i < fecount; ++i) {
res.ypx.v[i] = (res.ypx.v[i] & discard) | (p.ypx.v[i] & keep);
res.ymx.v[i] = (res.ymx.v[i] & discard) | (p.ymx.v[i] & keep);
res.xy2d.v[i] = (res.xy2d.v[i] & discard) | (p.xy2d.v[i] & keep);
}
}
// Compute the absolute value using bit manipulation.
inline int32_t iabs (int32_t x)
{
int32_t y = x >> 31;
int32_t a = (x ^ y) - y;
return a;
}
// Select the correct multiple and sign. It returns res = smult*16^(i*2)*B
static void compute_multiple (Precomputed &res, int8_t smult, int i)
{
uint32_t negative = (smult & 0x80) >> 7; // Only 1 or 0.
uint32_t mult = iabs (smult);
res = { feone, feone, fezero }; // Zero element.
for (int j = 1; j <= 8; ++j) {
// Load the multiple corresponding to the absolute value.
select (res, basemult[i][j-1], equal (mult, j));
}
// Compute the negative of the multiple.
Precomputed neg;
neg.ypx = res.ymx;
neg.ymx = res.ypx;
negate (neg.xy2d, res.xy2d);
// Select the negative one if smult < 0.
select (res, neg, negative);
}
// This is base_point^2²⁵⁶
#if AMBER_LIMB_BITS == 32
static const Edwards bm = {
{ 0x23c9847, 0x0b2d5e7, 0x2ddfa9c, 0x02375e9, 0x3cd01b5, 0x1a2738f, 0x09db05c, 0x1105ca3, 0x37ae0ea, 0x08aae4e },
{ 0x3801e2f, 0x0002e2b, 0x28af68f, 0x14aa073, 0x1938654, 0x009e462, 0x36f9f83, 0x0b0df04, 0x1e39fb5, 0x1f8bade },
{ 0x2eb300f, 0x0d354d4, 0x18846f0, 0x13cbc39, 0x0913f4f, 0x0c5f5a3, 0x0f41a9c, 0x062db53, 0x036b61a, 0x1789558 },
{ 0x2f7d68d, 0x098e371, 0x13a0391, 0x041f04c, 0x31931ef, 0x030a46b, 0x38de645, 0x154c5a0, 0x0ac216f, 0x13d7df0 }
};
#else
static const Edwards bm = {
{ 0x2cb579e3c9847, 0x8dd7a6ddfa9c, 0x689ce3fcd01b5, 0x441728c9db05c, 0x22ab93b7ae0ea },
{ 0xb8af801e2f, 0x52a81ce8af68f, 0x279189938654, 0x2c37c136f9f83, 0x7e2eb79e39fb5 },
{ 0x34d5352eb300f, 0x4f2f0e58846f0, 0x317d68c913f4f, 0x18b6d4cf41a9c, 0x5e2556036b61a },
{ 0x2638dc6f7d68d, 0x107c1313a0391, 0xc291af1931ef, 0x55316838de645, 0x4f5f7c0ac216f }
};
#endif
void scalarbase (Edwards &res, const uint8_t scalar[32])
{
int8_t sc[64];
// Store the scalar in sc with 4 bit limbs, taking values between 0 and
// 15.
for (int i = 0; i < 32; ++i) {
sc[2*i] = scalar[i] & 0xF;
sc[2*i + 1] = scalar[i] >> 4;
}
// Store the scalar in sc with limbs taking values between -8 and +7.
int8_t carry = 0;
for (int i = 0; i < 64; ++i) {
sc[i] += carry;
// Set carry to 1 if sc[i] >= 8
carry = (sc[i] + 8) >> 4;
// Substract 16 if sc[i] >= 8
sc[i] -= carry << 4;
}
// Carry may still be 1 if we have the most significant bit of the scalar
// set. This does not happen in pure X25519/Ed25519 but we allow scalars
// of 256 bits. If carry is set we need to add base^2²⁵⁶ at the end.
// Now compute res = P0 + 16P1, with P0 = sc₀*16⁰B + sc₂*16²B + sc₄*16⁴B
// + sc₆*16⁶B + ... + sc₆₂*16⁶²B and P1 = sc₁*16⁰B + sc₃*16²B + sc₅*16⁴B
// + sc₇*16⁶B + ... + sc₆₃*16⁶²B. Note that we only need to store the
// multiples of 16^i*B where i is even. Furthermore we just store the
// positive values. Negative values are treated by obtaining its
// corresponding positive value and substracting.
// Set res to carry * base^2²⁵⁶ as the initial value. Using this initial
// value we do not need to do anything else to support 256 bits.
res = edzero;
select (res, bm, carry);
Edwards res1 = edzero;
Precomputed tmp;
for (int i = 0; i < 32; ++i) {
compute_multiple (tmp, sc[i*2], i);
point_add (res, res, tmp);
compute_multiple (tmp, sc[i*2 + 1], i);
point_add (res1, res1, tmp);
}
point_double (res1, res1); // *2
point_double (res1, res1); // *4
point_double (res1, res1); // *8
point_double (res1, res1); // *16
point_add (res, res, res1);
}
// Reduce taken from Tweet NaCl.
typedef int32_t Limbtype;
// This is the order of the group in packed form.
static const Limbtype L[32] = {
0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0x10
};
// L is 2²⁵² + δ, with δ having 128 bits. Tweet NaCl takes advantage of this
// structure to reduce the upper bytes by substracting a multiple of L. For
// instance to remove the byte at position i, with value x[i] substract
// L*x[i] << (i*8-4). If we proceed from the highest byte to the last bit to
// remove then we remove the upper bits. We may end up with a negative
// number. The last step removes L*carry, with carry being signed.
void modL (uint8_t r[32], Limbtype x[64])
{
Limbtype carry;
int i, j;
// First remove the upper bytes by substracting x[i]*L << (i*8 - 4)
for (i = 63; i >= 32; --i) {
carry = 0;
for (j = i - 32; j < i - 12; ++j) {
x[j] += carry - 16 * x[i] * L[j - (i - 32)];
// Keep each limb between -128 and 127.
carry = (x[j] + 128) >> 8;