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

Generated by: LCOV version 1.11