summaryrefslogtreecommitdiff
path: root/CryptoPkg/Library/OpensslLib/OpensslGen/X64-MSFT/crypto/ec/x25519-x86_64.nasm
blob: d5dc6fbc47fbdf9c5dc614bc4b938f6f85fbfefa (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
default rel
%define XMMWORD
%define YMMWORD
%define ZMMWORD
section .text code align=64


global  x25519_fe51_mul

ALIGN   32
x25519_fe51_mul:
        mov     QWORD[8+rsp],rdi        ;WIN64 prologue
        mov     QWORD[16+rsp],rsi
        mov     rax,rsp
$L$SEH_begin_x25519_fe51_mul:
        mov     rdi,rcx
        mov     rsi,rdx
        mov     rdx,r8



        push    rbp

        push    rbx

        push    r12

        push    r13

        push    r14

        push    r15

        lea     rsp,[((-40))+rsp]

$L$fe51_mul_body:

        mov     rax,QWORD[rsi]
        mov     r11,QWORD[rdx]
        mov     r12,QWORD[8+rdx]
        mov     r13,QWORD[16+rdx]
        mov     rbp,QWORD[24+rdx]
        mov     r14,QWORD[32+rdx]

        mov     QWORD[32+rsp],rdi
        mov     rdi,rax
        mul     r11
        mov     QWORD[rsp],r11
        mov     rbx,rax
        mov     rax,rdi
        mov     rcx,rdx
        mul     r12
        mov     QWORD[8+rsp],r12
        mov     r8,rax
        mov     rax,rdi
        lea     r15,[r14*8+r14]
        mov     r9,rdx
        mul     r13
        mov     QWORD[16+rsp],r13
        mov     r10,rax
        mov     rax,rdi
        lea     rdi,[r15*2+r14]
        mov     r11,rdx
        mul     rbp
        mov     r12,rax
        mov     rax,QWORD[rsi]
        mov     r13,rdx
        mul     r14
        mov     r14,rax
        mov     rax,QWORD[8+rsi]
        mov     r15,rdx

        mul     rdi
        add     rbx,rax
        mov     rax,QWORD[16+rsi]
        adc     rcx,rdx
        mul     rdi
        add     r8,rax
        mov     rax,QWORD[24+rsi]
        adc     r9,rdx
        mul     rdi
        add     r10,rax
        mov     rax,QWORD[32+rsi]
        adc     r11,rdx
        mul     rdi
        imul    rdi,rbp,19
        add     r12,rax
        mov     rax,QWORD[8+rsi]
        adc     r13,rdx
        mul     rbp
        mov     rbp,QWORD[16+rsp]
        add     r14,rax
        mov     rax,QWORD[16+rsi]
        adc     r15,rdx

        mul     rdi
        add     rbx,rax
        mov     rax,QWORD[24+rsi]
        adc     rcx,rdx
        mul     rdi
        add     r8,rax
        mov     rax,QWORD[32+rsi]
        adc     r9,rdx
        mul     rdi
        imul    rdi,rbp,19
        add     r10,rax
        mov     rax,QWORD[8+rsi]
        adc     r11,rdx
        mul     rbp
        add     r12,rax
        mov     rax,QWORD[16+rsi]
        adc     r13,rdx
        mul     rbp
        mov     rbp,QWORD[8+rsp]
        add     r14,rax
        mov     rax,QWORD[24+rsi]
        adc     r15,rdx

        mul     rdi
        add     rbx,rax
        mov     rax,QWORD[32+rsi]
        adc     rcx,rdx
        mul     rdi
        add     r8,rax
        mov     rax,QWORD[8+rsi]
        adc     r9,rdx
        mul     rbp
        imul    rdi,rbp,19
        add     r10,rax
        mov     rax,QWORD[16+rsi]
        adc     r11,rdx
        mul     rbp
        add     r12,rax
        mov     rax,QWORD[24+rsi]
        adc     r13,rdx
        mul     rbp
        mov     rbp,QWORD[rsp]
        add     r14,rax
        mov     rax,QWORD[32+rsi]
        adc     r15,rdx

        mul     rdi
        add     rbx,rax
        mov     rax,QWORD[8+rsi]
        adc     rcx,rdx
        mul     rbp
        add     r8,rax
        mov     rax,QWORD[16+rsi]
        adc     r9,rdx
        mul     rbp
        add     r10,rax
        mov     rax,QWORD[24+rsi]
        adc     r11,rdx
        mul     rbp
        add     r12,rax
        mov     rax,QWORD[32+rsi]
        adc     r13,rdx
        mul     rbp
        add     r14,rax
        adc     r15,rdx

        mov     rdi,QWORD[32+rsp]
        jmp     NEAR $L$reduce51
$L$fe51_mul_epilogue:

$L$SEH_end_x25519_fe51_mul:

global  x25519_fe51_sqr

ALIGN   32
x25519_fe51_sqr:
        mov     QWORD[8+rsp],rdi        ;WIN64 prologue
        mov     QWORD[16+rsp],rsi
        mov     rax,rsp
$L$SEH_begin_x25519_fe51_sqr:
        mov     rdi,rcx
        mov     rsi,rdx



        push    rbp

        push    rbx

        push    r12

        push    r13

        push    r14

        push    r15

        lea     rsp,[((-40))+rsp]

$L$fe51_sqr_body:

        mov     rax,QWORD[rsi]
        mov     r15,QWORD[16+rsi]
        mov     rbp,QWORD[32+rsi]

        mov     QWORD[32+rsp],rdi
        lea     r14,[rax*1+rax]
        mul     rax
        mov     rbx,rax
        mov     rax,QWORD[8+rsi]
        mov     rcx,rdx
        mul     r14
        mov     r8,rax
        mov     rax,r15
        mov     QWORD[rsp],r15
        mov     r9,rdx
        mul     r14
        mov     r10,rax
        mov     rax,QWORD[24+rsi]
        mov     r11,rdx
        imul    rdi,rbp,19
        mul     r14
        mov     r12,rax
        mov     rax,rbp
        mov     r13,rdx
        mul     r14
        mov     r14,rax
        mov     rax,rbp
        mov     r15,rdx

        mul     rdi
        add     r12,rax
        mov     rax,QWORD[8+rsi]
        adc     r13,rdx

        mov     rsi,QWORD[24+rsi]
        lea     rbp,[rax*1+rax]
        mul     rax
        add     r10,rax
        mov     rax,QWORD[rsp]
        adc     r11,rdx
        mul     rbp
        add     r12,rax
        mov     rax,rbp
        adc     r13,rdx
        mul     rsi
        add     r14,rax
        mov     rax,rbp
        adc     r15,rdx
        imul    rbp,rsi,19
        mul     rdi
        add     rbx,rax
        lea     rax,[rsi*1+rsi]
        adc     rcx,rdx

        mul     rdi
        add     r10,rax
        mov     rax,rsi
        adc     r11,rdx
        mul     rbp
        add     r8,rax
        mov     rax,QWORD[rsp]
        adc     r9,rdx

        lea     rsi,[rax*1+rax]
        mul     rax
        add     r14,rax
        mov     rax,rbp
        adc     r15,rdx
        mul     rsi
        add     rbx,rax
        mov     rax,rsi
        adc     rcx,rdx
        mul     rdi
        add     r8,rax
        adc     r9,rdx

        mov     rdi,QWORD[32+rsp]
        jmp     NEAR $L$reduce51

ALIGN   32
$L$reduce51:
        mov     rbp,0x7ffffffffffff

        mov     rdx,r10
        shr     r10,51
        shl     r11,13
        and     rdx,rbp
        or      r11,r10
        add     r12,r11
        adc     r13,0

        mov     rax,rbx
        shr     rbx,51
        shl     rcx,13
        and     rax,rbp
        or      rcx,rbx
        add     r8,rcx
        adc     r9,0

        mov     rbx,r12
        shr     r12,51
        shl     r13,13
        and     rbx,rbp
        or      r13,r12
        add     r14,r13
        adc     r15,0

        mov     rcx,r8
        shr     r8,51
        shl     r9,13
        and     rcx,rbp
        or      r9,r8
        add     rdx,r9

        mov     r10,r14
        shr     r14,51
        shl     r15,13
        and     r10,rbp
        or      r15,r14

        lea     r14,[r15*8+r15]
        lea     r15,[r14*2+r15]
        add     rax,r15

        mov     r8,rdx
        and     rdx,rbp
        shr     r8,51
        add     rbx,r8

        mov     r9,rax
        and     rax,rbp
        shr     r9,51
        add     rcx,r9

        mov     QWORD[rdi],rax
        mov     QWORD[8+rdi],rcx
        mov     QWORD[16+rdi],rdx
        mov     QWORD[24+rdi],rbx
        mov     QWORD[32+rdi],r10

        mov     r15,QWORD[40+rsp]

        mov     r14,QWORD[48+rsp]

        mov     r13,QWORD[56+rsp]

        mov     r12,QWORD[64+rsp]

        mov     rbx,QWORD[72+rsp]

        mov     rbp,QWORD[80+rsp]

        lea     rsp,[88+rsp]

$L$fe51_sqr_epilogue:
        mov     rdi,QWORD[8+rsp]        ;WIN64 epilogue
        mov     rsi,QWORD[16+rsp]
        DB      0F3h,0C3h               ;repret

$L$SEH_end_x25519_fe51_sqr:

global  x25519_fe51_mul121666

ALIGN   32
x25519_fe51_mul121666:
        mov     QWORD[8+rsp],rdi        ;WIN64 prologue
        mov     QWORD[16+rsp],rsi
        mov     rax,rsp
$L$SEH_begin_x25519_fe51_mul121666:
        mov     rdi,rcx
        mov     rsi,rdx



        push    rbp

        push    rbx

        push    r12

        push    r13

        push    r14

        push    r15

        lea     rsp,[((-40))+rsp]

$L$fe51_mul121666_body:
        mov     eax,121666

        mul     QWORD[rsi]
        mov     rbx,rax
        mov     eax,121666
        mov     rcx,rdx
        mul     QWORD[8+rsi]
        mov     r8,rax
        mov     eax,121666
        mov     r9,rdx
        mul     QWORD[16+rsi]
        mov     r10,rax
        mov     eax,121666
        mov     r11,rdx
        mul     QWORD[24+rsi]
        mov     r12,rax
        mov     eax,121666
        mov     r13,rdx
        mul     QWORD[32+rsi]
        mov     r14,rax
        mov     r15,rdx

        jmp     NEAR $L$reduce51
$L$fe51_mul121666_epilogue:

$L$SEH_end_x25519_fe51_mul121666:
EXTERN  OPENSSL_ia32cap_P
global  x25519_fe64_eligible

ALIGN   32
x25519_fe64_eligible:

        mov     ecx,DWORD[((OPENSSL_ia32cap_P+8))]
        xor     eax,eax
        and     ecx,0x80100
        cmp     ecx,0x80100
        cmove   eax,ecx
        DB      0F3h,0C3h               ;repret



global  x25519_fe64_mul

ALIGN   32
x25519_fe64_mul:
        mov     QWORD[8+rsp],rdi        ;WIN64 prologue
        mov     QWORD[16+rsp],rsi
        mov     rax,rsp
$L$SEH_begin_x25519_fe64_mul:
        mov     rdi,rcx
        mov     rsi,rdx
        mov     rdx,r8



        push    rbp

        push    rbx

        push    r12

        push    r13

        push    r14

        push    r15

        push    rdi

        lea     rsp,[((-16))+rsp]

$L$fe64_mul_body:

        mov     rax,rdx
        mov     rbp,QWORD[rdx]
        mov     rdx,QWORD[rsi]
        mov     rcx,QWORD[8+rax]
        mov     r14,QWORD[16+rax]
        mov     r15,QWORD[24+rax]

        mulx    rax,r8,rbp
        xor     edi,edi
        mulx    rbx,r9,rcx
        adcx    r9,rax
        mulx    rax,r10,r14
        adcx    r10,rbx
        mulx    r12,r11,r15
        mov     rdx,QWORD[8+rsi]
        adcx    r11,rax
        mov     QWORD[rsp],r14
        adcx    r12,rdi

        mulx    rbx,rax,rbp
        adox    r9,rax
        adcx    r10,rbx
        mulx    rbx,rax,rcx
        adox    r10,rax
        adcx    r11,rbx
        mulx    rbx,rax,r14
        adox    r11,rax
        adcx    r12,rbx
        mulx    r13,rax,r15
        mov     rdx,QWORD[16+rsi]
        adox    r12,rax
        adcx    r13,rdi
        adox    r13,rdi

        mulx    rbx,rax,rbp
        adcx    r10,rax
        adox    r11,rbx
        mulx    rbx,rax,rcx
        adcx    r11,rax
        adox    r12,rbx
        mulx    rbx,rax,r14
        adcx    r12,rax
        adox    r13,rbx
        mulx    r14,rax,r15
        mov     rdx,QWORD[24+rsi]
        adcx    r13,rax
        adox    r14,rdi
        adcx    r14,rdi

        mulx    rbx,rax,rbp
        adox    r11,rax
        adcx    r12,rbx
        mulx    rbx,rax,rcx
        adox    r12,rax
        adcx    r13,rbx
        mulx    rbx,rax,QWORD[rsp]
        adox    r13,rax
        adcx    r14,rbx
        mulx    r15,rax,r15
        mov     edx,38
        adox    r14,rax
        adcx    r15,rdi
        adox    r15,rdi

        jmp     NEAR $L$reduce64
$L$fe64_mul_epilogue:

$L$SEH_end_x25519_fe64_mul:

global  x25519_fe64_sqr

ALIGN   32
x25519_fe64_sqr:
        mov     QWORD[8+rsp],rdi        ;WIN64 prologue
        mov     QWORD[16+rsp],rsi
        mov     rax,rsp
$L$SEH_begin_x25519_fe64_sqr:
        mov     rdi,rcx
        mov     rsi,rdx



        push    rbp

        push    rbx

        push    r12

        push    r13

        push    r14

        push    r15

        push    rdi

        lea     rsp,[((-16))+rsp]

$L$fe64_sqr_body:

        mov     rdx,QWORD[rsi]
        mov     rcx,QWORD[8+rsi]
        mov     rbp,QWORD[16+rsi]
        mov     rsi,QWORD[24+rsi]


        mulx    r15,r8,rdx
        mulx    rax,r9,rcx
        xor     edi,edi
        mulx    rbx,r10,rbp
        adcx    r10,rax
        mulx    r12,r11,rsi
        mov     rdx,rcx
        adcx    r11,rbx
        adcx    r12,rdi


        mulx    rbx,rax,rbp
        adox    r11,rax
        adcx    r12,rbx
        mulx    r13,rax,rsi
        mov     rdx,rbp
        adox    r12,rax
        adcx    r13,rdi


        mulx    r14,rax,rsi
        mov     rdx,rcx
        adox    r13,rax
        adcx    r14,rdi
        adox    r14,rdi

        adcx    r9,r9
        adox    r9,r15
        adcx    r10,r10
        mulx    rbx,rax,rdx
        mov     rdx,rbp
        adcx    r11,r11
        adox    r10,rax
        adcx    r12,r12
        adox    r11,rbx
        mulx    rbx,rax,rdx
        mov     rdx,rsi
        adcx    r13,r13
        adox    r12,rax
        adcx    r14,r14
        adox    r13,rbx
        mulx    r15,rax,rdx
        mov     edx,38
        adox    r14,rax
        adcx    r15,rdi
        adox    r15,rdi
        jmp     NEAR $L$reduce64

ALIGN   32
$L$reduce64:
        mulx    rbx,rax,r12
        adcx    r8,rax
        adox    r9,rbx
        mulx    rbx,rax,r13
        adcx    r9,rax
        adox    r10,rbx
        mulx    rbx,rax,r14
        adcx    r10,rax
        adox    r11,rbx
        mulx    r12,rax,r15
        adcx    r11,rax
        adox    r12,rdi
        adcx    r12,rdi

        mov     rdi,QWORD[16+rsp]
        imul    r12,rdx

        add     r8,r12
        adc     r9,0
        adc     r10,0
        adc     r11,0

        sbb     rax,rax
        and     rax,38

        add     r8,rax
        mov     QWORD[8+rdi],r9
        mov     QWORD[16+rdi],r10
        mov     QWORD[24+rdi],r11
        mov     QWORD[rdi],r8

        mov     r15,QWORD[24+rsp]

        mov     r14,QWORD[32+rsp]

        mov     r13,QWORD[40+rsp]

        mov     r12,QWORD[48+rsp]

        mov     rbx,QWORD[56+rsp]

        mov     rbp,QWORD[64+rsp]

        lea     rsp,[72+rsp]

$L$fe64_sqr_epilogue:
        mov     rdi,QWORD[8+rsp]        ;WIN64 epilogue
        mov     rsi,QWORD[16+rsp]
        DB      0F3h,0C3h               ;repret

$L$SEH_end_x25519_fe64_sqr:

global  x25519_fe64_mul121666

ALIGN   32
x25519_fe64_mul121666:
        mov     QWORD[8+rsp],rdi        ;WIN64 prologue
        mov     QWORD[16+rsp],rsi
        mov     rax,rsp
$L$SEH_begin_x25519_fe64_mul121666:
        mov     rdi,rcx
        mov     rsi,rdx


$L$fe64_mul121666_body:

        mov     edx,121666
        mulx    rcx,r8,QWORD[rsi]
        mulx    rax,r9,QWORD[8+rsi]
        add     r9,rcx
        mulx    rcx,r10,QWORD[16+rsi]
        adc     r10,rax
        mulx    rax,r11,QWORD[24+rsi]
        adc     r11,rcx
        adc     rax,0

        imul    rax,rax,38

        add     r8,rax
        adc     r9,0
        adc     r10,0
        adc     r11,0

        sbb     rax,rax
        and     rax,38

        add     r8,rax
        mov     QWORD[8+rdi],r9
        mov     QWORD[16+rdi],r10
        mov     QWORD[24+rdi],r11
        mov     QWORD[rdi],r8

$L$fe64_mul121666_epilogue:
        mov     rdi,QWORD[8+rsp]        ;WIN64 epilogue
        mov     rsi,QWORD[16+rsp]
        DB      0F3h,0C3h               ;repret

$L$SEH_end_x25519_fe64_mul121666:

global  x25519_fe64_add

ALIGN   32
x25519_fe64_add:
        mov     QWORD[8+rsp],rdi        ;WIN64 prologue
        mov     QWORD[16+rsp],rsi
        mov     rax,rsp
$L$SEH_begin_x25519_fe64_add:
        mov     rdi,rcx
        mov     rsi,rdx
        mov     rdx,r8


$L$fe64_add_body:

        mov     r8,QWORD[rsi]
        mov     r9,QWORD[8+rsi]
        mov     r10,QWORD[16+rsi]
        mov     r11,QWORD[24+rsi]

        add     r8,QWORD[rdx]
        adc     r9,QWORD[8+rdx]
        adc     r10,QWORD[16+rdx]
        adc     r11,QWORD[24+rdx]

        sbb     rax,rax
        and     rax,38

        add     r8,rax
        adc     r9,0
        adc     r10,0
        mov     QWORD[8+rdi],r9
        adc     r11,0
        mov     QWORD[16+rdi],r10
        sbb     rax,rax
        mov     QWORD[24+rdi],r11
        and     rax,38

        add     r8,rax
        mov     QWORD[rdi],r8

$L$fe64_add_epilogue:
        mov     rdi,QWORD[8+rsp]        ;WIN64 epilogue
        mov     rsi,QWORD[16+rsp]
        DB      0F3h,0C3h               ;repret

$L$SEH_end_x25519_fe64_add:

global  x25519_fe64_sub

ALIGN   32
x25519_fe64_sub:
        mov     QWORD[8+rsp],rdi        ;WIN64 prologue
        mov     QWORD[16+rsp],rsi
        mov     rax,rsp
$L$SEH_begin_x25519_fe64_sub:
        mov     rdi,rcx
        mov     rsi,rdx
        mov     rdx,r8


$L$fe64_sub_body:

        mov     r8,QWORD[rsi]
        mov     r9,QWORD[8+rsi]
        mov     r10,QWORD[16+rsi]
        mov     r11,QWORD[24+rsi]

        sub     r8,QWORD[rdx]
        sbb     r9,QWORD[8+rdx]
        sbb     r10,QWORD[16+rdx]
        sbb     r11,QWORD[24+rdx]

        sbb     rax,rax
        and     rax,38

        sub     r8,rax
        sbb     r9,0
        sbb     r10,0
        mov     QWORD[8+rdi],r9
        sbb     r11,0
        mov     QWORD[16+rdi],r10
        sbb     rax,rax
        mov     QWORD[24+rdi],r11
        and     rax,38

        sub     r8,rax
        mov     QWORD[rdi],r8

$L$fe64_sub_epilogue:
        mov     rdi,QWORD[8+rsp]        ;WIN64 epilogue
        mov     rsi,QWORD[16+rsp]
        DB      0F3h,0C3h               ;repret

$L$SEH_end_x25519_fe64_sub:

global  x25519_fe64_tobytes

ALIGN   32
x25519_fe64_tobytes:
        mov     QWORD[8+rsp],rdi        ;WIN64 prologue
        mov     QWORD[16+rsp],rsi
        mov     rax,rsp
$L$SEH_begin_x25519_fe64_tobytes:
        mov     rdi,rcx
        mov     rsi,rdx


$L$fe64_to_body:

        mov     r8,QWORD[rsi]
        mov     r9,QWORD[8+rsi]
        mov     r10,QWORD[16+rsi]
        mov     r11,QWORD[24+rsi]


        lea     rax,[r11*1+r11]
        sar     r11,63
        shr     rax,1
        and     r11,19
        add     r11,19

        add     r8,r11
        adc     r9,0
        adc     r10,0
        adc     rax,0

        lea     r11,[rax*1+rax]
        sar     rax,63
        shr     r11,1
        not     rax
        and     rax,19

        sub     r8,rax
        sbb     r9,0
        sbb     r10,0
        sbb     r11,0

        mov     QWORD[rdi],r8
        mov     QWORD[8+rdi],r9
        mov     QWORD[16+rdi],r10
        mov     QWORD[24+rdi],r11

$L$fe64_to_epilogue:
        mov     rdi,QWORD[8+rsp]        ;WIN64 epilogue
        mov     rsi,QWORD[16+rsp]
        DB      0F3h,0C3h               ;repret

$L$SEH_end_x25519_fe64_tobytes:
DB      88,50,53,53,49,57,32,112,114,105,109,105,116,105,118,101
DB      115,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82
DB      89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112
DB      114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
EXTERN  __imp_RtlVirtualUnwind


ALIGN   16
short_handler:
        push    rsi
        push    rdi
        push    rbx
        push    rbp
        push    r12
        push    r13
        push    r14
        push    r15
        pushfq
        sub     rsp,64

        mov     rax,QWORD[120+r8]
        mov     rbx,QWORD[248+r8]

        mov     rsi,QWORD[8+r9]
        mov     r11,QWORD[56+r9]

        mov     r10d,DWORD[r11]
        lea     r10,[r10*1+rsi]
        cmp     rbx,r10
        jb      NEAR $L$common_seh_tail

        mov     rax,QWORD[152+r8]
        jmp     NEAR $L$common_seh_tail



ALIGN   16
full_handler:
        push    rsi
        push    rdi
        push    rbx
        push    rbp
        push    r12
        push    r13
        push    r14
        push    r15
        pushfq
        sub     rsp,64

        mov     rax,QWORD[120+r8]
        mov     rbx,QWORD[248+r8]

        mov     rsi,QWORD[8+r9]
        mov     r11,QWORD[56+r9]

        mov     r10d,DWORD[r11]
        lea     r10,[r10*1+rsi]
        cmp     rbx,r10
        jb      NEAR $L$common_seh_tail

        mov     rax,QWORD[152+r8]

        mov     r10d,DWORD[4+r11]
        lea     r10,[r10*1+rsi]
        cmp     rbx,r10
        jae     NEAR $L$common_seh_tail

        mov     r10d,DWORD[8+r11]
        lea     rax,[r10*1+rax]

        mov     rbp,QWORD[((-8))+rax]
        mov     rbx,QWORD[((-16))+rax]
        mov     r12,QWORD[((-24))+rax]
        mov     r13,QWORD[((-32))+rax]
        mov     r14,QWORD[((-40))+rax]
        mov     r15,QWORD[((-48))+rax]
        mov     QWORD[144+r8],rbx
        mov     QWORD[160+r8],rbp
        mov     QWORD[216+r8],r12
        mov     QWORD[224+r8],r13
        mov     QWORD[232+r8],r14
        mov     QWORD[240+r8],r15

$L$common_seh_tail:
        mov     rdi,QWORD[8+rax]
        mov     rsi,QWORD[16+rax]
        mov     QWORD[152+r8],rax
        mov     QWORD[168+r8],rsi
        mov     QWORD[176+r8],rdi

        mov     rdi,QWORD[40+r9]
        mov     rsi,r8
        mov     ecx,154
        DD      0xa548f3fc

        mov     rsi,r9
        xor     rcx,rcx
        mov     rdx,QWORD[8+rsi]
        mov     r8,QWORD[rsi]
        mov     r9,QWORD[16+rsi]
        mov     r10,QWORD[40+rsi]
        lea     r11,[56+rsi]
        lea     r12,[24+rsi]
        mov     QWORD[32+rsp],r10
        mov     QWORD[40+rsp],r11
        mov     QWORD[48+rsp],r12
        mov     QWORD[56+rsp],rcx
        call    QWORD[__imp_RtlVirtualUnwind]

        mov     eax,1
        add     rsp,64
        popfq
        pop     r15
        pop     r14
        pop     r13
        pop     r12
        pop     rbp
        pop     rbx
        pop     rdi
        pop     rsi
        DB      0F3h,0C3h               ;repret


section .pdata rdata align=4
ALIGN   4
        DD      $L$SEH_begin_x25519_fe51_mul wrt ..imagebase
        DD      $L$SEH_end_x25519_fe51_mul wrt ..imagebase
        DD      $L$SEH_info_x25519_fe51_mul wrt ..imagebase

        DD      $L$SEH_begin_x25519_fe51_sqr wrt ..imagebase
        DD      $L$SEH_end_x25519_fe51_sqr wrt ..imagebase
        DD      $L$SEH_info_x25519_fe51_sqr wrt ..imagebase

        DD      $L$SEH_begin_x25519_fe51_mul121666 wrt ..imagebase
        DD      $L$SEH_end_x25519_fe51_mul121666 wrt ..imagebase
        DD      $L$SEH_info_x25519_fe51_mul121666 wrt ..imagebase
        DD      $L$SEH_begin_x25519_fe64_mul wrt ..imagebase
        DD      $L$SEH_end_x25519_fe64_mul wrt ..imagebase
        DD      $L$SEH_info_x25519_fe64_mul wrt ..imagebase

        DD      $L$SEH_begin_x25519_fe64_sqr wrt ..imagebase
        DD      $L$SEH_end_x25519_fe64_sqr wrt ..imagebase
        DD      $L$SEH_info_x25519_fe64_sqr wrt ..imagebase

        DD      $L$SEH_begin_x25519_fe64_mul121666 wrt ..imagebase
        DD      $L$SEH_end_x25519_fe64_mul121666 wrt ..imagebase
        DD      $L$SEH_info_x25519_fe64_mul121666 wrt ..imagebase

        DD      $L$SEH_begin_x25519_fe64_add wrt ..imagebase
        DD      $L$SEH_end_x25519_fe64_add wrt ..imagebase
        DD      $L$SEH_info_x25519_fe64_add wrt ..imagebase

        DD      $L$SEH_begin_x25519_fe64_sub wrt ..imagebase
        DD      $L$SEH_end_x25519_fe64_sub wrt ..imagebase
        DD      $L$SEH_info_x25519_fe64_sub wrt ..imagebase

        DD      $L$SEH_begin_x25519_fe64_tobytes wrt ..imagebase
        DD      $L$SEH_end_x25519_fe64_tobytes wrt ..imagebase
        DD      $L$SEH_info_x25519_fe64_tobytes wrt ..imagebase
section .xdata rdata align=8
ALIGN   8
$L$SEH_info_x25519_fe51_mul:
DB      9,0,0,0
        DD      full_handler wrt ..imagebase
        DD      $L$fe51_mul_body wrt ..imagebase,$L$fe51_mul_epilogue wrt ..imagebase
        DD      88,0
$L$SEH_info_x25519_fe51_sqr:
DB      9,0,0,0
        DD      full_handler wrt ..imagebase
        DD      $L$fe51_sqr_body wrt ..imagebase,$L$fe51_sqr_epilogue wrt ..imagebase
        DD      88,0
$L$SEH_info_x25519_fe51_mul121666:
DB      9,0,0,0
        DD      full_handler wrt ..imagebase
        DD      $L$fe51_mul121666_body wrt ..imagebase,$L$fe51_mul121666_epilogue wrt ..imagebase
        DD      88,0
$L$SEH_info_x25519_fe64_mul:
DB      9,0,0,0
        DD      full_handler wrt ..imagebase
        DD      $L$fe64_mul_body wrt ..imagebase,$L$fe64_mul_epilogue wrt ..imagebase
        DD      72,0
$L$SEH_info_x25519_fe64_sqr:
DB      9,0,0,0
        DD      full_handler wrt ..imagebase
        DD      $L$fe64_sqr_body wrt ..imagebase,$L$fe64_sqr_epilogue wrt ..imagebase
        DD      72,0
$L$SEH_info_x25519_fe64_mul121666:
DB      9,0,0,0
        DD      short_handler wrt ..imagebase
        DD      $L$fe64_mul121666_body wrt ..imagebase,$L$fe64_mul121666_epilogue wrt ..imagebase
$L$SEH_info_x25519_fe64_add:
DB      9,0,0,0
        DD      short_handler wrt ..imagebase
        DD      $L$fe64_add_body wrt ..imagebase,$L$fe64_add_epilogue wrt ..imagebase
$L$SEH_info_x25519_fe64_sub:
DB      9,0,0,0
        DD      short_handler wrt ..imagebase
        DD      $L$fe64_sub_body wrt ..imagebase,$L$fe64_sub_epilogue wrt ..imagebase
$L$SEH_info_x25519_fe64_tobytes:
DB      9,0,0,0
        DD      short_handler wrt ..imagebase
        DD      $L$fe64_to_body wrt ..imagebase,$L$fe64_to_epilogue wrt ..imagebase