-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgf25519.c
1524 lines (1368 loc) · 41.9 KB
/
gf25519.c
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
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <immintrin.h>
#include "gf25519.h"
/*
* We work in field Z_p with p = 2^255-MQ. The code below can be
* used to work modulo any odd integer with that format, provided that
* the macros below are adapted accordingly, and subject to the
* following restrictions:
*
* MQ must be odd and in the 1..2147483647 range
* GF_INVT508 must contain 1/2^508 mod p (64-bit limbs, little-endian)
* If 2^255-MQ is not prime, GF_MODPRIME must be zero
*
* GF_MODPRIME, when non-zero, removes the final check of inversion success;
* this check is harmless but unnecessary when the modulus is prime.
*
* Exception: gf_inv_FLT(), if defined, uses an hardcoded addition chain
* for 2^255-19, and will not work properly for other moduli (prime or not).
*/
#define MQ 19
static const gf GF_INVT508 = {
0x29D6DEAB9CB8606C,
0x788DD408827B63FD,
0x3CFC744C965683E6,
0x24E016B1490AA31A
};
#define GF_MODPRIME 1
/*
* Constants below need not be modified if MQ is changed.
*/
static const gf GF_ZERO = { 0, 0, 0, 0 };
static const gf GF_ONE = { 1, 0, 0, 0 };
static const gf GF_P = {
-MQ, (uint64_t)-1, (uint64_t)-1, (uint64_t)-1 >> 1
};
/*
* Helpful macro to convert expressions to strings, for inline assembly.
*/
#define ST(x) ST_(x)
#define ST_(x) #x
/*
* If USE_ALT_REDUCTION is 1, then an alternate reduction step is used
* in gf_mul_inline() and gf_sqr_inline(). The alternate reduction step
* involves an extra multiplication, but reduces the length of a
* dependency path in case of repeated squarings; it is used
* systematically in gf_sqr_x_inline(), where it appears to save 2
* cycles per squaring. For non-repeated squarings and for multiplications,
* it may have adverse effects depending on where it is used (specifically,
* how the output values are used), hence we keep the possibility of
* disabling it.
*/
#ifndef USE_ALT_REDUCTION
#define USE_ALT_REDUCTION 1
#endif
/*
* A field element is represented as four limbs, in base 2^64. Operands
* and result may be up to 2^256-1.
* A _normalized_ value is in 0..p-1. gf_normalize() ensures normalization;
* it is called before encoding, and also when starting inversion.
*/
/* see gf25519.h */
void
gf_add(gf *d, const gf *a, const gf *b)
{
unsigned long long d0, d1, d2, d3;
unsigned char cc;
/*
* Compute sum over 256 bits + carry.
*/
cc = _addcarry_u64(0, a->v0, b->v0, &d0);
cc = _addcarry_u64(cc, a->v1, b->v1, &d1);
cc = _addcarry_u64(cc, a->v2, b->v2, &d2);
cc = _addcarry_u64(cc, a->v3, b->v3, &d3);
/*
* If there is a carry, subtract 2*p, i.e. add 2*MQ and
* (implicitly) subtract 2^256.
*/
cc = _addcarry_u64(0, d0, -(unsigned long long)cc & (2 * MQ), &d0);
cc = _addcarry_u64(cc, d1, 0, (unsigned long long *)&d->v1);
cc = _addcarry_u64(cc, d2, 0, (unsigned long long *)&d->v2);
cc = _addcarry_u64(cc, d3, 0, (unsigned long long *)&d->v3);
/*
* Usually, there won't be an extra carry here, and the
* implicit subtraction of 2^256 is enough. However, if the
* the original sum was at least 2^257 - 2*MQ, then this
* will show up as an extra carry here. In that case, the
* low limb (d0) is necessarily less than 2*MQ, and thus adding
* back 2*MQ to it will not trigger any carry.
*/
(void)_addcarry_u64(0, d0, -(unsigned long long)cc & (2 * MQ),
(unsigned long long *)&d->v0);
}
/* see gf25519.h */
void
gf_sub(gf *d, const gf *a, const gf *b)
{
unsigned long long d0, d1, d2, d3, e;
unsigned char cc;
/*
* Compute subtraction over 256 bits.
*/
cc = _subborrow_u64(0, a->v0, b->v0, &d0);
cc = _subborrow_u64(cc, a->v1, b->v1, &d1);
cc = _subborrow_u64(cc, a->v2, b->v2, &d2);
cc = _subborrow_u64(cc, a->v3, b->v3, &d3);
/*
* If the result is negative, add back 2*p = 2^256 - 2*MQ (which
* we do by subtracting 2*MQ).
*/
e = -(unsigned long long)cc;
cc = _subborrow_u64(0, d0, e & (2 * MQ), &d0);
cc = _subborrow_u64(cc, d1, 0, (unsigned long long *)&d->v1);
cc = _subborrow_u64(cc, d2, 0, (unsigned long long *)&d->v2);
cc = _subborrow_u64(cc, d3, 0, (unsigned long long *)&d->v3);
/*
* If there is still a carry, then we must add 2*p again. In that
* case, subtracting 2*MQ from the low limb won't trigger a carry.
*/
d->v0 = d0 - (-(unsigned long long)cc & (2 * MQ));
}
/* see gf25519.h */
void
gf_neg(gf *d, const gf *a)
{
unsigned long long d0, d1, d2, d3, e;
unsigned char cc;
/*
* Compute 2*p - a over 256 bits.
*/
cc = _subborrow_u64(0, (unsigned long long)-(2 * MQ), a->v0, &d0);
cc = _subborrow_u64(cc, (unsigned long long)-1, a->v1, &d1);
cc = _subborrow_u64(cc, (unsigned long long)-1, a->v2, &d2);
cc = _subborrow_u64(cc, (unsigned long long)-1, a->v3, &d3);
/*
* If the result is negative, add back p = 2^255 - MQ.
*/
e = -(unsigned long long)cc;
cc = _addcarry_u64(0, d0, e & -MQ, (unsigned long long *)&d->v0);
cc = _addcarry_u64(cc, d1, e, (unsigned long long *)&d->v1);
cc = _addcarry_u64(cc, d2, e, (unsigned long long *)&d->v2);
(void)_addcarry_u64(cc, d3, e >> 1, (unsigned long long *)&d->v3);
}
/* see gf25519.h */
void
gf_condneg(gf *d, const gf *a, uint64_t ctl)
{
gf t;
gf_neg(&t, a);
d->v0 = a->v0 ^ (-ctl & (a->v0 ^ t.v0));
d->v1 = a->v1 ^ (-ctl & (a->v1 ^ t.v1));
d->v2 = a->v2 ^ (-ctl & (a->v2 ^ t.v2));
d->v3 = a->v3 ^ (-ctl & (a->v3 ^ t.v3));
}
#if 0
/* gf_mul() with no assembly, only 'unsigned __int128'. */
#define M64(lo, hi, x, y) do { \
unsigned __int128 m64_tmp; \
m64_tmp = (unsigned __int128)(x) * (unsigned __int128)(y); \
(lo) = (unsigned long long)m64_tmp; \
(hi) = (unsigned long long)(m64_tmp >> 64); \
} while (0)
#define M64_ADD(lo, hi, x, y, t1) do { \
unsigned __int128 m64_tmp; \
m64_tmp = (unsigned __int128)(x) * (unsigned __int128)(y) \
+ (unsigned __int128)(t1); \
(lo) = (unsigned long long)m64_tmp; \
(hi) = (unsigned long long)(m64_tmp >> 64); \
} while (0)
#define M64_ADD2(lo, hi, x, y, t1, t2) do { \
unsigned __int128 m64_tmp; \
m64_tmp = (unsigned __int128)(x) * (unsigned __int128)(y) \
+ (unsigned __int128)(t1) + (unsigned __int128)(t2); \
(lo) = (unsigned long long)m64_tmp; \
(hi) = (unsigned long long)(m64_tmp >> 64); \
} while (0)
void
gf_mul(gf *d, const gf *a, const gf *b)
{
unsigned long long t0, t1, t2, t3, t4, t5, t6, t7;
unsigned long long m, h;
unsigned char cc;
/*
* Compute 512-bit product in t0..t7.
*/
m = a->v0;
M64(t0, h, m, b->v0);
M64_ADD(t1, h, m, b->v1, h);
M64_ADD(t2, h, m, b->v2, h);
M64_ADD(t3, t4, m, b->v3, h);
m = a->v1;
M64_ADD(t1, h, m, b->v0, t1);
M64_ADD2(t2, h, m, b->v1, t2, h);
M64_ADD2(t3, h, m, b->v2, t3, h);
M64_ADD2(t4, t5, m, b->v3, t4, h);
m = a->v2;
M64_ADD(t2, h, m, b->v0, t2);
M64_ADD2(t3, h, m, b->v1, t3, h);
M64_ADD2(t4, h, m, b->v2, t4, h);
M64_ADD2(t5, t6, m, b->v3, t5, h);
m = a->v3;
M64_ADD(t3, h, m, b->v0, t3);
M64_ADD2(t4, h, m, b->v1, t4, h);
M64_ADD2(t5, h, m, b->v2, t5, h);
M64_ADD2(t6, t7, m, b->v3, t6, h);
/*
* Reduction, first pass.
*/
m = 2 * MQ;
M64_ADD(t0, h, m, t4, t0);
M64_ADD2(t1, h, m, t5, t1, h);
M64_ADD2(t2, h, m, t6, t2, h);
M64_ADD2(t3, t4, m, t7, t3, h);
/*
* Reduction, second pass. Since there's only a single word (t4),
* most of it is additions. Note that t4 < 2*MQ, and 4*MQ^2 < 2^64;
* therefore, we can use a simple multiplication on 64 bits.
*/
cc = _addcarry_u64(0, t0, m * t4, &t0);
cc = _addcarry_u64(cc, t1, 0, (unsigned long long *)&d->v1);
cc = _addcarry_u64(cc, t2, 0, (unsigned long long *)&d->v2);
cc = _addcarry_u64(cc, t3, 0, (unsigned long long *)&d->v3);
/*
* If there is a remaining carry here, then we have to propagate
* it back. But in that case, t0 < 4*MQ^2 and adding 2*MQ cannot
* overflow (4*MQ^2 + 2*MQ < 2^64).
*/
d->v0 = t0 + (-(uint64_t)cc & (2 * MQ));
}
#endif
__attribute__((used))
static const uint64_t global_m63 = 0x7FFFFFFFFFFFFFFF;
/*
* Inline version of multiplication, using assembly.
*/
__attribute__((always_inline))
static inline void
gf_mul_inline(gf *d, const gf *a, const gf *b)
{
__asm__ __volatile__ (
/*
* We compute the 512-bit result into r8..r15. Carry
* word is in rax. Multiplier word is in rdx (since that's
* what mulx requires). Note that mulx does not affect
* flags.
*/
/* Clear r15. This also clears CF and OF */
"xorq %%r15, %%r15\n\t"
/* a0*b -> r8..r12 */
"movq (%%rsi), %%rdx\n\t"
"mulx (%%rcx), %%r8, %%r9\n\t"
"mulx 8(%%rcx), %%rax, %%r10\n\t"
"adox %%rax, %%r9\n\t"
"mulx 16(%%rcx), %%rax, %%r11\n\t"
"adox %%rax, %%r10\n\t"
"mulx 24(%%rcx), %%rax, %%r12\n\t"
"adox %%rax, %%r11\n\t"
"adox %%r15, %%r12\n\t"
/* This last adox cannot overflow, and thus OF=0 */
/* a1*b -> added to r9..r13 */
"movq 8(%%rsi), %%rdx\n\t"
"mulx (%%rcx), %%rax, %%rbx\n\t"
"adcx %%rax, %%r9\n\t"
"adox %%rbx, %%r10\n\t"
"mulx 8(%%rcx), %%rax, %%rbx\n\t"
"adcx %%rax, %%r10\n\t"
"adox %%rbx, %%r11\n\t"
"mulx 16(%%rcx), %%rax, %%rbx\n\t"
"adcx %%rax, %%r11\n\t"
"adox %%rbx, %%r12\n\t"
"mulx 24(%%rcx), %%rax, %%r13\n\t"
"adcx %%rax, %%r12\n\t"
"adox %%r15, %%r13\n\t"
"adcx %%r15, %%r13\n\t"
/* Again, the last adcx and adox cannot overflow,
therefore CF=0 and OF=0 at this point */
/* a2*b -> added to r10..r14 */
"movq 16(%%rsi), %%rdx\n\t"
"mulx (%%rcx), %%rax, %%rbx\n\t"
"adcx %%rax, %%r10\n\t"
"adox %%rbx, %%r11\n\t"
"mulx 8(%%rcx), %%rax, %%rbx\n\t"
"adcx %%rax, %%r11\n\t"
"adox %%rbx, %%r12\n\t"
"mulx 16(%%rcx), %%rax, %%rbx\n\t"
"adcx %%rax, %%r12\n\t"
"adox %%rbx, %%r13\n\t"
"mulx 24(%%rcx), %%rax, %%r14\n\t"
"adcx %%rax, %%r13\n\t"
"adox %%r15, %%r14\n\t"
"adcx %%r15, %%r14\n\t"
/* a3*b -> added to r11..r15 */
"movq 24(%%rsi), %%rdx\n\t"
"xorq %%rsi, %%rsi\n\t"
"mulx (%%rcx), %%rax, %%rbx\n\t"
"adcx %%rax, %%r11\n\t"
"adox %%rbx, %%r12\n\t"
"mulx 8(%%rcx), %%rax, %%rbx\n\t"
"adcx %%rax, %%r12\n\t"
"adox %%rbx, %%r13\n\t"
"mulx 16(%%rcx), %%rax, %%rbx\n\t"
"adcx %%rax, %%r13\n\t"
"adox %%rbx, %%r14\n\t"
"mulx 24(%%rcx), %%rax, %%r15\n\t"
"adcx %%rax, %%r14\n\t"
"adox %%rsi, %%r15\n\t"
"adcx %%rsi, %%r15\n\t"
/* Multiply high words by 2*MQ and fold them on low words. */
"movl $(" ST(2 * MQ) "), %%edx\n\t"
"mulx %%r12, %%rax, %%r12\n\t"
"adcx %%rax, %%r8\n\t"
"adox %%r12, %%r9\n\t"
"mulx %%r13, %%rax, %%r13\n\t"
"adcx %%rax, %%r9\n\t"
"adox %%r13, %%r10\n\t"
"mulx %%r14, %%rax, %%r14\n\t"
"adcx %%rax, %%r10\n\t"
"adox %%r14, %%r11\n\t"
"mulx %%r15, %%rax, %%r15\n\t"
"adcx %%rax, %%r11\n\t"
"adox %%rsi, %%r15\n\t"
"adcx %%rsi, %%r15\n\t"
#if USE_ALT_REDUCTION
/* Augment extra word with top bit, and multiply by MQ
to fold. This is extra work right now, but removes
an extra dependency later on. */
"shld $1, %%r11, %%r15\n\t"
"andq global_m63(%%rip), %%r11\n\t"
"imulq $(" ST(MQ) "), %%r15, %%r15\n\t"
"addq %%r15, %%r8\n\t"
"adcq %%rsi, %%r9\n\t"
"adcq %%rsi, %%r10\n\t"
"adcq %%rsi, %%r11\n\t"
/* There cannot be a carry here, because top limb was
only 63 bits. */
#else
/* Extra word is in r15, multiply by 2*MQ and fold. */
"mulx %%r15, %%rax, %%rbx\n\t"
"adcx %%rax, %%r8\n\t"
"adcx %%rsi, %%r9\n\t"
"adcx %%rsi, %%r10\n\t"
"adcx %%rsi, %%r11\n\t"
/* If there is still a carry, fold it again. This time,
there cannot be a carry beyond the first limb. */
"cmovc %%rdx, %%rsi\n\t"
"addq %%rsi, %%r8\n\t"
#endif
/* Write result into output structure. */
"movq %%r8, (%0)\n\t"
"movq %%r9, 8(%0)\n\t"
"movq %%r10, 16(%0)\n\t"
"movq %%r11, 24(%0)\n\t"
: "=D" (d), "=S" (a), "=c" (b)
: "0" (d), "1" (a), "2" (b)
: "cc", "memory", "rax", "rbx", "rdx", "r8", "r9",
"r10", "r11", "r12", "r13", "r14", "r15"
);
}
/* see gf25519.h */
__attribute__((noinline))
void
gf_mul(gf *d, const gf *a, const gf *b)
{
gf_mul_inline(d, a, b);
}
/*
* Inline version of squarings, using assembly.
*/
__attribute__((always_inline))
static inline void
gf_sqr_inline(gf *d, const gf *a)
{
__asm__ __volatile__ (
/*
* We compute the 512-bit result into r8..r15. Carry
* word is in rax. Multiplier word is in rdx (since that's
* what mulx requires). Note that mulx does not affect
* flags.
*/
/* Load a0 into rdx */
"movq (%1), %%rdx\n\t"
/* Clear r15, CF and OF. */
"xorl %%r15d, %%r15d\n\t"
/* a0*a1 -> r9:r10
a0*a2 -> r10:r11
a0*a3 -> r11:r12 */
"mulx 8(%1), %%r9, %%r10\n\t"
"mulx 16(%1), %%r13, %%r11\n\t"
"mulx 24(%1), %%r14, %%r12\n\t"
"adcx %%r13, %%r10\n\t" // CF for r11
"adox %%r14, %%r11\n\t" // OF for r12
/* a1*a2 -> r11:r12
a1*a3 -> r12:r13 */
"movq 8(%1), %%rdx\n\t"
"mulx 16(%1), %%rax, %%rbx\n\t"
"mulx 24(%1), %%rcx, %%r13\n\t"
"adcx %%rax, %%r11\n\t" // CF for r12
"adox %%rbx, %%r12\n\t" // OF for r13
"adcx %%rcx, %%r12\n\t" // CF for r13
/* a2*a3 -> r13:r14 */
"movq 16(%1), %%rdx\n\t"
"mulx 24(%1), %%rax, %%r14\n\t"
"adox %%rax, %%r13\n\t" // OF for r14
"adcx %%r15, %%r13\n\t" // CF for r14
"adox %%r15, %%r14\n\t" // OF=0
"adcx %%r15, %%r14\n\t" // CF=0
/* We have the cross products in place, but we must
double them. This is a shift, and we use shl/shld
since it avoids carry propagation delays. */
"shld $1, %%r14, %%r15\n\t"
"shld $1, %%r13, %%r14\n\t"
"shld $1, %%r12, %%r13\n\t"
"shld $1, %%r11, %%r12\n\t"
"shld $1, %%r10, %%r11\n\t"
"shld $1, %%r9, %%r10\n\t"
"addq %%r9, %%r9\n\t"
/* Clear CF and OF. r8 was not used yet. */
"xorl %%r8d, %%r8d\n\t"
/* Now add a0^2, a1^2, a2^2 and a3^3
We still have a2 in rdx */
"mulx %%rdx, %%rax, %%rbx\n\t"
"movq (%1), %%rdx\n\t"
"mulx %%rdx, %%r8, %%rcx\n\t"
"adcx %%rcx, %%r9\n\t"
"movq 8(%1), %%rdx\n\t"
"mulx %%rdx, %%rdx, %%rcx\n\t"
"adcx %%rdx, %%r10\n\t"
"adcx %%rcx, %%r11\n\t"
"adcx %%rax, %%r12\n\t"
"adcx %%rbx, %%r13\n\t"
"movq 24(%1), %%rdx\n\t"
"mulx %%rdx, %%rax, %%rbx\n\t"
"adcx %%rax, %%r14\n\t"
"adcx %%rbx, %%r15\n\t"
/* At that point, CF=0 and OF=0.
We have the 512-bit square in r8..r15. */
/* Clear rsi. */
"xorl %%esi, %%esi\n\t"
/* Multiply high words by 2*MQ and fold them on low words. */
"movl $(" ST(2 * MQ) "), %%edx\n\t"
"mulx %%r12, %%rax, %%r12\n\t"
"adcx %%rax, %%r8\n\t"
"adox %%r12, %%r9\n\t"
"mulx %%r13, %%rax, %%r13\n\t"
"adcx %%rax, %%r9\n\t"
"adox %%r13, %%r10\n\t"
"mulx %%r14, %%rax, %%r14\n\t"
"adcx %%rax, %%r10\n\t"
"adox %%r14, %%r11\n\t"
"mulx %%r15, %%rax, %%r15\n\t"
"adcx %%rax, %%r11\n\t"
"adox %%rsi, %%r15\n\t"
"adcx %%rsi, %%r15\n\t"
#if USE_ALT_REDUCTION
/* Augment extra word with top bit, and multiply by MQ
to fold. This is extra work right now, but removes
an extra dependency later on. */
"shld $1, %%r11, %%r15\n\t"
"andq global_m63(%%rip), %%r11\n\t"
"imulq $(" ST(MQ) "), %%r15, %%r15\n\t"
"addq %%r15, %%r8\n\t"
"adcq %%rsi, %%r9\n\t"
"adcq %%rsi, %%r10\n\t"
"adcq %%rsi, %%r11\n\t"
/* There cannot be a carry here, because top limb was
only 63 bits. */
#else
/* Extra word is in r15, multiply by 2*MQ and fold. */
"mulx %%r15, %%rax, %%rbx\n\t"
"adcx %%rax, %%r8\n\t"
"adcx %%rsi, %%r9\n\t"
"adcx %%rsi, %%r10\n\t"
"adcx %%rsi, %%r11\n\t"
/* If there is still a carry, fold it again. This time,
there cannot be a carry beyond the first limb. */
"cmovc %%rdx, %%rsi\n\t"
"addq %%rsi, %%r8\n\t"
#endif
/* Write result into output structure. */
"movq %%r8, (%0)\n\t"
"movq %%r9, 8(%0)\n\t"
"movq %%r10, 16(%0)\n\t"
"movq %%r11, 24(%0)\n\t"
: "=D" (d), "=S" (a)
: "0" (d), "1" (a)
: "cc", "memory", "rax", "rbx", "rcx", "rdx", "r8", "r9",
"r10", "r11", "r12", "r13", "r14", "r15"
);
}
/* see gf25519.h */
__attribute__((noinline))
void
gf_sqr(gf *d, const gf *a)
{
gf_sqr_inline(d, a);
}
/*
* Inline version of the multiple squarings function, using assembly.
*/
__attribute__((always_inline))
static void
gf_sqr_x_inline(gf *d, const gf *a, long num)
{
__asm__ __volatile__ (
/*
* Load a0..a3 into rax:rbx:rcx:rbp
* Then reuse esi as loop counter.
*/
"movq (%%rsi), %%rax\n\t"
"movq 8(%%rsi), %%rbx\n\t"
"movq 16(%%rsi), %%rcx\n\t"
"movq 24(%%rsi), %%rbp\n\t"
"movl %%edx, %%esi\n\t"
/* Loop entry. */
"0:\n\t"
/* Load a0 into rdx */
"movq %%rax, %%rdx\n\t"
/* Clear r15, CF and OF. */
"xorl %%r15d, %%r15d\n\t"
/* a0*a1 -> r9:r10
a0*a2 -> r10:r11
a0*a3 -> r11:r12 */
"mulx %%rbx, %%r9, %%r10\n\t"
"mulx %%rcx, %%r13, %%r11\n\t"
"mulx %%rbp, %%r14, %%r12\n\t"
"adcx %%r13, %%r10\n\t" // CF for r11
"adox %%r14, %%r11\n\t" // OF for r12
/* a1*a2 -> r11:r12
a1*a3 -> r12:r13 */
"movq %%rbx, %%rdx\n\t"
"mulx %%rcx, %%r8, %%r14\n\t"
"adcx %%r8, %%r11\n\t" // CF for r12
"mulx %%rbp, %%r8, %%r13\n\t"
"adox %%r14, %%r12\n\t" // OF for r13
"adcx %%r8, %%r12\n\t" // CF for r13
/* a2*a3 -> r13:r14 */
"movq %%rcx, %%rdx\n\t"
"mulx %%rbp, %%r8, %%r14\n\t"
"adox %%r8, %%r13\n\t" // OF for r14
"adcx %%r15, %%r13\n\t" // CF for r14
"adox %%r15, %%r14\n\t" // OF=0
"adcx %%r15, %%r14\n\t" // CF=0
/* We have the cross products in place, but we must
double them. */
"shld $1, %%r14, %%r15\n\t"
"shld $1, %%r13, %%r14\n\t"
"shld $1, %%r12, %%r13\n\t"
"shld $1, %%r11, %%r12\n\t"
"shld $1, %%r10, %%r11\n\t"
"shld $1, %%r9, %%r10\n\t"
"addq %%r9, %%r9\n\t"
/* Clear CF and OF. r8 is still scratch at this point. */
"xorl %%r8d, %%r8d\n\t"
/* Now add a0^2, a1^2, a2^2 and a3^2 */
"movq %%rax, %%rdx\n\t"
"mulx %%rax, %%r8, %%rax\n\t"
"adcx %%rax, %%r9\n\t"
"movq %%rbx, %%rdx\n\t"
"mulx %%rbx, %%rax, %%rbx\n\t"
"adcx %%rax, %%r10\n\t"
"adcx %%rbx, %%r11\n\t"
"movq %%rcx, %%rdx\n\t"
"mulx %%rcx, %%rax, %%rbx\n\t"
"adcx %%rax, %%r12\n\t"
"adcx %%rbx, %%r13\n\t"
"movq %%rbp, %%rdx\n\t"
"mulx %%rbp, %%rax, %%rbx\n\t"
"adcx %%rax, %%r14\n\t"
"adcx %%rbx, %%r15\n\t"
/* At that point, CF=0 and OF=0.
We have the 512-bit square in r8..r15. */
/* Multiply high words by 2*MQ and fold them on low words.
At the same time, we move the resulting low words into
rax:rbx:rcx:rbp */
"movl $(" ST(2 * MQ) "), %%edx\n\t"
"mulx %%r12, %%rax, %%r12\n\t"
"adcx %%r8, %%rax\n\t"
"adox %%r12, %%r9\n\t"
"mulx %%r13, %%rbx, %%r13\n\t"
"adcx %%r9, %%rbx\n\t"
"adox %%r13, %%r10\n\t"
"mulx %%r14, %%rcx, %%r14\n\t"
"adcx %%r10, %%rcx\n\t"
"movl $0, %%r10d\n\t"
"adox %%r14, %%r11\n\t"
"mulx %%r15, %%rbp, %%r15\n\t"
"adcx %%r11, %%rbp\n\t"
"adox %%r10, %%r15\n\t"
"adcx %%r10, %%r15\n\t"
/* Augment extra word with top bit, and multiply by MQ
to fold. This is extra work right now, but removes
an extra dependency later on. */
"shld $1, %%rbp, %%r15\n\t"
"andq global_m63(%%rip), %%rbp\n\t"
"imulq $(" ST(MQ) "), %%r15, %%r15\n\t"
"addq %%r15, %%rax\n\t"
"adcq %%r10, %%rbx\n\t"
"adcq %%r10, %%rcx\n\t"
"adcq %%r10, %%rbp\n\t"
/* There cannot be a carry here, because top limb was
only 63 bits. */
/*
* Loop until all squares have been performed.
*/
"decl %%esi\n\t"
"jnz 0b\n\t"
/*
* Write result.
*/
"movq %%rax, (%%rdi)\n\t"
"movq %%rbx, 8(%%rdi)\n\t"
"movq %%rcx, 16(%%rdi)\n\t"
"movq %%rbp, 24(%%rdi)\n\t"
: "=D" (d), "=S" (a), "=d" (num)
: "0" (d), "1" (a), "2" (num)
: "cc", "memory", "rax", "rbx", "rcx", "rbp", "r8", "r9",
"r10", "r11", "r12", "r13", "r14", "r15"
);
}
/* see gf25519.h */
__attribute__((noinline))
void
gf_sqr_x(gf *d, const gf *a, long num)
{
gf_sqr_x_inline(d, a, num);
}
/*
* Normalize a value to 0..p-1.
*/
static void
gf_normalize(gf *d, const gf *a)
{
unsigned long long d0, d1, d2, d3, e;
unsigned char cc;
/*
* If top bit is set, propagate it.
*/
e = -(unsigned long long)(a->v3 >> 63);
cc = _addcarry_u64(0, a->v0, e & MQ, &d0);
cc = _addcarry_u64(cc, a->v1, 0, &d1);
cc = _addcarry_u64(cc, a->v2, 0, &d2);
(void)_addcarry_u64(cc, a->v3, e << 63, &d3);
/*
* Value is now at most 2^255+MQ-1. Subtract p, and add it back
* if the result is negative.
*/
cc = _subborrow_u64(0, d0, (unsigned long long)-MQ, &d0);
cc = _subborrow_u64(cc, d1, (unsigned long long)-1, &d1);
cc = _subborrow_u64(cc, d2, (unsigned long long)-1, &d2);
cc = _subborrow_u64(cc, d3, (unsigned long long)-1 >> 1, &d3);
e = -(unsigned long long)cc;
cc = _addcarry_u64(0, d0, e & -MQ, (unsigned long long *)&d->v0);
cc = _addcarry_u64(cc, d1, e, (unsigned long long *)&d->v1);
cc = _addcarry_u64(cc, d2, e, (unsigned long long *)&d->v2);
(void)_addcarry_u64(cc, d3, e >> 1, (unsigned long long *)&d->v3);
}
/* see gf25519.h */
uint64_t
gf_iszero(const gf *a)
{
unsigned long long t0, t1, t2;
/*
* Since values are over 256 bits, there are three possible
* representations for 0: 0, p and 2*p.
*/
t0 = a->v0 | a->v1 | a->v2 | a->v3;
t1 = (a->v0 + MQ) | ~a->v1 | ~a->v2 | (a->v3 ^ 0x7FFFFFFFFFFFFFFF);
t2 = (a->v0 + (2 * MQ)) | ~a->v1 | ~a->v2 | ~a->v3;
/*
* Value is zero if and only if one of t0, t1 or t2 is zero.
*/
return 1 - (((t0 | -t0) & (t1 | -t1) & (t2 | -t2)) >> 63);
}
/* see gf25519.h */
uint64_t
gf_eq(const gf *a, const gf *b)
{
gf t;
gf_sub(&t, a, b);
return gf_iszero(&t);
}
#if defined GF_INV_FLT && GF_INV_FLT
/*
* If GF_INV_FLT is at least 2, then override calls to gf_mul(),
* gf_sqr() and gf_sqr_x() to use the inline versions. This makes the
* resulting binary quite larger, but also slightly faster (not fast
* enough to compete with gf_inv(), though).
*/
#if GF_INV_FLT >= 2
#define gf_sqr gf_sqr_inline
#define gf_sqr_x gf_sqr_x_inline
#define gf_mul gf_mul_inline
#endif
/* see gf25519.h */
uint64_t
gf_inv_FLT(gf *d, const gf *a)
{
/*
* This code works only for p = 2^255-19. Inversion is done
* by raising to the power p-2 = 2^255-21; since p is prime,
* this yields the right result (and it yields 0 when input
* is 0, which is the expected behaviour in that case).
*
* For the exponent, an addition chain is used that uses
* 254 squarings and 11 extra multiplications. This is the
* addition chain from the original Curve25519 reference code.
*/
gf x, y, z, t;
/* x <- a^2 */
gf_sqr(&x, a);
/* y <- a^9 */
gf_sqr_x(&y, &x, 2);
gf_mul(&y, &y, a);
/* x <- a^11 */
gf_mul(&x, &y, &x);
/* y <- a^31 */
gf_sqr(&z, &x);
gf_mul(&y, &y, &z);
/* y <- a^1023 = a^(2^10-1) */
gf_sqr_x(&z, &y, 5);
gf_mul(&y, &z, &y);
/* z <- a^(2^20-1) */
gf_sqr_x(&z, &y, 10);
gf_mul(&z, &z, &y);
/* z <- a^(2^40-1) */
gf_sqr_x(&t, &z, 20);
gf_mul(&z, &z, &t);
/* z <- a^(2^50-1) */
gf_sqr_x(&z, &z, 10);
gf_mul(&z, &z, &y);
/* y <- a^(2^100-1) */
gf_sqr_x(&y, &z, 50);
gf_mul(&y, &y, &z);
/* y <- a^(2^200-1) */
gf_sqr_x(&t, &y, 100);
gf_mul(&y, &y, &t);
/* y <- a^(2^250-1) */
gf_sqr_x(&y, &y, 50);
gf_mul(&y, &y, &z);
/* d <- a^(2^255-19) */
gf_sqr_x(&y, &y, 5);
gf_mul(d, &y, &x);
/* If value was invertible, we get a non-zero here. If value
was not invertible, it was zero, and we get a zero result. */
return gf_iszero(d) ^ 1;
}
/*
* Remove overrides for force-inlining (if defined previously).
*/
#undef gf_sqr
#undef gf_sqr_x
#undef gf_mul
#endif
/*
* Compute (a*f+b*g)/2^31. Parameters f and g are provided with an
* unsigned type, but they are signed integers in the -2^31..+2^31
* range. Values a, b and d are not field elements, but signed 256-bit
* integers (i.e. top bit is the sign bit) which are nonnegative (value
* is between 0 and 2^255-1). The division by 2^31 is assumed to be
* exact (low j bits of a*f+b*g are dropped). The result is assumed to
* fit in 256 bits, including the sign bit (truncation is applied on
* higher bits).
*
* If the result turns out to be negative, then it is negated. Returned
* value is 1 if the result was negated, 0 otherwise.
*/
static inline uint64_t
s256_lin_div31_abs(gf *d, const gf *a, const gf *b,
unsigned long long f, unsigned long long g)
{
gf ta, tb;
unsigned long long sf, sg, d0, d1, d2, d3, t;
unsigned __int128 z;
unsigned char cc;
/*
* If f < 0, replace f with -f but keep the sign in sf.
* Similarly for g.
*/
sf = f >> 63;
f = (f ^ -sf) + sf;
sg = g >> 63;
g = (g ^ -sg) + sg;
/*
* Apply signs sf and sg to a and b, respectively.
*/
cc = _addcarry_u64(0, a->v0 ^ -sf, sf, (unsigned long long *)&ta.v0);
cc = _addcarry_u64(cc, a->v1 ^ -sf, 0, (unsigned long long *)&ta.v1);
cc = _addcarry_u64(cc, a->v2 ^ -sf, 0, (unsigned long long *)&ta.v2);
cc = _addcarry_u64(cc, a->v3 ^ -sf, 0, (unsigned long long *)&ta.v3);
cc = _addcarry_u64(0, b->v0 ^ -sg, sg, (unsigned long long *)&tb.v0);
cc = _addcarry_u64(cc, b->v1 ^ -sg, 0, (unsigned long long *)&tb.v1);
cc = _addcarry_u64(cc, b->v2 ^ -sg, 0, (unsigned long long *)&tb.v2);
cc = _addcarry_u64(cc, b->v3 ^ -sg, 0, (unsigned long long *)&tb.v3);
/*
* Now that f and g are nonnegative, compute a*f+b*g into
* d0:d1:d2:d3:t. Since f and g are at most 2^31, we can
* add two 128-bit products with no overflow (they are actually
* 95 bits each at most).
*/
z = (unsigned __int128)ta.v0 * (unsigned __int128)f
+ (unsigned __int128)tb.v0 * (unsigned __int128)g;
d0 = (unsigned long long)z;
t = (unsigned long long)(z >> 64);
z = (unsigned __int128)ta.v1 * (unsigned __int128)f
+ (unsigned __int128)tb.v1 * (unsigned __int128)g
+ (unsigned __int128)t;
d1 = (unsigned long long)z;
t = (unsigned long long)(z >> 64);
z = (unsigned __int128)ta.v2 * (unsigned __int128)f
+ (unsigned __int128)tb.v2 * (unsigned __int128)g
+ (unsigned __int128)t;
d2 = (unsigned long long)z;
t = (unsigned long long)(z >> 64);
z = (unsigned __int128)ta.v3 * (unsigned __int128)f
+ (unsigned __int128)tb.v3 * (unsigned __int128)g
+ (unsigned __int128)t;
d3 = (unsigned long long)z;
t = (unsigned long long)(z >> 64);
/*
* Don't forget the signs: if a < 0, then the result is
* overestimated by 2^256*f; similarly, if b < 0, then the
* result is overestimated by 2^256*g. We thus must subtract
* 2^256*(sa*f+sb*g), where sa and sb are the signs of a and b,
* respectively.
*/
t -= -(unsigned long long)(ta.v3 >> 63) & f;
t -= -(unsigned long long)(tb.v3 >> 63) & g;
/*
* Apply the shift.
*/
d0 = (d0 >> 31) | (d1 << 33);
d1 = (d1 >> 31) | (d2 << 33);
d2 = (d2 >> 31) | (d3 << 33);
d3 = (d3 >> 31) | (t << 33);
/*
* Perform conditional negation, if the result is negative.
*/
t >>= 63;
cc = _addcarry_u64(0, d0 ^ -t, t, (unsigned long long *)&d->v0);
cc = _addcarry_u64(cc, d1 ^ -t, 0, (unsigned long long *)&d->v1);
cc = _addcarry_u64(cc, d2 ^ -t, 0, (unsigned long long *)&d->v2);
(void)_addcarry_u64(cc, d3 ^ -t, 0, (unsigned long long *)&d->v3);
return t;
}
/*
* Compute u*f+v*g (modulo p). Parameters f and g are provided with
* an unsigned type, but they are signed integers in the -2^62..+2^62 range.
*/
static inline void
gf_lin(gf *d, const gf *u, const gf *v,
unsigned long long f, unsigned long long g)
{
gf tu, tv;
unsigned long long sf, sg, d0, d1, d2, d3, t;
unsigned __int128 z;
unsigned char cc;
/*
* If f < 0, replace f with -f but keep the sign in sf.
* Similarly for g.
*/
sf = f >> 63;
f = (f ^ -sf) + sf;
sg = g >> 63;
g = (g ^ -sg) + sg;
/*
* Apply signs sf and sg to u and v.
*/
gf_condneg(&tu, u, sf);
gf_condneg(&tv, v, sg);
/*
* Since f <= 2^62 and g <= 2^62, we can add 128-bit products: they
* fit on 126 bits each.
*/
z = (unsigned __int128)tu.v0 * (unsigned __int128)f