-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtinyecm.c
More file actions
4311 lines (3768 loc) · 153 KB
/
tinyecm.c
File metadata and controls
4311 lines (3768 loc) · 153 KB
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
/*
Copyright (c) 2020, Ben Buhrow
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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS 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 COPYRIGHT OWNER OR 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.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
/* portions of this file use the following license: */
/*
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
* Copyright (C) 2018 Tilman Neumann (www.tilman-neumann.de)
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*/
/*
Ported to C and released 7/31/19
Ben Buhrow
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "stdint.h"
#include <math.h>
#define D 120
#ifndef BITS
#define BITS 64
#endif
#if BITS==64
// full strength mul/sqr redc
__inline uint64_t mulredcx(uint64_t x, uint64_t y, uint64_t n, uint64_t nhat)
{
if (n & 0x8000000000000000)
{
__asm__(
"mulx %2, %%r10, %%r11 \n\t"
"movq %%r10, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"xorq %%r12, %%r12 \n\t"
"mulq %3 \n\t"
"mulq %4 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
"cmovae %4, %%r12 \n\t"
"subq %4, %%rdx \n\t"
"cmovc %%r12, %%r8 \n\t"
"addq %%r8, %%rdx \n\t"
: "=&d"(x)
: "0"(x), "r"(y), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "r12", "cc");
}
else
{
__asm__(
"mulx %2, %%r10, %%r11 \n\t"
"movq %3, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"mulq %%r10 \n\t"
"mulq %4 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
"subq %4, %%rdx \n\t"
"cmovc %4, %%r8 \n\t"
"addq %%r8, %%rdx \n\t"
: "=d"(x)
: "0"(x), "r"(y), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "cc");
}
return x;
}
__inline void mulredcx2(uint64_t x, uint64_t y, uint64_t x2, uint64_t y2,
uint64_t *r1, uint64_t *r2, uint64_t n, uint64_t nhat)
{
// attempt to do two independent mulredc faster than individually.
// mostly by using mulx/adcx/adox to process two carry chains
// separately.
// so far, it is very slightly worse. Perhaps because it
// uses more registers and has more instructions (an extra
// movq/subq to negate n and movq at the end), and more function
// arguments.
// also there is no reciprocal throughput advantage to using
// independent adc/ox vs. serial addq/adcq: they are all latency 1.
if (n & 0x8000000000000000)
{
__asm__(
"mulx %3, %%r10, %%r11 \n\t"
"movq %4, %%rdx \n\t"
"movq $0, %%rbx \n\t"
"mulx %5, %%r12, %%r13 \n\t"
"movq %6, %%rdx \n\t"
"mulx %%r10, %%rax, %%r8 \n\t"
"mulx %%r12, %%rdx, %%r15 \n\t"
"subq %7, %%rbx \n\t"
"mulx %7, %%r14, %%r15 \n\t"
"mulq %7 \n\t"
"xorq %%r8, %%r8 \n\t"
"xorq %1, %1 \n\t"
"adcx %%r12, %%r14 \n\t"
"adox %%r10, %%rax \n\t"
"adcx %%r13, %%r15 \n\t"
"adox %%r11, %%rdx \n\t"
"cmovnc %7, %%r8 \n\t"
"cmovno %7, %1 \n\t"
"xorq %%rax, %%rax \n\t"
"xorq %%r14, %%r14 \n\t"
"adcx %%rbx, %%r15 \n\t"
"adox %%rbx, %%rdx \n\t"
"cmovnc %%r8, %%rax \n\t"
"cmovno %1, %%r14 \n\t"
"addq %%r14, %%rdx \n\t"
"addq %%rax, %%r15 \n\t"
"movq %%r15, %1\n\t"
: "=d"(x), "=r"(x2)
: "0"(x), "r"(y), "1"(x2), "r"(y2), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "r12", "r13", "r14", "r15", "rbx", "cc");
}
else
{
__asm__(
"mulx %3, %%r10, %%r11 \n\t"
"movq %4, %%rdx \n\t"
"movq $0, %%rbx \n\t"
"mulx %5, %%r12, %%r13 \n\t"
"movq %6, %%rdx \n\t"
"mulx %%r10, %%rax, %%r8 \n\t"
"mulx %%r12, %%rdx, %%r15 \n\t"
"subq %7, %%rbx \n\t"
"mulx %7, %%r14, %%r15 \n\t"
"mulq %7 \n\t"
"xorq %%r8, %%r8 \n\t"
"adcx %%r12, %%r14 \n\t"
"adox %%r10, %%rax \n\t"
"adcx %%r13, %%r15 \n\t"
"adox %%r11, %%rdx \n\t"
"xorq %%r14, %%r14 \n\t"
"adcx %%rbx, %%r15 \n\t"
"adox %%rbx, %%rdx \n\t"
"cmovnc %7, %%r8 \n\t"
"cmovno %7, %%r14 \n\t"
"addq %%r14, %%rdx \n\t"
"addq %%r8, %%r15 \n\t"
"movq %%r15, %1\n\t"
: "=d"(x), "=r"(x2)
: "0"(x), "r"(y), "1"(x2), "r"(y2), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "r12", "r13", "r14", "r15", "rbx", "cc");
}
*r1 = x;
*r2 = x2;
return;
}
__inline uint64_t sqrredcx(uint64_t x, uint64_t n, uint64_t nhat)
{
if (n & 0x8000000000000000)
{
__asm__(
"mulx %1, %%r10, %%r11 \n\t"
"movq %%r10, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"xorq %%r12, %%r12 \n\t"
"mulq %2 \n\t"
"mulq %3 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
"cmovae %3, %%r12 \n\t"
"subq %3, %%rdx \n\t"
"cmovc %%r12, %%r8 \n\t"
"addq %%r8, %%rdx \n\t"
: "=&d"(x)
: "0"(x), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "r12", "cc");
}
else
{
__asm__(
"mulx %1, %%r10, %%r11 \n\t"
"movq %2, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"mulq %%r10 \n\t"
"mulq %3 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
"subq %3, %%rdx \n\t"
"cmovc %3, %%r8 \n\t"
"addq %%r8, %%rdx \n\t"
: "=d"(x)
: "0"(x), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "cc");
}
return x;
}
__inline void sqrredcx2(uint64_t x, uint64_t x2,
uint64_t *r1, uint64_t *r2, uint64_t n, uint64_t nhat)
{
// attempt to do two independent mulredc faster than individually.
// mostly by using mulx/adcx/adox to process two carry chains
// separately.
// so far, it is very slightly worse. Perhaps because it does
// use many more registers and has more instructions (an extra
// movq/subq to negate n, movq at the end, and more function
// arguments.
// we get reciprocal throughput advantage on skyx on the cmov's,
// the xor's, and the final adds
// maybe we need to do more of a staggered approach where we
// do the first muls, then when doing the adcx/adcx start the
// second set of muls
if (n & 0x8000000000000000)
{
__asm__(
"mulx %2, %%r10, %%r11 \n\t"
"movq %3, %%rdx \n\t"
"movq $0, %%rbx \n\t"
"mulx %3, %%r12, %%r13 \n\t"
"movq %4, %%rdx \n\t"
"mulx %%r12, %%r14, %%r15 \n\t"
"mulx %%r10, %%rax, %%r8 \n\t" /* output rax ready for mulq */
"movq %%r14, %%rdx \n\t"
"subq %5, %%rbx \n\t"
"mulx %5, %%r14, %%r15 \n\t" /* mulq must go 2nd because it commits rdx */
"mulq %5 \n\t"
"xorq %%r8, %%r8 \n\t"
"xorq %1, %1 \n\t"
"adcx %%r12, %%r14 \n\t"
"adox %%r10, %%rax \n\t"
"adcx %%r13, %%r15 \n\t"
"adox %%r11, %%rdx \n\t"
"cmovnc %5, %%r8 \n\t"
"cmovno %5, %1 \n\t"
"xorq %%rax, %%rax \n\t"
"xorq %%r14, %%r14 \n\t"
"adcx %%rbx, %%r15 \n\t"
"adox %%rbx, %%rdx \n\t"
"cmovnc %%r8, %%rax \n\t"
"cmovno %1, %%r14 \n\t"
"addq %%r14, %%rdx \n\t"
"addq %%rax, %%r15 \n\t"
"movq %%r15, %1\n\t"
: "=d"(x), "=r"(x2)
: "0"(x), "1"(x2), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "r12", "r13", "r14", "r15", "rbx", "cc");
}
else
{
__asm__(
"mulx %2, %%r10, %%r11 \n\t"
"movq %3, %%rdx \n\t"
"movq $0, %%rbx \n\t"
"mulx %3, %%r12, %%r13 \n\t"
"movq %4, %%rdx \n\t"
"mulx %%r12, %%r14, %%r15 \n\t"
"mulx %%r10, %%rax, %%r8 \n\t" /* output rax ready for mulq */
"movq %%r14, %%rdx \n\t"
"subq %5, %%rbx \n\t"
"mulx %5, %%r14, %%r15 \n\t" /* mulq must go 2nd because it commits rdx */
"mulq %5 \n\t"
"xorq %%r8, %%r8 \n\t"
"adcx %%r12, %%r14 \n\t"
"adox %%r10, %%rax \n\t"
"adcx %%r13, %%r15 \n\t"
"adox %%r11, %%rdx \n\t"
"xorq %%r14, %%r14 \n\t"
"adcx %%rbx, %%r15 \n\t"
"adox %%rbx, %%rdx \n\t"
"cmovnc %5, %%r8 \n\t"
"cmovno %5, %%r14 \n\t"
"addq %%r14, %%rdx \n\t"
"addq %%r8, %%r15 \n\t"
"movq %%r15, %1\n\t"
: "=d"(x), "=r"(x2)
: "0"(x), "1"(x2), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "r12", "r13", "r14", "r15", "rbx", "cc");
}
*r1 = x;
*r2 = x2;
return;
}
#elif BITS >= 62
// don't have to check for addmod overflow at the end
__inline uint64_t mulredcx(uint64_t x, uint64_t y, uint64_t n, uint64_t nhat)
{
__asm__(
"mulx %2, %%r10, %%r11 \n\t"
"movq %3, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"mulq %%r10 \n\t"
"mulq %4 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
"subq %4, %%rdx \n\t"
"cmovc %4, %%r8 \n\t"
"addq %%r8, %%rdx \n\t"
: "=d"(x)
: "0"(x), "r"(y), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "cc");
return x;
}
__inline void mulredcx2(uint64_t x, uint64_t y, uint64_t x2, uint64_t y2,
uint64_t *r1, uint64_t *r2, uint64_t n, uint64_t nhat)
{
__asm__(
"mulx %3, %%r10, %%r11 \n\t"
"movq %4, %%rdx \n\t"
"mulx %5, %%r12, %%r13 \n\t"
"movq %6, %%rdx \n\t"
"mulx %%r10, %%rax, %%r8 \n\t"
"mulx %%r12, %%rdx, %%r15 \n\t"
"mulx %7, %%r14, %%r15 \n\t"
"mulq %7 \n\t"
"movq $0, %%rbx \n\t"
"subq %7, %%rbx \n\t"
"xorq %%r8, %%r8 \n\t"
"adcx %%r12, %%r14 \n\t"
"adcx %%r13, %%r15 \n\t"
"adox %%r10, %%rax \n\t"
"adox %%r11, %%rdx \n\t"
"xorq %%r14, %%r14 \n\t"
"adcx %%rbx, %%r15 \n\t"
"adox %%rbx, %%rdx \n\t"
"cmovnc %7, %%r8 \n\t"
"cmovno %7, %%r14 \n\t"
"addq %%r14, %%rdx \n\t"
"addq %%r8, %%r15 \n\t"
"movq %%r15, %1\n\t"
: "=d"(x), "=r"(x2)
: "0"(x), "r"(y), "1"(x2), "r"(y2), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "r12", "r13", "r14", "r15", "rbx", "cc");
*r1 = x;
*r2 = x2;
return;
}
__inline uint64_t sqrredcx(uint64_t x, uint64_t n, uint64_t nhat)
{
__asm__(
"mulx %1, %%r10, %%r11 \n\t"
"movq %2, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"mulq %%r10 \n\t"
"mulq %3 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
"subq %3, %%rdx \n\t"
"cmovc %3, %%r8 \n\t"
"addq %%r8, %%rdx \n\t"
: "=d"(x)
: "0"(x), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "cc");
return x;
}
#else
// can be more lazy and not do final (mod n) at all.
// average curve count goes up a bit, but still faster overall.
__inline uint64_t mulredcx(uint64_t x, uint64_t y, uint64_t n, uint64_t nhat)
{
__asm__(
"mulx %2, %%r10, %%r11 \n\t"
"movq %3, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"mulq %%r10 \n\t"
"mulq %4 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
: "=d"(x)
: "0"(x), "r"(y), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "cc");
return x;
}
__inline uint64_t sqrredcx(uint64_t x, uint64_t n, uint64_t nhat)
{
__asm__(
"mulx %1, %%r10, %%r11 \n\t"
"movq %2, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"mulq %%r10 \n\t"
"mulq %3 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
: "=d"(x)
: "0"(x), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "cc");
return x;
}
#endif
__inline uint64_t mulredc63x(uint64_t x, uint64_t y, uint64_t n, uint64_t nhat)
{
__asm__(
"mulx %2, %%r10, %%r11 \n\t"
"movq %3, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"mulq %%r10 \n\t"
"mulq %4 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
"subq %4, %%rdx \n\t"
"cmovc %4, %%r8 \n\t"
"addq %%r8, %%rdx \n\t"
: "=d"(x)
: "0"(x), "r"(y), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "cc");
return x;
}
__inline uint64_t sqrredc63x(uint64_t x, uint64_t n, uint64_t nhat)
{
__asm__(
"mulx %1, %%r10, %%r11 \n\t"
"movq %2, %%rax \n\t"
"xorq %%r8, %%r8 \n\t"
"mulq %%r10 \n\t"
"mulq %3 \n\t"
"addq %%r10, %%rax \n\t"
"adcq %%r11, %%rdx \n\t"
"subq %3, %%rdx \n\t"
"cmovc %3, %%r8 \n\t"
"addq %%r8, %%rdx \n\t"
: "=d"(x)
: "0"(x), "r"(nhat), "r"(n)
: "rax", "r8", "r10", "r11", "cc");
return x;
}
//#define DEBUG 1
typedef struct
{
uint64_t X;
uint64_t Z;
} uecm_pt;
typedef struct
{
uint64_t sum1;
uint64_t diff1;
uint64_t sum2;
uint64_t diff2;
uint64_t tt1;
uint64_t tt2;
uint64_t tt3;
uint64_t tt4;
uint64_t tt5;
uint64_t s;
uint64_t n;
uecm_pt pt1;
uecm_pt pt2;
uecm_pt pt3;
uecm_pt pt4;
uecm_pt pt5;
uint32_t sigma;
uecm_pt Pa;
uecm_pt Pd;
uecm_pt Pad;
uecm_pt Pb[60];
uint64_t Paprod;
uint64_t Pbprod[60];
uint64_t stg2acc;
uint32_t stg1Add;
uint32_t stg1Doub;
uint32_t paired;
uint32_t ptadds;
uint64_t numprimes;
uint64_t A;
uint32_t last_pid;
uint32_t amin;
uint32_t stg1_max;
uint32_t stg2_max;
} uecm_work;
static const uint32_t map[61] = {
0, 1, 2, 0, 0, 0, 0, 3, 0, 0,
0, 4, 0, 5, 0, 0, 0, 6, 0, 7,
0, 0, 0, 8, 0, 0, 0, 0, 0, 9,
0, 10, 0, 0, 0, 0, 0, 11, 0, 0,
0, 12, 0, 13, 0, 0, 0, 14, 0, 15,
0, 0, 0, 16, 0, 0, 0, 0, 0, 17,
18 };
#define NUMP 801
static const uint32_t primes[NUMP] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223, 227, 229, 233,
239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
293, 307, 311, 313, 317, 331, 337, 347, 349, 353,
359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
421, 431, 433, 439, 443, 449, 457, 461, 463, 467,
479, 487, 491, 499, 503, 509, 521, 523, 541, 547,
557, 563, 569, 571, 577, 587, 593, 599, 601, 607,
613, 617, 619, 631, 641, 643, 647, 653, 659, 661,
673, 677, 683, 691, 701, 709, 719, 727, 733, 739,
743, 751, 757, 761, 769, 773, 787, 797, 809, 811,
821, 823, 827, 829, 839, 853, 857, 859, 863, 877,
881, 883, 887, 907, 911, 919, 929, 937, 941, 947,
953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019,
1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087,
1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153,
1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229,
1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297,
1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381,
1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453,
1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523,
1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597,
1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663,
1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741,
1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823,
1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901,
1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993,
1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063,
2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131,
2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221,
2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293,
2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371,
2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437,
2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539,
2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621,
2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689,
2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749,
2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833,
2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909,
2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001,
3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083,
3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187,
3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259,
3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343,
3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433,
3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517,
3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581,
3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659,
3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733,
3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823,
3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911,
3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001,
4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073,
4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153,
4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241,
4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327,
4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421,
4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507,
4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591,
4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663,
4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759,
4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861,
4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943,
4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009,
5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099,
5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189,
5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281,
5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393,
5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449,
5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527,
5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641,
5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701,
5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801,
5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861,
5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953,
5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067,
6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143,
};
// local functions
void uadd(uint64_t rho, uecm_work *work, uecm_pt *P1, uecm_pt *p2,
uecm_pt *Pin, uecm_pt *Pout);
void udup(uint64_t rho, uecm_work *work, uint64_t insum, uint64_t indiff, uecm_pt *P);
void uprac(uint64_t rho, uecm_work *work, uecm_pt *P, uint64_t c, double v);
int ucheck_factor(uint64_t Z, uint64_t n, uint64_t * f);
void ubuild(uecm_pt *P, uint64_t rho, uecm_work *work, uint32_t sigma);
void uecm_stage1(uint64_t rho, uecm_work *work, uecm_pt *P);
void uecm_stage2(uecm_pt *P, uint64_t rho, uecm_work *work);
void uecm_stage2_D60(uecm_pt* P, uint64_t rho, uecm_work* work);
void uecm_stage2_D120(uecm_pt *P, uint64_t rho, uecm_work *work);
void uecm_stage2_D120_inv(uecm_pt *P, uint64_t rho, uecm_work *work);
void uecm_stage2_w30u2(uecm_pt* P, uint64_t rho, uecm_work* work);
void uecm_stage2_w60u1(uecm_pt* P, uint64_t rho, uecm_work* work);
__inline void addsubmod(uint64_t x, uint64_t y, uint64_t *s, uint64_t *d, uint64_t n)
{
// attempt to do simultaneous add/sub faster than individually.
// so far, it is very slightly worse.
*d = 0 - y;
*s = x;
__asm__(
"xorq %%r10, %%r10 \n\t" /* zero r10 */
"xorq %%r8, %%r8 \n\t" /* zero r8 and clear CF and OF */
"adcx %3, %1\n\t" /* d = x - y, set CF */
"adox %4, %0\n\t" /* s = x + y, set OF */
"cmovnc %2, %%r8\n\t" /* sub overflow ? n : 0 */
"cmovo %2, %%r10\n\t" /* add overflow ? n : 0 */
"addq %%r8, %1\n\t" /* x = x - y mod n */
"subq %%r10, %0\n\t" /* y = y + x mod n */
:"+r" (*s), "+r" (*d)
: "r"(n), "r"(x), "r"(y)
: "r8", "r10", "cc"
);
return;
}
__inline uint64_t submod(uint64_t a, uint64_t b, uint64_t n)
{
__asm__(
"xorq %%r8, %%r8 \n\t"
"subq %1, %0 \n\t"
"cmovc %2, %%r8 \n\t"
"addq %%r8, %0 \n\t"
: "+r"(a)
: "r"(b), "r"(n)
: "r8", "cc");
return a;
}
#if BITS==64
__inline uint64_t addmod(uint64_t x, uint64_t y, uint64_t n)
{
uint64_t t = x - n;
x += y;
__asm__("add %2, %1\n\t"
"cmovc %1, %0\n\t"
:"+r" (x), "+&r" (t)
: "r" (y)
: "cc"
);
return x;
}
#else
// this works as long as inputs <= 62 bits so that the adds don't overflow.
// it is ok to have the result larger than n.
#define addmod(x, y, n) ((x) + (y))
#endif
__inline uint64_t u64div(uint64_t c, uint64_t n)
{
__asm__("divq %4"
: "=a"(c), "=d"(n)
: "1"(c), "0"(0), "r"(n));
return n;
}
uint64_t spDivide(uint64_t *q, uint64_t *r, uint64_t u[2], uint64_t v)
{
*r = u[1];
*q = u[0];
__asm__("divq %4"
: "=a"(*q), "=d"(*r)
: "1"(*r), "0"(*q), "r"(v));
return 0;
}
void spMultiply(uint64_t u, uint64_t v, uint64_t *product, uint64_t *carry)
{
*product = v;
*carry = u;
__asm__("movq %2, %%rax \n\t"
"mulq %3 \n\t"
"movq %%rax, %0 \n\t"
"movq %%rdx, %1 \n\t"
: "=r"(*product), "=r"(*carry)
: "1"(*carry), "0"(*product)
: "rax", "rdx", "cc");
return;
}
void spMulMod(uint64_t u, uint64_t v, uint64_t m, uint64_t *w)
{
uint64_t p[2];
uint64_t q;
spMultiply(u, v, &p[0], &p[1]);
spDivide(&q, w, p, m);
return;
}
uint64_t spGCD(uint64_t x, uint64_t y)
{
uint64_t a, b, c;
a = x; b = y;
while (b != 0)
{
c = a % b;
a = b;
b = c;
}
return a;
}
// quick and easy RNG
uint64_t LCGSTATE;
uint32_t spRand(uint32_t lower, uint32_t upper)
{
// advance the state of the LCG and return the appropriate result
LCGSTATE = 6364136223846793005ULL * LCGSTATE + 1442695040888963407ULL;
return lower + (uint32_t)(
(double)(upper - lower) * (double)(LCGSTATE >> 32) / 4294967296.0);
}
#define INV_2_POW_64 5.4210108624275221700372640043497e-20
uint64_t spRand64(uint64_t lower, uint64_t upper)
{
// advance the state of the LCG and return the appropriate result
LCGSTATE = 6364136223846793005ULL * LCGSTATE + 1442695040888963407ULL;
return lower + (uint64_t)(
(double)(upper - lower) * ((double)(LCGSTATE) * INV_2_POW_64));
}
void uadd(uint64_t rho, uecm_work *work, uecm_pt *P1, uecm_pt *P2,
uecm_pt *Pin, uecm_pt *Pout)
{
// compute:
//x+ = z- * [(x1-z1)(x2+z2) + (x1+z1)(x2-z2)]^2
//z+ = x- * [(x1-z1)(x2+z2) - (x1+z1)(x2-z2)]^2
// where:
//x- = original x
//z- = original z
// given the sums and differences of the original points (stored in work structure).
work->diff1 = submod(P1->X, P1->Z, work->n);
work->sum1 = addmod(P1->X, P1->Z, work->n);
work->diff2 = submod(P2->X, P2->Z, work->n);
work->sum2 = addmod(P2->X, P2->Z, work->n);
//addsubmod(P1->X, P1->Z, &work->sum1, &work->diff1, work->n);
//addsubmod(P2->X, P2->Z, &work->sum2, &work->diff2, work->n);
work->tt1 = mulredcx(work->diff1, work->sum2, work->n, rho); //U
work->tt2 = mulredcx(work->sum1, work->diff2, work->n, rho); //V
//mulredcx2(work->diff1, work->sum2, work->sum1, work->diff2,
// &work->tt1, &work->tt2, work->n, rho);
work->tt3 = addmod(work->tt1, work->tt2, work->n);
work->tt4 = submod(work->tt1, work->tt2, work->n);
//addsubmod(work->tt1, work->tt2, &work->tt3, &work->tt4, work->n);
work->tt1 = sqrredcx(work->tt3, work->n, rho); //(U + V)^2
work->tt2 = sqrredcx(work->tt4, work->n, rho); //(U - V)^2
//sqrredcx2(work->tt3, work->tt4,
// &work->tt1, &work->tt2, work->n, rho);
if (Pin == Pout)
{
uint64_t tmp;
Pout->Z = mulredcx(work->tt1, Pin->Z, work->n, rho); //Z * (U + V)^2
Pout->X = mulredcx(work->tt2, Pin->X, work->n, rho); //x * (U - V)^2
//mulredcx2(work->tt1, Pin->Z, work->tt2, Pin->X,
// &Pout->Z, &Pout->X, work->n, rho);
tmp = Pout->Z;
Pout->Z = Pout->X;
Pout->X = tmp;
}
else
{
Pout->X = mulredcx(work->tt1, Pin->Z, work->n, rho); //Z * (U + V)^2
Pout->Z = mulredcx(work->tt2, Pin->X, work->n, rho); //x * (U - V)^2
//mulredcx2(work->tt1, Pin->Z, work->tt2, Pin->X,
// &Pout->X, &Pout->Z, work->n, rho);
}
work->stg1Add++;
return;
}
void udup(uint64_t rho, uecm_work *work,
uint64_t insum, uint64_t indiff, uecm_pt *P)
{
work->tt1 = sqrredcx(indiff, work->n, rho); // U=(x1 - z1)^2
work->tt2 = sqrredcx(insum, work->n, rho); // V=(x1 + z1)^2
//sqrredcx2(indiff, insum,
// &work->tt1, &work->tt2, work->n, rho);
P->X = mulredcx(work->tt1, work->tt2, work->n, rho); // x=U*V
work->tt3 = submod(work->tt2, work->tt1, work->n); // w = V-U
work->tt2 = mulredcx(work->tt3, work->s, work->n, rho); // w = (A+2)/4 * w
work->tt2 = addmod(work->tt2, work->tt1, work->n); // w = w + U
P->Z = mulredcx(work->tt2, work->tt3, work->n, rho); // Z = w*(V-U)
work->stg1Doub++;
return;
}
//#define PRACOPT
//#define TRACEPRAC
#ifdef TRACEPRAC
#include "gmp.h"
#endif
void uprac70(uint64_t rho, uecm_work *work, uecm_pt *P)
{
uint64_t s1, s2, d1, d2;
uint64_t swp1, swp2;
int i;
//static const uint8_t steps[116] = {
// 0,6,0,6,0,6,0,4,6,0,4,6,0,4,4,6,
// 0,4,4,6,0,5,4,6,0,3,3,4,6,0,3,5,
// 4,6,0,3,4,3,4,6,0,5,5,4,6,0,5,3,
// 3,4,6,0,3,3,4,3,4,6,0,5,3,3,3,3,
// 3,3,3,3,4,3,3,4,6,0,5,4,3,3,4,6,
// 0,3,4,3,5,4,6,0,5,3,3,3,4,6,0,5,
// 4,3,5,4,6,0,5,5,3,3,4,6,0,4,3,3,
// 3,5,4,6};
// case 6 is always followed by 0 except for the very last one.
static const uint8_t steps[95] = {
0,6,6,6,4,6,4,6,4,4,6,4,4,6,5,4,
6,3,3,4,6,3,5,4,6,3,4,3,4,6,5,5,
4,6,5,3,3,4,6,3,3,4,3,4,6,5,3,3,
3,3,3,3,3,3,4,3,3,4,6,5,4,3,3,4,
6,3,4,3,5,4,6,5,3,3,3,4,6,5,4,3,
5,4,6,5,5,3,3,4,6,4,3,3,3,5,4};
#ifdef TRACEPRAC
mpz_t A, B, C, T, S, p;
mpz_init(A);
mpz_init(B);
mpz_init(C);
mpz_init(T);
mpz_init(S);
mpz_init(p);
mpz_set_ui(A, 1);
mpz_set_ui(B, 1);
mpz_set_ui(C, 1);
mpz_set_ui(T, 1);
mpz_set_ui(S, 1);
mpz_set_ui(p, 1);
#endif
for (i = 0; i < 95; i++)
{
if (steps[i] == 0)
{
work->pt1.X = work->pt2.X = work->pt3.X = P->X;
work->pt1.Z = work->pt2.Z = work->pt3.Z = P->Z;
d1 = submod(work->pt1.X, work->pt1.Z, work->n);
s1 = addmod(work->pt1.X, work->pt1.Z, work->n);
udup(rho, work, s1, d1, &work->pt1);
#ifdef TRACEPRAC
gmp_printf("A = B = C = P = %Zd\n", p);
mpz_add(A, A, A);
gmp_printf("A = 2 * A (%Zd)\n", A);
#endif
}
else if (steps[i] == 3)
{
// integrate step 4 followed by swap(1,2)
uadd(rho, work, &work->pt2, &work->pt1, &work->pt3, &work->pt4); // T = B + A (C)
#ifdef TRACEPRAC
mpz_add(T, B, A);
gmp_printf("T = B + A (%Zd)\n", T);
mpz_set(S, A);
mpz_set(A, T);
mpz_set(T, C);
mpz_set(C, B);
mpz_set(B, S);
gmp_printf("S = A (%Zd)\n", S);
gmp_printf("A = T (%Zd)\n", A);
gmp_printf("T = C (%Zd)\n", T);
gmp_printf("C = B (%Zd)\n", C);
gmp_printf("B = S (%Zd)\n", B);
#endif
swp1 = work->pt1.X;
swp2 = work->pt1.Z;
work->pt1.X = work->pt4.X;
work->pt1.Z = work->pt4.Z;
work->pt4.X = work->pt3.X;
work->pt4.Z = work->pt3.Z;
work->pt3.X = work->pt2.X;
work->pt3.Z = work->pt2.Z;
work->pt2.X = swp1;
work->pt2.Z = swp2;
}
else if (steps[i] == 4)
{
uadd(rho, work, &work->pt2, &work->pt1, &work->pt3, &work->pt4); // T = B + A (C)
#ifdef TRACEPRAC
mpz_add(T, B, A);
gmp_printf("T = B + A (%Zd)\n", T);
mpz_set(S, B);
mpz_set(B, T);
mpz_set(T, C);
mpz_set(C, S);
gmp_printf("S = B (%Zd)\n", S);
gmp_printf("B = T (%Zd)\n", B);
gmp_printf("T = C (%Zd)\n", T);
gmp_printf("C = S (%Zd)\n", S);
#endif
swp1 = work->pt2.X;
swp2 = work->pt2.Z;
work->pt2.X = work->pt4.X;
work->pt2.Z = work->pt4.Z;
work->pt4.X = work->pt3.X;
work->pt4.Z = work->pt3.Z;
work->pt3.X = swp1;
work->pt3.Z = swp2;
}
else if (steps[i] == 5)
{
d2 = submod(work->pt1.X, work->pt1.Z, work->n);
s2 = addmod(work->pt1.X, work->pt1.Z, work->n);
uadd(rho, work, &work->pt2, &work->pt1, &work->pt3, &work->pt2); // B = B + A (C)
udup(rho, work, s2, d2, &work->pt1); // A = 2A
#ifdef TRACEPRAC
mpz_add(B, B, A);
mpz_add(A, A, A);
gmp_printf("B = B + A (%Zd)\n", B);
gmp_printf("A = 2 * A (%Zd)\n", A);
#endif
}
else if (steps[i] == 6)
{
uadd(rho, work, &work->pt1, &work->pt2, &work->pt3, P); // A = A + B (C)
work->pt1.X = work->pt2.X = work->pt3.X = P->X;
work->pt1.Z = work->pt2.Z = work->pt3.Z = P->Z;
d1 = submod(work->pt1.X, work->pt1.Z, work->n);
s1 = addmod(work->pt1.X, work->pt1.Z, work->n);
udup(rho, work, s1, d1, &work->pt1);
#ifdef TRACEPRAC
mpz_add(p, A, B);
mpz_set(A, p);