-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
p256_64.zig
1825 lines (1786 loc) · 66.4 KB
/
p256_64.zig
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
// Autogenerated: 'src/ExtractionOCaml/word_by_word_montgomery' --lang Zig --internal-static --public-function-case camelCase --private-function-case camelCase --public-type-case UpperCamelCase --private-type-case UpperCamelCase --no-prefix-fiat --package-name p256 '' 64 '2^256 - 2^224 + 2^192 + 2^96 - 1' mul square add sub opp from_montgomery to_montgomery nonzero selectznz to_bytes from_bytes one msat divstep divstep_precomp
// curve description (via package name): p256
// machine_wordsize = 64 (from "64")
// requested operations: mul, square, add, sub, opp, from_montgomery, to_montgomery, nonzero, selectznz, to_bytes, from_bytes, one, msat, divstep, divstep_precomp
// m = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff (from "2^256 - 2^224 + 2^192 + 2^96 - 1")
//
// NOTE: In addition to the bounds specified above each function, all
// functions synthesized for this Montgomery arithmetic require the
// input to be strictly less than the prime modulus (m), and also
// require the input to be in the unique saturated representation.
// All functions also ensure that these two properties are true of
// return values.
//
// Computed values:
// eval z = z[0] + (z[1] << 64) + (z[2] << 128) + (z[3] << 192)
// bytes_eval z = z[0]
// + (z[1] << 8)
// + (z[2] << 16)
// + (z[3] << 24)
// + (z[4] << 32)
// + (z[5] << 40)
// + (z[6] << 48)
// + (z[7] << 56)
// + (z[8] << 64)
// + (z[9] << 72)
// + (z[10] << 80)
// + (z[11] << 88)
// + (z[12] << 96)
// + (z[13] << 104)
// + (z[14] << 112)
// + (z[15] << 120)
// + (z[16] << 128)
// + (z[17] << 136)
// + (z[18] << 144)
// + (z[19] << 152)
// + (z[20] << 160)
// + (z[21] << 168)
// + (z[22] << 176)
// + (z[23] << 184)
// + (z[24] << 192)
// + (z[25] << 200)
// + (z[26] << 208)
// + (z[27] << 216)
// + (z[28] << 224)
// + (z[29] << 232)
// + (z[30] << 240)
// + (z[31] << 248)
// twos_complement_eval z = let x1 := z[0] + (z[1] << 64) + (z[2] << 128) + (z[3] << 192) in
// if x1 & (2^256-1) < 2^255 then x1 & (2^256-1) else (x1 & (2^256-1)) - 2^256
const std = @import("std");
const mode = @import("builtin").mode; // Checked arithmetic is disabled in non-debug modes to avoid side channels
// The type MontgomeryDomainFieldElement is a field element in the Montgomery domain.
// Bounds: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]
pub const MontgomeryDomainFieldElement = [4]u64;
// The type NonMontgomeryDomainFieldElement is a field element NOT in the Montgomery domain.
// Bounds: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]
pub const NonMontgomeryDomainFieldElement = [4]u64;
/// The function addcarryxU64 is an addition with carry.
///
/// Postconditions:
/// out1 = (arg1 + arg2 + arg3) mod 2^64
/// out2 = ⌊(arg1 + arg2 + arg3) / 2^64⌋
///
/// Input Bounds:
/// arg1: [0x0 ~> 0x1]
/// arg2: [0x0 ~> 0xffffffffffffffff]
/// arg3: [0x0 ~> 0xffffffffffffffff]
/// Output Bounds:
/// out1: [0x0 ~> 0xffffffffffffffff]
/// out2: [0x0 ~> 0x1]
inline fn addcarryxU64(out1: *u64, out2: *u1, arg1: u1, arg2: u64, arg3: u64) void {
const x = @as(u128, arg2) +% arg3 +% arg1;
out1.* = @truncate(x);
out2.* = @truncate(x >> 64);
}
/// The function subborrowxU64 is a subtraction with borrow.
///
/// Postconditions:
/// out1 = (-arg1 + arg2 + -arg3) mod 2^64
/// out2 = -⌊(-arg1 + arg2 + -arg3) / 2^64⌋
///
/// Input Bounds:
/// arg1: [0x0 ~> 0x1]
/// arg2: [0x0 ~> 0xffffffffffffffff]
/// arg3: [0x0 ~> 0xffffffffffffffff]
/// Output Bounds:
/// out1: [0x0 ~> 0xffffffffffffffff]
/// out2: [0x0 ~> 0x1]
inline fn subborrowxU64(out1: *u64, out2: *u1, arg1: u1, arg2: u64, arg3: u64) void {
const x = @as(u128, arg2) -% arg3 -% arg1;
out1.* = @truncate(x);
out2.* = @truncate(x >> 64);
}
/// The function mulxU64 is a multiplication, returning the full double-width result.
///
/// Postconditions:
/// out1 = (arg1 * arg2) mod 2^64
/// out2 = ⌊arg1 * arg2 / 2^64⌋
///
/// Input Bounds:
/// arg1: [0x0 ~> 0xffffffffffffffff]
/// arg2: [0x0 ~> 0xffffffffffffffff]
/// Output Bounds:
/// out1: [0x0 ~> 0xffffffffffffffff]
/// out2: [0x0 ~> 0xffffffffffffffff]
inline fn mulxU64(out1: *u64, out2: *u64, arg1: u64, arg2: u64) void {
@setRuntimeSafety(mode == .Debug);
const x = @as(u128, arg1) * @as(u128, arg2);
out1.* = @as(u64, @truncate(x));
out2.* = @as(u64, @truncate(x >> 64));
}
/// The function cmovznzU64 is a single-word conditional move.
///
/// Postconditions:
/// out1 = (if arg1 = 0 then arg2 else arg3)
///
/// Input Bounds:
/// arg1: [0x0 ~> 0x1]
/// arg2: [0x0 ~> 0xffffffffffffffff]
/// arg3: [0x0 ~> 0xffffffffffffffff]
/// Output Bounds:
/// out1: [0x0 ~> 0xffffffffffffffff]
inline fn cmovznzU64(out1: *u64, arg1: u1, arg2: u64, arg3: u64) void {
@setRuntimeSafety(mode == .Debug);
const mask = 0 -% @as(u64, arg1);
out1.* = (mask & arg3) | ((~mask) & arg2);
}
/// The function mul multiplies two field elements in the Montgomery domain.
///
/// Preconditions:
/// 0 ≤ eval arg1 < m
/// 0 ≤ eval arg2 < m
/// Postconditions:
/// eval (from_montgomery out1) mod m = (eval (from_montgomery arg1) * eval (from_montgomery arg2)) mod m
/// 0 ≤ eval out1 < m
///
pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldElement, arg2: MontgomeryDomainFieldElement) void {
@setRuntimeSafety(mode == .Debug);
const x1 = (arg1[1]);
const x2 = (arg1[2]);
const x3 = (arg1[3]);
const x4 = (arg1[0]);
var x5: u64 = undefined;
var x6: u64 = undefined;
mulxU64(&x5, &x6, x4, (arg2[3]));
var x7: u64 = undefined;
var x8: u64 = undefined;
mulxU64(&x7, &x8, x4, (arg2[2]));
var x9: u64 = undefined;
var x10: u64 = undefined;
mulxU64(&x9, &x10, x4, (arg2[1]));
var x11: u64 = undefined;
var x12: u64 = undefined;
mulxU64(&x11, &x12, x4, (arg2[0]));
var x13: u64 = undefined;
var x14: u1 = undefined;
addcarryxU64(&x13, &x14, 0x0, x12, x9);
var x15: u64 = undefined;
var x16: u1 = undefined;
addcarryxU64(&x15, &x16, x14, x10, x7);
var x17: u64 = undefined;
var x18: u1 = undefined;
addcarryxU64(&x17, &x18, x16, x8, x5);
const x19 = (@as(u64, x18) + x6);
var x20: u64 = undefined;
var x21: u64 = undefined;
mulxU64(&x20, &x21, x11, 0xffffffff00000001);
var x22: u64 = undefined;
var x23: u64 = undefined;
mulxU64(&x22, &x23, x11, 0xffffffff);
var x24: u64 = undefined;
var x25: u64 = undefined;
mulxU64(&x24, &x25, x11, 0xffffffffffffffff);
var x26: u64 = undefined;
var x27: u1 = undefined;
addcarryxU64(&x26, &x27, 0x0, x25, x22);
const x28 = (@as(u64, x27) + x23);
var x29: u64 = undefined;
var x30: u1 = undefined;
addcarryxU64(&x29, &x30, 0x0, x11, x24);
var x31: u64 = undefined;
var x32: u1 = undefined;
addcarryxU64(&x31, &x32, x30, x13, x26);
var x33: u64 = undefined;
var x34: u1 = undefined;
addcarryxU64(&x33, &x34, x32, x15, x28);
var x35: u64 = undefined;
var x36: u1 = undefined;
addcarryxU64(&x35, &x36, x34, x17, x20);
var x37: u64 = undefined;
var x38: u1 = undefined;
addcarryxU64(&x37, &x38, x36, x19, x21);
var x39: u64 = undefined;
var x40: u64 = undefined;
mulxU64(&x39, &x40, x1, (arg2[3]));
var x41: u64 = undefined;
var x42: u64 = undefined;
mulxU64(&x41, &x42, x1, (arg2[2]));
var x43: u64 = undefined;
var x44: u64 = undefined;
mulxU64(&x43, &x44, x1, (arg2[1]));
var x45: u64 = undefined;
var x46: u64 = undefined;
mulxU64(&x45, &x46, x1, (arg2[0]));
var x47: u64 = undefined;
var x48: u1 = undefined;
addcarryxU64(&x47, &x48, 0x0, x46, x43);
var x49: u64 = undefined;
var x50: u1 = undefined;
addcarryxU64(&x49, &x50, x48, x44, x41);
var x51: u64 = undefined;
var x52: u1 = undefined;
addcarryxU64(&x51, &x52, x50, x42, x39);
const x53 = (@as(u64, x52) + x40);
var x54: u64 = undefined;
var x55: u1 = undefined;
addcarryxU64(&x54, &x55, 0x0, x31, x45);
var x56: u64 = undefined;
var x57: u1 = undefined;
addcarryxU64(&x56, &x57, x55, x33, x47);
var x58: u64 = undefined;
var x59: u1 = undefined;
addcarryxU64(&x58, &x59, x57, x35, x49);
var x60: u64 = undefined;
var x61: u1 = undefined;
addcarryxU64(&x60, &x61, x59, x37, x51);
var x62: u64 = undefined;
var x63: u1 = undefined;
addcarryxU64(&x62, &x63, x61, @as(u64, x38), x53);
var x64: u64 = undefined;
var x65: u64 = undefined;
mulxU64(&x64, &x65, x54, 0xffffffff00000001);
var x66: u64 = undefined;
var x67: u64 = undefined;
mulxU64(&x66, &x67, x54, 0xffffffff);
var x68: u64 = undefined;
var x69: u64 = undefined;
mulxU64(&x68, &x69, x54, 0xffffffffffffffff);
var x70: u64 = undefined;
var x71: u1 = undefined;
addcarryxU64(&x70, &x71, 0x0, x69, x66);
const x72 = (@as(u64, x71) + x67);
var x73: u64 = undefined;
var x74: u1 = undefined;
addcarryxU64(&x73, &x74, 0x0, x54, x68);
var x75: u64 = undefined;
var x76: u1 = undefined;
addcarryxU64(&x75, &x76, x74, x56, x70);
var x77: u64 = undefined;
var x78: u1 = undefined;
addcarryxU64(&x77, &x78, x76, x58, x72);
var x79: u64 = undefined;
var x80: u1 = undefined;
addcarryxU64(&x79, &x80, x78, x60, x64);
var x81: u64 = undefined;
var x82: u1 = undefined;
addcarryxU64(&x81, &x82, x80, x62, x65);
const x83 = (@as(u64, x82) + @as(u64, x63));
var x84: u64 = undefined;
var x85: u64 = undefined;
mulxU64(&x84, &x85, x2, (arg2[3]));
var x86: u64 = undefined;
var x87: u64 = undefined;
mulxU64(&x86, &x87, x2, (arg2[2]));
var x88: u64 = undefined;
var x89: u64 = undefined;
mulxU64(&x88, &x89, x2, (arg2[1]));
var x90: u64 = undefined;
var x91: u64 = undefined;
mulxU64(&x90, &x91, x2, (arg2[0]));
var x92: u64 = undefined;
var x93: u1 = undefined;
addcarryxU64(&x92, &x93, 0x0, x91, x88);
var x94: u64 = undefined;
var x95: u1 = undefined;
addcarryxU64(&x94, &x95, x93, x89, x86);
var x96: u64 = undefined;
var x97: u1 = undefined;
addcarryxU64(&x96, &x97, x95, x87, x84);
const x98 = (@as(u64, x97) + x85);
var x99: u64 = undefined;
var x100: u1 = undefined;
addcarryxU64(&x99, &x100, 0x0, x75, x90);
var x101: u64 = undefined;
var x102: u1 = undefined;
addcarryxU64(&x101, &x102, x100, x77, x92);
var x103: u64 = undefined;
var x104: u1 = undefined;
addcarryxU64(&x103, &x104, x102, x79, x94);
var x105: u64 = undefined;
var x106: u1 = undefined;
addcarryxU64(&x105, &x106, x104, x81, x96);
var x107: u64 = undefined;
var x108: u1 = undefined;
addcarryxU64(&x107, &x108, x106, x83, x98);
var x109: u64 = undefined;
var x110: u64 = undefined;
mulxU64(&x109, &x110, x99, 0xffffffff00000001);
var x111: u64 = undefined;
var x112: u64 = undefined;
mulxU64(&x111, &x112, x99, 0xffffffff);
var x113: u64 = undefined;
var x114: u64 = undefined;
mulxU64(&x113, &x114, x99, 0xffffffffffffffff);
var x115: u64 = undefined;
var x116: u1 = undefined;
addcarryxU64(&x115, &x116, 0x0, x114, x111);
const x117 = (@as(u64, x116) + x112);
var x118: u64 = undefined;
var x119: u1 = undefined;
addcarryxU64(&x118, &x119, 0x0, x99, x113);
var x120: u64 = undefined;
var x121: u1 = undefined;
addcarryxU64(&x120, &x121, x119, x101, x115);
var x122: u64 = undefined;
var x123: u1 = undefined;
addcarryxU64(&x122, &x123, x121, x103, x117);
var x124: u64 = undefined;
var x125: u1 = undefined;
addcarryxU64(&x124, &x125, x123, x105, x109);
var x126: u64 = undefined;
var x127: u1 = undefined;
addcarryxU64(&x126, &x127, x125, x107, x110);
const x128 = (@as(u64, x127) + @as(u64, x108));
var x129: u64 = undefined;
var x130: u64 = undefined;
mulxU64(&x129, &x130, x3, (arg2[3]));
var x131: u64 = undefined;
var x132: u64 = undefined;
mulxU64(&x131, &x132, x3, (arg2[2]));
var x133: u64 = undefined;
var x134: u64 = undefined;
mulxU64(&x133, &x134, x3, (arg2[1]));
var x135: u64 = undefined;
var x136: u64 = undefined;
mulxU64(&x135, &x136, x3, (arg2[0]));
var x137: u64 = undefined;
var x138: u1 = undefined;
addcarryxU64(&x137, &x138, 0x0, x136, x133);
var x139: u64 = undefined;
var x140: u1 = undefined;
addcarryxU64(&x139, &x140, x138, x134, x131);
var x141: u64 = undefined;
var x142: u1 = undefined;
addcarryxU64(&x141, &x142, x140, x132, x129);
const x143 = (@as(u64, x142) + x130);
var x144: u64 = undefined;
var x145: u1 = undefined;
addcarryxU64(&x144, &x145, 0x0, x120, x135);
var x146: u64 = undefined;
var x147: u1 = undefined;
addcarryxU64(&x146, &x147, x145, x122, x137);
var x148: u64 = undefined;
var x149: u1 = undefined;
addcarryxU64(&x148, &x149, x147, x124, x139);
var x150: u64 = undefined;
var x151: u1 = undefined;
addcarryxU64(&x150, &x151, x149, x126, x141);
var x152: u64 = undefined;
var x153: u1 = undefined;
addcarryxU64(&x152, &x153, x151, x128, x143);
var x154: u64 = undefined;
var x155: u64 = undefined;
mulxU64(&x154, &x155, x144, 0xffffffff00000001);
var x156: u64 = undefined;
var x157: u64 = undefined;
mulxU64(&x156, &x157, x144, 0xffffffff);
var x158: u64 = undefined;
var x159: u64 = undefined;
mulxU64(&x158, &x159, x144, 0xffffffffffffffff);
var x160: u64 = undefined;
var x161: u1 = undefined;
addcarryxU64(&x160, &x161, 0x0, x159, x156);
const x162 = (@as(u64, x161) + x157);
var x163: u64 = undefined;
var x164: u1 = undefined;
addcarryxU64(&x163, &x164, 0x0, x144, x158);
var x165: u64 = undefined;
var x166: u1 = undefined;
addcarryxU64(&x165, &x166, x164, x146, x160);
var x167: u64 = undefined;
var x168: u1 = undefined;
addcarryxU64(&x167, &x168, x166, x148, x162);
var x169: u64 = undefined;
var x170: u1 = undefined;
addcarryxU64(&x169, &x170, x168, x150, x154);
var x171: u64 = undefined;
var x172: u1 = undefined;
addcarryxU64(&x171, &x172, x170, x152, x155);
const x173 = (@as(u64, x172) + @as(u64, x153));
var x174: u64 = undefined;
var x175: u1 = undefined;
subborrowxU64(&x174, &x175, 0x0, x165, 0xffffffffffffffff);
var x176: u64 = undefined;
var x177: u1 = undefined;
subborrowxU64(&x176, &x177, x175, x167, 0xffffffff);
var x178: u64 = undefined;
var x179: u1 = undefined;
subborrowxU64(&x178, &x179, x177, x169, @as(u64, 0x0));
var x180: u64 = undefined;
var x181: u1 = undefined;
subborrowxU64(&x180, &x181, x179, x171, 0xffffffff00000001);
var x182: u64 = undefined;
var x183: u1 = undefined;
subborrowxU64(&x182, &x183, x181, x173, @as(u64, 0x0));
var x184: u64 = undefined;
cmovznzU64(&x184, x183, x174, x165);
var x185: u64 = undefined;
cmovznzU64(&x185, x183, x176, x167);
var x186: u64 = undefined;
cmovznzU64(&x186, x183, x178, x169);
var x187: u64 = undefined;
cmovznzU64(&x187, x183, x180, x171);
out1[0] = x184;
out1[1] = x185;
out1[2] = x186;
out1[3] = x187;
}
/// The function square squares a field element in the Montgomery domain.
///
/// Preconditions:
/// 0 ≤ eval arg1 < m
/// Postconditions:
/// eval (from_montgomery out1) mod m = (eval (from_montgomery arg1) * eval (from_montgomery arg1)) mod m
/// 0 ≤ eval out1 < m
///
pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldElement) void {
@setRuntimeSafety(mode == .Debug);
const x1 = (arg1[1]);
const x2 = (arg1[2]);
const x3 = (arg1[3]);
const x4 = (arg1[0]);
var x5: u64 = undefined;
var x6: u64 = undefined;
mulxU64(&x5, &x6, x4, (arg1[3]));
var x7: u64 = undefined;
var x8: u64 = undefined;
mulxU64(&x7, &x8, x4, (arg1[2]));
var x9: u64 = undefined;
var x10: u64 = undefined;
mulxU64(&x9, &x10, x4, (arg1[1]));
var x11: u64 = undefined;
var x12: u64 = undefined;
mulxU64(&x11, &x12, x4, (arg1[0]));
var x13: u64 = undefined;
var x14: u1 = undefined;
addcarryxU64(&x13, &x14, 0x0, x12, x9);
var x15: u64 = undefined;
var x16: u1 = undefined;
addcarryxU64(&x15, &x16, x14, x10, x7);
var x17: u64 = undefined;
var x18: u1 = undefined;
addcarryxU64(&x17, &x18, x16, x8, x5);
const x19 = (@as(u64, x18) + x6);
var x20: u64 = undefined;
var x21: u64 = undefined;
mulxU64(&x20, &x21, x11, 0xffffffff00000001);
var x22: u64 = undefined;
var x23: u64 = undefined;
mulxU64(&x22, &x23, x11, 0xffffffff);
var x24: u64 = undefined;
var x25: u64 = undefined;
mulxU64(&x24, &x25, x11, 0xffffffffffffffff);
var x26: u64 = undefined;
var x27: u1 = undefined;
addcarryxU64(&x26, &x27, 0x0, x25, x22);
const x28 = (@as(u64, x27) + x23);
var x29: u64 = undefined;
var x30: u1 = undefined;
addcarryxU64(&x29, &x30, 0x0, x11, x24);
var x31: u64 = undefined;
var x32: u1 = undefined;
addcarryxU64(&x31, &x32, x30, x13, x26);
var x33: u64 = undefined;
var x34: u1 = undefined;
addcarryxU64(&x33, &x34, x32, x15, x28);
var x35: u64 = undefined;
var x36: u1 = undefined;
addcarryxU64(&x35, &x36, x34, x17, x20);
var x37: u64 = undefined;
var x38: u1 = undefined;
addcarryxU64(&x37, &x38, x36, x19, x21);
var x39: u64 = undefined;
var x40: u64 = undefined;
mulxU64(&x39, &x40, x1, (arg1[3]));
var x41: u64 = undefined;
var x42: u64 = undefined;
mulxU64(&x41, &x42, x1, (arg1[2]));
var x43: u64 = undefined;
var x44: u64 = undefined;
mulxU64(&x43, &x44, x1, (arg1[1]));
var x45: u64 = undefined;
var x46: u64 = undefined;
mulxU64(&x45, &x46, x1, (arg1[0]));
var x47: u64 = undefined;
var x48: u1 = undefined;
addcarryxU64(&x47, &x48, 0x0, x46, x43);
var x49: u64 = undefined;
var x50: u1 = undefined;
addcarryxU64(&x49, &x50, x48, x44, x41);
var x51: u64 = undefined;
var x52: u1 = undefined;
addcarryxU64(&x51, &x52, x50, x42, x39);
const x53 = (@as(u64, x52) + x40);
var x54: u64 = undefined;
var x55: u1 = undefined;
addcarryxU64(&x54, &x55, 0x0, x31, x45);
var x56: u64 = undefined;
var x57: u1 = undefined;
addcarryxU64(&x56, &x57, x55, x33, x47);
var x58: u64 = undefined;
var x59: u1 = undefined;
addcarryxU64(&x58, &x59, x57, x35, x49);
var x60: u64 = undefined;
var x61: u1 = undefined;
addcarryxU64(&x60, &x61, x59, x37, x51);
var x62: u64 = undefined;
var x63: u1 = undefined;
addcarryxU64(&x62, &x63, x61, @as(u64, x38), x53);
var x64: u64 = undefined;
var x65: u64 = undefined;
mulxU64(&x64, &x65, x54, 0xffffffff00000001);
var x66: u64 = undefined;
var x67: u64 = undefined;
mulxU64(&x66, &x67, x54, 0xffffffff);
var x68: u64 = undefined;
var x69: u64 = undefined;
mulxU64(&x68, &x69, x54, 0xffffffffffffffff);
var x70: u64 = undefined;
var x71: u1 = undefined;
addcarryxU64(&x70, &x71, 0x0, x69, x66);
const x72 = (@as(u64, x71) + x67);
var x73: u64 = undefined;
var x74: u1 = undefined;
addcarryxU64(&x73, &x74, 0x0, x54, x68);
var x75: u64 = undefined;
var x76: u1 = undefined;
addcarryxU64(&x75, &x76, x74, x56, x70);
var x77: u64 = undefined;
var x78: u1 = undefined;
addcarryxU64(&x77, &x78, x76, x58, x72);
var x79: u64 = undefined;
var x80: u1 = undefined;
addcarryxU64(&x79, &x80, x78, x60, x64);
var x81: u64 = undefined;
var x82: u1 = undefined;
addcarryxU64(&x81, &x82, x80, x62, x65);
const x83 = (@as(u64, x82) + @as(u64, x63));
var x84: u64 = undefined;
var x85: u64 = undefined;
mulxU64(&x84, &x85, x2, (arg1[3]));
var x86: u64 = undefined;
var x87: u64 = undefined;
mulxU64(&x86, &x87, x2, (arg1[2]));
var x88: u64 = undefined;
var x89: u64 = undefined;
mulxU64(&x88, &x89, x2, (arg1[1]));
var x90: u64 = undefined;
var x91: u64 = undefined;
mulxU64(&x90, &x91, x2, (arg1[0]));
var x92: u64 = undefined;
var x93: u1 = undefined;
addcarryxU64(&x92, &x93, 0x0, x91, x88);
var x94: u64 = undefined;
var x95: u1 = undefined;
addcarryxU64(&x94, &x95, x93, x89, x86);
var x96: u64 = undefined;
var x97: u1 = undefined;
addcarryxU64(&x96, &x97, x95, x87, x84);
const x98 = (@as(u64, x97) + x85);
var x99: u64 = undefined;
var x100: u1 = undefined;
addcarryxU64(&x99, &x100, 0x0, x75, x90);
var x101: u64 = undefined;
var x102: u1 = undefined;
addcarryxU64(&x101, &x102, x100, x77, x92);
var x103: u64 = undefined;
var x104: u1 = undefined;
addcarryxU64(&x103, &x104, x102, x79, x94);
var x105: u64 = undefined;
var x106: u1 = undefined;
addcarryxU64(&x105, &x106, x104, x81, x96);
var x107: u64 = undefined;
var x108: u1 = undefined;
addcarryxU64(&x107, &x108, x106, x83, x98);
var x109: u64 = undefined;
var x110: u64 = undefined;
mulxU64(&x109, &x110, x99, 0xffffffff00000001);
var x111: u64 = undefined;
var x112: u64 = undefined;
mulxU64(&x111, &x112, x99, 0xffffffff);
var x113: u64 = undefined;
var x114: u64 = undefined;
mulxU64(&x113, &x114, x99, 0xffffffffffffffff);
var x115: u64 = undefined;
var x116: u1 = undefined;
addcarryxU64(&x115, &x116, 0x0, x114, x111);
const x117 = (@as(u64, x116) + x112);
var x118: u64 = undefined;
var x119: u1 = undefined;
addcarryxU64(&x118, &x119, 0x0, x99, x113);
var x120: u64 = undefined;
var x121: u1 = undefined;
addcarryxU64(&x120, &x121, x119, x101, x115);
var x122: u64 = undefined;
var x123: u1 = undefined;
addcarryxU64(&x122, &x123, x121, x103, x117);
var x124: u64 = undefined;
var x125: u1 = undefined;
addcarryxU64(&x124, &x125, x123, x105, x109);
var x126: u64 = undefined;
var x127: u1 = undefined;
addcarryxU64(&x126, &x127, x125, x107, x110);
const x128 = (@as(u64, x127) + @as(u64, x108));
var x129: u64 = undefined;
var x130: u64 = undefined;
mulxU64(&x129, &x130, x3, (arg1[3]));
var x131: u64 = undefined;
var x132: u64 = undefined;
mulxU64(&x131, &x132, x3, (arg1[2]));
var x133: u64 = undefined;
var x134: u64 = undefined;
mulxU64(&x133, &x134, x3, (arg1[1]));
var x135: u64 = undefined;
var x136: u64 = undefined;
mulxU64(&x135, &x136, x3, (arg1[0]));
var x137: u64 = undefined;
var x138: u1 = undefined;
addcarryxU64(&x137, &x138, 0x0, x136, x133);
var x139: u64 = undefined;
var x140: u1 = undefined;
addcarryxU64(&x139, &x140, x138, x134, x131);
var x141: u64 = undefined;
var x142: u1 = undefined;
addcarryxU64(&x141, &x142, x140, x132, x129);
const x143 = (@as(u64, x142) + x130);
var x144: u64 = undefined;
var x145: u1 = undefined;
addcarryxU64(&x144, &x145, 0x0, x120, x135);
var x146: u64 = undefined;
var x147: u1 = undefined;
addcarryxU64(&x146, &x147, x145, x122, x137);
var x148: u64 = undefined;
var x149: u1 = undefined;
addcarryxU64(&x148, &x149, x147, x124, x139);
var x150: u64 = undefined;
var x151: u1 = undefined;
addcarryxU64(&x150, &x151, x149, x126, x141);
var x152: u64 = undefined;
var x153: u1 = undefined;
addcarryxU64(&x152, &x153, x151, x128, x143);
var x154: u64 = undefined;
var x155: u64 = undefined;
mulxU64(&x154, &x155, x144, 0xffffffff00000001);
var x156: u64 = undefined;
var x157: u64 = undefined;
mulxU64(&x156, &x157, x144, 0xffffffff);
var x158: u64 = undefined;
var x159: u64 = undefined;
mulxU64(&x158, &x159, x144, 0xffffffffffffffff);
var x160: u64 = undefined;
var x161: u1 = undefined;
addcarryxU64(&x160, &x161, 0x0, x159, x156);
const x162 = (@as(u64, x161) + x157);
var x163: u64 = undefined;
var x164: u1 = undefined;
addcarryxU64(&x163, &x164, 0x0, x144, x158);
var x165: u64 = undefined;
var x166: u1 = undefined;
addcarryxU64(&x165, &x166, x164, x146, x160);
var x167: u64 = undefined;
var x168: u1 = undefined;
addcarryxU64(&x167, &x168, x166, x148, x162);
var x169: u64 = undefined;
var x170: u1 = undefined;
addcarryxU64(&x169, &x170, x168, x150, x154);
var x171: u64 = undefined;
var x172: u1 = undefined;
addcarryxU64(&x171, &x172, x170, x152, x155);
const x173 = (@as(u64, x172) + @as(u64, x153));
var x174: u64 = undefined;
var x175: u1 = undefined;
subborrowxU64(&x174, &x175, 0x0, x165, 0xffffffffffffffff);
var x176: u64 = undefined;
var x177: u1 = undefined;
subborrowxU64(&x176, &x177, x175, x167, 0xffffffff);
var x178: u64 = undefined;
var x179: u1 = undefined;
subborrowxU64(&x178, &x179, x177, x169, @as(u64, 0x0));
var x180: u64 = undefined;
var x181: u1 = undefined;
subborrowxU64(&x180, &x181, x179, x171, 0xffffffff00000001);
var x182: u64 = undefined;
var x183: u1 = undefined;
subborrowxU64(&x182, &x183, x181, x173, @as(u64, 0x0));
var x184: u64 = undefined;
cmovznzU64(&x184, x183, x174, x165);
var x185: u64 = undefined;
cmovznzU64(&x185, x183, x176, x167);
var x186: u64 = undefined;
cmovznzU64(&x186, x183, x178, x169);
var x187: u64 = undefined;
cmovznzU64(&x187, x183, x180, x171);
out1[0] = x184;
out1[1] = x185;
out1[2] = x186;
out1[3] = x187;
}
/// The function add adds two field elements in the Montgomery domain.
///
/// Preconditions:
/// 0 ≤ eval arg1 < m
/// 0 ≤ eval arg2 < m
/// Postconditions:
/// eval (from_montgomery out1) mod m = (eval (from_montgomery arg1) + eval (from_montgomery arg2)) mod m
/// 0 ≤ eval out1 < m
///
pub fn add(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldElement, arg2: MontgomeryDomainFieldElement) void {
@setRuntimeSafety(mode == .Debug);
var x1: u64 = undefined;
var x2: u1 = undefined;
addcarryxU64(&x1, &x2, 0x0, (arg1[0]), (arg2[0]));
var x3: u64 = undefined;
var x4: u1 = undefined;
addcarryxU64(&x3, &x4, x2, (arg1[1]), (arg2[1]));
var x5: u64 = undefined;
var x6: u1 = undefined;
addcarryxU64(&x5, &x6, x4, (arg1[2]), (arg2[2]));
var x7: u64 = undefined;
var x8: u1 = undefined;
addcarryxU64(&x7, &x8, x6, (arg1[3]), (arg2[3]));
var x9: u64 = undefined;
var x10: u1 = undefined;
subborrowxU64(&x9, &x10, 0x0, x1, 0xffffffffffffffff);
var x11: u64 = undefined;
var x12: u1 = undefined;
subborrowxU64(&x11, &x12, x10, x3, 0xffffffff);
var x13: u64 = undefined;
var x14: u1 = undefined;
subborrowxU64(&x13, &x14, x12, x5, @as(u64, 0x0));
var x15: u64 = undefined;
var x16: u1 = undefined;
subborrowxU64(&x15, &x16, x14, x7, 0xffffffff00000001);
var x17: u64 = undefined;
var x18: u1 = undefined;
subborrowxU64(&x17, &x18, x16, @as(u64, x8), @as(u64, 0x0));
var x19: u64 = undefined;
cmovznzU64(&x19, x18, x9, x1);
var x20: u64 = undefined;
cmovznzU64(&x20, x18, x11, x3);
var x21: u64 = undefined;
cmovznzU64(&x21, x18, x13, x5);
var x22: u64 = undefined;
cmovznzU64(&x22, x18, x15, x7);
out1[0] = x19;
out1[1] = x20;
out1[2] = x21;
out1[3] = x22;
}
/// The function sub subtracts two field elements in the Montgomery domain.
///
/// Preconditions:
/// 0 ≤ eval arg1 < m
/// 0 ≤ eval arg2 < m
/// Postconditions:
/// eval (from_montgomery out1) mod m = (eval (from_montgomery arg1) - eval (from_montgomery arg2)) mod m
/// 0 ≤ eval out1 < m
///
pub fn sub(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldElement, arg2: MontgomeryDomainFieldElement) void {
@setRuntimeSafety(mode == .Debug);
var x1: u64 = undefined;
var x2: u1 = undefined;
subborrowxU64(&x1, &x2, 0x0, (arg1[0]), (arg2[0]));
var x3: u64 = undefined;
var x4: u1 = undefined;
subborrowxU64(&x3, &x4, x2, (arg1[1]), (arg2[1]));
var x5: u64 = undefined;
var x6: u1 = undefined;
subborrowxU64(&x5, &x6, x4, (arg1[2]), (arg2[2]));
var x7: u64 = undefined;
var x8: u1 = undefined;
subborrowxU64(&x7, &x8, x6, (arg1[3]), (arg2[3]));
var x9: u64 = undefined;
cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff);
var x10: u64 = undefined;
var x11: u1 = undefined;
addcarryxU64(&x10, &x11, 0x0, x1, x9);
var x12: u64 = undefined;
var x13: u1 = undefined;
addcarryxU64(&x12, &x13, x11, x3, (x9 & 0xffffffff));
var x14: u64 = undefined;
var x15: u1 = undefined;
addcarryxU64(&x14, &x15, x13, x5, @as(u64, 0x0));
var x16: u64 = undefined;
var x17: u1 = undefined;
addcarryxU64(&x16, &x17, x15, x7, (x9 & 0xffffffff00000001));
out1[0] = x10;
out1[1] = x12;
out1[2] = x14;
out1[3] = x16;
}
/// The function opp negates a field element in the Montgomery domain.
///
/// Preconditions:
/// 0 ≤ eval arg1 < m
/// Postconditions:
/// eval (from_montgomery out1) mod m = -eval (from_montgomery arg1) mod m
/// 0 ≤ eval out1 < m
///
pub fn opp(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldElement) void {
@setRuntimeSafety(mode == .Debug);
var x1: u64 = undefined;
var x2: u1 = undefined;
subborrowxU64(&x1, &x2, 0x0, @as(u64, 0x0), (arg1[0]));
var x3: u64 = undefined;
var x4: u1 = undefined;
subborrowxU64(&x3, &x4, x2, @as(u64, 0x0), (arg1[1]));
var x5: u64 = undefined;
var x6: u1 = undefined;
subborrowxU64(&x5, &x6, x4, @as(u64, 0x0), (arg1[2]));
var x7: u64 = undefined;
var x8: u1 = undefined;
subborrowxU64(&x7, &x8, x6, @as(u64, 0x0), (arg1[3]));
var x9: u64 = undefined;
cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff);
var x10: u64 = undefined;
var x11: u1 = undefined;
addcarryxU64(&x10, &x11, 0x0, x1, x9);
var x12: u64 = undefined;
var x13: u1 = undefined;
addcarryxU64(&x12, &x13, x11, x3, (x9 & 0xffffffff));
var x14: u64 = undefined;
var x15: u1 = undefined;
addcarryxU64(&x14, &x15, x13, x5, @as(u64, 0x0));
var x16: u64 = undefined;
var x17: u1 = undefined;
addcarryxU64(&x16, &x17, x15, x7, (x9 & 0xffffffff00000001));
out1[0] = x10;
out1[1] = x12;
out1[2] = x14;
out1[3] = x16;
}
/// The function fromMontgomery translates a field element out of the Montgomery domain.
///
/// Preconditions:
/// 0 ≤ eval arg1 < m
/// Postconditions:
/// eval out1 mod m = (eval arg1 * ((2^64)⁻¹ mod m)^4) mod m
/// 0 ≤ eval out1 < m
///
pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldElement) void {
@setRuntimeSafety(mode == .Debug);
const x1 = (arg1[0]);
var x2: u64 = undefined;
var x3: u64 = undefined;
mulxU64(&x2, &x3, x1, 0xffffffff00000001);
var x4: u64 = undefined;
var x5: u64 = undefined;
mulxU64(&x4, &x5, x1, 0xffffffff);
var x6: u64 = undefined;
var x7: u64 = undefined;
mulxU64(&x6, &x7, x1, 0xffffffffffffffff);
var x8: u64 = undefined;
var x9: u1 = undefined;
addcarryxU64(&x8, &x9, 0x0, x7, x4);
var x10: u64 = undefined;
var x11: u1 = undefined;
addcarryxU64(&x10, &x11, 0x0, x1, x6);
var x12: u64 = undefined;
var x13: u1 = undefined;
addcarryxU64(&x12, &x13, x11, @as(u64, 0x0), x8);
var x14: u64 = undefined;
var x15: u1 = undefined;
addcarryxU64(&x14, &x15, 0x0, x12, (arg1[1]));
var x16: u64 = undefined;
var x17: u64 = undefined;
mulxU64(&x16, &x17, x14, 0xffffffff00000001);
var x18: u64 = undefined;
var x19: u64 = undefined;
mulxU64(&x18, &x19, x14, 0xffffffff);
var x20: u64 = undefined;
var x21: u64 = undefined;
mulxU64(&x20, &x21, x14, 0xffffffffffffffff);
var x22: u64 = undefined;
var x23: u1 = undefined;
addcarryxU64(&x22, &x23, 0x0, x21, x18);
var x24: u64 = undefined;
var x25: u1 = undefined;
addcarryxU64(&x24, &x25, 0x0, x14, x20);
var x26: u64 = undefined;
var x27: u1 = undefined;
addcarryxU64(&x26, &x27, x25, (@as(u64, x15) + (@as(u64, x13) + (@as(u64, x9) + x5))), x22);
var x28: u64 = undefined;
var x29: u1 = undefined;
addcarryxU64(&x28, &x29, x27, x2, (@as(u64, x23) + x19));
var x30: u64 = undefined;
var x31: u1 = undefined;
addcarryxU64(&x30, &x31, x29, x3, x16);
var x32: u64 = undefined;
var x33: u1 = undefined;
addcarryxU64(&x32, &x33, 0x0, x26, (arg1[2]));
var x34: u64 = undefined;
var x35: u1 = undefined;
addcarryxU64(&x34, &x35, x33, x28, @as(u64, 0x0));
var x36: u64 = undefined;
var x37: u1 = undefined;
addcarryxU64(&x36, &x37, x35, x30, @as(u64, 0x0));
var x38: u64 = undefined;
var x39: u64 = undefined;
mulxU64(&x38, &x39, x32, 0xffffffff00000001);
var x40: u64 = undefined;
var x41: u64 = undefined;
mulxU64(&x40, &x41, x32, 0xffffffff);
var x42: u64 = undefined;
var x43: u64 = undefined;
mulxU64(&x42, &x43, x32, 0xffffffffffffffff);
var x44: u64 = undefined;
var x45: u1 = undefined;
addcarryxU64(&x44, &x45, 0x0, x43, x40);
var x46: u64 = undefined;
var x47: u1 = undefined;
addcarryxU64(&x46, &x47, 0x0, x32, x42);
var x48: u64 = undefined;
var x49: u1 = undefined;
addcarryxU64(&x48, &x49, x47, x34, x44);
var x50: u64 = undefined;
var x51: u1 = undefined;
addcarryxU64(&x50, &x51, x49, x36, (@as(u64, x45) + x41));
var x52: u64 = undefined;
var x53: u1 = undefined;
addcarryxU64(&x52, &x53, x51, (@as(u64, x37) + (@as(u64, x31) + x17)), x38);
var x54: u64 = undefined;
var x55: u1 = undefined;
addcarryxU64(&x54, &x55, 0x0, x48, (arg1[3]));
var x56: u64 = undefined;
var x57: u1 = undefined;
addcarryxU64(&x56, &x57, x55, x50, @as(u64, 0x0));
var x58: u64 = undefined;
var x59: u1 = undefined;
addcarryxU64(&x58, &x59, x57, x52, @as(u64, 0x0));
var x60: u64 = undefined;
var x61: u64 = undefined;
mulxU64(&x60, &x61, x54, 0xffffffff00000001);
var x62: u64 = undefined;
var x63: u64 = undefined;
mulxU64(&x62, &x63, x54, 0xffffffff);
var x64: u64 = undefined;
var x65: u64 = undefined;
mulxU64(&x64, &x65, x54, 0xffffffffffffffff);
var x66: u64 = undefined;
var x67: u1 = undefined;
addcarryxU64(&x66, &x67, 0x0, x65, x62);
var x68: u64 = undefined;
var x69: u1 = undefined;
addcarryxU64(&x68, &x69, 0x0, x54, x64);
var x70: u64 = undefined;
var x71: u1 = undefined;
addcarryxU64(&x70, &x71, x69, x56, x66);
var x72: u64 = undefined;
var x73: u1 = undefined;
addcarryxU64(&x72, &x73, x71, x58, (@as(u64, x67) + x63));
var x74: u64 = undefined;
var x75: u1 = undefined;
addcarryxU64(&x74, &x75, x73, (@as(u64, x59) + (@as(u64, x53) + x39)), x60);
const x76 = (@as(u64, x75) + x61);
var x77: u64 = undefined;
var x78: u1 = undefined;
subborrowxU64(&x77, &x78, 0x0, x70, 0xffffffffffffffff);
var x79: u64 = undefined;
var x80: u1 = undefined;
subborrowxU64(&x79, &x80, x78, x72, 0xffffffff);
var x81: u64 = undefined;
var x82: u1 = undefined;
subborrowxU64(&x81, &x82, x80, x74, @as(u64, 0x0));
var x83: u64 = undefined;
var x84: u1 = undefined;
subborrowxU64(&x83, &x84, x82, x76, 0xffffffff00000001);
var x85: u64 = undefined;
var x86: u1 = undefined;