forked from sethtroisi/gmp-ecm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpmod.c
2474 lines (2227 loc) · 72.8 KB
/
mpmod.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
/* Modular multiplication.
Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
Paul Zimmermann, Alexander Kruppa and Cyril Bouvier.
This file is part of the ECM Library.
The ECM Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
The ECM Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the ECM Library; see the file COPYING.LIB. If not, see
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <stdio.h>
#include <stdlib.h>
#include "ecm-gmp.h"
#include "ecm-impl.h"
#include "mpmod.h"
#ifdef USE_ASM_REDC
#include "mulredc.h"
#endif
FILE *ECM_STDOUT, *ECM_STDERR; /* define them here since needed in tune.c */
/* define WANT_ASSERT to check normalization of residues */
/* #define WANT_ASSERT 1 */
/* #define DEBUG */
/* #define WANT_ASSERT_EXPENSIVE 1 */
#define ASSERT_NORMALIZED(x) ASSERT ((modulus->repr != ECM_MOD_MODMULN && \
modulus->repr != ECM_MOD_REDC) || \
mpz_size (x) <= mpz_size (modulus->orig_modulus))
#define MPZ_NORMALIZED(x) ASSERT (PTR(x)[ABSIZ(x)-1] != 0)
static void ecm_redc_basecase (mpz_ptr, mpz_ptr, mpmod_t) ATTRIBUTE_HOT;
static void ecm_mulredc_basecase (mpres_t, const mpres_t, const mpres_t,
mpmod_t) ATTRIBUTE_HOT;
static void base2mod (mpres_t, const mpres_t, mpres_t, mpmod_t) ATTRIBUTE_HOT;
static void REDC (mpres_t, const mpres_t, mpz_t, mpmod_t);
/* returns +/-l if n is a factor of N = 2^l +/- 1 with N <= n^threshold,
0 otherwise.
*/
int
isbase2 (const mpz_t n, const double threshold)
{
unsigned int k, lo;
int res = 0;
mpz_t u, w;
mpz_init (u);
mpz_init (w);
lo = mpz_sizeinbase (n, 2) - 1; /* 2^lo <= n < 2^(lo+1) */
mpz_set_ui (u, 1UL);
mpz_mul_2exp (u, u, 2UL * lo);
mpz_mod (w, u, n); /* 2^(2lo) mod n = -/+2^(2lo-l) if m*n = 2^l+/-1 */
if (mpz_cmp_ui (w, 1UL) == 0) /* if 2^(2lo) mod n = 1, then n divides
2^(2lo)-1. If algebraic factors have been
removed, n divides either 2^lo+1 or 2^lo-1.
But since n has lo+1 bits, n can only divide
2^lo+1. More precisely, n must be 2^lo+1. */
{
/* check that n equals 2^lo+1. Since n divides 2^(2lo)-1, n is odd. */
if (mpz_scan1 (n, 1UL) != lo)
lo = 0;
mpz_clear (w);
mpz_clear (u);
return lo;
}
k = mpz_sizeinbase (w, 2) - 1;
/* if w = 2^k then n divides 2^(2*lo-k)-1 */
mpz_set_ui (u, 1UL);
mpz_mul_2exp (u, u, k);
if (mpz_cmp(w, u) == 0)
res = k - 2 * lo;
else /* if w = -2^k then n divides 2^(2*lo-k)+1 */
{
mpz_neg (w, w);
mpz_mod (w, w, n);
k = mpz_sizeinbase (w, 2) - 1;
mpz_set_ui (u, 1UL);
mpz_mul_2exp (u, u, k);
if (mpz_cmp (w, u) == 0)
res = 2 * lo - k;
}
mpz_clear (u);
mpz_clear (w);
if (abs (res) > (int) (threshold * (double) lo))
res = 0;
if (abs (res) < 16)
res = 0;
return res;
}
/* Do base-2 reduction. R must not equal S or t. */
static void
base2mod (mpres_t R, const mpres_t S, mpres_t t, mpmod_t modulus)
{
unsigned long absbits = abs (modulus->bits);
ASSERT (R != S && R != t);
mpz_tdiv_q_2exp (R, S, absbits);
mpz_tdiv_r_2exp (t, S, absbits);
if (modulus->bits < 0)
mpz_add (R, R, t);
else
mpz_sub (R, t, R);
/* mpz_mod (R, R, modulus->orig_modulus); */
while (mpz_sizeinbase (R, 2) > absbits)
{
mpz_tdiv_q_2exp (t, R, absbits);
mpz_tdiv_r_2exp (R, R, absbits);
if (modulus->bits < 0)
mpz_add (R, R, t);
else
mpz_sub (R, R, t);
}
}
/* Modular reduction modulo the Fermat number 2^m+1.
n = m / GMP_NUMB_BITS. Result is < 2^m+1.
FIXME: this does not work with nails.
Only copies the data to R if reduction is needed and returns 1 in that
case. If the value in S is reduced already, nothing is done and 0 is
returned. Yes, this is ugly. */
static int
base2mod_2 (mpres_t R, const mpres_t S, mp_size_t n, mpz_t modulus)
{
mp_size_t s;
s = ABSIZ(S);
if (s > n)
{
if (s == n + 1)
{
mp_srcptr sp = PTR(S);
mp_ptr rp;
MPZ_REALLOC (R, s);
rp = PTR(R);
if ((rp[n] = mpn_sub_1 (rp, sp, n, sp[n])))
rp[n] = mpn_add_1 (rp, rp, n, rp[n]);
MPN_NORMALIZE(rp, s);
ASSERT (s <= n || (s == n && rp[n] == 1));
SIZ(R) = (SIZ(S) > 0) ? (int) s : (int) -s;
}
else /* should happen rarely */
mpz_mod (R, S, modulus);
return 1;
}
return 0;
}
/* subquadratic REDC, at mpn level.
{orig,n} is the original modulus.
Requires xn = 2n or 2n-1 and ABSIZ(orig_modulus)=n.
*/
static void
ecm_redc_n (mp_ptr rp, mp_srcptr x0p, mp_size_t xn,
mp_srcptr orig, mp_srcptr invm, mp_size_t n)
{
mp_ptr tp, up, xp;
mp_size_t nn = n + n;
mp_limb_t cy, cin;
TMP_DECL;
ASSERT((xn == nn) || (xn == nn - 1));
TMP_MARK;
up = TMP_ALLOC_LIMBS(nn + nn);
if (xn < nn)
{
xp = TMP_ALLOC_LIMBS(nn);
MPN_COPY (xp, x0p, xn);
xp[nn - 1] = 0;
}
else
xp = (mp_ptr) x0p;
#ifdef HAVE___GMPN_MULLO_N /* available up from GMP 5.0.0 */
__gmpn_mullo_n (up, xp, invm, n);
#else
ecm_mul_lo_n (up, xp, invm, n);
#endif
tp = up + nn;
mpn_mul_n (tp, up, orig, n);
/* add {x, 2n} and {tp, 2n}. We know that {tp, n} + {xp, n} will give
either 0, or a carry out. If xp[n-1] <> 0 or tp[n-1] <> 0,
then there is a carry. We use a binary OR, which sets the zero flag
if and only if both operands are zero. */
cin = (mp_limb_t) ((xp[n - 1] | tp[n - 1]) ? 1 : 0);
#ifdef HAVE___GMPN_ADD_NC
cy = __gmpn_add_nc (rp, tp + n, xp + n, n, cin);
#else
cy = mpn_add_n (rp, tp + n, xp + n, n);
cy += mpn_add_1 (rp, rp, n, cin);
#endif
/* since we add at most N-1 to the upper half of {x0p,2n},
one adjustment is enough */
if (cy)
cy -= mpn_sub_n (rp, rp, orig, n);
ASSERT (cy == 0);
TMP_FREE;
}
/* REDC. x and t must not be identical, t has limb growth */
/* subquadratic REDC, at mpz level */
static void
REDC (mpres_t r, const mpres_t x, mpz_t t, mpmod_t modulus)
{
mp_size_t n = modulus->bits / GMP_NUMB_BITS;
mp_size_t xn = ABSIZ(x);
ASSERT (xn <= 2 * n);
if (xn == 2 * n) /* ecm_redc_n also accepts xn=2n-1, but this seems slower
for now (see remark in TODO) */
{
mp_ptr rp;
MPZ_REALLOC (r, n);
rp = PTR(r);
ecm_redc_n (rp, PTR(x), xn, PTR(modulus->orig_modulus),
PTR(modulus->aux_modulus), n);
MPN_NORMALIZE(rp, n);
SIZ(r) = (SIZ(x) > 0) ? (int) n : (int) -n;
MPZ_NORMALIZED (r);
}
else
{
mpz_tdiv_r_2exp (t, x, modulus->bits);
mpz_mul (t, t, modulus->aux_modulus);
mpz_tdiv_r_2exp (t, t, modulus->bits); /* t = (x % R) * 1/N (mod R) */
mpz_mul (t, t, modulus->orig_modulus); /* t <= (R-1)*N */
mpz_add (t, t, x); /* t < (R-1)*N + R^2/B */
mpz_tdiv_q_2exp (r, t, modulus->bits); /* r = (x + m*N) / R
< N + R/B */
if (ABSIZ (r) > n) /* this can happen in practice but is extremely
unlikely: in particular it requires that the
upper limb of N has only ones */
mpz_sub (r, r, modulus->multiple);
}
ASSERT (ABSIZ(r) <= n);
}
/* Quadratic time redc for n word moduli. */
static inline void
redc_basecase_n (mp_ptr rp, mp_ptr cp, mp_srcptr np, const mp_size_t nn,
const mp_ptr invm)
{
#ifdef HAVE___GMPN_REDC_2
REDC2(rp, cp, np, nn, invm);
#else /* HAVE___GMPN_REDC_2 is not defined */
#ifdef HAVE___GMPN_REDC_1
REDC1(rp, cp, np, nn, invm[0]);
#else /* neither HAVE___GMPN_REDC_2 nor HAVE___GMPN_REDC_1 is defined */
mp_limb_t cy;
mp_size_t j;
for (j = 0; j < nn; j++)
{
cy = mpn_addmul_1 (cp, np, nn, cp[0] * invm[0]);
ASSERT(cp[0] == (mp_limb_t) 0);
cp[0] = cy;
cp++;
}
/* add vector of carries and shift */
cy = mpn_add_n (rp, cp, cp - nn, nn);
/* the result of Montgomery's REDC is less than 2^Nbits + N,
thus at most one correction is enough */
if (cy != 0)
{
mp_limb_t t;
t = mpn_sub_n (rp, rp, np, nn); /* a borrow should always occur here */
ASSERT (t == 1);
}
#endif /* HAVE___GMPN_REDC_1 */
#endif /* HAVE___GMPN_REDC_2 */
}
/* r <- c/R^nn mod n, where n has nn limbs, and R=2^GMP_NUMB_BITS.
n must be odd.
c must have space for at least 2*nn limbs.
r must have space for at least n limbs.
c and r can be the same variable.
The data in c is clobbered.
*/
static void
ecm_redc_basecase (mpz_ptr r, mpz_ptr c, mpmod_t modulus)
{
mp_ptr rp;
mp_ptr cp;
mp_srcptr np;
mp_size_t j, nn = modulus->bits / GMP_NUMB_BITS;
ASSERT(ABSIZ(c) <= 2 * nn);
ASSERT(ALLOC(c) >= 2 * nn);
ASSERT(ALLOC(r) >= nn);
cp = PTR(c);
rp = PTR(r);
np = PTR(modulus->orig_modulus);
for (j = ABSIZ(c); j < 2 * nn; j++)
cp[j] = 0;
redc_basecase_n (rp, cp, np, nn, modulus->Nprim);
MPN_NORMALIZE (rp, nn);
SIZ(r) = SIZ(c) < 0 ? (int) -nn : (int) nn;
}
#ifdef USE_ASM_REDC
/* Quadratic time multiplication and REDC with nn-limb modulus.
x and y are nn-limb residues, the nn-limb result is written to z.
This function merely calls the correct mulredc*() assembly function
depending on nn, and processes any leftover carry. */
static void
mulredc (mp_ptr z, mp_srcptr x, mp_srcptr y, mp_srcptr m,
const mp_size_t nn, const mp_limb_t invm)
{
mp_limb_t cy;
switch (nn)
{
case 1:
cy = mulredc1(z, x[0], y[0], m[0], invm);
break;
case 2:
cy = mulredc2(z, x, y, m, invm);
break;
case 3:
cy = mulredc3(z, x, y, m, invm);
break;
case 4:
cy = mulredc4(z, x, y, m, invm);
break;
case 5:
cy = mulredc5(z, x, y, m, invm);
break;
case 6:
cy = mulredc6(z, x, y, m, invm);
break;
case 7:
cy = mulredc7(z, x, y, m, invm);
break;
case 8:
cy = mulredc8(z, x, y, m, invm);
break;
case 9:
cy = mulredc9(z, x, y, m, invm);
break;
case 10:
cy = mulredc10(z, x, y, m, invm);
break;
case 11:
cy = mulredc11(z, x, y, m, invm);
break;
case 12:
cy = mulredc12(z, x, y, m, invm);
break;
case 13:
cy = mulredc13(z, x, y, m, invm);
break;
case 14:
cy = mulredc14(z, x, y, m, invm);
break;
case 15:
cy = mulredc15(z, x, y, m, invm);
break;
case 16:
cy = mulredc16(z, x, y, m, invm);
break;
case 17:
cy = mulredc17(z, x, y, m, invm);
break;
case 18:
cy = mulredc18(z, x, y, m, invm);
break;
case 19:
cy = mulredc19(z, x, y, m, invm);
break;
case 20:
cy = mulredc20(z, x, y, m, invm);
break;
default:
abort();
}
/* the result of Montgomery's REDC is less than 2^Nbits + N,
thus at most one correction is enough */
if (cy != 0)
{
ATTRIBUTE_UNUSED mp_limb_t t;
t = mpn_sub_n (z, z, m, nn); /* a borrow should always occur here */
ASSERT (t == 1);
}
}
#if 0
/* {rp, n} <- {ap, n}^2/B^n mod {np, n} where B = 2^GMP_NUMB_BITS */
static void
sqrredc (mp_ptr rp, mp_srcptr ap, mp_srcptr np, const mp_size_t n,
const mp_limb_t invm)
{
mp_ptr cp;
mp_size_t i;
mp_limb_t cy, q;
TMP_DECL;
TMP_MARK;
cp = TMP_ALLOC_LIMBS(2*n);
for (i = 0; i < n; i++)
umul_ppmm (cp[2*i+1], cp[2*i], ap[i], ap[i]);
if (UNLIKELY(n == 1))
{
q = cp[0] * invm;
rp[0] = mpn_addmul_1 (cp, np, 1, q);
cy = mpn_add_n (rp, rp, cp + 1, 1);
goto end_sqrredc;
}
if (cp[0] & (mp_limb_t) 1)
/* cp[n] is either some ap[i]^2 mod B or floor(ap[i]^2/B),
the latter is at most floor((B-1)^2/B) = B-2, and the former cannot be
B-1 since -1 is not a square mod 2^n for n >1, thus there is no carry
in cp[n] + ... below */
cp[n] += mpn_add_n (cp, cp, np, n);
/* now {cp, 2n} is even: divide by two */
mpn_rshift (cp, cp, 2*n, 1);
/* now cp[2n-1] is at most B/2-1 */
for (i = 0; i < n - 1; i++)
{
q = cp[i] * invm;
cp[i] = mpn_addmul_1 (cp + i, np, n, q);
/* accumulate ap[i+1..n-1] * ap[i] */
rp[i] = mpn_addmul_1 (cp + 2 * i + 1, ap + i + 1, n - 1 - i, ap[i]);
}
/* the last iteration did set cp[n-2] to zero, accumulated a[n-1] * a[n-2] */
/* cp[2n-1] was untouched so far, so it is still at most B/2-1 */
q = cp[n-1] * invm;
rp[n-1] = mpn_addmul_1 (cp + n - 1, np, n, q);
/* rp[n-1] <= floor((B^n-1)*(B-1)/B^n)<=B-2 */
/* now add {rp, n}, {cp+n, n} and {cp, n-1} */
/* cp[2n-1] still <= B/2-1 */
rp[n-1] += mpn_add_n (rp, rp, cp, n-1); /* no overflow in rp[n-1] + ... */
cy = mpn_add_n (rp, rp, cp + n, n);
/* multiply by 2 */
cy = (cy << 1) + mpn_lshift (rp, rp, n, 1);
end_sqrredc:
while (cy)
cy -= mpn_sub_n (rp, rp, np, n);
TMP_FREE;
}
#endif
#ifdef HAVE_NATIVE_MULREDC1_N
/* Multiplies y by the 1-limb value of x and does modulo reduction.
The resulting residue may be multiplied by some constant,
which makes this function useful only for cases where, e.g.,
all projective coordinates are multiplied by the same constant.
More precisely it computes:
{z, N} = {y, N} * x / 2^GMP_NUMB_BITS mod {m, N}
*/
static void
mulredc_1 (mp_ptr z, const mp_limb_t x, mp_srcptr y, mp_srcptr m,
const mp_size_t N, const mp_limb_t invm)
{
mp_limb_t cy;
switch (N) {
case 1:
cy = mulredc1(z, x, y[0], m[0], invm);
break;
case 2:
cy = mulredc1_2(z, x, y, m, invm);
break;
case 3:
cy = mulredc1_3(z, x, y, m, invm);
break;
case 4:
cy = mulredc1_4(z, x, y, m, invm);
break;
case 5:
cy = mulredc1_5(z, x, y, m, invm);
break;
case 6:
cy = mulredc1_6(z, x, y, m, invm);
break;
case 7:
cy = mulredc1_7(z, x, y, m, invm);
break;
case 8:
cy = mulredc1_8(z, x, y, m, invm);
break;
case 9:
cy = mulredc1_9(z, x, y, m, invm);
break;
case 10:
cy = mulredc1_10(z, x, y, m, invm);
break;
case 11:
cy = mulredc1_11(z, x, y, m, invm);
break;
case 12:
cy = mulredc1_12(z, x, y, m, invm);
break;
case 13:
cy = mulredc1_13(z, x, y, m, invm);
break;
case 14:
cy = mulredc1_14(z, x, y, m, invm);
break;
case 15:
cy = mulredc1_15(z, x, y, m, invm);
break;
case 16:
cy = mulredc1_16(z, x, y, m, invm);
break;
case 17:
cy = mulredc1_17(z, x, y, m, invm);
break;
case 18:
cy = mulredc1_18(z, x, y, m, invm);
break;
case 19:
cy = mulredc1_19(z, x, y, m, invm);
break;
case 20:
cy = mulredc1_20(z, x, y, m, invm);
break;
default:
{
abort ();
}
}
/* the result of Montgomery's REDC is less than 2^Nbits + N,
thus one correction (at most) is enough */
if (cy != 0)
{
ATTRIBUTE_UNUSED mp_limb_t t;
t = mpn_sub_n (z, z, m, N); /* a borrow should always occur here */
ASSERT (t == 1);
}
}
#endif /* ifdef HAVE_NATIVE_MULREDC1_N */
#endif
static int tune_mulredc_table[] = TUNE_MULREDC_TABLE;
static int tune_sqrredc_table[] = TUNE_SQRREDC_TABLE;
static void
ecm_mulredc_basecase_n (mp_ptr rp, mp_srcptr s1p, mp_srcptr s2p,
mp_srcptr np, mp_size_t nn, mp_ptr invm, mp_ptr tmp)
{
mp_limb_t cy;
mp_size_t j;
if (nn <= MULREDC_ASSEMBLY_MAX)
{
switch (tune_mulredc_table[nn])
{
case MPMOD_MULREDC: /* use quadratic assembly mulredc */
#ifdef USE_ASM_REDC
mulredc (rp, s1p, s2p, np, nn, invm[0]);
break;
#endif /* otherwise go through to the next available mode */
case MPMOD_MUL_REDC1: /* mpn_mul_n + __gmpn_redc_1 */
#ifdef HAVE___GMPN_REDC_1
mpn_mul_n (tmp, s1p, s2p, nn);
REDC1(rp, tmp, np, nn, invm[0]);
break;
#endif /* otherwise go through to the next available mode */
case MPMOD_MUL_REDC2: /* mpn_mul_n + __gmpn_redc_2 */
#ifdef HAVE___GMPN_REDC_2
mpn_mul_n (tmp, s1p, s2p, nn);
REDC2(rp, tmp, np, nn, invm);
break;
#endif /* otherwise go through to the next available mode */
case MPMOD_MUL_REDCN: /* mpn_mul_n + __gmpn_redc_n */
/* disable redc_n for now, since it uses the opposite
precomputed inverse wrt redc_1 and redc_2 */
#ifdef HAVE___GMPN_REDC_Nxxx
mpn_mul_n (tmp, s1p, s2p, nn);
__gmpn_redc_n (rp, tmp, np, nn, invm);
break;
#endif /* otherwise go through to the next available mode */
case MPMOD_MUL_REDC_C: /* plain C quadratic reduction */
mpn_mul_n (tmp, s1p, s2p, nn);
for (j = 0; j < nn; j++, tmp++)
tmp[0] = mpn_addmul_1 (tmp, np, nn, tmp[0] * invm[0]);
cy = mpn_add_n (rp, tmp - nn, tmp, nn);
if (cy != 0)
mpn_sub_n (rp, rp, np, nn); /* a borrow should always occur here */
break;
default:
{
outputf (OUTPUT_ERROR, "Invalid mulredc mode: %d\n",
tune_mulredc_table[nn]);
exit (EXIT_FAILURE);
}
}
}
else /* nn > MULREDC_ASSEMBLY_MAX */
{
mpn_mul_n (tmp, s1p, s2p, nn);
ecm_redc_n (rp, tmp, 2 * nn, np, invm, nn);
}
}
static void
ecm_sqrredc_basecase_n (mp_ptr rp, mp_srcptr s1p,
mp_srcptr np, mp_size_t nn, mp_ptr invm, mp_ptr tmp)
{
mp_limb_t cy;
mp_size_t j;
if (nn <= MULREDC_ASSEMBLY_MAX)
{
switch (tune_sqrredc_table[nn])
{
case MPMOD_MULREDC: /* use quadratic assembly mulredc */
#ifdef USE_ASM_REDC
mulredc (rp, s1p, s1p, np, nn, invm[0]);
break;
#endif /* otherwise go through to the next available mode */
case MPMOD_MUL_REDC1: /* mpn_sqr + __gmpn_redc_1 */
#ifdef HAVE___GMPN_REDC_1
mpn_sqr (tmp, s1p, nn);
REDC1(rp, tmp, np, nn, invm[0]);
break;
#endif /* otherwise go through to the next available mode */
case MPMOD_MUL_REDC2: /* mpn_sqr + __gmpn_redc_2 */
#ifdef HAVE___GMPN_REDC_2
mpn_sqr (tmp, s1p, nn);
REDC2(rp, tmp, np, nn, invm);
break;
#endif /* otherwise go through to the next available mode */
case MPMOD_MUL_REDCN: /* mpn_sqr + __gmpn_redc_n */
/* disable redc_n for now, since it uses the opposite
precomputed inverse wrt redc_1 and redc_2 */
#ifdef HAVE___GMPN_REDC_Nxxx
mpn_sqr (tmp, s1p, nn);
__gmpn_redc_n (rp, tmp, np, nn, invm);
break;
#endif /* otherwise go through to the next available mode */
case MPMOD_MUL_REDC_C: /* plain C quadratic reduction */
mpn_sqr (tmp, s1p, nn);
for (j = 0; j < nn; j++, tmp++)
tmp[0] = mpn_addmul_1 (tmp, np, nn, tmp[0] * invm[0]);
cy = mpn_add_n (rp, tmp - nn, tmp, nn);
if (cy != 0)
mpn_sub_n (rp, rp, np, nn); /* a borrow should always occur here */
break;
default:
{
outputf (OUTPUT_ERROR, "Invalid sqrredc mode: %d\n",
tune_sqrredc_table[nn]);
exit (EXIT_FAILURE);
}
}
}
else /* nn > MULREDC_ASSEMBLY_MAX */
{
mpn_sqr (tmp, s1p, nn);
ecm_redc_n (rp, tmp, 2 * nn, np, invm, nn);
}
}
/* R <- S1 * S2 mod modulus
i.e. R <- S1*S2/r^nn mod n, where n has nn limbs, and r=2^GMP_NUMB_BITS.
Same as ecm_redc_basecase previous, but combined with mul
Neither input argument must be in modulus->temp1
*/
static void
ecm_mulredc_basecase (mpres_t R, const mpres_t S1, const mpres_t S2,
mpmod_t modulus)
{
mp_ptr s1p, s2p, rp = PTR(R);
mp_size_t j, nn = modulus->bits / GMP_NUMB_BITS;
ASSERT(ALLOC(R) >= nn);
ASSERT(ALLOC(S1) >= nn);
ASSERT(ALLOC(S2) >= nn);
s1p = PTR(S1);
s2p = PTR(S2);
/* FIXME: S1 and S2 are input and marked const, we mustn't write to them */
for (j = ABSIZ(S1); j < nn; j++)
s1p[j] = 0;
for (j = ABSIZ(S2); j < nn; j++)
s2p[j] = 0;
ecm_mulredc_basecase_n (rp, s1p, s2p, PTR(modulus->orig_modulus), nn,
modulus->Nprim, PTR(modulus->temp1));
MPN_NORMALIZE (rp, nn);
SIZ(R) = (SIZ(S1)*SIZ(S2)) < 0 ? (int) -nn : (int) nn;
}
/* R <- S1^2 mod modulus
i.e. R <- S1^2/r^nn mod n, where n has nn limbs, and r=2^GMP_NUMB_BITS.
Same as ecm_redc_basecase previous, but combined with sqr
The input argument must not be in modulus->temp1 */
static void
ecm_sqrredc_basecase (mpres_t R, const mpres_t S1, mpmod_t modulus)
{
mp_ptr rp;
mp_ptr s1p;
mp_size_t j, nn = modulus->bits / GMP_NUMB_BITS;
ASSERT(ALLOC(R) >= nn);
ASSERT(ALLOC(S1) >= nn);
rp = PTR(R);
s1p = PTR(S1);
/* FIXME: S1 is input and marked const, we mustn't write to it */
for (j = ABSIZ(S1); j < nn; j++)
s1p[j] = 0;
ecm_sqrredc_basecase_n (rp, s1p, PTR(modulus->orig_modulus), nn,
modulus->Nprim, PTR(modulus->temp1));
MPN_NORMALIZE (rp, nn);
SIZ(R) = (int) nn;
}
/* If the user asked for a particular representation, always use it.
If repr = ECM_MOD_DEFAULT, use the thresholds.
Don't use base2 if repr = ECM_MOD_NOBASE2.
If a value is <= -16 or >= 16, it is a base2 exponent.
Return a non-zero value if an error occurred.
*/
int
mpmod_init (mpmod_t modulus, const mpz_t N, int repr)
{
int base2 = 0, r = 0;
mp_size_t n = mpz_size (N);
switch (repr)
{
case ECM_MOD_DEFAULT:
if ((base2 = isbase2 (N, BASE2_THRESHOLD)))
{
repr = ECM_MOD_BASE2;
break;
}
/* else go through */
#if defined( __GNUC__ ) && __GNUC__ >= 7 && !defined(__ICC)
__attribute__ ((fallthrough));
#endif
case ECM_MOD_NOBASE2:
if (mpz_size (N) < MPZMOD_THRESHOLD)
repr = ECM_MOD_MODMULN;
else if (mpz_size (N) < REDC_THRESHOLD)
repr = ECM_MOD_MPZ;
else
repr = ECM_MOD_REDC;
}
/* now repr is {ECM_MOD_BASE2, ECM_MOD_MODMULN, ECM_MOD_MPZ, ECM_MOD_REDC},
or |repr| >= 16. */
switch (repr)
{
case ECM_MOD_MPZ:
outputf (OUTPUT_VERBOSE, "Using mpz_mod\n");
mpmod_init_MPZ (modulus, N);
break;
case ECM_MOD_MODMULN:
outputf (OUTPUT_VERBOSE, "Using MODMULN [mulredc:%d, sqrredc:%d]\n",
(n <= MULREDC_ASSEMBLY_MAX) ? tune_mulredc_table[n] : 4,
(n <= MULREDC_ASSEMBLY_MAX) ? tune_sqrredc_table[n] : 4);
mpmod_init_MODMULN (modulus, N);
break;
case ECM_MOD_REDC:
outputf (OUTPUT_VERBOSE, "Using REDC\n");
mpmod_init_REDC (modulus, N);
break;
default: /* base2 case: either repr=ECM_MOD_BASE2, and base2 was
determined above, or |repr| >= 16, and we want base2 = repr */
if (repr != ECM_MOD_BASE2)
base2 = repr;
r = mpmod_init_BASE2 (modulus, base2, N);
break;
}
return r;
}
void
mpres_clear (mpres_t a, ATTRIBUTE_UNUSED const mpmod_t modulus)
{
mpz_clear (a);
PTR(a) = NULL; /* Make sure we segfault if we access it again */
}
void
mpmod_init_MPZ (mpmod_t modulus, const mpz_t N)
{
size_t n;
mpz_init_set (modulus->orig_modulus, N);
modulus->repr = ECM_MOD_MPZ;
n = mpz_size (N); /* number of limbs of N */
modulus->bits = n * GMP_NUMB_BITS; /* Number of bits,
rounded up to full limb */
mpz_init2 (modulus->temp1, 2UL * modulus->bits + GMP_NUMB_BITS);
mpz_init2 (modulus->temp2, modulus->bits);
mpz_init2 (modulus->aux_modulus, modulus->bits);
mpz_set_ui (modulus->aux_modulus, 1UL);
/* we precompute B^(n + ceil(n/2)) mod N, where B=2^GMP_NUMB_BITS */
mpz_mul_2exp (modulus->aux_modulus, modulus->aux_modulus,
(n + (n + 1) / 2) * GMP_NUMB_BITS);
mpz_mod (modulus->aux_modulus, modulus->aux_modulus, N);
return;
}
int
mpmod_init_BASE2 (mpmod_t modulus, const int base2, const mpz_t N)
{
int Nbits;
outputf (OUTPUT_VERBOSE,
"Using special division for factor of 2^%d%c1\n",
abs (base2), (base2 < 0) ? '-' : '+');
mpz_init_set (modulus->orig_modulus, N);
modulus->repr = ECM_MOD_BASE2;
modulus->bits = base2;
Nbits = mpz_size (N) * GMP_NUMB_BITS; /* Number of bits, rounded
up to full limb */
mpz_init2 (modulus->temp1, 2UL * Nbits + GMP_NUMB_BITS);
mpz_init2 (modulus->temp2, Nbits);
mpz_set_ui (modulus->temp1, 1UL);
mpz_mul_2exp (modulus->temp1, modulus->temp1, abs (base2));
if (base2 < 0)
mpz_sub_ui (modulus->temp1, modulus->temp1, 1UL);
else
mpz_add_ui (modulus->temp1, modulus->temp1, 1UL);
if (!mpz_divisible_p (modulus->temp1, N))
{
outputf (OUTPUT_ERROR, "mpmod_init_BASE2: n does not divide 2^%d%c1\n",
abs (base2), base2 < 0 ? '-' : '+');
mpz_clear (modulus->temp2);
mpz_clear (modulus->temp1);
mpz_clear (modulus->orig_modulus);
return ECM_ERROR;
}
modulus->Fermat = 0;
if (base2 > 0)
{
unsigned long i;
for (i = base2; (i & 1) == 0; i >>= 1);
if (i == 1)
{
modulus->Fermat = base2;
}
}
return 0;
}
/* initialize the following fields:
orig_modulus - the original modulus
bits - # of bits of N, rounded up to a multiple of GMP_NUMB_BITS
temp1, temp2 - auxiliary variables
Nprim - -1/N mod B^n where B=2^GMP_NUMB_BITS and n = #limbs(N)
R2 - (2^bits)^2 (mod N)
R3 - (2^bits)^3 (mod N)
multiple - smallest multiple of N >= 2^bits
*/
void
mpmod_init_MODMULN (mpmod_t modulus, const mpz_t N)
{
int Nbits;
mpz_init_set (modulus->orig_modulus, N);
modulus->repr = ECM_MOD_MODMULN;
Nbits = mpz_size (N) * GMP_NUMB_BITS; /* Number of bits, rounded
up to full limb */
modulus->bits = Nbits;
mpz_init2 (modulus->temp1, 2UL * Nbits + GMP_NUMB_BITS);
mpz_init2 (modulus->temp2, Nbits + 1);
modulus->Nprim = (mp_limb_t*) malloc (mpz_size (N) * sizeof (mp_limb_t));
mpz_init2 (modulus->R2, Nbits);
mpz_set_ui (modulus->temp1, 1UL);
mpz_mul_2exp (modulus->temp1, modulus->temp1, 2 * Nbits);
mpz_mod (modulus->R2, modulus->temp1, modulus->orig_modulus);
/* Now R2 = (2^bits)^2 (mod N) */
mpz_init2 (modulus->R3, Nbits);
mpz_mul_2exp (modulus->temp1, modulus->R2, Nbits);
mpz_mod (modulus->R3, modulus->temp1, modulus->orig_modulus);
/* Now R3 = (2^bits)^3 (mod N) */
mpz_init2 (modulus->multiple, Nbits);
mpz_set_ui (modulus->temp1, 1UL);
mpz_mul_2exp (modulus->temp1, modulus->temp1, Nbits);
/* compute ceil(2^bits / N) */
mpz_cdiv_q (modulus->temp1, modulus->temp1, modulus->orig_modulus);
mpz_mul (modulus->multiple, modulus->temp1, modulus->orig_modulus);
/* Now multiple is the smallest multiple of N >= 2^bits */
mpz_set_ui (modulus->temp1, 1UL);
mpz_mul_2exp (modulus->temp1, modulus->temp1, Nbits);
/* since we directly check even modulus in ecm/pm1/pp1,
N is odd here, thus 1/N mod 2^Nbits always exist */
mpz_invert (modulus->temp2, N, modulus->temp1); /* temp2 = 1/N mod B^n */
mpz_sub (modulus->temp2, modulus->temp1, modulus->temp2);
/* temp2 = -1/N mod B^n */
/* ensure Nprim has all its n limbs correctly set, for ecm_redc_n */
MPN_ZERO(modulus->Nprim, mpz_size (N));
mpn_copyi (modulus->Nprim, PTR(modulus->temp2), ABSIZ(modulus->temp2));
}
void
mpmod_init_REDC (mpmod_t modulus, const mpz_t N)
{
mp_size_t n;
int Nbits;
mpz_init_set (modulus->orig_modulus, N);
n = mpz_size (N);
modulus->repr = ECM_MOD_REDC;
Nbits = n * GMP_NUMB_BITS; /* Number of bits, rounded
up to full limb */
modulus->bits = Nbits;
mpz_init2 (modulus->temp1, 2 * Nbits + GMP_NUMB_BITS);
mpz_init2 (modulus->temp2, Nbits);
mpz_init2 (modulus->aux_modulus, Nbits);
mpz_set_ui (modulus->temp1, 1UL);
mpz_mul_2exp (modulus->temp1, modulus->temp1, Nbits);
/* since we directly check even modulus in ecm/pm1/pp1,
N is odd here, thus 1/N mod 2^Nbits always exist */
mpz_invert (modulus->aux_modulus, N, modulus->temp1);
mpz_sub (modulus->aux_modulus, modulus->temp1, modulus->aux_modulus);
/* ensure aux_modulus has n allocated limbs, for ecm_redc_n */
if (ABSIZ(modulus->aux_modulus) < n)
{
_mpz_realloc (modulus->aux_modulus, n);
/* in case the reallocation fails, _mpz_realloc sets the value to 0 */
ASSERT_ALWAYS (mpz_cmp_ui (modulus->aux_modulus, 0) != 0);
MPN_ZERO (PTR(modulus->aux_modulus) + ABSIZ(modulus->aux_modulus),
n - ABSIZ(modulus->aux_modulus));
}
mpz_init2 (modulus->R2, Nbits);
mpz_set_ui (modulus->temp1, 1UL);
mpz_mul_2exp (modulus->temp1, modulus->temp1, 2 * Nbits);
mpz_mod (modulus->R2, modulus->temp1, modulus->orig_modulus);
/* Now R2 = (2^bits)^2 (mod N) */
mpz_init2 (modulus->R3, Nbits);
mpz_mul_2exp (modulus->temp1, modulus->R2, Nbits);
mpz_mod (modulus->R3, modulus->temp1, modulus->orig_modulus);
/* Now R3 = (2^bits)^3 (mod N) */
mpz_init (modulus->multiple);
mpz_set_ui (modulus->temp1, 1UL);
mpz_mul_2exp (modulus->temp1, modulus->temp1, Nbits);
/* compute ceil(2^bits / N) */
mpz_cdiv_q (modulus->temp1, modulus->temp1, modulus->orig_modulus);
mpz_mul (modulus->multiple, modulus->temp1, modulus->orig_modulus);
/* Now multiple is the largest multiple of N >= 2^bits */
}
void
mpmod_clear (mpmod_t modulus)
{
mpz_clear (modulus->orig_modulus);