Line data Source code
1 : /* Copyright (C) 2000-2004 The PARI group.
2 :
3 : This file is part of the PARI/GP package.
4 :
5 : PARI/GP is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 2 of the License, or (at your option) any later
8 : version. It is distributed in the hope that it will be useful, but WITHOUT
9 : ANY WARRANTY WHATSOEVER.
10 :
11 : Check the License for details. You should have received a copy of it, along
12 : with the package; see the file 'COPYING'. If not, write to the Free Software
13 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 :
15 : /***********************************************************************/
16 : /** **/
17 : /** GENERIC ALGORITHMS ON BLACKBOX GROUP **/
18 : /** **/
19 : /***********************************************************************/
20 : #include "pari.h"
21 : #include "paripriv.h"
22 : #undef pow /* AIX: pow(a,b) is a macro, wrongly expanded on grp->pow(a,b,c) */
23 :
24 : #define DEBUGLEVEL DEBUGLEVEL_bb_group
25 :
26 : /***********************************************************************/
27 : /** **/
28 : /** POWERING **/
29 : /** **/
30 : /***********************************************************************/
31 :
32 : /* return (n>>(i+1-l)) & ((1<<l)-1) */
33 : static ulong
34 8319175 : int_block(GEN n, long i, long l)
35 : {
36 8319175 : long q = divsBIL(i), r = remsBIL(i)+1, lr;
37 8319443 : GEN nw = int_W(n, q);
38 8319443 : ulong w = (ulong) *nw, w2;
39 8319443 : if (r>=l) return (w>>(r-l))&((1UL<<l)-1);
40 571556 : w &= (1UL<<r)-1; lr = l-r;
41 571556 : w2 = (ulong) *int_precW(nw); w2 >>= (BITS_IN_LONG-lr);
42 571556 : return (w<<lr)|w2;
43 : }
44 :
45 : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
46 : static GEN
47 9312990 : sliding_window_powu(GEN x, ulong n, long e, void *E, GEN (*sqr)(void*,GEN),
48 : GEN (*mul)(void*,GEN,GEN))
49 : {
50 9312990 : long i, l = expu(n), u = (1UL<<(e-1));
51 9312708 : GEN tab = cgetg(1+u, t_VEC), x2 = sqr(E, x), z = NULL;
52 :
53 9313422 : gel(tab, 1) = x;
54 24631580 : for (i = 2; i <= u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
55 68933620 : while (l >= 0)
56 : {
57 : long w, v;
58 : GEN tw;
59 59745031 : if (e > l+1) e = l+1;
60 59745031 : w = (n>>(l+1-e)) & ((1UL<<e)-1); v = vals(w); l-=e;
61 59769995 : tw = gel(tab, 1 + (w>>(v+1)));
62 59769995 : if (!z) z = tw;
63 : else
64 : {
65 137284260 : for (i = 1; i <= e-v; i++) z = sqr(E, z);
66 50280997 : z = mul(E, z, tw);
67 : }
68 97362035 : for (i = 1; i <= v; i++) z = sqr(E, z);
69 109850781 : while (l >= 0)
70 : {
71 100775110 : if (n&(1UL<<l)) break;
72 50219740 : z = sqr(E, z); l--;
73 : }
74 : }
75 9188589 : return z;
76 : }
77 :
78 : /* assume n != 0, t_INT. Compute x^|n| using sliding window powering */
79 : static GEN
80 226112 : sliding_window_pow(GEN x, GEN n, long e, void *E, GEN (*sqr)(void*,GEN),
81 : GEN (*mul)(void*,GEN,GEN))
82 : {
83 : pari_sp av;
84 226112 : long i, l = expi(n), u = (1UL<<(e-1));
85 226111 : GEN tab = cgetg(1+u, t_VEC);
86 226111 : GEN x2 = sqr(E, x), z = NULL;
87 :
88 226270 : gel(tab, 1) = x;
89 2994932 : for (i=2; i<=u; i++) gel(tab,i) = mul(E, gel(tab,i-1), x2);
90 226115 : av = avma;
91 7963251 : while (l >= 0)
92 : {
93 : long w, v;
94 : GEN tw;
95 7713778 : if (e > l+1) e = l+1;
96 7713778 : w = int_block(n,l,e); v = vals(w); l-=e;
97 7712348 : tw = gel(tab, 1+(w>>(v+1)));
98 7712348 : if (!z) z = tw;
99 : else
100 : {
101 40784046 : for (i = 1; i <= e-v; i++) z = sqr(E, z);
102 7487332 : z = mul(E, z, tw);
103 : }
104 14658600 : for (i = 1; i <= v; i++) z = sqr(E, z);
105 20736318 : while (l >= 0)
106 : {
107 20487111 : if (gc_needed(av,1))
108 : {
109 1030 : if (DEBUGMEM>1) pari_warn(warnmem,"sliding_window_pow (%ld)", l);
110 1030 : z = gerepilecopy(av, z);
111 : }
112 20487111 : if (int_bit(n,l)) break;
113 12985306 : z = sqr(E, z); l--;
114 : }
115 : }
116 249473 : return z;
117 : }
118 :
119 : /* assume n != 0, t_INT. Compute x^|n| using leftright binary powering */
120 : static GEN
121 99715827 : leftright_binary_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
122 : GEN (*mul)(void*,GEN,GEN))
123 : {
124 99715827 : pari_sp av = avma;
125 : GEN y;
126 : int j;
127 :
128 99715827 : if (n == 1) return x;
129 99715827 : y = x; j = 1+bfffo(n);
130 : /* normalize, i.e set highest bit to 1 (we know n != 0) */
131 99715827 : n<<=j; j = BITS_IN_LONG-j;
132 : /* first bit is now implicit */
133 325263059 : for (; j; n<<=1,j--)
134 : {
135 225595316 : y = sqr(E,y);
136 225554959 : if (n & HIGHBIT) y = mul(E,y,x); /* first bit set: multiply by base */
137 225538626 : if (gc_needed(av,1))
138 : {
139 0 : if (DEBUGMEM>1) pari_warn(warnmem,"leftright_powu (%d)", j);
140 0 : y = gerepilecopy(av, y);
141 : }
142 : }
143 99667743 : return y;
144 : }
145 :
146 : GEN
147 111671067 : gen_powu_i(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
148 : GEN (*mul)(void*,GEN,GEN))
149 : {
150 111671067 : if (n == 1) return x;
151 109024716 : if (n < 512)
152 99714913 : return leftright_binary_powu(x, n, E, sqr, mul);
153 : else
154 9309803 : return sliding_window_powu(x, n, n < (1UL<<25)? 2: 3, E, sqr, mul);
155 : }
156 :
157 : GEN
158 178530 : gen_powu(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
159 : GEN (*mul)(void*,GEN,GEN))
160 : {
161 178530 : pari_sp av = avma;
162 178530 : if (n == 1) return gcopy(x);
163 154792 : return gerepilecopy(av, gen_powu_i(x,n,E,sqr,mul));
164 : }
165 :
166 : GEN
167 40652387 : gen_pow_i(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
168 : GEN (*mul)(void*,GEN,GEN))
169 : {
170 : long l, e;
171 40652387 : if (lgefint(n)==3) return gen_powu_i(x, uel(n,2), E, sqr, mul);
172 226108 : l = expi(n);
173 226113 : if (l<=64) e = 3;
174 167460 : else if (l<=160) e = 4;
175 85790 : else if (l<=384) e = 5;
176 22987 : else if (l<=896) e = 6;
177 11516 : else e = 7;
178 226113 : return sliding_window_pow(x, n, e, E, sqr, mul);
179 : }
180 :
181 : GEN
182 11034683 : gen_pow(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
183 : GEN (*mul)(void*,GEN,GEN))
184 : {
185 11034683 : pari_sp av = avma;
186 11034683 : return gerepilecopy(av, gen_pow_i(x,n,E,sqr,mul));
187 : }
188 :
189 : /* assume n > 0. Compute x^n using left-right binary powering */
190 : GEN
191 1324410 : gen_powu_fold_i(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
192 : GEN (*msqr)(void*,GEN))
193 : {
194 1324410 : pari_sp av = avma;
195 : GEN y;
196 : int j;
197 :
198 1324410 : if (n == 1) return x;
199 1324410 : y = x; j = 1+bfffo(n);
200 : /* normalize, i.e set highest bit to 1 (we know n != 0) */
201 1324410 : n<<=j; j = BITS_IN_LONG-j;
202 : /* first bit is now implicit */
203 5892921 : for (; j; n<<=1,j--)
204 : {
205 4568557 : if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
206 3346357 : else y = sqr(E,y);
207 4568523 : if (gc_needed(av,1))
208 : {
209 0 : if (DEBUGMEM>1) pari_warn(warnmem,"gen_powu_fold (%d)", j);
210 0 : y = gerepilecopy(av, y);
211 : }
212 : }
213 1324364 : return y;
214 : }
215 : GEN
216 5029 : gen_powu_fold(GEN x, ulong n, void *E, GEN (*sqr)(void*,GEN),
217 : GEN (*msqr)(void*,GEN))
218 : {
219 5029 : pari_sp av = avma;
220 5029 : if (n == 1) return gcopy(x);
221 5029 : return gerepilecopy(av, gen_powu_fold_i(x,n,E,sqr,msqr));
222 : }
223 :
224 : /* assume N != 0, t_INT. Compute x^|N| using left-right binary powering */
225 : GEN
226 687604 : gen_pow_fold_i(GEN x, GEN N, void *E, GEN (*sqr)(void*,GEN),
227 : GEN (*msqr)(void*,GEN))
228 : {
229 687604 : long ln = lgefint(N);
230 687604 : if (ln == 3) return gen_powu_fold_i(x, N[2], E, sqr, msqr);
231 : else
232 : {
233 145800 : GEN nd = int_MSW(N), y = x;
234 145800 : ulong n = *nd;
235 : long i;
236 : int j;
237 145800 : pari_sp av = avma;
238 :
239 145800 : if (n == 1)
240 7354 : j = 0;
241 : else
242 : {
243 138446 : j = 1+bfffo(n); /* < BIL */
244 : /* normalize, i.e set highest bit to 1 (we know n != 0) */
245 138446 : n <<= j; j = BITS_IN_LONG - j;
246 : }
247 : /* first bit is now implicit */
248 145800 : for (i=ln-2;;)
249 : {
250 54390861 : for (; j; n<<=1,j--)
251 : {
252 53599065 : if (n & HIGHBIT) y = msqr(E,y); /* first bit set: multiply by base */
253 37462476 : else y = sqr(E,y);
254 53607422 : if (gc_needed(av,1))
255 : {
256 0 : if (DEBUGMEM>1) pari_warn(warnmem,"gen_pow_fold (%ld,%d)", i,j);
257 0 : y = gerepilecopy(av, y);
258 : }
259 : }
260 843839 : if (--i == 0) return y;
261 883951 : nd = int_precW(nd);
262 883951 : n = *nd; j = BITS_IN_LONG;
263 : }
264 : }
265 : }
266 : GEN
267 541804 : gen_pow_fold(GEN x, GEN n, void *E, GEN (*sqr)(void*,GEN),
268 : GEN (*msqr)(void*,GEN))
269 : {
270 541804 : pari_sp av = avma;
271 541804 : return gerepilecopy(av, gen_pow_fold_i(x,n,E,sqr,msqr));
272 : }
273 :
274 : GEN
275 147 : gen_pow_init(GEN x, GEN n, long k, void *E, GEN (*sqr)(void*,GEN), GEN (*mul)(void*,GEN,GEN))
276 : {
277 147 : long i, j, l = expi(n);
278 147 : long m = 1UL<<(k-1);
279 147 : GEN x2 = sqr(E, x), y = gcopy(x);
280 147 : GEN R = cgetg(m+1, t_VEC);
281 644 : for(i = 1; i <= m; i++)
282 : {
283 497 : GEN C = cgetg(l+1, t_VEC);
284 497 : gel(C,1) = y;
285 27118 : for(j = 2; j <= l; j++)
286 26621 : gel(C,j) = sqr(E, gel(C,j-1));
287 497 : gel(R,i) = C;
288 497 : y = mul(E, y, x2);
289 : }
290 147 : return R;
291 : }
292 :
293 : GEN
294 53331 : gen_pow_table(GEN R, GEN n, void *E, GEN (*one)(void*), GEN (*mul)(void*,GEN,GEN))
295 : {
296 53331 : long e = expu(lg(R)-1) + 1;
297 53331 : long l = expi(n);
298 : long i, w;
299 53331 : GEN z = one(E), tw;
300 1241311 : for(i=0; i<=l; )
301 : {
302 1187980 : if (int_bit(n, i)==0) { i++; continue; }
303 605960 : if (i+e-1>l) e = l+1-i;
304 605960 : w = int_block(n,i+e-1,e);
305 605960 : tw = gmael(R, 1+(w>>1), i+1);
306 605960 : z = mul(E, z, tw);
307 605960 : i += e;
308 : }
309 53331 : return z;
310 : }
311 :
312 : GEN
313 28628612 : gen_powers(GEN x, long l, int use_sqr, void *E, GEN (*sqr)(void*,GEN),
314 : GEN (*mul)(void*,GEN,GEN), GEN (*one)(void*))
315 : {
316 : long i;
317 28628612 : GEN V = cgetg(l+2,t_VEC);
318 28628946 : gel(V,1) = one(E); if (l==0) return V;
319 28601993 : gel(V,2) = gcopy(x); if (l==1) return V;
320 12559348 : gel(V,3) = sqr(E,x);
321 12564073 : if (use_sqr)
322 40464764 : for(i = 4; i < l+2; i++)
323 30866641 : gel(V,i) = (i&1)? sqr(E,gel(V, (i+1)>>1))
324 30867360 : : mul(E,gel(V, i-1),x);
325 : else
326 7284939 : for(i = 4; i < l+2; i++)
327 4318970 : gel(V,i) = mul(E,gel(V,i-1),x);
328 12563373 : return V;
329 : }
330 :
331 : GEN
332 57285992 : producttree_scheme(long n)
333 : {
334 : GEN v, w;
335 : long i, j, k, u, l;
336 57285992 : if (n<=2) return mkvecsmall(n);
337 47703194 : u = expu(n-1);
338 47703214 : v = cgetg(n+1,t_VECSMALL);
339 47703255 : w = cgetg(n+1,t_VECSMALL);
340 47703606 : v[1] = n; l = 1;
341 155866178 : for (i=1; i<=u; i++)
342 : {
343 415572299 : for(j=1, k=1; j<=l; j++, k+=2)
344 : {
345 307409727 : long vj = v[j], v2 = vj>>1;
346 307409727 : w[k] = vj-v2;
347 307409727 : w[k+1] = v2;
348 : }
349 108162572 : swap(v,w); l<<=1;
350 : }
351 47703606 : fixlg(v, l+1); set_avma((pari_sp)v); return v;
352 : }
353 :
354 : GEN
355 60702195 : gen_product(GEN x, void *E, GEN (*mul)(void *,GEN,GEN))
356 : {
357 : pari_sp av;
358 60702195 : long i, k, l = lg(x);
359 : pari_timer ti;
360 : GEN y, v;
361 :
362 60702195 : if (l <= 2) return l == 1? gen_1: gcopy(gel(x,1));
363 56628782 : y = cgetg(l, t_VEC); av = avma;
364 56629089 : v = producttree_scheme(l-1);
365 56629527 : l = lg(v);
366 56629527 : if (DEBUGLEVEL>7) timer_start(&ti);
367 414002736 : for (k = i = 1; k < l; i += v[k++])
368 357375803 : gel(y,k) = v[k]==1? gel(x,i): mul(E, gel(x,i), gel(x,i+1));
369 163027582 : while (k > 2)
370 : {
371 106398362 : long n = k - 1;
372 106398362 : if (DEBUGLEVEL>7) timer_printf(&ti,"gen_product: remaining objects %ld",n);
373 407127615 : for (k = i = 1; i < n; i += 2) gel(y,k++) = mul(E, gel(y,i), gel(y,i+1));
374 106399560 : if (gc_needed(av,1)) gerepilecoeffs(av, y+1, k-1);
375 : }
376 56629220 : return gel(y,1);
377 : }
378 :
379 : /***********************************************************************/
380 : /** **/
381 : /** DISCRETE LOGARITHM **/
382 : /** **/
383 : /***********************************************************************/
384 : static GEN
385 68727636 : iter_rho(GEN x, GEN g, GEN q, GEN A, ulong h, void *E, const struct bb_group *grp)
386 : {
387 68727636 : GEN a = gel(A,1), b = gel(A,2), c = gel(A,3);
388 68727636 : switch((h | grp->hash(a)) % 3UL)
389 : {
390 22922806 : case 0: return mkvec3(grp->pow(E,a,gen_2), Fp_mulu(b,2,q), Fp_mulu(c,2,q));
391 22903981 : case 1: return mkvec3(grp->mul(E,a,x), addiu(b,1), c);
392 22900849 : case 2: return mkvec3(grp->mul(E,a,g), b, addiu(c,1));
393 : }
394 : return NULL; /* LCOV_EXCL_LINE */
395 : }
396 :
397 : /*Generic Pollard rho discrete log algorithm*/
398 : static GEN
399 49 : gen_Pollard_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
400 : {
401 49 : pari_sp av=avma;
402 49 : GEN A, B, l, sqrt4q = sqrti(shifti(q,4));
403 49 : ulong i, h = 0, imax = itou_or_0(sqrt4q);
404 49 : if (!imax) imax = ULONG_MAX;
405 : do {
406 49 : rho_restart:
407 49 : A = B = mkvec3(x,gen_1,gen_0);
408 49 : i=0;
409 : do {
410 22909212 : if (i>imax)
411 : {
412 0 : h++;
413 0 : if (DEBUGLEVEL)
414 0 : pari_warn(warner,"changing Pollard rho hash seed to %ld",h);
415 0 : goto rho_restart;
416 : }
417 22909212 : A = iter_rho(x, g, q, A, h, E, grp);
418 22909212 : B = iter_rho(x, g, q, B, h, E, grp);
419 22909212 : B = iter_rho(x, g, q, B, h, E, grp);
420 22909212 : if (gc_needed(av,2))
421 : {
422 1602 : if(DEBUGMEM>1) pari_warn(warnmem,"gen_Pollard_log");
423 1602 : gerepileall(av, 2, &A, &B);
424 : }
425 22909212 : i++;
426 22909212 : } while (!grp->equal(gel(A,1), gel(B,1)));
427 49 : gel(A,2) = modii(gel(A,2), q);
428 49 : gel(B,2) = modii(gel(B,2), q);
429 49 : h++;
430 49 : } while (equalii(gel(A,2), gel(B,2)));
431 49 : l = Fp_div(Fp_sub(gel(B,3), gel(A,3),q),Fp_sub(gel(A,2), gel(B,2), q), q);
432 49 : return gerepileuptoint(av, l);
433 : }
434 :
435 : /* compute a hash of g^(i-1), 1<=i<=n. Return [sorted hash, perm, g^-n] */
436 : GEN
437 723137 : gen_Shanks_init(GEN g, long n, void *E, const struct bb_group *grp)
438 : {
439 723137 : GEN p1 = g, G, perm, table = cgetg(n+1,t_VECSMALL);
440 723137 : pari_sp av=avma;
441 : long i;
442 723137 : table[1] = grp->hash(grp->pow(E,g,gen_0));
443 4107090 : for (i=2; i<=n; i++)
444 : {
445 3383953 : table[i] = grp->hash(p1);
446 3383953 : p1 = grp->mul(E,p1,g);
447 3383953 : if (gc_needed(av,2))
448 : {
449 0 : if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
450 0 : p1 = gerepileupto(av, p1);
451 : }
452 : }
453 723137 : G = gerepileupto(av, grp->pow(E,p1,gen_m1)); /* g^-n */
454 723137 : perm = vecsmall_indexsort(table);
455 723137 : table = vecsmallpermute(table,perm);
456 723137 : return mkvec4(table,perm,g,G);
457 : }
458 : /* T from gen_Shanks_init(g,n). Return v < n*N such that x = g^v or NULL */
459 : GEN
460 728513 : gen_Shanks(GEN T, GEN x, ulong N, void *E, const struct bb_group *grp)
461 : {
462 728513 : pari_sp av=avma;
463 728513 : GEN table = gel(T,1), perm = gel(T,2), g = gel(T,3), G = gel(T,4);
464 728513 : GEN p1 = x;
465 728513 : long n = lg(table)-1;
466 : ulong k;
467 4360123 : for (k=0; k<N; k++)
468 : { /* p1 = x G^k, G = g^-n */
469 4025990 : long h = grp->hash(p1), i = zv_search(table, h);
470 4025990 : if (i)
471 : {
472 395190 : do i--; while (i && table[i] == h);
473 394380 : for (i++; i <= n && table[i] == h; i++)
474 : {
475 394380 : GEN v = addiu(muluu(n,k), perm[i]-1);
476 394380 : if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
477 0 : if (DEBUGLEVEL)
478 0 : err_printf("gen_Shanks_log: false positive %lu, %lu\n", k,h);
479 : }
480 : }
481 3631610 : p1 = grp->mul(E,p1,G);
482 3631610 : if (gc_needed(av,2))
483 : {
484 0 : if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %lu", k);
485 0 : p1 = gerepileupto(av, p1);
486 : }
487 : }
488 334133 : return NULL;
489 : }
490 : /* Generic Shanks baby-step/giant-step algorithm. Return log_g(x), ord g = q.
491 : * One-shot: use gen_Shanks_init/log if many logs are desired; early abort
492 : * if log < sqrt(q) */
493 : static GEN
494 1516423 : gen_Shanks_log(GEN x, GEN g, GEN q, void *E, const struct bb_group *grp)
495 : {
496 1516423 : pari_sp av=avma, av1;
497 : long lbaby, i, k;
498 : GEN p1, table, giant, perm, ginv;
499 1516423 : p1 = sqrti(q);
500 1516422 : if (abscmpiu(p1,LGBITS) >= 0)
501 0 : pari_err_OVERFLOW("gen_Shanks_log [order too large]");
502 1516422 : lbaby = itos(p1)+1; table = cgetg(lbaby+1,t_VECSMALL);
503 1516422 : ginv = grp->pow(E,g,gen_m1);
504 1516423 : av1 = avma;
505 5029029 : for (p1=x, i=1;;i++)
506 : {
507 5029029 : if (grp->equal1(p1)) return gc_stoi(av, i-1);
508 4878160 : table[i] = grp->hash(p1); if (i==lbaby) break;
509 3512604 : p1 = grp->mul(E,p1,ginv);
510 3512606 : if (gc_needed(av1,2))
511 : {
512 6 : if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, baby = %ld", i);
513 6 : p1 = gerepileupto(av1, p1);
514 : }
515 : }
516 1365555 : p1 = giant = gerepileupto(av1, grp->mul(E,x,grp->pow(E, p1, gen_m1)));
517 1365555 : perm = vecsmall_indexsort(table);
518 1365555 : table = vecsmallpermute(table,perm);
519 1365555 : av1 = avma;
520 2203280 : for (k=1; k<= lbaby; k++)
521 : {
522 2203280 : long h = grp->hash(p1), i = zv_search(table, h);
523 2203280 : if (i)
524 : {
525 2731112 : while (table[i] == h && i) i--;
526 1365557 : for (i++; i <= lbaby && table[i] == h; i++)
527 : {
528 1365556 : GEN v = addiu(mulss(lbaby-1,k),perm[i]-1);
529 1365554 : if (grp->equal(grp->pow(E,g,v),x)) return gerepileuptoint(av,v);
530 1 : if (DEBUGLEVEL)
531 0 : err_printf("gen_Shanks_log: false positive %ld, %lu\n", k,h);
532 : }
533 : }
534 837725 : p1 = grp->mul(E,p1,giant);
535 837725 : if (gc_needed(av1,2))
536 : {
537 0 : if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_log, k = %ld", k);
538 0 : p1 = gerepileupto(av1, p1);
539 : }
540 : }
541 0 : set_avma(av); return cgetg(1, t_VEC); /* no solution */
542 : }
543 :
544 : /*Generic discrete logarithme in a group of prime order p*/
545 : GEN
546 6131233 : gen_plog(GEN x, GEN g, GEN p, void *E, const struct bb_group *grp)
547 : {
548 6131233 : if (grp->easylog)
549 : {
550 6053888 : GEN e = grp->easylog(E, x, g, p);
551 6053889 : if (e) return e;
552 : }
553 2754983 : if (grp->equal1(x)) return gen_0;
554 2748669 : if (grp->equal(x,g)) return gen_1;
555 1516472 : if (expi(p)<32) return gen_Shanks_log(x,g,p,E,grp);
556 49 : return gen_Pollard_log(x, g, p, E, grp);
557 : }
558 :
559 : GEN
560 11132507 : get_arith_ZZM(GEN o)
561 : {
562 11132507 : if (!o) return NULL;
563 11132507 : switch(typ(o))
564 : {
565 3703062 : case t_INT:
566 3703062 : if (signe(o) > 0) return mkvec2(o, Z_factor(o));
567 7 : break;
568 1369945 : case t_MAT:
569 1369945 : if (is_Z_factorpos(o)) return mkvec2(factorback(o), o);
570 14 : break;
571 6059499 : case t_VEC:
572 6059499 : if (lg(o) == 3 && signe(gel(o,1)) > 0 && is_Z_factorpos(gel(o,2))) return o;
573 0 : break;
574 : }
575 24 : pari_err_TYPE("generic discrete logarithm (order factorization)",o);
576 : return NULL; /* LCOV_EXCL_LINE */
577 : }
578 : GEN
579 1365664 : get_arith_Z(GEN o)
580 : {
581 1365664 : if (!o) return NULL;
582 1365664 : switch(typ(o))
583 : {
584 1118198 : case t_INT:
585 1118198 : if (signe(o) > 0) return o;
586 7 : break;
587 14 : case t_MAT:
588 14 : o = factorback(o);
589 0 : if (typ(o) == t_INT && signe(o) > 0) return o;
590 0 : break;
591 247445 : case t_VEC:
592 247445 : if (lg(o) != 3) break;
593 247445 : o = gel(o,1);
594 247445 : if (typ(o) == t_INT && signe(o) > 0) return o;
595 0 : break;
596 : }
597 14 : pari_err_TYPE("generic discrete logarithm (order factorization)",o);
598 : return NULL; /* LCOV_EXCL_LINE */
599 : }
600 :
601 : /* Generic Pohlig-Hellman discrete logarithm: smallest integer n >= 0 such that
602 : * g^n=a. Assume ord(g) | ord; grp->easylog() is an optional trapdoor
603 : * function that catches easy logarithms */
604 : GEN
605 4687613 : gen_PH_log(GEN a, GEN g, GEN ord, void *E, const struct bb_group *grp)
606 : {
607 4687613 : pari_sp av = avma;
608 : GEN v, ginv, fa, ex;
609 : long i, j, l;
610 :
611 4687613 : if (grp->equal(g, a)) /* frequent special case */
612 977678 : return grp->equal1(g)? gen_0: gen_1;
613 3709937 : if (grp->easylog)
614 : {
615 3660037 : GEN e = grp->easylog(E, a, g, ord);
616 3660008 : if (e) return e;
617 : }
618 2151584 : v = get_arith_ZZM(ord);
619 2151616 : ord= gel(v,1);
620 2151616 : fa = gel(v,2);
621 2151616 : ex = gel(fa,2);
622 2151616 : fa = gel(fa,1); l = lg(fa);
623 2151616 : ginv = grp->pow(E,g,gen_m1);
624 2151617 : v = cgetg(l, t_VEC);
625 6242955 : for (i = 1; i < l; i++)
626 : {
627 4091346 : GEN q = gel(fa,i), qj, gq, nq, ginv0, a0, t0;
628 4091346 : long e = itos(gel(ex,i));
629 4091346 : if (DEBUGLEVEL>5)
630 0 : err_printf("Pohlig-Hellman: DL mod %Ps^%ld\n",q,e);
631 4091346 : qj = new_chunk(e+1);
632 4091346 : gel(qj,0) = gen_1;
633 4091346 : gel(qj,1) = q;
634 5928940 : for (j = 2; j <= e; j++) gel(qj,j) = mulii(gel(qj,j-1), q);
635 4091346 : t0 = diviiexact(ord, gel(qj,e));
636 4091344 : a0 = grp->pow(E, a, t0);
637 4091346 : ginv0 = grp->pow(E, ginv, t0); /* ord(ginv0) | q^e */
638 4091346 : if (grp->equal1(ginv0)) { gel(v,i) = mkintmod(gen_0, gen_1); continue; }
639 4091339 : do gq = grp->pow(E,g, mulii(t0, gel(qj,--e))); while (grp->equal1(gq));
640 : /* ord(gq) = q */
641 4091332 : nq = gen_0;
642 4091332 : for (j = 0;; j++)
643 1837559 : { /* nq = sum_{i<j} b_i q^i */
644 5928891 : GEN b = grp->pow(E,a0, gel(qj,e-j));
645 : /* cheap early abort: wrong local order */
646 5928891 : if (j == 0 && !grp->equal1(grp->pow(E,b,q))) {
647 7 : set_avma(av); return cgetg(1, t_VEC);
648 : }
649 5928884 : b = gen_plog(b, gq, q, E, grp);
650 5928884 : if (typ(b) != t_INT) { set_avma(av); return cgetg(1, t_VEC); }
651 5928884 : nq = addii(nq, mulii(b, gel(qj,j)));
652 5928883 : if (j == e) break;
653 :
654 1837559 : a0 = grp->mul(E,a0, grp->pow(E,ginv0, b));
655 1837559 : ginv0 = grp->pow(E,ginv0, q);
656 : }
657 4091324 : gel(v,i) = mkintmod(nq, gel(qj,e+1));
658 : }
659 2151609 : return gerepileuptoint(av, lift(chinese1_coprime_Z(v)));
660 : }
661 :
662 : /***********************************************************************/
663 : /** **/
664 : /** ORDER OF AN ELEMENT **/
665 : /** **/
666 : /***********************************************************************/
667 : /*Find the exact order of a assuming a^o==1*/
668 : GEN
669 4088261 : gen_order(GEN a, GEN o, void *E, const struct bb_group *grp)
670 : {
671 4088261 : pari_sp av = avma;
672 : long i, l;
673 : GEN m;
674 :
675 4088261 : m = get_arith_ZZM(o);
676 4088266 : if (!m) pari_err_TYPE("gen_order [missing order]",a);
677 4088266 : o = gel(m,1);
678 4088266 : m = gel(m,2); l = lgcols(m);
679 12493117 : for (i = l-1; i; i--)
680 : {
681 8404899 : GEN t, y, p = gcoeff(m,i,1);
682 8404899 : long j, e = itos(gcoeff(m,i,2));
683 8404902 : if (l == 2) {
684 1026185 : t = gen_1;
685 1026185 : y = a;
686 : } else {
687 7378717 : t = diviiexact(o, powiu(p,e));
688 7378664 : y = grp->pow(E, a, t);
689 : }
690 8404848 : if (grp->equal1(y)) o = t;
691 : else {
692 7851623 : for (j = 1; j < e; j++)
693 : {
694 2857305 : y = grp->pow(E, y, p);
695 2857310 : if (grp->equal1(y)) break;
696 : }
697 5782873 : if (j < e) {
698 788555 : if (j > 1) p = powiu(p, j);
699 788555 : o = mulii(t, p);
700 : }
701 : }
702 : }
703 4088218 : return gerepilecopy(av, o);
704 : }
705 :
706 : /*Find the exact order of a assuming a^o==1, return [order,factor(order)] */
707 : GEN
708 5467 : gen_factored_order(GEN a, GEN o, void *E, const struct bb_group *grp)
709 : {
710 5467 : pari_sp av = avma;
711 : long i, l, ind;
712 : GEN m, F, P;
713 :
714 5467 : m = get_arith_ZZM(o);
715 5467 : if (!m) pari_err_TYPE("gen_factored_order [missing order]",a);
716 5467 : o = gel(m,1);
717 5467 : m = gel(m,2); l = lgcols(m);
718 5467 : P = cgetg(l, t_COL); ind = 1;
719 5467 : F = cgetg(l, t_COL);
720 18879 : for (i = l-1; i; i--)
721 : {
722 13412 : GEN t, y, p = gcoeff(m,i,1);
723 13412 : long j, e = itos(gcoeff(m,i,2));
724 13412 : if (l == 2) {
725 672 : t = gen_1;
726 672 : y = a;
727 : } else {
728 12740 : t = diviiexact(o, powiu(p,e));
729 12740 : y = grp->pow(E, a, t);
730 : }
731 13412 : if (grp->equal1(y)) o = t;
732 : else {
733 16198 : for (j = 1; j < e; j++)
734 : {
735 4655 : y = grp->pow(E, y, p);
736 4655 : if (grp->equal1(y)) break;
737 : }
738 12250 : gel(P,ind) = p;
739 12250 : gel(F,ind) = utoipos(j);
740 12250 : if (j < e) {
741 707 : if (j > 1) p = powiu(p, j);
742 707 : o = mulii(t, p);
743 : }
744 12250 : ind++;
745 : }
746 : }
747 5467 : setlg(P, ind); P = vecreverse(P);
748 5467 : setlg(F, ind); F = vecreverse(F);
749 5467 : return gerepilecopy(av, mkvec2(o, mkmat2(P,F)));
750 : }
751 :
752 : /* E has order o[1], ..., or o[#o], draw random points until all solutions
753 : * but one are eliminated */
754 : GEN
755 980 : gen_select_order(GEN o, void *E, const struct bb_group *grp)
756 : {
757 980 : pari_sp ltop = avma, btop;
758 : GEN lastgood, so, vo;
759 980 : long lo = lg(o), nbo=lo-1;
760 980 : if (nbo == 1) return icopy(gel(o,1));
761 441 : so = ZV_indexsort(o); /* minimize max( o[i+1] - o[i] ) */
762 441 : vo = zero_zv(lo);
763 441 : lastgood = gel(o, so[nbo]);
764 441 : btop = avma;
765 : for(;;)
766 0 : {
767 441 : GEN lasto = gen_0;
768 441 : GEN P = grp->rand(E), t = mkvec(gen_0);
769 : long i;
770 567 : for (i = 1; i < lo; i++)
771 : {
772 567 : GEN newo = gel(o, so[i]);
773 567 : if (vo[i]) continue;
774 567 : t = grp->mul(E,t, grp->pow(E, P, subii(newo,lasto)));/*P^o[i]*/
775 567 : lasto = newo;
776 567 : if (!grp->equal1(t))
777 : {
778 483 : if (--nbo == 1) { set_avma(ltop); return icopy(lastgood); }
779 42 : vo[i] = 1;
780 : }
781 : else
782 84 : lastgood = lasto;
783 : }
784 0 : set_avma(btop);
785 : }
786 : }
787 :
788 : /*******************************************************************/
789 : /* */
790 : /* n-th ROOT */
791 : /* */
792 : /*******************************************************************/
793 : /* Assume l is prime. Return a generator of the l-th Sylow and set *zeta to an element
794 : * of order l.
795 : *
796 : * q = l^e*r, e>=1, (r,l)=1
797 : * UNCLEAN */
798 : static GEN
799 283520 : gen_lgener(GEN l, long e, GEN r,GEN *zeta, void *E, const struct bb_group *grp)
800 : {
801 283520 : const pari_sp av1 = avma;
802 : GEN m, m1;
803 : long i;
804 220070 : for (;; set_avma(av1))
805 : {
806 503592 : m1 = m = grp->pow(E, grp->rand(E), r);
807 503586 : if (grp->equal1(m)) continue;
808 926056 : for (i=1; i<e; i++)
809 : {
810 642538 : m = grp->pow(E,m,l);
811 642534 : if (grp->equal1(m)) break;
812 : }
813 408225 : if (i==e) break;
814 : }
815 283519 : *zeta = m; return m1;
816 : }
817 :
818 : /* Let G be a cyclic group of order o>1. Returns a (random) generator */
819 :
820 : GEN
821 15883 : gen_gener(GEN o, void *E, const struct bb_group *grp)
822 : {
823 15883 : pari_sp ltop = avma, av;
824 : long i, lpr;
825 15883 : GEN F, N, pr, z=NULL;
826 15883 : F = get_arith_ZZM(o);
827 15883 : N = gel(F,1); pr = gel(F,2); lpr = lgcols(pr);
828 15883 : av = avma;
829 :
830 51562 : for (i = 1; i < lpr; i++)
831 : {
832 35679 : GEN l = gcoeff(pr,i,1);
833 35679 : long e = itos(gcoeff(pr,i,2));
834 35679 : GEN r = diviiexact(N,powis(l,e));
835 35679 : GEN zetan, zl = gen_lgener(l,e,r,&zetan,E,grp);
836 35679 : z = i==1 ? zl: grp->mul(E,z,zl);
837 35679 : if (gc_needed(av,2))
838 : { /* n can have lots of prime factors*/
839 0 : if(DEBUGMEM>1) pari_warn(warnmem,"gen_gener");
840 0 : z = gerepileupto(av, z);
841 : }
842 : }
843 15883 : return gerepileupto(ltop, z);
844 : }
845 :
846 : /* solve x^l = a , l prime in G of order q.
847 : *
848 : * q = (l^e)*r, e >= 1, (r,l) = 1
849 : * y is not an l-th power, hence generates the l-Sylow of G
850 : * m = y^(q/l) != 1 */
851 : static GEN
852 263632 : gen_Shanks_sqrtl(GEN a, GEN l, long e, GEN r, GEN y, GEN m,void *E,
853 : const struct bb_group *grp)
854 : {
855 263632 : pari_sp av = avma;
856 : long k;
857 : GEN p1, u1, u2, v, w, z, dl;
858 :
859 263632 : (void)bezout(r,l,&u1,&u2);
860 263633 : v = grp->pow(E,a,u2);
861 263632 : w = grp->pow(E,v,l);
862 263631 : w = grp->mul(E,w,grp->pow(E,a,gen_m1));
863 465982 : while (!grp->equal1(w))
864 : {
865 202518 : k = 0;
866 202518 : p1 = w;
867 : do
868 : {
869 361474 : z = p1; p1 = grp->pow(E,p1,l);
870 361473 : k++;
871 361473 : } while(!grp->equal1(p1));
872 202517 : if (k==e) return gc_NULL(av);
873 202349 : dl = gen_plog(z,m,l,E,grp);
874 202349 : if (typ(dl) != t_INT) return gc_NULL(av);
875 202349 : dl = negi(dl);
876 202349 : p1 = grp->pow(E, grp->pow(E,y, dl), powiu(l,e-k-1));
877 202350 : m = grp->pow(E,m,dl);
878 202349 : e = k;
879 202349 : v = grp->mul(E,p1,v);
880 202350 : y = grp->pow(E,p1,l);
881 202351 : w = grp->mul(E,y,w);
882 202351 : if (gc_needed(av,1))
883 : {
884 0 : if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtl");
885 0 : gerepileall(av,4, &y,&v,&w,&m);
886 : }
887 : }
888 263462 : return gerepilecopy(av, v);
889 : }
890 : /* Return one solution of x^n = a in a cyclic group of order q
891 : *
892 : * 1) If there is no solution, return NULL.
893 : *
894 : * 2) If there is a solution, there are exactly m of them [m = gcd(q-1,n)].
895 : * If zetan!=NULL, *zetan is set to a primitive m-th root of unity so that
896 : * the set of solutions is { x*zetan^k; k=0..m-1 }
897 : */
898 : GEN
899 253763 : gen_Shanks_sqrtn(GEN a, GEN n, GEN q, GEN *zetan, void *E, const struct bb_group *grp)
900 : {
901 253763 : pari_sp ltop = avma;
902 : GEN m, u1, u2, z;
903 : int is_1;
904 :
905 253763 : if (is_pm1(n))
906 : {
907 0 : if (zetan) *zetan = grp->pow(E,a,gen_0);
908 0 : return signe(n) < 0? grp->pow(E,a,gen_m1): gcopy(a);
909 : }
910 253763 : is_1 = grp->equal1(a);
911 253763 : if (is_1 && !zetan) return gcopy(a);
912 :
913 245051 : m = bezout(n,q,&u1,&u2);
914 245053 : z = grp->pow(E,a,gen_0);
915 245050 : if (!is_pm1(m))
916 : {
917 244651 : GEN F = Z_factor(m);
918 : long i, j, e;
919 : GEN r, zeta, y, l;
920 244656 : pari_sp av1 = avma;
921 492328 : for (i = nbrows(F); i; i--)
922 : {
923 247842 : l = gcoeff(F,i,1);
924 247842 : j = itos(gcoeff(F,i,2));
925 247842 : e = Z_pvalrem(q,l,&r);
926 247841 : y = gen_lgener(l,e,r,&zeta,E,grp);
927 247840 : if (zetan) z = grp->mul(E,z, grp->pow(E,y,powiu(l,e-j)));
928 247840 : if (!is_1) {
929 : do
930 : {
931 263632 : a = gen_Shanks_sqrtl(a,l,e,r,y,zeta,E,grp);
932 263632 : if (!a) return gc_NULL(ltop);
933 263464 : } while (--j);
934 : }
935 247672 : if (gc_needed(ltop,1))
936 : { /* n can have lots of prime factors*/
937 0 : if(DEBUGMEM>1) pari_warn(warnmem,"gen_Shanks_sqrtn");
938 0 : gerepileall(av1, zetan? 2: 1, &a, &z);
939 : }
940 : }
941 : }
942 244885 : if (!equalii(m, n))
943 462 : a = grp->pow(E,a,modii(u1,q));
944 244885 : if (zetan)
945 : {
946 252 : *zetan = z;
947 252 : gerepileall(ltop,2,&a,zetan);
948 : }
949 : else /* is_1 is 0: a was modified above -> gerepileupto valid */
950 244633 : a = gerepileupto(ltop, a);
951 244886 : return a;
952 : }
953 :
954 : /*******************************************************************/
955 : /* */
956 : /* structure of groups with pairing */
957 : /* */
958 : /*******************************************************************/
959 :
960 : static GEN
961 146671 : ellgroup_d2(GEN N, GEN d)
962 : {
963 146671 : GEN r = gcdii(N, d);
964 146671 : GEN F1 = gel(Z_factor(r), 1);
965 146671 : long i, j, l1 = lg(F1);
966 146671 : GEN F = cgetg(3, t_MAT);
967 146671 : gel(F,1) = cgetg(l1, t_COL);
968 146671 : gel(F,2) = cgetg(l1, t_COL);
969 271747 : for (i = 1, j = 1; i < l1; ++i)
970 : {
971 125076 : long v = Z_pval(N, gel(F1, i));
972 125076 : if (v<=1) continue;
973 68558 : gcoeff(F, j , 1) = gel(F1, i);
974 68558 : gcoeff(F, j++, 2) = stoi(v);
975 : }
976 146671 : setlg(F[1],j); setlg(F[2],j);
977 146671 : return j==1 ? NULL : mkvec2(factorback(F), F);
978 : }
979 :
980 : GEN
981 146832 : gen_ellgroup(GEN N, GEN d, GEN *pt_m, void *E, const struct bb_group *grp,
982 : GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
983 : {
984 146832 : pari_sp av = avma;
985 : GEN N0, N1, F;
986 146832 : if (pt_m) *pt_m = gen_1;
987 146832 : if (is_pm1(N)) return cgetg(1,t_VEC);
988 146671 : F = ellgroup_d2(N, d);
989 146671 : if (!F) {set_avma(av); return mkveccopy(N);}
990 65100 : N0 = gel(F,1); N1 = diviiexact(N, N0);
991 : while(1)
992 53909 : {
993 119009 : pari_sp av2 = avma;
994 : GEN P, Q, d, s, t, m;
995 :
996 119009 : P = grp->pow(E,grp->rand(E), N1);
997 119009 : s = gen_order(P, F, E, grp); if (equalii(s, N0)) {set_avma(av); return mkveccopy(N);}
998 :
999 95702 : Q = grp->pow(E,grp->rand(E), N1);
1000 95702 : t = gen_order(Q, F, E, grp); if (equalii(t, N0)) {set_avma(av); return mkveccopy(N);}
1001 :
1002 84373 : m = lcmii(s, t);
1003 84373 : d = pairorder(E, P, Q, m, F);
1004 : /* structure is [N/d, d] iff m d == N0. Note that N/d = N1 m */
1005 84373 : if (is_pm1(d) && equalii(m, N0)) {set_avma(av); return mkveccopy(N);}
1006 84205 : if (equalii(mulii(m, d), N0))
1007 : {
1008 30296 : GEN g = mkvec2(mulii(N1,m), d);
1009 30296 : if (!pt_m) return gerepilecopy(av, g);
1010 30296 : *pt_m = m; return gc_all(av, 2, &g, pt_m);
1011 : }
1012 53909 : set_avma(av2);
1013 : }
1014 : }
1015 :
1016 : GEN
1017 2716 : gen_ellgens(GEN D1, GEN d2, GEN m, void *E, const struct bb_group *grp,
1018 : GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F))
1019 : {
1020 2716 : pari_sp ltop = avma, av;
1021 : GEN F, d1, dm;
1022 : GEN P, Q, d, s;
1023 2716 : F = get_arith_ZZM(D1);
1024 2716 : d1 = gel(F, 1), dm = diviiexact(d1,m);
1025 2716 : av = avma;
1026 : do
1027 : {
1028 6421 : set_avma(av);
1029 6421 : P = grp->rand(E);
1030 6421 : s = gen_order(P, F, E, grp);
1031 6421 : } while (!equalii(s, d1));
1032 2716 : av = avma;
1033 : do
1034 : {
1035 5030 : set_avma(av);
1036 5030 : Q = grp->rand(E);
1037 5030 : d = pairorder(E, grp->pow(E, P, dm), grp->pow(E, Q, dm), m, F);
1038 5030 : } while (!equalii(d, d2));
1039 2716 : return gerepilecopy(ltop, mkvec2(P,Q));
1040 : }
|