This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mp_modul.pas
1760 lines (1533 loc) · 48.6 KB
/
mp_modul.pas
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
unit mp_modul;
{MP basic modular arithmetic, GCD and LCM, Jacobi/Kronecker}
interface
{$i STD.INC}
uses
mp_types;
{$i mp_conf.inc}
(*************************************************************************
DESCRIPTION : MP basic modular arithmetic, GCD and LCM, Jacobi/Kronecker
REQUIREMENTS : BP7, D1-D7/D9-D10/D12/D17-D18, FPC, VP
EXTERNAL DATA : (mp_types)
MEMORY USAGE : heap
DISPLAY MODE : ---
REFERENCES : [1] LibTomMath 0.30+ by Tom St Denis
[2] MPI by M.J. Fromberger
[3] Knuth, D.E.: The Art of computer programming. Vol 2
Seminumerical Algorithms, 3rd ed., 1998
[5] (HAC) Menezes,A., von Oorschot,P., Vanstone, S: Handbook of
Applied Cryptography, 1996, www.cacr.math.uwaterloo.ca/hac
[12] PARI/GP at http://pari.math.u-bordeaux.fr/
[23] J. Shallit, J. Sorenson, A Binary Algorithm for the Jacobi Symbol,
SIGSAM Bulletin, 27(1), 4-11, 1993; available online at
http://blue.butler.edu/~jsorenso/papers/binjac.ps or
http://citeseer.ist.psu.edu/article/shallit93binary.html
[24] H. Cohen, A Course in Computational Algebraic Number Theory
4th printing, 2000
[28] J.P. Sorenson, An analysis of Lehmer's Euclidean GCD algorithm,
ACM International Symposium on Symbolic and Algebraic Computation, 1995
http://blue.butler.edu/~jsorenso/papers/lehmer.pdf
Version Date Author Modification
------- -------- ------- ------------------------------------------
1.23.00 24.09.12 W.Ehrhardt Split from old mp_numth
1.23.01 24.10.12 we mp_initial_mod initialized (removed from mp_numth)
1.24.00 15.12.12 we Some word/integer types changed to TNInt
**************************************************************************)
(*-------------------------------------------------------------------------
This code uses material/ideas from the following 3rd party libraries:
- LibTomMath 0.30+ by Tom St Denis
- MPI 1.8.6 by Michael J. Fromberger
- NX V0.18 and V0.9+ by Marcel Martin
See the file '3rdparty.mpa' for the licenses.
----------------------------------------------------------------------------*)
(*-------------------------------------------------------------------------
(C) Copyright 2004-2012 Wolfgang Ehrhardt
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
var
mp_initial_mod : boolean; {Single initial modular reduction in mp_gcd}
{and in mp_kronecker/mp_jacobi}
procedure mp_addmod(const a,b,c: mp_int; var d: mp_int);
{-Calculate d = a + b (mod c)}
procedure mp_exptmod(const a,b,c: mp_int; var d: mp_int);
{-Compute d = a^b mod c, c>0. If b<0, a must have an inverse mod c}
procedure mp_exptmod_d(const a: mp_int; b: longint; const c: mp_int; var d: mp_int);
{-Compute d = a^b mod c, c>0, b longint. If b<0, a must have an inverse mod c}
procedure mp_gcd(const a,b: mp_int; var c: mp_int);
{-Calculate c = gcd(a,b) using the binary method}
function mp_gcd1(const a,b: mp_int; var c: mp_int): boolean;
{-Calculate c = gcd(a,b) using the binary method, return true if c=1 and no error}
procedure mp_gcd_euclid(const a,b: mp_int; var c: mp_int);
{-Calculate c = gcd(a,b), non optimized Euclid}
procedure mp_gcd_ml(const a,b: mp_int; var u: mp_int);
{-Calculate u = gcd(a,b) using the Sorenson's Modified Lehmer method}
procedure mp_invmod(const a, b: mp_int; var c: mp_int);
{-Compute c = a^-1 (mod b), b>0, via mp_xgcd, MP_UNDEF error if there is no inverse}
function mp_invmodf(const a, b: mp_int; var c: mp_int): boolean;
{-Compute c = a^-1 (mod b), b>0, via mp_xgcd, return true if inverse exists}
function mp_jacobi(const a,n: mp_int): integer;
{-Compute the Jacobi/Legendre symbol (a|n), n: odd and > 2}
function mp_jacobi_lm(a: longint; const n: mp_int): longint;
{-Compute the Jacobi/Legendre symbol (a|n), n: odd and > 2}
function mp_jacobi_ml(const a: mp_int; n: longint): longint;
{-Compute the Jacobi/Legendre symbol (a|n), n: odd and > 2}
function mp_kronecker(const a,n: mp_int): integer;
{-Compute the Kronecker symbol (a|n)}
procedure mp_lcm(const a,b: mp_int; var c: mp_int);
{-Compute least common multiple as |a*b|/(a, b)}
procedure mp_mulmod(const a,b,c: mp_int; var d: mp_int);
{-Calculate d = a * b (mod c)}
procedure mp_sqrmod(const a,c: mp_int; var d: mp_int);
{-Calculate d = a * a (mod c)}
procedure mp_submod(const a,b,c: mp_int; var d: mp_int);
{-Calculate d = a - b (mod c)}
procedure mp_xgcd(const a,b: mp_int; p1,p2,p3: pmp_int);
{-Extended gcd algorithm, calculate a*p1^ + b*p2^ = p3^ = gcd(a,b)}
{ p1,p2,p3 may be nil if the values are not required}
procedure mp_xgcd_bin(const a,b: mp_int; p1,p2,p3: pmp_int);
{-Extended binary gcd, calculate a*p1^ + b*p2^ = p3^ = gcd(a,b) }
{ p1,p2,p3 may be nil if the values are not needed. Note that p1^}
{ and p2^ are NOT unique and may differ from those of mp_xgcd!! }
procedure mp_xlcm(const a,b: mp_int; var c,x,y: mp_int);
{-Calculate c,x,y with lcm(a,b)=c=x*y and x|a, y|b, gcd(x,y)=1.}
{ c,x,y should be different variables, otherwise result may be inconsistent}
implementation
uses
mp_base;
type
TRedType = (MR_Barret, MR_Montgomery
{$ifdef MPC_Reduce_2k} ,MR_Reduce2k {$endif}); {supported reduction types for exptmod}
{---------------------------------------------------------------------------}
procedure mp_addmod(const a,b,c: mp_int; var d: mp_int);
{-Calculate d = a + b (mod c)}
var
t: mp_int;
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mp_not_init(a) or mp_not_init(b) or mp_not_init(c) or mp_not_init(d) then begin
{$ifdef MPC_HaltOnArgCheck}
{$ifdef MPC_UseExceptions}
raise MPXNotInit.Create('mp_addmod');
{$else}
RunError(MP_RTE_NOTINIT);
{$endif}
{$else}
set_mp_error(MP_NOTINIT);
exit;
{$endif}
end;
{$endif}
mp_init(t);
if mp_error<>MP_OKAY then exit;
mp_add(a, b, t);
mp_mod(t, c, d);
mp_clear(t);
end;
{---------------------------------------------------------------------------}
procedure mp_exptmod_win(const g,e,p: mp_int; var b: mp_int; redmode: TRedType);
{-Internal: Compute y=g^|e| mod p, p>0, internal sliding windows}
label
__M, __MU, __RES;
var
bitbuf, bitcpy, bitcnt, mode, x, y, winsize, wmax1,wmax2: integer;
digidx: TNint;
ps2: longint;
buf: mp_digit;
mp : mp_digit;
{$ifdef MPC_Reduce_2k}
d2k: mp_digit;
{$endif}
bc: longint;
res, mu: mp_int;
M: array[1..WEXP_TABSIZE] of mp_int;
{---------------------------------------------}
procedure Gen_Redux(var mpi: mp_int);
{-General modular reduction of mpi driven by redmode}
begin
case redmode of
MR_Montgomery: mp_montgomery_reduce(mpi, p, mp);
MR_Barret: mp_reduce(mpi, p, mu);
{$ifdef MPC_Reduce_2k}
MR_Reduce2k: begin
mp_reduce_2k(mpi, p, d2k);
end;
{$endif}
else if mp_error=MP_OKAY then set_mp_error(MP_BADARG);
end;
end;
begin
{Uses a left-to-right k-ary sliding window to compute the modular exponentiation}
{The value of k changes based on the size of the exponent.}
if mp_error<>MP_OKAY then exit;
{No checks}
{find window size}
bc := mp_bitsize(e);
if bc<=7 then winsize := 2
else if bc<=36 then winsize := 3
else if bc<=140 then winsize := 4
else if bc<=450 then winsize := 5
else if bc<=1303 then winsize := 6
else if bc<=3529 then winsize := 7
else winsize := 8;
if winsize>WEXP_MAX then winsize := WEXP_MAX;
wmax1 := 1 shl (winsize-1);
wmax2 := 1 shl winsize;
ps2 := 2*(p.used+1);
{initialize M array}
{initialize first cell}
mp_init_size(M[1],ps2);
if mp_error<>MP_OKAY then exit;
{now initialize the second half of the array}
for x:=wmax1 to wmax2-1 do begin
mp_init_size(M[x],ps2);
if mp_error<>MP_OKAY then begin
for y:=wmax2 to x-1 do mp_clear(M[y]);
mp_clear(M[1]);
exit;
end;
end;
{create mu, used for Barrett reduction}
mp_init_size(mu,ps2);
if mp_error<>MP_OKAY then goto __M;
{setup result}
mp_init(res);
if mp_error<>MP_OKAY then goto __MU;
{Do initial setup depending on reduction type}
if Redmode=MR_Montgomery then begin
mp_montgomery_setup(p, mp);
mp_montgomery_calcnorm(res, p);
{now set M[1] to G * R mod m}
mp_mulmod(g, res, p, M[1]);
end
else begin
{The M table contains powers of the base, }
{e.g. M[x] = g^x mod p }
mp_set(res, 1);
mp_mod(g, p, M[1]);
if Redmode=MR_Barret then mp_reduce_setup(mu, p)
{$ifdef MPC_Reduce_2k}
else if Redmode=MR_Reduce2k then begin
mp_reduce_2k_setup(p, d2k)
end
{$endif}
else begin
{unsupported reduction}
set_mp_error(MP_BADARG);
end;
end;
if mp_error<>MP_OKAY then goto __RES;
{Create M table }
{The first half of the table is not }
{computed though accept for M[0] and M[1] }
{compute the value at M[wmax1] by squaring M[1] (winsize-1) times}
mp_copy(M[1], M[wmax1]);
for x:=0 to winsize-2 do begin
mp_sqr(M[wmax1], M[wmax1]);
Gen_Redux(M[wmax1]);
if mp_error<>MP_OKAY then goto __RES;
end;
{create upper table, that is M[x] = M[x-1] * M[1] (mod p)}
for x:=wmax1+1 to wmax2-1 do begin
mp_mul(M[x-1], M[1], M[x]);
Gen_Redux(M[x]);
if mp_error<>MP_OKAY then goto __RES;
end;
{set initial mode and bit cnt}
mode := 0;
bitcnt := 1;
buf := 0;
digidx := e.used - 1;
bitcpy := 0;
bitbuf := 0;
repeat
if mp_error<>MP_OKAY then goto __RES;
{grab next digit as required }
dec(bitcnt);
if bitcnt=0 then begin
{if digidx = -1 we are out of digits}
if digidx = -1 then break;
{read next digit and reset the bitcnt}
buf := e.pdigits^[digidx]; dec(digidx);
bitcnt := DIGIT_BIT;
end;
{grab the next msb from the exponent}
y := (buf shr mp_digit(DIGIT_BIT - 1)) and 1;
{temporarly turn range checks off}
{$R-}
buf := buf shl 1;
{$ifdef RangeChecks_on} {$R+} {$endif}
{if the bit is zero and mode = 0 then we ignore it }
{These represent the leading zero bits before the first 1 bit }
{in the exponent. Technically this opt is not required but it }
{does lower the # of trivial squaring/reductions used }
if (mode=0) and (y=0) then continue;
{if the bit is zero and mode == 1 then we square}
if (mode=1) and (y=0) then begin
mp_sqr(res, res);
Gen_Redux(res);
if mp_error<>MP_OKAY then goto __RES;
continue;
end;
{else we add it to the window}
inc(bitcpy);
bitbuf := bitbuf or (y shl (winsize - bitcpy));
mode := 2;
if bitcpy=winsize then begin
{ok window is filled so square as required and multiply}
{square first}
for x:=0 to winsize-1 do begin
mp_sqr(res, res);
Gen_Redux(res);
if mp_error<>MP_OKAY then goto __RES;
end;
{then multiply}
mp_mul(res, M[bitbuf], res);
{and reduce}
Gen_Redux(res);
if mp_error<>MP_OKAY then goto __RES;
{empty window and reset}
bitcpy := 0;
bitbuf := 0;
mode := 1;
end;
until false;
{if bits remain then square/multiply}
if (mode=2) and (bitcpy > 0) then begin
{square then multiply if the bit is set}
for x:=0 to bitcpy-1 do begin
mp_sqr(res, res);
Gen_Redux(res);
if mp_error<>MP_OKAY then goto __RES;
bitbuf := bitbuf shl 1;
if (bitbuf and wmax2) <> 0 then begin
{then multiply}
mp_mul(res, M[1], res);
{and reduce}
Gen_Redux(res);
if mp_error<>MP_OKAY then goto __RES;
end;
end;
end;
if Redmode=MR_Montgomery then begin
{fix result if Montgomery reduction is used recall that any value}
{in a Montgomery system is actually multiplied by R mod n. So we}
{have to reduce one more time to cancel out the factor of R. }
mp_montgomery_reduce(res, P, mp);
if mp_error<>MP_OKAY then goto __RES;
end;
mp_exch(res, b);
__RES: mp_clear(res);
__MU: mp_clear(mu);
__M: mp_clear(M[1]);
for x:=wmax1 to wmax2-1 do mp_clear(M[x]);
end;
{---------------------------------------------------------------------------}
procedure mp_exptmod(const a,b,c: mp_int; var d: mp_int);
{-Compute d = a^b mod c, c>0. If b<0, a must have an inverse mod c}
var
rt: TRedType;
t: array[0..1] of mp_int; {a:t[0], b:t[1]}
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mp_not_init(a) or mp_not_init(b) or mp_not_init(c) or mp_not_init(d) then begin
{$ifdef MPC_HaltOnArgCheck}
{$ifdef MPC_UseExceptions}
raise MPXNotInit.Create('mp_exptmod');
{$else}
RunError(MP_RTE_NOTINIT);
{$endif}
{$else}
set_mp_error(MP_NOTINIT);
exit;
{$endif}
end;
{$endif}
{modulus c must be positive}
if s_mp_is_le0(c) then begin
{$ifdef MPC_HaltOnError}
{$ifdef MPC_UseExceptions}
raise MPXRange.Create('mp_exptmod: c<=0');
{$else}
RunError(MP_RTE_RANGE);
{$endif}
{$else}
set_mp_error(MP_RANGE);
exit;
{$endif}
end;
{if exponent b is negative we have to recourse}
if b.sign=MP_NEG then begin
{first compute 1/a mod c}
mp_init_multi(t);
if mp_error=MP_OKAY then begin
mp_invmod(a, c, t[0]);
{now get |b|}
mp_abs(b, t[1]);
{and now compute (1/a)^|b| instead of a^b [b < 0]}
mp_exptmod(t[0], t[1], c, d);
mp_clear_multi(t);
end;
exit;
end;
{easy outs}
if mp_is1(c) then begin
mp_zero(d);
exit;
end;
if mp_is1(b) then begin
mp_mod(a,c,d);
exit;
end;
{Default: Barrett reduction}
rt := MR_Barret;
if mp_isodd(c) then rt := MR_Montgomery;
{$ifdef MPC_Reduce_2k}
if mp_reduce_is_2k(c) then rt := MR_Reduce2k;
{*tbd: DR module variants}
{$endif}
{Use sliding window routine to compute the modular exponentiation}
mp_exptmod_win(a, b, c, d, rt)
end;
{---------------------------------------------------------------------------}
procedure mp_exptmod_d(const a: mp_int; b: longint; const c: mp_int; var d: mp_int);
{-Compute d = a^b mod c, c>0, b longint. If b<0, a must have an inverse mod c}
var
tmp: mp_int;
begin
if mp_error<>MP_OKAY then exit;
{arg check in mp_exptmod}
mp_init_set_int(tmp, b);
if mp_error=MP_OKAY then begin
mp_exptmod(a,tmp,c,d);
mp_clear(tmp);
end;
end;
{---------------------------------------------------------------------------}
procedure mp_gcd(const a,b: mp_int; var c: mp_int);
{-Calculate c = gcd(a,b) using the binary method}
var
u,v: mp_int;
k,u_lsb,v_lsb: longint;
{$ifdef MPC_UseGCD32}
ui,vi: longint;
{$endif}
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mp_not_init(a) or mp_not_init(b) or mp_not_init(c) then begin
{$ifdef MPC_HaltOnArgCheck}
{$ifdef MPC_UseExceptions}
raise MPXNotInit.Create('mp_gcd');
{$else}
RunError(MP_RTE_NOTINIT);
{$endif}
{$else}
set_mp_error(MP_NOTINIT);
exit;
{$endif}
end;
{$endif}
{if one arg is zero, gcd is the abs of the other}
if mp_iszero(a) then begin
mp_abs(b, c);
exit;
end
else if mp_iszero(b) then begin
mp_abs(a, c);
exit;
end;
{get copies of a and b we can modify}
mp_init2(u,v);
if mp_error<>MP_OKAY then exit;
{u,v are positive for the remainder of the algorithm}
mp_abs(a,u);
mp_abs(b,v);
if mp_error=MP_OKAY then begin
{Do a single initial modular reduction if the sizes of u and v}
{are very different, see H.Cohen's Alg. 1.3.5 in [24]}
if mp_initial_mod then begin
{u>0, v>0. Make u >= v}
if (u.used < v.used) or (mp_cmp_mag(u,v)=MP_LT) then mp_exch(u,v);
if u.used > v.used+1 then begin
{do the initial reduction}
mp_mod(u,v,u);
{done in u mod v = 0}
if u.used=0 then begin
{store result and clear local vars}
mp_exch(c,v);
mp_clear2(u,v);
exit;
end;
{$ifdef MPC_UseGCD32}
if mp_is_longint(u,ui) then begin
{u <>0 but fits into longint}
mp_mod_int(v,ui,vi);
mp_set_int(c,gcd32(ui,vi));
mp_clear2(u,v);
exit;
end;
{$endif}
end;
end;
{Here both u and v are > 0; find the common power of two for u and v}
u_lsb := mp_cnt_lsb(u);
v_lsb := mp_cnt_lsb(v);
if u_lsb<v_lsb then k:=u_lsb else k:=v_lsb;
{Make both u and v odd}
if u_lsb>0 then mp_shr(u, u_lsb, u);
if v_lsb>0 then mp_shr(v, v_lsb, v);
while (mp_error=MP_OKAY) and (v.used>0) do begin
{$ifdef MPC_UseGCD32}
if mp_is_longint(v,vi) then begin
mp_mod_int(u,vi,ui);
mp_set_int(u,gcd32(ui,vi));
break;
end;
{$endif}
{done if u=v, if u>v swap u and v}
if u.used>=v.used then begin
case mp_cmp_mag(u,v) of
MP_GT: mp_exch(u, v);
MP_EQ: break;
end;
end;
{subtract smaller from larger, resulting v is > 0}
s_mp_sub(v, u, v);
if v.pdigits^[0] and 2 <>0 then begin
{Only one factor two can be removed, so reduce overhead. This}
{occurs about one-half of the time, see Knuth [3] 4.5.2, p348}
mp_shr1(v);
end
else begin
{Divide out all factors of two}
mp_shr(v, mp_cnt_lsb(v), v);
end;
end;
{multiply by 2^k which we divided out at the beginning}
mp_shl(u, k, c);
end;
mp_clear2(u,v);
end;
{---------------------------------------------------------------------------}
function mp_gcd1(const a,b: mp_int; var c: mp_int): boolean;
{-Calculate c = gcd(a,b) using the binary method, return true if c=1 and no error}
begin
mp_gcd(a,b,c);
mp_gcd1 := mp_is1(c);
end;
{---------------------------------------------------------------------------}
procedure mp_gcd_euclid(const a,b: mp_int; var c: mp_int);
{-Calculate c = gcd(a,b), non optimized Euclid}
var
t: mp_int;
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mp_not_init(a) or mp_not_init(b) or mp_not_init(c) then begin
{$ifdef MPC_HaltOnArgCheck}
{$ifdef MPC_UseExceptions}
raise MPXNotInit.Create('mp_gcd_euclid');
{$else}
RunError(MP_RTE_NOTINIT);
{$endif}
{$else}
set_mp_error(MP_NOTINIT);
exit;
{$endif}
end;
{$endif}
{Simple Euclid algorithm, Knuth A}
mp_init(t);
if mp_error=MP_OKAY then begin
{make both mp_ints positive}
{handle b first (may be @b=@c)}
mp_abs(b,t);
mp_abs(a,c);
{ggc(c,t) = gcd(t, c mod t)}
while (mp_error=MP_OKAY) and (not mp_iszero(t)) do begin
mp_mod(c,t,c);
mp_exch(t,c);
end;
mp_clear(t);
end;
end;
{$ifdef MP_32BIT}
type
tml_int = longint; {FPC has 16 bit integer/maxint with 32 bit code!!!!!}
tml_dbl = int64;
const
Lehmer_MaxSingle = MP_DIGIT_MAX; {use with mul_d()}
Lehmer_MaxK = 2*DIGIT_BIT-1;
{$else}
type
tml_int = integer;
tml_dbl = longint;
const
Lehmer_MaxSingle = $7FFF; {use with mul_w()}
Lehmer_MaxK = 29;
{$endif}
{---------------------------------------------------------------------------}
function Lehmer(const u,v: mp_int; k: integer; var x0,x1,y0,y1: tml_int): boolean;
{-Single precision Lehmer steps to reduce u,v based on highest k bits of}
{ u,v. Return true if successful and reduction using xi,yi should be done}
var
hu,hv,hb: mp_word;
i: TNInt;
uh,vh,q,x2,y2: tml_dbl;
begin
{This is an implementation of Sorenson's Modified Lehmer procedure [28]. }
{It is based on results of Lehmer, Collins, Jebelean, and Sorenson. The }
{procedure performs single digit calculations that allow to combine some }
{Euclidean GCD steps into a single multiprecision calculation. It returns}
{Sorenson's x[i], y[i], x_[i-1], and y_[i-1] in x1, y1, x0, and y0. These}
{values are used in both GCD procedures (standard and extended). }
Lehmer := false;
{Check the technical conditions and exit if they are not fulfilled}
if (k>Lehmer_MaxK) or (u.sign=MP_NEG) or (v.sign=MP_NEG) or (u.used<2) or (v.used>u.used) or (u.used>v.used+1) then exit;
{Get the leading two digits of u and v; here v[u.used-1] may be zero.}
i := pred(u.used);
hu := (mp_word(u.pdigits^[i]) shl DIGIT_BIT) + u.pdigits^[pred(i)];
if v.used<u.used then hv := v.pdigits^[pred(i)]
else hv := (mp_word(v.pdigits^[i]) shl DIGIT_BIT) + v.pdigits^[pred(i)];
{Get the highest k bits of u and the corresponding bits of v}
hb := mp_word(1) shl k;
i :=0;
while hu>=hb do begin
hu := hu shr 1;
inc(i);
end;
hv := hv shr i;
if hv=0 then exit;
i := 0;
uh := hu;
vh := hv;
x0 := 1;
y0 := 0;
x1 := 0;
y1 := 1;
repeat
q := uh div vh;
x2 := vh;
vh := uh - q*vh;
uh := x2;
x2 := x0 - q*x1;
y2 := y0 - q*y1;
{In addition to the standard break conditions, exit if the next }
{new x2/y2 values are greater than the maximum allowed values. If}
{this happens during the first iteration, Lehmer is still false }
if (abs(x2) > Lehmer_MaxSingle) or (abs(y2) > Lehmer_MaxSingle) then break;
inc(i);
if odd(i) then begin
if (vh < -y2) or (uh-vh < x2-x1) then break;
end
else begin
if (vh < -x2) or (uh-vh < y2-y1) then break;
end;
x0 := x1;
x1 := x2;
y0 := y1;
y1 := y2;
Lehmer := true;
until false;
end;
{---------------------------------------------------------------------------}
procedure mp_gcd_ml(const a,b: mp_int; var u: mp_int);
{-Calculate u = gcd(a,b) using the Sorenson's Modified Lehmer method}
var
s,t,v: mp_int;
tu,tv: longint;
bsu,bsv: longint;
x0,x1,y0,y1: tml_int;
const
k=Lehmer_MaxK; k2=(k+1) div 2;
begin
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mp_not_init(a) or mp_not_init(b) or mp_not_init(u) then begin
{$ifdef MPC_HaltOnArgCheck}
{$ifdef MPC_UseExceptions}
raise MPXNotInit.Create('mp_gcd_ml');
{$else}
RunError(MP_RTE_NOTINIT);
{$endif}
{$else}
set_mp_error(MP_NOTINIT);
exit;
{$endif}
end;
{$endif}
mp_init3(s,t,v);
if mp_error=MP_OKAY then begin
{make both mp_ints positive}
{handle b first (may be @b=@u)}
mp_abs(b,v);
mp_abs(a,u);
{make u>=v, this will be a loop invariant}
if (u.used<v.used) or (mp_cmp_mag(u,v)=MP_LT) then mp_exch(u,v);
bsu := mp_bitsize(u);
bsv := mp_bitsize(v);
{Iterate until v is in longint range, then use gcd32}
while (mp_error=MP_OKAY) and (bsv>31) do begin
if (bsu-bsv <= k2) and Lehmer(u,v,k,x0,x1,y0,y1) then begin
{calculate new (u, v): v := x1*u + y1*v; u := x0*u + y0*v;}
{$ifdef MP_16BIT}
{v := x1*u + y1*v}
mp_mul_w(u,abs(x1),s); if x1<0 then s_mp_chs(s);
mp_mul_w(v,abs(y1),t); if y1<0 then s_mp_chs(t);
mp_add(s,t,t);
mp_exch(t,v);
{u := x0*u + y0*v}
mp_mul_w(u,abs(x0),s); if x0<0 then s_mp_chs(s);
mp_mul_w(t,abs(y0),t); if y0<0 then s_mp_chs(t);
mp_add(s,t,u);
{$else}
{v := x1*u + y1*v}
mp_mul_d(u,abs(x1),s); if x1<0 then s_mp_chs(s);
mp_mul_d(v,abs(y1),t); if y1<0 then s_mp_chs(t);
mp_add(s,t,t);
mp_exch(t,v);
{u := x0*u + y0*v}
mp_mul_d(u,abs(x0),s); if x0<0 then s_mp_chs(s);
mp_mul_d(t,abs(y0),t); if y0<0 then s_mp_chs(t);
mp_add(s,t,u);
{$endif}
end;
mp_mod(u,v,u);
mp_exch(v,u);
bsu := bsv;
bsv := mp_bitsize(v);
end;
if (mp_error=MP_OKAY) and (v.used>0) then begin
{v<>0 and fits into longint}
tv := mp_get_int(v);
mp_mod_int(u,tv,tu);
mp_set_int(u,gcd32(tv,tu));
end;
mp_clear3(s,t,v);
end;
end;
{---------------------------------------------------------------------------}
function mp_invmodf(const a, b: mp_int; var c: mp_int): boolean;
{-Compute c = a^-1 (mod b), b>0, via mp_xgcd, return true if inverse exists}
var
u,v: mp_int;
la, lb, lc: longint;
begin
{Note: this function is normally faster than the old fast_invmod}
{for odd b due to the added Lehmer steps in mp_xgcd. It is much }
{faster than the old mp_invmodf for even b.}
mp_invmodf := false;
if mp_error<>MP_OKAY then exit;
{$ifdef MPC_ArgCheck}
if mp_not_init(a) or mp_not_init(b) or mp_not_init(c) then begin
{$ifdef MPC_HaltOnArgCheck}
{$ifdef MPC_UseExceptions}
raise MPXNotInit.Create('mp_invmodf');
{$else}
RunError(MP_RTE_NOTINIT);
{$endif}
{$else}
set_mp_error(MP_NOTINIT);
exit;
{$endif}
end;
{$endif}
{if a and b are even or b<=0 then return no success}
if (mp_iseven(b) and mp_iseven(a)) or (b.sign=MP_NEG) or mp_iszero(b) then exit;
{use invmod32 if b has less than 32 bits}
if mp_is_longint(b,lb) then begin
if not mp_is_longint(a,la) then mp_mod_int(a,lb,la);
lc := invmod32(la,lb);
if lc<>0 then begin
mp_set_int(c,lc);
mp_invmodf := true;
end;
exit;
end;
{initialize temps}
mp_init2(u,v);
if mp_error<>MP_OKAY then exit;
{calculate a*u + b*? = v = gcd(a,b)}
mp_xgcd(a,b,@u,nil,@v);
{if gcd(a,b><>1 then there is no inverse}
if mp_is1(v) then begin
{Make 0 <= a^-1 < b}
while (mp_error=MP_OKAY) and (u.sign=MP_NEG) do mp_add(u,b,u);
while (mp_error=MP_OKAY) and (mp_cmp_mag(u, b)<>MP_LT) do mp_sub(u,b,u);
mp_exch(u, c);
mp_invmodf := mp_error=MP_OKAY;
end;
mp_clear2(u,v);
end;
{---------------------------------------------------------------------------}
procedure mp_invmod(const a, b: mp_int; var c: mp_int);
{-Compute c = a^-1 (mod b), b>0, via mp_xgcd, MP_UNDEF error if there is no inverse}
begin
if not mp_invmodf(a,b,c) then begin
{$ifdef MPC_HaltOnError}
{$ifdef MPC_UseExceptions}
raise MPXUndef.Create('mp_invmod: no inverse');
{$else}
RunError(MP_RTE_OTHER);
{$endif}
{$endif}
set_mp_error(MP_UNDEF);
end;
end;
{---------------------------------------------------------------------------}
function kron_intern(var x,y: mp_int): integer;
{-Internal kronecker: no mp_init, initial range/sanity checks etc}
var
res: integer;
m8: mp_digit;
k: longint;
begin
(*
Here the PARI/GP definition is used: The Kronecker symbol is the Legendre
symbol (x|y) extended to all integers by complete multiplicativity,
plus special rules for y = 0, -1, or 2:
y = 0: (x|0) = 1 if |x| = 1
0 otherwise
y =-1: (x|-1) = 1 if x >= 0,
-1 if x < 0.
y = 2: (x|2) = 0 if x is even
= 1 if x = 1, 7 mod 8
= -1 if x = 3, 5 mod 8
*)
{initialize return val for zero/error}
kron_intern := 0;
{easy case (x|0)}
if mp_iszero(y) then begin
if mp_is1a(x) then kron_intern := 1;
exit;
end;
{initialize accumulated result}
res := 1;
{here y<>0, make y positive}
if y.sign=MP_NEG then begin
{(x|y) = (x|-1)*(x|-y)}
if x.sign=MP_NEG then res := -res;
mp_abs(y,y);
end;
{if y even, reduce to odd case}
if y.pdigits^[0] and 1 = 0 then begin
{(x|2)=0 if x is even}
if mp_iseven(x) then exit;
{y is even; divide out highest power of two: y = 2^k*y'}
{(x|y) = (x|2)^k * (x|y')}
mp_makeodd(y,y,k);
if odd(k) then begin
{note: x is odd and <> 0, so pdigits^[0] is valid}
m8 := x.pdigits^[0] and 7;
if (m8=3) or (m8=5) then res := -res;
end;
end;
{Here y is positive and odd, and we actually calculate a Jacobi symbol.}
{x>=0 is needed for binary algorithm, so take abs and adjust sign.}
if x.sign=MP_NEG then begin
{(-x|y) = (x|y)*(-1|y); change sign if y=3 (mod 4)}
if y.pdigits^[0] and 3 = 3 then res := -res;
x.sign := MP_ZPOS;
end;
if mp_initial_mod and (y.used<=x.used) then begin
{Here x,y>0, y is odd. Reduce size once, see Cohen [24], Alg.1.4.12}
mp_mod(x,y,x);
end;