summaryrefslogtreecommitdiff
path: root/drivers/builtin_openssl2/crypto/bn/asm/modexp512-x86_64.pl
blob: bfd6e975416de8c9bc51b55f994b6fcb2f5ef5f6 (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
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
#!/usr/bin/env perl
#
# Copyright (c) 2010-2011 Intel Corp.
#   Author: Vinodh.Gopal@intel.com
#           Jim Guilford
#           Erdinc.Ozturk@intel.com
#           Maxim.Perminov@intel.com
#
# More information about algorithm used can be found at:
#   http://www.cse.buffalo.edu/srds2009/escs2009_submission_Gopal.pdf
#
# ====================================================================
# Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# 3. All advertising materials mentioning features or use of this
#    software must display the following acknowledgment:
#    "This product includes software developed by the OpenSSL Project
#    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
#
# 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
#    endorse or promote products derived from this software without
#    prior written permission. For written permission, please contact
#    licensing@OpenSSL.org.
#
# 5. Products derived from this software may not be called "OpenSSL"
#    nor may "OpenSSL" appear in their names without prior written
#    permission of the OpenSSL Project.
#
# 6. Redistributions of any form whatsoever must retain the following
#    acknowledgment:
#    "This product includes software developed by the OpenSSL Project
#    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
#
# THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
# ====================================================================

$flavour = shift;
$output  = shift;
if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }

my $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);

$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
die "can't locate x86_64-xlate.pl";

open OUT,"| \"$^X\" $xlate $flavour $output";
*STDOUT=*OUT;

use strict;
my $code=".text\n\n";
my $m=0;

#
# Define x512 macros
#

#MULSTEP_512_ADD	MACRO	x7, x6, x5, x4, x3, x2, x1, x0, dst, src1, src2, add_src, tmp1, tmp2
#
# uses rax, rdx, and args
sub MULSTEP_512_ADD
{
 my ($x, $DST, $SRC2, $ASRC, $OP, $TMP)=@_;
 my @X=@$x;	# make a copy
$code.=<<___;
	 mov	(+8*0)($SRC2), %rax
	 mul	$OP			# rdx:rax = %OP * [0]
	 mov	($ASRC), $X[0]
	 add	%rax, $X[0]
	 adc	\$0, %rdx
	 mov	$X[0], $DST
___
for(my $i=1;$i<8;$i++) {
$code.=<<___;
	 mov	%rdx, $TMP

	 mov	(+8*$i)($SRC2), %rax
	 mul	$OP			# rdx:rax = %OP * [$i]
	 mov	(+8*$i)($ASRC), $X[$i]
	 add	%rax, $X[$i]
	 adc	\$0, %rdx
	 add	$TMP, $X[$i]
	 adc	\$0, %rdx
___
}
$code.=<<___;
	 mov	%rdx, $X[0]
___
}

#MULSTEP_512	MACRO	x7, x6, x5, x4, x3, x2, x1, x0, dst, src2, src1_val, tmp
#
# uses rax, rdx, and args
sub MULSTEP_512
{
 my ($x, $DST, $SRC2, $OP, $TMP)=@_;
 my @X=@$x;	# make a copy
$code.=<<___;
	 mov	(+8*0)($SRC2), %rax
	 mul	$OP			# rdx:rax = %OP * [0]
	 add	%rax, $X[0]
	 adc	\$0, %rdx
	 mov	$X[0], $DST
___
for(my $i=1;$i<8;$i++) {
$code.=<<___;
	 mov	%rdx, $TMP

	 mov	(+8*$i)($SRC2), %rax
	 mul	$OP			# rdx:rax = %OP * [$i]
	 add	%rax, $X[$i]
	 adc	\$0, %rdx
	 add	$TMP, $X[$i]
	 adc	\$0, %rdx
___
}
$code.=<<___;
	 mov	%rdx, $X[0]
___
}

#
# Swizzle Macros
#

# macro to copy data from flat space to swizzled table
#MACRO swizzle	pDst, pSrc, tmp1, tmp2
# pDst and pSrc are modified
sub swizzle
{
 my ($pDst, $pSrc, $cnt, $d0)=@_;
$code.=<<___;
	 mov	\$8, $cnt
loop_$m:
	 mov	($pSrc), $d0
	 mov	$d0#w, ($pDst)
	 shr	\$16, $d0
	 mov	$d0#w, (+64*1)($pDst)
	 shr	\$16, $d0
	 mov	$d0#w, (+64*2)($pDst)
	 shr	\$16, $d0
	 mov	$d0#w, (+64*3)($pDst)
	 lea	8($pSrc), $pSrc
	 lea	64*4($pDst), $pDst
	 dec	$cnt
	 jnz	loop_$m
___

 $m++;
}

# macro to copy data from swizzled table to  flat space
#MACRO unswizzle	pDst, pSrc, tmp*3
sub unswizzle
{
 my ($pDst, $pSrc, $cnt, $d0, $d1)=@_;
$code.=<<___;
	 mov	\$4, $cnt
loop_$m:
	 movzxw	(+64*3+256*0)($pSrc), $d0
	 movzxw	(+64*3+256*1)($pSrc), $d1
	 shl	\$16, $d0
	 shl	\$16, $d1
	 mov	(+64*2+256*0)($pSrc), $d0#w
	 mov	(+64*2+256*1)($pSrc), $d1#w
	 shl	\$16, $d0
	 shl	\$16, $d1
	 mov	(+64*1+256*0)($pSrc), $d0#w
	 mov	(+64*1+256*1)($pSrc), $d1#w
	 shl	\$16, $d0
	 shl	\$16, $d1
	 mov	(+64*0+256*0)($pSrc), $d0#w
	 mov	(+64*0+256*1)($pSrc), $d1#w
	 mov	$d0, (+8*0)($pDst)
	 mov	$d1, (+8*1)($pDst)
	 lea	256*2($pSrc), $pSrc
	 lea	8*2($pDst), $pDst
	 sub	\$1, $cnt
	 jnz	loop_$m
___

 $m++;
}

#
# Data Structures
#

# Reduce Data
#
#
# Offset  Value
# 0C0     Carries
# 0B8     X2[10]
# 0B0     X2[9]
# 0A8     X2[8]
# 0A0     X2[7]
# 098     X2[6]
# 090     X2[5]
# 088     X2[4]
# 080     X2[3]
# 078     X2[2]
# 070     X2[1]
# 068     X2[0]
# 060     X1[12]  P[10]
# 058     X1[11]  P[9]  Z[8]
# 050     X1[10]  P[8]  Z[7]
# 048     X1[9]   P[7]  Z[6]
# 040     X1[8]   P[6]  Z[5]
# 038     X1[7]   P[5]  Z[4]
# 030     X1[6]   P[4]  Z[3]
# 028     X1[5]   P[3]  Z[2]
# 020     X1[4]   P[2]  Z[1]
# 018     X1[3]   P[1]  Z[0]
# 010     X1[2]   P[0]  Y[2]
# 008     X1[1]   Q[1]  Y[1]
# 000     X1[0]   Q[0]  Y[0]

my $X1_offset           =  0;			# 13 qwords
my $X2_offset           =  $X1_offset + 13*8;			# 11 qwords
my $Carries_offset      =  $X2_offset + 11*8;			# 1 qword
my $Q_offset            =  0;			# 2 qwords
my $P_offset            =  $Q_offset + 2*8;			# 11 qwords
my $Y_offset            =  0;			# 3 qwords
my $Z_offset            =  $Y_offset + 3*8;			# 9 qwords

my $Red_Data_Size       =  $Carries_offset + 1*8;			# (25 qwords)

#
# Stack Frame
#
#
# offset	value
# ...		<old stack contents>
# ...
# 280		Garray

# 278		tmp16[15]
# ...		...
# 200		tmp16[0]

# 1F8		tmp[7]
# ...		...
# 1C0		tmp[0]

# 1B8		GT[7]
# ...		...
# 180		GT[0]

# 178		Reduce Data
# ...		...
# 0B8		Reduce Data
# 0B0		reserved
# 0A8		reserved
# 0A0		reserved
# 098		reserved
# 090		reserved
# 088		reduce result addr
# 080		exp[8]

# ...
# 048		exp[1]
# 040		exp[0]

# 038		reserved
# 030		loop_idx
# 028		pg
# 020		i
# 018		pData	; arg 4
# 010		pG	; arg 2
# 008		pResult	; arg 1
# 000		rsp	; stack pointer before subtract

my $rsp_offset          =  0;
my $pResult_offset      =  8*1 + $rsp_offset;
my $pG_offset           =  8*1 + $pResult_offset;
my $pData_offset        =  8*1 + $pG_offset;
my $i_offset            =  8*1 + $pData_offset;
my $pg_offset           =  8*1 + $i_offset;
my $loop_idx_offset     =  8*1 + $pg_offset;
my $reserved1_offset    =  8*1 + $loop_idx_offset;
my $exp_offset          =  8*1 + $reserved1_offset;
my $red_result_addr_offset=  8*9 + $exp_offset;
my $reserved2_offset    =  8*1 + $red_result_addr_offset;
my $Reduce_Data_offset  =  8*5 + $reserved2_offset;
my $GT_offset           =  $Red_Data_Size + $Reduce_Data_offset;
my $tmp_offset          =  8*8 + $GT_offset;
my $tmp16_offset        =  8*8 + $tmp_offset;
my $garray_offset       =  8*16 + $tmp16_offset;
my $mem_size            =  8*8*32 + $garray_offset;

#
# Offsets within Reduce Data
#
#
#	struct MODF_2FOLD_MONT_512_C1_DATA {
#	UINT64 t[8][8];
#	UINT64 m[8];
#	UINT64 m1[8]; /* 2^768 % m */
#	UINT64 m2[8]; /* 2^640 % m */
#	UINT64 k1[2]; /* (- 1/m) % 2^128 */
#	};

my $T                   =  0;
my $M                   =  512;			# = 8 * 8 * 8
my $M1                  =  576;			# = 8 * 8 * 9 /* += 8 * 8 */
my $M2                  =  640;			# = 8 * 8 * 10 /* += 8 * 8 */
my $K1                  =  704;			# = 8 * 8 * 11 /* += 8 * 8 */

#
#   FUNCTIONS
#

{{{
#
# MULADD_128x512 : Function to multiply 128-bits (2 qwords) by 512-bits (8 qwords)
#                       and add 512-bits (8 qwords)
#                       to get 640 bits (10 qwords)
# Input: 128-bit mul source: [rdi+8*1], rbp
#        512-bit mul source: [rsi+8*n]
#        512-bit add source: r15, r14, ..., r9, r8
# Output: r9, r8, r15, r14, r13, r12, r11, r10, [rcx+8*1], [rcx+8*0]
# Clobbers all regs except: rcx, rsi, rdi
$code.=<<___;
.type	MULADD_128x512,\@abi-omnipotent
.align	16
MULADD_128x512:
___
	&MULSTEP_512([map("%r$_",(8..15))], "(+8*0)(%rcx)", "%rsi", "%rbp", "%rbx");
$code.=<<___;
	 mov	(+8*1)(%rdi), %rbp
___
	&MULSTEP_512([map("%r$_",(9..15,8))], "(+8*1)(%rcx)", "%rsi", "%rbp", "%rbx");
$code.=<<___;
	 ret
.size	MULADD_128x512,.-MULADD_128x512
___
}}}

{{{
#MULADD_256x512	MACRO	pDst, pA, pB, OP, TMP, X7, X6, X5, X4, X3, X2, X1, X0
#
# Inputs: pDst: Destination  (768 bits, 12 qwords)
#         pA:   Multiplicand (1024 bits, 16 qwords)
#         pB:   Multiplicand (512 bits, 8 qwords)
# Dst = Ah * B + Al
# where Ah is (in qwords) A[15:12] (256 bits) and Al is A[7:0] (512 bits)
# Results in X3 X2 X1 X0 X7 X6 X5 X4 Dst[3:0]
# Uses registers: arguments, RAX, RDX
sub MULADD_256x512
{
 my ($pDst, $pA, $pB, $OP, $TMP, $X)=@_;
$code.=<<___;
	mov	(+8*12)($pA), $OP
___
	&MULSTEP_512_ADD($X, "(+8*0)($pDst)", $pB, $pA, $OP, $TMP);
	push(@$X,shift(@$X));

$code.=<<___;
	 mov	(+8*13)($pA), $OP
___
	&MULSTEP_512($X, "(+8*1)($pDst)", $pB, $OP, $TMP);
	push(@$X,shift(@$X));

$code.=<<___;
	 mov	(+8*14)($pA), $OP
___
	&MULSTEP_512($X, "(+8*2)($pDst)", $pB, $OP, $TMP);
	push(@$X,shift(@$X));

$code.=<<___;
	 mov	(+8*15)($pA), $OP
___
	&MULSTEP_512($X, "(+8*3)($pDst)", $pB, $OP, $TMP);
	push(@$X,shift(@$X));
}

#
# mont_reduce(UINT64 *x,  /* 1024 bits, 16 qwords */
#	       UINT64 *m,  /*  512 bits,  8 qwords */
#	       MODF_2FOLD_MONT_512_C1_DATA *data,
#             UINT64 *r)  /*  512 bits,  8 qwords */
# Input:  x (number to be reduced): tmp16 (Implicit)
#         m (modulus):              [pM]  (Implicit)
#         data (reduce data):       [pData] (Implicit)
# Output: r (result):		     Address in [red_res_addr]
#         result also in: r9, r8, r15, r14, r13, r12, r11, r10

my @X=map("%r$_",(8..15));

$code.=<<___;
.type	mont_reduce,\@abi-omnipotent
.align	16
mont_reduce:
___

my $STACK_DEPTH         =  8;
	#
	# X1 = Xh * M1 + Xl
$code.=<<___;
	 lea	(+$Reduce_Data_offset+$X1_offset+$STACK_DEPTH)(%rsp), %rdi			# pX1 (Dst) 769 bits, 13 qwords
	 mov	(+$pData_offset+$STACK_DEPTH)(%rsp), %rsi			# pM1 (Bsrc) 512 bits, 8 qwords
	 add	\$$M1, %rsi
	 lea	(+$tmp16_offset+$STACK_DEPTH)(%rsp), %rcx			# X (Asrc) 1024 bits, 16 qwords

___

	&MULADD_256x512("%rdi", "%rcx", "%rsi", "%rbp", "%rbx", \@X);	# rotates @X 4 times
	# results in r11, r10, r9, r8, r15, r14, r13, r12, X1[3:0]

$code.=<<___;
	 xor	%rax, %rax
	# X1 += xl
	 add	(+8*8)(%rcx), $X[4]
	 adc	(+8*9)(%rcx), $X[5]
	 adc	(+8*10)(%rcx), $X[6]
	 adc	(+8*11)(%rcx), $X[7]
	 adc	\$0, %rax
	# X1 is now rax, r11-r8, r15-r12, tmp16[3:0]

	#
	# check for carry ;; carry stored in rax
	 mov	$X[4], (+8*8)(%rdi)			# rdi points to X1
	 mov	$X[5], (+8*9)(%rdi)
	 mov	$X[6], %rbp
	 mov	$X[7], (+8*11)(%rdi)

	 mov	%rax, (+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp)

	 mov	(+8*0)(%rdi), $X[4]
	 mov	(+8*1)(%rdi), $X[5]
	 mov	(+8*2)(%rdi), $X[6]
	 mov	(+8*3)(%rdi), $X[7]

	# X1 is now stored in: X1[11], rbp, X1[9:8], r15-r8
	# rdi -> X1
	# rsi -> M1

	#
	# X2 = Xh * M2 + Xl
	# do first part (X2 = Xh * M2)
	 add	\$8*10, %rdi			# rdi -> pXh ; 128 bits, 2 qwords
				#        Xh is actually { [rdi+8*1], rbp }
	 add	\$`$M2-$M1`, %rsi			# rsi -> M2
	 lea	(+$Reduce_Data_offset+$X2_offset+$STACK_DEPTH)(%rsp), %rcx			# rcx -> pX2 ; 641 bits, 11 qwords
___
	unshift(@X,pop(@X));	unshift(@X,pop(@X));
$code.=<<___;

	 call	MULADD_128x512			# args in rcx, rdi / rbp, rsi, r15-r8
	# result in r9, r8, r15, r14, r13, r12, r11, r10, X2[1:0]
	 mov	(+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp), %rax

	# X2 += Xl
	 add	(+8*8-8*10)(%rdi), $X[6]		# (-8*10) is to adjust rdi -> Xh to Xl
	 adc	(+8*9-8*10)(%rdi), $X[7]
	 mov	$X[6], (+8*8)(%rcx)
	 mov	$X[7], (+8*9)(%rcx)

	 adc	%rax, %rax
	 mov	%rax, (+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp)

	 lea	(+$Reduce_Data_offset+$Q_offset+$STACK_DEPTH)(%rsp), %rdi			# rdi -> pQ ; 128 bits, 2 qwords
	 add	\$`$K1-$M2`, %rsi			# rsi -> pK1 ; 128 bits, 2 qwords

	# MUL_128x128t128	rdi, rcx, rsi	; Q = X2 * K1 (bottom half)
	# B1:B0 = rsi[1:0] = K1[1:0]
	# A1:A0 = rcx[1:0] = X2[1:0]
	# Result = rdi[1],rbp = Q[1],rbp
	 mov	(%rsi), %r8			# B0
	 mov	(+8*1)(%rsi), %rbx			# B1

	 mov	(%rcx), %rax			# A0
	 mul	%r8			# B0
	 mov	%rax, %rbp
	 mov	%rdx, %r9

	 mov	(+8*1)(%rcx), %rax			# A1
	 mul	%r8			# B0
	 add	%rax, %r9

	 mov	(%rcx), %rax			# A0
	 mul	%rbx			# B1
	 add	%rax, %r9

	 mov	%r9, (+8*1)(%rdi)
	# end MUL_128x128t128

	 sub	\$`$K1-$M`, %rsi

	 mov	(%rcx), $X[6]
	 mov	(+8*1)(%rcx), $X[7]			# r9:r8 = X2[1:0]

	 call	MULADD_128x512			# args in rcx, rdi / rbp, rsi, r15-r8
	# result in r9, r8, r15, r14, r13, r12, r11, r10, X2[1:0]

	# load first half of m to rdx, rdi, rbx, rax
	# moved this here for efficiency
	 mov	(+8*0)(%rsi), %rax
	 mov	(+8*1)(%rsi), %rbx
	 mov	(+8*2)(%rsi), %rdi
	 mov	(+8*3)(%rsi), %rdx

	# continue with reduction
	 mov	(+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp), %rbp

	 add	(+8*8)(%rcx), $X[6]
	 adc	(+8*9)(%rcx), $X[7]

	#accumulate the final carry to rbp
	 adc	%rbp, %rbp

	# Add in overflow corrections: R = (X2>>128) += T[overflow]
	# R = {r9, r8, r15, r14, ..., r10}
	 shl	\$3, %rbp
	 mov	(+$pData_offset+$STACK_DEPTH)(%rsp), %rcx			# rsi -> Data (and points to T)
	 add	%rcx, %rbp			# pT ; 512 bits, 8 qwords, spread out

	# rsi will be used to generate a mask after the addition
	 xor	%rsi, %rsi

	 add	(+8*8*0)(%rbp), $X[0]
	 adc	(+8*8*1)(%rbp), $X[1]
	 adc	(+8*8*2)(%rbp), $X[2]
	 adc	(+8*8*3)(%rbp), $X[3]
	 adc	(+8*8*4)(%rbp), $X[4]
	 adc	(+8*8*5)(%rbp), $X[5]
	 adc	(+8*8*6)(%rbp), $X[6]
	 adc	(+8*8*7)(%rbp), $X[7]

	# if there is a carry:	rsi = 0xFFFFFFFFFFFFFFFF
	# if carry is clear:	rsi = 0x0000000000000000
	 sbb	\$0, %rsi

	# if carry is clear, subtract 0. Otherwise, subtract 256 bits of m
	 and	%rsi, %rax
	 and	%rsi, %rbx
	 and	%rsi, %rdi
	 and	%rsi, %rdx

	 mov	\$1, %rbp
	 sub	%rax, $X[0]
	 sbb	%rbx, $X[1]
	 sbb	%rdi, $X[2]
	 sbb	%rdx, $X[3]

	# if there is a borrow:		rbp = 0
	# if there is no borrow:	rbp = 1
	# this is used to save the borrows in between the first half and the 2nd half of the subtraction of m
	 sbb	\$0, %rbp

	#load second half of m to rdx, rdi, rbx, rax

	 add	\$$M, %rcx
	 mov	(+8*4)(%rcx), %rax
	 mov	(+8*5)(%rcx), %rbx
	 mov	(+8*6)(%rcx), %rdi
	 mov	(+8*7)(%rcx), %rdx

	# use the rsi mask as before
	# if carry is clear, subtract 0. Otherwise, subtract 256 bits of m
	 and	%rsi, %rax
	 and	%rsi, %rbx
	 and	%rsi, %rdi
	 and	%rsi, %rdx

	# if rbp = 0, there was a borrow before, it is moved to the carry flag
	# if rbp = 1, there was not a borrow before, carry flag is cleared
	 sub	\$1, %rbp

	 sbb	%rax, $X[4]
	 sbb	%rbx, $X[5]
	 sbb	%rdi, $X[6]
	 sbb	%rdx, $X[7]

	# write R back to memory

	 mov	(+$red_result_addr_offset+$STACK_DEPTH)(%rsp), %rsi
	 mov	$X[0], (+8*0)(%rsi)
	 mov	$X[1], (+8*1)(%rsi)
	 mov	$X[2], (+8*2)(%rsi)
	 mov	$X[3], (+8*3)(%rsi)
	 mov	$X[4], (+8*4)(%rsi)
	 mov	$X[5], (+8*5)(%rsi)
	 mov	$X[6], (+8*6)(%rsi)
	 mov	$X[7], (+8*7)(%rsi)

	 ret
.size	mont_reduce,.-mont_reduce
___
}}}

{{{
#MUL_512x512	MACRO	pDst, pA, pB, x7, x6, x5, x4, x3, x2, x1, x0, tmp*2
#
# Inputs: pDst: Destination  (1024 bits, 16 qwords)
#         pA:   Multiplicand (512 bits, 8 qwords)
#         pB:   Multiplicand (512 bits, 8 qwords)
# Uses registers rax, rdx, args
#   B operand in [pB] and also in x7...x0
sub MUL_512x512
{
 my ($pDst, $pA, $pB, $x, $OP, $TMP, $pDst_o)=@_;
 my ($pDst,  $pDst_o) = ($pDst =~ m/([^+]*)\+?(.*)?/);
 my @X=@$x;	# make a copy

$code.=<<___;
	 mov	(+8*0)($pA), $OP

	 mov	$X[0], %rax
	 mul	$OP			# rdx:rax = %OP * [0]
	 mov	%rax, (+$pDst_o+8*0)($pDst)
	 mov	%rdx, $X[0]
___
for(my $i=1;$i<8;$i++) {
$code.=<<___;
	 mov	$X[$i], %rax
	 mul	$OP			# rdx:rax = %OP * [$i]
	 add	%rax, $X[$i-1]
	 adc	\$0, %rdx
	 mov	%rdx, $X[$i]
___
}

for(my $i=1;$i<8;$i++) {
$code.=<<___;
	 mov	(+8*$i)($pA), $OP
___

	&MULSTEP_512(\@X, "(+$pDst_o+8*$i)($pDst)", $pB, $OP, $TMP);
	push(@X,shift(@X));
}

$code.=<<___;
	 mov	$X[0], (+$pDst_o+8*8)($pDst)
	 mov	$X[1], (+$pDst_o+8*9)($pDst)
	 mov	$X[2], (+$pDst_o+8*10)($pDst)
	 mov	$X[3], (+$pDst_o+8*11)($pDst)
	 mov	$X[4], (+$pDst_o+8*12)($pDst)
	 mov	$X[5], (+$pDst_o+8*13)($pDst)
	 mov	$X[6], (+$pDst_o+8*14)($pDst)
	 mov	$X[7], (+$pDst_o+8*15)($pDst)
___
}

#
# mont_mul_a3b : subroutine to compute (Src1 * Src2) % M (all 512-bits)
# Input:  src1: Address of source 1: rdi
#         src2: Address of source 2: rsi
# Output: dst:  Address of destination: [red_res_addr]
#    src2 and result also in: r9, r8, r15, r14, r13, r12, r11, r10
# Temp:   Clobbers [tmp16], all registers
$code.=<<___;
.type	mont_mul_a3b,\@abi-omnipotent
.align	16
mont_mul_a3b:
	#
	# multiply tmp = src1 * src2
	# For multiply: dst = rcx, src1 = rdi, src2 = rsi
	# stack depth is extra 8 from call
___
	&MUL_512x512("%rsp+$tmp16_offset+8", "%rdi", "%rsi", [map("%r$_",(10..15,8..9))], "%rbp", "%rbx");
$code.=<<___;
	#
	# Dst = tmp % m
	# Call reduce(tmp, m, data, dst)

	# tail recursion optimization: jmp to mont_reduce and return from there
	 jmp	mont_reduce
	# call	mont_reduce
	# ret
.size	mont_mul_a3b,.-mont_mul_a3b
___
}}}

{{{
#SQR_512 MACRO pDest, pA, x7, x6, x5, x4, x3, x2, x1, x0, tmp*4
#
# Input in memory [pA] and also in x7...x0
# Uses all argument registers plus rax and rdx
#
# This version computes all of the off-diagonal terms into memory,
# and then it adds in the diagonal terms

sub SQR_512
{
 my ($pDst, $pA, $x, $A, $tmp, $x7, $x6, $pDst_o)=@_;
 my ($pDst,  $pDst_o) = ($pDst =~ m/([^+]*)\+?(.*)?/);
 my @X=@$x;	# make a copy
$code.=<<___;
	# ------------------
	# first pass 01...07
	# ------------------
	 mov	$X[0], $A

	 mov	$X[1],%rax
	 mul	$A
	 mov	%rax, (+$pDst_o+8*1)($pDst)
___
for(my $i=2;$i<8;$i++) {
$code.=<<___;
	 mov	%rdx, $X[$i-2]
	 mov	$X[$i],%rax
	 mul	$A
	 add	%rax, $X[$i-2]
	 adc	\$0, %rdx
___
}
$code.=<<___;
	 mov	%rdx, $x7

	 mov	$X[0], (+$pDst_o+8*2)($pDst)

	# ------------------
	# second pass 12...17
	# ------------------

	 mov	(+8*1)($pA), $A

	 mov	(+8*2)($pA),%rax
	 mul	$A
	 add	%rax, $X[1]
	 adc	\$0, %rdx
	 mov	$X[1], (+$pDst_o+8*3)($pDst)

	 mov	%rdx, $X[0]
	 mov	(+8*3)($pA),%rax
	 mul	$A
	 add	%rax, $X[2]
	 adc	\$0, %rdx
	 add	$X[0], $X[2]
	 adc	\$0, %rdx
	 mov	$X[2], (+$pDst_o+8*4)($pDst)

	 mov	%rdx, $X[0]
	 mov	(+8*4)($pA),%rax
	 mul	$A
	 add	%rax, $X[3]
	 adc	\$0, %rdx
	 add	$X[0], $X[3]
	 adc	\$0, %rdx

	 mov	%rdx, $X[0]
	 mov	(+8*5)($pA),%rax
	 mul	$A
	 add	%rax, $X[4]
	 adc	\$0, %rdx
	 add	$X[0], $X[4]
	 adc	\$0, %rdx

	 mov	%rdx, $X[0]
	 mov	$X[6],%rax
	 mul	$A
	 add	%rax, $X[5]
	 adc	\$0, %rdx
	 add	$X[0], $X[5]
	 adc	\$0, %rdx

	 mov	%rdx, $X[0]
	 mov	$X[7],%rax
	 mul	$A
	 add	%rax, $x7
	 adc	\$0, %rdx
	 add	$X[0], $x7
	 adc	\$0, %rdx

	 mov	%rdx, $X[1]

	# ------------------
	# third pass 23...27
	# ------------------
	 mov	(+8*2)($pA), $A

	 mov	(+8*3)($pA),%rax
	 mul	$A
	 add	%rax, $X[3]
	 adc	\$0, %rdx
	 mov	$X[3], (+$pDst_o+8*5)($pDst)

	 mov	%rdx, $X[0]
	 mov	(+8*4)($pA),%rax
	 mul	$A
	 add	%rax, $X[4]
	 adc	\$0, %rdx
	 add	$X[0], $X[4]
	 adc	\$0, %rdx
	 mov	$X[4], (+$pDst_o+8*6)($pDst)

	 mov	%rdx, $X[0]
	 mov	(+8*5)($pA),%rax
	 mul	$A
	 add	%rax, $X[5]
	 adc	\$0, %rdx
	 add	$X[0], $X[5]
	 adc	\$0, %rdx

	 mov	%rdx, $X[0]
	 mov	$X[6],%rax
	 mul	$A
	 add	%rax, $x7
	 adc	\$0, %rdx
	 add	$X[0], $x7
	 adc	\$0, %rdx

	 mov	%rdx, $X[0]
	 mov	$X[7],%rax
	 mul	$A
	 add	%rax, $X[1]
	 adc	\$0, %rdx
	 add	$X[0], $X[1]
	 adc	\$0, %rdx

	 mov	%rdx, $X[2]

	# ------------------
	# fourth pass 34...37
	# ------------------

	 mov	(+8*3)($pA), $A

	 mov	(+8*4)($pA),%rax
	 mul	$A
	 add	%rax, $X[5]
	 adc	\$0, %rdx
	 mov	$X[5], (+$pDst_o+8*7)($pDst)

	 mov	%rdx, $X[0]
	 mov	(+8*5)($pA),%rax
	 mul	$A
	 add	%rax, $x7
	 adc	\$0, %rdx
	 add	$X[0], $x7
	 adc	\$0, %rdx
	 mov	$x7, (+$pDst_o+8*8)($pDst)

	 mov	%rdx, $X[0]
	 mov	$X[6],%rax
	 mul	$A
	 add	%rax, $X[1]
	 adc	\$0, %rdx
	 add	$X[0], $X[1]
	 adc	\$0, %rdx

	 mov	%rdx, $X[0]
	 mov	$X[7],%rax
	 mul	$A
	 add	%rax, $X[2]
	 adc	\$0, %rdx
	 add	$X[0], $X[2]
	 adc	\$0, %rdx

	 mov	%rdx, $X[5]

	# ------------------
	# fifth pass 45...47
	# ------------------
	 mov	(+8*4)($pA), $A

	 mov	(+8*5)($pA),%rax
	 mul	$A
	 add	%rax, $X[1]
	 adc	\$0, %rdx
	 mov	$X[1], (+$pDst_o+8*9)($pDst)

	 mov	%rdx, $X[0]
	 mov	$X[6],%rax
	 mul	$A
	 add	%rax, $X[2]
	 adc	\$0, %rdx
	 add	$X[0], $X[2]
	 adc	\$0, %rdx
	 mov	$X[2], (+$pDst_o+8*10)($pDst)

	 mov	%rdx, $X[0]
	 mov	$X[7],%rax
	 mul	$A
	 add	%rax, $X[5]
	 adc	\$0, %rdx
	 add	$X[0], $X[5]
	 adc	\$0, %rdx

	 mov	%rdx, $X[1]

	# ------------------
	# sixth pass 56...57
	# ------------------
	 mov	(+8*5)($pA), $A

	 mov	$X[6],%rax
	 mul	$A
	 add	%rax, $X[5]
	 adc	\$0, %rdx
	 mov	$X[5], (+$pDst_o+8*11)($pDst)

	 mov	%rdx, $X[0]
	 mov	$X[7],%rax
	 mul	$A
	 add	%rax, $X[1]
	 adc	\$0, %rdx
	 add	$X[0], $X[1]
	 adc	\$0, %rdx
	 mov	$X[1], (+$pDst_o+8*12)($pDst)

	 mov	%rdx, $X[2]

	# ------------------
	# seventh pass 67
	# ------------------
	 mov	$X[6], $A

	 mov	$X[7],%rax
	 mul	$A
	 add	%rax, $X[2]
	 adc	\$0, %rdx
	 mov	$X[2], (+$pDst_o+8*13)($pDst)

	 mov	%rdx, (+$pDst_o+8*14)($pDst)

	# start finalize (add	in squares, and double off-terms)
	 mov	(+$pDst_o+8*1)($pDst), $X[0]
	 mov	(+$pDst_o+8*2)($pDst), $X[1]
	 mov	(+$pDst_o+8*3)($pDst), $X[2]
	 mov	(+$pDst_o+8*4)($pDst), $X[3]
	 mov	(+$pDst_o+8*5)($pDst), $X[4]
	 mov	(+$pDst_o+8*6)($pDst), $X[5]

	 mov	(+8*3)($pA), %rax
	 mul	%rax
	 mov	%rax, $x6
	 mov	%rdx, $X[6]

	 add	$X[0], $X[0]
	 adc	$X[1], $X[1]
	 adc	$X[2], $X[2]
	 adc	$X[3], $X[3]
	 adc	$X[4], $X[4]
	 adc	$X[5], $X[5]
	 adc	\$0, $X[6]

	 mov	(+8*0)($pA), %rax
	 mul	%rax
	 mov	%rax, (+$pDst_o+8*0)($pDst)
	 mov	%rdx, $A

	 mov	(+8*1)($pA), %rax
	 mul	%rax

	 add	$A, $X[0]
	 adc	%rax, $X[1]
	 adc	\$0, %rdx

	 mov	%rdx, $A
	 mov	$X[0], (+$pDst_o+8*1)($pDst)
	 mov	$X[1], (+$pDst_o+8*2)($pDst)

	 mov	(+8*2)($pA), %rax
	 mul	%rax

	 add	$A, $X[2]
	 adc	%rax, $X[3]
	 adc	\$0, %rdx

	 mov	%rdx, $A

	 mov	$X[2], (+$pDst_o+8*3)($pDst)
	 mov	$X[3], (+$pDst_o+8*4)($pDst)

	 xor	$tmp, $tmp
	 add	$A, $X[4]
	 adc	$x6, $X[5]
	 adc	\$0, $tmp

	 mov	$X[4], (+$pDst_o+8*5)($pDst)
	 mov	$X[5], (+$pDst_o+8*6)($pDst)

	# %%tmp has 0/1 in column 7
	# %%A6 has a full value in column 7

	 mov	(+$pDst_o+8*7)($pDst), $X[0]
	 mov	(+$pDst_o+8*8)($pDst), $X[1]
	 mov	(+$pDst_o+8*9)($pDst), $X[2]
	 mov	(+$pDst_o+8*10)($pDst), $X[3]
	 mov	(+$pDst_o+8*11)($pDst), $X[4]
	 mov	(+$pDst_o+8*12)($pDst), $X[5]
	 mov	(+$pDst_o+8*13)($pDst), $x6
	 mov	(+$pDst_o+8*14)($pDst), $x7

	 mov	$X[7], %rax
	 mul	%rax
	 mov	%rax, $X[7]
	 mov	%rdx, $A

	 add	$X[0], $X[0]
	 adc	$X[1], $X[1]
	 adc	$X[2], $X[2]
	 adc	$X[3], $X[3]
	 adc	$X[4], $X[4]
	 adc	$X[5], $X[5]
	 adc	$x6, $x6
	 adc	$x7, $x7
	 adc	\$0, $A

	 add	$tmp, $X[0]

	 mov	(+8*4)($pA), %rax
	 mul	%rax

	 add	$X[6], $X[0]
	 adc	%rax, $X[1]
	 adc	\$0, %rdx

	 mov	%rdx, $tmp

	 mov	$X[0], (+$pDst_o+8*7)($pDst)
	 mov	$X[1], (+$pDst_o+8*8)($pDst)

	 mov	(+8*5)($pA), %rax
	 mul	%rax

	 add	$tmp, $X[2]
	 adc	%rax, $X[3]
	 adc	\$0, %rdx

	 mov	%rdx, $tmp

	 mov	$X[2], (+$pDst_o+8*9)($pDst)
	 mov	$X[3], (+$pDst_o+8*10)($pDst)

	 mov	(+8*6)($pA), %rax
	 mul	%rax

	 add	$tmp, $X[4]
	 adc	%rax, $X[5]
	 adc	\$0, %rdx

	 mov	$X[4], (+$pDst_o+8*11)($pDst)
	 mov	$X[5], (+$pDst_o+8*12)($pDst)

	 add	%rdx, $x6
	 adc	$X[7], $x7
	 adc	\$0, $A

	 mov	$x6, (+$pDst_o+8*13)($pDst)
	 mov	$x7, (+$pDst_o+8*14)($pDst)
	 mov	$A, (+$pDst_o+8*15)($pDst)
___
}

#
# sqr_reduce: subroutine to compute Result = reduce(Result * Result)
#
# input and result also in: r9, r8, r15, r14, r13, r12, r11, r10
#
$code.=<<___;
.type	sqr_reduce,\@abi-omnipotent
.align	16
sqr_reduce:
	 mov	(+$pResult_offset+8)(%rsp), %rcx
___
	&SQR_512("%rsp+$tmp16_offset+8", "%rcx", [map("%r$_",(10..15,8..9))], "%rbx", "%rbp", "%rsi", "%rdi");
$code.=<<___;
	# tail recursion optimization: jmp to mont_reduce and return from there
	 jmp	mont_reduce
	# call	mont_reduce
	# ret
.size	sqr_reduce,.-sqr_reduce
___
}}}

#
# MAIN FUNCTION
#

#mod_exp_512(UINT64 *result, /* 512 bits, 8 qwords */
#           UINT64 *g,   /* 512 bits, 8 qwords */
#           UINT64 *exp, /* 512 bits, 8 qwords */
#           struct mod_ctx_512 *data)

# window size = 5
# table size = 2^5 = 32
#table_entries	equ	32
#table_size	equ	table_entries * 8
$code.=<<___;
.globl	mod_exp_512
.type	mod_exp_512,\@function,4
mod_exp_512:
	 push	%rbp
	 push	%rbx
	 push	%r12
	 push	%r13
	 push	%r14
	 push	%r15

	# adjust stack down and then align it with cache boundary
	 mov	%rsp, %r8
	 sub	\$$mem_size, %rsp
	 and	\$-64, %rsp

	# store previous stack pointer and arguments
	 mov	%r8, (+$rsp_offset)(%rsp)
	 mov	%rdi, (+$pResult_offset)(%rsp)
	 mov	%rsi, (+$pG_offset)(%rsp)
	 mov	%rcx, (+$pData_offset)(%rsp)
.Lbody:
	# transform g into montgomery space
	# GT = reduce(g * C2) = reduce(g * (2^256))
	# reduce expects to have the input in [tmp16]
	 pxor	%xmm4, %xmm4
	 movdqu	(+16*0)(%rsi), %xmm0
	 movdqu	(+16*1)(%rsi), %xmm1
	 movdqu	(+16*2)(%rsi), %xmm2
	 movdqu	(+16*3)(%rsi), %xmm3
	 movdqa	%xmm4, (+$tmp16_offset+16*0)(%rsp)
	 movdqa	%xmm4, (+$tmp16_offset+16*1)(%rsp)
	 movdqa	%xmm4, (+$tmp16_offset+16*6)(%rsp)
	 movdqa	%xmm4, (+$tmp16_offset+16*7)(%rsp)
	 movdqa	%xmm0, (+$tmp16_offset+16*2)(%rsp)
	 movdqa	%xmm1, (+$tmp16_offset+16*3)(%rsp)
	 movdqa	%xmm2, (+$tmp16_offset+16*4)(%rsp)
	 movdqa	%xmm3, (+$tmp16_offset+16*5)(%rsp)

	# load pExp before rdx gets blown away
	 movdqu	(+16*0)(%rdx), %xmm0
	 movdqu	(+16*1)(%rdx), %xmm1
	 movdqu	(+16*2)(%rdx), %xmm2
	 movdqu	(+16*3)(%rdx), %xmm3

	 lea	(+$GT_offset)(%rsp), %rbx
	 mov	%rbx, (+$red_result_addr_offset)(%rsp)
	 call	mont_reduce

	# Initialize tmp = C
	 lea	(+$tmp_offset)(%rsp), %rcx
	 xor	%rax, %rax
	 mov	%rax, (+8*0)(%rcx)
	 mov	%rax, (+8*1)(%rcx)
	 mov	%rax, (+8*3)(%rcx)
	 mov	%rax, (+8*4)(%rcx)
	 mov	%rax, (+8*5)(%rcx)
	 mov	%rax, (+8*6)(%rcx)
	 mov	%rax, (+8*7)(%rcx)
	 mov	%rax, (+$exp_offset+8*8)(%rsp)
	 movq	\$1, (+8*2)(%rcx)

	 lea	(+$garray_offset)(%rsp), %rbp
	 mov	%rcx, %rsi			# pTmp
	 mov	%rbp, %rdi			# Garray[][0]
___

	&swizzle("%rdi", "%rcx", "%rax", "%rbx");

	# for (rax = 31; rax != 0; rax--) {
	#     tmp = reduce(tmp * G)
	#     swizzle(pg, tmp);
	#     pg += 2; }
$code.=<<___;
	 mov	\$31, %rax
	 mov	%rax, (+$i_offset)(%rsp)
	 mov	%rbp, (+$pg_offset)(%rsp)
	# rsi -> pTmp
	 mov	%rsi, (+$red_result_addr_offset)(%rsp)
	 mov	(+8*0)(%rsi), %r10
	 mov	(+8*1)(%rsi), %r11
	 mov	(+8*2)(%rsi), %r12
	 mov	(+8*3)(%rsi), %r13
	 mov	(+8*4)(%rsi), %r14
	 mov	(+8*5)(%rsi), %r15
	 mov	(+8*6)(%rsi), %r8
	 mov	(+8*7)(%rsi), %r9
init_loop:
	 lea	(+$GT_offset)(%rsp), %rdi
	 call	mont_mul_a3b
	 lea	(+$tmp_offset)(%rsp), %rsi
	 mov	(+$pg_offset)(%rsp), %rbp
	 add	\$2, %rbp
	 mov	%rbp, (+$pg_offset)(%rsp)
	 mov	%rsi, %rcx			# rcx = rsi = addr of tmp
___

	&swizzle("%rbp", "%rcx", "%rax", "%rbx");
$code.=<<___;
	 mov	(+$i_offset)(%rsp), %rax
	 sub	\$1, %rax
	 mov	%rax, (+$i_offset)(%rsp)
	 jne	init_loop

	#
	# Copy exponent onto stack
	 movdqa	%xmm0, (+$exp_offset+16*0)(%rsp)
	 movdqa	%xmm1, (+$exp_offset+16*1)(%rsp)
	 movdqa	%xmm2, (+$exp_offset+16*2)(%rsp)
	 movdqa	%xmm3, (+$exp_offset+16*3)(%rsp)


	#
	# Do exponentiation
	# Initialize result to G[exp{511:507}]
	 mov	(+$exp_offset+62)(%rsp), %eax
	 mov	%rax, %rdx
	 shr	\$11, %rax
	 and	\$0x07FF, %edx
	 mov	%edx, (+$exp_offset+62)(%rsp)
	 lea	(+$garray_offset)(%rsp,%rax,2), %rsi
	 mov	(+$pResult_offset)(%rsp), %rdx
___

	&unswizzle("%rdx", "%rsi", "%rbp", "%rbx", "%rax");

	#
	# Loop variables
	# rcx = [loop_idx] = index: 510-5 to 0 by 5
$code.=<<___;
	 movq	\$505, (+$loop_idx_offset)(%rsp)

	 mov	(+$pResult_offset)(%rsp), %rcx
	 mov	%rcx, (+$red_result_addr_offset)(%rsp)
	 mov	(+8*0)(%rcx), %r10
	 mov	(+8*1)(%rcx), %r11
	 mov	(+8*2)(%rcx), %r12
	 mov	(+8*3)(%rcx), %r13
	 mov	(+8*4)(%rcx), %r14
	 mov	(+8*5)(%rcx), %r15
	 mov	(+8*6)(%rcx), %r8
	 mov	(+8*7)(%rcx), %r9
	 jmp	sqr_2

main_loop_a3b:
	 call	sqr_reduce
	 call	sqr_reduce
	 call	sqr_reduce
sqr_2:
	 call	sqr_reduce
	 call	sqr_reduce

	#
	# Do multiply, first look up proper value in Garray
	 mov	(+$loop_idx_offset)(%rsp), %rcx			# bit index
	 mov	%rcx, %rax
	 shr	\$4, %rax			# rax is word pointer
	 mov	(+$exp_offset)(%rsp,%rax,2), %edx
	 and	\$15, %rcx
	 shrq	%cl, %rdx
	 and	\$0x1F, %rdx

	 lea	(+$garray_offset)(%rsp,%rdx,2), %rsi
	 lea	(+$tmp_offset)(%rsp), %rdx
	 mov	%rdx, %rdi
___

	&unswizzle("%rdx", "%rsi", "%rbp", "%rbx", "%rax");
	# rdi = tmp = pG

	#
	# Call mod_mul_a1(pDst,  pSrc1, pSrc2, pM, pData)
	#                 result result pG     M   Data
$code.=<<___;
	 mov	(+$pResult_offset)(%rsp), %rsi
	 call	mont_mul_a3b

	#
	# finish loop
	 mov	(+$loop_idx_offset)(%rsp), %rcx
	 sub	\$5, %rcx
	 mov	%rcx, (+$loop_idx_offset)(%rsp)
	 jge	main_loop_a3b

	#

end_main_loop_a3b:
	# transform result out of Montgomery space
	# result = reduce(result)
	 mov	(+$pResult_offset)(%rsp), %rdx
	 pxor	%xmm4, %xmm4
	 movdqu	(+16*0)(%rdx), %xmm0
	 movdqu	(+16*1)(%rdx), %xmm1
	 movdqu	(+16*2)(%rdx), %xmm2
	 movdqu	(+16*3)(%rdx), %xmm3
	 movdqa	%xmm4, (+$tmp16_offset+16*4)(%rsp)
	 movdqa	%xmm4, (+$tmp16_offset+16*5)(%rsp)
	 movdqa	%xmm4, (+$tmp16_offset+16*6)(%rsp)
	 movdqa	%xmm4, (+$tmp16_offset+16*7)(%rsp)
	 movdqa	%xmm0, (+$tmp16_offset+16*0)(%rsp)
	 movdqa	%xmm1, (+$tmp16_offset+16*1)(%rsp)
	 movdqa	%xmm2, (+$tmp16_offset+16*2)(%rsp)
	 movdqa	%xmm3, (+$tmp16_offset+16*3)(%rsp)
	 call	mont_reduce

	# If result > m, subract m
	# load result into r15:r8
	 mov	(+$pResult_offset)(%rsp), %rax
	 mov	(+8*0)(%rax), %r8
	 mov	(+8*1)(%rax), %r9
	 mov	(+8*2)(%rax), %r10
	 mov	(+8*3)(%rax), %r11
	 mov	(+8*4)(%rax), %r12
	 mov	(+8*5)(%rax), %r13
	 mov	(+8*6)(%rax), %r14
	 mov	(+8*7)(%rax), %r15

	# subtract m
	 mov	(+$pData_offset)(%rsp), %rbx
	 add	\$$M, %rbx

	 sub	(+8*0)(%rbx), %r8
	 sbb	(+8*1)(%rbx), %r9
	 sbb	(+8*2)(%rbx), %r10
	 sbb	(+8*3)(%rbx), %r11
	 sbb	(+8*4)(%rbx), %r12
	 sbb	(+8*5)(%rbx), %r13
	 sbb	(+8*6)(%rbx), %r14
	 sbb	(+8*7)(%rbx), %r15

	# if Carry is clear, replace result with difference
	 mov	(+8*0)(%rax), %rsi
	 mov	(+8*1)(%rax), %rdi
	 mov	(+8*2)(%rax), %rcx
	 mov	(+8*3)(%rax), %rdx
	 cmovnc	%r8, %rsi
	 cmovnc	%r9, %rdi
	 cmovnc	%r10, %rcx
	 cmovnc	%r11, %rdx
	 mov	%rsi, (+8*0)(%rax)
	 mov	%rdi, (+8*1)(%rax)
	 mov	%rcx, (+8*2)(%rax)
	 mov	%rdx, (+8*3)(%rax)

	 mov	(+8*4)(%rax), %rsi
	 mov	(+8*5)(%rax), %rdi
	 mov	(+8*6)(%rax), %rcx
	 mov	(+8*7)(%rax), %rdx
	 cmovnc	%r12, %rsi
	 cmovnc	%r13, %rdi
	 cmovnc	%r14, %rcx
	 cmovnc	%r15, %rdx
	 mov	%rsi, (+8*4)(%rax)
	 mov	%rdi, (+8*5)(%rax)
	 mov	%rcx, (+8*6)(%rax)
	 mov	%rdx, (+8*7)(%rax)

	 mov	(+$rsp_offset)(%rsp), %rsi
	 mov	0(%rsi),%r15
	 mov	8(%rsi),%r14
	 mov	16(%rsi),%r13
	 mov	24(%rsi),%r12
	 mov	32(%rsi),%rbx
	 mov	40(%rsi),%rbp
	 lea	48(%rsi),%rsp
.Lepilogue:
	 ret
.size mod_exp_512, . - mod_exp_512
___

if ($win64) {
# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
#		CONTEXT *context,DISPATCHER_CONTEXT *disp)
my $rec="%rcx";
my $frame="%rdx";
my $context="%r8";
my $disp="%r9";

$code.=<<___;
.extern	__imp_RtlVirtualUnwind
.type	mod_exp_512_se_handler,\@abi-omnipotent
.align	16
mod_exp_512_se_handler:
	push	%rsi
	push	%rdi
	push	%rbx
	push	%rbp
	push	%r12
	push	%r13
	push	%r14
	push	%r15
	pushfq
	sub	\$64,%rsp

	mov	120($context),%rax	# pull context->Rax
	mov	248($context),%rbx	# pull context->Rip

	lea	.Lbody(%rip),%r10
	cmp	%r10,%rbx		# context->Rip<prologue label
	jb	.Lin_prologue

	mov	152($context),%rax	# pull context->Rsp

	lea	.Lepilogue(%rip),%r10
	cmp	%r10,%rbx		# context->Rip>=epilogue label
	jae	.Lin_prologue

	mov	$rsp_offset(%rax),%rax	# pull saved Rsp

	mov	32(%rax),%rbx
	mov	40(%rax),%rbp
	mov	24(%rax),%r12
	mov	16(%rax),%r13
	mov	8(%rax),%r14
	mov	0(%rax),%r15
	lea	48(%rax),%rax
	mov	%rbx,144($context)	# restore context->Rbx
	mov	%rbp,160($context)	# restore context->Rbp
	mov	%r12,216($context)	# restore context->R12
	mov	%r13,224($context)	# restore context->R13
	mov	%r14,232($context)	# restore context->R14
	mov	%r15,240($context)	# restore context->R15

.Lin_prologue:
	mov	8(%rax),%rdi
	mov	16(%rax),%rsi
	mov	%rax,152($context)	# restore context->Rsp
	mov	%rsi,168($context)	# restore context->Rsi
	mov	%rdi,176($context)	# restore context->Rdi

	mov	40($disp),%rdi		# disp->ContextRecord
	mov	$context,%rsi		# context
	mov	\$154,%ecx		# sizeof(CONTEXT)
	.long	0xa548f3fc		# cld; rep movsq

	mov	$disp,%rsi
	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
	mov	0(%rsi),%r8		# arg3, disp->ControlPc
	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
	mov	40(%rsi),%r10		# disp->ContextRecord
	lea	56(%rsi),%r11		# &disp->HandlerData
	lea	24(%rsi),%r12		# &disp->EstablisherFrame
	mov	%r10,32(%rsp)		# arg5
	mov	%r11,40(%rsp)		# arg6
	mov	%r12,48(%rsp)		# arg7
	mov	%rcx,56(%rsp)		# arg8, (NULL)
	call	*__imp_RtlVirtualUnwind(%rip)

	mov	\$1,%eax		# ExceptionContinueSearch
	add	\$64,%rsp
	popfq
	pop	%r15
	pop	%r14
	pop	%r13
	pop	%r12
	pop	%rbp
	pop	%rbx
	pop	%rdi
	pop	%rsi
	ret
.size	mod_exp_512_se_handler,.-mod_exp_512_se_handler

.section	.pdata
.align	4
	.rva	.LSEH_begin_mod_exp_512
	.rva	.LSEH_end_mod_exp_512
	.rva	.LSEH_info_mod_exp_512

.section	.xdata
.align	8
.LSEH_info_mod_exp_512:
	.byte	9,0,0,0
	.rva	mod_exp_512_se_handler
___
}

sub reg_part {
my ($reg,$conv)=@_;
    if ($reg =~ /%r[0-9]+/)	{ $reg .= $conv; }
    elsif ($conv eq "b")	{ $reg =~ s/%[er]([^x]+)x?/%$1l/;	}
    elsif ($conv eq "w")	{ $reg =~ s/%[er](.+)/%$1/;		}
    elsif ($conv eq "d")	{ $reg =~ s/%[er](.+)/%e$1/;		}
    return $reg;
}

$code =~ s/(%[a-z0-9]+)#([bwd])/reg_part($1,$2)/gem;
$code =~ s/\`([^\`]*)\`/eval $1/gem;
$code =~ s/(\(\+[^)]+\))/eval $1/gem;
print $code;
close STDOUT;