-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcert.c
1826 lines (1667 loc) · 57.2 KB
/
vcert.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
/*
* Verify Cert
* version 0.96
*
* Copyright (c) 2013-2016 Dana Jacobsen (dana@acm.org).
* This is free software; you can redistribute it and/or modify it under
* the same terms as the Perl 5 programming language system itself.
*
* Verifies Primo v3, Primo v4, and MPU certificates.
*
* Return values:
* 0 all numbers are verified prime.
* 1 at least one number was verified composite.
* 2 the certificate does not provide a complete proof.
* 3 there is an error in the certificate.
*
* TODO: Allow multiple proofs per input file
* TODO: Projective EC for ~4x faster operation
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <gmp.h>
/*****************************************************************************/
/* Preliminary definitions */
/*****************************************************************************/
/* Projective doesn't work yet */
#define USE_AFFINE_EC 1
#define RET_PRIME 0
#define RET_COMPOSITE 1
#define RET_INVALID 2
#define RET_ERROR 3
#define CERT_UNKNOWN 0
#define CERT_PRIMO 1
#define CERT_PRIMO42 2
#define CERT_MPU 3
#define MAX_LINE_LEN 60000
#define MAX_STEPS 20000
#define MAX_QARRAY 100
#define BAD_LINES_ALLOWED 5 /* Similar to WraithX's verifier */
typedef unsigned long UV;
typedef signed long IV;
#define croak(fmt,...) { gmp_printf(fmt,##__VA_ARGS__); exit(RET_ERROR); }
#define MPUassert(c,text) if (!(c)) { croak("Internal error: " text); }
#define BGCD_PRIMES 168
#define BGCD_LASTPRIME 997
#define BGCD_NEXTPRIME 1009
void GMP_pn_primorial(mpz_t prim, UV n);
UV trial_factor(mpz_t n, UV from_n, UV to_n);
int miller_rabin_ui(mpz_t n, UV base);
int miller_rabin(mpz_t n, mpz_t a);
void lucas_seq(mpz_t U, mpz_t V, mpz_t n, IV P, IV Q, mpz_t k, mpz_t Qk, mpz_t t);
#define mpz_mulmod(r, a, b, n, t) \
do { mpz_mul(t, a, b); mpz_mod(r, t, n); } while (0)
/*****************************************************************************/
/* Some global variables and functions we'll use */
/*****************************************************************************/
int _verbose = 0;
int _quiet = 0;
int _testcount = 0;
int _base = 10;
int _step = 0;
int _format = CERT_UNKNOWN;
char _line[MAX_LINE_LEN+1];
char _vstr[MAX_LINE_LEN+1];
const char* _filename;
FILE* _fh;
mpz_t PROOFN, N, A, B, M, Q, X, Y, LQ, LP, S, R, T, J, W, T1, T2;
mpz_t QARRAY[MAX_QARRAY];
mpz_t AARRAY[MAX_QARRAY];
mpz_t _bgcd;
int _num_chains = 0;
mpz_t _chain_n[MAX_STEPS];
mpz_t _chain_q[MAX_STEPS];
static void var_init(void) {
int i;
mpz_init(PROOFN);
mpz_init(N);
mpz_init(A);
mpz_init(B);
/* MPU: */
mpz_init(M);
mpz_init(Q);
mpz_init(X);
mpz_init(Y);
mpz_init(LQ);
mpz_init(LP);
/* Primo: */
mpz_init(S);
mpz_init(R);
mpz_init(T);
mpz_init(J);
mpz_init(W);
mpz_init(_bgcd);
GMP_pn_primorial(_bgcd, BGCD_PRIMES);
mpz_init(T1);
mpz_init(T2);
for (i = 0; i < MAX_QARRAY; i++) {
mpz_init(QARRAY[i]);
mpz_init(AARRAY[i]);
}
}
static void var_free(void) {
int i;
mpz_clear(PROOFN);
mpz_clear(N);
mpz_clear(A);
mpz_clear(B);
mpz_clear(M);
mpz_clear(Q);
mpz_clear(X);
mpz_clear(Y);
mpz_clear(LQ);
mpz_clear(LP);
mpz_clear(S);
mpz_clear(R);
mpz_clear(T);
mpz_clear(J);
mpz_clear(W);
mpz_clear(_bgcd);
mpz_clear(T1);
mpz_clear(T2);
for (i = 0; i < MAX_QARRAY; i++) {
mpz_clear(QARRAY[i]);
mpz_clear(AARRAY[i]);
}
}
static void quit_prime(void) {
if (!_quiet) printf(" \r");
if (!_quiet) printf("PRIME\n");
var_free();
exit(RET_PRIME);
}
static void quit_composite(void) {
if (!_quiet) printf(" \r");
if (!_quiet) printf("COMPOSITE\n");
var_free();
exit(RET_COMPOSITE);
}
static void quit_invalid(const char* type, const char* msg) {
if (!_quiet) printf("\n");
if (!_quiet) gmp_printf("%s: step %d, %Zd failed condition %s\n", type, _step, N, msg);
var_free();
exit(RET_INVALID);
}
static void quit_error(const char* msg1, const char* msg2) {
if (!_quiet) printf("\n");
if (!_quiet) gmp_printf("ERROR: step %d, %s%s\n", _step, msg1, msg2);
var_free();
exit(RET_ERROR);
}
#if USE_AFFINE_EC
/*****************************************************************************/
/* EC: affine with point (x,y,1) */
/*****************************************************************************/
struct ec_affine_point { mpz_t x, y; };
/* P3 = P1 + P2 */
static void _ec_add_AB(mpz_t n,
struct ec_affine_point P1,
struct ec_affine_point P2,
struct ec_affine_point *P3,
mpz_t m,
mpz_t t1,
mpz_t t2)
{
if (!mpz_cmp(P1.x, P2.x)) {
mpz_add(t2, P1.y, P2.y);
mpz_mod(t1, t2, n);
if (!mpz_cmp_ui(t1, 0) ) {
mpz_set_ui(P3->x, 0);
mpz_set_ui(P3->y, 1);
return;
}
}
mpz_sub(t1, P2.x, P1.x);
mpz_mod(t2, t1, n);
/* t1 = 1/deltay mod n */
if (!mpz_invert(t1, t2, n)) {
/* We've found a factor! In multiply, gcd(mult,n) will be a factor. */
mpz_set_ui(P3->x, 0);
mpz_set_ui(P3->y, 1);
return;
}
mpz_sub(m, P2.y, P1.y);
mpz_mod(t2, m, n); /* t2 = deltay mod n */
mpz_mul(m, t1, t2);
mpz_mod(m, m, n); /* m = deltay / deltax mod n */
/* x3 = m^2 - x1 - x2 mod n */
mpz_mul(t1, m, m);
mpz_sub(t2, t1, P1.x);
mpz_sub(t1, t2, P2.x);
mpz_mod(P3->x, t1, n);
/* y3 = m(x1 - x3) - y1 mod n */
mpz_sub(t1, P1.x, P3->x);
mpz_mul(t2, m, t1);
mpz_sub(t1, t2, P1.y);
mpz_mod(P3->y, t1, n);
}
/* P3 = 2*P1 */
static void _ec_add_2A(mpz_t a,
mpz_t n,
struct ec_affine_point P1,
struct ec_affine_point *P3,
mpz_t m,
mpz_t t1,
mpz_t t2)
{
/* m = (3x1^2 + a) * (2y1)^-1 mod n */
mpz_mul_ui(t1, P1.y, 2);
if (!mpz_invert(m, t1, n)) {
mpz_set_ui(P3->x, 0);
mpz_set_ui(P3->y, 1);
return;
}
mpz_mul_ui(t1, P1.x, 3);
mpz_mul(t2, t1, P1.x);
mpz_add(t1, t2, a);
mpz_mul(t2, m, t1);
mpz_tdiv_r(m, t2, n);
/* x3 = m^2 - 2x1 mod n */
mpz_mul(t1, m, m);
mpz_mul_ui(t2, P1.x, 2);
mpz_sub(t1, t1, t2);
mpz_tdiv_r(P3->x, t1, n);
/* y3 = m(x1 - x3) - y1 mod n */
mpz_sub(t1, P1.x, P3->x);
mpz_mul(t2, t1, m);
mpz_sub(t1, t2, P1.y);
mpz_tdiv_r(P3->y, t1, n);
}
static int ec_affine_multiply(mpz_t a, mpz_t k, mpz_t n, struct ec_affine_point P, struct ec_affine_point *R, mpz_t d)
{
int found = 0;
struct ec_affine_point A, B, C;
mpz_t t, t2, t3, mult;
mpz_init(A.x); mpz_init(A.y);
mpz_init(B.x); mpz_init(B.y);
mpz_init(C.x); mpz_init(C.y);
mpz_init(t); mpz_init(t2); mpz_init(t3);
mpz_init_set_ui(mult, 1); /* holds intermediates, gcd at end */
mpz_set(A.x, P.x); mpz_set(A.y, P.y);
mpz_set_ui(B.x, 0); mpz_set_ui(B.y, 1);
/* Binary ladder multiply. */
while (mpz_cmp_ui(k, 0) > 0) {
if (mpz_odd_p(k)) {
mpz_sub(t, B.x, A.x);
mpz_mul(t2, mult, t);
mpz_mod(mult, t2, n);
if ( !mpz_cmp_ui(A.x, 0) && !mpz_cmp_ui(A.y, 1) ) {
/* nothing */
} else if ( !mpz_cmp_ui(B.x, 0) && !mpz_cmp_ui(B.y, 1) ) {
mpz_set(B.x, A.x); mpz_set(B.y, A.y);
} else {
_ec_add_AB(n, A, B, &C, t, t2, t3);
/* If the add failed to invert, then we have a factor. */
mpz_set(B.x, C.x); mpz_set(B.y, C.y);
}
mpz_sub_ui(k, k, 1);
} else {
mpz_mul_ui(t, A.y, 2);
mpz_mul(t2, mult, t);
mpz_mod(mult, t2, n);
_ec_add_2A(a, n, A, &C, t, t2, t3);
mpz_set(A.x, C.x); mpz_set(A.y, C.y);
mpz_tdiv_q_2exp(k, k, 1);
}
}
mpz_gcd(d, mult, n);
found = (mpz_cmp_ui(d, 1) && mpz_cmp(d, n));
mpz_tdiv_r(R->x, B.x, n);
mpz_tdiv_r(R->y, B.y, n);
mpz_clear(mult);
mpz_clear(t); mpz_clear(t2); mpz_clear(t3);
mpz_clear(A.x); mpz_clear(A.y);
mpz_clear(B.x); mpz_clear(B.y);
mpz_clear(C.x); mpz_clear(C.y);
return found;
}
#else
/*****************************************************************************/
/* EC: projective with point (X,1,Z) (Montgomery) */
/*****************************************************************************/
/* (xout:zout) = (x1:z1) + (x2:z2) */
static void pec_add3(mpz_t xout, mpz_t zout,
mpz_t x1, mpz_t z1,
mpz_t x2, mpz_t z2,
mpz_t xin, mpz_t zin,
mpz_t n, mpz_t u, mpz_t v, mpz_t w)
{
mpz_sub(u, x2, z2);
mpz_add(v, x1, z1);
mpz_mulmod(u, u, v, n, w); /* u = (x2 - z2) * (x1 + z1) % n */
mpz_add(v, x2, z2);
mpz_sub(w, x1, z1);
mpz_mulmod(v, v, w, n, v); /* v = (x2 + z2) * (x1 - z1) % n */
mpz_add(w, u, v); /* w = u+v */
mpz_sub(v, u, v); /* v = u-v */
mpz_mulmod(w, w, w, n, u); /* w = (u+v)^2 % n */
mpz_mulmod(v, v, v, n, u); /* v = (u-v)^2 % n */
mpz_set(u, xin);
mpz_mulmod(xout, w, zin, n, w);
mpz_mulmod(zout, v, u, n, w);
/* 6 mulmods, 6 adds */
}
/* (x2:z2) = 2(x1:z1) */
static void pec_double(mpz_t x2, mpz_t z2, mpz_t x1, mpz_t z1,
mpz_t b, mpz_t n, mpz_t u, mpz_t v, mpz_t w)
{
mpz_add(u, x1, z1);
mpz_mulmod(u, u, u, n, w); /* u = (x1+z1)^2 % n */
mpz_sub(v, x1, z1);
mpz_mulmod(v, v, v, n, w); /* v = (x1-z1)^2 % n */
mpz_mulmod(x2, u, v, n, w); /* x2 = uv % n */
mpz_sub(w, u, v); /* w = u-v = 4(x1 * z1) */
mpz_mulmod(u, b, w, n, z2);
mpz_add(u, u, v); /* u = (v+b*w) mod n */
mpz_mulmod(z2, w, u, n, v); /* z2 = (w*u) mod n */
/* 5 mulmods, 4 adds */
}
#define NORMALIZE(f, u, v, x, z, n) \
mpz_gcdext(f, u, NULL, z, n); \
mpz_mulmod(x, x, u, n, v); \
mpz_set_ui(z, 1);
static void pec_mult(mpz_t a, mpz_t b, mpz_t k, mpz_t n, mpz_t x, mpz_t z)
{
mpz_t u, v, w, x1, x2, z1, z2, r;
int l = -1;
mpz_init(u); mpz_init(v); mpz_init(w);
mpz_init(x1); mpz_init(x2); mpz_init(z1); mpz_init(z2);
mpz_sub_ui(k, k, 1);
mpz_init_set(r, k);
while (mpz_cmp_ui(r, 1) > 0) {
mpz_tdiv_q_2exp(r, r, 1);
l++;
}
mpz_clear(r);
if (mpz_tstbit(k, l)) {
pec_double(x2, z2, x, z, b, n, u, v, w);
pec_add3(x1, z1, x2, z2, x, z, x, z, n, u, v, w);
pec_double(x2, z2, x2, z2, b, n, u, v, w);
} else {
pec_double(x1, z1, x, z, b, n, u, v, w);
pec_add3(x2, z2, x, z, x1, z1, x, z, n, u, v, w);
}
l--;
while (l >= 1) {
if (mpz_tstbit(k, l)) {
pec_add3(x1, z1, x1, z1, x2, z2, x, z, n, u, v, w);
pec_double(x2, z2, x2, z2, b, n, u, v, w);
} else {
pec_add3(x2, z2, x2, z2, x1, z1, x, z, n, u, v, w);
pec_double(x1, z1, x1, z1, b, n, u, v, w);
}
l--;
}
if (mpz_tstbit(k, 0)) {
pec_double(x, z, x2, z2, b, n, u, v, w);
} else {
pec_add3(x, z, x2, z2, x1, z1, x, z, n, u, v, w);
}
mpz_clear(u); mpz_clear(v); mpz_clear(w);
mpz_clear(x1); mpz_clear(x2); mpz_clear(z1); mpz_clear(z2);
}
#endif
/*****************************************************************************/
/* M-R, Lucas, BPSW */
/*****************************************************************************/
int miller_rabin_ui(mpz_t n, UV base)
{
int rval;
mpz_t a;
mpz_init_set_ui(a, base);
rval = miller_rabin(n, a);
mpz_clear(a);
return rval;
}
int miller_rabin(mpz_t n, mpz_t a)
{
mpz_t nminus1, d, x;
UV s, r;
int rval;
{
int cmpr = mpz_cmp_ui(n, 2);
if (cmpr == 0) return 1; /* 2 is prime */
if (cmpr < 0) return 0; /* below 2 is composite */
if (mpz_even_p(n)) return 0; /* multiple of 2 is composite */
}
if (mpz_cmp_ui(a, 1) <= 0)
croak("Base %ld is invalid", mpz_get_si(a));
mpz_init_set(nminus1, n);
mpz_sub_ui(nminus1, nminus1, 1);
mpz_init_set(x, a);
/* Handle large and small bases. Use x so we don't modify their input a. */
if (mpz_cmp(x, n) >= 0)
mpz_mod(x, x, n);
if ( (mpz_cmp_ui(x, 1) <= 0) || (mpz_cmp(x, nminus1) >= 0) ) {
mpz_clear(nminus1);
mpz_clear(x);
return 1;
}
mpz_init_set(d, nminus1);
s = mpz_scan1(d, 0);
mpz_tdiv_q_2exp(d, d, s);
mpz_powm(x, x, d, n);
mpz_clear(d); /* done with a and d */
rval = 0;
if (!mpz_cmp_ui(x, 1) || !mpz_cmp(x, nminus1)) {
rval = 1;
} else {
for (r = 1; r < s; r++) {
mpz_powm_ui(x, x, 2, n);
if (!mpz_cmp_ui(x, 1)) {
break;
}
if (!mpz_cmp(x, nminus1)) {
rval = 1;
break;
}
}
}
mpz_clear(nminus1); mpz_clear(x);
return rval;
}
/* Returns Lucas sequence U_k mod n and V_k mod n defined by P,Q */
void lucas_seq(mpz_t U, mpz_t V, mpz_t n, IV P, IV Q, mpz_t k,
mpz_t Qk, mpz_t t)
{
UV b = mpz_sizeinbase(k, 2);
IV D = P*P - 4*Q;
MPUassert( mpz_cmp_ui(n, 2) >= 0, "lucas_seq: n is less than 2" );
MPUassert( mpz_cmp_ui(k, 0) >= 0, "lucas_seq: k is negative" );
MPUassert( P >= 0 && mpz_cmp_si(n, P) >= 0, "lucas_seq: P is out of range" );
MPUassert( mpz_cmp_si(n, Q) >= 0, "lucas_seq: Q is out of range" );
MPUassert( D != 0, "lucas_seq: D is zero" );
if (mpz_cmp_ui(k, 0) <= 0) {
mpz_set_ui(U, 0);
mpz_set_ui(V, 2);
return;
}
MPUassert( mpz_odd_p(n), "lucas_seq: implementation is for odd n" );
mpz_set_ui(U, 1);
mpz_set_si(V, P);
mpz_set_si(Qk, Q);
if (Q == 1) {
/* Use the fast V method if possible. Much faster with small n. */
mpz_set_si(t, P*P-4);
if (P > 2 && mpz_invert(t, t, n)) {
/* Compute V_k and V_{k+1}, then computer U_k from them. */
mpz_set_si(V, P);
mpz_init_set_si(U, P*P-2);
while (b > 1) {
b--;
if (mpz_tstbit(k, b-1)) {
mpz_mul(V, V, U); mpz_sub_ui(V, V, P); mpz_mod(V, V, n);
mpz_mul(U, U, U); mpz_sub_ui(U, U, 2); mpz_mod(U, U, n);
} else {
mpz_mul(U, V, U); mpz_sub_ui(U, U, P); mpz_mod(U, U, n);
mpz_mul(V, V, V); mpz_sub_ui(V, V, 2); mpz_mod(V, V, n);
}
}
mpz_mul_ui(U, U, 2);
mpz_submul_ui(U, V, P);
mpz_mul(U, U, t);
} else {
/* Fast computation of U_k and V_k, specific to Q = 1 */
while (b > 1) {
mpz_mulmod(U, U, V, n, t); /* U2k = Uk * Vk */
mpz_mul(V, V, V);
mpz_sub_ui(V, V, 2);
mpz_mod(V, V, n); /* V2k = Vk^2 - 2 Q^k */
b--;
if (mpz_tstbit(k, b-1)) {
mpz_mul_si(t, U, D);
/* U: U2k+1 = (P*U2k + V2k)/2 */
mpz_mul_si(U, U, P);
mpz_add(U, U, V);
if (mpz_odd_p(U)) mpz_add(U, U, n);
mpz_fdiv_q_2exp(U, U, 1);
/* V: V2k+1 = (D*U2k + P*V2k)/2 */
mpz_mul_si(V, V, P);
mpz_add(V, V, t);
if (mpz_odd_p(V)) mpz_add(V, V, n);
mpz_fdiv_q_2exp(V, V, 1);
}
}
}
} else {
while (b > 1) {
mpz_mulmod(U, U, V, n, t); /* U2k = Uk * Vk */
mpz_mul(V, V, V);
mpz_submul_ui(V, Qk, 2);
mpz_mod(V, V, n); /* V2k = Vk^2 - 2 Q^k */
mpz_mul(Qk, Qk, Qk); /* Q2k = Qk^2 */
b--;
if (mpz_tstbit(k, b-1)) {
mpz_mul_si(t, U, D);
/* U: U2k+1 = (P*U2k + V2k)/2 */
mpz_mul_si(U, U, P);
mpz_add(U, U, V);
if (mpz_odd_p(U)) mpz_add(U, U, n);
mpz_fdiv_q_2exp(U, U, 1);
/* V: V2k+1 = (D*U2k + P*V2k)/2 */
mpz_mul_si(V, V, P);
mpz_add(V, V, t);
if (mpz_odd_p(V)) mpz_add(V, V, n);
mpz_fdiv_q_2exp(V, V, 1);
mpz_mul_si(Qk, Qk, Q);
}
mpz_mod(Qk, Qk, n);
}
}
mpz_mod(U, U, n);
mpz_mod(V, V, n);
}
static int lucas_selfridge_params(IV* P, IV* Q, mpz_t n, mpz_t t)
{
IV D = 5;
UV Dui = (UV) D;
while (1) {
UV gcd = mpz_gcd_ui(NULL, n, Dui);
if ((gcd > 1) && mpz_cmp_ui(n, gcd) != 0)
return 0;
mpz_set_si(t, D);
if (mpz_jacobi(t, n) == -1)
break;
if (Dui == 21 && mpz_perfect_square_p(n))
return 0;
Dui += 2;
D = (D > 0) ? -Dui : Dui;
if (Dui > 1000000)
croak("lucas_selfridge_params: D exceeded 1e6");
}
if (P) *P = 1;
if (Q) *Q = (1 - D) / 4;
return 1;
}
static int lucas_extrastrong_params(IV* P, IV* Q, mpz_t n, mpz_t t, UV inc)
{
UV tP = 3;
if (inc < 1 || inc > 256)
croak("Invalid lucas parameter increment: %lu\n", (unsigned long)inc);
while (1) {
UV D = tP*tP - 4;
UV gcd = mpz_gcd_ui(NULL, n, D);
if (gcd > 1 && mpz_cmp_ui(n, gcd) != 0)
return 0;
mpz_set_ui(t, D);
if (mpz_jacobi(t, n) == -1)
break;
if (tP == (3+20*inc) && mpz_perfect_square_p(n))
return 0;
tP += inc;
if (tP > 65535)
croak("lucas_extrastrong_params: P exceeded 65535");
}
if (P) *P = (IV)tP;
if (Q) *Q = 1;
return 1;
}
int is_lucas_pseudoprime(mpz_t n, int strength)
{
mpz_t d, U, V, Qk, t;
IV P = 0, Q = 0;
UV s = 0;
int rval;
{
int cmpr = mpz_cmp_ui(n, 2);
if (cmpr == 0) return 1; /* 2 is prime */
if (cmpr < 0) return 0; /* below 2 is composite */
if (mpz_even_p(n)) return 0; /* multiple of 2 is composite */
}
mpz_init(t);
rval = (strength < 2) ? lucas_selfridge_params(&P, &Q, n, t)
: lucas_extrastrong_params(&P, &Q, n, t, 1);
if (!rval) {
mpz_clear(t);
return 0;
}
mpz_init(U); mpz_init(V); mpz_init(Qk);
mpz_init_set(d, n);
mpz_add_ui(d, d, 1);
if (strength > 0) {
s = mpz_scan1(d, 0);
mpz_tdiv_q_2exp(d, d, s);
}
lucas_seq(U, V, n, P, Q, d, Qk, t);
mpz_clear(d);
rval = 0;
if (strength == 0) {
/* Standard checks U_{n+1} = 0 mod n. */
rval = (mpz_sgn(U) == 0);
} else if (strength == 1) {
if (mpz_sgn(U) == 0) {
rval = 1;
} else {
while (s--) {
if (mpz_sgn(V) == 0) {
rval = 1;
break;
}
if (s) {
mpz_mul(V, V, V);
mpz_submul_ui(V, Qk, 2);
mpz_mod(V, V, n);
mpz_mulmod(Qk, Qk, Qk, n, t);
}
}
}
} else {
mpz_sub_ui(t, n, 2);
if ( mpz_sgn(U) == 0 && (mpz_cmp_ui(V, 2) == 0 || mpz_cmp(V, t) == 0) ) {
rval = 1;
} else {
s--; /* The extra strong test tests r < s-1 instead of r < s */
while (s--) {
if (mpz_sgn(V) == 0) {
rval = 1;
break;
}
if (s) {
mpz_mul(V, V, V);
mpz_sub_ui(V, V, 2);
mpz_mod(V, V, n);
}
}
}
}
mpz_clear(Qk); mpz_clear(V); mpz_clear(U); mpz_clear(t);
return rval;
}
int is_prob_prime(mpz_t n)
{
/* Step 1: Look for small divisors. This is done purely for performance.
* It is *not* a requirement for the BPSW test. */
/* If less than 1009, make trial factor handle it. */
if (mpz_cmp_ui(n, BGCD_NEXTPRIME) < 0)
return trial_factor(n, 2, BGCD_LASTPRIME) ? 0 : 2;
/* Check for tiny divisors (GMP can do these really fast) */
if ( mpz_even_p(n)
|| mpz_divisible_ui_p(n, 3)
|| mpz_divisible_ui_p(n, 5) ) return 0;
/* Do a big GCD with all primes < 1009 */
{
mpz_t t;
mpz_init(t);
mpz_gcd(t, n, _bgcd);
if (mpz_cmp_ui(t, 1) != 0) { mpz_clear(t); return 0; }
mpz_clear(t);
}
/* No divisors under 1009 */
if (mpz_cmp_ui(n, BGCD_NEXTPRIME*BGCD_NEXTPRIME) < 0)
return 2;
/* Step 2: The BPSW test. psp base 2 and slpsp. */
/* Miller Rabin with base 2 */
if (miller_rabin_ui(n, 2) == 0)
return 0;
/* Extra-Strong Lucas test */
if (is_lucas_pseudoprime(n, 2 /*extra strong*/) == 0)
return 0;
/* BPSW is deterministic below 2^64 */
if (mpz_sizeinbase(n, 2) <= 64)
return 2;
return 1;
}
/* These primorial and trial factor functions are really slow for numerous
* reasons, but most of all because mpz_nextprime is dog slow. We don't
* really use them, so don't worry about it too much. */
void GMP_pn_primorial(mpz_t prim, UV n)
{
mpz_t p;
mpz_init_set_ui(p, 2);
mpz_set_ui(prim, 1);
while (n--) {
mpz_mul(prim, prim, p);
mpz_nextprime(p, p);
}
mpz_clear(p);
}
UV trial_factor(mpz_t n, UV from_n, UV to_n)
{
mpz_t p;
UV f = 0;
if (mpz_cmp_ui(n, 4) < 0)
return (mpz_cmp_ui(n, 1) <= 0) ? 1 : 0; /* 0,1 => 1 2,3 => 0 */
if (from_n <= 2 && to_n >= 2 && mpz_even_p(n) ) return 2;
else if (from_n <= 3 && to_n >= 3 && mpz_divisible_ui_p(n, 3)) return 2;
if (from_n < 5)
from_n = 5;
if (from_n > to_n)
return 0;
mpz_init(p);
mpz_sqrt(p, n);
if (mpz_cmp_ui(p, to_n) < 0)
to_n = mpz_get_ui(p); /* limit to_n to sqrtn */
mpz_set_ui(p, from_n-1);
mpz_nextprime(p, p); /* Set p to the first prime >= from_n */
while (mpz_cmp_ui(p, to_n) <= 0) {
if (mpz_divisible_p(n, p)) {
f = mpz_get_ui(p);
break;
}
mpz_nextprime(p, p);
}
mpz_clear(p);
return f;
}
/*****************************************************************************/
/* Proof verification */
/*****************************************************************************/
/* What each of these does is verify:
* Assume Q is prime.
* Then N is prime based on the proof given.
* We verify any necessary conditions on Q (e.g. it must be odd, or > 0, etc.
* but do not verify Q prime. That is done in another proof step.
*/
/* ECPP using N, A, B, M, Q, X, Y
*
* A.O.L. Atkin and F. Morain, "Elliptic Curves and primality proving"
* Mathematics of Computation, v61, 1993, pages 29-68.
* http://www.ams.org/journals/mcom/1993-61-203/S0025-5718-1993-1199989-X/
*
* Page 10, Theorem 5.2:
* "Let N be an integer prime to 6, E an elliptic curve over Z/NZ, together
* with a point P on E and m and s two integers with s | m. For each prime
* divisor q of s, we put (m/q)P = (x_q : y_q : z_q). We assume that
* mP = O_E and gcd(z_q,N) = 1 for all q. Then, if p is a prime divisor
* of N, one has #E(Z/pZ) = 0 mod s."
* Page 10, Corollary 5.1:
* "With the same conditions, if s > (N^(1/4) + 1)^2, then N is prime."
*
* Basically this same result is repeated in Crandall and Pomerance 2005,
* Theorem 7.6.1 "Goldwasser-Kilian ECPP theorem".
*
* Wikipedia, "Elliptic curve primality testing":
* "Let N be a positive integer, and E be the set which is defined by the
* equation y^2 = x^3 + ax + b (mod N). Consider E over Z/NZ, use the
* usual addition law on E, and write O for the neutral element on E.
* Let m be an integer. If there is a prime q which divides m, and is
* greater than (N^(1/4) + 1)^2 and there exists a point P on E such that
* (1) mP = O, (2) (m/q)P is defined and not equal to O, then N is prime."
*
* We use the restricted form as stated by Wikipedia and used in the
* Atkin/Morain ECPP algorithm, where s is a prime (hence the "for each prime
* divisor q of s" of the general theorem is just s).
*/
void verify_ecpp(void) {
mpz_mod(A, A, N);
mpz_mod(B, B, N);
if (mpz_cmp_ui(N, 0) <= 0) quit_invalid("ECPP", "N > 0");
if (mpz_gcd_ui(NULL, N, 6) != 1) quit_invalid("ECPP", "gcd(N, 6) = 1");
mpz_mul(T1, A, A);
mpz_mul(T1, T1, A);
mpz_mul_ui(T1, T1, 4);
mpz_mul(T2, B, B);
mpz_mul_ui(T2, T2, 27);
mpz_add(T1, T1, T2);
mpz_gcd(T1, T1, N);
if (mpz_cmp_ui(T1, 1) != 0) quit_invalid("ECPP", "gcd(4*a^3 + 27*b^2, N) = 1");
mpz_mul(T1, X, X);
mpz_add(T1, T1, A);
mpz_mul(T1, T1, X);
mpz_add(T1, T1, B);
mpz_mod(T1, T1, N);
mpz_mul(T2, Y, Y);
mpz_mod(T2, T2, N);
if (mpz_cmp(T1, T2) != 0) quit_invalid("ECPP", "Y^2 = X^3 + A*X + B mod N");
mpz_mul_ui(T2, N, 4);
mpz_sqrt(T2, T2);
mpz_add_ui(T1, N, 1);
mpz_sub(T1, T1, T2);
if (mpz_cmp(M, T1) < 0) quit_invalid("ECPP", "M >= N + 1 - 2*sqrt(N)");
mpz_add_ui(T1, N, 1);
mpz_add(T1, T1, T2);
if (mpz_cmp(M, T1) > 0) quit_invalid("ECPP", "M <= N + 1 + 2*sqrt(N)");
mpz_root(T1, N, 4);
mpz_add_ui(T1, T1, 1);
mpz_mul(T1, T1, T1);
if (mpz_cmp(Q, T1) <= 0) quit_invalid("ECPP", "Q > (N^(1/4)+1)^2");
if (mpz_cmp(Q, N) >= 0) quit_invalid("ECPP", "Q < N");
/* While M = Q is odd to compute in a proof, it is allowed.
* In Primo terms, this means S=1 is allowed.
* if (mpz_cmp(M, Q) == 0) quit_invalid("ECPP", "M != Q");
*/
if (!mpz_divisible_p(M, Q)) quit_invalid("ECPP", "Q divides M");
{
#if USE_AFFINE_EC
struct ec_affine_point P0, P1, P2;
mpz_init_set(P0.x, X); mpz_init_set(P0.y, Y);
mpz_init(P1.x); mpz_init(P1.y);
mpz_init(P2.x); mpz_init(P2.y);
mpz_divexact(T1, M, Q);
if (ec_affine_multiply(A, T1, N, P0, &P2, T2))
quit_invalid("ECPP", "Factor found for N");
/* Check that P2 is not (0,1) */
if (mpz_cmp_ui(P2.x, 0) == 0 && mpz_cmp_ui(P2.y, 1) == 0)
quit_invalid("ECPP", "(M/Q) * EC(A,B,N,X,Y) is not identity");
mpz_set(T1, Q);
if (ec_affine_multiply(A, T1, N, P2, &P1, T2))
quit_invalid("ECPP", "Factor found for N");
/* Check that P1 is (0, 1) */
if (! (mpz_cmp_ui(P1.x, 0) == 0 && mpz_cmp_ui(P1.y, 1) == 0) )
quit_invalid("ECPP", "M * EC(A,B,N,X,Y) is identity");
mpz_clear(P0.x); mpz_clear(P0.y);
mpz_clear(P1.x); mpz_clear(P1.y);
mpz_clear(P2.x); mpz_clear(P2.y);
#else
mpz_t PX, PY, PA, PB;
mpz_init(PX); mpz_init(PY); mpz_init(PA); mpz_init(PB);
/* We have A,B,X,Y in affine coordinates, for the curve:
* Y^2 = X^3 + AX + B
* and want to turn this into points on a Montgomery curve:
* by^2 = x^3 + ax^2 + x
* so we can use the much faster (~4x) multiplication routines.
* The inverse of this operation is:
* X = (3x+a)/3b
* Y = y/b
* A = (3-a^2)/(3b^2)
* B = (2a^3-9a)/27b^3
* In our case we need to do the harder job of going the other direction.
*/
/* Make Montgomery variables from affine (TODO: make this work) */
mpz_add(PB, X, A);
mpz_mul(PB, PB, X);
mpz_add_ui(PB, PB, 1);
mpz_mul(PB, PB, X);
mpz_mod(PB, PB, N);
mpz_mul_ui(T2, PB, 3);
mpz_mul(T2, T2, PB);
mpz_mod(T2, T2, N);
mpz_gcdext(T2, T1, NULL, T2, N); /* T1 = 1/3g^2 */
if (mpz_cmp_ui(T2,1) != 0) quit_invalid("ECPP", "Factor found during gcd");
mpz_mul_ui(PX, X, 3);
mpz_add(PX, PX, A);
mpz_mul(PX, PX, PB);
mpz_mul(PX, PX, T1);
mpz_mod(PX, PX, N);
mpz_set(PY, Y);
mpz_mul_ui(PY, PY, 3);
mpz_mul(PY, PY, PB);
mpz_mul(PY, PY, T1);
mpz_mod(PY, PY, N); /* y = (3gY)/(3g^2) = Y/g */
mpz_mul(PA, A, A);
mpz_sub_ui(PA, PA, 3);
mpz_neg(PA, PA);
mpz_mul(PA, PA, T1);
mpz_mod(PA, PA, N);
mpz_divexact(T1, M, Q);
pec_mult(PA, PB, T1, N, PX, PY);
/* Check that point is not (0,0) */
if (mpz_cmp_ui(PX, 0) == 0 && mpz_cmp_ui(PY, 0) == 0)
quit_invalid("ECPP", "(M/Q) * EC(A,B,N,X,Y) is not identity");
mpz_set(T1, Q);
pec_mult(PA, PB, T1, N, PX, PY);
/* Check that point is (0, 0) */
if (! (mpz_cmp_ui(PX, 0) == 0 && mpz_cmp_ui(PY, 0) == 0) )
quit_invalid("ECPP", "M * EC(A,B,N,X,Y) is identity");
mpz_clear(PX); mpz_clear(PY); mpz_clear(PA); mpz_clear(PB);
#endif
}
}
/* Basic N+1 using N, Q, LP, LQ
*
* John Brillhart, D.H. Lehmer, J.L. Selfridge,
* "New Primality Criteria and Factorizations of 2^m +/- 1"
* Mathematics of Computation, v29, n130, April 1975, pp 620-647.
* http://www.ams.org/journals/mcom/1975-29-130/S0025-5718-1975-0384673-1/S0025-5718-1975-0384673-1.pdf
*
* Page 631, Theorem 15:
* "Let N+1 = mq, where q is an odd prime such that 2q-1 > sqrt(N).
* If there exists a Lucas sequence {V_k} of discriminant D with
* (D|N) = -1 for which N|V_{(N+1)/2}, but N∤V_{m/2}, then N is prime."
*/
void verify_bls15(void) {
if (mpz_even_p(Q)) quit_invalid("BLS15", "Q odd");
if (mpz_cmp_ui(Q, 2) <= 0) quit_invalid("BLS15", "Q > 2");
mpz_add_ui(T2, N, 1);
if (!mpz_divisible_p(T2, Q)) quit_invalid("BLS15", "Q divides N+1");
mpz_divexact(M, T2, Q);
mpz_mul(T1, M, Q);
mpz_sub_ui(T1, T1, 1);
if (mpz_cmp(T1, N) != 0) quit_invalid("BLS15", "MQ-1 = N");
if (mpz_cmp_ui(M, 0) <= 0) quit_invalid("BLS15", "M > 0");
mpz_mul_ui(T1, Q, 2);
mpz_sub_ui(T1, T1, 1);
mpz_sqrt(T2, N);
if (mpz_cmp(T1, T2) <= 0) quit_invalid("BLS15", "2Q-1 > sqrt(N)");
mpz_mul(T1, LP, LP);
mpz_mul_ui(T2, LQ, 4);
mpz_sub(T1, T1, T2);
if (mpz_sgn(T1) == 0) quit_invalid("BLS15", "D != 0");
if (mpz_jacobi(T1, N) != -1) quit_invalid("BLS15", "jacobi(D,N) = -1");
{
mpz_t U, V, k;
IV iLP, iLQ;
mpz_init(U); mpz_init(V); mpz_init(k);
iLP = mpz_get_si(LP);
iLQ = mpz_get_si(LQ);