Line data Source code
1 : /* Copyright (C) 2014 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 : /** HYPERELLIPTIC CURVES **/
18 : /** **/
19 : /********************************************************************/
20 : #include "pari.h"
21 : #include "paripriv.h"
22 :
23 : #define DEBUGLEVEL DEBUGLEVEL_hyperell
24 :
25 : /* Implementation of Kedlaya Algorithm for counting point on hyperelliptic
26 : curves by Bill Allombert based on a GP script by Bernadette Perrin-Riou.
27 :
28 : References:
29 : Pierrick Gaudry and Nicolas G\"urel
30 : Counting Points in Medium Characteristic Using Kedlaya's Algorithm
31 : Experiment. Math. Volume 12, Number 4 (2003), 395-402.
32 : http://projecteuclid.org/euclid.em/1087568016
33 :
34 : Harrison, M. An extension of Kedlaya's algorithm for hyperelliptic
35 : curves. Journal of Symbolic Computation, 47 (1) (2012), 89-101.
36 : http://arxiv.org/pdf/1006.4206v3.pdf
37 : */
38 :
39 : /* We use the basis of differentials (x^i*dx/y^k) (i=1 to 2*g-1),
40 : with k either 1 or 3, depending on p and d, see Harrison paper */
41 :
42 : static long
43 1764 : get_basis(long p, long d)
44 : {
45 1764 : if (odd(d))
46 868 : return p < d-1 ? 3 : 1;
47 : else
48 896 : return 2*p <= d-2 ? 3 : 1;
49 : }
50 :
51 : static GEN
52 20265 : FpXXQ_red(GEN S, GEN T, GEN p)
53 : {
54 20265 : pari_sp av = avma;
55 20265 : long i, dS = degpol(S);
56 : GEN A, C;
57 20265 : if (signe(S)==0) return pol_0(varn(T));
58 20265 : A = cgetg(dS+3, t_POL);
59 20265 : C = pol_0(varn(T));
60 1520393 : for(i=dS; i>0; i--)
61 : {
62 1500128 : GEN Si = FpX_add(C, gel(S,i+2), p);
63 1500128 : GEN R, Q = FpX_divrem(Si, T, p, &R);
64 1500128 : gel(A,i+2) = R;
65 1500128 : C = Q;
66 : }
67 20265 : gel(A,2) = FpX_add(C, gel(S,2), p);
68 20265 : A[1] = S[1];
69 20265 : return gc_GEN(av, FpXX_renormalize(A,dS+3));
70 : }
71 :
72 : static GEN
73 3402 : FpXXQ_sqr(GEN x, GEN T, GEN p)
74 : {
75 3402 : pari_sp av = avma;
76 3402 : long n = degpol(T);
77 3402 : GEN z = FpX_red(ZXX_sqr_Kronecker(x, n), p);
78 3402 : z = Kronecker_to_ZXX(z, n, varn(T));
79 3402 : return gc_upto(av, FpXXQ_red(z, T, p));
80 : }
81 :
82 : static GEN
83 16863 : FpXXQ_mul(GEN x, GEN y, GEN T, GEN p)
84 : {
85 16863 : pari_sp av = avma;
86 16863 : long n = degpol(T);
87 16863 : GEN z = FpX_red(ZXX_mul_Kronecker(x, y, n), p);
88 16863 : z = Kronecker_to_ZXX(z, n, varn(T));
89 16863 : return gc_upto(av, FpXXQ_red(z, T, p));
90 : }
91 :
92 : static GEN
93 1309 : ZpXXQ_invsqrt(GEN S, GEN T, ulong p, long e)
94 : {
95 1309 : pari_sp av = avma, av2;
96 : ulong mask;
97 1309 : long v = varn(S), n=1;
98 1309 : GEN a = pol_1(v);
99 1309 : if (e <= 1) return gc_GEN(av, a);
100 1309 : mask = quadratic_prec_mask(e);
101 1309 : av2 = avma;
102 4676 : for (;mask>1;)
103 : {
104 : GEN q, q2, q22, f, fq, afq;
105 3367 : long n2 = n;
106 3367 : n<<=1; if (mask & 1) n--;
107 3367 : mask >>= 1;
108 3367 : q = powuu(p,n); q2 = powuu(p,n2);
109 3367 : f = RgX_sub(FpXXQ_mul(FpXX_red(S, q), FpXXQ_sqr(a, T, q), T, q), pol_1(v));
110 3367 : fq = ZXX_Z_divexact(f, q2);
111 3367 : q22 = shifti(addiu(q2,1),-1);
112 3367 : afq = FpXX_Fp_mul(FpXXQ_mul(a, fq, T, q2), q22, q2);
113 3367 : a = RgX_sub(a, ZXX_Z_mul(afq, q2));
114 3367 : if (gc_needed(av2,1))
115 : {
116 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_invsqrt, e = %ld", n);
117 0 : a = gc_upto(av2, a);
118 : }
119 : }
120 1309 : return gc_upto(av, a);
121 : }
122 :
123 : static GEN
124 1029007 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
125 :
126 : static void
127 14 : is_sing(GEN H, ulong p)
128 : {
129 14 : pari_err_DOMAIN("hyperellpadicfrobenius","H","is singular at",utoi(p),H);
130 0 : }
131 :
132 : static void
133 1309 : get_UV(GEN *U, GEN *V, GEN T, ulong p, long e)
134 : {
135 1309 : GEN q = powuu(p,e), d;
136 1309 : GEN dT = FpX_deriv(T, q);
137 1309 : GEN R = polresultantext(T, dT);
138 1309 : long v = varn(T);
139 1309 : if (dvdiu(gel(R,3),p)) is_sing(T, p);
140 1309 : d = Zp_inv(gel(R,3), utoi(p), e);
141 1309 : *U = FpX_Fp_mul(FpX_red(to_ZX(gel(R,1),v),q),d,q);
142 1309 : *V = FpX_Fp_mul(FpX_red(to_ZX(gel(R,2),v),q),d,q);
143 1309 : }
144 :
145 : static GEN
146 133847 : frac_to_Fp(GEN a, GEN b, GEN p)
147 : {
148 133847 : GEN d = gcdii(a, b);
149 133847 : return Fp_div(diviiexact(a, d), diviiexact(b, d), p);
150 : }
151 :
152 : static GEN
153 10094 : ZpXXQ_frob(GEN S, GEN U, GEN V, long k, GEN T, ulong p, long e)
154 : {
155 10094 : pari_sp av = avma, av2;
156 10094 : long i, pr = degpol(S), dT = degpol(T), vT = varn(T);
157 10094 : GEN q = powuu(p,e);
158 10094 : GEN Tp = FpX_deriv(T, q), Tp1 = RgX_shift_shallow(Tp, 1);
159 10094 : GEN M = to_ZX(gel(S,pr+2),vT) , R;
160 10094 : av2 = avma;
161 987868 : for(i = pr-1; i>=k; i--)
162 : {
163 : GEN A, B, H, Bc;
164 : ulong v, r;
165 977774 : H = FpX_divrem(FpX_mul(V,M,q), T, q, &B);
166 977774 : A = FpX_add(FpX_mul(U,M,q), FpX_mul(H, Tp, q),q);
167 977774 : v = u_lvalrem(2*i+1,p,&r);
168 977774 : Bc = ZX_deriv(B);
169 977774 : Bc = FpX_Fp_mul(ZX_divuexact(Bc,upowuu(p,v)),Fp_divu(gen_2, r, q), q);
170 977774 : M = FpX_add(to_ZX(gel(S,i+2),vT), FpX_add(A, Bc, q), q);
171 977774 : if (gc_needed(av2,1))
172 : {
173 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 1, i = %ld", i);
174 0 : M = gc_upto(av2, M);
175 : }
176 : }
177 10094 : if (degpol(M)<dT-1)
178 5488 : return gc_upto(av, M);
179 4606 : R = RgX_shift_shallow(M,dT-degpol(M)-2);
180 4606 : av2 = avma;
181 237629 : for(i = degpol(M)-dT+2; i>=1; i--)
182 : {
183 : GEN B, c;
184 233023 : R = RgX_shift_shallow(R, 1);
185 233023 : gel(R,2) = gel(M, i+1);
186 233023 : if (degpol(R) < dT) continue;
187 130935 : B = FpX_add(FpX_mulu(T, 2*i, q), Tp1, q);
188 130935 : c = frac_to_Fp(leading_coeff(R), leading_coeff(B), q);
189 130935 : R = FpX_sub(R, FpX_Fp_mul(B, c, q), q);
190 130935 : if (gc_needed(av2,1))
191 : {
192 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 2, i = %ld", i);
193 0 : R = gc_upto(av2, R);
194 : }
195 : }
196 4606 : if (degpol(R)==dT-1)
197 : {
198 2912 : GEN c = frac_to_Fp(leading_coeff(R), leading_coeff(Tp), q);
199 2912 : R = FpX_sub(R, FpX_Fp_mul(Tp, c, q), q);
200 2912 : return gc_upto(av, R);
201 : } else
202 1694 : return gc_GEN(av, R);
203 : }
204 :
205 : static GEN
206 12026 : revdigits(GEN v)
207 : {
208 12026 : long i, n = lg(v)-1;
209 12026 : GEN w = cgetg(n+2, t_POL);
210 12026 : w[1] = evalsigne(1)|evalvarn(0);
211 168784 : for (i=0; i<n; i++)
212 156758 : gel(w,i+2) = gel(v,n-i);
213 12026 : return FpXX_renormalize(w, n+2);
214 : }
215 :
216 : static GEN
217 10094 : diff_red(GEN s, GEN A, long m, GEN T, GEN p)
218 : {
219 10094 : long v, n, vT = varn(T);
220 : GEN Q, sQ, qS;
221 : pari_timer ti;
222 10094 : if (DEBUGLEVEL>1) timer_start(&ti);
223 10094 : Q = revdigits(FpX_digits(A,T,p));
224 10094 : n = degpol(Q);
225 10094 : if (DEBUGLEVEL>1) timer_printf(&ti,"reddigits");
226 10094 : sQ = FpXXQ_mul(s,Q,T,p);
227 10094 : if (DEBUGLEVEL>1) timer_printf(&ti,"redmul");
228 10094 : qS = RgX_shift_shallow(sQ,m-n);
229 10094 : v = ZX_val(sQ);
230 10094 : if (n > m + v)
231 : {
232 4564 : long i, l = n-m-v;
233 4564 : GEN rS = cgetg(l+1,t_VEC);
234 29190 : for (i = l-1; i >=0 ; i--)
235 24626 : gel(rS,i+1) = to_ZX(gel(sQ, 1+v+l-i), vT);
236 4564 : rS = FpXV_FpX_fromdigits(rS,T,p);
237 4564 : gel(qS,2) = FpX_add(FpX_mul(rS, T, p), gel(qS, 2), p);
238 4564 : if (DEBUGLEVEL>1) timer_printf(&ti,"redadd");
239 : }
240 10094 : return qS;
241 : }
242 :
243 : static GEN
244 10094 : ZC_to_padic(GEN C, GEN q)
245 : {
246 10094 : long i, l = lg(C);
247 10094 : GEN V = cgetg(l,t_COL);
248 102914 : for(i = 1; i < l; i++)
249 92820 : gel(V, i) = gadd(gel(C, i), q);
250 10094 : return V;
251 : }
252 :
253 : static GEN
254 1309 : ZM_to_padic(GEN M, GEN q)
255 : {
256 1309 : long i, l = lg(M);
257 1309 : GEN V = cgetg(l,t_MAT);
258 11403 : for(i = 1; i < l; i++)
259 10094 : gel(V, i) = ZC_to_padic(gel(M, i), q);
260 1309 : return V;
261 : }
262 :
263 : static GEN
264 1743 : ZX_to_padic(GEN P, GEN q)
265 : {
266 1743 : long i, l = lg(P);
267 1743 : GEN Q = cgetg(l, t_POL);
268 1743 : Q[1] = P[1];
269 5978 : for (i=2; i<l ;i++)
270 4235 : gel(Q,i) = gadd(gel(P,i), q);
271 1743 : return normalizepol(Q);
272 : }
273 :
274 : static GEN
275 469 : ZXC_to_padic(GEN x, GEN q)
276 2212 : { pari_APPLY_type(t_COL, ZX_to_padic(gel(x, i), q)) }
277 :
278 : static GEN
279 147 : ZXM_to_padic(GEN x, GEN q)
280 616 : { pari_APPLY_same(ZXC_to_padic(gel(x, i), q)) }
281 :
282 : static GEN
283 1309 : ZlX_hyperellpadicfrobenius(GEN H, ulong p, long n)
284 : {
285 1309 : pari_sp av = avma;
286 : long k, N, i, d;
287 : GEN F, s, Q, pN1, U, V;
288 : pari_timer ti;
289 1309 : if (typ(H) != t_POL) pari_err_TYPE("hyperellpadicfrobenius",H);
290 1309 : if (p == 2) is_sing(H, 2);
291 1309 : d = degpol(H);
292 1309 : if (d <= 0)
293 0 : pari_err_CONSTPOL("hyperellpadicfrobenius");
294 1309 : if (n < 1)
295 0 : pari_err_DOMAIN("hyperellpadicfrobenius","n","<", gen_1, utoi(n));
296 1309 : k = get_basis(p, d);
297 1309 : N = n + ulogint(2*n, p) + 1;
298 1309 : pN1 = powuu(p,N+1);
299 1309 : Q = RgX_to_FpX(H, pN1);
300 1309 : if (dvdiu(leading_coeff(Q),p)) is_sing(H, p);
301 1309 : setvarn(Q,1);
302 1309 : if (DEBUGLEVEL>1) timer_start(&ti);
303 1309 : s = revdigits(FpX_digits(RgX_inflate(Q, p), Q, pN1));
304 1309 : if (DEBUGLEVEL>1) timer_printf(&ti,"s1");
305 1309 : s = ZpXXQ_invsqrt(s, Q, p, N);
306 1309 : if (k==3)
307 35 : s = FpXXQ_mul(s, FpXXQ_sqr(s, Q, pN1), Q, pN1);
308 1309 : if (DEBUGLEVEL>1) timer_printf(&ti,"invsqrt");
309 1309 : get_UV(&U, &V, Q, p, N+1);
310 1309 : F = cgetg(d, t_MAT);
311 11403 : for (i = 1; i < d; i++)
312 : {
313 10094 : pari_sp av2 = avma;
314 : GEN M, D;
315 10094 : D = diff_red(s, monomial(utoipos(p),p*i-1,1),(k*p-1)>>1, Q, pN1);
316 10094 : if (DEBUGLEVEL>1) timer_printf(&ti,"red");
317 10094 : M = ZpXXQ_frob(D, U, V, (k-1)>>1, Q, p, N + 1);
318 10094 : if (DEBUGLEVEL>1) timer_printf(&ti,"frob");
319 10094 : gel(F, i) = gc_GEN(av2, RgX_to_RgC(M, d-1));
320 : }
321 1309 : return gc_upto(av, F);
322 : }
323 :
324 : GEN
325 1309 : hyperellpadicfrobenius(GEN H, ulong p, long n)
326 : {
327 1309 : pari_sp av = avma;
328 1309 : GEN M = ZlX_hyperellpadicfrobenius(H, p, n);
329 1309 : GEN q = zeropadic_shallow(utoipos(p),n);
330 1309 : return gc_upto(av, ZM_to_padic(M, q));
331 : }
332 :
333 : INLINE GEN
334 2247 : FpXXX_renormalize(GEN x, long lx) { return ZXX_renormalize(x,lx); }
335 :
336 : static GEN
337 1806 : ZpXQXXQ_red(GEN F, GEN S, GEN T, GEN q, GEN p, long e)
338 : {
339 1806 : pari_sp av = avma;
340 1806 : long i, dF = degpol(F);
341 : GEN A, C;
342 1806 : if (signe(F)==0) return pol_0(varn(S));
343 1806 : A = cgetg(dF+3, t_POL);
344 1806 : C = pol_0(varn(S));
345 96404 : for(i=dF; i>0; i--)
346 : {
347 94598 : GEN Fi = FpXX_add(C, gel(F,i+2), q);
348 94598 : GEN R, Q = ZpXQX_divrem(Fi, S, T, q, p, e, &R);
349 94598 : gel(A,i+2) = R;
350 94598 : C = Q;
351 : }
352 1806 : gel(A,2) = FpXX_add(C, gel(F,2), q);
353 1806 : A[1] = F[1];
354 1806 : return gc_GEN(av, FpXXX_renormalize(A,dF+3));
355 : }
356 :
357 : static GEN
358 448 : ZpXQXXQ_sqr(GEN x, GEN S, GEN T, GEN q, GEN p, long e)
359 : {
360 448 : pari_sp av = avma;
361 : GEN z, kx;
362 448 : long n = degpol(S);
363 448 : kx = RgXX_to_Kronecker(x, n);
364 448 : z = Kronecker_to_ZXX(FpXQX_sqr(kx, T, q), n, varn(S));
365 448 : return gc_upto(av, ZpXQXXQ_red(z, S, T, q, p, e));
366 : }
367 :
368 : static GEN
369 1358 : ZpXQXXQ_mul(GEN x, GEN y, GEN S, GEN T, GEN q, GEN p, long e)
370 : {
371 1358 : pari_sp av = avma;
372 : GEN z, kx, ky;
373 1358 : long n = degpol(S);
374 1358 : kx = RgXX_to_Kronecker(x, n);
375 1358 : ky = RgXX_to_Kronecker(y, n);
376 1358 : z = Kronecker_to_ZXX(FpXQX_mul(ky, kx, T, q), n, varn(S));
377 1358 : return gc_upto(av, ZpXQXXQ_red(z, S, T, q, p, e));
378 : }
379 :
380 : static GEN
381 441 : FpXXX_red(GEN z, GEN p)
382 : {
383 : GEN res;
384 441 : long i, l = lg(z);
385 441 : res = cgetg(l,t_POL); res[1] = z[1];
386 17388 : for (i=2; i<l; i++)
387 : {
388 16947 : GEN zi = gel(z,i);
389 16947 : if (typ(zi)==t_INT)
390 0 : gel(res,i) = modii(zi,p);
391 : else
392 16947 : gel(res,i) = FpXX_red(zi,p);
393 : }
394 441 : return FpXXX_renormalize(res,lg(res));
395 : }
396 :
397 : static GEN
398 441 : FpXXX_Fp_mul(GEN z, GEN a, GEN p)
399 : {
400 441 : return FpXXX_red(RgX_Rg_mul(z, a), p);
401 : }
402 :
403 : static GEN
404 154 : ZpXQXXQ_invsqrt(GEN F, GEN S, GEN T, ulong p, long e)
405 : {
406 154 : pari_sp av = avma, av2, av3;
407 : ulong mask;
408 154 : long v = varn(F), n=1;
409 : pari_timer ti;
410 154 : GEN a = pol_1(v), pp = utoipos(p);
411 154 : if (DEBUGLEVEL>1) timer_start(&ti);
412 154 : if (e <= 1) return gc_GEN(av, a);
413 154 : mask = quadratic_prec_mask(e);
414 154 : av2 = avma;
415 595 : for (;mask>1;)
416 : {
417 : GEN q, q2, q22, f, fq, afq;
418 441 : long n2 = n;
419 441 : n<<=1; if (mask & 1) n--;
420 441 : mask >>= 1;
421 441 : q = powuu(p,n); q2 = powuu(p,n2);
422 441 : av3 = avma;
423 441 : f = RgX_sub(ZpXQXXQ_mul(F, ZpXQXXQ_sqr(a, S, T, q, pp, n), S, T, q, pp, n), pol_1(v));
424 441 : fq = gc_upto(av3, RgX_Rg_divexact(f, q2));
425 441 : q22 = shifti(addiu(q2,1),-1);
426 441 : afq = FpXXX_Fp_mul(ZpXQXXQ_mul(a, fq, S, T, q2, pp, n2), q22, q2);
427 441 : a = RgX_sub(a, RgX_Rg_mul(afq, q2));
428 441 : if (gc_needed(av2,1))
429 : {
430 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQXXQ_invsqrt, e = %ld", n);
431 0 : a = gc_upto(av2, a);
432 : }
433 : }
434 154 : return gc_upto(av, a);
435 : }
436 :
437 : static GEN
438 6573 : frac_to_Fq(GEN a, GEN b, GEN T, GEN q, GEN p, long e)
439 : {
440 6573 : GEN d = gcdii(ZX_content(a), ZX_content(b));
441 6573 : return ZpXQ_div(ZX_Z_divexact(a, d), ZX_Z_divexact(b, d), T, q, p, e);
442 : }
443 :
444 : static GEN
445 469 : ZpXQXXQ_frob(GEN F, GEN U, GEN V, long k, GEN S, GEN T, ulong p, long e)
446 : {
447 469 : pari_sp av = avma, av2;
448 469 : long i, pr = degpol(F), dS = degpol(S), v = varn(T);
449 469 : GEN q = powuu(p,e), pp = utoipos(p);
450 469 : GEN Sp = RgX_deriv(S), Sp1 = RgX_shift_shallow(Sp, 1);
451 469 : GEN M = gel(F,pr+2), R;
452 469 : av2 = avma;
453 52311 : for(i = pr-1; i>=k; i--)
454 : {
455 : GEN A, B, H, Bc;
456 : ulong v, r;
457 51842 : H = ZpXQX_divrem(FpXQX_mul(V, M, T, q), S, T, q, utoipos(p), e, &B);
458 51842 : A = FpXX_add(FpXQX_mul(U, M, T, q), FpXQX_mul(H, Sp, T, q),q);
459 51842 : v = u_lvalrem(2*i+1,p,&r);
460 51842 : Bc = RgX_deriv(B);
461 51842 : Bc = FpXX_Fp_mul(ZXX_Z_divexact(Bc,powuu(p,v)), Fp_divu(gen_2, r, q), q);
462 51842 : M = FpXX_add(gel(F,i+2), FpXX_add(A, Bc, q), q);
463 51842 : if (gc_needed(av2,1))
464 : {
465 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQXXQ_frob, step 1, i = %ld", i);
466 0 : M = gc_upto(av2, M);
467 : }
468 : }
469 469 : if (degpol(M)<dS-1)
470 266 : return gc_upto(av, M);
471 203 : R = RgX_shift_shallow(M,dS-degpol(M)-2);
472 203 : av2 = avma;
473 7175 : for(i = degpol(M)-dS+2; i>=1; i--)
474 : {
475 : GEN B, c;
476 6972 : R = RgX_shift_shallow(R, 1);
477 6972 : gel(R,2) = gel(M, i+1);
478 6972 : if (degpol(R) < dS) continue;
479 6412 : B = FpXX_add(FpXX_mulu(S, 2*i, q), Sp1, q);
480 6412 : c = frac_to_Fq(to_ZX(leading_coeff(R),v), to_ZX(leading_coeff(B),v), T, q, pp, e);
481 6412 : R = FpXX_sub(R, FpXQX_FpXQ_mul(B, c, T, q), q);
482 6412 : if (gc_needed(av2,1))
483 : {
484 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZpXXQ_frob, step 2, i = %ld", i);
485 0 : R = gc_upto(av2, R);
486 : }
487 : }
488 203 : if (degpol(R)==dS-1)
489 : {
490 161 : GEN c = frac_to_Fq(to_ZX(leading_coeff(R),v), to_ZX(leading_coeff(Sp),v), T, q, pp, e);
491 161 : R = FpXX_sub(R, FpXQX_FpXQ_mul(Sp, c, T, q), q);
492 161 : return gc_upto(av, R);
493 : } else
494 42 : return gc_GEN(av, R);
495 : }
496 :
497 : static GEN
498 469 : Fq_diff_red(GEN s, GEN A, long m, GEN S, GEN T, GEN q, GEN p, long e)
499 : {
500 : long v, n;
501 : GEN Q, sQ, qS;
502 : pari_timer ti;
503 469 : if (DEBUGLEVEL>1) timer_start(&ti);
504 469 : Q = revdigits(ZpXQX_digits(A, S, T, q, p, e));
505 469 : n = degpol(Q);
506 469 : if (DEBUGLEVEL>1) timer_printf(&ti,"reddigits");
507 469 : sQ = ZpXQXXQ_mul(s, Q, S, T, q, p, e);
508 469 : if (DEBUGLEVEL>1) timer_printf(&ti,"redmul");
509 469 : qS = RgX_shift_shallow(sQ,m-n);
510 469 : v = ZX_val(sQ);
511 469 : if (n > m + v)
512 : {
513 189 : long i, l = n-m-v;
514 189 : GEN rS = cgetg(l+1,t_VEC);
515 1547 : for (i = l-1; i >=0 ; i--)
516 1358 : gel(rS,i+1) = gel(sQ, 1+v+l-i);
517 189 : rS = FpXQXV_FpXQX_fromdigits(rS, S, T, q);
518 189 : gel(qS,2) = FpXX_add(FpXQX_mul(rS, S, T, q), gel(qS, 2), q);
519 189 : if (DEBUGLEVEL>1) timer_printf(&ti,"redadd");
520 : }
521 469 : return qS;
522 : }
523 :
524 : static void
525 154 : Fq_get_UV(GEN *U, GEN *V, GEN S, GEN T, ulong p, long e)
526 : {
527 154 : GEN q = powuu(p, e), pp = utoipos(p), d;
528 154 : GEN dS = RgX_deriv(S), R = polresultantext(S, dS), C;
529 154 : long v = varn(S);
530 154 : if (signe(FpX_red(to_ZX(gel(R,3),v), pp))==0) is_sing(S, p);
531 147 : C = FpXQ_red(to_ZX(gel(R, 3),v), T, q);
532 147 : d = ZpXQ_inv(C, T, pp, e);
533 147 : *U = FpXQX_FpXQ_mul(FpXQX_red(to_ZX(gel(R,1),v),T,q),d,T,q);
534 147 : *V = FpXQX_FpXQ_mul(FpXQX_red(to_ZX(gel(R,2),v),T,q),d,T,q);
535 147 : }
536 :
537 : static GEN
538 469 : ZXX_to_FpXC(GEN x, long N, GEN p, long v)
539 : {
540 : long i, l;
541 : GEN z;
542 469 : l = lg(x)-1; x++;
543 469 : if (l > N+1) l = N+1; /* truncate higher degree terms */
544 469 : z = cgetg(N+1,t_COL);
545 2170 : for (i=1; i<l ; i++)
546 : {
547 1701 : GEN xi = gel(x, i);
548 1701 : gel(z,i) = typ(xi)==t_INT? scalarpol(Fp_red(xi, p), v): FpX_red(xi, p);
549 : }
550 511 : for ( ; i<=N ; i++)
551 42 : gel(z,i) = pol_0(v);
552 469 : return z;
553 : }
554 :
555 : GEN
556 154 : ZlXQX_hyperellpadicfrobenius(GEN H, GEN T, ulong p, long n)
557 : {
558 154 : pari_sp av = avma;
559 : long k, N, i, d, N1;
560 : GEN xp, F, s, q, Q, pN1, U, V, pp;
561 : pari_timer ti;
562 154 : if (typ(H) != t_POL) pari_err_TYPE("hyperellpadicfrobenius",H);
563 154 : if (p == 2) is_sing(H, 2);
564 154 : d = degpol(H);
565 154 : if (d <= 0) pari_err_CONSTPOL("hyperellpadicfrobenius");
566 154 : if (n < 1) pari_err_DOMAIN("hyperellpadicfrobenius","n","<", gen_1, utoi(n));
567 154 : k = get_basis(p, d); pp = utoipos(p);
568 154 : N = n + ulogint(2*n, p) + 1;
569 154 : q = powuu(p,n); N1 = N+1;
570 154 : pN1 = powuu(p,N1); T = FpX_get_red(T, pN1);
571 154 : Q = RgX_to_FqX(H, T, pN1);
572 154 : if (signe(FpX_red(to_ZX(leading_coeff(Q),varn(Q)),pp))==0) is_sing(H, p);
573 154 : if (DEBUGLEVEL>1) timer_start(&ti);
574 154 : xp = ZpX_Frobenius(T, pp, N1);
575 154 : s = RgX_inflate(FpXY_FpXQ_evalx(Q, xp, T, pN1), p);
576 154 : s = revdigits(ZpXQX_digits(s, Q, T, pN1, pp, N1));
577 154 : if (DEBUGLEVEL>1) timer_printf(&ti,"s1");
578 154 : s = ZpXQXXQ_invsqrt(s, Q, T, p, N);
579 154 : if (k==3)
580 7 : s = ZpXQXXQ_mul(s, ZpXQXXQ_sqr(s, Q, T, pN1, pp, N1), Q, T, pN1, pp, N1);
581 154 : if (DEBUGLEVEL>1) timer_printf(&ti,"invsqrt");
582 154 : Fq_get_UV(&U, &V, Q, T, p, N+1);
583 147 : if (DEBUGLEVEL>1) timer_printf(&ti,"get_UV");
584 147 : F = cgetg(d, t_MAT);
585 616 : for (i = 1; i < d; i++)
586 : {
587 469 : pari_sp av2 = avma;
588 : GEN M, D;
589 469 : D = Fq_diff_red(s, monomial(pp,p*i-1,1),(k*p-1)>>1, Q, T, pN1, pp, N1);
590 469 : if (DEBUGLEVEL>1) timer_printf(&ti,"red");
591 469 : M = ZpXQXXQ_frob(D, U, V, (k - 1)>>1, Q, T, p, N1);
592 469 : if (DEBUGLEVEL>1) timer_printf(&ti,"frob");
593 469 : gel(F, i) = gc_upto(av2, ZXX_to_FpXC(M, d-1, q, varn(T)));
594 : }
595 147 : return gc_upto(av, F);
596 : }
597 :
598 : GEN
599 154 : nfhyperellpadicfrobenius(GEN H, GEN T, ulong p, long n)
600 : {
601 154 : pari_sp av = avma;
602 154 : GEN pp = utoipos(p), q = zeropadic_shallow(pp, n);
603 154 : GEN M = ZlXQX_hyperellpadicfrobenius(lift_shallow(H),T,p,n);
604 147 : GEN MM = ZpXQM_prodFrobenius(M, T, pp, n);
605 147 : GEN m = gmul(ZXM_to_padic(MM, q), gmodulo(gen_1, T));
606 147 : return gc_upto(av, m);
607 : }
608 :
609 : GEN
610 595 : hyperellpadicfrobenius0(GEN H, GEN Tp, long n)
611 : {
612 : GEN T, p;
613 595 : if (!ff_parse_Tp(Tp, &T,&p,0)) pari_err_TYPE("hyperellpadicfrobenius", Tp);
614 595 : if (lgefint(p) > 3) pari_err_IMPL("large prime in hyperellpadicfrobenius");
615 7 : return T? nfhyperellpadicfrobenius(H, T, itou(p), n)
616 602 : : hyperellpadicfrobenius(H, itou(p), n);
617 : }
618 :
619 : static GEN
620 84 : F2x_genus2charpoly_naive(GEN P, GEN Q)
621 : {
622 84 : long a, b = 1, c = 0;
623 84 : GEN T = mkvecsmall2(P[1], 7);
624 84 : GEN PT = F2x_rem(P, T), QT = F2x_rem(Q, T);
625 84 : long q0 = F2x_eval(Q, 0), q1 = F2x_eval(Q, 1);
626 84 : long dP = F2x_degree(P), dQ = F2x_degree(Q);
627 84 : a= dQ<3 ? 0: dP<=5 ? 1: -1;
628 84 : a += (q0? F2x_eval(P, 0)? -1: 1: 0) + (q1? F2x_eval(P, 1)? -1: 1: 0);
629 84 : b += q0 + q1;
630 84 : if (lgpol(QT))
631 70 : c = (F2xq_trace(F2xq_div(PT, F2xq_sqr(QT, T), T), T)==0 ? 1: -1);
632 84 : return mkvecsmalln(6, 0UL, 4UL, 2*a, (b+2*c+a*a)>>1, a, 1UL);
633 : }
634 :
635 : static GEN
636 259 : Flx_difftable(GEN P, ulong p)
637 : {
638 259 : long i, n = degpol(P);
639 259 : GEN V = cgetg(n+2, t_VEC);
640 259 : gel(V, n+1) = P;
641 1771 : for(i = n; i >= 1; i--)
642 1512 : gel(V, i) = Flx_diff1(gel(V, i+1), p);
643 259 : return V;
644 : }
645 :
646 : static GEN
647 1582 : FlxV_Fl2_eval_pre(GEN V, GEN x, ulong D, ulong p, ulong pi)
648 : {
649 1582 : long i, n = lg(V)-1;
650 1582 : GEN r = cgetg(n+1, t_VEC);
651 12110 : for (i = 1; i <= n; i++)
652 10528 : gel(r, i) = Flx_Fl2_eval_pre(gel(V, i), x, D, p, pi);
653 1582 : return r;
654 : }
655 :
656 : static GEN
657 44716 : Fl2V_next(GEN V, ulong p)
658 : {
659 44716 : long i, n = lg(V)-1;
660 44716 : GEN r = cgetg(n+1, t_VEC);
661 44716 : gel(r, 1) = gel(V, 1);
662 287056 : for (i = 2; i <= n; i++)
663 242340 : gel(r, i) = Flv_add(gel(V, i), gel(V, i-1), p);
664 44716 : return r;
665 : }
666 :
667 : static GEN
668 259 : FlxV_constant(GEN x)
669 2030 : { pari_APPLY_long(Flx_constant(gel(x,i))) }
670 :
671 : static GEN
672 259 : Flx_genus2charpoly_naive(GEN H, ulong p)
673 : {
674 259 : pari_sp av = avma, av2;
675 259 : ulong pi = get_Fl_red(p);
676 259 : ulong i, j, p2 = p>>1, D = 2, e = ((p&2UL) == 0) ? -1 : 1;
677 259 : long a, b, c = 0, n = degpol(H);
678 259 : GEN t, d, k = const_vecsmall(p, -1);
679 259 : k[1] = 0;
680 1841 : for (i=1, j=1; i < p; i += 2, j = Fl_add(j, i, p)) k[j+1] = 1;
681 308 : while (k[1+D] >= 0) D++;
682 259 : b = n == 5 ? 0 : 1;
683 259 : a = b ? k[1+Flx_lead(H)]: 0;
684 259 : t = Flx_difftable(H, p);
685 259 : d = FlxV_constant(t);
686 259 : av2 = avma;
687 3682 : for (i=0; i < p; i++)
688 : {
689 3423 : ulong v = uel(d,n+1);
690 3423 : a += k[1+v];
691 3423 : b += !!v;
692 3423 : if (n==6)
693 2289 : uel(d,7) = Fl_add(uel(d,7), uel(d,6), p);
694 3423 : uel(d,6) = Fl_add(uel(d,6), uel(d,5), p);
695 3423 : uel(d,5) = Fl_add(uel(d,5), uel(d,4), p);
696 3423 : uel(d,4) = Fl_add(uel(d,4), uel(d,3), p);
697 3423 : uel(d,3) = Fl_add(uel(d,3), uel(d,2), p);
698 3423 : uel(d,2) = Fl_add(uel(d,2), uel(d,1), p);
699 : }
700 1841 : for (j=1; j <= p2; j++)
701 : {
702 1582 : GEN V = FlxV_Fl2_eval_pre(t, mkvecsmall2(0, j), D, p, pi);
703 1582 : for (i=0;; i++)
704 44716 : {
705 46298 : GEN r2 = gel(V, n+1);
706 92596 : c += uel(r2,2) ?
707 44009 : (uel(r2,1) ? uel(k,1+Fl2_norm_pre(r2, D, p, pi)): e)
708 90307 : : !!uel(r2,1);
709 46298 : if (i == p-1) break;
710 44716 : V = Fl2V_next(V, p);
711 : }
712 1582 : set_avma(av2);
713 : }
714 259 : set_avma(av);
715 259 : return mkvecsmalln(6, 0UL, p*p, a*p, (b+2*c+a*a)>>1, a, 1UL);
716 : }
717 :
718 : static GEN
719 679 : charpoly_funceq(GEN P, GEN q)
720 : {
721 679 : long i, l, g = degpol(P)>>1;
722 679 : GEN R, Q = gpowers0(q, g-1, q); /* Q[i] = q^i, i <= g */
723 679 : R = cgetg_copy(P, &l); R[1] = P[1];
724 3164 : for (i=0; i<g; i++) gel(R, i+2) = mulii(gel(P, 2*g-i+2), gel(Q, g-i));
725 3843 : for (; i<=2*g; i++) gel(R, i+2) = icopy(gel(P, i+2));
726 679 : return R;
727 : }
728 :
729 : static long
730 686 : hyperell_Weil_bound(GEN q, ulong g, GEN p)
731 : {
732 686 : pari_sp av = avma;
733 686 : GEN w = mulii(binomialuu(2*g,g),sqrtint(shifti(powiu(q, g),2)));
734 686 : return gc_long(av, logint(w,p) + 1);
735 : }
736 :
737 : /* return 4P + Q^2 */
738 : static GEN
739 289139 : check_hyperell(GEN PQ)
740 : {
741 : GEN H;
742 289139 : if (is_vec_t(typ(PQ)) && lg(PQ)==3)
743 225651 : H = gadd(gsqr(gel(PQ, 2)), gmul2n(gel(PQ, 1), 2));
744 : else
745 63488 : H = gmul2n(PQ, 2);
746 289139 : return typ(H) == t_POL? H: NULL;
747 : }
748 :
749 : GEN
750 1036 : hyperellcharpoly(GEN PQ)
751 : {
752 1036 : pari_sp av = avma;
753 1036 : GEN M, R, T=NULL, pp=NULL, q;
754 1036 : long d, n, eps = 0;
755 : ulong p;
756 1036 : GEN H = check_hyperell(PQ);
757 1036 : if (!H || !RgX_is_FpXQX(H, &T, &pp) || !pp)
758 0 : pari_err_TYPE("hyperellcharpoly", PQ);
759 1036 : p = itou(pp);
760 1036 : if (!T)
761 : {
762 889 : if (p==2 && is_vec_t(typ(PQ)))
763 : {
764 84 : long dP, dQ, v = varn(H);
765 84 : GEN P = gel(PQ,1), Q = gel(PQ,2);
766 84 : if (typ(P)!=t_POL) P = scalarpol(P, v);
767 84 : if (typ(Q)!=t_POL) Q = scalarpol(Q, v);
768 84 : dP = degpol(P); dQ = degpol(Q);
769 84 : if (dP<=6 && dQ <=3 && (dQ==3 || dP>=5))
770 : {
771 84 : GEN P2 = RgX_to_F2x(P), Q2 = RgX_to_F2x(Q);
772 84 : GEN D = F2x_add(F2x_mul(P2, F2x_sqr(F2x_deriv(Q2))), F2x_sqr(F2x_deriv(P2)));
773 84 : if (F2x_degree(F2x_gcd(D, Q2))) is_sing(PQ, 2);
774 84 : if (dP==6 && dQ<3 && F2x_coeff(P2,5)==F2x_coeff(Q2,2))
775 0 : is_sing(PQ, 2); /* The curve is singular at infinity */
776 84 : R = zx_to_ZX(F2x_genus2charpoly_naive(P2, Q2));
777 84 : return gc_upto(av, R);
778 : }
779 : }
780 805 : H = RgX_to_FpX(H, pp);
781 805 : d = degpol(H);
782 805 : if (d <= 0) is_sing(H, p);
783 805 : if (p > 2 && ((d == 5 && p < 17500) || (d == 6 && p < 24500)))
784 : {
785 266 : GEN Hp = ZX_to_Flx(H, p);
786 266 : if (!Flx_is_squarefree(Hp, p)) is_sing(H, p);
787 259 : R = zx_to_ZX(Flx_genus2charpoly_naive(Hp, p));
788 259 : return gc_upto(av, R);
789 : }
790 539 : n = hyperell_Weil_bound(pp, (d-1)>>1, pp);
791 539 : eps = odd(d)? 0: Fp_issquare(leading_coeff(H), pp);
792 539 : M = hyperellpadicfrobenius(H, p, n);
793 539 : R = centerlift(carberkowitz(M, 0));
794 539 : q = pp;
795 : }
796 : else
797 : {
798 : int fixvar;
799 147 : T = typ(T)==t_FFELT? FF_mod(T): RgX_to_FpX(T, pp);
800 147 : q = powuu(p, degpol(T));
801 147 : fixvar = (varncmp(varn(T),varn(H)) <= 0);
802 147 : if (fixvar) setvarn(T, fetch_var());
803 147 : H = RgX_to_FpXQX(H, T, pp);
804 147 : d = degpol(H);
805 147 : if (d <= 0) is_sing(H, p);
806 147 : eps = odd(d)? 0: Fq_issquare(leading_coeff(H), T, pp);
807 147 : n = hyperell_Weil_bound(q, (d-1)>>1, pp);
808 147 : M = nfhyperellpadicfrobenius(H, T, p, n);
809 140 : R = simplify_shallow(centerlift(liftpol_shallow(carberkowitz(M, 0))));
810 140 : if (fixvar) (void)delete_var();
811 : }
812 679 : if (!odd(d))
813 : {
814 301 : GEN b = get_basis(p, d) == 3 ? gen_1 : q;
815 301 : GEN pn = powuu(p, n);
816 301 : R = FpX_div_by_X_x(R, eps? b: negi(b), pn, NULL);
817 301 : R = FpX_center_i(R, pn, shifti(pn,-1));
818 : }
819 679 : return gc_upto(av, charpoly_funceq(R, q));
820 : }
821 :
822 : int
823 3493 : hyperellisoncurve(GEN W, GEN P)
824 : {
825 3493 : pari_sp av = avma;
826 : long res;
827 : GEN x, y;
828 3493 : if (typ(P)!=t_VEC || lg(P)!=3) pari_err_TYPE("hyperellisoncurve",P);
829 3493 : x = gel(P,1); y = gel(P,2);
830 3493 : if (typ(W)==t_POL)
831 0 : res = gequal(gsqr(y), poleval(W,x));
832 : else
833 : {
834 3493 : if (typ(W)!=t_VEC || lg(W)!=3) pari_err_TYPE("hyperellisoncurve",W);
835 3493 : res = gequal(gmul(y, gadd(y,poleval(gel(W,2), x))), poleval(gel(W,1), x));
836 : }
837 3493 : return gc_int(av, res);
838 : }
839 :
840 : GEN
841 35 : hyperellordinate(GEN W, GEN x)
842 : {
843 35 : pari_sp av = avma;
844 35 : if (typ(W)==t_POL)
845 : {
846 14 : GEN d = poleval(W,x), y;
847 14 : if (gequal0(d)) { return gc_GEN(av, mkvec(d)); }
848 14 : if (!issquareall(d, &y)) retgc_const(av, cgetg(1, t_VEC));
849 7 : return gc_GEN(av, mkvec2(y, gneg(y)));
850 : }
851 : else
852 : {
853 : GEN b, c, d, rd, y;
854 21 : if (typ(W)!=t_VEC || lg(W)!=3) pari_err_TYPE("hyperellisoncurve",W);
855 21 : b = poleval(gel(W,2), x); c = poleval(gel(W,1), x);
856 21 : d = gadd(gsqr(b), gmul2n(c, 2));
857 21 : if (gequal0(d)) { return gc_GEN(av, mkvec(gmul2n(gneg(b),-1))); }
858 14 : if (!issquareall(d, &rd)) retgc_const(av, cgetg(1, t_VEC));
859 7 : y = gmul2n(gsub(rd, b), -1);
860 7 : return gc_GEN(av, mkvec2(y, gsub(y,rd)));
861 : }
862 : }
863 :
864 : GEN
865 118950 : hyperelldisc(GEN PQ)
866 : {
867 118950 : pari_sp av = avma;
868 118950 : GEN D, H = check_hyperell(PQ);
869 : long d, g;
870 118950 : if (!H || signe(H)==0) pari_err_TYPE("hyperelldisc",PQ);
871 118950 : d = degpol(H); g = ((d+1)>>1)-1;
872 118950 : D = gmul2n(RgX_disc(H),-4*(g+1));
873 118950 : if (odd(d)) D = gmul(D, gsqr(leading_coeff(H)));
874 118950 : return gc_upto(av, D);
875 : }
876 :
877 : static long
878 126502 : get_ep(GEN W)
879 : {
880 126502 : GEN P = gel(W,1), Q = gel(W,2);
881 126502 : if (signe(Q)==0) return ZX_lval(P,2);
882 86307 : return minss(ZX_lval(P,2), ZX_lval(Q,2));
883 : }
884 :
885 : static GEN
886 50798 : algo51(GEN W, GEN M)
887 : {
888 50798 : GEN P = gel(W,1), Q = gel(W,2);
889 : for(;;)
890 10640 : {
891 61438 : long vP = ZX_lval(P,2);
892 61438 : long vQ = signe(Q) ? ZX_lval(Q,2): vP+1;
893 : long r;
894 : /* 1 */
895 61438 : if (vQ==0) break;
896 : /* 2 */
897 36120 : if (vP==0)
898 : {
899 : GEN H, H1;
900 : /* a */
901 29602 : RgX_even_odd(FpX_red(P,gen_2),&H, &H1);
902 29602 : if (signe(H1)) break;
903 : /* b */
904 14951 : P = ZX_add(P, ZX_mul(H, ZX_sub(Q, H)));
905 14951 : Q = ZX_sub(Q, ZX_shifti(H, 1));
906 14951 : vP = ZX_lval(P,2);
907 14951 : vQ = signe(Q) ? ZX_lval(Q,2): vP+1;
908 : }
909 : /* 2c */
910 21469 : if (vP==1) break;
911 : /* 2d */
912 10640 : r = minss(2*vQ, vP)>>1;
913 10640 : if (M) gel(M,1) = shifti(gel(M,1), r);
914 10640 : P = ZX_shifti(P, -2*r);
915 10640 : Q = ZX_shifti(Q, -r);
916 : }
917 50798 : return mkvec2(P,Q);
918 : }
919 :
920 : static GEN
921 103343 : algo52(GEN W, GEN c, long *pt_lambda)
922 : {
923 : long lambda;
924 103343 : GEN P = gel(W,1), Q = gel(W,2);
925 : for(;;)
926 116959 : {
927 : GEN H, H1;
928 : /* 1 */
929 220302 : GEN Pc = ZX_affine(P,gen_2,c), Qc = ZX_affine(Q,gen_2,c);
930 220302 : long mP = ZX_lval(Pc,2), mQ = signe(Qc) ? ZX_lval(Qc,2): mP+1;
931 : /* 2 */
932 220302 : if (2*mQ <= mP) { lambda = 2*mQ; break; }
933 : /* 3 */
934 188031 : if (odd(mP)) { lambda = mP; break; }
935 : /* 4 */
936 127718 : RgX_even_odd(FpX_red(ZX_shifti(Pc, -mP),gen_2),&H, &H1);
937 127718 : if (signe(H1)) { lambda = mP; break; }
938 : /* 5 */
939 116959 : P = ZX_add(P, ZX_mul(H, ZX_sub(Q, H)));
940 116959 : Q = ZX_sub(Q, ZX_shifti(H, 1));
941 : }
942 103343 : *pt_lambda = lambda;
943 103343 : return mkvec2(P,Q);
944 : }
945 :
946 : static long
947 147379 : test53(long lambda, long ep, long g)
948 : {
949 147379 : return (lambda <= g+1) || (odd(g) && lambda<g+3 && ep==1);
950 : }
951 :
952 : static long
953 189382 : test55(GEN W, long ep, long g)
954 : {
955 189382 : GEN P = gel(W,1), Q = gel(W,2);
956 189382 : GEN Pe = FpX_red(ep ? ZX_shifti(P,-1): P, gen_2);
957 189382 : GEN Qe = FpX_red(ep ? ZX_shifti(Q,-1): Q, gen_2);
958 189382 : if (ep==0)
959 : {
960 149101 : if (signe(Qe)!=0) return ZX_val(Qe) >= (g + 3)>>1;
961 90723 : else return ZX_val(FpX_deriv(Pe, gen_2)) >= g+1;
962 : }
963 : else
964 40281 : return ZX_val(Qe) >= (g+1)>>1 && ZX_val(Pe) >= g + 1;
965 : }
966 :
967 : static GEN
968 50798 : hyperell_reverse(GEN W, long g)
969 : {
970 50798 : return mkvec2(RgXn_recip_shallow(gel(W,1),2*g+3),
971 50798 : RgXn_recip_shallow(gel(W,2),g+2));
972 : }
973 :
974 : /* [P,Q] -> [P(2x)/4^r, Q(2x)/2^r] */
975 : static GEN
976 169459 : ZX2_unscale(GEN W, long r)
977 : {
978 169459 : GEN P = ZX_unscale2n(gel(W,1), 1);
979 169459 : GEN Q = ZX_unscale2n(gel(W,2), 1);
980 169459 : if (r)
981 : {
982 30861 : P = ZX_shifti(P, -2*r);
983 30861 : Q = ZX_shifti(Q, -r);
984 : }
985 169459 : return mkvec2(P,Q);
986 : }
987 : /* [P,Q] -> [P(2x+c)/4^r, Q(2x+c)/2^r] */
988 : static GEN
989 163476 : ZX2_affine_unscale(GEN W, long c, long r)
990 : {
991 238466 : if (c) W = mkvec2(ZX_Z_translate(gel(W,1), gen_1),
992 74990 : ZX_Z_translate(gel(W,2), gen_1));
993 163476 : return ZX2_unscale(W, r);
994 : }
995 :
996 : static GEN
997 50784 : algo56(GEN W, long g)
998 : {
999 : long ep;
1000 50784 : GEN M = mkvec2(gen_1, matid(2)), Woo;
1001 50784 : W = algo51(W, M);
1002 50784 : Woo = hyperell_reverse(W, g);
1003 50784 : ep = get_ep(Woo);
1004 50784 : if (test55(Woo,ep,g))
1005 : {
1006 : long lambda;
1007 11737 : Woo = algo52(Woo, gen_0, &lambda);
1008 11737 : if (!test53(lambda,ep,g))
1009 : {
1010 5969 : long r = lambda>>1;
1011 5969 : gel(M,1) = shifti(gel(M,1), r);
1012 5969 : gel(M,2) = ZM2_mul(gel(M,2), mkmat22(gen_0, gen_1, gen_2, gen_0));
1013 5969 : W = ZX2_unscale(Woo, r);
1014 : }
1015 : }
1016 : for(;;)
1017 24892 : {
1018 75676 : long j, ep = get_ep(W);
1019 189340 : for (j = 0; j < 2; j++)
1020 138556 : if (test55(ZX2_affine_unscale(W, j, 0), ep, g))
1021 : {
1022 : long lambda;
1023 91564 : GEN c = utoi(j), Wc = algo52(W, c, &lambda);
1024 91564 : if (!test53(lambda,ep,g))
1025 : {
1026 24892 : long r = lambda>>1;
1027 24892 : gel(M,1) = shifti(gel(M,1), r);
1028 24892 : gel(M,2) = ZM2_mul(gel(M,2), mkmat22(gen_2, c, gen_0, gen_1));
1029 24892 : W = ZX2_affine_unscale(Wc, j, r);
1030 24892 : break;
1031 : }
1032 : }
1033 75676 : if (j==2) break;
1034 : }
1035 50784 : return mkvec2(W, M);
1036 : }
1037 :
1038 : static GEN
1039 14 : algo56bis(GEN W, long g, long inf, long thr)
1040 : {
1041 14 : pari_sp av = avma;
1042 14 : GEN vl = cgetg(3,t_VEC);
1043 14 : long nl = 1;
1044 14 : W = algo51(W, NULL);
1045 14 : if (inf)
1046 : {
1047 14 : GEN Woo = hyperell_reverse(W, g);
1048 14 : long ep = get_ep(Woo);
1049 14 : if (test55(ZX2_unscale(W, 0), ep, g))
1050 : {
1051 : long lambda;
1052 14 : Woo = algo52(Woo, gen_0, &lambda);
1053 14 : if (lambda == thr) gel(vl,nl++) = ZX2_unscale(Woo, lambda>>1);
1054 : }
1055 : }
1056 : {
1057 14 : long j, ep = get_ep(W);
1058 42 : for (j = 0; j < 2; j++)
1059 28 : if (test55(ZX2_affine_unscale(W, j, 0), ep, g))
1060 : {
1061 : long lambda;
1062 28 : GEN Wc = algo52(W, utoi(j), &lambda);
1063 28 : if (lambda == thr) gel(vl,nl++) = ZX2_affine_unscale(Wc, j, lambda>>1);
1064 : }
1065 : }
1066 14 : setlg(vl, nl);
1067 14 : return gc_GEN(av,vl);
1068 : }
1069 :
1070 : /* return the (degree 2) apolar invariant (the nth transvectant of P and P) */
1071 : static GEN
1072 84 : ZX_apolar(GEN P, long n)
1073 : {
1074 84 : pari_sp av = avma;
1075 84 : long d = degpol(P), i;
1076 84 : GEN s = gen_0, g = cgetg(n+2,t_VEC);
1077 84 : gel(g,1) = gen_1;
1078 588 : for (i = 1; i <= n; i++) gel(g,i+1) = muliu(gel(g,i),i); /* g[i+1] = i! */
1079 658 : for (i = n-d; i <= d; i++)
1080 : {
1081 574 : GEN a = mulii(mulii(gel(g,i+1),gel(g,n-i+1)),
1082 574 : mulii(gel(P,i+2),gel(P,n-i+2)));
1083 574 : s = odd(i)? subii(s, a): addii(s, a);
1084 : }
1085 84 : return gc_INT(av,s);
1086 : }
1087 :
1088 : static GEN
1089 53017 : algo57(GEN F, long g, GEN pr)
1090 : {
1091 : long i, l;
1092 53017 : GEN D, C = content(F);
1093 53017 : GEN e = gel(core2(shifti(C,-vali(C))),2);
1094 53017 : GEN M = mkvec2(e, matid(2));
1095 53017 : long minvd = (2*g+1)>>(odd(g) ? 4:2);
1096 53017 : F = ZX_Z_divexact(F, sqri(e));
1097 53017 : D = absi(hyperelldisc(F));
1098 53017 : if (!pr)
1099 : {
1100 84 : GEN A = gcdii(D, ZX_apolar(F, 2*g+2));
1101 84 : pr = gel(factor(shifti(A, -vali(A))),1);
1102 : }
1103 53017 : l = lg(pr);
1104 312772 : for (i = 1; i < l; i++)
1105 : {
1106 : long ep;
1107 259755 : GEN p = gel(pr, i), ps2 = shifti(p,-1), Fe;
1108 259755 : if (equaliu(p,2) || Z_pval(D,p) < minvd) continue;
1109 197533 : ep = ZX_pvalrem(F,p, &Fe); Fe = FpX_red(Fe, p);
1110 197533 : if (degpol(Fe) < g+1+ep)
1111 : {
1112 6406 : GEN Fi = ZX_unscale(RgXn_recip_shallow(F,2*g+3), p);
1113 6406 : long lambda = ZX_pval(Fi,p);
1114 6406 : if (!test53(lambda,ep,g))
1115 : {
1116 3815 : GEN ppr = powiu(p,lambda>>1);
1117 3815 : F = ZX_Z_divexact(Fi,sqri(ppr));
1118 3815 : gel(M,1) = mulii(gel(M,1), ppr);
1119 3815 : gel(M,2) = ZM2_mul(gel(M,2), mkmat22(gen_0,gen_1,p,gen_0));
1120 : }
1121 : }
1122 : for(;;)
1123 25186 : {
1124 : GEN Fe, R;
1125 222719 : long j, lR, ep = ZX_pvalrem(F,p, &Fe);
1126 222719 : R = FpX_roots_mult(FpX_red(Fe, p), g+2-ep, p); lR = lg(R);
1127 235205 : for (j = 1; j<lR; j++)
1128 : {
1129 37672 : GEN c = Fp_center(gel(R,j), p, ps2);
1130 37672 : GEN Fi = ZX_affine(F,p,c);
1131 37672 : long lambda = ZX_pval(Fi,p);
1132 37672 : if (!test53(lambda,ep,g))
1133 : {
1134 25186 : GEN ppr = powiu(p,lambda>>1);
1135 25186 : F = ZX_Z_divexact(Fi, sqri(ppr));
1136 25186 : gel(M,1) = mulii(gel(M,1), ppr);
1137 25186 : gel(M,2) = ZM2_mul(gel(M,2), mkmat22(p,c,gen_0,gen_1));
1138 25186 : break;
1139 : }
1140 : }
1141 222719 : if (j==lR) break;
1142 : }
1143 : }
1144 53017 : return mkvec2(F, M);
1145 : }
1146 :
1147 : /* if inf=0, ignore point at infinity */
1148 : static GEN
1149 3080 : algo57bis(GEN F, long g, GEN p, long inf, long thr)
1150 : {
1151 3080 : pari_sp av = avma;
1152 3080 : GEN vl = cgetg(3,t_VEC), Fe;
1153 3080 : long nl = 1, ep = ZX_pvalrem(F,p, &Fe);
1154 3080 : Fe = FpX_red(Fe, p);
1155 : {
1156 3080 : GEN R = FpX_roots_mult(Fe, thr-ep, p);
1157 3080 : long j, lR = lg(R);
1158 5999 : for (j = 1; j<lR; j++)
1159 : {
1160 2919 : GEN Fj = ZX_affine(F, p, gel(R,j));
1161 2919 : long lambda = ZX_pvalrem(Fj, p, &Fj);
1162 2919 : if (lambda == thr) gel(vl,nl++) = odd(lambda)? ZX_Z_mul(Fj, p): Fj;
1163 : }
1164 : }
1165 3080 : if (inf==1 && 2*g+2-degpol(Fe) >= thr-ep)
1166 : {
1167 0 : GEN Fj = ZX_unscale(RgXn_recip_shallow(F,2*g+3), p);
1168 0 : long lambda = ZX_pvalrem(Fj, p, &Fj);
1169 0 : if (lambda == thr) gel(vl,nl++) = odd(lambda)? ZX_Z_mul(Fj, p): Fj;
1170 : }
1171 3080 : setlg(vl, nl);
1172 3080 : return gc_GEN(av,vl);
1173 : }
1174 :
1175 : static GEN
1176 3094 : next_model(GEN G, long g, GEN p, long inf, long thr)
1177 : {
1178 3108 : return equaliu(p,2) ? algo56bis(G, g, inf, thr)
1179 3108 : : algo57bis(G, g, p, inf, thr);
1180 : }
1181 :
1182 : static GEN
1183 1463 : get_extremal(GEN F, GEN G, long g, GEN p)
1184 : {
1185 : while (1)
1186 1260 : {
1187 : GEN Wi;
1188 1463 : Wi = next_model(G, g, p, 0, g+2);
1189 1463 : if (lg(Wi)==1) return F;
1190 1330 : F = gel(Wi,1);
1191 1330 : Wi = next_model(F, g, p, 0, g+1);
1192 1330 : if (lg(Wi)==1) return F;
1193 1260 : G = gel(Wi,1);
1194 : }
1195 : }
1196 :
1197 : GEN
1198 336 : hyperellextremalmodels(GEN F, long g, GEN p)
1199 : {
1200 336 : pari_sp av = avma;
1201 : GEN R, W;
1202 : long l;
1203 336 : if (equaliu(p,2))
1204 : {
1205 14 : if (get_ep(F)>0) return mkvec(F);
1206 : } else
1207 322 : if( ZX_pval(F,p) > 0) return mkvec(F);
1208 301 : W = next_model(F, g, p, 1, g+1); l = lg(W);
1209 301 : if (l==1) { set_avma(av); return mkvec(F); }
1210 196 : R = cgetg(3, t_VEC);
1211 196 : gel(R,1) = get_extremal(F, gel(W,1), g, p);
1212 196 : gel(R,2) = l==3 ? get_extremal(F, gel(W,2), g, p) : F;
1213 196 : if (gel(R,2) == gel(R,1)) setlg(R,2);
1214 196 : return gc_GEN(av, R);
1215 : }
1216 :
1217 : static GEN
1218 303719 : RgX_RgM2_eval(GEN P, GEN A, GEN Bp, long d)
1219 : {
1220 303719 : if (signe(P)==0)
1221 79834 : return P;
1222 : else
1223 : {
1224 223885 : long dP = degpol(P);
1225 223885 : GEN R = RgX_homogenous_evalpow(P, A, Bp);
1226 223885 : if (d > dP)
1227 17277 : R = gmul(R, gel(Bp,1+d-dP));
1228 223885 : return R;
1229 : }
1230 : }
1231 :
1232 : static GEN
1233 53031 : minimalmodel_merge(GEN W2, GEN Modd, long g, long v)
1234 : {
1235 53031 : GEN P = gel(W2,1), Q = gel(W2,2);
1236 53031 : GEN e = gel(Modd,1), M = gel(Modd,2);
1237 53031 : GEN A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
1238 53031 : GEN B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
1239 53031 : GEN Bp = gpowers(B, 2*g+2);
1240 53031 : long f = mod4(e)==1 ? 1: -1;
1241 53031 : GEN m = shifti(f > 0 ? subui(1,e): addui(1,e), -2);
1242 53031 : GEN m24 = subii(shifti(m,1), shifti(sqri(m),2));
1243 53031 : P = RgX_RgM2_eval(P, A, Bp, 2*g+2);
1244 53031 : Q = RgX_RgM2_eval(Q, A, Bp, g+1);
1245 53031 : P = ZX_Z_divexact(ZX_add(P, ZX_Z_mul(ZX_sqr(Q), m24)),sqri(e));
1246 53031 : if (f < 0) Q = ZX_neg(Q);
1247 53031 : return mkvec2(P,Q);
1248 : }
1249 :
1250 : static GEN
1251 106048 : hyperell_redQ(GEN W)
1252 : {
1253 106048 : GEN P = gel(W,1), Q = gel(W,2);
1254 106048 : GEN Pr, Qr = FpX_red(Q, gen_2);
1255 106048 : Pr = ZX_add(P, ZX_shifti(ZX_mul(ZX_sub(Q, Qr),ZX_add(Q, Qr)),-2));
1256 106048 : return mkvec2(Pr, Qr);
1257 : }
1258 :
1259 : static GEN
1260 50735 : minimalmodel_getH(GEN W, GEN Qn, GEN e, GEN M, long g, long v)
1261 : {
1262 50735 : GEN Q = gel(W,2);
1263 50735 : GEN A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
1264 50735 : GEN B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
1265 50735 : GEN Bp = gpowers(B, g+1);
1266 50735 : return ZX_shifti(ZX_sub(ZX_Z_mul(Qn,e),RgX_RgM2_eval(Q, A, Bp, g+1)), -1);
1267 : }
1268 :
1269 : static void
1270 53073 : check_hyperell_Q(const char *fun, GEN *pW, GEN *pF)
1271 : {
1272 53073 : GEN W = *pW, F = check_hyperell(W);
1273 : long v, d;
1274 53073 : if (!F || !signe(F) || !RgX_is_ZX(F)) pari_err_TYPE(fun, W);
1275 53066 : if (!signe(ZX_disc(F))) pari_err_DOMAIN(fun,"disc(W)","==",gen_0,W);
1276 53059 : v = varn(F); d = degpol(F);
1277 53059 : if (typ(W)==t_POL) W = mkvec2(W, pol_0(v));
1278 : else
1279 : {
1280 43603 : GEN P = gel(W, 1), Q = gel(W, 2);
1281 43603 : long g = ((d+1) >> 1) - 1;
1282 43603 : if (typ(P)!=t_POL) P = scalarpol_shallow(P, v);
1283 43603 : if (typ(Q)!=t_POL) Q = scalarpol_shallow(Q, v);
1284 43603 : if (!RgX_is_ZX(P) || !RgX_is_ZX(Q)) pari_err_TYPE(fun,W);
1285 43603 : if (degpol(P) > 2*g+2) pari_err_DOMAIN(fun, "deg(P)", ">", utoi(2*g+2), P);
1286 43603 : if (degpol(Q) > g+1) pari_err_DOMAIN(fun, "deg(Q)", ">", utoi(g+1), Q);
1287 43603 : W = mkvec2(P, Q);
1288 : }
1289 53059 : if (d < 3) pari_err_DOMAIN(fun, "genus", "=", gen_0, gen_0);
1290 53045 : *pW = W; *pF = F;
1291 53045 : }
1292 :
1293 : GEN
1294 53031 : hyperellminimalmodel(GEN W, GEN *pM, GEN pr)
1295 : {
1296 53031 : pari_sp av = avma;
1297 : GEN Wr, F, WM2, F2, W2, M2, Modd, Wf, ef, Mf, Hf;
1298 : long d, g, v;
1299 53031 : check_hyperell_Q("hyperellminimalmodel",&W, &F);
1300 53031 : if (pr && (!is_vec_t(typ(pr)) || !RgV_is_ZV(pr)))
1301 14 : pari_err_TYPE("hyperellminimalmodel",pr);
1302 53017 : d = degpol(F); g = ((d+1)>>1)-1; v = varn(F);
1303 53017 : Wr = hyperell_redQ(W);
1304 53017 : if (!pr || RgV_isin(pr, gen_2))
1305 : {
1306 50784 : WM2 = algo56(Wr,g); W2 = gel(WM2, 1); M2 = gel(WM2, 2);
1307 50784 : F2 = check_hyperell(W2);
1308 : }
1309 : else
1310 : {
1311 2233 : W2 = Wr; F2 = F; M2 = mkvec2(gen_1, matid(2));
1312 : }
1313 53017 : Modd = gel(algo57(F2, g, pr), 2);
1314 53017 : Wf = hyperell_redQ(minimalmodel_merge(W2, Modd, g, v));
1315 53017 : if (!pM) return gc_GEN(av, Wf);
1316 50721 : ef = mulii(gel(M2,1), gel(Modd,1));
1317 50721 : Mf = ZM2_mul(gel(M2,2), gel(Modd,2));
1318 50721 : Hf = minimalmodel_getH(W, gel(Wf,2), ef, Mf, g, v);
1319 50721 : *pM = mkvec3(ef, Mf, Hf);
1320 50721 : return gc_all(av, 2, &Wf, pM);
1321 : }
1322 :
1323 : GEN
1324 14 : hyperellminimaldisc(GEN W, GEN pr)
1325 : {
1326 14 : pari_sp av = avma;
1327 14 : GEN C = hyperellminimalmodel(W, NULL, pr);
1328 14 : return gc_INT(av, hyperelldisc(C));
1329 : }
1330 :
1331 : static GEN
1332 35 : redqfbsplit(GEN a, GEN b, GEN c, GEN d)
1333 : {
1334 35 : GEN p = subii(d,b), q = shifti(a,1);
1335 35 : GEN U, Q, u, v, w = bezout(p, q, &u, &v);
1336 :
1337 35 : if (!equali1(w)) { p = diviiexact(p, w); q = diviiexact(q, w); }
1338 35 : U = mkmat22(p, negi(v), q, u);
1339 35 : Q = qfb3_SL2_apply(mkvec3(a,b,c), U);
1340 35 : b = gel(Q, 2); c = gel(Q,3);
1341 35 : if (signe(b) < 0) gel(U,2) = mkcol2(v, negi(u));
1342 35 : gel(U,2) = ZC_lincomb(gen_1, truedivii(negi(c), d), gel(U,2), gel(U,1));
1343 35 : return U;
1344 : }
1345 :
1346 : static GEN
1347 16386 : polreduce(GEN P, GEN M)
1348 : {
1349 16386 : long v = varn(P), dP = degpol(P), d = odd(dP) ? dP+1: dP;
1350 16386 : GEN A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
1351 16386 : GEN B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
1352 16386 : return RgX_RgM2_eval(P, A, gpowers(B, d), d);
1353 : }
1354 :
1355 : /* assume deg(P) > 2 */
1356 : static GEN
1357 8193 : red_Cremona_Stoll(GEN P, GEN *pM)
1358 : {
1359 : GEN q1, q2, q3, M, R;
1360 8193 : long i, prec = nbits2prec(2*gexpo(P)) + EXTRAPRECWORD, d = degpol(P);
1361 8193 : GEN dP = ZX_deriv(P);
1362 : for (;;)
1363 0 : {
1364 8193 : GEN r = QX_complex_roots(P, prec);
1365 8193 : q1 = gen_0; q2 = gen_0; q3 = gen_0;
1366 41000 : for (i = 1; i <= d; i++)
1367 : {
1368 32807 : GEN ri = gel(r,i);
1369 32807 : GEN s = ginv(gabs(RgX_cxeval(dP,ri,NULL), prec));
1370 32807 : if (d!=4) s = gpow(s, gdivgs(gen_2,d-2), prec);
1371 32807 : q1 = gadd(q1, s);
1372 32807 : q2 = gsub(q2, gmul(real_i(ri), s));
1373 32807 : q3 = gadd(q3, gmul(gnorm(ri), s));
1374 : }
1375 8193 : M = lllgram(mkmat22(q1,q2,q2,q3));
1376 8193 : if (M && lg(M) == 3) break;
1377 0 : prec = precdbl(prec);
1378 : }
1379 8193 : R = polreduce(P, M);
1380 8193 : *pM = M;
1381 8193 : return R;
1382 : }
1383 :
1384 : /* assume deg(P) > 2 */
1385 : GEN
1386 8193 : ZX_hyperellred(GEN P, GEN *pM)
1387 : {
1388 8193 : pari_sp av = avma;
1389 8193 : long d = degpol(P);
1390 : GEN q1, q2, q3, D, vD;
1391 8193 : GEN a = gel(P,d+2), b = gel(P,d+1), c = gel(P, d);
1392 : GEN M, R, M2;
1393 :
1394 8193 : q1 = muliu(sqri(a), d);
1395 8193 : q2 = shifti(mulii(a,b), 1);
1396 8193 : q3 = subii(sqri(b), shifti(mulii(a,c), 1));
1397 8193 : D = gcdii(gcdii(q1, q2), q3);
1398 8193 : if (!equali1(D))
1399 : {
1400 8172 : q1 = diviiexact(q1, D);
1401 8172 : q2 = diviiexact(q2, D);
1402 8172 : q3 = diviiexact(q3, D);
1403 : }
1404 8193 : D = qfb_disc3(q1, q2, q3);
1405 8193 : if (!signe(D))
1406 49 : M = mkmat22(gen_1, truedivii(negi(q2),shifti(q1,1)), gen_0, gen_1);
1407 8144 : else if (issquareall(D,&vD))
1408 35 : M = redqfbsplit(q1, q2, q3, vD);
1409 : else
1410 8109 : M = gel(qfbredsl2(mkqfb(q1,q2,q3,D), NULL), 2);
1411 8193 : R = red_Cremona_Stoll(polreduce(P, M), &M2);
1412 8193 : if (pM) *pM = gmul(M, M2);
1413 8193 : return gc_all(av, pM ? 2: 1, &R, pM);
1414 : }
1415 :
1416 : GEN
1417 42 : hyperellred(GEN W, GEN *pM)
1418 : {
1419 42 : pari_sp av = avma;
1420 : long g, d, v;
1421 : GEN F, M, Wf, Hf;
1422 42 : check_hyperell_Q("hyperellred", &W, &F);
1423 14 : d = degpol(F); g = ((d+1)>>1)-1; v = varn(F);
1424 14 : (void) ZX_hyperellred(F, &M);
1425 14 : Wf = hyperell_redQ(minimalmodel_merge(W, mkvec2(gen_1, M), g, v));
1426 14 : Hf = minimalmodel_getH(W, gel(Wf,2), gen_1, M, g, v);
1427 14 : if (pM) *pM = mkvec3(gen_1, M, Hf);
1428 14 : return gc_all(av, pM ? 2: 1, &Wf, pM);
1429 : }
1430 :
1431 : static void
1432 65296 : check_hyperell_Rg(const char *fun, GEN *pW, GEN *pF)
1433 : {
1434 65296 : GEN W = *pW, F = check_hyperell(W);
1435 : long v;
1436 65296 : if (!F)
1437 7 : pari_err_TYPE(fun, W);
1438 65289 : if (degpol(F) <= 0) pari_err_CONSTPOL(fun);
1439 65282 : v = varn(F);
1440 65282 : if (typ(W)==t_POL) W = mkvec2(W, pol_0(v));
1441 : else
1442 : {
1443 65254 : GEN P = gel(W, 1), Q = gel(W, 2);
1444 65254 : long g = ((degpol(F)+1)>>1)-1;
1445 65254 : if( typ(P)!=t_POL) P = scalarpol(P, v);
1446 65254 : if( typ(Q)!=t_POL) Q = scalarpol(Q, v);
1447 65254 : if (degpol(P) > 2*g+2)
1448 0 : pari_err_DOMAIN(fun, "poldegree(P)", ">", utoi(2*g+2), P);
1449 65254 : if (degpol(Q) > g+1)
1450 0 : pari_err_DOMAIN(fun, "poldegree(Q)", ">", utoi(g+1), Q);
1451 :
1452 65254 : W = mkvec2(P, Q);
1453 : }
1454 65282 : if (pF) *pF = F;
1455 65282 : *pW = W;
1456 65282 : }
1457 :
1458 : static void
1459 65282 : check_hyperell_vc(const char *fun, GEN C, long v, GEN *e, GEN *M, GEN *H)
1460 : {
1461 65282 : if (typ(C) != t_VEC || lg(C) != 4) pari_err_TYPE(fun,C);
1462 65275 : *e = gel(C,1); *M = gel(C,2); *H = gel(C,3);
1463 65275 : if (typ(*M) != t_MAT || lg(*M) != 3 || lgcols(*M) != 3) pari_err_TYPE(fun,C);
1464 65268 : if (typ(*H)!=t_POL || varncmp(varn(*H),v) > 0) *H = scalarpol_shallow(*H,v);
1465 65268 : }
1466 :
1467 : GEN
1468 65296 : hyperellchangecurve(GEN W, GEN C)
1469 : {
1470 65296 : pari_sp av = avma;
1471 : GEN F, P, Q, A, B, Bp, e, M, H;
1472 : long d, g, v;
1473 65296 : check_hyperell_Rg("hyperellchangecurve",&W,&F);
1474 65282 : P = gel(W,1); Q = gel(W,2);
1475 65282 : d = degpol(F); g = ((d+1)>>1)-1; v = varn(F);
1476 65282 : check_hyperell_vc("hyperellchangecurve", C, v, &e, &M, &H);
1477 65268 : if (varncmp(gvar(M),v) <= 0)
1478 0 : pari_err_PRIORITY("hyperellchangecurve",M,"<=",v);
1479 65268 : A = deg1pol_shallow(gcoeff(M,1,1), gcoeff(M,1,2), v);
1480 65268 : B = deg1pol_shallow(gcoeff(M,2,1), gcoeff(M,2,2), v);
1481 65268 : Bp = gpowers(B, 2*g+2);
1482 65268 : P = RgX_RgM2_eval(P, A, Bp, 2*g+2);
1483 65268 : Q = RgX_RgM2_eval(Q, A, Bp, g+1);
1484 65268 : P = RgX_Rg_div(RgX_sub(P, RgX_mul(H,RgX_add(Q,H))), gsqr(e));
1485 65268 : Q = RgX_Rg_div(RgX_add(Q, RgX_mul2n(H,1)), e);
1486 65268 : return gc_GEN(av, mkvec2(P,Q));
1487 : }
1488 :
1489 : /****************************************************************************/
1490 : /*** ***/
1491 : /*** genus2charpoly ***/
1492 : /*** ***/
1493 : /****************************************************************************/
1494 :
1495 : /* Half stable reduction */
1496 :
1497 : static long
1498 588 : Zst_val(GEN P, GEN f, GEN p, long vt, GEN *pR)
1499 : {
1500 588 : pari_sp av = avma;
1501 588 : long v = varn(P);
1502 : while(1)
1503 1260 : {
1504 1848 : long i, j, dm = LONG_MAX;
1505 1848 : GEN Pm = NULL;
1506 1848 : long dP = degpol(P);
1507 7532 : for (i = 0; i <= minss(dP, dm); i++)
1508 : {
1509 5684 : GEN Py = gel(P, i+2);
1510 5684 : if (signe(Py))
1511 : {
1512 4186 : if (typ(Py)==t_POL)
1513 : {
1514 3864 : long dPy = degpol(Py);
1515 12502 : for (j = 0; j <= minss(dPy, dm-i); j++)
1516 : {
1517 8638 : GEN c = gel(Py, j+2);
1518 8638 : if (signe(c))
1519 : {
1520 3556 : if (i+j < dm)
1521 : {
1522 1848 : dm = i+j;
1523 1848 : Pm = monomial(gen_1, dm, v);
1524 1848 : gel(Pm,dm+2) = gen_0;
1525 : }
1526 3556 : gel(Pm,i+2) = c;
1527 : }
1528 : }
1529 : } else
1530 : {
1531 322 : if (i < dm)
1532 : {
1533 77 : dm = i;
1534 77 : Pm = monomial(Py, dm, v);
1535 : }
1536 : else
1537 245 : gel(Pm, i+2) = Py;
1538 : }
1539 : }
1540 : }
1541 1848 : Pm = RgX_renormalize(Pm);
1542 1848 : if (ZX_pval(Pm,p)==0)
1543 : {
1544 588 : *pR = gc_GEN(av, P);
1545 588 : return dm;
1546 : }
1547 1260 : Pm = RgX_homogenize_deg(Pm, dm, vt);
1548 1260 : P = gadd(gsub(P, Pm), gmul(f, ZXX_Z_divexact(Pm, p)));
1549 : }
1550 : }
1551 :
1552 : static long
1553 588 : Zst_normval(GEN P, GEN f, GEN p, long vt, GEN *pR)
1554 : {
1555 588 : long v = Zst_val(P, f, p, vt, pR);
1556 588 : long e = RgX_val(*pR)>>1;
1557 588 : if (e > 0)
1558 : {
1559 0 : v -= 2*e;
1560 0 : *pR = RgX_shift(*pR, -2*e);
1561 : }
1562 588 : return v;
1563 : }
1564 :
1565 : static GEN
1566 1176 : RgXY_swapsafe(GEN P, long v1, long v2)
1567 : {
1568 1176 : if (varn(P)==v2)
1569 : {
1570 77 : P = shallowcopy(P); setvarn(P,v1); return P;
1571 : } else
1572 1099 : return RgXY_swap(P, RgXY_degreex(P), v2);
1573 : }
1574 :
1575 : static GEN
1576 588 : Zst_red1(GEN P, GEN f, GEN p, long vt)
1577 : {
1578 588 : pari_sp av = avma;
1579 : GEN r, f1, f2, P1, P2;
1580 588 : long vs = varn(P);
1581 588 : long w = Zst_normval(P, f, p, vt, &r), ww = w-odd(w);
1582 588 : GEN st = monomial(pol_x(vt), 1, vs);
1583 588 : f1 = gsubst(f, vt, st);
1584 588 : P1 = gsubst(gdiv(r, monomial(gen_1,ww,vs)),vt,st);
1585 588 : f2 = gsubst(f, vs, st);
1586 588 : P2 = gsubst(gdiv(r, monomial(gen_1,ww,vt)),vs,st);
1587 588 : f2 = RgXY_swapsafe(f2, vs, vt);
1588 588 : P2 = RgXY_swapsafe(P2, vs, vt);
1589 588 : return gc_GEN(av, mkvec4(P1, f1, P2, f2));
1590 : }
1591 :
1592 : static GEN
1593 1176 : Zst_reduce(GEN P, GEN p, long vt, long *pv)
1594 : {
1595 : GEN C;
1596 1176 : long v = RgX_val(P);
1597 1176 : *pv = v + ZXX_pvalrem(RgX_shift(P, -v), p, &P);
1598 1176 : C = constant_coeff(P);
1599 1176 : C = typ(C) == t_POL ? C: scalarpol_shallow(C, vt);
1600 1176 : return FpX_red(C, p);
1601 : }
1602 :
1603 : static GEN
1604 588 : Zst_red3(GEN C, GEN p, long vt)
1605 : {
1606 : while(1)
1607 511 : {
1608 588 : GEN P1 = gel(C,1) ,f1 = gel(C,2), Poo = gel(C,3), foo= gel(C,4);
1609 : long e;
1610 588 : GEN Qoop = Zst_reduce(Poo, p, vt, &e), Qp, R;
1611 588 : if (RgX_val(Qoop) >= 3-e)
1612 : {
1613 0 : C = Zst_red1(Poo, foo, p, vt);
1614 511 : continue;
1615 : }
1616 588 : Qp = Zst_reduce(P1, p, vt, &e);
1617 588 : R = FpX_roots_mult(Qp, 3-e, p);
1618 588 : if (lg(R) > 1)
1619 511 : {
1620 511 : GEN xz = deg1pol_shallow(gen_1, gel(R,1), vt);
1621 511 : C = Zst_red1(gsubst(P1, vt, xz), gsubst(f1, vt, xz), p, vt);
1622 511 : continue;
1623 : }
1624 77 : return Qp;
1625 : }
1626 : }
1627 :
1628 : static GEN
1629 77 : genus2_halfstablemodel_i(GEN P, GEN p, long vt)
1630 : {
1631 : GEN Qp, R, Poo, Qoop;
1632 77 : long e = ZX_pvalrem(P, p, &Qp);
1633 77 : R = FpX_roots_mult(FpX_red(Qp,p), 4-e, p);
1634 77 : if (lg(R) > 1)
1635 : {
1636 77 : GEN C = Zst_red1(ZX_Z_translate(P, gel(R,1)), pol_x(vt), p, vt);
1637 77 : return Zst_red3(C, p, vt);
1638 : }
1639 0 : Poo = RgXn_recip_shallow(P, 7);
1640 0 : e = ZX_pvalrem(Poo, p, &Qoop);
1641 0 : Qoop = FpX_red(Qoop,p);
1642 0 : if (RgX_val(Qoop)>=4-e)
1643 : {
1644 0 : GEN C = Zst_red1(Poo, pol_x(vt), p, vt);
1645 0 : return Zst_red3(C, p, vt);
1646 : }
1647 0 : return gcopy(P);
1648 : }
1649 :
1650 : static GEN
1651 77 : genus2_halfstablemodel(GEN P, GEN p)
1652 : {
1653 77 : pari_sp av = avma;
1654 77 : long vt = fetch_var(), vs = varn(P);
1655 77 : GEN S = genus2_halfstablemodel_i(P, p, vt);
1656 77 : setvarn(S, vs); delete_var();
1657 77 : return gc_GEN(av, S);
1658 : }
1659 :
1660 : /* semi-stable reduction */
1661 :
1662 : static GEN
1663 532 : genus2_redmodel(GEN P, GEN p)
1664 : {
1665 : GEN LP, U, F;
1666 : long i, k, r;
1667 532 : if (degpol(P) < 0) return mkvec2(cgetg(1, t_COL), P);
1668 497 : F = FpX_factor_squarefree(P, p);
1669 497 : r = lg(F); U = NULL;
1670 1967 : for (i = k = 1; i < r; i++)
1671 : {
1672 1470 : GEN f = gel(F,i);
1673 1470 : long df = degpol(f);
1674 1470 : if (!df) continue;
1675 777 : if (odd(i)) U = U? FpX_mul(U, f, p): f;
1676 777 : if (i > 1) gel(F,k++) = df == 1? mkcol(f): gel(FpX_factor(f, p), 1);
1677 : }
1678 497 : LP = leading_coeff(P);
1679 497 : if (!U)
1680 112 : U = scalarpol_shallow(LP, varn(P));
1681 : else
1682 : {
1683 385 : GEN LU = leading_coeff(U);
1684 385 : if (!equalii(LU, LP)) U = FpX_Fp_mul(U, Fp_div(LP, LU, p), p);
1685 : }
1686 497 : setlg(F,k); if (k > 1) F = shallowconcat1(F);
1687 497 : return mkvec2(F, U);
1688 : }
1689 :
1690 : static GEN
1691 2870 : xdminusone(long d)
1692 : {
1693 2870 : return gsub(pol_xn(d, 0),gen_1);
1694 : }
1695 :
1696 : static GEN
1697 126 : ellfromeqncharpoly(GEN P, GEN Q, GEN p)
1698 : {
1699 : long v;
1700 : GEN E, F, t, y;
1701 126 : v = fetch_var();
1702 126 : y = pol_x(v);
1703 126 : F = gsub(gadd(ZX_sqr(y), gmul(y, Q)), P);
1704 126 : E = ellinit(ellfromeqn(F), p, DEFAULTPREC);
1705 126 : delete_var();
1706 126 : t = ellcharpoly(E, p);
1707 126 : obj_free(E);
1708 126 : return t;
1709 : }
1710 :
1711 : static GEN
1712 0 : nfellcharpoly(GEN e, GEN T, GEN p)
1713 : {
1714 : GEN nf, E, t;
1715 0 : e = shallowcopy(e);
1716 0 : nf = nfinit(mkvec2(T, mkvec(p)), DEFAULTPREC);
1717 : while(1)
1718 : {
1719 0 : E = ellinit(e, nf, DEFAULTPREC);
1720 0 : if (lg(E)!=1) break;
1721 0 : gel(e,5) = gadd(gel(e,5), p);
1722 : }
1723 0 : t = elleulerf(E, p);
1724 0 : obj_free(E);
1725 0 : return RgX_recip(ginv(t));
1726 : }
1727 :
1728 : static GEN
1729 0 : genus2_red5(GEN P, GEN T, GEN p)
1730 : {
1731 0 : long vx = varn(P), vy = varn(T);
1732 0 : GEN f = shallowcopy(T), pi = shifti(p,-1);
1733 0 : setvarn(f, vx);
1734 : while(1)
1735 0 : {
1736 : GEN Pr, R, r, Rs;
1737 0 : long v = ZXX_pvalrem(P, p, &Pr);
1738 0 : R = FpXQX_roots_mult(Pr, 2-v, T, p);
1739 0 : if (lg(R)==1) return P;
1740 0 : r = FpX_center(gel(R,1), p, pi);
1741 0 : Pr = RgX_affine(P, p, r);
1742 0 : setvarn(r, vx);
1743 0 : f = RgX_Rg_div(gsub(f, r), p);
1744 0 : Rs = RgX_rem(RgXY_swap(Pr, 3, vy), gsub(f, pol_x(vy)));
1745 0 : Pr = RgXY_swap(Rs, 3, vy);
1746 0 : if (ZXX_pvalrem(Pr, sqri(p), &Pr)==0) return P;
1747 0 : P = Pr;
1748 : }
1749 : }
1750 :
1751 : static GEN
1752 336 : genus2_type5(GEN P, GEN p)
1753 : {
1754 : GEN E, F, T, a, a2, Q;
1755 : long v;
1756 336 : if (equaliu(p, 2))
1757 14 : (void) ZXX_pvalrem(P, sqri(p), &P);
1758 336 : (void) ZX_pvalrem(P, p, &F);
1759 336 : F = FpX_red(F, p);
1760 336 : if (degpol(F) < 1) return NULL;
1761 336 : F = FpX_factor(F, p);
1762 336 : if (mael(F,2,1) != 3 || degpol(gmael(F,1,1)) != 2) return NULL;
1763 0 : T = gmael(F, 1, 1);
1764 0 : v = fetch_var_higher();
1765 0 : Q = RgV_to_RgX(ZX_digits(P, T), v);
1766 0 : Q = genus2_red5(Q, T, p);
1767 0 : a = gel(Q,5); a2 = ZX_sqr(a);
1768 0 : E = mkvec5(gen_0, gel(Q,4), gen_0, ZX_mul(gel(Q,3),a), ZX_mul(gel(Q,2),a2));
1769 0 : delete_var();
1770 0 : return nfellcharpoly(E, T, p);
1771 : }
1772 :
1773 : /* Assume P has semistable reduction at p */
1774 : static GEN
1775 532 : genus2_eulerfact_semistable(GEN P, GEN p)
1776 : {
1777 532 : GEN Pp = FpX_red(P, p);
1778 532 : GEN GU = genus2_redmodel(Pp, p);
1779 532 : long d = 6-degpol(Pp), v = d/2, w = odd(d);
1780 : GEN abe, tor;
1781 532 : GEN ki, kp = pol_1(0), kq = pol_1(0);
1782 532 : GEN F = gel(GU,1), Q = gel(GU,2);
1783 532 : long dQ = degpol(Q), lF = lg(F)-1;
1784 :
1785 7 : abe = dQ >= 5 ? hyperellcharpoly(gmul(Q,gmodulo(gen_1,p)))
1786 1057 : : dQ >= 3 ? ellfromeqncharpoly(Q,gen_0,p)
1787 525 : : pol_1(0);
1788 420 : ki = dQ != 0 ? xdminusone(1)
1789 644 : : Fp_issquare(gel(Q,2),p) ? ZX_sqr(xdminusone(1))
1790 112 : : xdminusone(2);
1791 532 : if (lF)
1792 : {
1793 : long i;
1794 1064 : for(i=1; i <= lF; i++)
1795 : {
1796 616 : GEN Fi = gel(F, i);
1797 616 : long d = degpol(Fi);
1798 616 : GEN e = FpX_rem(Q, Fi, p);
1799 980 : GEN kqf = lgpol(e)==0 ? xdminusone(d):
1800 714 : FpXQ_issquare(e, Fi, p) ? ZX_sqr(xdminusone(d))
1801 364 : : xdminusone(2*d);
1802 616 : kp = gmul(kp, xdminusone(d));
1803 616 : kq = gmul(kq, kqf);
1804 : }
1805 : }
1806 532 : if (v)
1807 : {
1808 245 : GEN kqoo = w==1 ? xdminusone(1):
1809 0 : Fp_issquare(leading_coeff(Q), p)? ZX_sqr(xdminusone(1))
1810 0 : : xdminusone(2);
1811 245 : kp = gmul(kp, xdminusone(1));
1812 245 : kq = gmul(kq, kqoo);
1813 : }
1814 532 : tor = RgX_div(ZX_mul(xdminusone(1), kq), ZX_mul(ki, kp));
1815 532 : return ZX_mul(abe, tor);
1816 : }
1817 :
1818 : GEN
1819 931 : genus2_eulerfact(GEN P, GEN p, long ra, long rt)
1820 : {
1821 931 : pari_sp av = avma;
1822 : GEN W, R, E;
1823 931 : long d = 2*ra+rt;
1824 931 : if (d == 0) return pol_1(0);
1825 322 : R = genus2_type5(P, p);
1826 322 : if (R) return R;
1827 322 : W = hyperellextremalmodels(P, 2, p);
1828 322 : if (lg(W) < 3)
1829 : {
1830 189 : GEN F = genus2_eulerfact_semistable(P,p);
1831 189 : if (degpol(F)!=d)
1832 : {
1833 77 : GEN S = genus2_halfstablemodel(P, p);
1834 77 : F = genus2_eulerfact_semistable(S, p);
1835 77 : if (degpol(F)!=d) pari_err_BUG("genus2charpoly");
1836 : }
1837 189 : return F;
1838 : }
1839 133 : E = gmul(genus2_eulerfact_semistable(gel(W,1),p),
1840 133 : genus2_eulerfact_semistable(gel(W,2),p));
1841 133 : return gc_upto(av, E);
1842 : }
1843 :
1844 : /* p = 2 */
1845 :
1846 : static GEN
1847 28 : F2x_genus2_find_trans(GEN P, GEN Q, GEN F)
1848 : {
1849 28 : pari_sp av = avma;
1850 28 : long i, d = F2x_degree(F), v = P[1];
1851 : GEN M, C, V;
1852 28 : M = cgetg(d+1, t_MAT);
1853 84 : for (i=1; i<=d; i++)
1854 : {
1855 56 : GEN Mi = F2x_rem(F2x_add(F2x_shift(Q,i-1), monomial_F2x(2*i-2,v)), F);
1856 56 : gel(M,i) = F2x_to_F2v(Mi, d);
1857 : }
1858 28 : C = F2x_to_F2v(F2x_rem(P, F), d);
1859 28 : V = F2m_F2c_invimage(M, C);
1860 28 : return gc_leaf(av, F2v_to_F2x(V, v));
1861 : }
1862 :
1863 : static GEN
1864 35 : F2x_genus2_trans(GEN P, GEN Q, GEN H)
1865 : {
1866 35 : return F2x_add(P,F2x_add(F2x_mul(H,Q), F2x_sqr(H)));
1867 : }
1868 :
1869 : static GEN
1870 42 : F2x_genus_redoo(GEN P, GEN Q, long k)
1871 : {
1872 42 : if (F2x_degree(P)==2*k)
1873 : {
1874 14 : long c = F2x_coeff(P,2*k-1), dQ = F2x_degree(Q);
1875 14 : if ((dQ==k-1 && c==1) || (dQ<k-1 && c==0))
1876 7 : return F2x_genus2_trans(P, Q, monomial_F2x(k, P[1]));
1877 : }
1878 35 : return P;
1879 : }
1880 :
1881 : static GEN
1882 35 : F2x_pseudodisc(GEN P, GEN Q)
1883 : {
1884 35 : GEN dP = F2x_deriv(P), dQ = F2x_deriv(Q);
1885 35 : return F2x_gcd(Q, F2x_add(F2x_mul(P, F2x_sqr(dQ)), F2x_sqr(dP)));
1886 : }
1887 :
1888 : static GEN
1889 14 : F2x_genus_red(GEN P, GEN Q)
1890 : {
1891 : long dP, dQ;
1892 : GEN F, FF;
1893 14 : P = F2x_genus_redoo(P, Q, 3);
1894 14 : P = F2x_genus_redoo(P, Q, 2);
1895 14 : P = F2x_genus_redoo(P, Q, 1);
1896 14 : dP = F2x_degree(P);
1897 14 : dQ = F2x_degree(Q);
1898 14 : FF = F = F2x_pseudodisc(P,Q);
1899 35 : while(F2x_degree(F)>0)
1900 : {
1901 21 : GEN M = gel(F2x_factor(F),1);
1902 21 : long i, l = lg(M);
1903 49 : for(i=1; i<l; i++)
1904 : {
1905 28 : GEN R = F2x_sqr(gel(M,i));
1906 28 : GEN H = F2x_genus2_find_trans(P, Q, R);
1907 28 : P = F2x_div(F2x_genus2_trans(P, Q, H), R);
1908 28 : Q = F2x_div(Q, gel(M,i));
1909 : }
1910 21 : F = F2x_pseudodisc(P, Q);
1911 : }
1912 14 : return mkvec4(P,Q,FF,mkvecsmall2(dP,dQ));
1913 : }
1914 :
1915 : /* Number of solutions of x^2+b*x+c */
1916 : static long
1917 21 : F2xqX_quad_nbroots(GEN b, GEN c, GEN T)
1918 : {
1919 21 : if (lgpol(b) > 0)
1920 : {
1921 14 : GEN d = F2xq_div(c, F2xq_sqr(b, T), T);
1922 14 : return F2xq_trace(d, T)? 0: 2;
1923 : }
1924 : else
1925 7 : return 1;
1926 : }
1927 :
1928 : static GEN
1929 14 : genus2_eulerfact2_semistable(GEN PQ)
1930 : {
1931 14 : GEN V = F2x_genus_red(ZX_to_F2x(gel(PQ, 1)), ZX_to_F2x(gel(PQ, 2)));
1932 14 : GEN P = gel(V, 1), Q = gel(V, 2);
1933 14 : GEN F = gel(V, 3), v = gel(V, 4);
1934 : GEN abe, tor;
1935 14 : GEN ki, kp = pol_1(0), kq = pol_1(0);
1936 14 : long dP = F2x_degree(P), dQ = F2x_degree(Q), d = maxss(dP, 2*dQ);
1937 14 : if (!lgpol(F)) return pol_1(0);
1938 21 : ki = dQ!=0 || dP>0 ? xdminusone(1):
1939 7 : dP==-1 ? ZX_sqr(xdminusone(1)): xdminusone(2);
1940 28 : abe = d>=5? hyperellcharpoly(gmul(PQ,gmodulss(1,2))):
1941 14 : d>=3? ellfromeqncharpoly(F2x_to_ZX(P), F2x_to_ZX(Q), gen_2):
1942 14 : pol_1(0);
1943 14 : if (lgpol(F))
1944 : {
1945 14 : GEN M = gel(F2x_factor(F), 1);
1946 14 : long i, lF = lg(M)-1;
1947 35 : for(i=1; i <= lF; i++)
1948 : {
1949 21 : GEN Fi = gel(M, i);
1950 21 : long d = F2x_degree(Fi);
1951 21 : long nb = F2xqX_quad_nbroots(F2x_rem(Q, Fi), F2x_rem(P, Fi), Fi);
1952 35 : GEN kqf = nb==1 ? xdminusone(d):
1953 0 : nb==2 ? ZX_sqr(xdminusone(d))
1954 14 : : xdminusone(2*d);
1955 21 : kp = gmul(kp, xdminusone(d));
1956 21 : kq = gmul(kq, kqf);
1957 : }
1958 : }
1959 14 : if (maxss(v[1],2*v[2])<5)
1960 : {
1961 14 : GEN kqoo = v[1]>2*v[2] ? xdminusone(1):
1962 0 : v[1]<2*v[2] ? ZX_sqr(xdminusone(1))
1963 7 : : xdminusone(2);
1964 7 : kp = gmul(kp, xdminusone(1));
1965 7 : kq = gmul(kq, kqoo);
1966 : }
1967 14 : tor = RgX_div(ZX_mul(xdminusone(1),kq), ZX_mul(ki, kp));
1968 14 : return ZX_mul(abe, tor);
1969 : }
1970 :
1971 : GEN
1972 14 : genus2_eulerfact2(GEN F, GEN PQ)
1973 : {
1974 14 : pari_sp av = avma;
1975 14 : GEN W, R = genus2_type5(F, gen_2), E;
1976 14 : if (R) return R;
1977 14 : W = hyperellextremalmodels(PQ, 2, gen_2);
1978 14 : if (lg(W) < 3) return genus2_eulerfact2_semistable(PQ);
1979 0 : E = gmul(genus2_eulerfact2_semistable(gel(W,1)),
1980 0 : genus2_eulerfact2_semistable(gel(W,2)));
1981 0 : return gc_upto(av, E);
1982 : }
1983 :
1984 : GEN
1985 882 : genus2charpoly(GEN G, GEN p)
1986 : {
1987 882 : pari_sp av = avma;
1988 882 : GEN gr = genus2red(G, p), F;
1989 882 : GEN PQ = gel(gr, 3), L = gel(gr, 4), r = gel(L, 4);
1990 882 : GEN P = gadd(gsqr(gel(PQ, 2)), gmul2n(gel(PQ, 1), 2));
1991 882 : if (equaliu(p,2))
1992 0 : F = genus2_eulerfact2(P, PQ);
1993 : else
1994 882 : F = genus2_eulerfact(P,p, r[1],r[2]);
1995 882 : return gc_upto(av, F);
1996 : }
|