-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA_numbers_factored.gp
1797 lines (1704 loc) · 61 KB
/
RSA_numbers_factored.gp
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
\\ RSA_numbers_factored.gp
\\
\\ RSA number n = RSA-l:
\\ ```
\\ RSA_unfactored: [l,n]
\\ RSA_factored: [l,n,p,q] (n = p * q)
\\ RSA_factored_2: [l,n,p,q,pm1,qm1] (n = p * q, Xm1 factorization dict of X-1)
\\ ```
\\
\\ v1.11
\\ - add RSA.svg(), rewite RSA_svg demos
\\ - make validate() functions to enable/disable output
\\ - add RSA.sort_factors(), new demos
\\ - complete Python doc for sections 4+5, new section 6
\\ - functional parity for Python, JavaScript/nodejs and PARI/GP implementations
\\ - add RSA.unfactored(mod4=-1)
\\ - add to_sqrtm1()
\\ - add to_squares_sum()
\\ - add p-1/q-1 factorization dictionaries for RSA-230..RSA-250 (.py/.js/.gp)
\\ - make use of cypari2 if available, initially for using ".halfgcd(()"
\\ - improve doc
\\ - new RSA_svg.py demo
\\ - improve markdown
\\
\\ v1.10
\\ - add uniq arg to RSA().square_sums()
\\ - add smp1m4 list of primes =1 (mod 4) less than 1000
\\ - add sqtst()
\\ - add lazydocs doc with Makefile fixing Example[s] bugs, docstrings up to and including SECTION03
\\ - add sq2d()
\\ - add MicroPython version
\\ - add square_sums_4()
\\ - avoid redundant return type in "Returns:", now lazydocs Makefile handles that
\\ - completed documentation, fix small typos, correct some hinting types, new examples
\\ - Todo: show lazydocs class member functions as "method" and not as "function"
\\ - New makefile doc|pylint|validate targets
\\ - "make pylint" clean (top line disable and pylint options)
\\ - make sure every function/method gets called at least once in validation
\\ - pylint warning related simplifications
\\ - "validate" target to compare with "validate.good", new "black" target
\\ - code formatting now with "black", results in need for --max-module-lines=2500
\\ - new "doc_diff" target
\\ - blacked Pydroid3 demo, then made pylint clean
\\ - updated both README.md, added table of contents
\\
\\ v1.9
\\ - remove not needed anymore RSA(). \\_\\_init\\_\\_()
\\ - add RSA().square_sums()
\\ - manual transpilation to RSA_numbers_factored.js
\\ - new home in RSA_numbers_factored repo python directory
\\ - gist now is pointer to new home only
\\ - add HTML demos making use of transpiled RSA_numbers_factored.js
\\
\\ v1.8
\\ - include Robin Chapman code to determine prime p=1 (mod 4) sum of squares
\\ - make few changes, documented in that code section
\\ - make square_sum_prod base on sq2, eliminate subprocess.Popen()
\\ - remove RSA().square_sum_prod because not needed anymore
\\ - remove Popen and Pipe import
\\
\\ v1.7
\\ - add "mod4" attribute to "has_factors()" and "RAS().factored()"
\\ - default None selects all
\\ - int value specifies remainder "mod 4" to be selected
\\ - tuple specifies remainders "mod 4" for prime factores to be selected
\\ - remove "has_factors_1_1(_)", use "has_factors(_, mod4=(1,1))" instead
\\ - remove "RSA().factored_1_1()", use "RSA().factored(mod4=(1,1))"
\\ - add "RSA().square_diffs()" for returning two pairs
\\
\\ v1.6
\\ - enable square_sum_prod() functions to deal with primefactors in list
\\ - add asserts enforcing "=1 (mod 4)" for square_sum_prod() functions
\\ - add has_factors_1_1() [returns whether both primefactors are "=1 (mod4)"]
\\ - add RSA().factored_1_1() based on that
\\
\\ v1.5
\\ - add square_sums() for converting square_sum_prod output of composite number
\\ - add square_sums() assertions
\\
\\ v1.4
\\ - add RSA.square_sum_prod(), using Popen() pipe for >1 calls
\\
\\ v1.3
\\ - add square_sum_prod(), for use see:
\\ - https://github.com/Hermann-SW/square_sum_prod/blob/master/Popen.py
\\
\\ v1.2
\\ - improve has_factors() and has_factors_2()
\\ - correct RSA().factored() to always return 4-tuples
\\
\\ v1.1
\\ - removed not needed imports
\\ - added has_factors/has_factors_2 functions and used them everywwhere
\\ - resolved the RSA-190 assertion issue, using reduced_ works for all
\\ - added RSA convenience class
\\ - added some RSA class assertions for validation
\\ - RSA class factored() returns all RSA number tupels having factors
\\ - RSA class factored_2() returns all RSA number tupels p-1/q-1 factorizations
\\
\\ v1.0
\\ - added primeprod_ functions
\\ - added factorization dictionaries for (p-1) and (q-1) of RSA-59 ... RSA-220
\\ - added Wikipedia RSA numbers that have not been factored sofar as well
\\ - added dict_ functions
\\ - added dictprod_ functions
\\ - added dict_totient and dictprod_totient assertions [ mod phi(phi(n)) ]
\\ - added comments
\\
\\ v0.2
\\ - added ind(rsa, x) function, returning index in rsa of RSA-x number
\\
\\ v0.1
\\ - initial version, with bits(), digits(), rsa list and main() testing
\\
\\
\\ Global variable descriptions:
\\ - small primes =1 (mod 4) less than 1000
\\ - list of RSA numbers
\\
\\ from math import log2, log10
\\ from itertools import combinations, chain
\\ from typing import Tuple, List, Union, Dict, NewType, Type
\\ from sympy.ntheory import isprime
\\ from sympy import lcm
\\
assert(b,v="",s="")=
{
if(!(b),
error(Str(v)" "Str(s)));
}
len(v)=#v;
last(v)=v[#v];
jacobi_symbol(a,n)=kronecker(a,n);
bits(N)=#binary(N);
digits_(N)=#digits(N);
\\ Robert Chapman 2010 code from https://math.stackexchange.com/a/5883/1084297
\\ with small changes:
\\ - asserts instead bad case returns
\\ - renamed root4() to root4m1() indicating which 4th root gets determined
\\ - made sq2() return tuple with positive numbers; before sq2(13) returned (-3,-2)
\\ - sq2(p) result can be obtained from sympy.solvers.diophantine.diophantine by diop_DN(-1, p)[0]
\\
mods(a,n)=
{
\\ """returns "signed" a (mod n), in range -n//2..n//2"""
assert(n>0,n," <=0");
a=a%n;
if(2*a>n,
return(a-n));
return(a);
}
powmods(a,r,n)=
{
\\ """returns "signed" a**r (mod n), in range -n//2..n//2"""
my(out=1);
while(r>0,
if(r%2==1,
r-=1;out=mods(out*a, n););
r=floor(r/2);
a=mods(a*a, n););
out;
}
quos(a,n)=
{
\\ """returns equivalent of "a//n" for signed mod"""
assert(n>0,n," <= 0");
floor((a-mods(a, n))/n);
}
grem(w,z)=
{
\\ """returns remainder in Gaussian integers when dividing w by z"""
my(w0,w1,z0,z1,n);
[w0,w1]=w;
[z0,z1]=z;
n=z0*z0+z1*z1;
assert(n>0,"division by ",n);
u0=quos(w0*z0+w1*z1,n);
u1=quos(w1*z0-w0*z1,n);
[w0-z0*u0+z1*u1, w1-z0*u1-z1*u0];
}
ggcd(w,z)=
{
\\ """returns greatest common divisor for gaussian integers"""
while(z!=[0,0],
[w,z]=[z, grem(w,z)];);
w;
}
root4m1(p)=
{
\\ """returns sqrt(-1) (mod p)"""
assert(p>1&&p%4==1,p," not 1 (mod 4)");
my(k=p\4,j=2,a,b);
while(1,
a=powmods(j,k,p);
b=mods(a*a,p);
if(b==-1,
return(a););
assert(b==1,p," not prime");
j+=1;);
}
sq2(p)=
{
\\ """determine pair of numbers, their squares summing up to p"""
assert(p>1&&p%4==1,p," not 1 (mod 4)");
my(a=root4m1(p),x,y);
[x,y]=ggcd([p,0],[a,1]);
[abs(x),abs(y)];
}
\\ Functions dealing with representations of int as sum of two squares
\\
sq2d(p)=
{
\\ """determine pair of numbers, their squares difference being p"""
assert(p>1&&isprime(p),p," not prime");
[1+p\2,p\2];
}
square_sum_prod(n)=
{
\\ """
\\ Args:
\\ n: int or RSA_number.
\\ Returns:
\\ _: int list with squares of pairs of ints sum up to prime, prime[s] multiply to n.
\\ Example:
\\ For prime 233 and composite number RSA-59.
\\ ```
\\ ? square_sum_prod(233)
\\ [13, 8]
\\ ?
\\ ? r=rsa[1];
\\ ? s=square_sum_prod(r);
\\ ? (s[1]^2+s[2]^2)*(s[3]^2+s[4]^2)==r[2]
\\ 1
\\ ?
\\ ```
\\ """
if(type(n)=="t_VEC",
L=square_sum_prod(n[3]);
return(concat(L,square_sum_prod(n[4]))));
return(sq2(n));
}
square_sums_(s)=
{
\\ """
\\ Args:
\\ s: List of int returned by square_sum_prod(n).
\\ Returns:
\\ _: List of int pairs, their squares summing up to n.
\\ Example:
\\ For composite number RSA-59.
\\ ```
\\ ? r=rsa[1];
\\ ? s=square_sum_prod(r);
\\ ? square_sums_(s)
\\ [[93861205413769670113229603198, 250662312444502854557140314865], [264836754409721537369435955610, 38768728061109707828243001823]]
\\ ? foreach(square_sums_(s),v,[a,b]=v;print(a^2+b^2==r[2]))
\\ 1
\\ 1
\\ ?
\\ ```
\\ """
if(#s==2,
return([s]));
my([a,b]=s[#s-1..#s],r=List(),L,S);
foreach(square_sums_(s[1..#s-2]),p,
\\ Brahmagupta–Fibonacci identity
listput(r,[abs(a*p[1]-b*p[2]),a*p[2]+b*p[1]]);
listput(r,[a*p[1]+b*p[2],abs(b*p[1]-a*p[2])]));
return([x|x<-r]);
}
square_sums(L,revt=0,revl=0,uniq=0)=
{
\\ """
\\ Args:
\\ L: List of int.
\\ revt: sorting direction for tuples.
\\ revl: sorting direction for list.
\\ uniq: eliminate duplicates if True.
\\ Returns:
\\ _: square_sums_(l) sorted (tuples and list), optionally with duplicates removed.
\\ Example:
\\ For list corresponding to number
\\ 5\\*5\\*13 (5 = 2\\*\\*2 + 1\\*\\*2, 13 = 3\\*\\*2 + 2\\*\\*2).
\\ ```
\\ ? s=[2,1,2,1,3,2];
\\ ? square_sums(s)
\\ [[1, 18], [6, 17], [10, 15], [10, 15]]
\\ ? square_sums(s,1,1)
\\ [[18, 1], [17, 6], [15, 10], [15, 10]]
\\ ? square_sums(s,,,1)
\\ [[1, 18], [6, 17], [10, 15]]
\\ ? foreach(square_sums(s,,,1),v,[a,b]=v;assert(a^2+b^2==5*5*13))
\\ ?
\\ ```
\\ """
my(r=square_sums_(L));
r=[vecsort(t,,4*revt)|t<-r];
r=vecsort(r,1,4*revl+8*uniq);
return(r);
}
{ \\ [x|x<-[2..1000],x%4==1&&isprime(x)]
smp1m4=[
5,
13,
17,
29,
37,
41,
53,
61,
73,
89,
97,
101,
109,
113,
137,
149,
157,
173,
181,
193,
197,
229,
233,
241,
257,
269,
277,
281,
293,
313,
317,
337,
349,
353,
373,
389,
397,
401,
409,
421,
433,
449,
457,
461,
509,
521,
541,
557,
569,
577,
593,
601,
613,
617,
641,
653,
661,
673,
677,
701,
709,
733,
757,
761,
769,
773,
797,
809,
821,
829,
853,
857,
877,
881,
929,
937,
941,
953,
977,
997];
}
sqtst(L,k,dbg=0)=
{
\\ """
\\ Note:
\\ sqtst() verifies that 2**(k-1) == unique #sum_of_squares by many
\\ asserts for all k-element subsets of l
\\ Args:
\\ L: list of distinct primes =1 (mod 4)
\\ k: size of subsets
\\ dbg: 0=without debug output, 1-3 with more and more
\\ Example:
\\ ```
\\ ? smp1m4[1..3]
\\ [5, 13, 17]
\\ ? sqtst(smp1m4[1..3],2,dbg=3)
\\ Vecsmall([1, 2])
\\ [2, 1, 3, 2]
\\ [[1, 8], [4, 7]]
\\ Vecsmall([1, 3])
\\ [2, 1, 4, 1]
\\ [[2, 9], [6, 7]]
\\ Vecsmall([2, 3])
\\ [3, 2, 4, 1]
\\ [[5, 14], [10, 11]]
\\ ? sqtst(smp1m4[1..20],7)
\\ ?
\\ ```
\\ """
assert(#L>=k);
my(LS,S);
forsubset([#L,k],s,
LS=concat([sq2(L[x])|x<-s]);
S=square_sums(LS,,,1);
if(dbg>=1,
if(dbg>=3,
print(s));
if(dbg>=2,
print(LS));
print(S));
assert(2^(k-1)==#S));
}
to_squares_sum(sqrtm1,p)=
{
\\ """much faster in case cypari2 is available
\\ Args:
\\ sqrtm1: sqrt(-1) (mod p).
\\ p: prime p =1 (mod 4).
\\ Returns:
\\ _: sum of squares for p.
\\ Example:
\\ ```
\\ >>> to_squares_sum(11, 61)
\\ (6, -5)
\\ >>>
\\ ```
\\ """
[M,V]=halfgcd(sqrtm1,p);
return([V[2],M[2, 1]]);
}
to_sqrtm1(xy,p)=
{
\\ """
\\ Args:
\\ xy: xy[0]**2 + xy[1]**2 == p.
\\ p: prime p =1 (mod 4).
\\ Returns:
\\ _: sqrt(-1) (mod p).
\\ Example:
\\ ```
\\ >>> to_sqrtm1((6, -5), 61)
\\ 11
\\ >>>
\\ ```
\\ """
return(lift(Mod(xy[1],p)/xy[2]));
}
\\ Functions working on "rsa" list
\\
idx(rsa_,L)=
{
\\ """
\\ Args:
\\ rsa_: list of RSA numbers
\\ L: bit-length or decimal-digit-length of RSA number
\\ Returns:
\\ _: index of RSA-l in rsa list, -1 if not found
\\ """
for(i=1,#rsa_,
if(rsa_[i][1]==L,
return(i)));
return(-1);
}
has_factors(r,mod4=-1)=
{
\\ """
\\ Args:
\\ r: an RSA number
\\ mod4: optional restriction (remainder mod 4 for number or its both prime factors)
\\ Returns:
\\ _: RSA number has factors and adheres mod 4 restriction(s)
\\ """
return(#r>=4 && (
mod4==-1
|| (type(mod4)=="t_INT"&&r[2]%4==mod4)
|| (type(mod4)=="t_VEC"&&r[3]%4==mod4[1]&&r[4]%4==mod4[2])));
}
has_factors_2(r,mod4=-1)=
{
\\ """
\\ Args:
\\ r: an RSA number
\\ mod4: optional restriction (remainder mod 4 for number or its both prime factors)
\\ Returns:
\\ _: RSA number has factors p and q, and factorization dictionaries of p-1 and q-1
\\ Example:
\\ For RSA-100
\\ ```
\\ ? r=rsa[3];
\\ ? has_factors_2(r)
\\ 1
\\ ? [l,n,p,q,pm1,qm1]=r;
\\ ? l
\\ 100
\\ ? q
\\ 40094690950920881030683735292761468389214899724061
\\ ? qm1
\\
\\ [ 2 2]
\\
\\ [ 5 1]
\\
\\ [ 41 1]
\\
\\ [ 2119363 1]
\\
\\ [ 602799725049211 1]
\\
\\ [38273186726790856290328531 1]
\\
\\ ?
\\ ```
\\ """
return(#r>=6 && (
mod4==-1
|| (type(mod4)=="t_INT"&&r[2]%4==mod4)
|| (type(mod4)=="t_VEC"&&r[3]%4==mod4[1]&&r[4]%4==mod4[2])));
}
without_factors(r,mod4=-1)=
{
\\ """
\\ Args:
\\ r: an RSA number
\\ Returns:
\\ _: RSA number has no known factors p and q
\\ """
return(#r==2 && (
mod4==-1
|| (type(mod4)=="t_INT"&&r[2]%4==mod4)));
}
\\ primeprod_f functions, passing p and q instead n=p*q much faster than sympy.f
\\
primeprod_totient(p,q)=
{
\\ """
\\ Args:
\\ p,q: odd primes.
\\ Returns:
\\ _: totient(n) with n=p*q.
\\ """
return((p-1)*(q-1));
}
primeprod_reduced_totient(p,q)=
{
\\ """
\\ Args:
\\ p,q: odd primes.
\\ Returns:
\\ _: reduced_totient(n) with n=p*q.
\\ """
return(lcm(p-1,q-1));
}
\\ Functions on factorization dictionaries.
\\ [as returned by sympy.factorint() (in rsa[x][5] for p-1 and rsa[x][6] for q-1) ]
\\
dict_int(d) =
{
\\ """
\\ Args:
\\ d: factorization dictionary.
\\ Returns:
\\ _: n with d = sympy.factorint(n).
\\ """
my(p=1);
foreach(mattranspose(d),v,
p*=v[1]^v[2]);
return(p);
}
dict_totient(d)=
{
\\ """
\\ Args:
\\ d: factorization dictionary.
\\ Returns:
\\ _: totient(n) with d = sympy.factorint(n).
\\ """
my(p=1);
foreach(mattranspose(d),v,
p*=(v[1]-1)*(v[1]^(v[2]-1)));
return(p);
}
\\ functions on pair of factorization dictionaries
\\
dictprod_totient(d1,d2)=
{
\\ """
\\ Args:
\\ d1,d2: factorization dictionaries.
\\ Returns:
\\ _: totient(n) with n=dict_int(d1)*dict_int(d2).
\\ """
return(dict_totient(d1)*dict_totient(d2));
}
dictprod_reduced_totient(d1,d2)=
{
\\ """
\\ Args:
\\ d1,d2: factorization dictionaries.
\\ Returns:
\\ _: reduced_totient(n) with n=dict_int(d1)*dict_int(d2).
\\ """
return(lcm(dict_totient(d1),dict_totient(d2)));
}
\\ Validation functions, rsa list
\\
validate_squares() =
{
my(s,p,L);
s=[2,1,3,2,4,1]; \\ 1105 = 5 * 13 * 17 = (2² + 1²) * (3² + 2²) * (4² + 1²)
p=1;
for(j=1,#s,
p*=s[j]^2+s[j+1]^2;j+=1);
L=square_sums_(s);
foreach(L,t,
assert(t[1]^2+t[2]^2==p));
L=square_sums(s); \\ [[4, 33], [9, 32], [12, 31], [23, 24]]
foreach(L,t,
assert(t[1]^2+t[2]^2==p);
assert(t[1]<t[2]));
for(j=1,#L-1,
assert(L[j][1]<L[j+1][1]));
L=square_sums(s,1,1);
foreach(L,t,
assert(t[1]^2+t[2]^2==p);
assert(t[1]>t[2]));
for(j=1,#L-1,
assert(L[j][1]>L[j+1][1]));
sqtst(smp1m4[11..20],8);
s=square_sum_prod(rsa[1]);
assert((s[1]^2+s[2]^2)*(s[3]^2+s[4]^2)==rsa[1][2]);
assert(sq2d(257)[1]^2-sq2d(257)[2]^2==257);
assert(sq2(100049)[1]^2+sq2(100049)[2]^2==100049);
}
validate(rsa_,doprint=0) =
{
\\ Assert many identities to assure data consistency and generate demo
\\ output for non RSA-class functionality.
\\
my(br,r,L,n,p,q,pm1,qm1,k,isprimeflag=0);
/*
sympy.ntheory.primetest.isprime(n) is different+faster than GP isprime().
isprime(x,{flag=0}): true(1) if x is a (proven) prime number, false(0) if not.
If flag is 0 or omitted, use a combination of algorithms. If flag is 1, the
primality is certified by the Pocklington-Lehmer Test. If flag is 2, the
primality is certified using the APRCL test. If flag is 3, use ECPP.
*/
if(doprint,
print("\nwith p-1 and q-1 factorizations (n=p*q): ",#[""|r<-rsa_,#r==6]));
br=0;
assert(#[""|r<-rsa_,#r==6]==25);
for(i=1,#rsa_,r=rsa_[i];
if(has_factors_2(r),
[L,n,p,q,pm1,qm1]=r,
if(has_factors(r),
[L,n,p,q]=r,
[L,n]=r));
assert(L==digits_(n)||L==bits(n));
if(i>1,
assert(n>rsa_[i-1][2]));
if(has_factors(r),
assert(n==p*q);
assert(isprime(p,isprimeflag));
assert(isprime(q,isprimeflag));
assert(Mod(997,n)^primeprod_totient(p,q)==Mod(1,n));
assert(Mod(997,n)^primeprod_reduced_totient(p, q)==Mod(1,n)));
if(has_factors_2(r),
foreach(pm1[,1],k,
assert(isprime(k,isprimeflag)));
foreach(qm1[,1],k,
assert(isprime(k,isprimeflag)));
assert(dict_int(pm1)==p-1);
assert(dict_int(qm1)==q-1);
assert(Mod(997,p-1)^dict_totient(pm1)==Mod(1,p-1));
assert(Mod(997,q-1)^dict_totient(qm1)==Mod(1,q-1));
assert (
Mod(65537,primeprod_reduced_totient(p,q))^
dictprod_reduced_totient(pm1,qm1)
==Mod(1,primeprod_reduced_totient(p,q))
);
\\ this does only work for RSA number != RSA-190
if(L!=190,
assert(
Mod(65537,primeprod_totient(p,q))^dictprod_totient(pm1,qm1)==Mod(1,primeprod_totient(p,q)))));
if(!has_factors_2(r)&&has_factors_2(rsa_[i-1]),
if(doprint,
print(
"\n\nwithout (p-1) and (q-1) factorizations, but p and q: ",
#[""|r<-rsa_,#r==4]));
br=4;
assert(#[""|r<-rsa_,#r==4]==0));
if(!has_factors(r)&&has_factors(rsa_[i-1]),
if(doprint,
print(
"\nhave not been factored sofar: ",
#[""|r<-rsa_,#r==2]));
br = 4;
assert(#[""|r<-rsa_,#r==2]==31));
if(doprint,
printf("%3d %s%s%s",L,
if(L==bits(n),"bits ","digits"),
if(i<#rsa_,",",concat(concat("(=",digits_(rsa_[i][2]))," digits)\n")),
if(i%7==br||i==#rsa_,"\n",""));
));
validate_squares();
foreach(rsa_,r,
if (#r>4,
assert(dict_int(r[5])==r[3]-1,r[5],"pm1_wrong");
assert(dict_int(r[6])==r[4]-1,r[6],"qm1_wrong")));
}
\\ rsa list entries of form (n=p*q):
\\ x, RSA-x = n [, p, q [, (p-1), (q-1) as factorization dictionaries]]
\\
rsa = [\
[\
59,\
71641520761751435455133616475667090434063332228247871795429,\
200429218120815554269743635437,\
357440504101388365610785389017,\
[2, 2; 3, 2; 946790500267, 1; 5880369817360553, 1],\
[2, 3; 41, 1; 149, 1; 1356913, 1; 2739881, 1; 1967251783951, 1]\
],\
[\
79,\
7293469445285646172092483905177589838606665884410340391954917800303813280275279,\
848184382919488993608481009313734808977,\
8598919753958678882400042972133646037727,\
[2, 4; 3, 1; 181, 1; 725252770335461, 1; 134611158882680922107, 1],\
[\
2, 1;\
3, 1;\
13, 1;\
283, 1;\
158923, 1;\
139139007277, 1;\
17616807254846020469, 1\
]\
],\
[\
100,\
1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139,\
37975227936943673922808872755445627854565536638199,\
40094690950920881030683735292761468389214899724061,\
[\
2, 1;\
3167, 1;\
3613, 1;\
587546788471, 1;\
3263521422991, 1;\
865417043661324529, 1\
],\
[\
2, 2;\
5, 1;\
41, 1;\
2119363, 1;\
602799725049211, 1;\
38273186726790856290328531, 1\
]\
],\
[\
110,\
35794234179725868774991807832568455403003778024228226193532908190484670252364677411513516111204504060317568667,\
6122421090493547576937037317561418841225758554253106999,\
5846418214406154678836553182979162384198610505601062333,\
[\
2, 1;\
11, 1;\
41, 1;\
127, 1;\
53445720712446074139157404521548080741185454495287, 1\
],\
[\
2, 2;\
13, 1;\
379, 1;\
293729, 1;\
3577378891, 1;\
282316043074791150281193589330501811, 1\
]\
],\
[\
120,\
227010481295437363334259960947493668895875336466084780038173258247009162675779735389791151574049166747880487470296548479,\
327414555693498015751146303749141488063642403240171463406883,\
693342667110830181197325401899700641361965863127336680673013,\
[\
2, 1;\
19, 1;\
23, 1;\
173, 1;\
191, 1;\
20207133825867205597523477, 1;\
561051027433723110582599363, 1\
],\
[\
2, 2;\
673, 1;\
9500104961, 1;\
11317677666073, 1;\
2395450201344737432933763488281637, 1\
]\
],\
[\
129,\
114381625757888867669235779976146612010218296721242362562561842935706935245733897830597123563958705058989075147599290026879543541,\
3490529510847650949147849619903898133417764638493387843990820577,\
32769132993266709549961988190834461413177642967992942539798288533,\
[2, 5; 3, 2; 12119894134887676906763366735777424074367238328102041124968127, 1],\
[\
2, 2;\
41, 1;\
199811786544309204572938952383136959836449042487761844754867613, 1\
]\
],\
[\
130,\
1807082088687404805951656164405905566278102516769401349170127021450056662540244048387341127590812303371781887966563182013214880557,\
39685999459597454290161126162883786067576449112810064832555157243,\
45534498646735972188403686897274408864356301263205069600999044599,\
[\
2, 1;\
17, 1;\
70790437, 1;\
122695989299375939, 1;\
134385819829647641627927415253175893091, 1\
],\
[\
2, 1;\
11, 1;\
29, 1;\
1823, 1;\
5659, 1;\
9349, 1;\
91917993786815014822957, 1;\
8050592072224516717989781921, 1\
]\
],\
[\
140,\
21290246318258757547497882016271517497806703963277216278233383215381949984056495911366573853021918316783107387995317230889569230873441936471,\
3398717423028438554530123627613875835633986495969597423490929302771479,\
6264200187401285096151654948264442219302037178623509019111660653946049,\
[\
2, 1;\
7, 1;\
7649, 1;\
435653, 1;\
396004811, 1;\
183967535370446691250943879126698812223588425357931, 1\
],\
[\
2, 6;\
61, 1;\
135613, 1;\
3159671789, 1;\
3744661133861411144034292857028083085348933344798791, 1\
]\
],\
[\
150,\
155089812478348440509606754370011861770654545830995430655466945774312632703463465954363335027577729025391453996787414027003501631772186840890795964683,\
348009867102283695483970451047593424831012817350385456889559637548278410717,\
445647744903640741533241125787086176005442536297766153493419724532460296199,\
[\
2, 2;\
7, 1;\
24514564358712967361, 1;\
1562667948044178859823, 1;\
324446162657135923876474272694399, 1\
],\
[\
2, 1;\
11, 1;\
11807588869, 1;\
30053283389, 1;\
57084195242235980757292641664096499756257280147893049, 1\
]\
],\
[\
155,\
10941738641570527421809707322040357612003732945449205990913842131476349984288934784717997257891267332497625752899781833797076537244027146743531593354333897,\
102639592829741105772054196573991675900716567808038066803341933521790711307779,\
106603488380168454820927220360012878679207958575989291522270608237193062808643,\
[\
2, 1;\
607, 1;\
305999, 1;\
276297036357806107796483997979900139708537040550885894355659143575473, 1\
],\
[\
2, 1;\
241, 1;\
430028152261281581326171, 1;\
514312985943800777534375166399250129284222855975011, 1\
]\
],\
[\
160,\
2152741102718889701896015201312825429257773588845675980170497676778133145218859135673011059773491059602497907111585214302079314665202840140619946994927570407753,\
45427892858481394071686190649738831656137145778469793250959984709250004157335359,\
47388090603832016196633832303788951973268922921040957944741354648812028493909367,\
[\
2, 1;\
37, 1;\
41, 1;\
43, 1;\
61, 1;\
541, 1;\
13951723, 1;\
104046987091804241291, 1;\
7268655850686072522262146377121494569334513, 1\
],\
[\
2, 1;\
9973, 1;\
165833, 1;\
369456908150299181, 1;\
3414553020359960488907, 1;\
11356507337369007109137638293561, 1\
]\
],\
[\
170,\
26062623684139844921529879266674432197085925380486406416164785191859999628542069361450283931914514618683512198164805919882053057222974116478065095809832377336510711545759,\
3586420730428501486799804587268520423291459681059978161140231860633948450858040593963,\
7267029064107019078863797763923946264136137803856996670313708936002281582249587494493,\