Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - Hensel.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25406-bf255ab81b) Lines: 632 683 92.5 %
Date: 2020-06-04 05:59:24 Functions: 63 68 92.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  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. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : #include "pari.h"
      14             : #include "paripriv.h"
      15             : 
      16             : /***********************************************************************/
      17             : /**                                                                   **/
      18             : /**       QUADRATIC HENSEL LIFT (adapted from V. Shoup's NTL)         **/
      19             : /**                                                                   **/
      20             : /***********************************************************************/
      21             : /* Setup for divide/conquer quadratic Hensel lift
      22             :  *  a = set of k t_POL in Z[X] = factors over Fp (T=NULL) or Fp[Y]/(T)
      23             :  *  V = set of products of factors built as follows
      24             :  *  1) V[1..k] = initial a
      25             :  *  2) iterate:
      26             :  *      append to V the two smallest factors (minimal degree) in a, remove them
      27             :  *      from a and replace them by their product [net loss for a = 1 factor]
      28             :  *
      29             :  * W = bezout coeffs W[i]V[i] + W[i+1]V[i+1] = 1
      30             :  *
      31             :  * link[i] = -j if V[i] = a[j]
      32             :  *            j if V[i] = V[j] * V[j+1]
      33             :  * Arrays (link, V, W) pre-allocated for 2k - 2 elements */
      34             : static void
      35       60775 : BuildTree(GEN link, GEN V, GEN W, GEN a, GEN T, GEN p)
      36             : {
      37       60775 :   long k = lg(a)-1;
      38             :   long i, j, s, minp, mind;
      39             : 
      40      222607 :   for (i=1; i<=k; i++) { gel(V,i) = gel(a,i); link[i] = -i; }
      41             : 
      42      101057 :   for (j=1; j <= 2*k-5; j+=2,i++)
      43             :   {
      44       40282 :     minp = j;
      45       40282 :     mind = degpol(gel(V,j));
      46      491010 :     for (s=j+1; s<i; s++)
      47      450728 :       if (degpol(gel(V,s)) < mind) { minp = s; mind = degpol(gel(V,s)); }
      48             : 
      49       40282 :     swap(gel(V,j), gel(V,minp));
      50       40282 :     lswap(link[j], link[minp]);
      51             : 
      52       40282 :     minp = j+1;
      53       40282 :     mind = degpol(gel(V,j+1));
      54      450728 :     for (s=j+2; s<i; s++)
      55      410446 :       if (degpol(gel(V,s)) < mind) { minp = s; mind = degpol(gel(V,s)); }
      56             : 
      57       40282 :     swap(gel(V,j+1), gel(V,minp));
      58       40282 :     lswap(link[j+1], link[minp]);
      59             : 
      60       40282 :     gel(V,i) = FqX_mul(gel(V,j), gel(V,j+1), T, p);
      61       40282 :     link[i] = j;
      62             :   }
      63             : 
      64      161825 :   for (j=1; j <= 2*k-3; j+=2)
      65             :   {
      66             :     GEN d, u, v;
      67      101057 :     d = FqX_extgcd(gel(V,j), gel(V,j+1), T, p, &u, &v);
      68      101057 :     if (degpol(d) > 0) pari_err_COPRIME("BuildTree", gel(V,j), gel(V,j+1));
      69      101050 :     d = gel(d,2);
      70      101050 :     if (!gequal1(d))
      71             :     {
      72       91262 :       if (typ(d)==t_POL)
      73             :       {
      74        8309 :         d = FpXQ_inv(d, T, p);
      75        8309 :         u = FqX_Fq_mul(u, d, T, p);
      76        8309 :         v = FqX_Fq_mul(v, d, T, p);
      77             :       }
      78             :       else
      79             :       {
      80       82953 :         d = Fp_inv(d, p);
      81       82951 :         u = FqX_Fp_mul(u, d, T,p);
      82       82953 :         v = FqX_Fp_mul(v, d, T,p);
      83             :       }
      84             :     }
      85      101050 :     gel(W,j) = u;
      86      101050 :     gel(W,j+1) = v;
      87             :   }
      88       60768 : }
      89             : 
      90             : /* au + bv = 1 (p0), ab = f (p0). Lift mod p1 = p0 pd (<= p0^2).
      91             :  * If noinv is set, don't lift the inverses u and v */
      92             : static void
      93      424772 : ZpX_HenselLift(GEN V, GEN W, long j, GEN f, GEN pd, GEN p0, GEN p1, int noinv)
      94             : {
      95      424772 :   pari_sp av = avma;
      96      424772 :   long space = lg(f) * lgefint(p1);
      97             :   GEN a2, b2, g, z, s, t;
      98      424772 :   GEN a = gel(V,j), b = gel(V,j+1);
      99      424772 :   GEN u = gel(W,j), v = gel(W,j+1);
     100             : 
     101      424772 :   (void)new_chunk(space); /* HACK */
     102      424772 :   g = ZX_sub(f, ZX_mul(a,b));
     103      424772 :   g = ZX_Z_divexact(g, p0);
     104      424771 :   g = FpX_red(g, pd);
     105      424772 :   z = FpX_mul(v,g, pd);
     106      424772 :   t = FpX_divrem(z,a, pd, &s);
     107      424772 :   t = ZX_add(ZX_mul(u,g), ZX_mul(t,b));
     108      424772 :   t = FpX_red(t, pd);
     109      424772 :   t = ZX_Z_mul(t,p0);
     110      424770 :   s = ZX_Z_mul(s,p0);
     111      424770 :   set_avma(av);
     112      424770 :   a2 = ZX_add(a,s);
     113      424772 :   b2 = ZX_add(b,t);
     114             : 
     115             :   /* already reduced mod p1 = pd p0 */
     116      424772 :   gel(V,j)   = a2;
     117      424772 :   gel(V,j+1) = b2;
     118      424772 :   if (noinv) return;
     119             : 
     120      333473 :   av = avma;
     121      333473 :   (void)new_chunk(space); /* HACK */
     122      333473 :   g = ZX_add(ZX_mul(u,a2), ZX_mul(v,b2));
     123      333473 :   g = Z_ZX_sub(gen_1, g);
     124      333473 :   g = ZX_Z_divexact(g, p0);
     125      333473 :   g = FpX_red(g, pd);
     126      333472 :   z = FpX_mul(v,g, pd);
     127      333472 :   t = FpX_divrem(z,a, pd, &s);
     128      333473 :   t = ZX_add(ZX_mul(u,g), ZX_mul(t,b));
     129      333473 :   t = FpX_red(t, pd);
     130      333473 :   t = ZX_Z_mul(t,p0);
     131      333473 :   s = ZX_Z_mul(s,p0);
     132      333473 :   set_avma(av);
     133      333473 :   gel(W,j)   = ZX_add(u,t);
     134      333473 :   gel(W,j+1) = ZX_add(v,s);
     135             : }
     136             : 
     137             : static void
     138       36757 : ZpXQ_HenselLift(GEN V, GEN W, long j, GEN f, GEN Td, GEN T1, GEN pd, GEN p0, GEN p1, int noinv)
     139             : {
     140       36757 :   pari_sp av = avma;
     141       36757 :   const long n = degpol(T1), vT = varn(T1);
     142       36757 :   long space = lg(f) * lgefint(p1) * lg(T1);
     143             :   GEN a2, b2, g, z, s, t;
     144       36757 :   GEN a = gel(V,j), b = gel(V,j+1);
     145       36757 :   GEN u = gel(W,j), v = gel(W,j+1);
     146             : 
     147       36757 :   (void)new_chunk(space); /* HACK */
     148       36757 :   g = RgX_sub(f, Kronecker_to_ZXX(ZXX_mul_Kronecker(a,b,n), n, vT));
     149       36757 :   g = FpXQX_red(g, T1, p1);
     150       36757 :   g = RgX_Rg_divexact(g, p0);
     151       36757 :   z = FpXQX_mul(v,g, Td,pd);
     152       36757 :   t = FpXQX_divrem(z,a, Td,pd, &s);
     153       36757 :   t = ZX_add(ZXX_mul_Kronecker(u,g,n), ZXX_mul_Kronecker(t,b,n));
     154       36757 :   t = Kronecker_to_ZXX(t, n, vT);
     155       36757 :   t = FpXQX_red(t, Td, pd);
     156       36757 :   t = RgX_Rg_mul(t,p0);
     157       36757 :   s = RgX_Rg_mul(s,p0);
     158       36757 :   set_avma(av);
     159             : 
     160       36757 :   a2 = RgX_add(a,s);
     161       36757 :   b2 = RgX_add(b,t);
     162             :   /* already reduced mod p1 = pd p0 */
     163       36757 :   gel(V,j)   = a2;
     164       36757 :   gel(V,j+1) = b2;
     165       36757 :   if (noinv) return;
     166             : 
     167       27860 :   av = avma;
     168       27860 :   (void)new_chunk(space); /* HACK */
     169       27860 :   g = ZX_add(ZXX_mul_Kronecker(u,a2,n), ZXX_mul_Kronecker(v,b2,n));
     170       27860 :   g = Kronecker_to_ZXX(g, n, vT);
     171       27860 :   g = Rg_RgX_sub(gen_1, g);
     172       27860 :   g = FpXQX_red(g, T1, p1);
     173       27860 :   g = RgX_Rg_divexact(g, p0);
     174       27860 :   z = FpXQX_mul(v,g, Td,pd);
     175       27860 :   t = FpXQX_divrem(z,a, Td,pd, &s);
     176       27860 :   t = ZX_add(ZXX_mul_Kronecker(u,g,n), ZXX_mul_Kronecker(t,b,n));
     177       27860 :   t = Kronecker_to_ZXX(t, n, vT);
     178       27860 :   t = FpXQX_red(t, Td, pd);
     179       27860 :   t = RgX_Rg_mul(t,p0);
     180       27860 :   s = RgX_Rg_mul(s,p0);
     181       27860 :   set_avma(av);
     182       27860 :   gel(W,j)   = RgX_add(u,t);
     183       27860 :   gel(W,j+1) = RgX_add(v,s);
     184             : }
     185             : 
     186             : /* v list of factors, w list of inverses.  f = v[j] v[j+1]
     187             :  * Lift v[j] and v[j+1] mod p0 pd (possibly mod T), then all their divisors */
     188             : static void
     189     1092627 : ZpX_RecTreeLift(GEN link, GEN v, GEN w, GEN pd, GEN p0, GEN p1,
     190             :                 GEN f, long j, int noinv)
     191             : {
     192     1092627 :   if (j < 0) return;
     193      424771 :   ZpX_HenselLift(v, w, j, f, pd, p0,p1, noinv);
     194      424772 :   ZpX_RecTreeLift(link, v, w, pd, p0,p1, gel(v,j)  , link[j  ], noinv);
     195      424772 :   ZpX_RecTreeLift(link, v, w, pd, p0,p1, gel(v,j+1), link[j+1], noinv);
     196             : }
     197             : static void
     198       77014 : ZpXQ_RecTreeLift(GEN link, GEN v, GEN w, GEN Td, GEN T1, GEN pd, GEN p0, GEN p1,
     199             :                  GEN f, long j, int noinv)
     200             : {
     201       77014 :   if (j < 0) return;
     202       36757 :   ZpXQ_HenselLift(v, w, j, f, Td,T1, pd, p0,p1, noinv);
     203       36757 :   ZpXQ_RecTreeLift(link, v, w, Td,T1, pd, p0,p1, gel(v,j)  , link[j  ], noinv);
     204       36757 :   ZpXQ_RecTreeLift(link, v, w, Td,T1, pd, p0,p1, gel(v,j+1), link[j+1], noinv);
     205             : }
     206             : 
     207             : /* Assume n > 0. We want to go to accuracy n, starting from accuracy 1, using
     208             :  * a quadratically convergent algorithm. Goal: 9 -> 1,2,3,5,9 instead of
     209             :  * 1,2,4,8,9 (sequence of accuracies).
     210             :  *
     211             :  * Let a0 = 1, a1 = 2, a2, ... ak = n, the sequence of accuracies. To obtain
     212             :  * it, work backwards:
     213             :  *   a(k) = n, a(i-1) = (a(i) + 1) \ 2,
     214             :  * but we do not want to store a(i) explicitly, even as a t_VECSMALL, since
     215             :  * this would leave an object on the stack. We store a(i) implicitly in a
     216             :  * MASK: let a(0) = 1, if the i-bit of MASK is set, set a(i+1) = 2 a(i) - 1,
     217             :  * and 2a(i) otherwise.
     218             :  *
     219             :  * In fact, we do something a little more complicated to simplify the
     220             :  * function interface and avoid returning k and MASK separately: we return
     221             :  * MASK + 2^(k+1), so the highest bit of the mask indicates the length of the
     222             :  * sequence, and the following ones are as above. */
     223             : ulong
     224     2600212 : quadratic_prec_mask(long n)
     225             : {
     226     2600212 :   long a = n, i;
     227     2600212 :   ulong mask = 0;
     228     2600212 :   for(i = 1;; i++, mask <<= 1)
     229             :   {
     230     3936760 :     mask |= (a&1); a = (a+1)>>1;
     231     6536972 :     if (a==1) return mask | (1UL << i);
     232             :   }
     233             : }
     234             : 
     235             : /* Lift to precision p^e0.
     236             :  * a = modular factors of f mod (p,T) [possibly T=NULL]
     237             :  *  OR a TreeLift structure [e, link, v, w]: go on lifting
     238             :  * flag = 0: standard.
     239             :  * flag = 1: return TreeLift structure */
     240             : static GEN
     241       60803 : MultiLift(GEN f, GEN a, GEN T, GEN p, long e0, long flag)
     242             : {
     243       60803 :   long i, eold, e, k = lg(a) - 1;
     244             :   GEN E, v, w, link, penew, Tnew;
     245             :   ulong mask;
     246             :   pari_timer Ti;
     247             : 
     248       60803 :   if (k < 2) pari_err_DOMAIN("MultiLift", "#(modular factors)", "<", gen_2,a);
     249       60803 :   if (e0 < 1) pari_err_DOMAIN("MultiLift", "precision", "<", gen_1,stoi(e0));
     250       60803 :   if (e0 == 1) return a;
     251             : 
     252       60775 :   if (DEBUGLEVEL > 3) timer_start(&Ti);
     253       60775 :   if (typ(gel(a,1)) == t_INT)
     254             :   { /* a = TreeLift structure */
     255           0 :     e = itos(gel(a,1));
     256           0 :     link = gel(a,2);
     257           0 :     v    = gel(a,3);
     258           0 :     w    = gel(a,4);
     259             :   }
     260             :   else
     261             :   {
     262       60775 :     e = 1;
     263       60775 :     v = cgetg(2*k-2 + 1, t_VEC);
     264       60775 :     w = cgetg(2*k-2 + 1, t_VEC);
     265       60775 :     link=cgetg(2*k-2 + 1, t_VECSMALL);
     266       60775 :     BuildTree(link, v, w, a, T? FpX_red(T,p): NULL, p);
     267       60768 :     if (DEBUGLEVEL > 3) timer_printf(&Ti, "building tree");
     268             :   }
     269       60768 :   mask = quadratic_prec_mask(e0);
     270       60768 :   eold = 1;
     271       60768 :   penew = NULL;
     272       60768 :   Tnew = NULL;
     273       60768 :   if (DEBUGLEVEL > 3) err_printf("lifting to prec %ld\n", e0);
     274      307352 :   while (mask > 1)
     275             :   {
     276      246584 :     long enew = eold << 1;
     277      246584 :     if (mask & 1) enew--;
     278      246584 :     mask >>= 1;
     279      246584 :     if (enew >= e) { /* mask == 1: last iteration */
     280      246584 :       GEN peold = penew? penew: powiu(p, eold);
     281      246584 :       GEN Td = NULL, pd;
     282      246584 :       long d = enew - eold; /* = eold or eold-1 */
     283             :       /* lift from p^eold to p^enew */
     284      246584 :       pd = (d == eold)? peold: diviiexact(peold, p); /* p^d */
     285      246584 :       penew = mulii(peold,pd);
     286      246582 :       if (T) {
     287        3500 :         if (Tnew)
     288        2534 :           Td = (d == eold)? Tnew: FpX_red(Tnew,pd);
     289             :         else
     290         966 :           Td = FpX_red(T, peold);
     291        3500 :         Tnew = FpX_red(T, penew);
     292        3500 :         ZpXQ_RecTreeLift(link, v, w, Td, Tnew, pd, peold, penew, f, lg(v)-2,
     293             :                          (flag == 0 && mask == 1));
     294             :       }
     295             :       else
     296      243082 :         ZpX_RecTreeLift(link, v, w, pd, peold, penew, f, lg(v)-2,
     297             :                         (flag == 0 && mask == 1));
     298      246584 :       if (DEBUGLEVEL > 3) timer_printf(&Ti, "reaching prec %ld", enew);
     299             :     }
     300      246584 :     eold = enew;
     301             :   }
     302             : 
     303       60768 :   if (flag)
     304         490 :     E = mkvec4(utoipos(e0), link, v, w);
     305             :   else
     306             :   {
     307       60278 :     E = cgetg(k+1, t_VEC);
     308      260670 :     for (i = 1; i <= 2*k-2; i++)
     309             :     {
     310      200392 :       long t = link[i];
     311      200392 :       if (t < 0) gel(E,-t) = gel(v,i);
     312             :     }
     313             :   }
     314       60768 :   return E;
     315             : }
     316             : 
     317             : /* Q list of (coprime, monic) factors of pol mod (T,p). Lift mod p^e = pe.
     318             :  * T may be NULL */
     319             : GEN
     320      102596 : ZpX_liftfact(GEN pol, GEN Q, GEN pe, GEN p, long e)
     321             : {
     322      102596 :   pari_sp av = avma;
     323      102596 :   pol = FpX_normalize(pol, pe);
     324      102596 :   if (lg(Q) == 2) return mkvec(pol);
     325       59340 :   return gerepilecopy(av, MultiLift(pol, Q, NULL, p, e, 0));
     326             : }
     327             : 
     328             : GEN
     329         973 : ZpXQX_liftfact(GEN pol, GEN Q, GEN T, GEN pe, GEN p, long e)
     330             : {
     331         973 :   pari_sp av = avma;
     332         973 :   pol = FpXQX_normalize(pol, T, pe);
     333         973 :   if (lg(Q) == 2) return mkvec(pol);
     334         973 :   return gerepilecopy(av, MultiLift(pol, Q, T, p, e, 0));
     335             : }
     336             : 
     337             : GEN
     338        3052 : ZqX_liftfact(GEN f, GEN a, GEN T, GEN pe, GEN p, long e)
     339        3052 : { return T ? ZpXQX_liftfact(f, a, T, pe, p, e): ZpX_liftfact(f, a, pe, p, e); }
     340             : GEN
     341          42 : ZqX_liftroot(GEN f, GEN a, GEN T, GEN p, long e)
     342          42 : { return T ? ZpXQX_liftroot(f, a,T , p, e): ZpX_liftroot(f, a, p, e); }
     343             : 
     344             : /* U = NULL treated as 1 */
     345             : static void
     346        2198 : BezoutPropagate(GEN link, GEN v, GEN w, GEN pe, GEN U, GEN f, long j)
     347             : {
     348             :   GEN Q, R;
     349        2198 :   if (j < 0) return;
     350             : 
     351         854 :   Q = FpX_mul(gel(v,j), gel(w,j), pe);
     352         854 :   if (U)
     353             :   {
     354         364 :     Q = FpXQ_mul(Q, U, f, pe);
     355         364 :     R = FpX_sub(U, Q, pe);
     356             :   }
     357             :   else
     358         490 :     R = Fp_FpX_sub(gen_1, Q, pe);
     359         854 :   gel(w,j+1) = Q; /*  0 mod U v[j],  1 mod (1-U) v[j+1] */
     360         854 :   gel(w,j) = R; /*  1 mod U v[j],  0 mod (1-U) v[j+1] */
     361         854 :   BezoutPropagate(link, v, w, pe, R, f, link[j  ]);
     362         854 :   BezoutPropagate(link, v, w, pe, Q, f, link[j+1]);
     363             : }
     364             : 
     365             : /* as above, but return the Bezout coefficients for the lifted modular factors
     366             :  *   U[i] = 1 mod Qlift[i]
     367             :  *          0 mod Qlift[j], j != i */
     368             : GEN
     369         511 : bezout_lift_fact(GEN pol, GEN Q, GEN p, long e)
     370             : {
     371         511 :   pari_sp av = avma;
     372             :   GEN E, link, v, w, pe;
     373         511 :   long i, k = lg(Q)-1;
     374         511 :   if (k == 1) retmkvec(pol_1(varn(pol)));
     375         490 :   pe = powiu(p, e);
     376         490 :   pol = FpX_normalize(pol, pe);
     377         490 :   E = MultiLift(pol, Q, NULL, p, e, 1);
     378         490 :   link = gel(E,2);
     379         490 :   v    = gel(E,3);
     380         490 :   w    = gel(E,4);
     381         490 :   BezoutPropagate(link, v, w, pe, NULL, pol, lg(v)-2);
     382         490 :   E = cgetg(k+1, t_VEC);
     383        2198 :   for (i = 1; i <= 2*k-2; i++)
     384             :   {
     385        1708 :     long t = link[i];
     386        1708 :     if (t < 0) E[-t] = w[i];
     387             :   }
     388         490 :   return gerepilecopy(av, E);
     389             : }
     390             : 
     391             : /* Front-end for ZpX_liftfact:
     392             :    lift the factorization of pol mod p given by L to p^N (if possible) */
     393             : GEN
     394          28 : polhensellift(GEN pol, GEN L, GEN Tp, long N)
     395             : {
     396             :   GEN T, p;
     397             :   long i, l;
     398          28 :   pari_sp av = avma;
     399             :   void (*chk)(GEN, const char*);
     400             : 
     401          28 :   if (typ(pol) != t_POL) pari_err_TYPE("polhensellift",pol);
     402          28 :   RgX_check_ZXX(pol, "polhensellift");
     403          28 :   if (!is_vec_t(typ(L)) || lg(L) < 3) pari_err_TYPE("polhensellift",L);
     404          28 :   if (N < 1) pari_err_DOMAIN("polhensellift", "precision", "<", gen_1,stoi(N));
     405          28 :   if (!ff_parse_Tp(Tp, &T, &p, 0)) pari_err_TYPE("polhensellift",Tp);
     406          28 :   chk = T? RgX_check_ZXX: RgX_check_ZX;
     407          28 :   l = lg(L); L = leafcopy(L);
     408          70 :   for (i = 1; i < l; i++)
     409             :   {
     410          49 :     GEN q = gel(L,i);
     411          49 :     if (typ(q) != t_POL) gel(L,i) = scalar_ZX_shallow(q, varn(pol));
     412          49 :     else chk(q, "polhensellift");
     413             :   }
     414          21 :   return gerepilecopy(av, ZqX_liftfact(pol, L, T, powiu(p,N), p, N));
     415             : }
     416             : 
     417             : static GEN
     418       48034 : FqV_roots_from_deg1(GEN x, GEN T, GEN p)
     419             : {
     420       48034 :   long i,l = lg(x);
     421       48034 :   GEN r = cgetg(l,t_COL);
     422      123642 :   for (i=1; i<l; i++) { GEN P = gel(x,i); gel(r,i) = Fq_neg(gel(P,2), T, p); }
     423       48034 :   return r;
     424             : }
     425             : 
     426             : static GEN
     427       47992 : ZpX_liftroots_full(GEN f, GEN S, GEN q, GEN p, long e)
     428             : {
     429       47992 :   pari_sp av = avma;
     430       47992 :   GEN y = ZpX_liftfact(f, deg1_from_roots(S, varn(f)), q, p, e);
     431       47992 :   return gerepileupto(av, FqV_roots_from_deg1(y, NULL, q));
     432             : }
     433             : 
     434             : GEN
     435       47834 : ZpX_roots(GEN F, GEN p, long e)
     436             : {
     437       47834 :   pari_sp av = avma;
     438       47834 :   GEN q = powiu(p, e);
     439       47834 :   GEN f = FpX_normalize(F, p);
     440       47834 :   GEN g = FpX_normalize(FpX_split_part(f, p), p);
     441             :   GEN S;
     442       47834 :   if (degpol(g) < degpol(f))
     443             :   {
     444       42004 :     GEN h = FpX_div(f, g, p);
     445       42004 :     F = gel(ZpX_liftfact(F, mkvec2(g, h), q, p, e), 1);
     446             :   }
     447       47834 :   S = FpX_roots(g, p);
     448       47834 :   return gerepileupto(av, ZpX_liftroots_full(F, S, q, p, e));
     449             : }
     450             : 
     451             : static GEN
     452          42 : ZpXQX_liftroots_full(GEN f, GEN S, GEN T, GEN q, GEN p, long e)
     453             : {
     454          42 :   pari_sp av = avma;
     455          42 :   GEN y = ZpXQX_liftfact(f, deg1_from_roots(S, varn(f)), T, q, p, e);
     456          42 :   return gerepileupto(av, FqV_roots_from_deg1(y, T, q));
     457             : }
     458             : 
     459             : GEN
     460          42 : ZpXQX_roots(GEN F, GEN T, GEN p, long e)
     461             : {
     462          42 :   pari_sp av = avma;
     463          42 :   GEN q = powiu(p, e);
     464          42 :   GEN f = FpXQX_normalize(F, T, p);
     465          42 :   GEN g = FpXQX_normalize(FpXQX_split_part(f, T, p), T, p);
     466             :   GEN S;
     467          42 :   if (degpol(g) < degpol(f))
     468             :   {
     469          14 :     GEN h = FpXQX_div(f, g, T, p);
     470          14 :     F = gel(ZpXQX_liftfact(F, mkvec2(g, h), T, q, p, e), 1);
     471             :   }
     472          42 :   S = FpXQX_roots(g, T, p);
     473          42 :   return gerepileupto(av, ZpXQX_liftroots_full(F, S, T, q, p, e));
     474             : }
     475             : 
     476             : GEN
     477        2429 : ZqX_roots(GEN F, GEN T, GEN p, long e)
     478             : {
     479        2429 :   return T ? ZpXQX_roots(F, T, p, e): ZpX_roots(F, p, e);
     480             : }
     481             : 
     482             : /* SPEC:
     483             :  * p is a t_INT > 1, e >= 1
     484             :  * f is a ZX with leading term prime to p.
     485             :  * a is a simple root mod l for all l|p.
     486             :  * Return roots of f mod p^e, as integers (implicitly mod p^e)
     487             :  * STANDARD USE: p is a prime power */
     488             : GEN
     489         406 : ZpX_liftroot(GEN f, GEN a, GEN p, long e)
     490             : {
     491         406 :   pari_sp av = avma;
     492         406 :   GEN q = p, fr, W;
     493             :   ulong mask;
     494             : 
     495         406 :   a = modii(a,q);
     496         406 :   if (e == 1) return a;
     497         406 :   mask = quadratic_prec_mask(e);
     498         406 :   fr = FpX_red(f,q);
     499         406 :   W = Fp_inv(FpX_eval(ZX_deriv(fr), a, q), q); /* 1/f'(a) mod p */
     500             :   for(;;)
     501             :   {
     502        1519 :     q = sqri(q);
     503        1519 :     if (mask & 1) q = diviiexact(q, p);
     504        1519 :     mask >>= 1;
     505        1519 :     fr = FpX_red(f,q);
     506        1519 :     a = Fp_sub(a, Fp_mul(W, FpX_eval(fr, a,q), q), q);
     507        1519 :     if (mask == 1) return gerepileuptoint(av, a);
     508        1113 :     W = Fp_sub(shifti(W,1), Fp_mul(Fp_sqr(W,q), FpX_eval(ZX_deriv(fr),a,q), q), q);
     509             :   }
     510             : }
     511             : 
     512             : GEN
     513         158 : ZpX_liftroots(GEN f, GEN S, GEN p, long e)
     514             : {
     515         158 :   long i, n = lg(S)-1, d = degpol(f);
     516             :   GEN r;
     517         158 :   if (n == d) return ZpX_liftroots_full(f, S, powiu(p, e), p, e);
     518           0 :   r = cgetg(n+1, typ(S));
     519           0 :   for (i=1; i <= n; i++)
     520           0 :     gel(r,i) = ZpX_liftroot(f, gel(S,i), p, e);
     521           0 :   return r;
     522             : }
     523             : 
     524             : GEN
     525         119 : ZpXQX_liftroot_vald(GEN f, GEN a, long v, GEN T, GEN p, long e)
     526             : {
     527         119 :   pari_sp av = avma, av2;
     528         119 :   GEN pv = p, q, W, df, Tq, fr, dfr;
     529             :   ulong mask;
     530         119 :   a = Fq_red(a, T, p);
     531         119 :   if (e <= v+1) return a;
     532         119 :   df = RgX_deriv(f);
     533         119 :   if (v) { pv = powiu(p,v); df = ZXX_Z_divexact(df, pv); }
     534         119 :   mask = quadratic_prec_mask(e-v);
     535         119 :   Tq = FpXT_red(T, p); dfr = FpXQX_red(df, Tq, p);
     536         119 :   W = Fq_inv(FqX_eval(dfr, a, Tq, p), Tq, p); /* 1/f'(a) mod (T,p) */
     537         119 :   q = p;
     538         119 :   av2 = avma;
     539             :   for (;;)
     540         161 :   {
     541             :     GEN u, fa, qv, q2v, q2, Tq2;
     542         280 :     q2 = q; q = sqri(q);
     543         280 :     if (mask & 1) q = diviiexact(q,p);
     544         280 :     mask >>= 1;
     545         280 :     if (v) { qv = mulii(q, pv); q2v = mulii(q2, pv); }
     546         280 :     else { qv = q; q2v = q2; }
     547         280 :     Tq2 = FpXT_red(T, q2v); Tq = FpXT_red(T, qv);
     548         280 :     fr = FpXQX_red(f, Tq, qv);
     549         280 :     fa = FqX_eval(fr, a, Tq, qv);
     550         280 :     fa = typ(fa)==t_INT? diviiexact(fa,q2v): ZX_Z_divexact(fa, q2v);
     551         280 :     a = Fq_sub(a, Fq_mul(Fq_mul(W,fa,Tq2,q2v),q2, Tq,qv), Tq, qv);
     552         280 :     if (mask == 1) return gerepileupto(av, a);
     553         161 :     dfr = FpXQX_red(df, Tq, q);
     554         161 :     u = Fq_sub(Fq_mul(W,FqX_eval(dfr,a,Tq,q),Tq,q),gen_1,Tq,q);
     555         161 :     u = typ(u)==t_INT? diviiexact(u,q2): ZX_Z_divexact(u,q2);
     556         161 :     W = Fq_sub(W, Fq_mul(Fq_mul(u,W,Tq2,q2),q2, Tq,q),Tq,q);
     557         161 :     if (gc_needed(av2,2))
     558             :     {
     559           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQX_liftroot, e = %ld", e);
     560           0 :       gerepileall(av2, 3, &a, &W, &q);
     561             :     }
     562             :   }
     563             : }
     564             : 
     565             : GEN
     566         119 : ZpXQX_liftroot(GEN f, GEN a, GEN T, GEN p, long e) { return ZpXQX_liftroot_vald(f,a,0,T,p,e); }
     567             : 
     568             : GEN
     569           0 : ZpXQX_liftroots(GEN f, GEN S, GEN T, GEN p, long e)
     570             : {
     571           0 :   long i, n = lg(S)-1, d = degpol(f);
     572             :   GEN r;
     573           0 :   if (n == d) return ZpXQX_liftroots_full(f, S, T, powiu(p, e), p, e);
     574           0 :   r = cgetg(n+1, typ(S));
     575           0 :   for (i=1; i <= n; i++)
     576           0 :     gel(r,i) = ZpXQX_liftroot(f, gel(S,i), T, p, e);
     577           0 :   return r;
     578             : }
     579             : 
     580             : /* Same as ZpX_liftroot for the polynomial X^n-b*/
     581             : GEN
     582      171899 : Zp_sqrtnlift(GEN b, GEN n, GEN a, GEN p, long e)
     583             : {
     584      171899 :   pari_sp ltop=avma;
     585             :   GEN q, w, n_1;
     586             :   ulong mask;
     587      171899 :   long pis2 = equalii(n, gen_2)? 1: 0;
     588      171899 :   if (e == 1) return icopy(a);
     589      167013 :   n_1 = subiu(n,1);
     590      167013 :   mask = quadratic_prec_mask(e);
     591      167013 :   w = Fp_inv(pis2 ? shifti(a,1): Fp_mul(n,Fp_pow(a,n_1,p), p), p);
     592      167013 :   q = p;
     593             :   for(;;)
     594             :   {
     595      262164 :     q = sqri(q);
     596      262164 :     if (mask & 1) q = diviiexact(q, p);
     597      262164 :     mask >>= 1;
     598      262164 :     if (lgefint(q) == 3 && lgefint(n) == 3)
     599       95013 :     {
     600      261760 :       ulong Q = uel(q,2), N = uel(n,2);
     601      261760 :       ulong A = umodiu(a, Q);
     602      261760 :       ulong B = umodiu(b, Q);
     603      261760 :       ulong W = umodiu(w, Q);
     604             : 
     605      261760 :       A = Fl_sub(A, Fl_mul(W, Fl_sub(Fl_powu(A,N,Q), B, Q), Q), Q);
     606      261760 :       a = utoi(A);
     607      261760 :       if (mask == 1) break;
     608       95013 :       W = Fl_sub(Fl_add(W,W,Q),
     609             :                  Fl_mul(Fl_sqr(W,Q), Fl_mul(N,Fl_powu(A, N-1, Q), Q), Q), Q);
     610       95013 :       w = utoi(W);
     611             :     }
     612             :     else
     613             :     {
     614             :       /* a -= w (a^n - b) */
     615         404 :       a = modii(subii(a, mulii(w, subii(Fp_pow(a,n,q),b))), q);
     616         404 :       if (mask == 1) break;
     617             :       /* w += w - w^2 n a^(n-1)*/
     618         276 :       w = subii(shifti(w,1), Fp_mul(Fp_sqr(w,q),
     619         138 :                            pis2? shifti(a,1): mulii(n,Fp_pow(a,n_1,q)), q));
     620             :     }
     621             :   }
     622      167013 :   return gerepileuptoint(ltop,a);
     623             : }
     624             : 
     625             : /* Same as ZpX_liftroot for the polynomial X^2-b */
     626             : GEN
     627      139769 : Zp_sqrtlift(GEN b, GEN a, GEN p, long e)
     628             : {
     629      139769 :   return Zp_sqrtnlift(b, gen_2, a, p, e);
     630             : }
     631             : 
     632             : GEN
     633     1352379 : Zp_sqrt(GEN x, GEN p, long e)
     634             : {
     635             :   pari_sp av;
     636             :   GEN z;
     637     1352379 :   if (absequaliu(p,2)) return Z2_sqrt(x,e);
     638     1350874 :   av = avma;
     639     1350874 :   z = Fp_sqrt(Fp_red(x, p), p);
     640     1350874 :   if (!z) return NULL;
     641     1350825 :   if (e > 1) z = Zp_sqrtlift(x, z, p, e);
     642     1350825 :   return gerepileuptoint(av, z);
     643             : }
     644             : 
     645             : /* Compute (x-1)/(x+1)/p^k */
     646             : static GEN
     647       20879 : ZpXQ_log_to_ath(GEN x, long k, GEN T, GEN p, long e, GEN pe)
     648             : {
     649       20879 :   pari_sp av = avma;
     650       20879 :   long vT = get_FpX_var(T);
     651             :   GEN bn, bdi;
     652       20879 :   GEN bd = ZX_Z_add(x, gen_1);
     653       20879 :   if (absequaliu(p,2)) /*For p=2, we need to simplify by 2*/
     654             :   {
     655        6949 :     bn = ZX_shifti(x,-(k+1));
     656        6949 :     bdi= ZpXQ_invlift(ZX_shifti(bd ,-1), pol_1(vT), T, p, e);
     657             :   }
     658             :   else
     659             :   {
     660       13930 :     bn = ZX_Z_divexact(ZX_Z_sub(x, gen_1),powiu(p,k));
     661       13930 :     bdi= ZpXQ_invlift(bd, scalarpol(Fp_inv(gen_2,p),vT), T, p, e);
     662             :   }
     663       20879 :   return gerepileupto(av, FpXQ_mul(bn, bdi, T, pe));
     664             : }
     665             : 
     666             : /* Assume p odd, a = 1 [p], return log(a) */
     667             : GEN
     668       20879 : ZpXQ_log(GEN a, GEN T, GEN p, long N)
     669             : {
     670       20879 :   pari_sp av = avma;
     671             :   pari_timer ti;
     672       20879 :   long is2 = absequaliu(p,2);
     673       20879 :   ulong pp = is2 ? 0: itou_or_0(p);
     674       20879 :   double lp = is2 ? 1: pp ? log2(pp): expi(p);
     675       20879 :   long k = maxss(1 , (long) .5+pow((double)(N>>1)/(lp*lp), 1./3));
     676             :   GEN ak, s, b, pol;
     677       20879 :   long e = is2 ? N-1: N;
     678       20879 :   long i, l = (e-2)/(2*(k+is2));
     679       20879 :   GEN pe = powiu(p,e);
     680       20879 :   GEN TNk, pNk = powiu(p,N+k);
     681       20879 :   if( DEBUGLEVEL>=3) timer_start(&ti);
     682       20879 :   TNk = FpX_get_red(get_FpX_mod(T), pNk);
     683       20879 :   ak = FpXQ_pow(a, powiu(p,k), TNk, pNk);
     684       20879 :   if( DEBUGLEVEL>=3) timer_printf(&ti,"FpXQ_pow(%ld)",k);
     685       20879 :   b = ZpXQ_log_to_ath(ak, k, T, p, e, pe);
     686       20879 :   if( DEBUGLEVEL>=3) timer_printf(&ti,"ZpXQ_log_to_ath");
     687       20879 :   pol= cgetg(l+3,t_POL);
     688       20879 :   pol[1] = evalsigne(1)|evalvarn(0);
     689       56365 :   for(i=0; i<=l; i++)
     690             :   {
     691             :     GEN g;
     692       35486 :     ulong z = 2*i+1;
     693       35486 :     if (pp)
     694             :     {
     695       26222 :       long w = u_lvalrem(z, pp, &z);
     696       26222 :       g = powuu(pp,2*i*k-w);
     697             :     }
     698        9264 :     else g = powiu(p,2*i*k);
     699       35483 :     gel(pol,i+2) = Fp_divu(g, z,pe);
     700             :   }
     701       20879 :   if( DEBUGLEVEL>=3) timer_printf(&ti,"pol(%ld)",l);
     702       20879 :   s = FpX_FpXQ_eval(pol, FpXQ_sqr(b, T, pe), T,  pe);
     703       20879 :   if( DEBUGLEVEL>=3) timer_printf(&ti,"FpX_FpXQ_eval");
     704       20879 :   s = ZX_shifti(FpXQ_mul(b, s, T, pe), 1);
     705       20879 :   return gerepileupto(av, is2? s: FpX_red(s, pe));
     706             : }
     707             : 
     708             : /***********************************************************************/
     709             : /**                                                                   **/
     710             : /**                 Generic quadratic hensel lift over Zp[X]          **/
     711             : /**                                                                   **/
     712             : /***********************************************************************/
     713             : /* q = p^N */
     714             : 
     715             : GEN
     716           0 : gen_ZpM_Dixon(GEN F, GEN V, GEN q, GEN p, long N, void *E,
     717             :                             GEN lin(void *E, GEN F, GEN d, GEN q),
     718             :                             GEN invl(void *E, GEN d))
     719             : {
     720           0 :   pari_sp av = avma;
     721             :   long N2, M;
     722             :   GEN VN2, V2, VM, bil;
     723             :   GEN q2, qM;
     724           0 :   V = FpM_red(V, q);
     725           0 :   if (N == 1) return invl(E, V);
     726           0 :   N2 = (N + 1)>>1; M = N - N2;
     727           0 :   F = FpM_red(F, q);
     728           0 :   qM = powiu(p, M);
     729           0 :   q2 = M == N2? qM: mulii(qM, p);
     730             :   /* q2 = p^N2, qM = p^M, q = q2 * qM */
     731           0 :   VN2 = gen_ZpM_Dixon(F, V, q2, p, N2, E, lin, invl);
     732           0 :   bil = lin(E, F, VN2, q);
     733           0 :   V2 = ZM_Z_divexact(ZM_sub(V, bil), q2);
     734           0 :   VM = gen_ZpM_Dixon(F, V2, qM, p, M, E, lin, invl);
     735           0 :   return gerepileupto(av, FpM_red(ZM_add(VN2, ZM_Z_mul(VM, q2)), q));
     736             : }
     737             : 
     738             : GEN
     739      199829 : gen_ZpX_Dixon(GEN F, GEN V, GEN q, GEN p, long N, void *E,
     740             :                             GEN lin(void *E, GEN F, GEN d, GEN q),
     741             :                             GEN invl(void *E, GEN d))
     742             : {
     743      199829 :   pari_sp av = avma;
     744             :   long N2, M;
     745             :   GEN VN2, V2, VM, bil;
     746             :   GEN q2, qM;
     747      199829 :   V = FpX_red(V, q);
     748      199829 :   if (N == 1) return invl(E, V);
     749       45458 :   N2 = (N + 1)>>1; M = N - N2;
     750       45458 :   F = FpXT_red(F, q);
     751       45458 :   qM = powiu(p, M);
     752       45458 :   q2 = M == N2? qM: mulii(qM, p);
     753             :   /* q2 = p^N2, qM = p^M, q = q2 * qM */
     754       45458 :   VN2 = gen_ZpX_Dixon(F, V, q2, p, N2, E, lin, invl);
     755       45458 :   bil = lin(E, F, VN2, q);
     756       45458 :   V2 = ZX_Z_divexact(ZX_sub(V, bil), q2);
     757       45458 :   VM = gen_ZpX_Dixon(F, V2, qM, p, M, E, lin, invl);
     758       45458 :   return gerepileupto(av, FpX_red(ZX_add(VN2, ZX_Z_mul(VM, q2)), q));
     759             : }
     760             : 
     761             : GEN
     762      434950 : gen_ZpM_Newton(GEN x, GEN p, long n, void *E,
     763             :                       GEN eval(void *E, GEN f, GEN q),
     764             :                       GEN invd(void *E, GEN V, GEN v, GEN q, long M))
     765             : {
     766      434950 :   pari_sp ltop = avma, av;
     767      434950 :   long N = 1, N2, M;
     768             :   long mask;
     769      434950 :   GEN q = p;
     770      434950 :   if (n == 1) return gcopy(x);
     771      434950 :   mask = quadratic_prec_mask(n);
     772      434950 :   av = avma;
     773      895321 :   while (mask > 1)
     774             :   {
     775             :     GEN qM, q2, v, V;
     776      460371 :     N2 = N; N <<= 1;
     777      460371 :     q2 = q;
     778      460371 :     if (mask&1UL) { /* can never happen when q2 = p */
     779       18664 :       N--; M = N2-1;
     780       18664 :       qM = diviiexact(q2,p); /* > 1 */
     781       18664 :       q = mulii(qM,q2);
     782             :     } else {
     783      441707 :       M = N2;
     784      441707 :       qM = q2;
     785      441707 :       q = sqri(q2);
     786             :     }
     787             :     /* q2 = p^N2, qM = p^M, q = p^N = q2 * qM */
     788      460371 :     mask >>= 1;
     789      460371 :     v = eval(E, x, q);
     790      460371 :     V = ZM_Z_divexact(gel(v,1), q2);
     791      460371 :     x = FpM_sub(x, ZM_Z_mul(invd(E, V, v, qM, M), q2), q);
     792      460371 :     if (gc_needed(av, 1))
     793             :     {
     794           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_ZpX_Newton");
     795           0 :       gerepileall(av, 2, &x, &q);
     796             :     }
     797             :   }
     798      434950 :   return gerepileupto(ltop, x);
     799             : }
     800             : 
     801             : static GEN
     802      460371 : _ZpM_invd(void *E, GEN V, GEN v, GEN q, long M/*unused*/)
     803             : {
     804             :   (void)E; (void)M;
     805      460371 :   return FpM_mul(V, gel(v,2), q);
     806             : }
     807             : 
     808             : static GEN
     809      460371 : _ZpM_eval(void *E, GEN x, GEN q)
     810             : {
     811      460371 :   GEN f = RgM_Rg_sub_shallow(FpM_mul(x, FpM_red((GEN) E, q), q), gen_1);
     812      460371 :   return mkvec2(f, x);
     813             : }
     814             : 
     815             : GEN
     816      434950 : ZpM_invlift(GEN M, GEN C, GEN p, long n)
     817             : {
     818      434950 :   return gen_ZpM_Newton(C, p, n, (void *)M, _ZpM_eval, _ZpM_invd);
     819             : }
     820             : 
     821             : GEN
     822      265756 : gen_ZpX_Newton(GEN x, GEN p, long n, void *E,
     823             :                       GEN eval(void *E, GEN f, GEN q),
     824             :                       GEN invd(void *E, GEN V, GEN v, GEN q, long M))
     825             : {
     826      265756 :   pari_sp ltop = avma, av;
     827      265756 :   long N = 1, N2, M;
     828             :   long mask;
     829      265756 :   GEN q = p;
     830      265756 :   if (n == 1) return gcopy(x);
     831      261927 :   mask = quadratic_prec_mask(n);
     832      261927 :   av = avma;
     833      924793 :   while (mask > 1)
     834             :   {
     835             :     GEN qM, q2, v, V;
     836      662866 :     N2 = N; N <<= 1;
     837      662866 :     q2 = q;
     838      662866 :     if (mask&1UL) { /* can never happen when q2 = p */
     839      275466 :       N--; M = N2-1;
     840      275466 :       qM = diviiexact(q2,p); /* > 1 */
     841      275464 :       q = mulii(qM,q2);
     842             :     } else {
     843      387400 :       M = N2;
     844      387400 :       qM = q2;
     845      387400 :       q = sqri(q2);
     846             :     }
     847             :     /* q2 = p^N2, qM = p^M, q = p^N = q2 * qM */
     848      662860 :     mask >>= 1;
     849      662860 :     v = eval(E, x, q);
     850      662869 :     V = ZX_Z_divexact(gel(v,1), q2);
     851      662865 :     x = FpX_sub(x, ZX_Z_mul(invd(E, V, v, qM, M), q2), q);
     852      662866 :     if (gc_needed(av, 1))
     853             :     {
     854           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_ZpX_Newton");
     855           0 :       gerepileall(av, 2, &x, &q);
     856             :     }
     857             :   }
     858      261927 :   return gerepileupto(ltop, x);
     859             : }
     860             : 
     861             : struct _ZpXQ_inv
     862             : {
     863             :   GEN T, a, p ,n;
     864             : };
     865             : 
     866             : static GEN
     867      521199 : _inv_invd(void *E, GEN V, GEN v, GEN q, long M/*unused*/)
     868             : {
     869      521199 :   struct _ZpXQ_inv *d = (struct _ZpXQ_inv *) E;
     870      521199 :   GEN Tq = FpXT_red(d->T, q);
     871             :   (void)M;
     872      521199 :   return FpXQ_mul(V, gel(v,2), Tq, q);
     873             : }
     874             : 
     875             : static GEN
     876      521198 : _inv_eval(void *E, GEN x, GEN q)
     877             : {
     878      521198 :   struct _ZpXQ_inv *d = (struct _ZpXQ_inv *) E;
     879      521198 :   GEN Tq = FpXT_red(d->T, q);
     880      521199 :   GEN f = FpX_Fp_sub(FpXQ_mul(x, FpX_red(d->a, q), Tq, q), gen_1, q);
     881      521199 :   return mkvec2(f, x);
     882             : }
     883             : 
     884             : GEN
     885      201143 : ZpXQ_invlift(GEN a, GEN x, GEN T, GEN p, long e)
     886             : {
     887             :   struct _ZpXQ_inv d;
     888      201143 :   d.a = a; d.T = T; d.p = p;
     889      201143 :   return gen_ZpX_Newton(x, p, e, &d, _inv_eval, _inv_invd);
     890             : }
     891             : 
     892             : GEN
     893      180264 : ZpXQ_inv(GEN a, GEN T, GEN p, long e)
     894             : {
     895      180264 :   pari_sp av=avma;
     896             :   GEN ai;
     897      180264 :   if (lgefint(p)==3)
     898             :   {
     899      180236 :     ulong pp = p[2];
     900      180236 :     ai = Flx_to_ZX(Flxq_inv(ZX_to_Flx(a,pp), ZXT_to_FlxT(T, pp), pp));
     901             :   } else
     902          28 :     ai = FpXQ_inv(FpX_red(a,p), FpXT_red(T,p),p);
     903      180264 :   return gerepileupto(av, ZpXQ_invlift(a, ai, T, p, e));
     904             : }
     905             : 
     906             : GEN
     907       31584 : ZpXQ_div(GEN a, GEN b, GEN T, GEN q, GEN p, long e)
     908             : {
     909       31584 :   return FpXQ_mul(a, ZpXQ_inv(b, T, p, e), T, q);
     910             : }
     911             : 
     912             : GEN
     913      146440 : ZpXQX_divrem(GEN x, GEN Sp, GEN T, GEN q, GEN p, long e, GEN *pr)
     914             : {
     915      146440 :   pari_sp av = avma;
     916      146440 :   GEN S = get_FpXQX_mod(Sp);
     917      146440 :   GEN b = leading_coeff(S), bi;
     918             :   GEN S2, Q;
     919      146440 :   if (typ(b)==t_INT) return FpXQX_divrem(x, Sp, T, q, pr);
     920      146440 :   bi = ZpXQ_inv(b, T, p, e);
     921      146440 :   S2 = FqX_Fq_mul_to_monic(S, bi, T, q);
     922      146440 :   Q = FpXQX_divrem(x, S2, T, q, pr);
     923      146440 :   if (pr==ONLY_DIVIDES && !Q) { set_avma(av); return NULL; }
     924      146440 :   if (pr==ONLY_REM || pr==ONLY_DIVIDES) return gerepileupto(av, Q);
     925      146440 :   Q = FpXQX_FpXQ_mul(Q, bi, T, q);
     926      146440 :   gerepileall(av, 2, &Q, pr);
     927      146440 :   return Q;
     928             : }
     929             : 
     930             : GEN
     931         623 : ZpXQX_digits(GEN x, GEN B, GEN T, GEN q, GEN p, long e)
     932             : {
     933         623 :   pari_sp av = avma;
     934         623 :   GEN b = leading_coeff(B), bi;
     935             :   GEN B2, P, V, W;
     936             :   long i, lV;
     937         623 :   if (typ(b)==t_INT) return FpXQX_digits(x, B, T, q);
     938         623 :   bi = ZpXQ_inv(b, T, p, e);
     939         623 :   B2 = FqX_Fq_mul_to_monic(B, bi, T, q);
     940         623 :   V = FpXQX_digits(x, B2, T, q);
     941         623 :   lV = lg(V)-1;
     942         623 :   P = FpXQ_powers(bi, lV-1, T, q);
     943         623 :   W = cgetg(lV+1, t_VEC);
     944       11200 :   for(i=1; i<=lV; i++)
     945       10577 :     gel(W, i) = FpXQX_FpXQ_mul(gel(V,i), gel(P, i), T, q);
     946         623 :   return gerepileupto(av, W);
     947             : }
     948             : 
     949             : struct _ZpXQ_sqrtn
     950             : {
     951             :   GEN T, a, n, ai;
     952             : };
     953             : 
     954             : static GEN
     955        1806 : _sqrtn_invd(void *E, GEN V, GEN v, GEN q, long M)
     956             : {
     957        1806 :   struct _ZpXQ_sqrtn *d = (struct _ZpXQ_sqrtn *) E;
     958        1806 :   GEN Tq = FpX_red(d->T, q), aiq = FpX_red(d->ai, q);
     959             :   (void)M;
     960        1806 :   return FpXQ_mul(FpXQ_mul(V, gel(v,2), Tq, q), aiq, Tq, q);
     961             : }
     962             : 
     963             : static GEN
     964        1806 : _sqrtn_eval(void *E, GEN x, GEN q)
     965             : {
     966        1806 :   struct _ZpXQ_sqrtn *d = (struct _ZpXQ_sqrtn *) E;
     967        1806 :   GEN Tq = FpX_red(d->T, q);
     968        1806 :   GEN f = FpX_sub(FpXQ_pow(x, d->n, Tq, q), d->a, q);
     969        1806 :   return mkvec2(f, x);
     970             : }
     971             : 
     972             : GEN
     973        1155 : ZpXQ_sqrtnlift(GEN a, GEN n, GEN x, GEN T, GEN p, long e)
     974             : {
     975             :   struct _ZpXQ_sqrtn d;
     976        1155 :   d.a = a; d.T = T; d.n = n;
     977        1155 :   d.ai = ZpXQ_inv(ZX_Z_mul(a, n),T,p,(e+1)>>1);
     978        1155 :   return gen_ZpX_Newton(x, p, e, &d, _sqrtn_eval, _sqrtn_invd);
     979             : }
     980             : 
     981             : static GEN
     982           0 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
     983             : 
     984             : GEN
     985           0 : Zq_sqrtnlift(GEN a, GEN n, GEN x, GEN T, GEN p, long e)
     986             : {
     987           0 :   return T? ZpXQ_sqrtnlift(to_ZX(a,varn(T)), n, to_ZX(x,varn(T)), T, p, e)
     988           0 :           : Zp_sqrtnlift(a, n, x, p, e);
     989             : }
     990             : 
     991             : GEN
     992           0 : ZpXQ_sqrt(GEN a, GEN T, GEN p, long e)
     993             : {
     994           0 :   pari_sp av = avma;
     995           0 :   GEN z = FpXQ_sqrt(FpX_red(a, p), T, p);
     996           0 :   if (!z) return NULL;
     997           0 :   if (e <= 1) return gerepileupto(av, z);
     998           0 :   return gerepileupto(av, ZpXQ_sqrtnlift(a, gen_2, z, T, p, e));
     999             : }
    1000             : 
    1001             : GEN
    1002        5670 : ZpX_ZpXQ_liftroot_ea(GEN P, GEN S, GEN T, GEN p, long n, void *E,
    1003             :                      GEN early(void *E, GEN x, GEN q))
    1004             : {
    1005        5670 :   pari_sp ltop = avma, av;
    1006             :   long N, r;
    1007             :   long mask;
    1008             :   GEN q2, q, W, Q, Tq2, Tq, Pq;
    1009             :   pari_timer ti;
    1010        5670 :   T = FpX_get_red(T, powiu(p, n));
    1011        5670 :   if (n == 1) return gcopy(S);
    1012        5670 :   mask = quadratic_prec_mask(n);
    1013        5670 :   av = avma;
    1014        5670 :   q2 = p; q = sqri(p); mask >>= 1; N = 2;
    1015        5670 :   if (DEBUGLEVEL > 3) timer_start(&ti);
    1016        5670 :   Tq = FpXT_red(T,q);
    1017        5670 :   Tq2 = FpXT_red(Tq,q2);
    1018        5670 :   Pq = FpX_red(P,q);
    1019        5670 :   W = FpXQ_inv(FpX_FpXQ_eval(FpX_deriv(P,q2), S, Tq2, q2), Tq2, q2);
    1020        5670 :   Q  = ZX_Z_divexact(FpX_FpXQ_eval(Pq, S, Tq, q), q2);
    1021        5670 :   r = brent_kung_optpow(degpol(P), 4, 3);
    1022        5670 :   if (DEBUGLEVEL > 3)
    1023           0 :     err_printf("ZpX_ZpXQ_liftroot: lifting to prec %ld\n",n);
    1024             :   for (;;)
    1025       12657 :   {
    1026             :     GEN H, Sq, Wq, Spow, dP, qq, Pqq, Tqq;
    1027       18327 :     H  = FpXQ_mul(W, Q, Tq2, q2);
    1028       18327 :     Sq = FpX_sub(S, ZX_Z_mul(H, q2), q);
    1029       18327 :     if (DEBUGLEVEL > 3)
    1030           0 :       timer_printf(&ti,"ZpX_ZpXQ_liftroot: reaching prec %ld",N);
    1031       18327 :     if (mask==1)
    1032        3536 :       return gerepileupto(ltop, Sq);
    1033       14791 :     if (early)
    1034             :     {
    1035       13265 :       GEN Se = early(E, Sq, q);
    1036       13265 :       if (Se) return gerepileupto(ltop, Se);
    1037             :     }
    1038       12657 :     qq = sqri(q); N <<= 1;
    1039       12657 :     if (mask&1UL) { qq = diviiexact(qq, p); N--; }
    1040       12657 :     mask >>= 1;
    1041       12657 :     Pqq  = FpX_red(P, qq);
    1042       12657 :     Tqq  = FpXT_red(T, qq);
    1043       12657 :     Spow = FpXQ_powers(Sq, r, Tqq, qq);
    1044       12657 :     Q  = ZX_Z_divexact(FpX_FpXQV_eval(Pqq, Spow, Tqq, qq), q);
    1045       12657 :     dP = FpX_FpXQV_eval(FpX_deriv(Pq, q), FpXV_red(Spow, q), Tq, q);
    1046       12657 :     Wq = ZX_Z_divexact(FpX_Fp_sub(FpXQ_mul(W, dP, Tq, q), gen_1, q), q2);
    1047       12657 :     Wq = ZX_Z_mul(FpXQ_mul(W, Wq, Tq2, q2), q2);
    1048       12657 :     Wq = FpX_sub(W, Wq, q);
    1049       12657 :     S = Sq; W = Wq; q2 = q; q = qq; Tq2 = Tq; Tq = Tqq; Pq = Pqq;
    1050       12657 :     if (gc_needed(av, 1))
    1051             :     {
    1052           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpX_ZpXQ_Newton");
    1053           0 :       gerepileall(av, 8, &S, &W, &Q, &Tq2, &Tq, &Pq, &q, &q2);
    1054             :     }
    1055             :   }
    1056             : }
    1057             : 
    1058             : GEN
    1059        1694 : ZpX_ZpXQ_liftroot(GEN P, GEN S, GEN T, GEN p, long n)
    1060             : {
    1061        1694 :   return ZpX_ZpXQ_liftroot_ea(P, S, T, p, n, NULL, NULL);
    1062             : }
    1063             : 
    1064             : GEN
    1065         301 : ZpX_Frobenius(GEN T, GEN p, long e)
    1066             : {
    1067         301 :   return ZpX_ZpXQ_liftroot(get_FpX_mod(T), FpX_Frobenius(T, p), T, p, e);
    1068             : }
    1069             : 
    1070             : GEN
    1071         147 : ZpXQM_prodFrobenius(GEN M, GEN T, GEN p, long e)
    1072             : {
    1073         147 :   pari_sp av = avma;
    1074         147 :   GEN xp = ZpX_Frobenius(T, p, e);
    1075         147 :   GEN z = FpXQM_autsum(mkvec2(xp, M), get_FpX_degree(T), T, powiu(p,e));
    1076         147 :   return gerepilecopy(av, gel(z,2));
    1077             : }
    1078             : 
    1079             : /* Canonical lift of polynomial */
    1080             : 
    1081       11130 : static GEN _can_invl(void *E, GEN V) {(void) E; return V; }
    1082             : 
    1083        3654 : static GEN _can_lin(void *E, GEN F, GEN V, GEN q)
    1084             : {
    1085        3654 :   GEN v = RgX_splitting(V, 3);
    1086             :   (void) E;
    1087        3654 :   return FpX_sub(V,ZXV_dotproduct(v, F), q);
    1088             : }
    1089             : 
    1090             : static GEN
    1091        7476 : _can_iter(void *E, GEN f, GEN q)
    1092             : {
    1093        7476 :   GEN h = RgX_splitting(f,3);
    1094        7476 :   GEN h1s = ZX_sqr(gel(h,1)), h2s = ZX_sqr(gel(h,2)), h3s = ZX_sqr(gel(h,3));
    1095        7476 :   GEN h12 = ZX_mul(gel(h,1), gel(h,2));
    1096        7476 :   GEN h13 = ZX_mul(gel(h,1), gel(h,3));
    1097        7476 :   GEN h23 = ZX_mul(gel(h,2), gel(h,3));
    1098        7476 :   GEN h1c = ZX_mul(gel(h,1), h1s);
    1099        7476 :   GEN h3c = ZX_mul(gel(h,3), h3s);
    1100        7476 :   GEN th = ZX_mul(ZX_sub(h2s,ZX_mulu(h13,3)),gel(h,2));
    1101        7476 :   GEN y = FpX_sub(f,ZX_add(RgX_shift_shallow(h3c,2),ZX_add(RgX_shift_shallow(th,1),h1c)),q);
    1102             :   (void) E;
    1103        7476 :   return mkvecn(7,y,h1s,h2s,h3s,h12,h13,h23);
    1104             : }
    1105             : 
    1106             : static GEN
    1107        7476 : _can_invd(void *E, GEN V, GEN v, GEN qM, long M)
    1108             : {
    1109        7476 :   GEN h1s=gel(v,2), h2s=gel(v,3), h3s=gel(v,4);
    1110        7476 :   GEN h12=gel(v,5), h13=gel(v,6), h23=gel(v,7);
    1111        7476 :   GEN F = mkvec3(ZX_sub(h1s,RgX_shift_shallow(h23,1)),RgX_shift_shallow(ZX_sub(h2s,h13),1),
    1112             :                  ZX_sub(RgX_shift_shallow(h3s,2),RgX_shift_shallow(h12,1)));
    1113             :   (void)E;
    1114        7476 :   return gen_ZpX_Dixon(ZXV_Z_mul(F, utoi(3)), V, qM, utoi(3), M, NULL,
    1115             :                                                  _can_lin, _can_invl);
    1116             : }
    1117             : 
    1118             : static GEN
    1119        3717 : F3x_frobeniuslift(GEN P, long n)
    1120        3717 : { return gen_ZpX_Newton(Flx_to_ZX(P),utoi(3), n, NULL, _can_iter, _can_invd); }
    1121             : 
    1122       29736 : static GEN _can5_invl(void *E, GEN V) {(void) E; return V; }
    1123             : 
    1124        9107 : static GEN _can5_lin(void *E, GEN F, GEN V, GEN q)
    1125             : {
    1126        9107 :   ulong p = *(ulong*)E;
    1127        9107 :   GEN v = RgX_splitting(V, p);
    1128        9107 :   return FpX_sub(V,ZXV_dotproduct(v, F), q);
    1129             : }
    1130             : 
    1131             : /* P(X,t) -> P(X*t^n,t) mod (t^p-1) */
    1132             : static GEN
    1133       62146 : _shift(GEN P, long n, ulong p, long v)
    1134             : {
    1135       62146 :   long i, l=lg(P);
    1136       62146 :   GEN r = cgetg(l,t_POL); r[1] = P[1];
    1137      483238 :   for(i=2;i<l;i++)
    1138             :   {
    1139      421092 :     long s = n*(i-2)%p;
    1140      421092 :     GEN ci = gel(P,i);
    1141      421092 :     if (typ(ci)==t_INT)
    1142      104979 :       gel(r,i) = monomial(ci, s, v);
    1143             :     else
    1144      316113 :       gel(r,i) = RgX_rotate_shallow(ci, s, p);
    1145             :   }
    1146       62146 :   return FpXX_renormalize(r, l);
    1147             : }
    1148             : 
    1149             : struct _can_mul
    1150             : {
    1151             :   GEN T, q;
    1152             :   ulong p;
    1153             : };
    1154             : 
    1155             : static GEN
    1156       41517 : _can5_mul(void *E, GEN A, GEN B)
    1157             : {
    1158       41517 :   struct _can_mul *d = (struct _can_mul *)E;
    1159       41517 :   GEN a = gel(A,1), b = gel(B,1);
    1160       41517 :   long n = itos(gel(A,2));
    1161       41517 :   GEN bn = _shift(b, n, d->p, get_FpX_var(d->T));
    1162       41517 :   GEN c = FpXQX_mul(a, bn, d->T, d->q);
    1163       41517 :   return mkvec2(c, addii(gel(A,2), gel(B,2)));
    1164             : }
    1165             : 
    1166             : static GEN
    1167       41349 : _can5_sqr(void *E, GEN A)
    1168             : {
    1169       41349 :   return _can5_mul(E,A,A);
    1170             : }
    1171             : 
    1172             : static GEN
    1173       20629 : _can5_iter(void *E, GEN f, GEN q)
    1174             : {
    1175       20629 :   pari_sp av = avma;
    1176             :   struct _can_mul D;
    1177       20629 :   ulong p = *(ulong*)E;
    1178       20629 :   long i, vT = fetch_var();
    1179             :   GEN N, P, d, V, fs;
    1180       20629 :   D.q = q; D.T = ZX_Z_sub(pol_xn(p,vT),gen_1);
    1181       20629 :   D.p = p;
    1182       20629 :   fs = mkvec2(_shift(f, 1, p, vT), gen_1);
    1183       20629 :   N = gel(gen_powu_i(fs,p-1,(void*)&D,_can5_sqr,_can5_mul),1);
    1184       20629 :   N = ZXX_evalx0(FpXQX_red(N,polcyclo(p,vT),q));
    1185       20629 :   P = FpX_mul(N,f,q);
    1186       20629 :   P = RgX_deflate(P, p);
    1187       20629 :   d = RgX_splitting(N, p);
    1188       20629 :   V = cgetg(p+1,t_VEC);
    1189       20629 :   gel(V,1) = ZX_mulu(gel(d,1), p);
    1190      103915 :   for(i=2; i<= (long)p; i++)
    1191       83286 :     gel(V,i) = ZX_mulu(RgX_shift_shallow(gel(d,p+2-i), 1), p);
    1192       20629 :   (void)delete_var(); return gerepilecopy(av, mkvec2(ZX_sub(f,P),V));
    1193             : }
    1194             : 
    1195             : static GEN
    1196       20629 : _can5_invd(void *E, GEN H, GEN v, GEN qM, long M)
    1197             : {
    1198       20629 :   ulong p = *(long*)E;
    1199       20629 :   return gen_ZpX_Dixon(gel(v,2), H, qM, utoi(p), M, E, _can5_lin, _can5_invl);
    1200             : }
    1201             : 
    1202             : GEN
    1203       13958 : Flx_Teichmuller(GEN P, ulong p, long n)
    1204             : {
    1205       24199 :   return p==3 ? F3x_frobeniuslift(P,n):
    1206       10241 :          gen_ZpX_Newton(Flx_to_ZX(P),utoi(p), n, &p, _can5_iter, _can5_invd);
    1207             : }
    1208             : 
    1209             : GEN
    1210          35 : polteichmuller(GEN P, ulong p, long n)
    1211             : {
    1212          35 :   pari_sp av = avma;
    1213          35 :   GEN q = NULL;
    1214          35 :   if (typ(P)!=t_POL || !RgX_is_FpX(P,&q))
    1215           0 :     pari_err_TYPE("polteichmuller",P);
    1216          35 :   if (q && !equaliu(q,p))
    1217           0 :     pari_err_MODULUS("polteichmuller",q,utoi(p));
    1218          35 :   if (n <= 0)
    1219           0 :     pari_err_DOMAIN("polteichmuller", "precision", "<=",gen_0,stoi(n));
    1220          63 :   return gerepileupto(av, p==2 ? F2x_Teichmuller(RgX_to_F2x(P), n)
    1221          28 :                                : Flx_Teichmuller(RgX_to_Flx(P, p), p, n));
    1222             : }

Generated by: LCOV version 1.13