Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - ecpp.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.18.1 lcov report (development 30074-db9b3c0519) Lines: 748 805 92.9 %
Date: 2025-03-13 08:38:27 Functions: 95 98 96.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2014 The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : 
      18             : #define DEBUGLEVEL DEBUGLEVEL_isprime
      19             : 
      20             : #define dbg_mode()  if (DEBUGLEVEL >= 2)
      21             : #define dbg_mode1() if (DEBUGLEVEL >= 3)
      22             : 
      23             : #define ANSI_RED     "\x1b[31m"
      24             : #define ANSI_GREEN   "\x1b[32m"
      25             : #define ANSI_YELLOW  "\x1b[33m"
      26             : #define ANSI_BLUE    "\x1b[34m"
      27             : #define ANSI_MAGENTA "\x1b[35m"
      28             : #define ANSI_CYAN    "\x1b[36m"
      29             : #define ANSI_WHITE   "\x1b[37m"
      30             : 
      31             : #define ANSI_BRIGHT_RED     "\x1b[31;1m"
      32             : #define ANSI_BRIGHT_GREEN   "\x1b[32;1m"
      33             : #define ANSI_BRIGHT_YELLOW  "\x1b[33;1m"
      34             : #define ANSI_BRIGHT_BLUE    "\x1b[34;1m"
      35             : #define ANSI_BRIGHT_MAGENTA "\x1b[35;1m"
      36             : #define ANSI_BRIGHT_CYAN    "\x1b[36;1m"
      37             : #define ANSI_BRIGHT_WHITE   "\x1b[37;1m"
      38             : 
      39             : #define ANSI_RESET   "\x1b[0m"
      40             : 
      41             : /* THINGS THAT DON'T BELONG */
      42             : 
      43             : /* Assume that x is a vector such that
      44             :    f(x) = 1 for x <= k
      45             :    f(x) = 0 otherwise.
      46             :    Return k.
      47             : */
      48             : static long
      49        6612 : zv_binsearch0(void *E, long (*f)(void* E, GEN x), GEN x)
      50             : {
      51        6612 :   long lo = 1;
      52        6612 :   long hi = lg(x)-1;
      53       21774 :   while (lo < hi) {
      54       15162 :     long mi = lo + (hi - lo)/2 + 1;
      55       15162 :     if (f(E,gel(x,mi))) lo = mi;
      56        8760 :     else hi = mi - 1;
      57             :   }
      58        6612 :   if (f(E,gel(x,lo))) return lo;
      59        3282 :   return 0;
      60             : }
      61             : 
      62             : INLINE long
      63           0 : time_record(GEN* X0, const char* Xx, long t)
      64             : {
      65           0 :   long i = Xx[0]-'A'+1, j = Xx[1]-'1'+1;
      66           0 :   umael3(*X0, 1, i, j) += t;
      67           0 :   umael3(*X0, 2, i, j) ++; return t;
      68             : }
      69             : 
      70             : INLINE long
      71           0 : timer_record(GEN* X0, const char* Xx, pari_timer* ti)
      72           0 : { return time_record(X0, Xx, timer_delay(ti)); }
      73             : 
      74             : INLINE long
      75        4596 : FpJ_is_inf(GEN P) { return signe(gel(P, 3)) == 0; }
      76             : 
      77             : /*****************************************************************/
      78             : 
      79             : /* D < 0 fundamental: return the number of units in Q(sqrt(D)) */
      80             : INLINE long
      81    49418154 : D_get_wD(long D)
      82             : {
      83    49418154 :   if (D == -4) return 4;
      84    49416816 :   if (D == -3) return 6;
      85    49415262 :   return 2;
      86             : }
      87             : 
      88             : /* Dinfo contains information related to the discirminant
      89             :  *    D: the discriminant (D < 0)
      90             :  *    h: the class number associated to D
      91             :  *   bi: the "best invariant" for computing polclass(D, bi)
      92             :  *   pd: the degree of polclass; equal to h except when bi is a double
      93             :  *       eta-quotient w_p,q with p|D and q|D, where pd = h/2.
      94             :  * Dfac: the prime factorization of D; we have D = q0 q1* q2* ... qn*
      95             :  *       where q0 = 1, -4, 8, -8, qi* = 1 mod 4 and |qi| is a prime.
      96             :  *       The factorization is a vecsmall listing the indices of the qi* as
      97             :  *       they appear in the primelist (q0 = 1 is omitted) */
      98             : INLINE long
      99    49878714 : Dinfo_get_D(GEN Dinfo) { return gel(Dinfo, 1)[1]; }
     100             : INLINE long
     101    25769430 : Dinfo_get_h(GEN Dinfo) { return gel(Dinfo, 1)[2]; }
     102             : INLINE long
     103    19503138 : Dinfo_get_bi(GEN Dinfo) { return gel(Dinfo, 1)[3]; }
     104             : INLINE ulong
     105    49542642 : Dinfo_get_pd(GEN Dinfo) { return umael(Dinfo, 1, 4); }
     106             : INLINE GEN
     107    27862404 : Dinfo_get_Dfac(GEN Dinfo) { return gel(Dinfo, 2); }
     108             : 
     109             : /* primelist and indexlist
     110             :  *
     111             :  * primelist begins with 8, -4, -8; subsequent elements are the p^* for
     112             :  * successive odd primes <= maxsqrt, by increasing absolute value
     113             :  *
     114             :  *        i |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |  10 |  11 |
     115             :  * ---------+----+----+----+----+----+----+----+----+----+-----+-----+
     116             :  *        i |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |  10 |  11 |
     117             :  * Plist[i] |  8 | -4 | -8 | -3 |  5 | -7 |-11 | 13 | 17 | -19 | -23 |
     118             :  *        p |  3 |  5 |  7 |  9 | 11 | 13 | 15 | 17 | 19 |  21 |  23 |
     119             :  * ---------+----+----+----+----+----+----+----+----+----+-----+-----+*/
     120             : 
     121             : /* primelist containing primes whose absolute value is at most maxsqrt */
     122             : static GEN
     123          66 : ecpp_primelist_init(long maxsqrt)
     124             : {
     125          66 :   GEN P = cgetg(maxsqrt, t_VECSMALL);
     126             :   forprime_t T;
     127          66 :   long p, j = 4;
     128          66 :   u_forprime_init(&T, 3, maxsqrt);
     129          66 :   P[1] =  8; P[2] = -4; P[3] = -8;
     130        4470 :   while((p = u_forprime_next(&T))) P[j++] = ((p & 3UL) == 1)? p: -p;
     131          66 :   setlg(P,j); return P;
     132             : }
     133             : 
     134             : static GEN
     135        2190 : Dfac_to_p(GEN x, GEN P) { pari_APPLY_long(uel(P,x[i])); }
     136             : static GEN
     137        2190 : Dfac_to_roots(GEN x, GEN P) { pari_APPLY_type(t_VEC, gel(P,x[i])); }
     138             : 
     139             : #if 0
     140             : INLINE ulong
     141             : ecpp_param_get_maxsqrt(GEN param) { return umael3(param, 1, 1, 1); }
     142             : INLINE ulong
     143             : ecpp_param_get_maxdisc(GEN param) { return umael3(param, 1, 1, 2); }
     144             : #endif
     145             : INLINE ulong
     146         960 : ecpp_param_get_maxpcdg(GEN param) { return umael3(param, 1, 1, 3); }
     147             : INLINE GEN
     148         960 : ecpp_param_get_uprimelist(GEN param) { return gmael(param, 1, 2); }
     149             : INLINE GEN
     150       15096 : ecpp_param_get_primelist(GEN param) { return gmael(param, 1, 3); }
     151             : INLINE GEN
     152         960 : ecpp_param_get_disclist(GEN param) { return gmael(param, 1, 4); }
     153             : INLINE GEN
     154       14088 : ecpp_param_get_primeord(GEN param) { return gmael(param, 1, 5); }
     155             : INLINE GEN
     156        3306 : ecpp_param_get_primorial_vec(GEN param) { return gel(param, 2); }
     157             : INLINE GEN
     158        4266 : ecpp_param_get_tune(GEN param) { return gel(param, 3); }
     159             : 
     160             : /*  Input: x, 20 <= x <= 35
     161             :  * Output: a vector whose ith entry is the product of all primes below 2^x */
     162             : static GEN
     163          66 : primorial_vec(ulong x)
     164             : {
     165          66 :   pari_sp av = avma;
     166          66 :   long i, y = x-19;
     167          66 :   GEN v = primes_upto_zv(1UL << x), w = cgetg(y+1, t_VEC);
     168             :   /* ind[i]th prime number is the largest prime <= 2^(20+i) */
     169          66 :   long ind[] = { 0, 82025L, 155611L, 295947L, 564163L, 1077871L, 2063689L,
     170             :                  3957809L, 7603553L, 14630843L, 28192750L, 54400028L,
     171             :                  105097565L, 203280221L, 393615806L, 762939111L, 1480206279L};
     172          66 :   gel(w,1) = zv_prod_Z(vecslice(v,1,ind[1]));
     173         120 :   for (i = 2; i <= y; i++)
     174             :   {
     175          54 :     pari_sp av2 = avma;
     176          54 :     GEN q = mulii(gel(w,i-1), zv_prod_Z(vecslice(v,ind[i-1]+1,ind[i])));
     177          54 :     gel(w,i) = gerepileuptoint(av2, q);
     178             :   }
     179          66 :   return gerepileupto(av, w);
     180             : }
     181             : 
     182             : /* NDmqg contains
     183             :    N, as in the theorem in ??ecpp
     184             :    Dinfo, as discussed in the comment about Dinfo
     185             :    m, as in the theorem in ??ecpp
     186             :    q, as in the theorem in ??ecpp
     187             :    g, a quadratic nonresidue modulo N
     188             :    sqrt, a list of squareroots
     189             : */
     190             : INLINE GEN
     191         906 : NDmqg_get_N(GEN x) { return gel(x,1); }
     192             : INLINE GEN
     193        8388 : NDmqg_get_Dinfo(GEN x) { return gel(x,2); }
     194             : INLINE GEN
     195         906 : NDmqg_get_m(GEN x) { return gel(x,3); }
     196             : INLINE GEN
     197         906 : NDmqg_get_q(GEN x) { return gel(x,4); }
     198             : INLINE GEN
     199         906 : NDmqg_get_g(GEN x) { return gel(x,5); }
     200             : INLINE GEN
     201         906 : NDmqg_get_sqrt(GEN x) { return gel(x,6); }
     202             : 
     203             : /* COMPARATOR FUNCTIONS */
     204             : 
     205             : static int
     206    24704124 : sort_disclist(void *data, GEN x, GEN y)
     207             : {
     208             :   long d1, h1, g1, o1, pd1, hf1, wD1, d2, h2, g2, o2, pd2, hf2, wD2;
     209             :   (void)data;
     210    24704124 :   d1 = Dinfo_get_D(x); wD1 = D_get_wD(d1);
     211    24704124 :   d2 = Dinfo_get_D(y); wD2 = D_get_wD(d2);
     212             :   /* higher number of units means more elliptic curves to try */
     213    24704124 :   if (wD1 != wD2) return wD2 > wD1 ? 1 : -1;
     214             :   /* lower polclass degree is better: faster computation of roots modulo N */
     215    24702648 :   pd1 = Dinfo_get_pd(x); /* degree of polclass */
     216    24702648 :   pd2 = Dinfo_get_pd(y);
     217    24702648 :   if (pd1 != pd2) return pd1 > pd2 ? 1 : -1;
     218    12882276 :   g1 = lg(Dinfo_get_Dfac(x))-1; /* genus number */
     219    12882276 :   h1 = Dinfo_get_h(x); /* class number */
     220    12882276 :   o1 = h1 >> (g1-1); /* odd class number */
     221    12882276 :   g2 = lg(Dinfo_get_Dfac(y))-1;
     222    12882276 :   h2 = Dinfo_get_h(y);
     223    12882276 :   o2 = h2 >> (g2-1);
     224    12882276 :   if (o1 != o2) return g1 > g2 ? 1 : -1;
     225             :   /* lower class number is better: higher probability of succesful cornacchia */
     226    10233216 :   if (h1 != h2) return h1 > h2 ? 1 : -1;
     227             :   /* higher height factor is better: polclass would have lower coefficients */
     228     9750486 :   hf1 = modinv_height_factor(Dinfo_get_bi(x)); /* height factor for best inv. */
     229     9750486 :   hf2 = modinv_height_factor(Dinfo_get_bi(y));
     230     9750486 :   if (hf1 != hf2) return hf2 > hf1 ? 1 : -1;
     231             :   /* "higher" discriminant is better since its absolute value is lower */
     232     4413594 :   if (d1 != d2) return d2 > d1 ? 1 : -1;
     233           0 :   return 0;
     234             : }
     235             : 
     236             : INLINE long
     237        6576 : NDmqg_get_D(GEN x) { return Dinfo_get_D(NDmqg_get_Dinfo(x)); }
     238             : static int
     239        3288 : sort_NDmq_by_D(void *data, GEN x, GEN y)
     240             : {
     241        3288 :   long d1 = NDmqg_get_D(x);
     242        3288 :   long d2 = NDmqg_get_D(y);
     243        3288 :   (void)data; return d2 > d1 ? 1 : -1;
     244             : }
     245             : 
     246             : static int
     247       49182 : sort_Dmq_by_q(void *data, GEN x, GEN y)
     248       49182 : { (void)data; return cmpii(gel(x,3), gel(y,3)); }
     249             : 
     250             : INLINE long
     251           0 : Dmq_get_D(GEN Dmq) { return Dinfo_get_D(gel(Dmq,1)); }
     252             : INLINE long
     253        3972 : Dmq_get_h(GEN Dmq) { return Dinfo_get_h(gel(Dmq,1)); }
     254             : static int
     255        1986 : sort_Dmq_by_cnum(void *data, GEN x, GEN y)
     256             : {
     257        1986 :   ulong h1 = Dmq_get_h(x);
     258        1986 :   ulong h2 = Dmq_get_h(y);
     259        1986 :   if (h1 != h2) return h1 > h2 ? 1 : -1;
     260         792 :   return sort_Dmq_by_q(data, x, y);
     261             : }
     262             : 
     263             : static void
     264         336 : allh_r(GEN H, ulong minD, ulong maxD)
     265             : {
     266         336 :   ulong a, A = usqrt(maxD/3), minD2 = (minD-1) >> 1, maxD2 = maxD >> 1;
     267      135912 :   for (a = 1; a <= A; a++)
     268             :   {
     269      135576 :     ulong a2 = a << 1, aa = a*a, aa4 = aa << 2, b, B;
     270             :     { /* b = 0 */
     271      135576 :       ulong D = aa << 1;
     272      135576 :       if (D <= minD2)
     273      105420 :         D += a2 * ((minD2 - D + a2) / a2);
     274    30661296 :       for (; D <= maxD2; D += a2) H[D]++;
     275             :     }
     276      135576 :     B = aa4 - 1; /* 4a^2 - b^2 */
     277    36028560 :     for (b = 1; b < a; b++)
     278             :     {
     279    35892984 :       ulong D = B >> 1; /* (4a^2 - b^2) / 2 */
     280    35892984 :       B -= (b << 1) + 1; if (D > maxD2) continue;
     281    29539914 :       if (D > minD2)
     282             :       {
     283     2653050 :         H[D]++; D += a2; /* c = a */
     284             :       } else
     285    26886864 :         D += a2 * ((minD2 - D + a2) / a2);
     286  1861955046 :       for (; D <= maxD2; D += a2) H[D] += 2;
     287             :     }
     288             :     { /* b = a */
     289      135576 :       ulong D = (aa4 - aa) >> 1;
     290      135576 :       if (D <= minD2)
     291      121752 :         D += a2 * ((minD2 - D + a2) / a2);
     292    31360068 :       for (; D <= maxD2; D += a2) H[D] ++;
     293             :     }
     294             :   }
     295         336 : }
     296             : 
     297             : /* return H s.t if -maxD <= D < 0 is fundamental then H[(-D)>>1] is the
     298             :  * ordinary class number of Q(sqrt(D)); junk at other entries. */
     299             : 
     300             : static GEN
     301          66 : allh(ulong maxD)
     302             : {
     303          66 :   ulong maxD2 = maxD>>1, s = 1UL<<16;
     304          66 :   GEN H = zero_zv(maxD2);
     305             :   ulong a;
     306         402 :   for (a = 0; a < maxD; a += s)
     307         336 :     allh_r(H, a+1, minss(a+s,maxD));
     308          66 :   return H;
     309             : }
     310             : 
     311             : static GEN
     312     1634934 : mkDinfo(GEN c, long D, long h)
     313             : {
     314             :   long bi, pd, p1, p2;
     315     1634934 :   bi = disc_best_modinv(D);
     316     1634934 :   pd = (modinv_degree(&p1,&p2,bi) && (-D)%p1==0 && (-D)%p2==0)? h/2: h;
     317     1634934 :   return mkvec2(mkvecsmall4(D, h, bi, pd), c);
     318             : }
     319             : 
     320             : static GEN
     321          66 : ecpp_disclist_init(ulong maxdisc, GEN primelist)
     322             : {
     323          66 :   pari_sp av = avma;
     324             :   long t, ip, u, lp, lmerge;
     325          66 :   GEN merge, ev, od, Harr = allh(maxdisc); /* table of class numbers*/
     326          66 :   long lenv = maxdisc/4; /* max length of od/ev */
     327          66 :   long N = maxomegau(maxdisc) + 1;
     328             : 
     329             :   /* od[t] attached to discriminant 1-4*t, ev[t] attached to -4*t */
     330          66 :   od = cgetg(lenv + 1, t_VEC); /* contains 'long' at first: save memory */
     331          66 :   ev = cgetg(lenv + 1, t_VEC);
     332             :   /* first pass: squarefree sieve and restrict to maxsqrt-smooth disc */
     333     4852566 :   for (t = 1; t <= lenv; t++)
     334             :   {
     335     4852500 :     od[t] = 1;
     336     4852500 :     switch(t&7)
     337             :     {
     338     1213122 :       case 0: case 4: ev[t] = 0; break;
     339      606582 :       case 2: ev[t] =-8; break;
     340      606540 :       case 6: ev[t] = 8; break;
     341     2426256 :       default:ev[t] =-4; break;
     342             :     }
     343             :   }
     344          66 :   lp = lg(primelist);
     345        4470 :   for (ip = 4; ip < lp; ip++) /* skip 8,-4,-8 */
     346             :   { /* sieve by squares of primes */
     347        4404 :     long s, q = primelist[ip], p = labs(q);
     348        4404 :     s = (q == p)? 3: 1;
     349     8226780 :     for (t = (s*p+1)>>2; t <= lenv; t += p, s += 4)
     350             :     {
     351     8222376 :       long c = od[t];
     352     8222376 :       if (c) { if (s%p == 0) od[t] = 0; else  od[t] = c*q; }
     353             :     }
     354        4404 :     s = 1;
     355     8224560 :     for (t = p; t <= lenv; t += p, s++)
     356             :     {
     357     8220156 :       long c = ev[t];
     358     8220156 :       if (c) { if (s%p == 0) ev[t] = 0; else  ev[t] = c*q; }
     359             :     }
     360             :   }
     361     4852566 :   for (u = 0, t = 1; t <= lenv; t++)
     362             :   { /* restrict to maxsqrt-smooth disc */
     363     4852500 :     if (od[t] != -4*t+1) od[t] = 0; else u++;
     364     4852500 :     if (ev[t] != -4*t)   ev[t] = 0; else u++;
     365             :   }
     366             : 
     367             :   /* second pass: sieve by primes and record factorization */
     368     4852566 :   for (t = 1; t <= lenv; t++)
     369             :   {
     370     4852500 :     if (od[t]) gel(od,t) = vecsmalltrunc_init(N);
     371     4852500 :     if (ev[t])
     372             :     {
     373      708066 :       GEN F = vecsmalltrunc_init(N);
     374             :       long id;
     375      708066 :       switch(t&7)
     376             :       {
     377      190218 :         case 2: id = 3; break;
     378      190194 :         case 6: id = 1; break;
     379      327654 :         default:id = 2; break;
     380             :       }
     381      708066 :       vecsmalltrunc_append(F, id);
     382      708066 :       gel(ev,t) = F;
     383             :     }
     384             :   }
     385          66 :   lp = lg(primelist);
     386        4470 :   for (ip = 4; ip < lp; ip++) /* skip 8,-4,-8 */
     387             :   {
     388        4404 :     long s, q = primelist[ip], p = labs(q);
     389        4404 :     s = (q == p)? 3: 1;
     390     8226780 :     for (t = (s*p+1)>>2; t <= lenv; t += p, s += 4)
     391             :     {
     392     8222376 :       GEN c = gel(od,t);
     393     8222376 :       if (c) vecsmalltrunc_append(c, ip);
     394             :     }
     395        4404 :     s = 1;
     396     8224560 :     for (t = p; t <= lenv; t += p, s++)
     397             :     {
     398     8220156 :       GEN c = gel(ev,t);
     399     8220156 :       if (c) vecsmalltrunc_append(c, ip);
     400             :     }
     401             :   }
     402             :   /* merging the two arrays */
     403          66 :   merge = cgetg(u+1, t_VEC); lmerge = 0;
     404     4852566 :   for (t = 1; t <= lenv; t++)
     405             :   {
     406             :     GEN c;
     407     4852500 :     c = gel(od,t); if (c) gel(merge, ++lmerge) = mkDinfo(c,1-4*t, Harr[2*t-1]);
     408     4852500 :     c = gel(ev,t); if (c) gel(merge, ++lmerge) = mkDinfo(c, -4*t, Harr[2*t]);
     409             :   }
     410          66 :   setlg(merge, lmerge);
     411          66 :   gen_sort_inplace(merge, NULL, &sort_disclist, NULL);
     412          66 :   return gerepilecopy(av, merge);
     413             : }
     414             : 
     415             : static GEN
     416          66 : ecpp_primeord_init(GEN primelist, GEN disclist)
     417             : {
     418          66 :   long i, k=1, lgdisclist = lg(disclist), lprimelist = lg(primelist);
     419          66 :   GEN primeord = zero_Flv(lprimelist-1);
     420     1634934 :   for (i=1;i < lgdisclist; i++)
     421             :   {
     422     1634868 :     GEN Dinfo = gel(disclist, i), Dfac = Dinfo_get_Dfac(Dinfo);
     423     1634868 :     long j, l = lg(Dfac);
     424     7329120 :     for (j = 1; j < l; j++)
     425             :     {
     426     5694252 :       long ip = Dfac[j];
     427     5694252 :       if (primeord[ip]==0)
     428        4602 :         primeord[ip]=k++;
     429             :     }
     430             :   }
     431          66 :   return perm_inv(primeord);
     432             : }
     433             : 
     434             : /*  Input: a vector tune whose components are [maxsqrt,maxpcdg,tdivexp,expiN]
     435             :  * Output: vector param of precomputations
     436             :  *   let x =  be a component of tune then
     437             :  *   param[1][1] = [maxsqrt, maxdisc, maxpcdg]
     438             :  *   param[1][2] = primelist = Vecsmall of primes
     439             :  *   param[1][3] = disclist  = vector of Dinfos, sorted by quality
     440             :  *   param[2]    = primorial_vec
     441             :  *   param[3]    = tune */
     442             : static GEN
     443          66 : ecpp_param_set(GEN tune, GEN x)
     444             : {
     445             :   pari_timer ti;
     446          66 :   pari_sp av = avma;
     447          66 :   ulong maxsqrt = uel(x,1), maxpcdg = uel(x,2), tdivexp = uel(x,3);
     448          66 :   ulong maxdisc = maxsqrt * maxsqrt;
     449             :   GEN Plist, U, Utree, Dlist, Olist, primorial, T;
     450          66 :   T = mkvecsmall3(maxsqrt, maxdisc, maxpcdg);
     451          66 :   dbg_mode() {
     452           0 :     err_printf(ANSI_BRIGHT_WHITE "\nECPP: parameters %Ps:\n"
     453             :                "init time: " ANSI_RESET, x);
     454           0 :     timer_start(&ti);
     455             :   }
     456          66 :   Plist = ecpp_primelist_init(maxsqrt);
     457          66 :   U = zv_abs(Plist);
     458          66 :   Utree = mkvec2(U, ZV_producttree(U));
     459          66 :   dbg_mode() err_printf("%ld",timer_delay(&ti));
     460          66 :   Dlist = ecpp_disclist_init(maxdisc, Plist);
     461          66 :   dbg_mode() err_printf(", %ld",timer_delay(&ti));
     462          66 :   Olist = ecpp_primeord_init(Plist, Dlist);
     463          66 :   dbg_mode() err_printf(", %ld",timer_delay(&ti));
     464          66 :   primorial = primorial_vec(tdivexp);
     465          66 :   dbg_mode() err_printf(", %ld\n",timer_delay(&ti));
     466          66 :   return gerepilecopy(av, mkvec3(mkvec5(T,Utree,Plist,Dlist,Olist),
     467             :                                  primorial, tune));
     468             : }
     469             : 
     470             : /* cert contains [N, t, s, a4, [x, y]] as documented in ??ecpp; the following
     471             :  * information can be obtained:
     472             :  * D = squarefreepart(t^2-4N)
     473             :  * m = (N+1-t), q = m/s, a6 = y^3 - x^2 - a4*x, J */
     474             : INLINE GEN
     475        2592 : cert_get_N(GEN x) { return gel(x,1); }
     476             : INLINE GEN
     477        1476 : cert_get_t(GEN x) { return gel(x,2); }
     478             : INLINE GEN
     479         144 : cert_get_D(GEN x)
     480             : {
     481         144 :   GEN N = cert_get_N(x), t = cert_get_t(x);
     482         144 :   return coredisc(subii(sqri(t), shifti(N, 2)));
     483             : }
     484             : INLINE GEN
     485         756 : cert_get_m(GEN x)
     486             : {
     487         756 :   GEN N = cert_get_N(x), t = cert_get_t(x);
     488         756 :   return subii(addiu(N, 1), t);
     489             : }
     490             : INLINE GEN
     491         756 : cert_get_s(GEN x) { return gel(x,3); }
     492             : INLINE GEN
     493         180 : cert_get_q(GEN x) { return diviiexact(cert_get_m(x), cert_get_s(x)); }
     494             : INLINE GEN
     495           6 : cert_get_qlast(GEN x) { return cert_get_q(veclast(x)); }
     496             : INLINE GEN
     497        1248 : cert_get_a4(GEN x) { return gel(x,4); }
     498             : INLINE GEN
     499        1110 : cert_get_P(GEN x) { return gel(x,5); }
     500             : INLINE GEN
     501         132 : cert_get_x(GEN x) { return gel(cert_get_P(x), 1); }
     502             : INLINE GEN
     503         402 : cert_get_a6(GEN z)
     504             : {
     505         402 :   GEN N = cert_get_N(z), a = cert_get_a4(z), P = cert_get_P(z);
     506         402 :   GEN x = gel(P,1), xx = Fp_sqr(x, N);
     507         402 :   GEN y = gel(P,2), yy = Fp_sqr(y, N);
     508         402 :   return Fp_sub(yy, Fp_mul(x, Fp_add(xx,a,N), N), N);
     509             : }
     510             : INLINE GEN
     511         144 : cert_get_E(GEN x) { return mkvec2(cert_get_a4(x), cert_get_a6(x)); }
     512             : INLINE GEN
     513          84 : cert_get_J(GEN x)
     514             : {
     515          84 :   GEN a = cert_get_a4(x), b = cert_get_a6(x), N = cert_get_N(x);
     516          84 :   return Fp_ellj(a, b, N);
     517             : }
     518             : 
     519             : /* "Twist factor". Does not cover J = 0, 1728 */
     520             : static GEN
     521          42 : cert_get_lambda(GEN z)
     522             : {
     523             :   GEN N, J, a, b, A, B;
     524          42 :   J = cert_get_J(z);
     525          42 :   N = cert_get_N(z);
     526          42 :   a = cert_get_a4(z);
     527          42 :   b = cert_get_a6(z);
     528          42 :   Fp_ellj_to_a4a6(J, N, &A, &B);
     529          42 :   return Fp_div(Fp_mul(a,B,N), Fp_mul(b,A,N), N);
     530             : }
     531             : 
     532             : /* Solves for T such that if
     533             :    [A, B] = [3*J*(1728-J), 2*J*(1728-J)^2]
     534             :    and you let
     535             :    L = T^3 + A*T + B, A = A*L^2, B = B*L^3
     536             :    then
     537             :    x == TL and y == L^2
     538             : */
     539             : static GEN
     540          42 : cert_get_T(GEN z)
     541             : {
     542          42 :   GEN N = cert_get_N(z), x = cert_get_x(z);
     543          42 :   GEN l = cert_get_lambda(z); /* l = 1/L */
     544          42 :   return Fp_mul(x, l, N);
     545             : }
     546             : 
     547             : /* database for all invariants, stolen from Hamish's code */
     548             : static GEN
     549          48 : polmodular_db_init_allinv(void)
     550             : {
     551          48 :   const long DEFAULT_MODPOL_DB_LEN = 32;
     552          48 :   GEN a, b = cgetg(39+1, t_VEC);
     553             :   long i;
     554        1920 :   for (i = 1; i < 40; i++) gel(b,i) = zerovec_block(DEFAULT_MODPOL_DB_LEN);
     555          48 :   a = zerovec_block(DEFAULT_MODPOL_DB_LEN);
     556          48 :   return mkvec2(a, b);
     557             : }
     558             : 
     559             : /* Given D and a database of modular polynomials, return polclass(D, inv) */
     560             : static GEN
     561         906 : D_polclass(long D, long inv, GEN *db)
     562             : {
     563         906 :   GEN HD, t = mkvec2(gel(*db, 1), inv == 0? gen_0: gmael(*db, 2, inv));
     564         906 :   HD = polclass0(D, inv, 0, &t);
     565         906 :   gel(*db, 1) = gel(t,1);
     566         906 :   if (inv != 0) gmael(*db, 2, inv) = gel(t,2);
     567         906 :   return HD;
     568             : }
     569             : 
     570             : static GEN
     571        2082 : NqE_check(GEN N, GEN q, GEN a, GEN b, GEN s)
     572             : {
     573        2082 :   GEN mP, sP, P = random_FpE(a, b, N);
     574        2082 :   GEN PJ = mkvec3(gel(P,1), gel(P,2), gen_1);
     575        2082 :   sP = FpJ_mul(PJ, s, a, N); if (FpJ_is_inf(sP)) return NULL;
     576        2082 :   mP = FpJ_mul(sP, q, a, N); return FpJ_is_inf(mP)? mkvec2(a, P): NULL;
     577             : }
     578             : 
     579             : /* Find an elliptic curve E modulo N and a point P on E which corresponds to
     580             :  * the ith element of the certificate; uses N, D, m, q, J from previous steps.
     581             :  * All computations are modulo N unless stated otherwise */
     582             : 
     583             : /* g is a quadratic and cubic nonresidue modulo N */
     584             : static GEN
     585         174 : j0_find_g(GEN N)
     586             : {
     587         174 :   GEN n = diviuexact(subiu(N, 1), 3);
     588             :   for(;;)
     589         420 :   {
     590         594 :     GEN g = randomi(N);
     591         594 :     if (kronecker(g, N) == -1 && !equali1(Fp_pow(g, n, N))) return g;
     592             :   }
     593             : }
     594             : 
     595             : static GEN
     596         906 : find_EP(GEN N, long D, GEN q, GEN g, GEN J, GEN s)
     597             : {
     598             :   GEN A0, B0;
     599         906 :   Fp_ellj_to_a4a6(J, N, &A0, &B0);
     600             :   for(;;)
     601           0 :   { /* expect one iteration: not worth saving the A's and B's */
     602         906 :     GEN gg, v, A = A0, B = B0;
     603             :     long i;
     604         906 :     if ((v = NqE_check(N, q, A, B, s))) return v;
     605         594 :     switch (D_get_wD(D))
     606             :     {
     607         264 :       case 2:
     608         264 :         gg = Fp_sqr(g, N);
     609         264 :         A = Fp_mul(A, gg, N);
     610         264 :         B = Fp_mul(Fp_mul(B, gg, N), g, N);
     611         264 :         if ((v = NqE_check(N, q, A, B, s))) return v;
     612           0 :         break;
     613         120 :       case 4:
     614         252 :         for (i = 1; i < 4; i++)
     615             :         {
     616         252 :           A = Fp_mul(A, g, N);
     617         252 :           if ((v = NqE_check(N, q, A, B, s))) return v;
     618             :         }
     619           0 :         break;
     620         210 :       default: /* 6 */
     621         210 :         B = Fp_mul(B, g, N);
     622         210 :         if ((v = NqE_check(N, q, A, B, s))) return v;
     623         174 :         g = j0_find_g(N);
     624         540 :         for (i = 1; i < 6; i++)
     625             :         {
     626         540 :           B = Fp_mul(B, g, N);
     627         540 :           if (i == 3) continue;
     628         450 :           if ((v = NqE_check(N, q, A, B, s))) return v;
     629             :         }
     630           0 :         break;
     631             :     }
     632             :   }
     633             : }
     634             : 
     635             : /* Return the genus field. If real is set only the real part */
     636             : static GEN
     637         354 : genusfield(GEN Dfac, GEN sq, GEN p, long real)
     638             : {
     639         354 :   long i, j, l = lg(Dfac), dn, n = 0;
     640         354 :   GEN sn, s = gen_0, R = cgetg(l-1+!real, t_VECSMALL);
     641         408 :   for (i = 1; i < l; i++)
     642         408 :     if (Dfac[i] < 0) { n = i; break; }
     643         354 :   if (n == 0) pari_err_BUG("realgenusfield");
     644         354 :   dn = Dfac[n]; sn = gel(sq, n);
     645        1086 :   for (j = i = 1; i < l; i++)
     646         732 :     if (i != n)
     647             :     {
     648         378 :       long d = Dfac[i];
     649         378 :       GEN si = gel(sq,i);
     650         378 :       R[j++] = d > 0 ? d : d * dn;
     651         378 :       s = Fp_add(s, d > 0? si: Fp_mul(si, sn, p), p);
     652             :     }
     653         354 :   if (!real)
     654             :   {
     655         138 :     R[j++] = dn;
     656         138 :     s = Fp_add(s,sn, p);
     657             :   }
     658         354 :   return mkvec2(R, s);
     659             : }
     660             : 
     661             : static GEN
     662          30 : realpart(GEN P, GEN R)
     663             : {
     664          30 :   GEN G = galoisinit(rnfequation(P,R), NULL);
     665          30 :   GEN T = galoisfixedfield(G, gel(gal_get_gen(G),2), 1, -1);
     666          30 :   setvarn(T,1); return polredbest(T,0);
     667             : }
     668             : 
     669             : static GEN
     670          30 : dihedralpol(long D, long l)
     671             : {
     672          30 :   GEN P = quadpoly0(utoineg(-D), 1);
     673          30 :   GEN bnf = Buchall(P, 1, BIGDEFAULTPREC);
     674          30 :   GEN x = bnrclassfield(bnf, utoipos(l), 0, BIGDEFAULTPREC);
     675          60 :   pari_APPLY_same(realpart(P, gel(x,i)))
     676             : }
     677             : 
     678             : static GEN
     679         906 : FpX_classtower_oneroot(GEN P, GEN Dinfo, GEN Dfac, GEN sq, GEN p)
     680             : {
     681         906 :   long D = Dinfo_get_D(Dinfo), h = Dinfo_get_h(Dinfo);
     682         906 :   if (degpol(P) > 1)
     683             :   {
     684         354 :     long real = !modinv_is_double_eta(Dinfo_get_bi(Dinfo));
     685         354 :     GEN V = genusfield(Dfac, sq, p, real), v = gel(V,1), R = gel(V,2);
     686         354 :     GEN N = NULL;
     687         354 :     long i, l = lg(v);
     688         870 :     for (i = 1; i < l; i++)
     689             :     {
     690         516 :       GEN Q = deg2pol_shallow(gen_1, gen_0, stoi(-v[i]), 1);
     691         516 :       if (!N) N = Q;
     692             :       else
     693             :       {
     694         198 :         GEN cm = polcompositum0(N,Q,3);
     695         198 :         N = gel(cm,1); P = QXY_QXQ_evalx(P,gmael(cm,2,2),N);
     696             :       }
     697         516 :       P = liftpol_shallow(gmael(nffactor(N,P),1,1));
     698             :     }
     699         354 :     if (h%3 == 0 && (!N || degpol(N)*3<degpol(P)))
     700             :     {
     701          30 :       GEN v = dihedralpol(D,3);
     702          30 :       long i, l = lg(v);
     703          30 :       for (i = 1; i < l; i++)
     704             :       {
     705          30 :         GEN Q = gel(v,i);
     706          30 :         if (!N) N = Q;
     707             :         else
     708             :         {
     709           0 :           GEN cm = polcompositum0(N, Q, 3);
     710           0 :           N = gel(cm,1); P = QXY_QXQ_evalx(P,gmael(cm,2,2),N);
     711           0 :           R = Fp_mul(R, gel(cm,4),p);
     712             :         }
     713          30 :         R = Fp_add(R, FpX_oneroot_split(Q, p), p);
     714          30 :         P = liftpol_shallow(gmael(nffactor(N,P),1,1));
     715          30 :         if (degpol(N)*3>=degpol(P)) break;
     716             :       }
     717             :     }
     718         354 :     if (N)
     719         348 :       P = FpXY_evalx(Q_primpart(P), R, p);
     720             :   }
     721         906 :   return P;
     722             : }
     723             : 
     724             : GEN
     725         906 : ecpp_step2_worker(GEN S, GEN HD, GEN primelist, long dbg)
     726             : {
     727         906 :   pari_sp av = avma;
     728             :   pari_timer ti;
     729             :   GEN J, t, s, EP, rt, res;
     730         906 :   GEN N = NDmqg_get_N(S), Dinfo = NDmqg_get_Dinfo(S);
     731         906 :   GEN m = NDmqg_get_m(S), q = NDmqg_get_q(S);
     732         906 :   GEN g = NDmqg_get_g(S), sq = NDmqg_get_sqrt(S);
     733         906 :   long D = Dinfo_get_D(Dinfo), inv = Dinfo_get_bi(Dinfo);
     734         906 :   GEN Dfacp = Dfac_to_p(Dinfo_get_Dfac(Dinfo), primelist);
     735         906 :   long C2 = 0, C3 = 0, C4 = 0, D1 = 0;
     736         906 :   GEN P, c = getrand();
     737         906 :   setrand(gen_1); /* for reproducibility */
     738             :   /* C2: Find a root modulo N of polclass(D,inv) */
     739         906 :   if (dbg >= 2) timer_start(&ti);
     740         906 :   P = FpX_classtower_oneroot(HD, Dinfo, Dfacp, sq, N);
     741         906 :   if (dbg >= 2) C2 = timer_delay(&ti);
     742         906 :   rt = FpX_oneroot_split(P, N);
     743         906 :   if (dbg >= 2) C3 = timer_delay(&ti);
     744             :   /* C3: Convert root from previous step into the appropriate j-invariant */
     745         906 :   J = Fp_modinv_to_j(rt, inv, N); /* root of polclass(D) */
     746         906 :   if (dbg >= 2) C4 = timer_delay(&ti);
     747             :   /* D1: Find an elliptic curve E with a point P satisfying the theorem */
     748         906 :   s = diviiexact(m, q);
     749         906 :   EP = find_EP(N, D, q, g, J, s);
     750         906 :   if (dbg >= 2) D1 = timer_delay(&ti);
     751             : 
     752             :   /* D2: Compute for t and s */
     753         906 :   t = subii(addiu(N, 1), m); /* t = N+1-m */
     754         906 :   res = mkvec2(mkvec5(N, t, s, gel(EP,1), gel(EP,2)),mkvecsmall4(C2,C3,C4,D1));
     755         906 :   setrand(c);
     756         906 :   return gerepilecopy(av, res);
     757             : }
     758             : 
     759             : /* This uses [N, D, m, q] from step 1 to find the appropriate j-invariants
     760             :  * to be used in step 3. Step 2 is divided into substeps 2a, 2b, 2c */
     761             : static GEN
     762          48 : ecpp_step2(GEN step1, GEN *X0, GEN primelist)
     763             : {
     764          48 :   long j, Dprev = 0;
     765             :   pari_timer ti;
     766          48 :   GEN perm = gen_indexsort(step1, NULL, &sort_NDmq_by_D);
     767          48 :   GEN step2 = cgetg(lg(step1), t_VEC);
     768          48 :   GEN HD = NULL, db = polmodular_db_init_allinv();
     769          48 :   long ls = lg(step2), pending = 0;
     770             :   GEN worker;
     771             :   struct pari_mt pt;
     772          48 :   GEN Hi = cgetg(ls, t_VEC);
     773         954 :   for (j = 1; j < ls; j++)
     774             :   {
     775         906 :     long i = uel(perm, j);
     776         906 :     GEN S = gel(step1, i);
     777         906 :     GEN Dinfo = NDmqg_get_Dinfo(S);
     778         906 :     long D = Dinfo_get_D(Dinfo), inv = Dinfo_get_bi(Dinfo);
     779             :     /* C1: Find the appropriate class polynomial modulo N */
     780         906 :     dbg_mode() timer_start(&ti);
     781         906 :     if (D != Dprev) HD = D_polclass(D, inv, &db);
     782         906 :     dbg_mode() {
     783           0 :       GEN N = NDmqg_get_N(S);
     784           0 :       long tt = timer_record(X0, "C1", &ti);
     785           0 :       err_printf(ANSI_BRIGHT_GREEN "\n[ %3d | %4ld bits]" ANSI_RESET, i, expi(N));
     786           0 :       err_printf(ANSI_GREEN " D = %8ld poldeg = %4ld" ANSI_RESET, D, degpol(HD));
     787           0 :       if (D == Dprev) err_printf(" %6ld", tt);
     788           0 :       else err_printf(ANSI_BRIGHT_WHITE " %6ld" ANSI_RESET, tt);
     789             :     }
     790         906 :     gel(Hi, i) = HD;
     791             :   }
     792          48 :   gunclone_deep(db);
     793          48 :   worker = snm_closure(is_entry("_ecpp_step2_worker"),
     794             :                        mkvec2(primelist,stoi(DEBUGLEVEL)));
     795          48 :   mt_queue_start_lim(&pt, worker, ls-1);
     796         954 :   for (j = 1; j < ls || pending; j++)
     797             :   {
     798             :     long jj;
     799         906 :     GEN S = gel(step1, j), HD = gel(Hi, j), done;
     800         906 :     mt_queue_submit(&pt, j, j < ls ? mkvec2(S, HD) : NULL);
     801         906 :     done = mt_queue_get(&pt, &jj, &pending);
     802         906 :     if (!done) continue;
     803         906 :     dbg_mode() {
     804           0 :       GEN T = gel(done,2);
     805           0 :       GEN N = NDmqg_get_N(gel(step1, jj));
     806           0 :       err_printf(ANSI_BRIGHT_GREEN "\n[ %3d | %4ld bits]" ANSI_RESET, jj, expi(N));
     807           0 :       err_printf(" %6ld", time_record(X0, "C2", T[1]));
     808           0 :       err_printf(" %6ld", time_record(X0, "C3", T[2]));
     809           0 :       err_printf(" %6ld", time_record(X0, "C4", T[3]));
     810           0 :       err_printf(" %6ld", time_record(X0, "D1", T[4]));
     811             :     }
     812         906 :     gel(step2, jj) = gel(done,1);
     813             :   }
     814          48 :   mt_queue_end(&pt);
     815          48 :   return step2;
     816             : }
     817             : /* end of functions for step 2 */
     818             : 
     819             : GEN
     820       14088 : ecpp_sqrt_worker(GEN p, GEN g, GEN N)
     821             : {
     822       14088 :   GEN r = Fp_sqrt_i(p, g, N);
     823       14088 :   return r ? r: gen_0;
     824             : }
     825             : 
     826             : static void
     827       14088 : ecpp_parsqrt(GEN N, GEN param, GEN kroP, GEN sqrtlist, GEN g, long nb, long *nbsqrt)
     828             : {
     829       14088 :   GEN primelist = ecpp_param_get_primelist(param);
     830       14088 :   GEN ordinv = ecpp_param_get_primeord(param);
     831       14088 :   long i, k, l = lg(ordinv), n = *nbsqrt+1;
     832       14088 :   GEN worker = snm_closure(is_entry("_ecpp_sqrt_worker"), mkvec2(g, N));
     833       14088 :   GEN W = cgetg(nb+1,t_VEC), V;
     834       42156 :   for (i=n, k=1; k<=nb && i<l; i++)
     835             :   {
     836       28068 :     long pi = ordinv[i];
     837       28068 :     if (kroP[pi] > 0)
     838       14088 :       gel(W,k++) = stoi(primelist[pi]);
     839             :   }
     840       14088 :   *nbsqrt=i-1;
     841       14088 :   setlg(W,k);
     842       14088 :   V = gen_parapply(worker, W);
     843       42156 :   for (i=n, k=1; k<=nb && i<l; i++)
     844             :   {
     845       28068 :     long pi = ordinv[i];
     846       28068 :     if (kroP[pi] > 0)
     847             :     {
     848       14088 :       dbg_mode() err_printf(ANSI_MAGENTA "S" ANSI_RESET);
     849       14088 :       if (!signe(gel(V,k)))
     850           0 :         pari_err_BUG("D_find_discsqrt"); /* possible if N composite */
     851       14088 :       gel(sqrtlist,pi) = gel(V,k++);
     852             :     }
     853             :   }
     854       14088 : }
     855             : 
     856             : /* start of functions for step 1 */
     857             : 
     858             : /* This finds the square root of D modulo N [given by Dfac]: find the square
     859             :  * root modulo N of each prime p dividing D and multiply them out */
     860             : static GEN
     861       34866 : D_find_discsqrt(GEN N, GEN param, GEN Dfac, GEN kroP, GEN sqrtlist, GEN g, long *nbsqrt)
     862             : {
     863       34866 :   GEN s = NULL, sj;
     864       34866 :   long i, l = lg(Dfac);
     865      106212 :   for (i = 1; i < l; i++)
     866             :   {
     867       71346 :     long j = Dfac[i];
     868       85434 :     while(!signe(gel(sqrtlist,j)))
     869       14088 :       ecpp_parsqrt(N, param, kroP, sqrtlist, g, mt_nbthreads(), nbsqrt);
     870       71346 :     sj = gel(sqrtlist,j);
     871       71346 :     s = s? Fp_mul(s, sj, N): sj;
     872             :   }
     873       34866 :   return s;/* != NULL */
     874             : }
     875             : 
     876             : /* Given a solution U, V to U^2 + |D|V^2 = 4N, this find all the possible
     877             :      cardinalities of the elliptic curves over the finite field F_N
     878             :      whose endomorphism ring is isomorphic to the maximal order
     879             :      in the imaginary quadratic number field K = Q(sqrt(D)). ???
     880             : */
     881             : static GEN
     882        9312 : NUV_find_m(GEN Dinfo, GEN N, GEN U, GEN V, long wD)
     883             : {
     884        9312 :   GEN m, Nplus1 = addiu(N, 1), u = U, mvec = cgetg(wD+1, t_VEC);
     885             :   long i;
     886       31164 :   for (i = 0; i < wD; i++)
     887             :   {
     888       21852 :     if (odd(i)) m = subii(Nplus1, u);
     889             :     else
     890             :     {
     891       10926 :       if (wD == 4 && i==2) u = shifti(V, 1);
     892       10500 :       else if (wD == 6 && i==2) u = shifti(submuliu(U, V, 3), -1);
     893        9906 :       else if (wD == 6 && i==4) u = shifti(addmuliu(U, V, 3), -1);
     894       10926 :       m = addii(Nplus1, u);
     895             :     }
     896       21852 :     gel(mvec, i+1) = mkvec2(Dinfo, m);
     897             :   }
     898        9312 :   return mvec;
     899             : }
     900             : 
     901             : /* Populates Dmbatch with Dmvec's -- whose components are of the form [D,m],
     902             :    where m is a cardinalities of an elliptic curve with endomorphism ring
     903             :    isomorphic to the maximal order of imaginary quadratic K = Q(sqrt(D)).
     904             :    It returns 0 if:
     905             :      * Any of the p* dividing D is not a quadratic residue mod N
     906             :      * Cornacchia cannot find a solution U^2 + |D|V^2 = 4N.
     907             :    Otherwise, it returns the number of cardinalities added to Dmbatch.
     908             :    Finally, sqrtlist and g help compute the square root modulo N of D.
     909             : */
     910             : static long
     911      461172 : D_collectcards(GEN N, GEN param, GEN* X0, GEN Dinfo, GEN sqrtlist, GEN g, GEN Dmbatch, GEN kroP, long *nbsqrt)
     912             : {
     913      461172 :   long i, l, corn_succ, wD, D = Dinfo_get_D(Dinfo);
     914      461172 :   GEN U, V, sqrtofDmodN, Dfac = Dinfo_get_Dfac(Dinfo);
     915             :   pari_timer ti;
     916             : 
     917             :   /* A1: Check (p*|N) = 1 for all primes dividing D */
     918      461172 :   l = lg(Dfac);
     919      659136 :   for (i = 1; i < l; i++)
     920             :   {
     921      624270 :     long j = Dfac[i];
     922      624270 :     if (kroP[j] < 0) return 0;
     923             :   }
     924             :   /* A3: Get square root of D mod N */
     925       34866 :   dbg_mode() timer_start(&ti);
     926       34866 :   sqrtofDmodN = D_find_discsqrt(N, param, Dfac, kroP, sqrtlist, g, nbsqrt);
     927       34866 :   dbg_mode() timer_record(X0, "A3", &ti);
     928             :   /* A5: Use square root with Cornacchia to solve U^2 + |D|V^2 = 4N */
     929       34866 :   dbg_mode() timer_start(&ti);
     930       34866 :   corn_succ = cornacchia2_sqrt( utoi(labs(D)), N, sqrtofDmodN, &U, &V);
     931       34866 :   dbg_mode() timer_record(X0, "A5", &ti);
     932       34866 :   if (!corn_succ) {
     933       25554 :     dbg_mode1() err_printf(ANSI_YELLOW "c" ANSI_RESET);
     934       25554 :     return 0;
     935             :   }
     936             :   /* A6: Collect the w(D) cardinalities of E/(F_N) with CM by D */
     937        9312 :   dbg_mode() err_printf(ANSI_BRIGHT_YELLOW "D" ANSI_RESET);
     938        9312 :   wD = D_get_wD(D);
     939        9312 :   vectrunc_append_batch(Dmbatch, NUV_find_m(Dinfo,N,U,V,wD));
     940        9312 :   return wD;
     941             : }
     942             : 
     943             : /* Compute (S(16N, 2) + S(4096N, 4) + 4)\4 + 1,  where S is the nth root
     944             :  * rounded down. This is at most one more than (N^1/4 + 1)^2. */
     945             : static GEN
     946        3306 : ecpp_qlo(GEN N)
     947             : {
     948        3306 :   GEN a = sqrtnint(shifti(N, 4), 2);
     949        3306 :   GEN b = sqrtnint(shifti(N, 12), 4);
     950        3306 :   return addiu(shifti(addii(a, b), -2), 2);
     951             : }
     952             : 
     953             : static long
     954       11448 : lessthan_qlo(void* E, GEN Dmq) { return (cmpii(gel(Dmq,3), (GEN)E) < 0); }
     955             : static long
     956       10326 : gained_bits(void* E, GEN Dmq) { return (expi(gel(Dmq,3)) <= (long)E); }
     957             : 
     958             : /*  Input: Dmqvec
     959             :  * Output: Dmqvec such that q satisfies (N^1/4 + 1)^2 < q < N/2 */
     960             : static GEN
     961        3306 : Dmqvec_slice(GEN N, GEN Dmqvec)
     962             : {
     963             :   long lo, hi;
     964             : 
     965        3306 :   gen_sort_inplace(Dmqvec, NULL, &sort_Dmq_by_q, NULL); /* sort wrt q */
     966        3306 :   lo = zv_binsearch0((void*)ecpp_qlo(N), &lessthan_qlo, Dmqvec); lo++;
     967        3306 :   if (lo >= lg(Dmqvec)) return NULL;
     968             : 
     969        3306 :   hi = zv_binsearch0((void*)(expi(N)-1), &gained_bits, Dmqvec);
     970        3306 :   if (hi == 0) return NULL;
     971             : 
     972        3306 :   return vecslice(Dmqvec, lo, hi);
     973             : }
     974             : 
     975             : /* Given a Dmvec of [D,m]'s, simultaneously remove all prime factors of each
     976             :  * m less then BOUND_PRIMORIAL. This implements Franke 2004: Proving the
     977             :  * Primality of Very Large Numbers with fastECPP */
     978             : static GEN
     979        3306 : Dmvec_batchfactor(GEN Dmvec, GEN primorial)
     980             : {
     981        3306 :   long i, l = lg(Dmvec);
     982        3306 :   GEN leaf, v = cgetg(l, t_VEC);
     983       25158 :   for (i = 1; i < l; i++)
     984             :   { /* cheaply remove powers of 2 */
     985       21852 :     GEN m = gmael(Dmvec, i, 2);
     986       21852 :     long v2 = vali(m);
     987       21852 :     gel(v,i) = v2? shifti(m,-v2): m;
     988             :   }
     989        3306 :   leaf = Z_ZV_mod_tree(primorial, v, ZV_producttree(v));
     990             :   /* Go through each leaf and remove small factors. */
     991       25158 :   for (i = 1; i < l; i++)
     992             :   {
     993       21852 :     GEN q = gel(v,i), Dm = gel(Dmvec,i), L = gel(leaf,i);
     994       72270 :     while (!equali1(L)) { L = gcdii(q, L); q = diviiexact(q, L); }
     995       21852 :     gel(v,i) = mkvec3(gel(Dm,1), gel(Dm,2), q);
     996             :   }
     997        3306 :   return v;
     998             : }
     999             : 
    1000             : /* return good parameters [maxsqrt, maxpcdg, tdivexp] for ecpp(N) */
    1001             : static GEN
    1002        4266 : tunevec(long expiN, GEN param)
    1003             : {
    1004        4266 :   GEN T = ecpp_param_get_tune(param);
    1005        4266 :   long i, n = lg(T)-1;
    1006        6948 :   for (i = 1; i < n; i++) { GEN x = gel(T,i); if (expiN <= x[4]) return x; }
    1007        1902 :   return gel(T,n);
    1008             : }
    1009             : static long
    1010        4266 : tunevec_tdivbd(long expiN, GEN param) { return uel(tunevec(expiN, param), 3); }
    1011             : static long
    1012         960 : tunevec_batchsize(long expiN, GEN param)
    1013             : {
    1014         960 :   long t, b = 28 - tunevec_tdivbd(expiN, param);
    1015         960 :   if (b < 0) return expiN;
    1016         960 :   t = expiN >> b; return t < 1? 1: t;
    1017             : }
    1018             : 
    1019             : static GEN
    1020        3306 : Dmbatch_factor_Dmqvec(GEN N, long expiN, GEN* X0, GEN Dmbatch, GEN param)
    1021             : {
    1022        3306 :   pari_sp av = avma;
    1023             :   pari_timer ti;
    1024        3306 :   GEN Dmqvec, primorial_vec = ecpp_param_get_primorial_vec(param);
    1025        3306 :   GEN primorial = gel(primorial_vec, tunevec_tdivbd(expiN, param)-19);
    1026             : 
    1027             :   /* B1: Factor by batch */
    1028        3306 :   dbg_mode() timer_start(&ti);
    1029        3306 :   Dmqvec = Dmvec_batchfactor(Dmbatch, primorial);
    1030        3306 :   dbg_mode() timer_record(X0, "B1", &ti);
    1031             : 
    1032             :   /* B2: For each batch, remove cardinalities lower than (N^(1/4)+1)^2
    1033             :    *     and cardinalities in which we didn't win enough bits */
    1034        3306 :   Dmqvec = Dmqvec_slice(N, Dmqvec);
    1035        3306 :   if (!Dmqvec) return gc_NULL(av); /* nothing is left */
    1036        3306 :   return gerepilecopy(av, Dmqvec);
    1037             : }
    1038             : 
    1039             : GEN
    1040       17418 : ecpp_ispsp_worker(GEN N)
    1041       17418 : { return mkvecsmall(BPSW_psp_nosmalldiv(N)); }
    1042             : 
    1043             : static GEN
    1044         906 : mkNDmqg(GEN z, GEN N, GEN Dmq, GEN g, GEN sqrtlist)
    1045             : {
    1046         906 :   GEN Dinfo = gel(Dmq,1), sq =  Dfac_to_roots(Dinfo_get_Dfac(Dinfo),sqrtlist);
    1047         906 :   GEN NDmqg = mkcol6(N, Dinfo, gel(Dmq,2), gel(Dmq,3), g, sq);
    1048         906 :   return mkvec2(NDmqg, z);
    1049             : }
    1050             : /* expi(N) > 64. Return [ NDmqg, N_downrun(q) ]; NDmqg is a vector of the form
    1051             :  * [N,D,m,q,g,sqrt]. For successive D, find a square root of D mod N (g is a
    1052             :  * quadratic nonresidue), solve U^2 + |D|V^2 = 4N, then find candidates for m.
    1053             :  * When enough m's, batch-factor them to produce the q's. If one of the q's is
    1054             :  * pseudoprime, recursive call with N = q. May return gen_0 at toplevel
    1055             :  * => N has a small prime divisor */
    1056             : static GEN
    1057         960 : N_downrun(GEN N, GEN param, GEN worker, GEN *X0, long *depth, long persevere, long stopat)
    1058             : {
    1059             :   pari_timer T, ti;
    1060         960 :   long lgdisclist, lprimelist, nbsqrt = 0, t, i, j, expiN = expi(N);
    1061         960 :   long persevere_next = 0, FAIL = 0;
    1062             :   ulong maxpcdg;
    1063             :   GEN primelist, uprimelist, disclist, sqrtlist, g, Dmbatch, kroP, Np;
    1064             : 
    1065         960 :   dbg_mode() timer_start(&T);
    1066         960 :   (*depth)++; /* we're going down the tree. */
    1067         960 :   maxpcdg = ecpp_param_get_maxpcdg(param);
    1068         960 :   primelist = ecpp_param_get_primelist(param);
    1069         960 :   uprimelist = ecpp_param_get_uprimelist(param);
    1070         960 :   disclist = ecpp_param_get_disclist(param);
    1071         960 :   lgdisclist = lg(disclist);
    1072         960 :   t = tunevec_batchsize(expiN, param);
    1073             : 
    1074             :   /* Precomputation for taking square roots, g needed for Fp_sqrt_i */
    1075         960 :   g = Fp_2gener(N); if (!g) return gen_0; /* Composite if this happens. */
    1076             : 
    1077             :   /* Initialize sqrtlist for this N. */
    1078         960 :   lprimelist = lg(primelist);
    1079         960 :   sqrtlist = zerovec(lprimelist-1);
    1080             : 
    1081             :   /* A2: Check (p*|N) = 1 for all p */
    1082         960 :   dbg_mode() timer_start(&ti);
    1083             :   /* kroP[i] will contain (primelist[i] | N) */
    1084         960 :   kroP = cgetg(lprimelist,t_VECSMALL);
    1085         960 :   switch(mod8(N))
    1086             :   { /* primelist[1,2,3] = [8,-4,-8]; N is odd */
    1087         300 :     case 1: kroP[1] = kroP[2] = kroP[3] = 1; break;
    1088         222 :     case 3: kroP[1] = -1; kroP[2] =-1; kroP[3] = 1; break;
    1089         288 :     case 5: kroP[1] = -1; kroP[2] = 1; kroP[3] =-1; break;
    1090         150 :     case 7: kroP[1] =  1; kroP[2] =-1; kroP[3] =-1; break;
    1091             :   }
    1092         960 :   Np = Z_ZV_mod_tree(N, gel(uprimelist,1), gel(uprimelist,2));
    1093      114138 :   for(i=4; i<lprimelist; i++)
    1094             :   {
    1095      113178 :     if (Np[i]==0) return gen_0;
    1096      113178 :     kroP[i] = kross(Np[i],primelist[i]);
    1097             :   }
    1098         960 :   dbg_mode() timer_record(X0, "A2", &ti);
    1099             : 
    1100             :   /* Print the start of this iteration. */
    1101         960 :   dbg_mode() {
    1102           0 :     char o = persevere? '<': '[';
    1103           0 :     char c = persevere? '>': ']';
    1104           0 :     err_printf(ANSI_BRIGHT_CYAN "\n%c %3d | %4ld bits%c "
    1105             :                ANSI_RESET, o, *depth, expiN, c);
    1106             :   }
    1107             :   /* Initialize Dmbatch, populated with candidate cardinalities in Phase I
    1108             :    * (until #Dmbatch >= t); its elements will be factored on Phase II */
    1109         960 :   Dmbatch = vectrunc_init(t+7);
    1110             : 
    1111             :   /* Number of cardinalities so far; should always be equal to lg(Dmbatch)-1. */
    1112             :   /* i determines which discriminant we are considering. */
    1113         960 :   i = 1;
    1114        3354 :   while (!FAIL)
    1115             :   {
    1116             :     pari_timer F;
    1117        3348 :     long lgDmqlist, last_i = i, numcard = 0; /* #Dmbatch */
    1118             :     GEN Dmqlist;
    1119             : 
    1120             :     /* Dmbatch begins "empty", but keep the allocated memory. */
    1121        3348 :     setlg(Dmbatch, 1);
    1122             : 
    1123             :     /* PHASE I: Go through the D's and search for candidate m's.
    1124             :      * Fill up Dmbatch until there are at least t elements. */
    1125      461232 :     while (i < lgdisclist )
    1126             :     {
    1127      461208 :       GEN Dinfo = gel(disclist, i);
    1128             :       long n;
    1129      461208 :       if (!persevere && Dinfo_get_pd(Dinfo) > maxpcdg) { FAIL = 1; break; }
    1130      461172 :       n = D_collectcards(N,param, X0, Dinfo, sqrtlist, g, Dmbatch, kroP, &nbsqrt);
    1131      462078 :       if (n < 0) return gen_0;
    1132      461172 :       last_i = i++;
    1133      461172 :       numcard += n; if (numcard >= t) break;
    1134             :     }
    1135             : 
    1136             :     /* We have exhausted disclist and there are no card. to be factored */
    1137        3348 :     if (numcard <= 0 && (FAIL || i >= lgdisclist)) break;
    1138             : 
    1139             :     /* PHASE II: Find the corresponding q's for the m's found. Use Dmbatch */
    1140             :     /* Find coresponding q of the candidate m's. */
    1141        3306 :     dbg_mode() timer_start(&F);
    1142        3306 :     Dmqlist = Dmbatch_factor_Dmqvec(N, expiN, X0, Dmbatch, param);
    1143        3306 :     if (Dmqlist == NULL) continue; /* none left => next discriminant. */
    1144             : 
    1145             :     /* If we are cheating by adding class numbers, sort by class number */
    1146        3306 :     if (Dinfo_get_pd(gel(disclist, last_i)) > maxpcdg)
    1147         330 :       gen_sort_inplace(Dmqlist, NULL, &sort_Dmq_by_cnum, NULL);
    1148             : 
    1149             :     /* Check pseudoprimality before going down. */
    1150        3306 :     lgDmqlist = lg(Dmqlist);
    1151        3342 :     for (j = 1; j < lgDmqlist; j++)
    1152             :     {
    1153             :       GEN z, Dmq, q;
    1154             :       struct pari_mt pt;
    1155             :       pari_timer ti2;
    1156        3342 :       long running, stop = 0, pending = 0;
    1157        3342 :       mt_queue_start_lim(&pt, worker, lgDmqlist-j);
    1158        3342 :       dbg_mode() timer_start(&ti2);
    1159       20760 :       while ((running = (!stop && j < lgDmqlist)) || pending)
    1160             :       {
    1161             :         long jj;
    1162             :         GEN done;
    1163       17418 :         mt_queue_submit(&pt, j, running ? mkvec(gmael(Dmqlist,j,3)) : NULL);
    1164       17418 :         done = mt_queue_get(&pt, &jj, &pending);
    1165       17418 :         if (done)
    1166             :         {
    1167       17418 :           if (done[1] == 0)
    1168       16476 :           { dbg_mode() err_printf(ANSI_WHITE "." ANSI_RESET); }
    1169         942 :           else if (!stop || jj < stop)
    1170         942 :             stop = jj;
    1171             :         }
    1172       17418 :         j++;
    1173             :       }
    1174        3342 :       mt_queue_end(&pt);
    1175        3342 :       dbg_mode() timer_record(X0, "B3", &ti2);
    1176        3342 :       if (!stop && j >= lgDmqlist) break;
    1177         942 :       j = stop; Dmq = gel(Dmqlist,j); q = gel(Dmq,3);
    1178             : 
    1179         942 :       dbg_mode() {
    1180           0 :         err_printf(ANSI_BRIGHT_RED "\n       %5ld bits " ANSI_RESET,
    1181           0 :                    expi(q)-expiN);
    1182           0 :         err_printf("  D = %ld", Dmq_get_D(Dmq));
    1183           0 :         err_printf(ANSI_BRIGHT_BLUE "  h = %ld" ANSI_RESET, Dmq_get_h(Dmq));
    1184             :       }
    1185             :       /* q is pseudoprime */
    1186         942 :       if (expi(q) < stopat) z = gen_1; /* q is prime; sentinel */
    1187             :       else
    1188             :       {
    1189         894 :         z = N_downrun(q, param, worker, X0, depth, persevere_next, stopat);
    1190         894 :         if (!z) {
    1191          36 :           dbg_mode() { char o = persevere? '<': '[', c = persevere? '>': ']';
    1192           0 :                        err_printf(ANSI_CYAN "\n%c %3d | %4ld bits%c "
    1193             :                                   ANSI_RESET, o, *depth, expiN, c); }
    1194          36 :           continue; /* downrun failed */
    1195             :         }
    1196        1764 :         if (isintzero(z)) return z;
    1197             :       }
    1198         906 :       return mkNDmqg(z, N, Dmq, g, sqrtlist); /* SUCCESS */
    1199             :     }
    1200        2400 :     if (i >= lgdisclist) break; /* discriminants exhausted: FAIL */
    1201        2394 :     if (Dinfo_get_pd(gel(disclist, last_i)) > maxpcdg)
    1202             :     {
    1203         312 :       dbg_mode() err_printf(ANSI_BRIGHT_RED "  !" ANSI_RESET);
    1204         312 :       persevere_next = 1;
    1205             :     }
    1206             :   }
    1207             :   /* FAILED: Out of discriminants. */
    1208          54 :   umael(*X0, 3, 1)++; /* FAILS++ */
    1209          54 :   dbg_mode() err_printf(ANSI_BRIGHT_RED "  X" ANSI_RESET);
    1210          54 :   (*depth)--; return NULL;
    1211             : }
    1212             : 
    1213             : /* x: the output of the (recursive) downrun function; return a vector
    1214             :  * whose components are [N, D, m, q, g] */
    1215             : static GEN
    1216          48 : ecpp_flattencert(GEN x, long depth)
    1217             : {
    1218          48 :   long i, l = depth+1;
    1219          48 :   GEN ret = cgetg(l, t_VEC);
    1220         954 :   for (i = 1; i < l; i++) { gel(ret, i) = gel(x,1); x = gel(x,2); }
    1221          48 :   return ret;
    1222             : }
    1223             : 
    1224             : /* Call the first downrun then unravel the skeleton of the certificate.
    1225             :  * Assume expi(N) < 64. This returns one of the following:
    1226             :  * - a vector of the form [N, D, m, q, y]
    1227             :  * - gen_0 (if N is composite)
    1228             :  * - NULL (if FAIL) */
    1229             : static GEN
    1230          66 : ecpp_step1(GEN N, GEN param, GEN* X0, long stopat)
    1231             : {
    1232          66 :   pari_sp av = avma;
    1233          66 :   long depth = 0;
    1234          66 :   GEN worker = strtofunction("_ecpp_ispsp_worker");
    1235          66 :   GEN downrun = N_downrun(N, param, worker, X0, &depth, 1, stopat);
    1236          66 :   if (downrun == NULL) return gc_NULL(av);
    1237          48 :   if (typ(downrun) != t_VEC) return gen_0;
    1238          48 :   return gerepilecopy(av, ecpp_flattencert(downrun, depth));
    1239             : }
    1240             : 
    1241             : /* The input is an integer N.
    1242             :    It uses the precomputation step0 done in ecpp_step0. */
    1243             : static GEN
    1244          66 : ecpp_param(GEN N, GEN param, long stopat)
    1245             : {
    1246             : 
    1247             :   GEN step1, answer, Tv, Cv, X0;
    1248          66 :   pari_sp av = avma;
    1249             :   long i, j;
    1250             : 
    1251             :   /* Check if N is pseudoprime to begin with. */
    1252          66 :   if (!BPSW_psp(N)) return gen_0;
    1253             : 
    1254             :   /* Check if we should even prove it. */
    1255          66 :   if (expi(N) < stopat) return N;
    1256             : 
    1257             :   /* Timers and Counters */
    1258          66 :   Tv = mkvec4(zero_zv(5), zero_zv(3), zero_zv(4), zero_zv(1));
    1259          66 :   Cv = mkvec4(zero_zv(5), zero_zv(3), zero_zv(4), zero_zv(1));
    1260          66 :   X0 = mkvec3(Tv, Cv, zero_zv(1));
    1261             : 
    1262          66 :   step1 = ecpp_step1(N, param, &X0, stopat);
    1263          66 :   if (step1 == NULL) return gc_NULL(av);
    1264          48 :   if (typ(step1) != t_VEC) { set_avma(av); return gen_0; }
    1265             : 
    1266          48 :   answer = ecpp_step2(step1, &X0, ecpp_param_get_primelist(param));
    1267             : 
    1268          48 :   dbg_mode() {
    1269           0 :   for (i = 1; i < lg(Tv); i++)
    1270             :   {
    1271           0 :     GEN v = gel(Tv, i);
    1272           0 :     long l = lg(v);
    1273           0 :     for (j = 1; j < l; j++)
    1274             :     {
    1275           0 :       long t = umael3(X0,1, i,j), n = umael3(X0,2, i,j);
    1276           0 :       if (!t) continue;
    1277           0 :       err_printf("\n  %c%ld: %16ld %16ld %16.3f", 'A'+i-1,j, t,n, (double)t/n);
    1278             :     }
    1279             :   }
    1280           0 :     err_printf("\n" ANSI_BRIGHT_RED "\nFAILS: %16ld" ANSI_RESET "\n", umael(X0, 3, 1));
    1281             :   }
    1282          48 :   return gerepilecopy(av, answer);
    1283             : }
    1284             : 
    1285             : static const long ecpp_tune[][4]=
    1286             : {
    1287             :   {  100,  2,  20,   500 },
    1288             :   {  350, 14,  21,  1000 },
    1289             :   {  450, 18,  21,  1500 },
    1290             :   {  750, 22,  22,  2000 },
    1291             :   {  900, 26,  23,  2500 },
    1292             :   { 1000, 32,  23,  3000 },
    1293             :   { 1200, 38,  24,  3500 },
    1294             :   { 1400, 44,  24,  4000 },
    1295             :   { 1600, 50,  24,  4500 },
    1296             :   { 1800, 56,  25,  5000 },
    1297             :   { 2000, 62,  25,  5500 },
    1298             :   { 2200, 68,  26,  6000 },
    1299             :   { 2400, 74,  26,  6500 },
    1300             :   { 2600, 80,  26,  7000 },
    1301             :   { 2800, 86,  26,  7500 },
    1302             :   { 3000, 92,  27,  8000 },
    1303             :   { 3200, 98,  27,  8500 },
    1304             :   { 3400, 104, 28,  9000 },
    1305             :   { 3600, 110, 28,  9500 },
    1306             :   { 3800, 116, 29, 10000 },
    1307             :   { 4000, 122, 29,     0 }
    1308             : };
    1309             : 
    1310             : /* assume N BPSW-pseudoprime */
    1311             : GEN
    1312          84 : ecpp0(GEN C, long stopat)
    1313             : {
    1314          84 :   pari_sp av = avma;
    1315             :   long expiN, i, tunelen;
    1316          84 :   GEN N = C, tune;
    1317             : 
    1318             :   /* Is it a partial certificate? */
    1319          84 :   if (typ(C) == t_VEC && check_ecppcert(C))
    1320           6 :     N = cert_get_qlast(C);
    1321          84 :   if (typ(N) != t_INT)
    1322           0 :     pari_err_TYPE("ecpp",C);
    1323             : 
    1324             :   /* Check if we should even prove it. */
    1325          84 :   expiN = expi(N);
    1326          84 :   if (stopat < 64) stopat = 64;
    1327          84 :   if (expiN < stopat) return C;
    1328             : 
    1329          48 :   tunelen = (expiN+499)/500;
    1330          48 :   tune = cgetg(tunelen+1, t_VEC);
    1331         120 :   for (i = 1; i <= tunelen && ecpp_tune[i-1][3]; i++)
    1332          72 :     gel(tune,i) = mkvecsmall4(ecpp_tune[i-1][0], ecpp_tune[i-1][1],
    1333          72 :                               ecpp_tune[i-1][2], ecpp_tune[i-1][3]);
    1334          48 :   for (; i <= tunelen; i++) gel(tune,i) = mkvecsmall4(100*(i+20),4*i+42,30,500*i);
    1335             :   for(;;)
    1336          18 :   {
    1337          66 :     GEN C2, param, x = gel(tune, tunelen);
    1338          66 :     param = ecpp_param_set(tune, x);
    1339          66 :     C2 = ecpp_param(N, param, stopat);
    1340          66 :     if (C2)
    1341          48 :       return typ(C)==t_INT? C2: gerepilecopy(av, shallowconcat(C,C2));
    1342          18 :     x[1] *= 2;
    1343          18 :     x[2] *= 2;
    1344          18 :     x[3] = minss(x[3]+1, 30);
    1345             :   }
    1346             : }
    1347             : 
    1348             : GEN
    1349          24 : ecpp(GEN N)
    1350          24 : { return ecpp0(N, 0); }
    1351             : 
    1352             : long
    1353          36 : isprimeECPP(GEN N)
    1354             : {
    1355          36 :   pari_sp av = avma;
    1356          36 :   if (!BPSW_psp(N)) return 0;
    1357          24 :   return gc_long(av, !isintzero(ecpp(N)));
    1358             : }
    1359             : 
    1360             : /* PARI ECPP Certificate -> Human-readable format */
    1361             : static GEN
    1362           6 : cert_out(GEN x)
    1363             : {
    1364           6 :   long i, l = lg(x);
    1365             :   pari_str str;
    1366           6 :   str_init(&str, 1);
    1367           6 :   if (typ(x) == t_INT)
    1368             :   {
    1369           0 :     str_printf(&str, "%Ps is prime.\nIndeed, ispseudoprime(%Ps) = 1 and %Ps < 2^64.\n", x, x, x);
    1370           0 :     return strtoGENstr(str.string);
    1371             :   }
    1372          18 :   for (i = 1; i < l; i++)
    1373             :   {
    1374          12 :     GEN xi = gel(x, i);
    1375          12 :     str_printf(&str, "[%ld]\n", i);
    1376          12 :     str_printf(&str, " N = %Ps\n", cert_get_N(xi));
    1377          12 :     str_printf(&str, " t = %Ps\n", cert_get_t(xi));
    1378          12 :     str_printf(&str, " s = %Ps\n", cert_get_s(xi));
    1379          12 :     str_printf(&str, "a = %Ps\n", cert_get_a4(xi));
    1380          12 :     str_printf(&str, "D = %Ps\n", cert_get_D(xi));
    1381          12 :     str_printf(&str, "m = %Ps\n", cert_get_m(xi));
    1382          12 :     str_printf(&str, "q = %Ps\n", cert_get_q(xi));
    1383          12 :     str_printf(&str, "E = %Ps\n", cert_get_E(xi));
    1384          12 :     str_printf(&str, "P = %Ps\n", cert_get_P(xi));
    1385             :   }
    1386           6 :   return strtoGENstr(str.string);
    1387             : }
    1388             : 
    1389             : /* PARI ECPP Certificate -> Magma Certificate
    1390             :  * [* [*
    1391             :  *     N, |D|, -1, m,
    1392             :  *     [* a, b *],
    1393             :  *     [* x, y, 1 *],
    1394             :  *     [* [* q, 1 *] *]
    1395             :  *   *], ... *] */
    1396             : static GEN
    1397          12 : magma_out(GEN x)
    1398             : {
    1399          12 :   long i, n = lg(x)-1;
    1400             :   pari_str ret;
    1401          12 :   str_init(&ret, 1);
    1402          12 :   if (typ(x) == t_INT)
    1403             :   {
    1404           0 :     str_printf(&ret, "Operation not supported.");
    1405           0 :     return strtoGENstr(ret.string);
    1406             :   }
    1407          12 :   str_printf(&ret, "[* ");
    1408         144 :   for (i = 1; i<=n; i++)
    1409             :   {
    1410         132 :     GEN xi = gel(x,i), E = cert_get_E(xi), P = cert_get_P(xi);
    1411         132 :     str_printf(&ret, "[* %Ps, %Ps, -1, %Ps, ",
    1412             :               cert_get_N(xi), negi(cert_get_D(xi)), cert_get_m(xi));
    1413         132 :     str_printf(&ret, "[* %Ps, %Ps *], ", gel(E,1), gel(E,2));
    1414         132 :     str_printf(&ret, "[* %Ps, %Ps, 1 *], ", gel(P,1), gel(P,2));
    1415         132 :     str_printf(&ret, "[* [* %Ps, 1 *] *] *]", cert_get_q(xi));
    1416         132 :     if (i != n) str_printf(&ret, ", ");
    1417             :   }
    1418          12 :   str_printf(&ret, " *]");
    1419          12 :   return strtoGENstr(ret.string);
    1420             : }
    1421             : 
    1422             : /* Prints: label=(sign)hexvalue\n
    1423             :  *   where sign is + or -
    1424             :  *   hexvalue is value in hex, of the form: 0xABC123 */
    1425             : static void
    1426         618 : primo_printval(pari_str *ret, const char* label, GEN value)
    1427             : {
    1428         618 :   str_printf(ret, label);
    1429         618 :   if (signe(value) >= 0) str_printf(ret, "=0x%P0X\n", value);
    1430         150 :   else str_printf(ret, "=-0x%P0X\n", negi(value));
    1431         618 : }
    1432             : 
    1433             : /* Input: PARI ECPP Certificate / Output: PRIMO Certificate
    1434             :  *
    1435             :  * Let Y^2 = X^3 + aX + b be the elliptic curve over N with the point (x,y)
    1436             :  * as described in the PARI certificate.
    1437             :  *
    1438             :  * If J != 0, 1728, PRIMO asks for [J,T], where T = a/A * B/b * x,
    1439             :  * A = 3J(1728-J) and B = 2J(1728-J)^2.
    1440             :  *
    1441             :  * If J=0 or J=1728, PRIMO asks for [A,B,T]; we let A=a and B=b => T = x */
    1442             : static GEN
    1443          12 : primo_out(GEN x)
    1444             : {
    1445          12 :   long i, l = (typ(x) == t_INT) ? 1 : lg(x);
    1446             :   pari_str ret;
    1447          12 :   str_init(&ret, 1);
    1448          12 :   str_printf(&ret, "[PRIMO - Primality Certificate]\nFormat=4\n");
    1449          12 :   str_printf(&ret, "TestCount=%ld\n\n[Comments]\n", l-1);
    1450          12 :   str_printf(&ret, "Generated by %s\n",paricfg_version);
    1451          12 :   str_printf(&ret, "https://pari.math.u-bordeaux.fr/\n\n[Candidate]\n");
    1452          12 :   if (typ(x) == t_INT)
    1453             :   {
    1454           0 :     str_printf(&ret, "N=0x%P0X\n", x);
    1455           0 :     return strtoGENstr(ret.string);
    1456             :   }
    1457          12 :   str_printf(&ret, "N=0x%P0X\n\n", cert_get_N(gel(x,1)));
    1458         144 :   for (i = 1; i < l; i++)
    1459             :   {
    1460         132 :     GEN a4, a6, N, Nover2, xi = gel(x,i);
    1461             :     long Ais0, Bis0;
    1462         132 :     str_printf(&ret, "[%ld]\n", i);
    1463         132 :     N = cert_get_N(xi);
    1464         132 :     Nover2 = shifti(N, -1);
    1465         132 :     primo_printval(&ret, "S", cert_get_s(xi));
    1466         132 :     primo_printval(&ret, "W", cert_get_t(xi));
    1467         132 :     a4 = cert_get_a4(xi); Ais0 = isintzero(a4);
    1468         132 :     a6 = cert_get_a6(xi); Bis0 = isintzero(a6);
    1469         132 :     if (!Ais0 && !Bis0) { /* J != 0, 1728 */
    1470          42 :       primo_printval(&ret, "J", Fp_center_i(cert_get_J(xi), N, Nover2));
    1471          42 :       primo_printval(&ret, "T", cert_get_T(xi));
    1472             :     } else {
    1473          90 :       if (!Ais0) a4 = Fp_center_i(a4, N, Nover2);
    1474          90 :       if (!Bis0) a6 = Fp_center_i(a6, N, Nover2);
    1475          90 :       primo_printval(&ret, "A", a4);
    1476          90 :       primo_printval(&ret, "B", a6);
    1477          90 :       primo_printval(&ret, "T", cert_get_x(xi));
    1478             :     }
    1479         132 :     str_printf(&ret, "\n");
    1480             :   }
    1481          12 :   return strtoGENstr(ret.string);
    1482             : }
    1483             : 
    1484             : /* return 1 if q > (N^1/4 + 1)^2, 0 otherwise */
    1485             : static long
    1486         432 : Nq_isvalid(GEN N, GEN q)
    1487             : {
    1488         432 :   GEN c = subii(sqri(subiu(q,1)), N);
    1489         432 :   if (signe(c) <= 0) return 0;
    1490             :   /* (q-1)^2 > N; check that (N - (q-1)^2)^2 > 16Nq */
    1491         432 :   return (cmpii(sqri(c), shifti(mulii(N,q), 4)) > 0);
    1492             : }
    1493             : 
    1494             : GEN
    1495         438 : primecertisvalid_ecpp_worker(GEN certi)
    1496             : {
    1497             :   GEN N, W, mP, sP, r, m, s, a, P, q;
    1498         438 :   if (lg(certi)-1 != 5) return gen_0;
    1499             : 
    1500         438 :   N = cert_get_N(certi);
    1501         438 :   if (typ(N) != t_INT || signe(N) <= 0) return gen_0;
    1502         438 :   switch(umodiu(N,6))
    1503             :   {
    1504         432 :     case 1: case 5: break; /* Check if N is not divisible by 2 or 3 */
    1505           6 :     default: return gen_0;
    1506             :   }
    1507             : 
    1508         432 :   W = cert_get_t(certi);
    1509         432 :   if (typ(W) != t_INT) return gen_0;
    1510             :   /* Check if W^2 < 4N (Hasse interval) */
    1511         432 :   if (!(cmpii(sqri(W), shifti(N,2)) < 0)) return gen_0;
    1512             : 
    1513         432 :   s = cert_get_s(certi);
    1514         432 :   if (typ(s) != t_INT || signe(s) < 0) return gen_0;
    1515             : 
    1516         432 :   m = cert_get_m(certi);
    1517         432 :   q = dvmdii(m, s, &r);
    1518             :   /* Check m%s == 0 */
    1519         432 :   if (!isintzero(r)) return gen_0;
    1520             :   /* Check q > (N^(1/4) + 1)^2 */
    1521         432 :   if (!Nq_isvalid(N, q)) return gen_0;
    1522             : 
    1523         432 :   a = cert_get_a4(certi);
    1524         432 :   if (typ(a) != t_INT) return gen_0;
    1525             : 
    1526         432 :   P = cert_get_P(certi);
    1527         432 :   if (typ(P) != t_VEC || lg(P) != 3) return gen_0;
    1528         432 :   P = FpE_to_FpJ(P);
    1529             : 
    1530             :   /* Check sP != 0 and third component is coprime to N */
    1531         432 :   sP = FpJ_mul(P, s, a, N);
    1532         432 :   if (!isint1(gcdii(gel(sP, 3), N))) return gen_0;
    1533             : 
    1534             :   /* Check mP == 0 */
    1535         432 :   mP = FpJ_mul(sP, q, a, N);
    1536         432 :   if (!FpJ_is_inf(mP)) return gen_0;
    1537             : 
    1538         432 :   return q;
    1539             : }
    1540             : 
    1541             : /* return 1 if 'cert' is a valid PARI ECPP certificate */
    1542             : static long
    1543          30 : ecppisvalid_i(GEN cert)
    1544             : {
    1545          30 :   const long trustbits = 64;/* a pseudoprime < 2^trustbits is prime */
    1546          30 :   long i, lgcert = lg(cert);
    1547          30 :   GEN ql, q = gen_0, worker, check;
    1548             : 
    1549          30 :   if (typ(cert) == t_INT)
    1550             :   {
    1551           0 :     if (expi(cert) >= trustbits) return 0; /* >= 2^trustbits */
    1552           0 :     return BPSW_psp(cert);
    1553             :   }
    1554          30 :   if (typ(cert) != t_VEC || lgcert <= 1) return 0;
    1555          30 :   ql = veclast(cert); if (lg(ql)-1 != 5) return 0;
    1556          30 :   ql = cert_get_q(ql);
    1557          30 :   if (expi(ql) >= trustbits || !BPSW_psp(ql)) return 0;
    1558          30 :   worker = strtofunction("_primecertisvalid_ecpp_worker");
    1559          30 :   check = gen_parapply(worker, cert);
    1560         420 :   for (i = 1; i < lgcert; i++)
    1561             :   {
    1562         396 :     GEN certi = gel(cert, i);
    1563         396 :     GEN qq = gel(check,i), N = cert_get_N(certi);
    1564         396 :     if (isintzero(qq)) return 0;
    1565         390 :     if (i > 1 && !equalii(N, q)) return 0; /* N of this entry doesn't match q of previous */
    1566         390 :     q = qq;
    1567             :   }
    1568          24 :   return 1;
    1569             : }
    1570             : long
    1571          30 : ecppisvalid(GEN cert)
    1572          30 : { pari_sp av = avma; return gc_long(av, ecppisvalid_i(cert)); }
    1573             : 
    1574             : GEN
    1575          30 : ecppexport(GEN cert, long flag)
    1576             : {
    1577          30 :   switch(flag)
    1578             :   {
    1579           6 :     case 0: return cert_out(cert);
    1580          12 :     case 1: return primo_out(cert);
    1581          12 :     case 2: return magma_out(cert);
    1582             :   }
    1583           0 :   pari_err_FLAG("primecertexport");
    1584             :   return NULL;/*LCOV_EXCL_LINE*/
    1585             : }

Generated by: LCOV version 1.16