-
Notifications
You must be signed in to change notification settings - Fork 3
/
ecpp-to-aks.cpp
2173 lines (1862 loc) · 59.3 KB
/
ecpp-to-aks.cpp
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
// Hacked in to do 1 round of ecpp and then send the q value to run in aks
//
//
/**
* @author RyanLindeman@gmail.com
* @description Implementation of the Atkin-Morain primality test using
* Elliptic curve primality proving (ECPP). Many of the algorithms used by
* this program were described in the freely available book titled "Prime
* Numbers - A Computational Perspective" written by Richard Crandall and Carl
* Pomerance. Many thanks goes out to these individuals for writing this
* book and allowing it to be publicly distributed. Without this book this
* program would not be possible.
*
* I don't claim that this program is perfect, but it seems to perform well
* and is hopefully commented sufficiently to help the reader understand how
* to implement the Atkin-Morain ECPP algorithm in practice. Please don't
* hesitate to contact me with any bugs you may find so this program can be
* improved. It was written for my Advanced Algorithms (CS 6150) class in
* the Fall 2011.
*
* History
* @date 20111029 - Initial release
*/
#include <assert.h>
#include <gmp.h>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include "aks.h"
#include "miller-rabin.h"
// Global constants
const unsigned int MAX_DISCRIMINANTS = 28; ///< Number of discriminants
const unsigned int MAX_POINTS = 100; ///< Number of points to test
const unsigned int MAX_PRIMES = 1000; ///< Number of primes for sieve
const unsigned int MIN_LENSTRA_B1 = 1000; ///< Minimum primes for LenstraECM
const unsigned int AVG_LENSTRA_B1 = 2001; ///< Upper limit for LenstraECM
const unsigned int MAX_LENSTRA_B1 = 2000000000; ///< Maximum for ECPP
const unsigned int MAX_LENSTRA_CURVES = 20; ///< Number of curves to use
const unsigned int SIEVE_TEST_CUTOFF = UINT_MAX; ///< Cutoff for sieve test
// Global Variables
gmp_randstate_t gRandomState; ///< Holds random generator state and algorithm type
mpz_t gD[MAX_DISCRIMINANTS]; ///< Fixed array of discriminants
bool gDebug = false; ///< Debug flag for additional debug output
bool gCertificate = false; ///< Certificate flag for printing certificate
// Point structure
struct Point
{
mpz_t x;
mpz_t y;
};
/**
* InitDiscriminants will initialize the gD array the appropriate
* values.
*/
void InitDiscriminants(void)
{
int32_t anD[MAX_DISCRIMINANTS] = {
/* Place holder */ 0,
/* Class 1 */ -3,-4, -7,-8,-11,-19,-43,-67,-163,
/* Class 2 */ -15,-20,-24,-35,-40,-51,-52,-88,-91,-115,-123,-148,-187,-232,
-235,-267,-403,-427};
/* Class 3 */ //-23,-31,-59,-83,-107,-139,-211,-283,-307,-331,-379,-499,-547,
//-643,-883,-907};
/* Class 4 */ //-39,-55,-56,-68,-84,-120,-132,-136,-155,-168,-184,-195,-203,
//-219,-228,-259,-280,-291,-292,-312,-323,-328,-340,-355,-372,
//-388,-408,-435,-483,-520,-532,-555,-568,-595,-627,-667,-708,
//-715,-723,-760,-763,-772,-795,-955,-1003,-1012,-1027,-1227,
//-1243,-1387,-1411,-1435,-1507,-1555};
if(gDebug)
{
printf("Static discriminant list includes the following:\n");
}
// Call mpz_init on each array element and assign its value
for(unsigned int i=0;i<MAX_DISCRIMINANTS;i++)
{
// Initialize and set each gD array element
mpz_init_set_si(gD[i], anD[i]);
if(gDebug)
{
// Display discriminants available
gmp_printf("%d=%Zd\n", i, gD[i]);
}
}
}
/**
* Computes s and t that satisfy 2^s * t = n, with t odd.
*/
void FactorPow2(mpz_t* s, mpz_t* t, mpz_t& n)
{
mpz_set_ui(*s, 0);
mpz_set(*t, n);
while (mpz_even_p(*t))
{
mpz_add_ui(*s, *s, 1);
mpz_divexact_ui(*t, *t, 2);
}
}
/**
* SquareMod returns the solution x that satisfies the following equation:
* x^2 === a (mod p). This is an implementation of algorithm (2.3.8). The
* book indicates that algorithm (2.3.9) is faster, but more complicated to
* implement. If your desperate for more speed you should focus on improving
* FindFactor first.
*/
bool SquareMod(mpz_t* theX, mpz_t& theA, mpz_t& theP)
{
bool anResult = false; // Was a valid X found?
// Perform the simple Jacobi test. If Jacobi returns -1 or there is no
// solution
if(1 == mpz_jacobi(theA, theP))
{
mpz_t anMod4;
mpz_t anMod8;
// Initialize some temporary variables
mpz_init(anMod4);
mpz_init(anMod8);
// Compute anMod4 first
mpz_mod_ui(anMod4, theP, 4);
// Compute anMod8 next
mpz_mod_ui(anMod8, theP, 8);
// Test the simplest cases p === 3, 7 (mod 8)
if(mpz_cmp_ui(anMod4,3) == 0)
{
mpz_t anExp; // The exponent
// Initialize and set the exponent
mpz_init_set(anExp, theP);
// Add 1 to the exponent
mpz_add_ui(anExp, anExp, 1);
// Divide the exponent by 4
mpz_div_ui(anExp, anExp, 4);
// Compute x = a^(p+1)/4 mod p
mpz_powm(*theX, theA, anExp, theP);
// Clear the exponent
mpz_clear(anExp);
// We found a valid x
anResult = true;
}
// Test the next simplest case p === 5 (mod 8)
else if(mpz_cmp_ui(anMod8, 5) == 0)
{
mpz_t anExp1; // The exponent (p-1)/4
mpz_t anModP; // Temporary value for second test
// Initialize and set the exponent
mpz_init_set(anExp1, theP);
mpz_init(anModP);
// Subtract from the exponent 1
mpz_sub_ui(anExp1, anExp1, 1);
// Divide the exponent by 4
mpz_tdiv_q_ui(anExp1, anExp1, 4);
// Compute x = a^(p-1)/4 mod p
mpz_powm(anModP, theA, anExp1, theP);
mpz_mod(anModP, anModP, theP);
// See if the anModP result is 1
if(mpz_cmp_ui(anModP,1) == 0)
{
mpz_t anExp2; // The exponent (p+3)/8
// Initialize and set the exponent
mpz_init_set(anExp2, theP);
// Add to the exponent 3
mpz_add_ui(anExp2, anExp2, 3);
// Divide the exponent by 8
mpz_tdiv_q_ui(anExp2, anExp2, 8);
// Compute x = a^(p+3)/8 mod p
mpz_powm(*theX, theA, anExp2, theP);
// Clear our values we don't need anymore
mpz_clear(anExp2);
}
else
{
mpz_t anExp2; // The exponent (p-5)/8
mpz_t anBase; // The base a*4
// Initialize and set the exponent
mpz_init_set(anExp2, theP);
mpz_init(anBase);
// Subtract from the exponent 5
mpz_sub_ui(anExp2, anExp2, 5);
// Divide the exponent by 8
mpz_tdiv_q_ui(anExp2, anExp2, 8);
// Compute 4*a as the base
mpz_mul_ui(anBase, theA, 4);
// Compute x = 4*a^(p-5)/8 mod p
mpz_powm(anBase, anBase, anExp2, theP);
// Multiply the result by 2*a*x mod p
mpz_mul_ui(anBase, anBase, 2);
mpz_mul(anBase, anBase, theA);
mpz_mod(*theX, anBase, theP);
// Clear our values we don't need anymore
mpz_clear(anBase);
mpz_clear(anExp2);
}
// Clear our values we don't need anymore
mpz_clear(anModP);
mpz_clear(anExp1);
// We found a valid x
anResult = true;
}
// Still nothing? Try the hardest case p === 1 (mod 8)
else
{
// Case 2 of algorithm
mpz_t d, s, t, A, D, m, i;
mpz_t anExp1, anExp2, anModP, two;
// Initialize our variables used for case 2
mpz_init(d);
mpz_init(s);
mpz_init(t);
mpz_init(A);
mpz_init(D);
mpz_init(m);
mpz_init(i);
mpz_init(anExp1);
mpz_init(anExp2);
mpz_init(anModP);
mpz_init(two);
// Find a random integer d = [2, p - 1] with Jacobi -1
mpz_sub_ui(anExp1, theP, 3);
do {
mpz_urandomm(d, gRandomState, anExp1);
mpz_add_ui(d, d, 2);
} while (mpz_jacobi(d, theP) != -1);
// Represent p - 1 = 2 ^ s * t, with t odd
mpz_sub_ui(anExp1, theP, 1);
FactorPow2(&s, &t, anExp1);
// Compute A = a ^ t mod p
mpz_powm(A, theA, t, theP);
// Compute D = d ^ t mod p
mpz_powm(D, d, t, theP);
// Compute -1 mod p
mpz_sub_ui(anModP, theP, 1);
// Compute m
mpz_set_ui(two, 2);
mpz_set_ui(m, 0);
for (mpz_set_ui(i, 0); mpz_cmp(i, s) < 0; mpz_add_ui(i, i, 1))
{
// Compute 2 ^ (s - 1 - i)
mpz_sub_ui(anExp1, s, 1);
mpz_sub(anExp1, anExp1, i);
mpz_powm(anExp1, two, anExp1, theP);
// Compute A * D ^ m
mpz_powm(anExp2, D, m, theP);
mpz_mul(anExp2, anExp2, A);
mpz_mod(anExp2, anExp2, theP);
// Compute (A * D ^ m) ^ (2 ^ (s - 1 - i))
mpz_powm(anExp1, anExp2, anExp1, theP);
if (mpz_cmp(anExp1, anModP) == 0)
{
// Compute m = m + 2 ^ i
mpz_powm(anExp1, two, i, theP);
mpz_add(m, m, anExp1);
}
}
// Compute a ^ ((t + 1) / 2)
mpz_add_ui(anExp1, t, 1);
mpz_divexact_ui(anExp1, anExp1, 2);
mpz_powm(anExp1, theA, anExp1, theP);
// Compute D ^ (m / 2)
mpz_div_ui(anExp2, m, 2);
mpz_powm(anExp2, D, anExp2, theP);
// Compute x = a ^ ((t + 1) / 2) * D ^ (m / 2) mod p
mpz_mul(anExp1, anExp1, anExp2);
mpz_mod(anExp1, anExp1, theP);
mpz_set(*theX, anExp1);
// Clear our case 2 variables
mpz_clear(two);
mpz_clear(anModP);
mpz_clear(anExp2);
mpz_clear(anExp1);
mpz_clear(i);
mpz_clear(m);
mpz_clear(D);
mpz_clear(A);
mpz_clear(t);
mpz_clear(s);
mpz_clear(d);
// A result was found, return true
anResult = true;
}
// Clear our temporary variables before returning
mpz_clear(anMod8);
mpz_clear(anMod4);
}
// If no valid result was found, then set theX to 0
if(false == anResult)
{
// Is this necessary?
mpz_set_ui(*theX, 0);
}
// Return the result found if any
return anResult;
}
/**
* ModifiedCornacchia will either report that no solution exists for
* 4p = u^2 + abs(D)v^2 (where p is a given prime and -4p < D < 0 or returns
* the solution (u, v). This algorithm is based on algorithm (2.3.13) in
* the book.
*/
bool ModifiedCornacchia(mpz_t* theU, mpz_t* theV, mpz_t& theP, mpz_t& theD)
{
bool anResult = false; // No solution found yet
mpz_t x0; // Initial square root value for x
mpz_t a; // Euclid chain value a
mpz_t b; // Euclid chain value b
mpz_t c; // Euclid chain value c
mpz_t t; // Temporary value for final report
// Initalize square root value
mpz_init(x0);
mpz_init(a);
mpz_init(b);
mpz_init(c);
mpz_init(t);
// Obtain the initial square root value for x0
bool anFound = SquareMod(&x0, theD, theP); // uses 2.3.8 algorithm
// If x0 was found, then ensure x0^2 !=== D (mod 2)
if(anFound)
{
mpz_t xt; // Temporary value x0 mod 2
mpz_t dt; // Temporary value theD mod 2
// Initalize some temporary values
mpz_init(xt);
mpz_init(dt);
// Compute x0 mod 2 and D mod 2
mpz_mod_ui(dt, theD, 2);
mpz_mod_ui(xt, x0, 2);
// If x0^2 !=== D (mod 2) then adjust x0
if(mpz_cmp(dt,xt) != 0)
{
mpz_sub(x0, theP, x0);
}
// Clear our temporary values
mpz_clear(dt);
mpz_clear(xt);
} else {
if(gDebug)
{
// Warn the user and proceed anyway with x0 = 0
printf("x0 not found!?\n");
}
}
// Initialize Euclid chain
// Set a = 2p
mpz_set(a, theP);
mpz_mul_ui(a, a, 2);
// Set b = x0
mpz_set(b, x0);
// Set c = lower_bound(2*sqrt(p)) (see 9.2.11 algorithm)
mpz_sqrt(c, theP);
mpz_mul_ui(c, c, 2);
// Euclid chain
while(mpz_cmp(b, c) > 0)
{
mpz_t anModB; // Temporary result for a mod b
// Initialize our temporary value for a mod b
mpz_init(anModB);
// Compute a mod b first
mpz_mod(anModB, a, b);
// Set a = b
mpz_set(a, b);
// Set b = anModB
mpz_set(b, anModB);
// Clear our temporary result for a mod b now
mpz_clear(anModB);
} // while(b > c)
// Final report/check
// Compute a = b^2 to use later
mpz_pow_ui(a, b, 2);
// Compute c = 4p to use later
mpz_mul_ui(c, theP, 4);
// t = 4p - y^2;
mpz_sub(t, c, a);
// Compute a = abs(D)
mpz_abs(a, theD);
// Compute c = t / a
mpz_tdiv_q(c, t, a);
// Compute a = t mod a
mpz_mod(a, t, a);
// Now test if solution was found
if(mpz_cmp_ui(a, 0) == 0 && mpz_perfect_square_p(c) != 0)
{
// Set theU value for our solution
mpz_set(*theU, b);
// Set theV value for our solution
mpz_sqrt(*theV, c);
// We found a solution
anResult = true;
}
// Clear our values used above
mpz_clear(t);
mpz_clear(c);
mpz_clear(b);
mpz_clear(a);
mpz_clear(x0);
// Return anResult which is true if solution was found
return anResult;
}
/**
* Add implements the Elliptic add method described by algorithm (7.2.2). This
* will return true if an illegal inversion occurred and will not set theR
* unless the inversion succeeded.
*/
bool Add(struct Point* theR, struct Point& theP1, struct Point& theP2,
mpz_t& theN)
{
bool anResult = false; // True if illegal inversion occurred
mpz_t m; // Elliptic slope value m
// Initialize our m value first
mpz_init(m);
// Does P1 and P2 possibly represent inverse points?
if(mpz_cmp(theP1.x, theP2.x) == 0)
{
// Compute (y2 + y1) mod n
mpz_add(m, theP1.y, theP2.y);
mpz_mod(m, m, theN);
// Are we at the infinite point O? then return O
if(mpz_cmp_ui(m, 0) == 0)
{
// Set our return result as O
mpz_set_ui(theR->x, 0);
mpz_set_ui(theR->y, 1);
// Clear our m value used above
mpz_clear(m);
// Illegal inversion at the infinite point O
return true;
}
}
// Compute (x2 - x1) portion of m = (y2-y1)(x2-x1)^-1
mpz_sub(m, theP2.x, theP1.x);
// Compute inverse (x2-x1)^-1 first
anResult = (mpz_invert(m, m, theN) == 0);
// Only proceed if our inversion succeeded
if(false == anResult)
{
Point R; // Point R to return via theR
// Initialize our working point R
mpz_init(R.x);
mpz_init(R.y);
// Compute (y2 - y1)
mpz_sub(R.x, theP2.y, theP1.y);
// Compute m = ((y2-y1)(x2-x1)^-1) mod n
mpz_mul(m, R.x, m);
mpz_mod(m, m, theN);
// Compute R.x = m^2 - x1 - x2
mpz_powm_ui(R.x, m, 2, theN);
mpz_sub(R.x, R.x, theP1.x);
mpz_sub(R.x, R.x, theP2.x);
mpz_mod(R.x, R.x, theN);
// Compute R.y = m(x1 - x3) - y1
mpz_sub(R.y, theP1.x, R.x);
mpz_mul(R.y, R.y, m);
mpz_sub(R.y, R.y, theP1.y);
mpz_mod(R.y, R.y, theN);
// Set our return results now
mpz_set(theR->x, R.x);
mpz_set(theR->y, R.y);
// Clear our working point R
mpz_clear(R.y);
mpz_clear(R.x);
}
// Clear our m value used above
mpz_clear(m);
// Return true if illegal inversion occurred
return anResult;
}
/**
* Double implements the double portion of the Elliptic add method described by
* algorithm (7.2.2).
*/
void Double(struct Point* theR, struct Point& theP, mpz_t& theN, mpz_t& theA)
{
mpz_t m; // Elliptic slope value m
Point R; // Point R to return via theR
// Initialize our values first
mpz_init(m);
mpz_init(R.x);
mpz_init(R.y);
// Compute 2*P.y first
mpz_mul_ui(m, theP.y, 2);
// Compute inverse (2y)^-1 first
mpz_invert(m, m, theN);
// Compute (3x^2 + a)
mpz_mul(R.x, theP.x, theP.x);
mpz_mul_ui(R.x, R.x, 3);
mpz_add(R.x, R.x, theA);
// Compute m = (3x^2 + a)(2y)^-1
mpz_mul(m, R.x, m);
mpz_mod(m, m, theN);
// Compute R.x = m^2 - 2x
mpz_powm_ui(R.x, m, 2, theN);
mpz_submul_ui(R.x, theP.x, 2);
mpz_mod(R.x, R.x, theN);
// Compute R.y = m(x1 - x3) - y1
mpz_sub(R.y, theP.x, R.x);
mpz_mul(R.y, R.y, m);
mpz_sub(R.y, R.y, theP.y);
mpz_mod(R.y, R.y, theN);
// Set our return results now
mpz_set(theR->x, R.x);
mpz_set(theR->y, R.y);
// Clear our values used above
mpz_clear(R.y);
mpz_clear(R.x);
mpz_clear(m);
}
/**
* Multiply implements the Elliptical multiplication method described by
* algorithm (7.2.4). This implementation doesn't completely match the
* algorithm described in the book, but has been proven to yield the same
* results as much as I can test. This will return true if an illegal
* inversion occurred during one of the Elliptical add method calls.
*/
bool Multiply(struct Point* theR, mpz_t* theD, mpz_t& theM, struct Point& P,
mpz_t& theN, mpz_t& theA)
{
bool anResult = false; // True if illegal inversion occurred
// If theM provided is zero, return O (point at infinity)
if(mpz_cmp_ui(theM, 0) == 0)
{
// Return O since theN provided was 0
mpz_set_ui(theR->x, 0);
mpz_set_ui(theR->y, 1);
}
else
{
mpz_t i; // number of multiplies to perform
Point A; // The original number provided
Point B; // Another number to be added, starts at infinity
// Initialize our counter
mpz_init_set(i, theM);
mpz_init_set(A.x, P.x);
mpz_init_set(A.y, P.y);
mpz_init_set_ui(B.x, 0);
mpz_init_set_ui(B.y, 1);
// Loop while no illegal inversions have occurred and theM is > 0
while(false == anResult && mpz_cmp_ui(i, 0) > 0)
{
// When our counter is odd, use the Add method
if(mpz_odd_p(i))
{
// Subtract one from our counter
mpz_sub_ui(i, i, 1);
// Compute difference between B.x and A.x
mpz_sub(*theD, B.x, A.x);
mpz_mod(*theD, *theD, theN);
// Perform GCD test
mpz_gcd(*theD, *theD, theN);
// Continue our loop only if d == 1 or d == n
anResult = !(mpz_cmp_ui(*theD, 1) == 0 || mpz_cmp(*theD, theN) == 0);
// If A is at infinity (point O) don't do anything
if(mpz_cmp_ui(A.x, 0) == 0 && mpz_cmp_ui(A.y, 1) == 0)
{
// do nothing
}
// If B is at infinity (point O) then B = A*O = A
else if(mpz_cmp_ui(B.x, 0) == 0 && mpz_cmp_ui(B.y, 1) == 0)
{
mpz_set(B.x, A.x);
mpz_set(B.y, A.y);
}
// Otherwise, just do the addition if theD != 1 or n
else if(!anResult)
{
// Check for illegal inversions during Add
anResult = Add(&B, A, B, theN);
}
}
// Our counter is even, use the Double method instead of Add
else
{
// Divide our counter by two for each double we perform
mpz_tdiv_q_ui(i, i, 2);
// Compute the value 2*A.y
mpz_mul_ui(*theD, A.y, 2);
mpz_mod(*theD, *theD, theN);
// Perform GCD test
mpz_gcd(*theD, *theD, theN);
// Continue our loop only if d == 1 or d == n
anResult = !(mpz_cmp_ui(*theD, 1) == 0 || mpz_cmp(*theD, theN) == 0);
// Use the double method if theD != 1 or n
if(!anResult)
{
// Even value, use double
Double(&A, A, theN, theA);
}
}
}
// B has our results, make sure they are not bigger than n
mpz_mod(B.x, B.x, theN);
mpz_mod(B.y, B.y, theN);
// Set our results to whatever B is set to now
mpz_set(theR->x, B.x);
mpz_set(theR->y, B.y);
// Clear our values used above
mpz_clear(B.y);
mpz_clear(B.x);
mpz_clear(A.y);
mpz_clear(A.x);
mpz_clear(i);
}
// Return true if an illegal inversions occurred or a divisor d was found
return anResult;
}
/**
* LenstraECM will attempt to find the largest non-trivial prime number that
* will factor theN provided according to algorithm (7.4.2). The book says
* that algorithm (7.4.4) is a faster implementation. This method is called by
* FindFactor to find a probable prime (theQ) that if also proven prime will
* be part of the certificate proving theN to be prime. The book says that
* LenstraECM is only the 3rd fastest algorithm for finding factors. If you
* want to improve the running time of this program, replace this with another
* algorithm that is faster.
*/
bool LenstraECM(mpz_t* theQ, mpz_t& theN, unsigned long Bmax)
{
bool anResult = false; // True if factor was found and returned, false otherwise
Point P; // Point (x,y) on curve E
mpz_t a; // Random value a for curve E
mpz_t b; // Random value b for curve E
mpz_t g; // The factor found if any
mpz_t t; // Temporary value for computing b
mpz_t p; // Prime number to test for factor
// Initialize our values
mpz_init(P.x);
mpz_init(P.y);
mpz_init(a);
mpz_init(b);
mpz_init(g);
mpz_init(t);
mpz_init(p);
// Loop until either a factor is found or we give up trying
unsigned long B1 = MIN_LENSTRA_B1;
do
{
for(unsigned int i = 0; i < MAX_LENSTRA_CURVES; i++)
{
// Step 1: Find E_a,b() and point P(x,y)
// Pick a random x from 0 to N-1
mpz_urandomm(P.x, gRandomState, theN);
// Pick a random y from 0 to N-1
mpz_urandomm(P.y, gRandomState, theN);
// Pick a random a from 0 to N-1
mpz_urandomm(a, gRandomState, theN);
// Compute b = (y^2 - x^3 - ax) mod n
mpz_pow_ui(b, P.y, 2); // y^2
mpz_pow_ui(t, P.x, 3); // x^3
mpz_sub(b, b, t); // y^2 - x^3
mpz_mul(t, a, P.x); // ax
mpz_sub(b, b, t); // y^2 - x^3 - ax
mpz_mod(b, b, theN); // (y^2 - x^3 - ax) mod n
// Compute 4a^3 + 27b^2
mpz_pow_ui(t, a, 3); // a^3
mpz_mul_ui(t, t, 4); // 4*(a^3)
mpz_pow_ui(b, b, 2); // b^2
mpz_mul_ui(b, b, 27); // 27*(b^2)
mpz_add(t, t, b); // t = (4a^3) + (27b^2)
mpz_mod(t, t, theN); // t mod n
mpz_gcd(g, t, theN); // Compute g = gcd(4a^3+27b^2, n)
// Continue until we find a g != n
if(mpz_cmp(g, theN) == 0)
continue;
// If G is greater than 1 but not equal to n then we have found a factor
else if(mpz_cmp_ui(g, 1) > 0)
{
// Return theQ = theN / g;
mpz_remove(*theQ, theN, g);
// Factor g was found, return theQ = theN / g
anResult = true;
// Break out of the loop, a factor was found!
break;
}
// Start with prime 2 and work our way up from there
mpz_set_ui(p, 2);
// Step 2: Prime-power multipliers
do
{
// Step 2a: Compute (t=p^(x-1)) <= (B1/p)
// Compute (B1 / p) as our loop cutoff
mpz_set_ui(b, B1);
mpz_tdiv_q(b, b, p);
// Start with t = p
mpz_set(t, p);
while(mpz_cmp(t, b) < 0)
{
// Compute t = t*p
mpz_mul(t, t, p);
}
// Step 2b: Compute P=pP where P is our point and p is our prime to test
bool found = Multiply(&P, &g, t, P, theN, a);
// An illegal value was found such that g is a divisor of theN
if(found)
{
// Was the only factor found the same as theN? then return theN
if(mpz_cmp(g, theN) == 0)
{
// theN is prime already so just return it
mpz_set(*theQ, theN);
}
// Otherwise return theQ = theN / g
else
{
// We found a significant factor so return it
mpz_remove(*theQ, theN, g);
}
// Factor g was found, return theQ assigned above
anResult = true;
// Break out of the loop, a factor was found!
break;
}
// Try the next prime p
mpz_nextprime(p, p);
} while(!anResult && mpz_cmp_ui(p, B1) < 0);
} // for(i<MAX_LENSTRA_CURVES)
// Increment B1 and try again
B1 *= 2;
} while(!anResult && B1 < Bmax);
// Clear our values used above
mpz_clear(p);
mpz_clear(t);
mpz_clear(g);
mpz_clear(b);
mpz_clear(a);
mpz_clear(P.y);
mpz_clear(P.x);
// Return true if factor was found and theQ = theN / g
return anResult;
}
/**
* FindFactor will attempt to reduce theM provided to a potential prime theQ
* that is also less than theT by factoring all primes out of theM. If theM
* is prime and also bigger than theN then FindFactor will return false
* meaning theQ can't be found because it would be bigger than theN! My
* general feel is that this is the part of the code that takes the most
* running time. If you can speed this up, you will make significant increase
* to the running time of the ECPP algorithm.
*/
bool FindFactor(mpz_t* theQ, mpz_t& theN, mpz_t& theM, mpz_t& theT,
unsigned long Bmax)
{
mpz_t prime; // Prime numbers to remove from theQ
// Start with theQ = theM
mpz_set(*theQ, theM);
// Step 1: Make sure theQ is not probably prime and >= theN
// If theQ >= theN and is prime then stop now and try another curve
if(miller_rabin_is_prime(*theQ, 10) && mpz_cmp(*theQ, theN) >= 0)
return false;
// Step 2: Perform a simple sieve of the smaller primes starting with 2
mpz_init_set_ui(prime, 2);
// Loop through MAX_PRIMES prime numbers and try to remove them from theQ
unsigned long count = MAX_PRIMES; // Primes to try to remove from theQ
do
{
// Remove as many of this prime as possible
mpz_remove(*theQ, *theQ, prime);
// Find the next prime to try to remove from theQ
mpz_nextprime(prime, prime);
// Make sure theQ is still larger than theT
if(mpz_cmp(*theQ, theT) < 0)
return false; // theQ is smaller than theT
// Is theQ prime and smaller than theN now? then return theQ
if(mpz_cmp(*theQ, theN) < 0 && miller_rabin_is_prime(*theQ, 10))
{
// Clear our prime value used above and return now
mpz_clear(prime);
// We found a suitable theQ value, stop now
return true;
}
} while(mpz_cmp(*theQ, theT) >= 0 && count-- > 0);
// Clear our prime value used above
mpz_clear(prime);
// Step 3: Use LenstraECM to try to find additional factors
bool found = false;
do
{
// Did we find a factor such that theQ = theN / x?
found = LenstraECM(theQ, *theQ, Bmax);
// Make sure theQ is still larger than theT
if(mpz_cmp(*theQ, theT) < 0)
return false; // theQ is smaller than theT
// Have we found something smaller than theM that is also prime?
if(mpz_cmp(*theQ, theN) < 0 && miller_rabin_is_prime(*theQ, 10))
{
return true;
}
} while(found);
// Make sure theM != theQ
if(mpz_cmp(*theQ, theM) == 0)
return false;
// Make sure theQ < theN
if(mpz_cmp(*theQ, theN) >= 0)
return false;
// Return false, we didn't find a suitable q
return false;
}
/**
* FactorOrders attempts to find a possible order m that factors as m = kq
* where k > 1 and q is a probable prime > (n^0.25 + 1)^2. If this can't be
* done after K_max iterations than return FALSE and choose a new discriminant
* D and curve m. This is based on step 2 of algorithm (7.6.3).
*/
bool FactorOrders(mpz_t* theM, mpz_t* theQ, mpz_t& theU, mpz_t& theV,
mpz_t& theN, mpz_t& theD, unsigned long Bmax)
{
bool anResult = false; // Was factor theQ found?
mpz_t t; // t = (n^0.25 + 1)^2 to test theQ with
mpz_t m0; // m0 = n + 1
mpz_t m1; // m1 = m0 + u
mpz_t m2; // m2 = m0 - u
// Initialize and set our temporary value t to theN
mpz_init(t);
mpz_init_set(m0, theN);
mpz_init(m1);
mpz_init(m2);
// Add 1 to m0
mpz_add_ui(m0, m0, 1);
// Now take the double square root of theN and store in T
mpz_sqrt(t, theN);
mpz_sqrt(t, t);
// Now add one to the result
mpz_add_ui(t, t, 1);
// Now square the result
mpz_pow_ui(t, t, 2);
// Test initial special cases of m0 = n + 1 +/- u
mpz_add(m1, m0, theU); // Add u to m0 for m1
mpz_sub(m2, m0, theU); // Subtract u from m0 for m2
if(true == FindFactor(theQ, theN, m1, t, Bmax))
{