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 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.0 lcov report (development 22872-edcf83abb) Lines: 535 555 96.4 %
Date: 2018-07-20 05:36:03 Functions: 47 48 97.9 %
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       54394 : BuildTree(GEN link, GEN V, GEN W, GEN a, GEN T, GEN p)
      36             : {
      37       54394 :   long k = lg(a)-1;
      38             :   long i, j, s, minp, mind;
      39             : 
      40       54394 :   for (i=1; i<=k; i++) { gel(V,i) = gel(a,i); link[i] = -i; }
      41             : 
      42       86994 :   for (j=1; j <= 2*k-5; j+=2,i++)
      43             :   {
      44       32600 :     minp = j;
      45       32600 :     mind = degpol(gel(V,j));
      46      425682 :     for (s=j+1; s<i; s++)
      47      393082 :       if (degpol(gel(V,s)) < mind) { minp = s; mind = degpol(gel(V,s)); }
      48             : 
      49       32600 :     swap(gel(V,j), gel(V,minp));
      50       32600 :     lswap(link[j], link[minp]);
      51             : 
      52       32600 :     minp = j+1;
      53       32600 :     mind = degpol(gel(V,j+1));
      54      393082 :     for (s=j+2; s<i; s++)
      55      360482 :       if (degpol(gel(V,s)) < mind) { minp = s; mind = degpol(gel(V,s)); }
      56             : 
      57       32600 :     swap(gel(V,j+1), gel(V,minp));
      58       32600 :     lswap(link[j+1], link[minp]);
      59             : 
      60       32600 :     gel(V,i) = FqX_mul(gel(V,j), gel(V,j+1), T, p);
      61       32600 :     link[i] = j;
      62             :   }
      63             : 
      64      141381 :   for (j=1; j <= 2*k-3; j+=2)
      65             :   {
      66             :     GEN d, u, v;
      67       86994 :     d = FqX_extgcd(gel(V,j), gel(V,j+1), T, p, &u, &v);
      68       86994 :     if (degpol(d) > 0) pari_err_COPRIME("BuildTree", gel(V,j), gel(V,j+1));
      69       86987 :     d = gel(d,2);
      70       86987 :     if (!gequal1(d))
      71             :     {
      72       79498 :       if (typ(d)==t_POL)
      73             :       {
      74        5383 :         d = FpXQ_inv(d, T, p);
      75        5383 :         u = FqX_Fq_mul(u, d, T, p);
      76        5383 :         v = FqX_Fq_mul(v, d, T, p);
      77             :       }
      78             :       else
      79             :       {
      80       74115 :         d = Fp_inv(d, p);
      81       74115 :         u = FqX_Fp_mul(u, d, T,p);
      82       74115 :         v = FqX_Fp_mul(v, d, T,p);
      83             :       }
      84             :     }
      85       86987 :     gel(W,j) = u;
      86       86987 :     gel(W,j+1) = v;
      87             :   }
      88       54387 : }
      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      373088 : ZpX_HenselLift(GEN V, GEN W, long j, GEN f, GEN pd, GEN p0, GEN p1, int noinv)
      94             : {
      95      373088 :   pari_sp av = avma;
      96      373088 :   long space = lg(f) * lgefint(p1);
      97             :   GEN a2, b2, g, z, s, t;
      98      373088 :   GEN a = gel(V,j), b = gel(V,j+1);
      99      373088 :   GEN u = gel(W,j), v = gel(W,j+1);
     100             : 
     101      373088 :   (void)new_chunk(space); /* HACK */
     102      373088 :   g = ZX_sub(f, ZX_mul(a,b));
     103      373088 :   g = ZX_Z_divexact(g, p0);
     104      373088 :   g = FpX_red(g, pd);
     105      373088 :   z = FpX_mul(v,g, pd);
     106      373088 :   t = FpX_divrem(z,a, pd, &s);
     107      373088 :   t = ZX_add(ZX_mul(u,g), ZX_mul(t,b));
     108      373088 :   t = FpX_red(t, pd);
     109      373088 :   t = ZX_Z_mul(t,p0);
     110      373088 :   s = ZX_Z_mul(s,p0);
     111      373088 :   avma = av;
     112      373088 :   a2 = ZX_add(a,s);
     113      373088 :   b2 = ZX_add(b,t);
     114             : 
     115             :   /* already reduced mod p1 = pd p0 */
     116      373088 :   gel(V,j)   = a2;
     117      373088 :   gel(V,j+1) = b2;
     118      373088 :   if (noinv) return;
     119             : 
     120      294396 :   av = avma;
     121      294396 :   (void)new_chunk(space); /* HACK */
     122      294396 :   g = ZX_add(ZX_mul(u,a2), ZX_mul(v,b2));
     123      294396 :   g = Z_ZX_sub(gen_1, g);
     124      294396 :   g = ZX_Z_divexact(g, p0);
     125      294396 :   g = FpX_red(g, pd);
     126      294396 :   z = FpX_mul(v,g, pd);
     127      294396 :   t = FpX_divrem(z,a, pd, &s);
     128      294396 :   t = ZX_add(ZX_mul(u,g), ZX_mul(t,b));
     129      294396 :   t = FpX_red(t, pd);
     130      294396 :   t = ZX_Z_mul(t,p0);
     131      294396 :   s = ZX_Z_mul(s,p0);
     132      294396 :   avma = av;
     133      294396 :   gel(W,j)   = ZX_add(u,t);
     134      294396 :   gel(W,j+1) = ZX_add(v,s);
     135             : }
     136             : 
     137             : static void
     138       23009 : ZpXQ_HenselLift(GEN V, GEN W, long j, GEN f, GEN Td, GEN T1, GEN pd, GEN p0, GEN p1, int noinv)
     139             : {
     140       23009 :   pari_sp av = avma;
     141       23009 :   const long n = degpol(T1), vT = varn(T1);
     142       23009 :   long space = lg(f) * lgefint(p1) * lg(T1);
     143             :   GEN a2, b2, g, z, s, t;
     144       23009 :   GEN a = gel(V,j), b = gel(V,j+1);
     145       23009 :   GEN u = gel(W,j), v = gel(W,j+1);
     146             : 
     147       23009 :   (void)new_chunk(space); /* HACK */
     148       23009 :   g = RgX_sub(f, Kronecker_to_ZXX(ZXX_mul_Kronecker(a,b,n), n, vT));
     149       23009 :   g = FpXQX_red(g, T1, p1);
     150       23009 :   g = RgX_Rg_divexact(g, p0);
     151       23009 :   z = FpXQX_mul(v,g, Td,pd);
     152       23009 :   t = FpXQX_divrem(z,a, Td,pd, &s);
     153       23009 :   t = ZX_add(ZXX_mul_Kronecker(u,g,n), ZXX_mul_Kronecker(t,b,n));
     154       23009 :   t = Kronecker_to_ZXX(t, n, vT);
     155       23009 :   t = FpXQX_red(t, Td, pd);
     156       23009 :   t = RgX_Rg_mul(t,p0);
     157       23009 :   s = RgX_Rg_mul(s,p0);
     158       23009 :   avma = av;
     159             : 
     160       23009 :   a2 = RgX_add(a,s);
     161       23009 :   b2 = RgX_add(b,t);
     162             :   /* already reduced mod p1 = pd p0 */
     163       23009 :   gel(V,j)   = a2;
     164       23009 :   gel(V,j+1) = b2;
     165       23009 :   if (noinv) return;
     166             : 
     167       17171 :   av = avma;
     168       17171 :   (void)new_chunk(space); /* HACK */
     169       17171 :   g = ZX_add(ZXX_mul_Kronecker(u,a2,n), ZXX_mul_Kronecker(v,b2,n));
     170       17171 :   g = Kronecker_to_ZXX(g, n, vT);
     171       17171 :   g = Rg_RgX_sub(gen_1, g);
     172       17171 :   g = FpXQX_red(g, T1, p1);
     173       17171 :   g = RgX_Rg_divexact(g, p0);
     174       17171 :   z = FpXQX_mul(v,g, Td,pd);
     175       17171 :   t = FpXQX_divrem(z,a, Td,pd, &s);
     176       17171 :   t = ZX_add(ZXX_mul_Kronecker(u,g,n), ZXX_mul_Kronecker(t,b,n));
     177       17171 :   t = Kronecker_to_ZXX(t, n, vT);
     178       17171 :   t = FpXQX_red(t, Td, pd);
     179       17171 :   t = RgX_Rg_mul(t,p0);
     180       17171 :   s = RgX_Rg_mul(s,p0);
     181       17171 :   avma = av;
     182       17171 :   gel(W,j)   = RgX_add(u,t);
     183       17171 :   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      960100 : ZpX_RecTreeLift(GEN link, GEN v, GEN w, GEN pd, GEN p0, GEN p1,
     190             :                 GEN f, long j, int noinv)
     191             : {
     192      960100 :   if (j < 0) return;
     193      373088 :   ZpX_HenselLift(v, w, j, f, pd, p0,p1, noinv);
     194      373088 :   ZpX_RecTreeLift(link, v, w, pd, p0,p1, gel(v,j)  , link[j  ], noinv);
     195      373088 :   ZpX_RecTreeLift(link, v, w, pd, p0,p1, gel(v,j+1), link[j+1], noinv);
     196             : }
     197             : static void
     198       49329 : 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       49329 :   if (j < 0) return;
     202       23009 :   ZpXQ_HenselLift(v, w, j, f, Td,T1, pd, p0,p1, noinv);
     203       23009 :   ZpXQ_RecTreeLift(link, v, w, Td,T1, pd, p0,p1, gel(v,j)  , link[j  ], noinv);
     204       23009 :   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      862374 : quadratic_prec_mask(long n)
     225             : {
     226      862374 :   long a = n, i;
     227      862374 :   ulong mask = 0;
     228     3668762 :   for(i = 1;; i++, mask <<= 1)
     229             :   {
     230     6475150 :     mask |= (a&1); a = (a+1)>>1;
     231     4531136 :     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       54422 : MultiLift(GEN f, GEN a, GEN T, GEN p, long e0, long flag)
     242             : {
     243       54422 :   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       54422 :   if (k < 2) pari_err_DOMAIN("MultiLift", "#(modular factors)", "<", gen_2,a);
     249       54422 :   if (e0 < 1) pari_err_DOMAIN("MultiLift", "precision", "<", gen_1,stoi(e0));
     250       54422 :   if (e0 == 1) return a;
     251             : 
     252       54394 :   if (DEBUGLEVEL > 3) timer_start(&Ti);
     253       54394 :   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       54394 :     e = 1;
     263       54394 :     v = cgetg(2*k-2 + 1, t_VEC);
     264       54394 :     w = cgetg(2*k-2 + 1, t_VEC);
     265       54394 :     link=cgetg(2*k-2 + 1, t_VECSMALL);
     266       54394 :     BuildTree(link, v, w, a, T? FpX_red(T,p): NULL, p);
     267       54387 :     if (DEBUGLEVEL > 3) timer_printf(&Ti, "building tree");
     268             :   }
     269       54387 :   mask = quadratic_prec_mask(e0);
     270       54387 :   eold = 1;
     271       54387 :   penew = NULL;
     272       54387 :   Tnew = NULL;
     273       54387 :   if (DEBUGLEVEL > 3) err_printf("lifting to prec %ld\n", e0);
     274      326009 :   while (mask > 1)
     275             :   {
     276      217235 :     long enew = eold << 1;
     277      217235 :     if (mask & 1) enew--;
     278      217235 :     mask >>= 1;
     279      217235 :     if (enew >= e) { /* mask == 1: last iteration */
     280      217235 :       GEN peold = penew? penew: powiu(p, eold);
     281      217235 :       GEN Td = NULL, pd;
     282      217235 :       long d = enew - eold; /* = eold or eold-1 */
     283             :       /* lift from p^eold to p^enew */
     284      217235 :       pd = (d == eold)? peold: diviiexact(peold, p); /* p^d */
     285      217235 :       penew = mulii(peold,pd);
     286      217235 :       if (T) {
     287        3311 :         if (Tnew)
     288        2359 :           Td = (d == eold)? Tnew: FpX_red(Tnew,pd);
     289             :         else
     290         952 :           Td = FpX_red(T, peold);
     291        3311 :         Tnew = FpX_red(T, penew);
     292        3311 :         ZpXQ_RecTreeLift(link, v, w, Td, Tnew, pd, peold, penew, f, lg(v)-2,
     293             :                          (flag == 0 && mask == 1));
     294             :       }
     295             :       else
     296      213924 :         ZpX_RecTreeLift(link, v, w, pd, peold, penew, f, lg(v)-2,
     297             :                         (flag == 0 && mask == 1));
     298      217235 :       if (DEBUGLEVEL > 3) timer_printf(&Ti, "reaching prec %ld", enew);
     299             :     }
     300      217235 :     eold = enew;
     301             :   }
     302             : 
     303       54387 :   if (flag)
     304         770 :     E = mkvec4(utoipos(e0), link, v, w);
     305             :   else
     306             :   {
     307       53617 :     E = cgetg(k+1, t_VEC);
     308      222677 :     for (i = 1; i <= 2*k-2; i++)
     309             :     {
     310      169060 :       long t = link[i];
     311      169060 :       if (t < 0) gel(E,-t) = gel(v,i);
     312             :     }
     313             :   }
     314       54387 :   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       92950 : ZpX_liftfact(GEN pol, GEN Q, GEN pe, GEN p, long e)
     321             : {
     322       92950 :   pari_sp av = avma;
     323       92950 :   pol = FpX_normalize(pol, pe);
     324       92950 :   if (lg(Q) == 2) return mkvec(pol);
     325       52693 :   return gerepilecopy(av, MultiLift(pol, Q, NULL, p, e, 0));
     326             : }
     327             : 
     328             : GEN
     329         959 : ZpXQX_liftfact(GEN pol, GEN Q, GEN T, GEN pe, GEN p, long e)
     330             : {
     331         959 :   pari_sp av = avma;
     332         959 :   pol = FpXQX_normalize(pol, T, pe);
     333         959 :   if (lg(Q) == 2) return mkvec(pol);
     334         959 :   return gerepilecopy(av, MultiLift(pol, Q, T, p, e, 0));
     335             : }
     336             : 
     337             : GEN
     338        1925 : ZqX_liftfact(GEN f, GEN a, GEN T, GEN pe, GEN p, long e)
     339        1925 : { 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             : 
     345             : /* U = NULL treated as 1 */
     346             : static void
     347        5684 : BezoutPropagate(GEN link, GEN v, GEN w, GEN pe, GEN U, GEN f, long j)
     348             : {
     349             :   GEN Q, R;
     350        5684 :   if (j < 0) return;
     351             : 
     352        2457 :   Q = FpX_mul(gel(v,j), gel(w,j), pe);
     353        2457 :   if (U)
     354             :   {
     355        1687 :     Q = FpXQ_mul(Q, U, f, pe);
     356        1687 :     R = FpX_sub(U, Q, pe);
     357             :   }
     358             :   else
     359         770 :     R = Fp_FpX_sub(gen_1, Q, pe);
     360        2457 :   gel(w,j+1) = Q; /*  0 mod U v[j],  1 mod (1-U) v[j+1] */
     361        2457 :   gel(w,j) = R; /*  1 mod U v[j],  0 mod (1-U) v[j+1] */
     362        2457 :   BezoutPropagate(link, v, w, pe, R, f, link[j  ]);
     363        2457 :   BezoutPropagate(link, v, w, pe, Q, f, link[j+1]);
     364             : }
     365             : 
     366             : /* as above, but return the Bezout coefficients for the lifted modular factors
     367             :  *   U[i] = 1 mod Qlift[i]
     368             :  *          0 mod Qlift[j], j != i */
     369             : GEN
     370         777 : bezout_lift_fact(GEN pol, GEN Q, GEN p, long e)
     371             : {
     372         777 :   pari_sp av = avma;
     373             :   GEN E, link, v, w, pe;
     374         777 :   long i, k = lg(Q)-1;
     375         777 :   if (k == 1) return mkvec(pol);
     376         770 :   pe = powiu(p, e);
     377         770 :   pol = FpX_normalize(pol, pe);
     378         770 :   E = MultiLift(pol, Q, NULL, p, e, 1);
     379         770 :   link = gel(E,2);
     380         770 :   v    = gel(E,3);
     381         770 :   w    = gel(E,4);
     382         770 :   BezoutPropagate(link, v, w, pe, NULL, pol, lg(v)-2);
     383         770 :   E = cgetg(k+1, t_VEC);
     384        5684 :   for (i = 1; i <= 2*k-2; i++)
     385             :   {
     386        4914 :     long t = link[i];
     387        4914 :     if (t < 0) E[-t] = w[i];
     388             :   }
     389         770 :   return gerepilecopy(av, E);
     390             : }
     391             : 
     392             : /* Front-end for ZpX_liftfact:
     393             :    lift the factorization of pol mod p given by L to p^N (if possible) */
     394             : GEN
     395          28 : polhensellift(GEN pol, GEN L, GEN p, long N)
     396             : {
     397          28 :   GEN T = NULL;
     398             :   long i, l, t;
     399          28 :   pari_sp av = avma;
     400             :   void (*chk)(GEN, const char*);
     401             : 
     402          28 :   if (typ(pol) != t_POL) pari_err_TYPE("polhensellift",pol);
     403          28 :   RgX_check_ZXX(pol, "polhensellift");
     404          28 :   if (!is_vec_t(typ(L)) || lg(L) < 3) pari_err_TYPE("polhensellift",L);
     405          28 :   t = typ(p);
     406          28 :   if (t == t_VEC) /* [p, T] */
     407             :   {
     408           7 :     T = gel(p,2);
     409           7 :     if (typ(T) != t_POL) pari_err_TYPE("polhensellift",pol);
     410           7 :     RgX_check_ZX(T, "polhensellift");
     411           7 :     p = gel(p,1); t = typ(p);
     412             :   }
     413          28 :   chk = T? RgX_check_ZXX: RgX_check_ZX;
     414          28 :   if (t != t_INT) pari_err_TYPE("polhensellift",p);
     415          28 :   if (N < 1) pari_err_DOMAIN("polhensellift", "precision", "<", gen_1,stoi(N));
     416             : 
     417          28 :   l = lg(L); L = leafcopy(L);
     418          70 :   for (i = 1; i < l; i++)
     419             :   {
     420          49 :     GEN q = gel(L,i);
     421          49 :     if (typ(q) != t_POL) gel(L,i) = scalar_ZX_shallow(q, varn(pol));
     422          49 :     else chk(q, "polhensellift");
     423             :   }
     424          21 :   return gerepilecopy(av, ZqX_liftfact(pol, L, T, powiu(p,N), p, N));
     425             : }
     426             : 
     427             : static GEN
     428       43738 : FqV_roots_from_deg1(GEN x, GEN T, GEN p)
     429             : {
     430       43738 :   long i,l = lg(x);
     431       43738 :   GEN r = cgetg(l,t_VEC);
     432       43738 :   for (i=1; i<l; i++) { GEN P = gel(x,i); gel(r,i) = Fq_neg(gel(P,2), T, p); }
     433       43738 :   return r;
     434             : }
     435             : 
     436             : static GEN
     437       43703 : ZpX_liftroots_full(GEN f, GEN S, GEN q, GEN p, long e)
     438             : {
     439       43703 :   pari_sp av = avma;
     440       43703 :   GEN y = ZpX_liftfact(f, deg1_from_roots(S, varn(f)), q, p, e);
     441       43703 :   return gerepileupto(av, FqV_roots_from_deg1(y, NULL, q));
     442             : }
     443             : 
     444             : GEN
     445       43675 : ZpX_roots(GEN F, GEN p, long e)
     446             : {
     447       43675 :   pari_sp av = avma;
     448       43675 :   GEN q = powiu(p, e);
     449       43675 :   GEN f = FpX_normalize(F, p);
     450       43675 :   GEN g = FpX_normalize(FpX_split_part(f, p), p);
     451             :   GEN S;
     452       43675 :   if (degpol(g) < degpol(f))
     453             :   {
     454       39999 :     GEN h = FpX_div(f, g, p);
     455       39999 :     F = gel(ZpX_liftfact(F, mkvec2(g, h), q, p, e), 1);
     456             :   }
     457       43675 :   S = FpX_roots(g, p);
     458       43675 :   return gerepileupto(av, ZpX_liftroots_full(F, S, q, p, e));
     459             : }
     460             : 
     461             : static GEN
     462          35 : ZpXQX_liftroots_full(GEN f, GEN S, GEN T, GEN q, GEN p, long e)
     463             : {
     464          35 :   pari_sp av = avma;
     465          35 :   GEN y = ZpXQX_liftfact(f, deg1_from_roots(S, varn(f)), T, q, p, e);
     466          35 :   return gerepileupto(av, FqV_roots_from_deg1(y, T, q));
     467             : }
     468             : 
     469             : GEN
     470          35 : ZpXQX_roots(GEN F, GEN T, GEN p, long e)
     471             : {
     472          35 :   pari_sp av = avma;
     473          35 :   GEN q = powiu(p, e);
     474          35 :   GEN f = FpXQX_normalize(F, T, p);
     475          35 :   GEN g = FpXQX_normalize(FpXQX_split_part(f, T, p), T, p);
     476             :   GEN S;
     477          35 :   if (degpol(g) < degpol(f))
     478             :   {
     479           7 :     GEN h = FpXQX_div(f, g, T, p);
     480           7 :     F = gel(ZpXQX_liftfact(F, mkvec2(g, h), T, q, p, e), 1);
     481             :   }
     482          35 :   S = FpXQX_roots(g, T, p);
     483          35 :   return gerepileupto(av, ZpXQX_liftroots_full(F, S, T, q, p, e));
     484             : }
     485             : 
     486             : GEN
     487        2353 : ZqX_roots(GEN F, GEN T, GEN p, long e)
     488             : {
     489        2353 :   return T ? ZpXQX_roots(F, T, p, e): ZpX_roots(F, p, e);
     490             : }
     491             : 
     492             : /* SPEC:
     493             :  * p is a t_INT > 1, e >= 1
     494             :  * f is a ZX with leading term prime to p.
     495             :  * a is a simple root mod l for all l|p.
     496             :  * Return roots of f mod p^e, as integers (implicitly mod p^e)
     497             :  * STANDARD USE: p is a prime power */
     498             : GEN
     499         406 : ZpX_liftroot(GEN f, GEN a, GEN p, long e)
     500             : {
     501         406 :   pari_sp av = avma;
     502         406 :   GEN q = p, fr, W;
     503             :   ulong mask;
     504             : 
     505         406 :   a = modii(a,q);
     506         406 :   if (e == 1) return a;
     507         406 :   mask = quadratic_prec_mask(e);
     508         406 :   fr = FpX_red(f,q);
     509         406 :   W = Fp_inv(FpX_eval(ZX_deriv(fr), a, q), q); /* 1/f'(a) mod p */
     510             :   for(;;)
     511             :   {
     512        2632 :     q = sqri(q);
     513        1519 :     if (mask & 1) q = diviiexact(q, p);
     514        1519 :     mask >>= 1;
     515        1519 :     fr = FpX_red(f,q);
     516        1519 :     a = Fp_sub(a, Fp_mul(W, FpX_eval(fr, a,q), q), q);
     517        1519 :     if (mask == 1) return gerepileuptoint(av, a);
     518        1113 :     W = Fp_sub(shifti(W,1), Fp_mul(Fp_sqr(W,q), FpX_eval(ZX_deriv(fr),a,q), q), q);
     519             :   }
     520             : }
     521             : 
     522             : GEN
     523          28 : ZpX_liftroots(GEN f, GEN S, GEN p, long e)
     524             : {
     525          28 :   long i, n = lg(S)-1, d = degpol(f);
     526             :   GEN r;
     527          28 :   if (n == d) return ZpX_liftroots_full(f, S, powiu(p, e), p, e);
     528           0 :   r = cgetg(n+1, typ(S));
     529           0 :   for (i=1; i <= n; i++)
     530           0 :     gel(r,i) = ZpX_liftroot(f, gel(S,i), p, e);
     531           0 :   return r;
     532             : }
     533             : 
     534             : GEN
     535          70 : ZpXQX_liftroot_vald(GEN f, GEN a, long v, GEN T, GEN p, long e)
     536             : {
     537          70 :   pari_sp av = avma, av2;
     538          70 :   GEN pv = p, q, W, df, Tq, fr, dfr;
     539             :   ulong mask;
     540          70 :   a = Fq_red(a, T, p);
     541          70 :   if (e <= v+1) return a;
     542          70 :   df = RgX_deriv(f);
     543          70 :   if (v) { pv = powiu(p,v); df = ZXX_Z_divexact(df, pv); }
     544          70 :   mask = quadratic_prec_mask(e-v);
     545          70 :   Tq = FpXT_red(T, p); dfr = FpXQX_red(df, Tq, p);
     546          70 :   W = Fq_inv(FqX_eval(dfr, a, Tq, p), Tq, p); /* 1/f'(a) mod (T,p) */
     547          70 :   q = p;
     548          70 :   av2 = avma;
     549             :   for (;;)
     550          91 :   {
     551             :     GEN u, fa, qv, q2v, q2, Tq2;
     552         161 :     q2 = q; q = sqri(q);
     553         161 :     if (mask & 1) q = diviiexact(q,p);
     554         161 :     mask >>= 1;
     555         161 :     if (v) { qv = mulii(q, pv); q2v = mulii(q2, pv); }
     556         161 :     else { qv = q; q2v = q2; }
     557         161 :     Tq2 = FpXT_red(T, q2v); Tq = FpXT_red(T, qv);
     558         161 :     fr = FpXQX_red(f, Tq, qv);
     559         161 :     fa = FqX_eval(fr, a, Tq, qv);
     560         161 :     fa = typ(fa)==t_INT? diviiexact(fa,q2v): ZX_Z_divexact(fa, q2v);
     561         161 :     a = Fq_sub(a, Fq_mul(Fq_mul(W,fa,Tq2,q2v),q2, Tq,qv), Tq, qv);
     562         161 :     if (mask == 1) return gerepileupto(av, a);
     563          91 :     dfr = FpXQX_red(df, Tq, q);
     564          91 :     u = Fq_sub(Fq_mul(W,FqX_eval(dfr,a,Tq,q),Tq,q),gen_1,Tq,q);
     565          91 :     u = typ(u)==t_INT? diviiexact(u,q2): ZX_Z_divexact(u,q2);
     566          91 :     W = Fq_sub(W, Fq_mul(Fq_mul(u,W,Tq2,q2),q2, Tq,q),Tq,q);
     567          91 :     if (gc_needed(av2,2))
     568             :     {
     569           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpXQX_liftroot, e = %ld", e);
     570           0 :       gerepileall(av2, 3, &a, &W, &q);
     571             :     }
     572             :   }
     573             : }
     574             : 
     575             : GEN
     576          70 : ZpXQX_liftroot(GEN f, GEN a, GEN T, GEN p, long e) { return ZpXQX_liftroot_vald(f,a,0,T,p,e); }
     577             : 
     578             : /* Same as ZpX_liftroot for the polynomial X^n-b*/
     579             : GEN
     580       50213 : Zp_sqrtnlift(GEN b, GEN n, GEN a, GEN p, long e)
     581             : {
     582       50213 :   pari_sp ltop=avma;
     583             :   GEN q, w, n_1;
     584             :   ulong mask;
     585       50213 :   long pis2 = equalii(n, gen_2)? 1: 0;
     586       50213 :   if (e == 1) return icopy(a);
     587       45397 :   n_1 = subiu(n,1);
     588       45397 :   mask = quadratic_prec_mask(e);
     589       45397 :   w = Fp_inv(pis2 ? shifti(a,1): Fp_mul(n,Fp_pow(a,n_1,p), p), p);
     590       45397 :   q = p;
     591             :   for(;;)
     592             :   {
     593      169631 :     q = sqri(q);
     594      107514 :     if (mask & 1) q = diviiexact(q, p);
     595      107514 :     mask >>= 1;
     596      107514 :     if (lgefint(q) == 3 && lgefint(n) == 3)
     597       61965 :     {
     598      106998 :       ulong Q = uel(q,2), N = uel(n,2);
     599      106998 :       ulong A = umodiu(a, Q);
     600      106998 :       ulong B = umodiu(b, Q);
     601      106998 :       ulong W = umodiu(w, Q);
     602             : 
     603      106998 :       A = Fl_sub(A, Fl_mul(W, Fl_sub(Fl_powu(A,N,Q), B, Q), Q), Q);
     604      106998 :       a = utoi(A);
     605      106998 :       if (mask == 1) break;
     606       61965 :       W = Fl_sub(Fl_add(W,W,Q),
     607             :                  Fl_mul(Fl_sqr(W,Q), Fl_mul(N,Fl_powu(A, N-1, Q), Q), Q), Q);
     608       61965 :       w = utoi(W);
     609             :     }
     610             :     else
     611             :     {
     612             :       /* a -= w (a^n - b) */
     613         516 :       a = modii(subii(a, mulii(w, subii(Fp_pow(a,n,q),b))), q);
     614         516 :       if (mask == 1) break;
     615             :       /* w += w - w^2 n a^(n-1)*/
     616         289 :       w = subii(shifti(w,1), Fp_mul(Fp_sqr(w,q),
     617         137 :                            pis2? shifti(a,1): mulii(n,Fp_pow(a,n_1,q)), q));
     618             :     }
     619             :   }
     620       45397 :   return gerepileuptoint(ltop,a);
     621             : }
     622             : 
     623             : 
     624             : /* Same as ZpX_liftroot for the polynomial X^2-b */
     625             : GEN
     626       13139 : Zp_sqrtlift(GEN b, GEN a, GEN p, long e)
     627             : {
     628       13139 :   return Zp_sqrtnlift(b, gen_2, a, p, e);
     629             : }
     630             : 
     631             : GEN
     632        3801 : Zp_sqrt(GEN x, GEN p, long e)
     633             : {
     634             :   pari_sp av;
     635             :   GEN z;
     636        3801 :   if (absequaliu(p,2)) return Z2_sqrt(x,e);
     637        2324 :   av = avma;
     638        2324 :   z = Fp_sqrt(Fp_red(x, p), p);
     639        2324 :   if (!z) return NULL;
     640        2289 :   if (e > 1) z = Zp_sqrtlift(x, z, p, e);
     641        2289 :   return gerepileuptoint(av, z);
     642             : }
     643             : 
     644             : /* Compute (x-1)/(x+1)/p^k */
     645             : static GEN
     646       20867 : ZpXQ_log_to_ath(GEN x, long k, GEN T, GEN p, long e, GEN pe)
     647             : {
     648       20867 :   pari_sp av = avma;
     649       20867 :   long vT = get_FpX_var(T);
     650             :   GEN bn, bdi;
     651       20867 :   GEN bd = ZX_Z_add(x, gen_1);
     652       20867 :   if (absequaliu(p,2)) /*For p=2, we need to simplify by 2*/
     653             :   {
     654        6937 :     bn = ZX_shifti(x,-(k+1));
     655        6937 :     bdi= ZpXQ_invlift(ZX_shifti(bd ,-1), pol_1(vT), T, p, e);
     656             :   }
     657             :   else
     658             :   {
     659       13930 :     bn = ZX_Z_divexact(ZX_Z_sub(x, gen_1),powiu(p,k));
     660       13930 :     bdi= ZpXQ_invlift(bd, scalarpol(Fp_inv(gen_2,p),vT), T, p, e);
     661             :   }
     662       20867 :   return gerepileupto(av, FpXQ_mul(bn, bdi, T, pe));
     663             : }
     664             : 
     665             : /* Assume p odd, a = 1 [p], return log(a) */
     666             : GEN
     667       20867 : ZpXQ_log(GEN a, GEN T, GEN p, long N)
     668             : {
     669       20867 :   pari_sp av = avma;
     670             :   pari_timer ti;
     671       20867 :   long is2 = absequaliu(p,2);
     672       20867 :   ulong pp = is2 ? 0: itou_or_0(p);
     673       20867 :   double lp = is2 ? 1: pp ? log2(pp): expi(p);
     674       20867 :   long k = maxss(1 , (long) .5+pow((double)(N>>1)/(lp*lp), 1./3));
     675             :   GEN ak, s, b, pol;
     676       20867 :   long e = is2 ? N-1: N;
     677       20867 :   long i, l = (e-2)/(2*(k+is2));
     678       20867 :   GEN pe = powiu(p,e);
     679       20867 :   GEN TNk, pNk = powiu(p,N+k);
     680       20867 :   if( DEBUGLEVEL>=3) timer_start(&ti);
     681       20867 :   TNk = FpX_get_red(get_FpX_mod(T), pNk);
     682       20867 :   ak = FpXQ_pow(a, powiu(p,k), TNk, pNk);
     683       20867 :   if( DEBUGLEVEL>=3) timer_printf(&ti,"FpXQ_pow(%ld)",k);
     684       20867 :   b = ZpXQ_log_to_ath(ak, k, T, p, e, pe);
     685       20867 :   if( DEBUGLEVEL>=3) timer_printf(&ti,"ZpXQ_log_to_ath");
     686       20867 :   pol= cgetg(l+3,t_POL);
     687       20867 :   pol[1] = evalsigne(1)|evalvarn(0);
     688       56259 :   for(i=0; i<=l; i++)
     689             :   {
     690             :     GEN g;
     691       35392 :     ulong z = 2*i+1;
     692       35392 :     if (pp)
     693             :     {
     694       26222 :       long w = u_lvalrem(z, pp, &z);
     695       26222 :       g = powuu(pp,2*i*k-w);
     696             :     }
     697        9170 :     else g = powiu(p,2*i*k);
     698       35391 :     gel(pol,i+2) = Fp_div(g, utoi(z),pe);
     699             :   }
     700       20867 :   if( DEBUGLEVEL>=3) timer_printf(&ti,"pol(%ld)",l);
     701       20867 :   s = FpX_FpXQ_eval(pol, FpXQ_sqr(b, T, pe), T,  pe);
     702       20867 :   if( DEBUGLEVEL>=3) timer_printf(&ti,"FpX_FpXQ_eval");
     703       20867 :   s = ZX_shifti(FpXQ_mul(b, s, T, pe), 1);
     704       20867 :   return gerepileupto(av, is2? s: FpX_red(s, pe));
     705             : }
     706             : 
     707             : /***********************************************************************/
     708             : /**                                                                   **/
     709             : /**                 Generic quadratic hensel lift over Zp[X]          **/
     710             : /**                                                                   **/
     711             : /***********************************************************************/
     712             : /* q = p^N */
     713             : 
     714             : GEN
     715      199437 : gen_ZpX_Dixon(GEN F, GEN V, GEN q, GEN p, long N, void *E,
     716             :                             GEN lin(void *E, GEN F, GEN d, GEN q),
     717             :                             GEN invl(void *E, GEN d))
     718             : {
     719      199437 :   pari_sp av = avma;
     720             :   long N2, M;
     721             :   GEN VN2, V2, VM, bil;
     722             :   GEN q2, qM;
     723      199437 :   V = FpX_red(V, q);
     724      199437 :   if (N == 1) return invl(E, V);
     725       45318 :   N2 = (N + 1)>>1; M = N - N2;
     726       45318 :   F = FpXT_red(F, q);
     727       45318 :   qM = powiu(p, M);
     728       45318 :   q2 = M == N2? qM: mulii(qM, p);
     729             :   /* q2 = p^N2, qM = p^M, q = q2 * qM */
     730       45318 :   VN2 = gen_ZpX_Dixon(F, V, q2, p, N2, E, lin, invl);
     731       45318 :   bil = lin(E, F, VN2, q);
     732       45318 :   V2 = ZX_Z_divexact(ZX_sub(V, bil), q2);
     733       45318 :   VM = gen_ZpX_Dixon(F, V2, qM, p, M, E, lin, invl);
     734       45318 :   return gerepileupto(av, FpX_red(ZX_add(VN2, ZX_Z_mul(VM, q2)), q));
     735             : }
     736             : 
     737             : GEN
     738      261723 : gen_ZpX_Newton(GEN x, GEN p, long n, void *E,
     739             :                       GEN eval(void *E, GEN f, GEN q),
     740             :                       GEN invd(void *E, GEN V, GEN v, GEN q, long M))
     741             : {
     742      261723 :   pari_sp ltop = avma, av;
     743      261723 :   long N = 1, N2, M;
     744             :   long mask;
     745      261723 :   GEN q = p;
     746      261723 :   if (n == 1) return gcopy(x);
     747      257915 :   mask = quadratic_prec_mask(n);
     748      257915 :   av = avma;
     749     1163958 :   while (mask > 1)
     750             :   {
     751             :     GEN qM, q2, v, V;
     752      648128 :     N2 = N; N <<= 1;
     753      648128 :     q2 = q;
     754      648128 :     if (mask&1UL) { /* can never happen when q2 = p */
     755      271411 :       N--; M = N2-1;
     756      271411 :       qM = diviiexact(q2,p); /* > 1 */
     757      271409 :       q = mulii(qM,q2);
     758             :     } else {
     759      376717 :       M = N2;
     760      376717 :       qM = q2;
     761      376717 :       q = sqri(q2);
     762             :     }
     763             :     /* q2 = p^N2, qM = p^M, q = p^N = q2 * qM */
     764      648124 :     mask >>= 1;
     765      648124 :     v = eval(E, x, q);
     766      648128 :     V = ZX_Z_divexact(gel(v,1), q2);
     767      648120 :     x = FpX_sub(x, ZX_Z_mul(invd(E, V, v, qM, M), q2), q);
     768      648128 :     if (gc_needed(av, 1))
     769             :     {
     770           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"gen_ZpX_Newton");
     771           0 :       gerepileall(av, 2, &x, &q);
     772             :     }
     773             :   }
     774      257915 :   return gerepileupto(ltop, x);
     775             : }
     776             : 
     777             : struct _ZpXQ_inv
     778             : {
     779             :   GEN T, a, p ,n;
     780             : };
     781             : 
     782             : static GEN
     783      506513 : _inv_invd(void *E, GEN V, GEN v, GEN q, long M/*unused*/)
     784             : {
     785      506513 :   struct _ZpXQ_inv *d = (struct _ZpXQ_inv *) E;
     786      506513 :   GEN Tq = FpXT_red(d->T, q);
     787             :   (void)M;
     788      506511 :   return FpXQ_mul(V, gel(v,2), Tq, q);
     789             : }
     790             : 
     791             : static GEN
     792      506512 : _inv_eval(void *E, GEN x, GEN q)
     793             : {
     794      506512 :   struct _ZpXQ_inv *d = (struct _ZpXQ_inv *) E;
     795      506512 :   GEN Tq = FpXT_red(d->T, q);
     796      506513 :   GEN f = FpX_Fp_sub(FpXQ_mul(x, FpX_red(d->a, q), Tq, q), gen_1, q);
     797      506513 :   return mkvec2(f, x);
     798             : }
     799             : 
     800             : GEN
     801      197120 : ZpXQ_invlift(GEN a, GEN x, GEN T, GEN p, long e)
     802             : {
     803             :   struct _ZpXQ_inv d;
     804      197120 :   d.a = a; d.T = T; d.p = p;
     805      197120 :   return gen_ZpX_Newton(x, p, e, &d, _inv_eval, _inv_invd);
     806             : }
     807             : 
     808             : GEN
     809      176253 : ZpXQ_inv(GEN a, GEN T, GEN p, long e)
     810             : {
     811      176253 :   pari_sp av=avma;
     812             :   GEN ai;
     813      176253 :   if (lgefint(p)==3)
     814             :   {
     815      176225 :     ulong pp = p[2];
     816      176225 :     ai = Flx_to_ZX(Flxq_inv(ZX_to_Flx(a,pp), ZXT_to_FlxT(T, pp), pp));
     817             :   } else
     818          28 :     ai = FpXQ_inv(FpX_red(a,p), FpXT_red(T,p),p);
     819      176253 :   return gerepileupto(av, ZpXQ_invlift(a, ai, T, p, e));
     820             : }
     821             : 
     822             : GEN
     823       31584 : ZpXQ_div(GEN a, GEN b, GEN T, GEN q, GEN p, long e)
     824             : {
     825       31584 :   return FpXQ_mul(a, ZpXQ_inv(b, T, p, e), T, q);
     826             : }
     827             : 
     828             : GEN
     829      142422 : ZpXQX_divrem(GEN x, GEN Sp, GEN T, GEN q, GEN p, long e, GEN *pr)
     830             : {
     831      142422 :   pari_sp av = avma;
     832      142422 :   GEN S = get_FpXQX_mod(Sp);
     833      142422 :   GEN b = leading_coeff(S), bi;
     834             :   GEN S2, Q;
     835      142422 :   if (typ(b)==t_INT) return FpXQX_divrem(x, Sp, T, q, pr);
     836      142422 :   bi = ZpXQ_inv(b, T, p, e);
     837      142422 :   S2 = FqX_Fq_mul_to_monic(S, bi, T, q);
     838      142422 :   Q = FpXQX_divrem(x, S2, T, q, pr);
     839      142422 :   if (pr==ONLY_DIVIDES && !Q) { avma = av; return NULL; }
     840      142422 :   if (pr==ONLY_REM || pr==ONLY_DIVIDES) return gerepileupto(av, Q);
     841      142422 :   Q = FpXQX_FpXQ_mul(Q, bi, T, q);
     842      142422 :   gerepileall(av, 2, &Q, pr);
     843      142422 :   return Q;
     844             : }
     845             : 
     846             : GEN
     847         588 : ZpXQX_digits(GEN x, GEN B, GEN T, GEN q, GEN p, long e)
     848             : {
     849         588 :   pari_sp av = avma;
     850         588 :   GEN b = leading_coeff(B), bi;
     851             :   GEN B2, P, V, W;
     852             :   long i, lV;
     853         588 :   if (typ(b)==t_INT) return FpXQX_digits(x, B, T, q);
     854         588 :   bi = ZpXQ_inv(b, T, p, e);
     855         588 :   B2 = FqX_Fq_mul_to_monic(B, bi, T, q);
     856         588 :   V = FpXQX_digits(x, B2, T, q);
     857         588 :   lV = lg(V)-1;
     858         588 :   P = FpXQ_powers(bi, lV-1, T, q);
     859         588 :   W = cgetg(lV+1, t_VEC);
     860       11081 :   for(i=1; i<=lV; i++)
     861       10493 :     gel(W, i) = FpXQX_FpXQ_mul(gel(V,i), gel(P, i), T, q);
     862         588 :   return gerepileupto(av, W);
     863             : }
     864             : 
     865             : struct _ZpXQ_sqrtn
     866             : {
     867             :   GEN T, a, n, ai;
     868             : };
     869             : 
     870             : static GEN
     871        2037 : _sqrtn_invd(void *E, GEN V, GEN v, GEN q, long M)
     872             : {
     873        2037 :   struct _ZpXQ_sqrtn *d = (struct _ZpXQ_sqrtn *) E;
     874        2037 :   GEN Tq = FpX_red(d->T, q), aiq = FpX_red(d->ai, q);
     875             :   (void)M;
     876        2037 :   return FpXQ_mul(FpXQ_mul(V, gel(v,2), Tq, q), aiq, Tq, q);
     877             : }
     878             : 
     879             : static GEN
     880        2037 : _sqrtn_eval(void *E, GEN x, GEN q)
     881             : {
     882        2037 :   struct _ZpXQ_sqrtn *d = (struct _ZpXQ_sqrtn *) E;
     883        2037 :   GEN Tq = FpX_red(d->T, q);
     884        2037 :   GEN f = FpX_sub(FpXQ_pow(x, d->n, Tq, q), d->a, q);
     885        2037 :   return mkvec2(f, x);
     886             : }
     887             : 
     888             : GEN
     889        1204 : ZpXQ_sqrtnlift(GEN a, GEN n, GEN x, GEN T, GEN p, long e)
     890             : {
     891             :   struct _ZpXQ_sqrtn d;
     892        1204 :   d.a = a; d.T = T; d.n = n;
     893        1204 :   d.ai = ZpXQ_inv(ZX_Z_mul(a, n),T,p,(e+1)>>1);
     894        1204 :   return gen_ZpX_Newton(x, p, e, &d, _sqrtn_eval, _sqrtn_invd);
     895             : }
     896             : 
     897             : static GEN
     898          98 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
     899             : 
     900             : GEN
     901         415 : Zq_sqrtnlift(GEN a, GEN n, GEN x, GEN T, GEN p, long e)
     902             : {
     903          49 :   return T? ZpXQ_sqrtnlift(to_ZX(a,varn(T)), n, to_ZX(x,varn(T)), T, p, e)
     904         464 :           : Zp_sqrtnlift(a, n, x, p, e);
     905             : }
     906             : 
     907             : GEN
     908           0 : ZpXQ_sqrt(GEN a, GEN T, GEN p, long e)
     909             : {
     910           0 :   pari_sp av = avma;
     911           0 :   GEN z = FpXQ_sqrt(FpX_red(a, p), T, p);
     912           0 :   if (!z) return NULL;
     913           0 :   if (e <= 1) return gerepileupto(av, z);
     914           0 :   return gerepileupto(av, ZpXQ_sqrtnlift(a, gen_2, z, T, p, e));
     915             : }
     916             : 
     917             : GEN
     918        4054 : ZpX_ZpXQ_liftroot_ea(GEN P, GEN S, GEN T, GEN p, long n, void *E,
     919             :                      int early(void *E, GEN x, GEN q))
     920             : {
     921        4054 :   pari_sp ltop = avma, av;
     922             :   long N, r;
     923             :   long mask;
     924             :   GEN q2, q, W, Q, Tq2, Tq, Pq;
     925             :   pari_timer ti;
     926        4054 :   T = FpX_get_red(T, powiu(p, n));
     927        4054 :   if (n == 1) return gcopy(S);
     928        4054 :   mask = quadratic_prec_mask(n);
     929        4054 :   av = avma;
     930        4054 :   q2 = p; q = sqri(p); mask >>= 1; N = 2;
     931        4054 :   if (DEBUGLEVEL > 3) timer_start(&ti);
     932        4054 :   Tq = FpXT_red(T,q);
     933        4054 :   Tq2 = FpXT_red(Tq,q2);
     934        4054 :   Pq = FpX_red(P,q);
     935        4054 :   W = FpXQ_inv(FpX_FpXQ_eval(FpX_deriv(P,q2), S, Tq2, q2), Tq2, q2);
     936        4054 :   Q  = ZX_Z_divexact(FpX_FpXQ_eval(Pq, S, Tq, q), q2);
     937        4054 :   r = brent_kung_optpow(degpol(P), 4, 3);
     938        4054 :   if (DEBUGLEVEL > 3)
     939           0 :     err_printf("ZpX_ZpXQ_liftroot: lifting to prec %ld\n",N);
     940             :   for (;;)
     941        7743 :   {
     942             :     GEN H, Sq, Wq, Spow, dP, qq, Pqq, Tqq;
     943       11797 :     H  = FpXQ_mul(W, Q, Tq2, q2);
     944       11797 :     Sq = FpX_sub(S, ZX_Z_mul(H, q2), q);
     945       11797 :     if (DEBUGLEVEL > 3)
     946           0 :       timer_printf(&ti,"ZpX_ZpXQ_liftroot: reaching prec %ld",N);
     947       11797 :     if (mask==1 || (early && early(E, Sq, q)))
     948        4054 :       return gerepileupto(ltop, Sq);
     949        7743 :     qq = sqri(q); N <<= 1;
     950        7743 :     if (mask&1UL) { qq = diviiexact(qq, p); N--; }
     951        7743 :     mask >>= 1;
     952        7743 :     Pqq  = FpX_red(P, qq);
     953        7743 :     Tqq  = FpXT_red(T, qq);
     954        7743 :     Spow = FpXQ_powers(Sq, r, Tqq, qq);
     955        7743 :     Q  = ZX_Z_divexact(FpX_FpXQV_eval(Pqq, Spow, Tqq, qq), q);
     956        7743 :     dP = FpX_FpXQV_eval(FpX_deriv(Pq, q), FpXV_red(Spow, q), Tq, q);
     957        7743 :     Wq = ZX_Z_divexact(FpX_Fp_sub(FpXQ_mul(W, dP, Tq, q), gen_1, q), q2);
     958        7743 :     Wq = ZX_Z_mul(FpXQ_mul(W, Wq, Tq2, q2), q2);
     959        7743 :     Wq = FpX_sub(W, Wq, q);
     960        7743 :     S = Sq; W = Wq; q2 = q; q = qq; Tq2 = Tq; Tq = Tqq; Pq = Pqq;
     961        7743 :     if (gc_needed(av, 1))
     962             :     {
     963           1 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZpX_ZpXQ_Newton");
     964           1 :       gerepileall(av, 8, &S, &W, &Q, &Tq2, &Tq, &Pq, &q, &q2);
     965             :     }
     966             :   }
     967             : }
     968             : 
     969             : GEN
     970        1904 : ZpX_ZpXQ_liftroot(GEN P, GEN S, GEN T, GEN p, long n)
     971             : {
     972        1904 :   return ZpX_ZpXQ_liftroot_ea(P, S, T, p, n, NULL, NULL);
     973             : }
     974             : 
     975             : GEN
     976         287 : ZpX_Frobenius(GEN T, GEN p, long e)
     977             : {
     978         287 :   return ZpX_ZpXQ_liftroot(get_FpX_mod(T), FpX_Frobenius(T, p), T, p, e);
     979             : }
     980             : 
     981             : GEN
     982         140 : ZpXQM_prodFrobenius(GEN M, GEN T, GEN p, long e)
     983             : {
     984         140 :   pari_sp av = avma;
     985         140 :   GEN xp = ZpX_Frobenius(T, p, e);
     986         140 :   GEN z = FpXQM_autsum(mkvec2(xp, M), get_FpX_degree(T), T, powiu(p,e));
     987         140 :   return gerepilecopy(av, gel(z,2));
     988             : }

Generated by: LCOV version 1.13