forked from sethtroisi/gmp-ecm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecm.c
1644 lines (1470 loc) · 51.9 KB
/
ecm.c
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
/* Elliptic Curve Method: toplevel and stage 1 routines.
Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
2012, 2016 Paul Zimmermann, Alexander Kruppa, Cyril Bouvier, David Cleaver.
This file is part of the ECM Library.
The ECM Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
The ECM Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the ECM Library; see the file COPYING.LIB. If not, see
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ecm-impl.h"
#include "getprime_r.h"
#include <math.h>
#ifdef HAVE_ADDLAWS
#include "addlaws.h"
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#else
# define ULONG_MAX __GMP_ULONG_MAX
#endif
#ifdef TIMING_CRT
extern int mpzspv_from_mpzv_slow_time, mpzspv_to_mpzv_time,
mpzspv_normalise_time;
#endif
/* the following factor takes into account the smaller expected smoothness
for Montgomery's curves (batch mode) with respect to Suyama's curves */
/* For param 1 we use A=4d-2 with d a square (see main.c). In that
case, Cyril Bouvier and Razvan Barbulescu have shown that the average
expected torsion is that of a generic Suyama curve multiplied by the
constant 2^(1/3)/(3*3^(1/128)) */
#define EXTRA_SMOOTHNESS_SQUARE 0.416384512396064
/* For A=4d-2 (param 3) for d a random integer, the average expected torsion
is that of a generic Suyama curve multiplied by the constant
1/(3*3^(1/128)) */
#define EXTRA_SMOOTHNESS_32BITS_D 0.330484606500389
/******************************************************************************
* *
* Elliptic Curve Method *
* *
******************************************************************************/
void duplicate (mpres_t, mpres_t, mpres_t, mpres_t, mpmod_t, mpres_t,
mpres_t, mpres_t, mpres_t) ATTRIBUTE_HOT;
void add3 (mpres_t, mpres_t, mpres_t, mpres_t, mpres_t, mpres_t, mpres_t,
mpres_t, mpmod_t, mpres_t, mpres_t, mpres_t) ATTRIBUTE_HOT;
#define mpz_mulmod5(r,s1,s2,m,t) { mpz_mul(t,s1,s2); mpz_mod(r, t, m); }
/* switch from Montgomery's form g*y^2 = x^3 + a*x^2 + x
to Weierstrass' form Y^2 = X^3 + A*X + B
by change of variables x -> g*X-a/3, y -> g*Y.
We have A = (3-a^2)/(3g^2), X = (3x+a)/(3g), Y = y/g.
If a factor is found during the modular inverse, returns
ECM_FACTOR_FOUND_STEP1 and the factor in f, otherwise returns
ECM_NO_FACTOR_FOUND.
The input value of y is the y0 value in the initial equation:
g*y0^2 = x0^3 + a*x0^2 + x0.
*/
int
montgomery_to_weierstrass (mpz_t f, mpres_t x, mpres_t y, mpres_t A, mpmod_t n)
{
mpres_t g;
mpres_init (g, n);
mpres_add (g, x, A, n);
mpres_mul (g, g, x, n);
mpres_add_ui (g, g, 1, n);
mpres_mul (g, g, x, n); /* g = x^3+a*x^2+x (y=1) */
mpres_mul_ui (y, g, 3, n);
mpres_mul (y, y, g, n); /* y = 3g^2 */
if (!mpres_invert (y, y, n)) /* y = 1/(3g^2) temporarily */
{
mpres_gcd (f, y, n);
mpres_clear (g, n);
return ECM_FACTOR_FOUND_STEP1;
}
/* update x */
mpres_mul_ui (x, x, 3, n); /* 3x */
mpres_add (x, x, A, n); /* 3x+a */
mpres_mul (x, x, g, n); /* (3x+a)*g */
mpres_mul (x, x, y, n); /* (3x+a)/(3g) */
/* update A */
mpres_sqr (A, A, n); /* a^2 */
mpres_sub_ui (A, A, 3, n);
mpres_neg (A, A, n); /* 3-a^2 */
mpres_mul (A, A, y, n); /* (3-a^2)/(3g^2) */
/* update y */
mpres_mul_ui (g, g, 3, n); /* 3g */
mpres_mul (y, y, g, n); /* (3g)/(3g^2) = 1/g */
mpres_clear (g, n);
return ECM_NO_FACTOR_FOUND;
}
/* adds Q=(x2:z2) and R=(x1:z1) and puts the result in (x3:z3),
using 6 muls (4 muls and 2 squares), and 6 add/sub.
One assumes that Q-R=P or R-Q=P where P=(x:z).
- n : number to factor
- u, v, w : auxiliary variables
Modifies: x3, z3, u, v, w.
(x3,z3) may be identical to (x2,z2) and to (x,z)
*/
void
add3 (mpres_t x3, mpres_t z3, mpres_t x2, mpres_t z2, mpres_t x1, mpres_t z1,
mpres_t x, mpres_t z, mpmod_t n, mpres_t u, mpres_t v, mpres_t w)
{
mpres_sub (u, x2, z2, n);
mpres_add (v, x1, z1, n); /* u = x2-z2, v = x1+z1 */
mpres_mul (u, u, v, n); /* u = (x2-z2)*(x1+z1) */
mpres_add (w, x2, z2, n);
mpres_sub (v, x1, z1, n); /* w = x2+z2, v = x1-z1 */
mpres_mul (v, w, v, n); /* v = (x2+z2)*(x1-z1) */
mpres_add (w, u, v, n); /* w = 2*(x1*x2-z1*z2) */
mpres_sub (v, u, v, n); /* v = 2*(x2*z1-x1*z2) */
mpres_sqr (w, w, n); /* w = 4*(x1*x2-z1*z2)^2 */
mpres_sqr (v, v, n); /* v = 4*(x2*z1-x1*z2)^2 */
if (x == x3) /* same variable: in-place variant */
{
/* x3 <- w * z mod n
z3 <- x * v mod n */
mpres_mul (z3, w, z, n);
mpres_mul (x3, x, v, n);
mpres_swap (x3, z3, n);
}
else
{
mpres_mul (x3, w, z, n); /* x3 = 4*z*(x1*x2-z1*z2)^2 mod n */
mpres_mul (z3, x, v, n); /* z3 = 4*x*(x2*z1-x1*z2)^2 mod n */
}
/* mul += 6; */
}
/* computes 2P=(x2:z2) from P=(x1:z1), with 5 muls (3 muls and 2 squares)
and 4 add/sub.
- n : number to factor
- b : (a+2)/4 mod n
- t, u, v, w : auxiliary variables
*/
void
duplicate (mpres_t x2, mpres_t z2, mpres_t x1, mpres_t z1, mpmod_t n,
mpres_t b, mpres_t u, mpres_t v, mpres_t w)
{
mpres_add (u, x1, z1, n);
mpres_sqr (u, u, n); /* u = (x1+z1)^2 mod n */
mpres_sub (v, x1, z1, n);
mpres_sqr (v, v, n); /* v = (x1-z1)^2 mod n */
mpres_mul (x2, u, v, n); /* x2 = u*v = (x1^2 - z1^2)^2 mod n */
mpres_sub (w, u, v, n); /* w = u-v = 4*x1*z1 */
mpres_mul (u, w, b, n); /* u = w*b = ((A+2)/4*(4*x1*z1)) mod n */
mpres_add (u, u, v, n); /* u = (x1-z1)^2+(A+2)/4*(4*x1*z1) */
mpres_mul (z2, w, u, n); /* z2 = ((4*x1*z1)*((x1-z1)^2+(A+2)/4*(4*x1*z1))) mod n */
}
/* multiply P=(x:z) by e and puts the result in (x:z). */
void
ecm_mul (mpres_t x, mpres_t z, mpz_t e, mpmod_t n, mpres_t b)
{
size_t l;
int negated = 0;
mpres_t x0, z0, x1, z1, u, v, w;
/* In Montgomery coordinates, the point at infinity is (0::0) */
if (mpz_sgn (e) == 0)
{
mpz_set_ui (x, 0);
mpz_set_ui (z, 0);
return;
}
/* The negative of a point (x:y:z) is (x:-y:u). Since we do not compute
y, e*(x::z) == (-e)*(x::z). */
if (mpz_sgn (e) < 0)
{
negated = 1;
mpz_neg (e, e);
}
if (mpz_cmp_ui (e, 1) == 0)
goto ecm_mul_end;
mpres_init (x0, n);
mpres_init (z0, n);
mpres_init (x1, n);
mpres_init (z1, n);
mpres_init (u, n);
mpres_init (v, n);
mpres_init (w, n);
l = mpz_sizeinbase (e, 2) - 1; /* l >= 1 */
mpres_set (x0, x, n);
mpres_set (z0, z, n);
duplicate (x1, z1, x0, z0, n, b, u, v, w);
/* invariant: (P1,P0) = ((k+1)P, kP) where k = floor(e/2^l) */
while (l-- > 0)
{
if (ecm_tstbit (e, l)) /* k, k+1 -> 2k+1, 2k+2 */
{
add3 (x0, z0, x0, z0, x1, z1, x, z, n, u, v, w); /* 2k+1 */
duplicate (x1, z1, x1, z1, n, b, u, v, w); /* 2k+2 */
}
else /* k, k+1 -> 2k, 2k+1 */
{
add3 (x1, z1, x1, z1, x0, z0, x, z, n, u, v, w); /* 2k+1 */
duplicate (x0, z0, x0, z0, n, b, u, v, w); /* 2k */
}
}
mpres_set (x, x0, n);
mpres_set (z, z0, n);
mpres_clear (x0, n);
mpres_clear (z0, n);
mpres_clear (x1, n);
mpres_clear (z1, n);
mpres_clear (u, n);
mpres_clear (v, n);
mpres_clear (w, n);
ecm_mul_end:
/* Undo negation to avoid changing the caller's e value */
if (negated)
mpz_neg (e, e);
}
#define ADD 6.0 /* number of multiplications in an addition */
#define DUP 5.0 /* number of multiplications in a duplicate */
/* returns the number of modular multiplications for computing
V_n from V_r * V_{n-r} - V_{n-2r}.
ADD is the cost of an addition
DUP is the cost of a duplicate
*/
static double
lucas_cost (ecm_uint n, double v)
{
ecm_uint d, e, r;
double c; /* cost */
d = n;
r = (ecm_uint) ((double) d * v + 0.5);
if (r >= n)
return (ADD * (double) n);
d = n - r;
e = 2 * r - n;
c = DUP + ADD; /* initial duplicate and final addition */
while (d != e)
{
if (d < e)
{
r = d;
d = e;
e = r;
}
if (d - e <= e / 4 && ((d + e) % 3) == 0)
{ /* condition 1 */
d = (2 * d - e) / 3;
e = (e - d) / 2;
c += 3.0 * ADD; /* 3 additions */
}
else if (d - e <= e / 4 && (d - e) % 6 == 0)
{ /* condition 2 */
d = (d - e) / 2;
c += ADD + DUP; /* one addition, one duplicate */
}
else if ((d + 3) / 4 <= e)
{ /* condition 3 */
d -= e;
c += ADD; /* one addition */
}
else if ((d + e) % 2 == 0)
{ /* condition 4 */
d = (d - e) / 2;
c += ADD + DUP; /* one addition, one duplicate */
}
/* now d+e is odd */
else if (d % 2 == 0)
{ /* condition 5 */
d /= 2;
c += ADD + DUP; /* one addition, one duplicate */
}
/* now d is odd and e is even */
else if (d % 3 == 0)
{ /* condition 6 */
d = d / 3 - e;
c += 3.0 * ADD + DUP; /* three additions, one duplicate */
}
else if ((d + e) % 3 == 0)
{ /* condition 7 */
d = (d - 2 * e) / 3;
c += 3.0 * ADD + DUP; /* three additions, one duplicate */
}
else if ((d - e) % 3 == 0)
{ /* condition 8 */
d = (d - e) / 3;
c += 3.0 * ADD + DUP; /* three additions, one duplicate */
}
else /* necessarily e is even: catches all cases */
{ /* condition 9 */
e /= 2;
c += ADD + DUP; /* one addition, one duplicate */
}
}
return c;
}
/* computes kP from P=(xA:zA) and puts the result in (xA:zA). Assumes k>2.
WARNING! The calls to add3() assume that the two input points are distinct,
which is not neccessarily satisfied. The result can be that in rare cases
the point at infinity (z==0) results when it shouldn't. A test case is
echo 33554520197234177 | ./ecm -sigma 2046841451 373 1
which finds the prime even though it shouldn't (23^2=529 divides order).
This is not a problem for ECM since at worst we'll find a factor we
shouldn't have found. For other purposes (i.e. primality proving) this
would have to be fixed first.
*/
static void
prac (mpres_t xA, mpres_t zA, ecm_uint k, mpmod_t n, mpres_t b,
mpres_t u, mpres_t v, mpres_t w, mpres_t xB, mpres_t zB, mpres_t xC,
mpres_t zC, mpres_t xT, mpres_t zT, mpres_t xT2, mpres_t zT2)
{
ecm_uint d, e, r, i = 0, nv;
double c, cmin;
__mpz_struct *tmp;
#define NV 10
/* 1/val[0] = the golden ratio (1+sqrt(5))/2, and 1/val[i] for i>0
is the real number whose continued fraction expansion is all 1s
except for a 2 in i+1-st place */
static double val[NV] =
{ 0.61803398874989485, 0.72360679774997897, 0.58017872829546410,
0.63283980608870629, 0.61242994950949500, 0.62018198080741576,
0.61721461653440386, 0.61834711965622806, 0.61791440652881789,
0.61807966846989581};
/* for small n, it makes no sense to try 10 different Lucas chains */
nv = mpz_size ((mpz_ptr) n);
if (nv > NV)
nv = NV;
if (nv > 1)
{
/* chooses the best value of v */
for (d = 0, cmin = ADD * (double) k; d < nv; d++)
{
c = lucas_cost (k, val[d]);
if (c < cmin)
{
cmin = c;
i = d;
}
}
}
d = k;
r = (ecm_uint) ((double) d * val[i] + 0.5);
/* first iteration always begins by Condition 3, then a swap */
d = k - r;
e = 2 * r - k;
mpres_set (xB, xA, n);
mpres_set (zB, zA, n); /* B=A */
mpres_set (xC, xA, n);
mpres_set (zC, zA, n); /* C=A */
duplicate (xA, zA, xA, zA, n, b, u, v, w); /* A = 2*A */
while (d != e)
{
if (d < e)
{
r = d;
d = e;
e = r;
mpres_swap (xA, xB, n);
mpres_swap (zA, zB, n);
}
/* do the first line of Table 4 whose condition qualifies */
if (d - e <= e / 4 && ((d + e) % 3) == 0)
{ /* condition 1 */
d = (2 * d - e) / 3;
e = (e - d) / 2;
add3 (xT, zT, xA, zA, xB, zB, xC, zC, n, u, v, w); /* T = f(A,B,C) */
add3 (xT2, zT2, xT, zT, xA, zA, xB, zB, n, u, v, w); /* T2 = f(T,A,B) */
add3 (xB, zB, xB, zB, xT, zT, xA, zA, n, u, v, w); /* B = f(B,T,A) */
mpres_swap (xA, xT2, n);
mpres_swap (zA, zT2, n); /* swap A and T2 */
}
else if (d - e <= e / 4 && (d - e) % 6 == 0)
{ /* condition 2 */
d = (d - e) / 2;
add3 (xB, zB, xA, zA, xB, zB, xC, zC, n, u, v, w); /* B = f(A,B,C) */
duplicate (xA, zA, xA, zA, n, b, u, v, w); /* A = 2*A */
}
else if ((d + 3) / 4 <= e)
{ /* condition 3 */
d -= e;
add3 (xT, zT, xB, zB, xA, zA, xC, zC, n, u, v, w); /* T = f(B,A,C) */
/* circular permutation (B,T,C) */
tmp = xB;
xB = xT;
xT = xC;
xC = tmp;
tmp = zB;
zB = zT;
zT = zC;
zC = tmp;
}
else if ((d + e) % 2 == 0)
{ /* condition 4 */
d = (d - e) / 2;
add3 (xB, zB, xB, zB, xA, zA, xC, zC, n, u, v, w); /* B = f(B,A,C) */
duplicate (xA, zA, xA, zA, n, b, u, v, w); /* A = 2*A */
}
/* now d+e is odd */
else if (d % 2 == 0)
{ /* condition 5 */
d /= 2;
add3 (xC, zC, xC, zC, xA, zA, xB, zB, n, u, v, w); /* C = f(C,A,B) */
duplicate (xA, zA, xA, zA, n, b, u, v, w); /* A = 2*A */
}
/* now d is odd, e is even */
else if (d % 3 == 0)
{ /* condition 6 */
d = d / 3 - e;
duplicate (xT, zT, xA, zA, n, b, u, v, w); /* T = 2*A */
add3 (xT2, zT2, xA, zA, xB, zB, xC, zC, n, u, v, w); /* T2 = f(A,B,C) */
add3 (xA, zA, xT, zT, xA, zA, xA, zA, n, u, v, w); /* A = f(T,A,A) */
add3 (xT, zT, xT, zT, xT2, zT2, xC, zC, n, u, v, w); /* T = f(T,T2,C) */
/* circular permutation (C,B,T) */
tmp = xC;
xC = xB;
xB = xT;
xT = tmp;
tmp = zC;
zC = zB;
zB = zT;
zT = tmp;
}
else if ((d + e) % 3 == 0)
{ /* condition 7 */
d = (d - 2 * e) / 3;
add3 (xT, zT, xA, zA, xB, zB, xC, zC, n, u, v, w); /* T = f(A,B,C) */
add3 (xB, zB, xT, zT, xA, zA, xB, zB, n, u, v, w); /* B = f(T,A,B) */
duplicate (xT, zT, xA, zA, n, b, u, v, w);
add3 (xA, zA, xA, zA, xT, zT, xA, zA, n, u, v, w); /* A = 3*A */
}
else if ((d - e) % 3 == 0)
{ /* condition 8 */
d = (d - e) / 3;
add3 (xT, zT, xA, zA, xB, zB, xC, zC, n, u, v, w); /* T = f(A,B,C) */
add3 (xC, zC, xC, zC, xA, zA, xB, zB, n, u, v, w); /* C = f(A,C,B) */
mpres_swap (xB, xT, n);
mpres_swap (zB, zT, n); /* swap B and T */
duplicate (xT, zT, xA, zA, n, b, u, v, w);
add3 (xA, zA, xA, zA, xT, zT, xA, zA, n, u, v, w); /* A = 3*A */
}
else /* necessarily e is even here */
{ /* condition 9 */
e /= 2;
add3 (xC, zC, xC, zC, xB, zB, xA, zA, n, u, v, w); /* C = f(C,B,A) */
duplicate (xB, zB, xB, zB, n, b, u, v, w); /* B = 2*B */
}
}
add3 (xA, zA, xA, zA, xB, zB, xC, zC, n, u, v, w);
ASSERT(d == 1);
}
/* Input: x is initial point
A is curve parameter in Montgomery's form:
g*y^2*z = x^3 + a*x^2*z + x*z^2
n is the number to factor
B1 is the stage 1 bound
Output: If a factor is found, it is returned in f.
Otherwise, x contains the x-coordinate of the point computed
in stage 1 (with z coordinate normalized to 1).
B1done is set to B1 if stage 1 completed normally,
or to the largest prime processed if interrupted, but never
to a smaller value than B1done was upon function entry.
Return value: ECM_FACTOR_FOUND_STEP1 if a factor, otherwise
ECM_NO_FACTOR_FOUND
*/
static int
ecm_stage1 (mpz_t f, mpres_t x, mpres_t A, mpmod_t n, double B1,
double *B1done, mpz_t go, int (*stop_asap)(void),
char *chkfilename)
{
mpres_t b, z, u, v, w, xB, zB, xC, zC, xT, zT, xT2, zT2;
uint64_t p, r, last_chkpnt_p;
int ret = ECM_NO_FACTOR_FOUND;
long last_chkpnt_time;
prime_info_t prime_info;
prime_info_init (prime_info);
mpres_init (b, n);
mpres_init (z, n);
mpres_init (u, n);
mpres_init (v, n);
mpres_init (w, n);
mpres_init (xB, n);
mpres_init (zB, n);
mpres_init (xC, n);
mpres_init (zC, n);
mpres_init (xT, n);
mpres_init (zT, n);
mpres_init (xT2, n);
mpres_init (zT2, n);
last_chkpnt_time = cputime ();
mpres_set_ui (z, 1, n);
mpres_add_ui (b, A, 2, n);
mpres_div_2exp (b, b, 2, n); /* b == (A0+2)*B/4 */
/* preload group order */
if (go != NULL)
ecm_mul (x, z, go, n, b);
/* prac() wants multiplicands > 2 */
for (r = 2; r <= B1; r *= 2)
if (r > *B1done)
duplicate (x, z, x, z, n, b, u, v, w);
/* We'll do 3 manually, too (that's what ecm4 did..) */
for (r = 3; r <= B1; r *= 3)
if (r > *B1done)
{
duplicate (xB, zB, x, z, n, b, u, v, w);
add3 (x, z, x, z, xB, zB, x, z, n, u, v, w);
}
last_chkpnt_p = 3;
p = getprime_mt (prime_info); /* Puts 3 into p. Next call gives 5 */
for (p = getprime_mt (prime_info); p <= B1; p = getprime_mt (prime_info))
{
for (r = p; r <= B1; r *= p)
if (r > *B1done)
prac (x, z, (ecm_uint) p, n, b, u, v, w, xB, zB, xC, zC, xT,
zT, xT2, zT2);
if (mpres_is_zero (z, n))
{
outputf (OUTPUT_VERBOSE, "Reached point at infinity, %.0f divides "
"group orders\n", p);
break;
}
if (stop_asap != NULL && (*stop_asap) ())
{
outputf (OUTPUT_NORMAL, "Interrupted at prime %.0f\n", p);
break;
}
if (chkfilename != NULL && p > last_chkpnt_p + 10000 &&
elltime (last_chkpnt_time, cputime ()) > CHKPNT_PERIOD)
{
writechkfile (chkfilename, ECM_ECM, MAX(p, *B1done), n, A, x, NULL, z);
last_chkpnt_p = p;
last_chkpnt_time = cputime ();
}
}
/* If stage 1 finished normally, p is the smallest prime >B1 here.
In that case, set to B1 */
if (p > B1)
p = B1;
if (p > *B1done)
*B1done = p;
if (chkfilename != NULL)
writechkfile (chkfilename, ECM_ECM, *B1done, n, A, x, NULL, z);
prime_info_clear (prime_info);
if (!mpres_invert (u, z, n)) /* Factor found? */
{
mpres_gcd (f, z, n);
ret = ECM_FACTOR_FOUND_STEP1;
}
mpres_mul (x, x, u, n);
mpres_clear (zT2, n);
mpres_clear (xT2, n);
mpres_clear (zT, n);
mpres_clear (xT, n);
mpres_clear (zC, n);
mpres_clear (xC, n);
mpres_clear (zB, n);
mpres_clear (xB, n);
mpres_clear (w, n);
mpres_clear (v, n);
mpres_clear (u, n);
mpres_clear (z, n);
mpres_clear (b, n);
return ret;
}
#define DEBUG_EC_W 0
#ifdef HAVE_ADDLAWS
/* Input: when Etype == ECM_EC_TYPE_WEIERSTRASS*:
(x, y) is initial point
A is curve parameter in Weierstrass's form:
Y^2 = X^3 + A*X + B, where B = y^2-(x^3+A*x) is implicit
when Etype == ECM_EC_TYPE_TWISTED_HESSIAN:
(x, y) is initial point
A=a/d is curve parameter in Hessian form: a*X^3+Y^3+Z^3=d*X*Y*Z
n is the number to factor
B1 is the stage 1 bound
batch_s = prod(p^e <= B1) if != 1
Output: If a factor is found, it is returned in f.
Otherwise, (x, y) contains the point computed in stage 1.
B1done is set to B1 if stage 1 completed normally,
or to the largest prime processed if interrupted, but never
to a smaller value than B1done was upon function entry.
Return value: ECM_FACTOR_FOUND_STEP1 if a factor is found, otherwise
ECM_NO_FACTOR_FOUND
*/
static int
ecm_stage1_W (mpz_t f, ell_curve_t E, ell_point_t P, mpmod_t n,
double B1, double *B1done, mpz_t batch_s, mpz_t go,
int (*stop_asap)(void), char *chkfilename)
{
mpres_t xB;
ell_point_t Q;
uint64_t p = 0, r, last_chkpnt_p;
int ret = ECM_NO_FACTOR_FOUND, status;
long last_chkpnt_time;
prime_info_t prime_info;
prime_info_init (prime_info);
mpres_init (xB, n);
ell_point_init(Q, E, n);
last_chkpnt_time = cputime ();
#if DEBUG_EC_W >= 2
gmp_printf("N:=%Zd;\n", n->orig_modulus);
printf("E:="); ell_curve_print(E, n);
printf("E:=[E[4], E[5]];\n");
printf("P:="); ell_point_print(P, E, n); printf("; Q:=P;\n");
#endif
/* preload group order */
if (go != NULL){
if (ell_point_mul (f, Q, go, P, E, n) == 0){
ret = ECM_FACTOR_FOUND_STEP1;
goto end_of_stage1_w;
}
ell_point_set(P, Q, E, n);
}
#if DEBUG_EC_W >= 1
gmp_printf("go:=%Zd;\n", go);
printf("goP:="); ell_point_print(P, E, n); printf(";\n");
#endif
if(mpz_cmp_ui(batch_s, 1) == 0){
outputf (OUTPUT_VERBOSE, "Using traditional approach to Step 1\n");
for (r = 2; r <= B1; r *= 2)
if (r > *B1done){
if(ell_point_duplicate (f, Q, P, E, n) == 0){
ret = ECM_FACTOR_FOUND_STEP1;
goto end_of_stage1_w;
}
ell_point_set(P, Q, E, n);
#if DEBUG_EC_W >= 2
printf("P%ld:=", (long)r); ell_point_print(P, E, n); printf(";\n");
printf("Q:=EcmMult(2, Q, E, N);\n");
printf("(Q[1]*P%ld[3]-Q[3]*P%ld[1]) mod N;\n",(long)r,(long)r);
#endif
}
last_chkpnt_p = 3;
for (p = getprime_mt (prime_info); p <= B1; p = getprime_mt (prime_info)){
for (r = p; r <= B1; r *= p){
if (r > *B1done){
mpz_set_ui(f, (ecm_uint)p);
status = ell_point_mul (f, Q, f, P, E, n);
if(status == 0){
}
else if(E->law == ECM_LAW_HOMOGENEOUS){
if(E->type == ECM_EC_TYPE_TWISTED_HESSIAN)
mpres_gcd(f, Q->x, n);
else
mpres_gcd(f, Q->z, n);
// gmp_printf("gcd=%Zd\n", f);
if(mpz_cmp(f, n->orig_modulus) < 0
&& mpz_cmp_ui(f, 1) > 0)
status = 0;
}
if(status == 0){
ret = ECM_FACTOR_FOUND_STEP1;
goto end_of_stage1_w;
}
#if DEBUG_EC_W >= 2
printf("R%ld:=", (long)r); ell_point_print(Q, E, n);
printf(";\nQ:=EcmMult(%ld, Q, E, N);\n", (long)p);
printf("(Q[1]*R%ld[3]-Q[3]*R%ld[1]) mod N;\n",(long)r,(long)r);
#endif
ell_point_set(P, Q, E, n);
}
}
if (ell_point_is_zero (P, E, n)){
outputf (OUTPUT_VERBOSE, "Reached point at infinity, "
"%.0f divides group orders\n", p);
break;
}
if (stop_asap != NULL && (*stop_asap) ()){
outputf (OUTPUT_NORMAL, "Interrupted at prime %.0f\n", p);
break;
}
if (chkfilename != NULL && p > last_chkpnt_p + 10000 &&
elltime (last_chkpnt_time, cputime ()) > CHKPNT_PERIOD){
writechkfile (chkfilename, ECM_ECM, MAX(p, *B1done),
n, E->a4, P->x, P->y, P->z);
last_chkpnt_p = p;
last_chkpnt_time = cputime ();
}
}
}
else{
#if USE_ADD_SUB_CHAINS == 0 /* keeping it simple */
if (ell_point_mul (f, Q, batch_s, P, E, n) == 0){
ret = ECM_FACTOR_FOUND_STEP1;
goto end_of_stage1_w;
}
#else
/* batch mode and special coding... */
short *S = NULL;
size_t iS;
int w;
add_sub_unpack(&w, &S, &iS, batch_s);
if (ell_point_mul_add_sub_with_S(f, Q, P, E, n, w, S, iS) == 0){
ret = ECM_FACTOR_FOUND_STEP1;
}
#endif
ell_point_set(P, Q, E, n);
p = B1;
}
end_of_stage1_w:
/* If stage 1 finished normally, p is the smallest prime > B1 here.
In that case, set to B1 */
if (p > B1)
p = B1;
if (p > *B1done)
*B1done = p;
if (chkfilename != NULL)
writechkfile (chkfilename, ECM_ECM, *B1done, n, E->a4, P->x, P->y,P->z);
prime_info_clear (prime_info);
#if DEBUG_EC_W >= 2
printf("lastP="); ell_point_print(P, E, n); printf("\n");
#endif
if(ret != ECM_FACTOR_FOUND_STEP1){
if(ell_point_is_zero(P, E, n) == 1){
/* too bad */
ell_point_set_to_zero(P, E, n);
mpz_set(f, n->orig_modulus);
ret = ECM_FACTOR_FOUND_STEP1;
}
else{
/* for affine case, z = 1 anyway */
if(E->law == ECM_LAW_HOMOGENEOUS){
if (!mpres_invert (xB, P->z, n)){ /* Factor found? */
mpres_gcd (f, P->z, n);
gmp_printf("# factor found during normalization: %Zd\n", f);
ret = ECM_FACTOR_FOUND_STEP1;
}
else{
/* normalize to get (x:y:1) valid in W or H form... */
#if DEBUG_EC_W >= 2
mpres_get_z(f, xB, n); gmp_printf("1/z=%Zd\n", f);
#endif
mpres_mul (P->x, P->x, xB, n);
mpres_mul (P->y, P->y, xB, n);
#if DEBUG_EC_W >= 2
mpres_get_z(f, P->x, n); gmp_printf("x/z=%Zd\n", f);
mpres_get_z(f, P->y, n); gmp_printf("y/z=%Zd\n", f);
#endif
mpres_set_ui (P->z, 1, n);
}
}
}
}
mpres_clear (xB, n);
ell_point_clear(Q, E, n);
return ret;
}
#endif
/* choose "optimal" S according to step 2 range B2 */
int
choose_S (mpz_t B2len)
{
if (mpz_cmp_d (B2len, 1e7) < 0)
return 1; /* x^1 */
else if (mpz_cmp_d (B2len, 1e8) < 0)
return 2; /* x^2 */
else if (mpz_cmp_d (B2len, 1e9) < 0)
return -3; /* Dickson(3) */
else if (mpz_cmp_d (B2len, 1e10) < 0)
return -6; /* Dickson(6) */
else if (mpz_cmp_d (B2len, 3e11) < 0)
return -12; /* Dickson(12) */
else
return -30; /* Dickson(30) */
}
#define DIGITS_START 35
#define DIGITS_INCR 5
#define DIGITS_END 80
void
print_expcurves (double B1, const mpz_t B2, unsigned long dF, unsigned long k,
int S, int param)
{
double prob;
int i, j;
char sep, outs[128], flt[16];
double smoothness_correction;
if (param == ECM_PARAM_SUYAMA || param == ECM_PARAM_BATCH_2)
smoothness_correction = 1.0;
else if (param == ECM_PARAM_BATCH_SQUARE)
smoothness_correction = EXTRA_SMOOTHNESS_SQUARE;
else if (param == ECM_PARAM_BATCH_32BITS_D)
smoothness_correction = EXTRA_SMOOTHNESS_32BITS_D;
else /* This case should never happen */
smoothness_correction = 0.0;
for (i = DIGITS_START, j = 0; i <= DIGITS_END; i += DIGITS_INCR)
j += sprintf (outs + j, "%u%c", i, (i < DIGITS_END) ? '\t' : '\n');
outs[j] = '\0';
outputf (OUTPUT_VERBOSE, "Expected number of curves to find a factor "
"of n digits:\n%s", outs);
for (i = DIGITS_START; i <= DIGITS_END; i += DIGITS_INCR)
{
sep = (i < DIGITS_END) ? '\t' : '\n';
prob = ecmprob (B1, mpz_get_d (B2),
/* smoothness depends on the parametrization */
pow (10., i - .5) / smoothness_correction,
(double) dF * dF * k, S);
if (prob > 1. / 10000000)
outputf (OUTPUT_VERBOSE, "%.0f%c", floor (1. / prob + .5), sep);
else if (prob > 0.)
{
/* on Windows: 2.6e+009 or 2.6e+025 (8 characters in string) */
/* on Linux : 2.6e+09 or 2.6e+25 (7 characters in string) */
/* This will normalize the output so that the Windows ouptut will match the Linux output */
if (sprintf (flt, "%.2g", floor (1. / prob + .5)) == 8)
memmove (&flt[5], &flt[6], strlen(flt) - 5);
outputf (OUTPUT_VERBOSE, "%s%c", flt, sep);
}
else
outputf (OUTPUT_VERBOSE, "Inf%c", sep);
}
}
void
print_exptime (double B1, const mpz_t B2, unsigned long dF, unsigned long k,
int S, double tottime, int param)
{
double prob, exptime;
int i, j;
char sep, outs[128];
double smoothness_correction;
if (param == ECM_PARAM_SUYAMA || param == ECM_PARAM_BATCH_2)
smoothness_correction = 1.0;
else if (param == ECM_PARAM_BATCH_SQUARE)
smoothness_correction = EXTRA_SMOOTHNESS_SQUARE;
else if (param == ECM_PARAM_BATCH_32BITS_D)
smoothness_correction = EXTRA_SMOOTHNESS_32BITS_D;
else /* This case should never happen */
smoothness_correction = 0.0;
for (i = DIGITS_START, j = 0; i <= DIGITS_END; i += DIGITS_INCR)
j += sprintf (outs + j, "%u%c", i, (i < DIGITS_END) ? '\t' : '\n');
outs[j] = '\0';
outputf (OUTPUT_VERBOSE, "Expected time to find a factor of n digits:\n%s",
outs);
for (i = DIGITS_START; i <= DIGITS_END; i += DIGITS_INCR)
{
sep = (i < DIGITS_END) ? '\t' : '\n';
prob = ecmprob (B1, mpz_get_d (B2),
/* in batch mode, the extra smoothness is smaller */
pow (10., i - .5) / smoothness_correction,
(double) dF * dF * k, S);
exptime = (prob > 0.) ? tottime / prob : HUGE_VAL;
outputf (OUTPUT_TRACE, "Digits: %d, Total time: %.0f, probability: "
"%g, expected time: %.0f\n", i, tottime, prob, exptime);
if (exptime < 1000.)
outputf (OUTPUT_VERBOSE, "%.0fms%c", exptime, sep);
else if (exptime < 60000.) /* One minute */
outputf (OUTPUT_VERBOSE, "%.2fs%c", exptime / 1000., sep);
else if (exptime < 3600000.) /* One hour */
outputf (OUTPUT_VERBOSE, "%.2fm%c", exptime / 60000., sep);
else if (exptime < 86400000.) /* One day */
outputf (OUTPUT_VERBOSE, "%.2fh%c", exptime / 3600000., sep);
else if (exptime < 31536000000.) /* One year */
outputf (OUTPUT_VERBOSE, "%.2fd%c", exptime / 86400000., sep);
else if (exptime < 31536000000000.) /* One thousand years */
outputf (OUTPUT_VERBOSE, "%.2fy%c", exptime / 31536000000., sep);
else if (exptime < 31536000000000000.) /* One million years */
outputf (OUTPUT_VERBOSE, "%.0fy%c", exptime / 31536000000., sep);
else if (prob > 0.)
outputf (OUTPUT_VERBOSE, "%.1gy%c", exptime / 31536000000., sep);
else
outputf (OUTPUT_VERBOSE, "Inf%c", sep);
}
}
/* y should be NULL for P+1, and P-1, it contains the y coordinate for the
Weierstrass form for ECM (when sigma_is_A = -1). */
/* if gpu != 0 then it contains the number of curves that will be computed on
the GPU */
void
print_B1_B2_poly (int verbosity, int method, double B1, double B1done,
mpz_t B2min_param, mpz_t B2min, mpz_t B2, int S, mpz_t sigma,
int sigma_is_A, int Etype,
mpz_t y, int param, unsigned int gpu)
{
ASSERT ((method == ECM_ECM) || (y == NULL));
ASSERT ((-1 <= sigma_is_A) && (sigma_is_A <= 1));
ASSERT (param != ECM_PARAM_DEFAULT || sigma_is_A == 1 || sigma_is_A == -1);
if (test_verbose (verbosity))
{
outputf (verbosity, "Using ");
if (ECM_IS_DEFAULT_B1_DONE(B1done))
outputf (verbosity, "B1=%1.0f, ", B1);
else
outputf (verbosity, "B1=%1.0f-%1.0f, ", B1done, B1);
if (mpz_sgn (B2min_param) < 0)
outputf (verbosity, "B2=%Zd", B2);
else
outputf (verbosity, "B2=%Zd-%Zd", B2min, B2);
if (S > 0)
outputf (verbosity, ", polynomial x^%u", S);
else if (S < 0)
outputf (verbosity, ", polynomial Dickson(%u)", -S);
/* don't print in resume case, since x0 is saved in resume file */
if (method == ECM_ECM)
{
if (sigma_is_A == 1)
outputf (verbosity, ", A=%Zd", sigma);
else if (sigma_is_A == 0)
{
if (gpu) /* if not 0, contains number_of_curves */
{
outputf (verbosity, ", sigma=%d:%Zd", param, sigma);
mpz_add_ui (sigma, sigma, gpu-1);
outputf (verbosity, "-%d:%Zd", param, sigma);
mpz_sub_ui (sigma, sigma, gpu-1);