-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkernel.cl
3179 lines (3004 loc) · 132 KB
/
kernel.cl
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
typedef struct
{
int X[10];
int Y[10];
int Z[10];
} ge_p2;
typedef struct
{
int X[10];
int Y[10];
int Z[10];
int T[10];
} ge_p3;
typedef struct
{
int X[10];
int Y[10];
int Z[10];
int T[10];
} ge_p1p1;
typedef struct
{
int yplusx[10];
int yminusx[10];
int xy2d[10];
} ge_precomp;
typedef unsigned long uint64_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
void ge_p3_tobytes(unsigned char *s, const ge_p3 *h);
void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q);
void ge_scalarmult_base(ge_p3 *h, const unsigned char *a);
void ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p);
void ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p);
void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p);
void ge_p3_0(ge_p3 *h);
void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p);
void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p);
void fe_0(int *h);
void fe_1(int *h);
void fe_tobytes(unsigned char *s, const int *h);
void fe_copy(int *h, const int *f);
int fe_isnegative(const int *f);
void fe_cmov(int *f, const int *g, unsigned int b);
void fe_neg(int *h, const int *f);
void fe_add(int *h, const int *f, const int *g);
void fe_invert(int *out, const int *z);
void fe_sq(int *h, const int *f);
void fe_sq2(int *h, const int *f);
void fe_mul(int *h, const int *f, const int *g);
void fe_sub(int *h, const int *f, const int *g);
void memcpy(void *dest, const void *src, int len)
{
uint8_t *d = dest;
const uint8_t *s = src;
while (len--)
*d++ = *s++;
}
// https://github.com/dragmz/libtomcrypt/blob/develop/src/hashes/sha2/sha512.c
// https://github.com/dragmz/libtomcrypt/blob/develop/src/headers/tomcrypt_private.h
// https://github.com/dragmz/libtomcrypt/blob/develop/src/headers/tomcrypt_macros.h
#define ROR64(value, bits) (((value) >> (bits)) | ((value) << (64 - (bits))))
#define LOAD64H(x, y) \
{ \
x = (((uint64_t)((y)[0] & 255)) << 56) | \
(((uint64_t)((y)[1] & 255)) << 48) | \
(((uint64_t)((y)[2] & 255)) << 40) | \
(((uint64_t)((y)[3] & 255)) << 32) | \
(((uint64_t)((y)[4] & 255)) << 24) | \
(((uint64_t)((y)[5] & 255)) << 16) | \
(((uint64_t)((y)[6] & 255)) << 8) | (((uint64_t)((y)[7] & 255))); \
}
#define STORE64H(x, y) \
{ \
(y)[0] = (uint8_t)(((x) >> 56) & 255); \
(y)[1] = (uint8_t)(((x) >> 48) & 255); \
(y)[2] = (uint8_t)(((x) >> 40) & 255); \
(y)[3] = (uint8_t)(((x) >> 32) & 255); \
(y)[4] = (uint8_t)(((x) >> 24) & 255); \
(y)[5] = (uint8_t)(((x) >> 16) & 255); \
(y)[6] = (uint8_t)(((x) >> 8) & 255); \
(y)[7] = (uint8_t)((x) & 255); \
}
#define SHA512_HASH_SIZE (512 / 8)
typedef struct
{
uint64_t length;
uint64_t state[8];
uint8_t curlen;
uint8_t buf[128];
} sha512_ctx;
static const uint64_t K[80] = {
0x428a2f98d728ae22UL, 0x7137449123ef65cdUL, 0xb5c0fbcfec4d3b2fUL,
0xe9b5dba58189dbbcUL, 0x3956c25bf348b538UL, 0x59f111f1b605d019UL,
0x923f82a4af194f9bUL, 0xab1c5ed5da6d8118UL, 0xd807aa98a3030242UL,
0x12835b0145706fbeUL, 0x243185be4ee4b28cUL, 0x550c7dc3d5ffb4e2UL,
0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL, 0x9bdc06a725c71235UL,
0xc19bf174cf692694UL, 0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL,
0x0fc19dc68b8cd5b5UL, 0x240ca1cc77ac9c65UL, 0x2de92c6f592b0275UL,
0x4a7484aa6ea6e483UL, 0x5cb0a9dcbd41fbd4UL, 0x76f988da831153b5UL,
0x983e5152ee66dfabUL, 0xa831c66d2db43210UL, 0xb00327c898fb213fUL,
0xbf597fc7beef0ee4UL, 0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL,
0x06ca6351e003826fUL, 0x142929670a0e6e70UL, 0x27b70a8546d22ffcUL,
0x2e1b21385c26c926UL, 0x4d2c6dfc5ac42aedUL, 0x53380d139d95b3dfUL,
0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL, 0x81c2c92e47edaee6UL,
0x92722c851482353bUL, 0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL,
0xc24b8b70d0f89791UL, 0xc76c51a30654be30UL, 0xd192e819d6ef5218UL,
0xd69906245565a910UL, 0xf40e35855771202aUL, 0x106aa07032bbd1b8UL,
0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL, 0x2748774cdf8eeb99UL,
0x34b0bcb5e19b48a8UL, 0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL,
0x5b9cca4f7763e373UL, 0x682e6ff3d6b2b8a3UL, 0x748f82ee5defb2fcUL,
0x78a5636f43172f60UL, 0x84c87814a1f0ab72UL, 0x8cc702081a6439ecUL,
0x90befffa23631e28UL, 0xa4506cebde82bde9UL, 0xbef9a3f7b2c67915UL,
0xc67178f2e372532bUL, 0xca273eceea26619cUL, 0xd186b8c721c0c207UL,
0xeada7dd6cde0eb1eUL, 0xf57d4f7fee6ed178UL, 0x06f067aa72176fbaUL,
0x0a637dc5a2c898a6UL, 0x113f9804bef90daeUL, 0x1b710b35131c471bUL,
0x28db77f523047d84UL, 0x32caab7b40c72493UL, 0x3c9ebe0a15c9bebcUL,
0x431d67c49c100d4cUL, 0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL,
0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL};
#define BLOCK_SIZE 128
#define Ch(x, y, z) (z ^ (x & (y ^ z)))
#define Maj(x, y, z) (((x | y) & z) | (x & y))
#define S(x, n) ROR64(x, n)
#define R(x, n) (((x) & 0xFFFFFFFFFFFFFFFFUL) >> ((uint64_t)n))
#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
#define Sha512Round(a, b, c, d, e, f, g, h, i) \
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
t1 = Sigma0(a) + Maj(a, b, c); \
d += t0; \
h = t0 + t1;
static void sha512_process(sha512_ctx *ctx, uint8_t const *buf)
{
uint64_t S[8];
uint64_t W[80];
uint64_t t0;
uint64_t t1;
int i;
for (i = 0; i < 8; i++)
{
S[i] = ctx->state[i];
}
for (i = 0; i < 16; i++)
{
LOAD64H(W[i], buf + (8 * i));
}
for (i = 16; i < 80; i++)
{
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
}
for (i = 0; i < 80; i += 8)
{
Sha512Round(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i + 0);
Sha512Round(S[7], S[0], S[1], S[2], S[3], S[4], S[5], S[6], i + 1);
Sha512Round(S[6], S[7], S[0], S[1], S[2], S[3], S[4], S[5], i + 2);
Sha512Round(S[5], S[6], S[7], S[0], S[1], S[2], S[3], S[4], i + 3);
Sha512Round(S[4], S[5], S[6], S[7], S[0], S[1], S[2], S[3], i + 4);
Sha512Round(S[3], S[4], S[5], S[6], S[7], S[0], S[1], S[2], i + 5);
Sha512Round(S[2], S[3], S[4], S[5], S[6], S[7], S[0], S[1], i + 6);
Sha512Round(S[1], S[2], S[3], S[4], S[5], S[6], S[7], S[0], i + 7);
}
for (i = 0; i < 8; i++)
{
ctx->state[i] = ctx->state[i] + S[i];
}
}
void sha512256_init(sha512_ctx *ctx)
{
ctx->curlen = 0;
ctx->length = 0;
ctx->state[0] = 0x22312194FC2BF72CUL;
ctx->state[1] = 0x9F555FA3C84C64C2UL;
ctx->state[2] = 0x2393B86B6F53B151UL;
ctx->state[3] = 0x963877195940EABDUL;
ctx->state[4] = 0x96283EE2A88EFFE3UL;
ctx->state[5] = 0xBE5E1E2553863992UL;
ctx->state[6] = 0x2B0199FC2C85B8AAUL;
ctx->state[7] = 0x0EB72DDC81C52CA2UL;
}
void sha512_init(sha512_ctx *ctx)
{
ctx->curlen = 0;
ctx->length = 0;
ctx->state[0] = 0x6a09e667f3bcc908UL;
ctx->state[1] = 0xbb67ae8584caa73bUL;
ctx->state[2] = 0x3c6ef372fe94f82bUL;
ctx->state[3] = 0xa54ff53a5f1d36f1UL;
ctx->state[4] = 0x510e527fade682d1UL;
ctx->state[5] = 0x9b05688c2b3e6c1fUL;
ctx->state[6] = 0x1f83d9abfb41bd6bUL;
ctx->state[7] = 0x5be0cd19137e2179UL;
}
void sha512_finalise(sha512_ctx *ctx, unsigned char *Digest)
{
int i;
ctx->length += ctx->curlen * 8UL;
ctx->buf[ctx->curlen++] = (uint8_t)0x80;
while (ctx->curlen < 120)
{
ctx->buf[ctx->curlen++] = (uint8_t)0;
}
STORE64H(ctx->length, ctx->buf + 120);
sha512_process(ctx, ctx->buf);
for (i = 0; i < 8; i++)
{
STORE64H(ctx->state[i], Digest + (8 * i));
}
}
void sha512256_finalise(sha512_ctx *ctx, unsigned char *Digest)
{
int i;
ctx->length += ctx->curlen * 8UL;
ctx->buf[ctx->curlen++] = (uint8_t)0x80;
while (ctx->curlen < 120)
{
ctx->buf[ctx->curlen++] = (uint8_t)0;
}
STORE64H(ctx->length, ctx->buf + 120);
sha512_process(ctx, ctx->buf);
for (i = 0; i < 4; i++)
{
STORE64H(ctx->state[i], Digest + (8 * i));
}
}
void crypto_hash_sha512(void const *input, unsigned char *Digest, const int len)
{
sha512_ctx context;
sha512_init(&context);
memcpy(context.buf, input, len);
context.curlen += len;
sha512_finalise(&context, Digest);
}
void crypto_hash_sha512256(void const *input, unsigned char *Digest, const int len)
{
sha512_ctx context;
sha512256_init(&context);
memcpy(context.buf, input, len);
context.curlen += len;
sha512256_finalise(&context, Digest);
}
// https://lib25519.cr.yp.to/lib25519-20220726/crypto_dh/x25519/ref10/fe_0.c.html
void fe_0(int *h)
{
h[0] = 0;
h[1] = 0;
h[2] = 0;
h[3] = 0;
h[4] = 0;
h[5] = 0;
h[6] = 0;
h[7] = 0;
h[8] = 0;
h[9] = 0;
}
// https://lib25519.cr.yp.to/lib25519-20220726/crypto_dh/x25519/ref10/fe_1.c.html
void fe_1(int *h)
{
h[0] = 1;
h[1] = 0;
h[2] = 0;
h[3] = 0;
h[4] = 0;
h[5] = 0;
h[6] = 0;
h[7] = 0;
h[8] = 0;
h[9] = 0;
}
// https://lib25519.cr.yp.to/lib25519-20220726/crypto_sign/ed25519/ref10/fe_add.c.html
void fe_add(int *h, const int *f, const int *g)
{
int f0 = f[0];
int f1 = f[1];
int f2 = f[2];
int f3 = f[3];
int f4 = f[4];
int f5 = f[5];
int f6 = f[6];
int f7 = f[7];
int f8 = f[8];
int f9 = f[9];
int g0 = g[0];
int g1 = g[1];
int g2 = g[2];
int g3 = g[3];
int g4 = g[4];
int g5 = g[5];
int g6 = g[6];
int g7 = g[7];
int g8 = g[8];
int g9 = g[9];
int h0 = f0 + g0;
int h1 = f1 + g1;
int h2 = f2 + g2;
int h3 = f3 + g3;
int h4 = f4 + g4;
int h5 = f5 + g5;
int h6 = f6 + g6;
int h7 = f7 + g7;
int h8 = f8 + g8;
int h9 = f9 + g9;
h[0] = h0;
h[1] = h1;
h[2] = h2;
h[3] = h3;
h[4] = h4;
h[5] = h5;
h[6] = h6;
h[7] = h7;
h[8] = h8;
h[9] = h9;
}
// https://lib25519.cr.yp.to/lib25519-20220726/crypto_sign/ed25519/ref10/fe_cmov.c.html
void fe_cmov(int *f, const int *g, unsigned int b)
{
int f0 = f[0];
int f1 = f[1];
int f2 = f[2];
int f3 = f[3];
int f4 = f[4];
int f5 = f[5];
int f6 = f[6];
int f7 = f[7];
int f8 = f[8];
int f9 = f[9];
int g0 = g[0];
int g1 = g[1];
int g2 = g[2];
int g3 = g[3];
int g4 = g[4];
int g5 = g[5];
int g6 = g[6];
int g7 = g[7];
int g8 = g[8];
int g9 = g[9];
int x0 = f0 ^ g0;
int x1 = f1 ^ g1;
int x2 = f2 ^ g2;
int x3 = f3 ^ g3;
int x4 = f4 ^ g4;
int x5 = f5 ^ g5;
int x6 = f6 ^ g6;
int x7 = f7 ^ g7;
int x8 = f8 ^ g8;
int x9 = f9 ^ g9;
b = (unsigned int)(-(int)b);
x0 &= b;
x1 &= b;
x2 &= b;
x3 &= b;
x4 &= b;
x5 &= b;
x6 &= b;
x7 &= b;
x8 &= b;
x9 &= b;
f[0] = f0 ^ x0;
f[1] = f1 ^ x1;
f[2] = f2 ^ x2;
f[3] = f3 ^ x3;
f[4] = f4 ^ x4;
f[5] = f5 ^ x5;
f[6] = f6 ^ x6;
f[7] = f7 ^ x7;
f[8] = f8 ^ x8;
f[9] = f9 ^ x9;
}
// https://lib25519.cr.yp.to/lib25519-20230630/crypto_mGnP/ed25519/ref10/fe_copy.c.html
void fe_copy(int *h, const int *f)
{
int f0 = f[0];
int f1 = f[1];
int f2 = f[2];
int f3 = f[3];
int f4 = f[4];
int f5 = f[5];
int f6 = f[6];
int f7 = f[7];
int f8 = f[8];
int f9 = f[9];
h[0] = f0;
h[1] = f1;
h[2] = f2;
h[3] = f3;
h[4] = f4;
h[5] = f5;
h[6] = f6;
h[7] = f7;
h[8] = f8;
h[9] = f9;
}
// https://lib25519.cr.yp.to/lib25519-20230630/crypto_mGnP/ed25519/ref10/fe_invert.c.html
void fe_invert(int *out, const int *z)
{
int t0[10];
int t1[10];
int t2[10];
int t3[10];
int i;
// TODO: look into optimizing this:
// https://lib25519.cr.yp.to/lib25519-20230630/crypto_pow/inv25519/donna_c64/pow.c.html
// https://lib25519.cr.yp.to/lib25519-20230630/crypto_pow/inv25519/ref10/pow225521.h.html
fe_sq(t0, z);
for (i = 1; i < 1; ++i)
{
fe_sq(t0, t0);
}
fe_sq(t1, t0);
for (i = 1; i < 2; ++i)
{
fe_sq(t1, t1);
}
fe_mul(t1, z, t1);
fe_mul(t0, t0, t1);
fe_sq(t2, t0);
for (i = 1; i < 1; ++i)
{
fe_sq(t2, t2);
}
fe_mul(t1, t1, t2);
fe_sq(t2, t1);
for (i = 1; i < 5; ++i)
{
fe_sq(t2, t2);
}
fe_mul(t1, t2, t1);
fe_sq(t2, t1);
for (i = 1; i < 10; ++i)
{
fe_sq(t2, t2);
}
fe_mul(t2, t2, t1);
fe_sq(t3, t2);
for (i = 1; i < 20; ++i)
{
fe_sq(t3, t3);
}
fe_mul(t2, t3, t2);
fe_sq(t2, t2);
for (i = 1; i < 10; ++i)
{
fe_sq(t2, t2);
}
fe_mul(t1, t2, t1);
fe_sq(t2, t1);
for (i = 1; i < 50; ++i)
{
fe_sq(t2, t2);
}
fe_mul(t2, t2, t1);
fe_sq(t3, t2);
for (i = 1; i < 100; ++i)
{
fe_sq(t3, t3);
}
fe_mul(t2, t3, t2);
fe_sq(t2, t2);
for (i = 1; i < 50; ++i)
{
fe_sq(t2, t2);
}
fe_mul(t1, t2, t1);
fe_sq(t1, t1);
for (i = 1; i < 5; ++i)
{
fe_sq(t1, t1);
}
fe_mul(out, t1, t0);
}
// https://lib25519.cr.yp.to/lib25519-20230630/crypto_mGnP/ed25519/ref10/fe_isnegative.c.html
int fe_isnegative(const int *f)
{
unsigned char s[32];
fe_tobytes(s, f);
return s[0] & 1;
}
// https://lib25519.cr.yp.to/lib25519-20230630/crypto_mGnP/ed25519/ref10/fe_mul.c.html
void fe_mul(int *h, const int *f, const int *g)
{
int f0 = f[0];
int f1 = f[1];
int f2 = f[2];
int f3 = f[3];
int f4 = f[4];
int f5 = f[5];
int f6 = f[6];
int f7 = f[7];
int f8 = f[8];
int f9 = f[9];
int g0 = g[0];
int g1 = g[1];
int g2 = g[2];
int g3 = g[3];
int g4 = g[4];
int g5 = g[5];
int g6 = g[6];
int g7 = g[7];
int g8 = g[8];
int g9 = g[9];
int g1_19 = 19 * g1;
int g2_19 = 19 * g2;
int g3_19 = 19 * g3;
int g4_19 = 19 * g4;
int g5_19 = 19 * g5;
int g6_19 = 19 * g6;
int g7_19 = 19 * g7;
int g8_19 = 19 * g8;
int g9_19 = 19 * g9;
int f1_2 = 2 * f1;
int f3_2 = 2 * f3;
int f5_2 = 2 * f5;
int f7_2 = 2 * f7;
int f9_2 = 2 * f9;
long f0g0 = f0 * (long)g0;
long f0g1 = f0 * (long)g1;
long f0g2 = f0 * (long)g2;
long f0g3 = f0 * (long)g3;
long f0g4 = f0 * (long)g4;
long f0g5 = f0 * (long)g5;
long f0g6 = f0 * (long)g6;
long f0g7 = f0 * (long)g7;
long f0g8 = f0 * (long)g8;
long f0g9 = f0 * (long)g9;
long f1g0 = f1 * (long)g0;
long f1g1_2 = f1_2 * (long)g1;
long f1g2 = f1 * (long)g2;
long f1g3_2 = f1_2 * (long)g3;
long f1g4 = f1 * (long)g4;
long f1g5_2 = f1_2 * (long)g5;
long f1g6 = f1 * (long)g6;
long f1g7_2 = f1_2 * (long)g7;
long f1g8 = f1 * (long)g8;
long f1g9_38 = f1_2 * (long)g9_19;
long f2g0 = f2 * (long)g0;
long f2g1 = f2 * (long)g1;
long f2g2 = f2 * (long)g2;
long f2g3 = f2 * (long)g3;
long f2g4 = f2 * (long)g4;
long f2g5 = f2 * (long)g5;
long f2g6 = f2 * (long)g6;
long f2g7 = f2 * (long)g7;
long f2g8_19 = f2 * (long)g8_19;
long f2g9_19 = f2 * (long)g9_19;
long f3g0 = f3 * (long)g0;
long f3g1_2 = f3_2 * (long)g1;
long f3g2 = f3 * (long)g2;
long f3g3_2 = f3_2 * (long)g3;
long f3g4 = f3 * (long)g4;
long f3g5_2 = f3_2 * (long)g5;
long f3g6 = f3 * (long)g6;
long f3g7_38 = f3_2 * (long)g7_19;
long f3g8_19 = f3 * (long)g8_19;
long f3g9_38 = f3_2 * (long)g9_19;
long f4g0 = f4 * (long)g0;
long f4g1 = f4 * (long)g1;
long f4g2 = f4 * (long)g2;
long f4g3 = f4 * (long)g3;
long f4g4 = f4 * (long)g4;
long f4g5 = f4 * (long)g5;
long f4g6_19 = f4 * (long)g6_19;
long f4g7_19 = f4 * (long)g7_19;
long f4g8_19 = f4 * (long)g8_19;
long f4g9_19 = f4 * (long)g9_19;
long f5g0 = f5 * (long)g0;
long f5g1_2 = f5_2 * (long)g1;
long f5g2 = f5 * (long)g2;
long f5g3_2 = f5_2 * (long)g3;
long f5g4 = f5 * (long)g4;
long f5g5_38 = f5_2 * (long)g5_19;
long f5g6_19 = f5 * (long)g6_19;
long f5g7_38 = f5_2 * (long)g7_19;
long f5g8_19 = f5 * (long)g8_19;
long f5g9_38 = f5_2 * (long)g9_19;
long f6g0 = f6 * (long)g0;
long f6g1 = f6 * (long)g1;
long f6g2 = f6 * (long)g2;
long f6g3 = f6 * (long)g3;
long f6g4_19 = f6 * (long)g4_19;
long f6g5_19 = f6 * (long)g5_19;
long f6g6_19 = f6 * (long)g6_19;
long f6g7_19 = f6 * (long)g7_19;
long f6g8_19 = f6 * (long)g8_19;
long f6g9_19 = f6 * (long)g9_19;
long f7g0 = f7 * (long)g0;
long f7g1_2 = f7_2 * (long)g1;
long f7g2 = f7 * (long)g2;
long f7g3_38 = f7_2 * (long)g3_19;
long f7g4_19 = f7 * (long)g4_19;
long f7g5_38 = f7_2 * (long)g5_19;
long f7g6_19 = f7 * (long)g6_19;
long f7g7_38 = f7_2 * (long)g7_19;
long f7g8_19 = f7 * (long)g8_19;
long f7g9_38 = f7_2 * (long)g9_19;
long f8g0 = f8 * (long)g0;
long f8g1 = f8 * (long)g1;
long f8g2_19 = f8 * (long)g2_19;
long f8g3_19 = f8 * (long)g3_19;
long f8g4_19 = f8 * (long)g4_19;
long f8g5_19 = f8 * (long)g5_19;
long f8g6_19 = f8 * (long)g6_19;
long f8g7_19 = f8 * (long)g7_19;
long f8g8_19 = f8 * (long)g8_19;
long f8g9_19 = f8 * (long)g9_19;
long f9g0 = f9 * (long)g0;
long f9g1_38 = f9_2 * (long)g1_19;
long f9g2_19 = f9 * (long)g2_19;
long f9g3_38 = f9_2 * (long)g3_19;
long f9g4_19 = f9 * (long)g4_19;
long f9g5_38 = f9_2 * (long)g5_19;
long f9g6_19 = f9 * (long)g6_19;
long f9g7_38 = f9_2 * (long)g7_19;
long f9g8_19 = f9 * (long)g8_19;
long f9g9_38 = f9_2 * (long)g9_19;
long h0 = f0g0 + f1g9_38 + f2g8_19 + f3g7_38 + f4g6_19 + f5g5_38 + f6g4_19 +
f7g3_38 + f8g2_19 + f9g1_38;
long h1 = f0g1 + f1g0 + f2g9_19 + f3g8_19 + f4g7_19 + f5g6_19 + f6g5_19 +
f7g4_19 + f8g3_19 + f9g2_19;
long h2 = f0g2 + f1g1_2 + f2g0 + f3g9_38 + f4g8_19 + f5g7_38 + f6g6_19 +
f7g5_38 + f8g4_19 + f9g3_38;
long h3 = f0g3 + f1g2 + f2g1 + f3g0 + f4g9_19 + f5g8_19 + f6g7_19 + f7g6_19 +
f8g5_19 + f9g4_19;
long h4 = f0g4 + f1g3_2 + f2g2 + f3g1_2 + f4g0 + f5g9_38 + f6g8_19 + f7g7_38 +
f8g6_19 + f9g5_38;
long h5 = f0g5 + f1g4 + f2g3 + f3g2 + f4g1 + f5g0 + f6g9_19 + f7g8_19 +
f8g7_19 + f9g6_19;
long h6 = f0g6 + f1g5_2 + f2g4 + f3g3_2 + f4g2 + f5g1_2 + f6g0 + f7g9_38 +
f8g8_19 + f9g7_38;
long h7 =
f0g7 + f1g6 + f2g5 + f3g4 + f4g3 + f5g2 + f6g1 + f7g0 + f8g9_19 + f9g8_19;
long h8 = f0g8 + f1g7_2 + f2g6 + f3g5_2 + f4g4 + f5g3_2 + f6g2 + f7g1_2 +
f8g0 + f9g9_38;
long h9 = f0g9 + f1g8 + f2g7 + f3g6 + f4g5 + f5g4 + f6g3 + f7g2 + f8g1 + f9g0;
long carry0;
long carry1;
long carry2;
long carry3;
long carry4;
long carry5;
long carry6;
long carry7;
long carry8;
long carry9;
carry0 = (h0 + (long)(1 << 25)) >> 26;
h1 += carry0;
h0 -= carry0 << 26;
carry4 = (h4 + (long)(1 << 25)) >> 26;
h5 += carry4;
h4 -= carry4 << 26;
carry1 = (h1 + (long)(1 << 24)) >> 25;
h2 += carry1;
h1 -= carry1 << 25;
carry5 = (h5 + (long)(1 << 24)) >> 25;
h6 += carry5;
h5 -= carry5 << 25;
carry2 = (h2 + (long)(1 << 25)) >> 26;
h3 += carry2;
h2 -= carry2 << 26;
carry6 = (h6 + (long)(1 << 25)) >> 26;
h7 += carry6;
h6 -= carry6 << 26;
carry3 = (h3 + (long)(1 << 24)) >> 25;
h4 += carry3;
h3 -= carry3 << 25;
carry7 = (h7 + (long)(1 << 24)) >> 25;
h8 += carry7;
h7 -= carry7 << 25;
carry4 = (h4 + (long)(1 << 25)) >> 26;
h5 += carry4;
h4 -= carry4 << 26;
carry8 = (h8 + (long)(1 << 25)) >> 26;
h9 += carry8;
h8 -= carry8 << 26;
carry9 = (h9 + (long)(1 << 24)) >> 25;
h0 += carry9 * 19;
h9 -= carry9 << 25;
carry0 = (h0 + (long)(1 << 25)) >> 26;
h1 += carry0;
h0 -= carry0 << 26;
h[0] = (int)h0;
h[1] = (int)h1;
h[2] = (int)h2;
h[3] = (int)h3;
h[4] = (int)h4;
h[5] = (int)h5;
h[6] = (int)h6;
h[7] = (int)h7;
h[8] = (int)h8;
h[9] = (int)h9;
}
// https://lib25519.cr.yp.to/lib25519-20230630/crypto_mGnP/ed25519/ref10/fe_neg.c.html
void fe_neg(int *h, const int *f)
{
int f0 = f[0];
int f1 = f[1];
int f2 = f[2];
int f3 = f[3];
int f4 = f[4];
int f5 = f[5];
int f6 = f[6];
int f7 = f[7];
int f8 = f[8];
int f9 = f[9];
int h0 = -f0;
int h1 = -f1;
int h2 = -f2;
int h3 = -f3;
int h4 = -f4;
int h5 = -f5;
int h6 = -f6;
int h7 = -f7;
int h8 = -f8;
int h9 = -f9;
h[0] = h0;
h[1] = h1;
h[2] = h2;
h[3] = h3;
h[4] = h4;
h[5] = h5;
h[6] = h6;
h[7] = h7;
h[8] = h8;
h[9] = h9;
}
// https://lib25519.cr.yp.to/lib25519-20230630/crypto_mGnP/ed25519/ref10/fe_sq.c.html
void fe_sq(int *h, const int *f)
{
int16 fs = *(int16*)f;
int16 fs_2 = fs * 2;
int f5_38 = 38 * fs[5];
int f6_19 = 19 * fs[6];
int f7_38 = 38 * fs[7];
int f8_19 = 19 * fs[8];
int f9_38 = 38 * fs[9];
long16 f0fs_2 = (long16)(
(long)fs[0],
(long)fs[1],
(long)fs[2],
(long)fs[3],
(long)fs[4],
(long)fs[5],
(long)fs[6],
(long)fs[7],
(long)fs[8],
(long)fs[9],
0,
0,
0,
0,
0,
0)
*
(long16)(
fs[0],
fs_2[0],
fs_2[0],
fs_2[0],
fs_2[0],
fs_2[0],
fs_2[0],
fs_2[0],
fs_2[0],
fs_2[0],
0, 0, 0, 0, 0, 0
);
long16 f1fs_2_4_76 = (long16)(
(long)f9_38,
0,
(long)fs[1],
(long)fs[2],
(long)fs_2[3],
(long)fs[4],
(long)fs_2[5],
(long)fs[6],
(long)fs_2[7],
(long)fs[8],
0, 0, 0, 0, 0, 0
) * fs_2[1];
long16 f2fs_1_2_38 = (long16)(
(long)f8_19,
(long)f9_38,
0,
0,
(long)fs[2],
(long)fs[3],
(long)fs[4],
(long)fs[5],
(long)fs[6],
(long)fs[7],
0, 0, 0, 0, 0, 0
) * (long16)(
fs_2[2],
fs[2],
0,
0,
fs[2],
fs_2[2],
fs_2[2],
fs_2[2],
fs_2[2],
fs_2[2],
0, 0, 0, 0, 0, 0
);
long16 f3fs_2_3_38_76 = (long16)(
(long)f7_38,
(long)f8_19,
(long)f9_38,
0,
0,
0,
(long)fs[3],
(long)fs[4],
(long)fs_2[5],
(long)fs[6],
0, 0, 0, 0, 0, 0
) * fs_2[3];
long16 f4fs_1_2_38 = (long16)(
(long)f6_19,
(long)f7_38,
(long)f8_19,
(long)f9_38,
0,
0,
0,
0,
(long)fs[4],
(long)fs[5],
0, 0, 0, 0, 0, 0
) * (long16)(
fs_2[4],
fs[4],
fs_2[4],
fs[4],
0,
0,
0,
0,
fs[4],
fs_2[4],
0, 0, 0, 0, 0, 0
);
long16 f5fs_38_76 = (long16)(
(long)f5_38,
(long)f6_19,
(long)f7_38,
(long)f8_19,
(long)f9_38,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
) * (long16)(
fs[5],
fs_2[5],
fs_2[5],
fs_2[5],
fs_2[5],
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
);
long16 f6f2_19_38 = (long16)(
0,
0,
(long)f6_19,
(long)f7_38,
(long)f8_19,
(long)f9_38,
0,
0,
0, 0, 0, 0, 0, 0, 0, 0
) * (long16)(
0,
0,
fs[6],
fs[6],
fs_2[6],
fs[6],
0,
0, 0, 0, 0, 0, 0, 0, 0, 0
);
long16 f7fs_38_76 = (long16)(
0,
0,
0,
0,
(long)f7_38,
(long)f8_19,
(long)f9_38,
0, 0, 0, 0, 0, 0, 0, 0, 0
) * (long16)(
0,
0,
0,
0,
fs[7],
fs_2[7],
fs_2[7],
0, 0, 0, 0, 0, 0, 0, 0, 0
);