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 - polmodular.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 27775-aca467eab2) Lines: 2308 2378 97.1 %
Date: 2022-07-03 07:33:15 Functions: 143 143 100.0 %
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_polmodular
      19             : 
      20             : #define dbg_printf(lvl) if (DEBUGLEVEL >= (lvl) + 3) err_printf
      21             : 
      22             : /**
      23             :  * START Code from AVSs "class_inv.h"
      24             :  */
      25             : 
      26             : /* actually just returns the square-free part of the level, which is
      27             :  * all we care about */
      28             : long
      29       32338 : modinv_level(long inv)
      30             : {
      31       32338 :   switch (inv) {
      32       23767 :     case INV_J:     return 1;
      33        1316 :     case INV_G2:
      34        1316 :     case INV_W3W3E2:return 3;
      35        1098 :     case INV_F:
      36             :     case INV_F2:
      37             :     case INV_F4:
      38        1098 :     case INV_F8:    return 6;
      39          70 :     case INV_F3:    return 2;
      40         511 :     case INV_W3W3:  return 6;
      41        1596 :     case INV_W2W7E2:
      42        1596 :     case INV_W2W7:  return 14;
      43         269 :     case INV_W3W5:  return 15;
      44         301 :     case INV_W2W3E2:
      45         301 :     case INV_W2W3:  return 6;
      46         644 :     case INV_W2W5E2:
      47         644 :     case INV_W2W5:  return 30;
      48         441 :     case INV_W2W13: return 26;
      49        1725 :     case INV_W3W7:  return 42;
      50         544 :     case INV_W5W7:  return 35;
      51          56 :     case INV_W3W13: return 39;
      52             :   }
      53             :   pari_err_BUG("modinv_level"); return 0;/*LCOV_EXCL_LINE*/
      54             : }
      55             : 
      56             : /* Where applicable, returns N=p1*p2 (possibly p2=1) s.t. two j's
      57             :  * related to the same f are N-isogenous, and 0 otherwise.  This is
      58             :  * often (but not necessarily) equal to the level. */
      59             : long
      60     7242098 : modinv_degree(long *p1, long *p2, long inv)
      61             : {
      62     7242098 :   switch (inv) {
      63      297328 :     case INV_W3W5:  return (*p1 = 3) * (*p2 = 5);
      64      427322 :     case INV_W2W3E2:
      65      427322 :     case INV_W2W3:  return (*p1 = 2) * (*p2 = 3);
      66     1545180 :     case INV_W2W5E2:
      67     1545180 :     case INV_W2W5:  return (*p1 = 2) * (*p2 = 5);
      68      947778 :     case INV_W2W7E2:
      69      947778 :     case INV_W2W7:  return (*p1 = 2) * (*p2 = 7);
      70     1470077 :     case INV_W2W13: return (*p1 = 2) * (*p2 = 13);
      71      510561 :     case INV_W3W7:  return (*p1 = 3) * (*p2 = 7);
      72      782111 :     case INV_W3W3E2:
      73      782111 :     case INV_W3W3:  return (*p1 = 3) * (*p2 = 3);
      74      349389 :     case INV_W5W7:  return (*p1 = 5) * (*p2 = 7);
      75      195062 :     case INV_W3W13: return (*p1 = 3) * (*p2 = 13);
      76             :   }
      77      717290 :   *p1 = *p2 = 1; return 0;
      78             : }
      79             : 
      80             : /* Certain invariants require that D not have 2 in it's conductor, but
      81             :  * this doesn't apply to every invariant with even level so we handle
      82             :  * it separately */
      83             : INLINE int
      84      470433 : modinv_odd_conductor(long inv)
      85             : {
      86      470433 :   switch (inv) {
      87       70942 :     case INV_F:
      88             :     case INV_W3W3:
      89       70942 :     case INV_W3W7: return 1;
      90             :   }
      91      399491 :   return 0;
      92             : }
      93             : 
      94             : long
      95    22757862 : modinv_height_factor(long inv)
      96             : {
      97    22757862 :   switch (inv) {
      98     1879589 :     case INV_J:     return 1;
      99     1700216 :     case INV_G2:    return 3;
     100     3110886 :     case INV_F:     return 72;
     101          28 :     case INV_F2:    return 36;
     102      538020 :     case INV_F3:    return 24;
     103          49 :     case INV_F4:    return 18;
     104          49 :     case INV_F8:    return 9;
     105          63 :     case INV_W2W3:  return 72;
     106     2361072 :     case INV_W3W3:  return 36;
     107     3548202 :     case INV_W2W5:  return 54;
     108     1341537 :     case INV_W2W7:  return 48;
     109        1386 :     case INV_W3W5:  return 36;
     110     3892770 :     case INV_W2W13: return 42;
     111     1143429 :     case INV_W3W7:  return 32;
     112     1171891 :     case INV_W2W3E2:return 36;
     113      149184 :     case INV_W2W5E2:return 27;
     114     1081990 :     case INV_W2W7E2:return 24;
     115          49 :     case INV_W3W3E2:return 18;
     116      837438 :     case INV_W5W7:  return 24;
     117          14 :     case INV_W3W13: return 28;
     118             :     default: pari_err_BUG("modinv_height_factor"); return 0;/*LCOV_EXCL_LINE*/
     119             :   }
     120             : }
     121             : 
     122             : long
     123     1907423 : disc_best_modinv(long D)
     124             : {
     125             :   long ret;
     126     1907423 :   ret = INV_F;     if (modinv_good_disc(ret, D)) return ret;
     127     1534057 :   ret = INV_W2W3;  if (modinv_good_disc(ret, D)) return ret;
     128     1534057 :   ret = INV_W2W5;  if (modinv_good_disc(ret, D)) return ret;
     129     1238755 :   ret = INV_W2W7;  if (modinv_good_disc(ret, D)) return ret;
     130     1139957 :   ret = INV_W2W13; if (modinv_good_disc(ret, D)) return ret;
     131      838012 :   ret = INV_W3W3;  if (modinv_good_disc(ret, D)) return ret;
     132      651805 :   ret = INV_W2W3E2;if (modinv_good_disc(ret, D)) return ret;
     133      579453 :   ret = INV_W3W5;  if (modinv_good_disc(ret, D)) return ret;
     134      579299 :   ret = INV_W3W7;  if (modinv_good_disc(ret, D)) return ret;
     135      511091 :   ret = INV_W3W13; if (modinv_good_disc(ret, D)) return ret;
     136      511091 :   ret = INV_W2W5E2;if (modinv_good_disc(ret, D)) return ret;
     137      494753 :   ret = INV_F3;    if (modinv_good_disc(ret, D)) return ret;
     138      464485 :   ret = INV_W2W7E2;if (modinv_good_disc(ret, D)) return ret;
     139      376656 :   ret = INV_W5W7;  if (modinv_good_disc(ret, D)) return ret;
     140      308581 :   ret = INV_W3W3E2;if (modinv_good_disc(ret, D)) return ret;
     141      308581 :   ret = INV_G2;    if (modinv_good_disc(ret, D)) return ret;
     142      160517 :   return INV_J;
     143             : }
     144             : 
     145             : INLINE long
     146       39403 : modinv_sparse_factor(long inv)
     147             : {
     148       39403 :   switch (inv) {
     149        4855 :   case INV_G2:
     150             :   case INV_F8:
     151             :   case INV_W3W5:
     152             :   case INV_W2W5E2:
     153             :   case INV_W3W3E2:
     154        4855 :     return 3;
     155         625 :   case INV_F:
     156         625 :     return 24;
     157         357 :   case INV_F2:
     158             :   case INV_W2W3:
     159         357 :     return 12;
     160         112 :   case INV_F3:
     161         112 :     return 8;
     162        1645 :   case INV_F4:
     163             :   case INV_W2W3E2:
     164             :   case INV_W2W5:
     165             :   case INV_W3W3:
     166        1645 :     return 6;
     167        1046 :   case INV_W2W7:
     168        1046 :     return 4;
     169        2951 :   case INV_W2W7E2:
     170             :   case INV_W2W13:
     171             :   case INV_W3W7:
     172        2951 :     return 2;
     173             :   }
     174       27812 :   return 1;
     175             : }
     176             : 
     177             : #define IQ_FILTER_1MOD3 1
     178             : #define IQ_FILTER_2MOD3 2
     179             : #define IQ_FILTER_1MOD4 4
     180             : #define IQ_FILTER_3MOD4 8
     181             : 
     182             : INLINE long
     183       12824 : modinv_pfilter(long inv)
     184             : {
     185       12824 :   switch (inv) {
     186        2838 :   case INV_G2:
     187             :   case INV_W3W3:
     188             :   case INV_W3W3E2:
     189             :   case INV_W3W5:
     190             :   case INV_W2W5:
     191             :   case INV_W2W3E2:
     192             :   case INV_W2W5E2:
     193             :   case INV_W5W7:
     194             :   case INV_W3W13:
     195        2838 :     return IQ_FILTER_1MOD3; /* ensure unique cube roots */
     196         529 :   case INV_W2W7:
     197             :   case INV_F3:
     198         529 :     return IQ_FILTER_1MOD4; /* ensure at most two 4th/8th roots */
     199         972 :   case INV_F:
     200             :   case INV_F2:
     201             :   case INV_F4:
     202             :   case INV_F8:
     203             :   case INV_W2W3:
     204             :     /* Ensure unique cube roots and at most two 4th/8th roots */
     205         972 :     return IQ_FILTER_1MOD3 | IQ_FILTER_1MOD4;
     206             :   }
     207        8485 :   return 0;
     208             : }
     209             : 
     210             : int
     211     6890458 : modinv_good_prime(long inv, long p)
     212             : {
     213     6890458 :   switch (inv) {
     214      296644 :   case INV_G2:
     215             :   case INV_W2W3E2:
     216             :   case INV_W3W3:
     217             :   case INV_W3W3E2:
     218             :   case INV_W3W5:
     219             :   case INV_W2W5E2:
     220             :   case INV_W2W5:
     221      296644 :     return (p % 3) == 2;
     222      254190 :   case INV_W2W7:
     223             :   case INV_F3:
     224      254190 :     return (p & 3) != 1;
     225      309605 :   case INV_F2:
     226             :   case INV_F4:
     227             :   case INV_F8:
     228             :   case INV_F:
     229             :   case INV_W2W3:
     230      309605 :     return ((p % 3) == 2) && (p & 3) != 1;
     231             :   }
     232     6030019 :   return 1;
     233             : }
     234             : 
     235             : /* Returns true if the prime p does not divide the conductor of D */
     236             : INLINE int
     237     3257079 : prime_to_conductor(long D, long p)
     238             : {
     239             :   long b;
     240     3257079 :   if (p > 2) return (D % (p * p));
     241     1250577 :   b = D & 0xF;
     242     1250577 :   return (b && b != 4); /* 2 divides the conductor of D <=> D=0,4 mod 16 */
     243             : }
     244             : 
     245             : INLINE GEN
     246     3257079 : red_primeform(long D, long p)
     247             : {
     248     3257079 :   pari_sp av = avma;
     249             :   GEN P;
     250     3257079 :   if (!prime_to_conductor(D, p)) return NULL;
     251     3257079 :   P = primeform_u(stoi(D), p); /* primitive since p \nmid conductor */
     252     3257079 :   return gerepileupto(av, qfbred_i(P));
     253             : }
     254             : 
     255             : /* Computes product of primeforms over primes appearing in the prime
     256             :  * factorization of n (including multiplicity) */
     257             : GEN
     258      135737 : qfb_nform(long D, long n)
     259             : {
     260      135737 :   pari_sp av = avma;
     261      135737 :   GEN N = NULL, fa = factoru(n), P = gel(fa,1), E = gel(fa,2);
     262      135737 :   long i, l = lg(P);
     263             : 
     264      407127 :   for (i = 1; i < l; ++i)
     265             :   {
     266             :     long j, e;
     267      271390 :     GEN Q = red_primeform(D, P[i]);
     268      271390 :     if (!Q) return gc_NULL(av);
     269      271390 :     e = E[i];
     270      271390 :     if (i == 1) { N = Q; j = 1; } else j = 0;
     271      407127 :     for (; j < e; ++j) N = qfbcomp_i(Q, N);
     272             :   }
     273      135737 :   return gerepileupto(av, N);
     274             : }
     275             : 
     276             : INLINE int
     277     1688036 : qfb_is_two_torsion(GEN x)
     278             : {
     279     3376072 :   return equali1(gel(x,1)) || !signe(gel(x,2))
     280     3376072 :     || equalii(gel(x,1), gel(x,2)) || equalii(gel(x,1), gel(x,3));
     281             : }
     282             : 
     283             : /* Returns true iff the products p1*p2, p1*p2^-1, p1^-1*p2, and
     284             :  * p1^-1*p2^-1 are all distinct in cl(D) */
     285             : INLINE int
     286      232590 : qfb_distinct_prods(long D, long p1, long p2)
     287             : {
     288             :   GEN P1, P2;
     289             : 
     290      232590 :   P1 = red_primeform(D, p1);
     291      232590 :   if (!P1) return 0;
     292      232590 :   P1 = qfbsqr_i(P1);
     293             : 
     294      232590 :   P2 = red_primeform(D, p2);
     295      232590 :   if (!P2) return 0;
     296      232590 :   P2 = qfbsqr_i(P2);
     297             : 
     298      232590 :   return !(equalii(gel(P1,1), gel(P2,1)) && absequalii(gel(P1,2), gel(P2,2)));
     299             : }
     300             : 
     301             : /* By Corollary 3.1 of Enge-Schertz Constructing elliptic curves over finite
     302             :  * fields using double eta-quotients, we need p1 != p2 to both be noninert
     303             :  * and prime to the conductor, and if p1=p2=p we want p split and prime to the
     304             :  * conductor. We exclude the case that p1=p2 divides the conductor, even
     305             :  * though this does yield class invariants */
     306             : INLINE int
     307     5315161 : modinv_double_eta_good_disc(long D, long inv)
     308             : {
     309     5315161 :   pari_sp av = avma;
     310             :   GEN P;
     311             :   long i1, i2, p1, p2, N;
     312             : 
     313     5315161 :   N = modinv_degree(&p1, &p2, inv);
     314     5315161 :   if (! N) return 0;
     315     5315161 :   i1 = kross(D, p1);
     316     5315161 :   if (i1 < 0) return 0;
     317             :   /* Exclude ramified case for w_{p,p} */
     318     2407366 :   if (p1 == p2 && !i1) return 0;
     319     2407366 :   i2 = kross(D, p2);
     320     2407366 :   if (i2 < 0) return 0;
     321             :   /* this also verifies that p1 is prime to the conductor */
     322     1371341 :   P = red_primeform(D, p1);
     323     1371341 :   if (!P || gequal1(gel(P,1)) /* don't allow p1 to be principal */
     324             :       /* if p1 is unramified, require it to have order > 2 */
     325     1371341 :       || (i1 && qfb_is_two_torsion(P))) return gc_bool(av,0);
     326     1369717 :   if (p1 == p2) /* if p1=p2 we need p1*p1 to be distinct from its inverse */
     327      220549 :     return gc_bool(av, !qfb_is_two_torsion(qfbsqr_i(P)));
     328             : 
     329             :   /* this also verifies that p2 is prime to the conductor */
     330     1149168 :   P = red_primeform(D, p2);
     331     1149168 :   if (!P || gequal1(gel(P,1)) /* don't allow p2 to be principal */
     332             :       /* if p2 is unramified, require it to have order > 2 */
     333     1149168 :       || (i2 && qfb_is_two_torsion(P))) return gc_bool(av,0);
     334     1147705 :   set_avma(av);
     335             : 
     336             :   /* if p1 and p2 are split, we also require p1*p2, p1*p2^-1, p1^-1*p2,
     337             :    * and p1^-1*p2^-1 to be distinct */
     338     1147705 :   if (i1>0 && i2>0 && !qfb_distinct_prods(D, p1, p2)) return gc_bool(av,0);
     339     1144589 :   if (!i1 && !i2) {
     340             :     /* if both p1 and p2 are ramified, make sure their product is not
     341             :      * principal */
     342      135359 :     P = qfb_nform(D, N);
     343      135359 :     if (equali1(gel(P,1))) return gc_bool(av,0);
     344      135107 :     set_avma(av);
     345             :   }
     346     1144337 :   return 1;
     347             : }
     348             : 
     349             : /* Assumes D is a good discriminant for inv, which implies that the
     350             :  * level is prime to the conductor */
     351             : long
     352         504 : modinv_ramified(long D, long inv, long *pN)
     353             : {
     354         504 :   long p1, p2; *pN = modinv_degree(&p1, &p2, inv);
     355         504 :   if (*pN <= 1) return 0;
     356         504 :   return !(D % p1) && !(D % p2);
     357             : }
     358             : 
     359             : int
     360    14679580 : modinv_good_disc(long inv, long D)
     361             : {
     362    14679580 :   switch (inv) {
     363      716496 :   case INV_J:
     364      716496 :     return 1;
     365      463617 :   case INV_G2:
     366      463617 :     return !!(D % 3);
     367      502845 :   case INV_F3:
     368      502845 :     return (-D & 7) == 7;
     369     2062667 :   case INV_F:
     370             :   case INV_F2:
     371             :   case INV_F4:
     372             :   case INV_F8:
     373     2062667 :     return ((-D & 7) == 7) && (D % 3);
     374      622069 :   case INV_W3W5:
     375      622069 :     return (D % 3) && modinv_double_eta_good_disc(D, inv);
     376      335664 :   case INV_W3W3E2:
     377      335664 :     return (D % 3) && modinv_double_eta_good_disc(D, inv);
     378      897015 :   case INV_W3W3:
     379      897015 :     return (D & 1) && (D % 3) && modinv_double_eta_good_disc(D, inv);
     380      667688 :   case INV_W2W3E2:
     381      667688 :     return (D % 3) && modinv_double_eta_good_disc(D, inv);
     382     1554721 :   case INV_W2W3:
     383     1554721 :     return ((-D & 7) == 7) && (D % 3) && modinv_double_eta_good_disc(D, inv);
     384     1581685 :   case INV_W2W5:
     385     1581685 :     return ((-D % 80) != 20) && (D % 3) && modinv_double_eta_good_disc(D, inv);
     386      549171 :   case INV_W2W5E2:
     387      549171 :     return (D % 3) && modinv_double_eta_good_disc(D, inv);
     388      566027 :   case INV_W2W7E2:
     389      566027 :     return ((-D % 112) != 84) && modinv_double_eta_good_disc(D, inv);
     390     1324607 :   case INV_W2W7:
     391     1324607 :     return ((-D & 7) == 7) && modinv_double_eta_good_disc(D, inv);
     392     1196839 :   case INV_W2W13:
     393     1196839 :     return ((-D % 208) != 52) && modinv_double_eta_good_disc(D, inv);
     394      666806 :   case INV_W3W7:
     395      666806 :     return (D & 1) && (-D % 21) && modinv_double_eta_good_disc(D, inv);
     396      450975 :   case INV_W5W7: /* NB: This is a guess; avs doesn't have an entry */
     397      450975 :     return (D % 3) && modinv_double_eta_good_disc(D, inv);
     398      520688 :   case INV_W3W13: /* NB: This is a guess; avs doesn't have an entry */
     399      520688 :     return (D & 1) && (D % 3) && modinv_double_eta_good_disc(D, inv);
     400             :   }
     401           0 :   pari_err_BUG("modinv_good_discriminant");
     402             :   return 0;/*LCOV_EXCL_LINE*/
     403             : }
     404             : 
     405             : int
     406         742 : modinv_is_Weber(long inv)
     407             : {
     408           0 :   return inv == INV_F || inv == INV_F2 || inv == INV_F3 || inv == INV_F4
     409         742 :     || inv == INV_F8;
     410             : }
     411             : 
     412             : int
     413      207147 : modinv_is_double_eta(long inv)
     414             : {
     415      207147 :   switch (inv) {
     416       31275 :   case INV_W2W3:
     417             :   case INV_W2W3E2:
     418             :   case INV_W2W5:
     419             :   case INV_W2W5E2:
     420             :   case INV_W2W7:
     421             :   case INV_W2W7E2:
     422             :   case INV_W2W13:
     423             :   case INV_W3W3:
     424             :   case INV_W3W3E2:
     425             :   case INV_W3W5:
     426             :   case INV_W3W7:
     427             :   case INV_W5W7:
     428             :   case INV_W3W13:
     429       31275 :     return 1;
     430             :   }
     431      175872 :   return 0;
     432             : }
     433             : 
     434             : /* END Code from "class_inv.h" */
     435             : 
     436             : INLINE int
     437        9834 : safe_abs_sqrt(ulong *r, ulong x, ulong p, ulong pi, ulong s2)
     438             : {
     439        9834 :   if (krouu(x, p) == -1)
     440             :   {
     441        4454 :     if (p%4 == 1) return 0;
     442        4454 :     x = Fl_neg(x, p);
     443             :   }
     444        9834 :   *r = Fl_sqrt_pre_i(x, s2, p, pi);
     445        9834 :   return 1;
     446             : }
     447             : 
     448             : INLINE int
     449        4867 : eighth_root(ulong *r, ulong x, ulong p, ulong pi, ulong s2)
     450             : {
     451             :   ulong s;
     452        4867 :   if (krouu(x, p) == -1) return 0;
     453        2644 :   s = Fl_sqrt_pre_i(x, s2, p, pi);
     454        2644 :   return safe_abs_sqrt(&s, s, p, pi, s2) && safe_abs_sqrt(r, s, p, pi, s2);
     455             : }
     456             : 
     457             : INLINE ulong
     458        2903 : modinv_f_from_j(ulong j, ulong p, ulong pi, ulong s2, long only_residue)
     459             : {
     460        2903 :   pari_sp av = avma;
     461             :   GEN pol, r;
     462             :   long i;
     463        2903 :   ulong g2, f = ULONG_MAX;
     464             : 
     465             :   /* f^8 must be a root of X^3 - \gamma_2 X - 16 */
     466        2903 :   g2 = Fl_sqrtl_pre(j, 3, p, pi);
     467             : 
     468        2903 :   pol = mkvecsmall5(0UL, Fl_neg(16 % p, p), Fl_neg(g2, p), 0UL, 1UL);
     469        2903 :   r = Flx_roots(pol, p);
     470        5332 :   for (i = 1; i < lg(r); ++i)
     471        5332 :     if (only_residue)
     472        1178 :     { if (krouu(r[i], p) != -1) return gc_ulong(av,r[i]); }
     473        4154 :     else if (eighth_root(&f, r[i], p, pi, s2)) return gc_ulong(av,f);
     474           0 :   pari_err_BUG("modinv_f_from_j");
     475             :   return 0;/*LCOV_EXCL_LINE*/
     476             : }
     477             : 
     478             : INLINE ulong
     479         357 : modinv_f3_from_j(ulong j, ulong p, ulong pi, ulong s2)
     480             : {
     481         357 :   pari_sp av = avma;
     482             :   GEN pol, r;
     483             :   long i;
     484         357 :   ulong f = ULONG_MAX;
     485             : 
     486         357 :   pol = mkvecsmall5(0UL,
     487         357 :       Fl_neg(4096 % p, p), Fl_sub(768 % p, j, p), Fl_neg(48 % p, p), 1UL);
     488         357 :   r = Flx_roots(pol, p);
     489         713 :   for (i = 1; i < lg(r); ++i)
     490         713 :     if (eighth_root(&f, r[i], p, pi, s2)) return gc_ulong(av,f);
     491           0 :   pari_err_BUG("modinv_f3_from_j");
     492             :   return 0;/*LCOV_EXCL_LINE*/
     493             : }
     494             : 
     495             : /* Return the exponent e for the double-eta "invariant" w such that
     496             :  * w^e is a class invariant.  For example w2w3^12 is a class
     497             :  * invariant, so double_eta_exponent(INV_W2W3) is 12 and
     498             :  * double_eta_exponent(INV_W2W3E2) is 6. */
     499             : INLINE ulong
     500       54061 : double_eta_exponent(long inv)
     501             : {
     502       54061 :   switch (inv) {
     503        2506 :   case INV_W2W3: return 12;
     504       13228 :   case INV_W2W3E2:
     505             :   case INV_W2W5:
     506       13228 :   case INV_W3W3: return 6;
     507        9835 :   case INV_W2W7: return 4;
     508        5771 :   case INV_W3W5:
     509             :   case INV_W2W5E2:
     510        5771 :   case INV_W3W3E2: return 3;
     511       15511 :   case INV_W2W7E2:
     512             :   case INV_W2W13:
     513       15511 :   case INV_W3W7: return 2;
     514        7210 :   default: return 1;
     515             :   }
     516             : }
     517             : 
     518             : INLINE ulong
     519          49 : weber_exponent(long inv)
     520             : {
     521          49 :   switch (inv)
     522             :   {
     523          42 :   case INV_F:  return 24;
     524           0 :   case INV_F2: return 12;
     525           7 :   case INV_F3: return 8;
     526           0 :   case INV_F4: return 6;
     527           0 :   case INV_F8: return 3;
     528           0 :   default:     return 1;
     529             :   }
     530             : }
     531             : 
     532             : INLINE ulong
     533       29509 : double_eta_power(long inv, ulong w, ulong p, ulong pi)
     534             : {
     535       29509 :   return Fl_powu_pre(w, double_eta_exponent(inv), p, pi);
     536             : }
     537             : 
     538             : static GEN
     539         161 : double_eta_raw_to_Fp(GEN f, GEN p)
     540             : {
     541         161 :   GEN u = FpX_red(RgV_to_RgX(gel(f,1), 0), p);
     542         161 :   GEN v = FpX_red(RgV_to_RgX(gel(f,2), 0), p);
     543         161 :   return mkvec3(u, v, gel(f,3));
     544             : }
     545             : 
     546             : /* Given a root x of polclass(D, inv) modulo N, returns a root of polclass(D,0)
     547             :  * modulo N by plugging x to a modular polynomial. For double-eta quotients,
     548             :  * this is done by plugging x into the modular polynomial Phi(INV_WpWq, j)
     549             :  * Enge, Morain 2013: Generalised Weber Functions. */
     550             : GEN
     551        1057 : Fp_modinv_to_j(GEN x, long inv, GEN p)
     552             : {
     553        1057 :   switch(inv)
     554             :   {
     555         392 :     case INV_J: return Fp_red(x, p);
     556         455 :     case INV_G2: return Fp_powu(x, 3, p);
     557          49 :     case INV_F: case INV_F2: case INV_F3: case INV_F4: case INV_F8:
     558             :     {
     559          49 :       GEN xe = Fp_powu(x, weber_exponent(inv), p);
     560          49 :       return Fp_div(Fp_powu(subiu(xe, 16), 3, p), xe, p);
     561             :     }
     562         161 :     default:
     563         161 :     if (modinv_is_double_eta(inv))
     564             :     {
     565         161 :       GEN xe = Fp_powu(x, double_eta_exponent(inv), p);
     566         161 :       GEN uvk = double_eta_raw_to_Fp(double_eta_raw(inv), p);
     567         161 :       GEN J0 = FpX_eval(gel(uvk,1), xe, p);
     568         161 :       GEN J1 = FpX_eval(gel(uvk,2), xe, p);
     569         161 :       GEN J2 = Fp_pow(xe, gel(uvk,3), p);
     570         161 :       GEN phi = mkvec3(J0, J1, J2);
     571         161 :       return FpX_oneroot(RgX_to_FpX(RgV_to_RgX(phi,1), p),p);
     572             :     }
     573             :     pari_err_BUG("Fp_modinv_to_j"); return NULL;/* LCOV_EXCL_LINE */
     574             :   }
     575             : }
     576             : 
     577             : /* Assuming p = 2 (mod 3) and p = 3 (mod 4): if the two 12th roots of
     578             :  * x (mod p) exist, set *r to one of them and return 1, otherwise
     579             :  * return 0 (without touching *r). */
     580             : INLINE int
     581         917 : twelth_root(ulong *r, ulong x, ulong p, ulong pi, ulong s2)
     582             : {
     583         917 :   ulong t = Fl_sqrtl_pre(x, 3, p, pi);
     584         917 :   if (krouu(t, p) == -1) return 0;
     585         868 :   t = Fl_sqrt_pre_i(t, s2, p, pi);
     586         868 :   return safe_abs_sqrt(r, t, p, pi, s2);
     587             : }
     588             : 
     589             : INLINE int
     590        5486 : sixth_root(ulong *r, ulong x, ulong p, ulong pi, ulong s2)
     591             : {
     592        5486 :   ulong t = Fl_sqrtl_pre(x, 3, p, pi);
     593        5486 :   if (krouu(t, p) == -1) return 0;
     594        5299 :   *r = Fl_sqrt_pre_i(t, s2, p, pi);
     595        5299 :   return 1;
     596             : }
     597             : 
     598             : INLINE int
     599        3917 : fourth_root(ulong *r, ulong x, ulong p, ulong pi, ulong s2)
     600             : {
     601             :   ulong s;
     602        3917 :   if (krouu(x, p) == -1) return 0;
     603        3678 :   s = Fl_sqrt_pre_i(x, s2, p, pi);
     604        3678 :   return safe_abs_sqrt(r, s, p, pi, s2);
     605             : }
     606             : 
     607             : INLINE int
     608       24394 : double_eta_root(long inv, ulong *r, ulong w, ulong p, ulong pi, ulong s2)
     609             : {
     610       24394 :   switch (double_eta_exponent(inv)) {
     611         917 :   case 12: return twelth_root(r, w, p, pi, s2);
     612        5486 :   case 6: return sixth_root(r, w, p, pi, s2);
     613        3917 :   case 4: return fourth_root(r, w, p, pi, s2);
     614        2591 :   case 3: *r = Fl_sqrtl_pre(w, 3, p, pi); return 1;
     615        8408 :   case 2: return krouu(w, p) != -1 && !!(*r = Fl_sqrt_pre_i(w, s2, p, pi));
     616        3075 :   default: *r = w; return 1; /* case 1 */
     617             :   }
     618             : }
     619             : 
     620             : /* F = double_eta_Fl(inv, p) */
     621             : static GEN
     622       41462 : Flx_double_eta_xpoly(GEN F, ulong j, ulong p, ulong pi)
     623             : {
     624       41462 :   GEN u = gel(F,1), v = gel(F,2), w;
     625       41462 :   long i, k = itos(gel(F,3)), lu = lg(u), lv = lg(v), lw = lu + 1;
     626             : 
     627       41462 :   w = cgetg(lw, t_VECSMALL); /* lu >= max(lv,k) */
     628       41461 :   w[1] = 0; /* variable number */
     629     1161080 :   for (i = 1; i < lv; i++) uel(w, i+1) = Fl_add(uel(u,i), Fl_mul_pre(j, uel(v,i), p, pi), p);
     630       82926 :   for (     ; i < lu; i++) uel(w, i+1) = uel(u,i);
     631       41463 :   uel(w, k+2) = Fl_add(uel(w, k+2), Fl_sqr_pre(j, p, pi), p);
     632       41463 :   return Flx_renormalize(w, lw);
     633             : }
     634             : 
     635             : /* F = double_eta_Fl(inv, p) */
     636             : static GEN
     637       29509 : Flx_double_eta_jpoly(GEN F, ulong x, ulong p, ulong pi)
     638             : {
     639       29509 :   pari_sp av = avma;
     640       29509 :   GEN u = gel(F,1), v = gel(F,2), xs;
     641       29509 :   long k = itos(gel(F,3));
     642             :   ulong a, b, c;
     643             : 
     644             :   /* u is always longest and the length is bigger than k */
     645       29509 :   xs = Fl_powers_pre(x, lg(u) - 1, p, pi);
     646       29509 :   c = Flv_dotproduct_pre(u, xs, p, pi);
     647       29509 :   b = Flv_dotproduct_pre(v, xs, p, pi);
     648       29509 :   a = uel(xs, k + 1);
     649       29509 :   set_avma(av);
     650       29509 :   return mkvecsmall4(0, c, b, a);
     651             : }
     652             : 
     653             : /* reduce F = double_eta_raw(inv) mod p */
     654             : static GEN
     655       29789 : double_eta_raw_to_Fl(GEN f, ulong p)
     656             : {
     657       29789 :   GEN u = ZV_to_Flv(gel(f,1), p);
     658       29789 :   GEN v = ZV_to_Flv(gel(f,2), p);
     659       29789 :   return mkvec3(u, v, gel(f,3));
     660             : }
     661             : /* double_eta_raw(inv) mod p */
     662             : static GEN
     663       23580 : double_eta_Fl(long inv, ulong p)
     664       23580 : { return double_eta_raw_to_Fl(double_eta_raw(inv), p); }
     665             : 
     666             : /* Go through roots of Psi(X,j) until one has an double_eta_exponent(inv)-th
     667             :  * root, and return that root. F = double_eta_Fl(inv,p) */
     668             : INLINE ulong
     669        5697 : modinv_double_eta_from_j(GEN F, long inv, ulong j, ulong p, ulong pi, ulong s2)
     670             : {
     671        5697 :   pari_sp av = avma;
     672             :   long i;
     673        5697 :   ulong f = ULONG_MAX;
     674        5697 :   GEN a = Flx_double_eta_xpoly(F, j, p, pi);
     675        5697 :   a = Flx_roots(a, p);
     676        6511 :   for (i = 1; i < lg(a); ++i)
     677        6511 :     if (double_eta_root(inv, &f, uel(a, i), p, pi, s2)) break;
     678        5697 :   if (i == lg(a)) pari_err_BUG("modinv_double_eta_from_j");
     679        5697 :   return gc_ulong(av,f);
     680             : }
     681             : 
     682             : /* assume j1 != j2 */
     683             : static long
     684       12186 : modinv_double_eta_from_2j(
     685             :   ulong *r, long inv, ulong j1, ulong j2, ulong p, ulong pi, ulong s2)
     686             : {
     687       12186 :   GEN f, g, d, F = double_eta_Fl(inv, p);
     688       12186 :   f = Flx_double_eta_xpoly(F, j1, p, pi);
     689       12186 :   g = Flx_double_eta_xpoly(F, j2, p, pi);
     690       12186 :   d = Flx_gcd(f, g, p);
     691             :   /* we should have deg(d) = 1, but because j1 or j2 may not have the correct
     692             :    * endomorphism ring, we use the less strict conditional underneath */
     693       24372 :   return (degpol(d) > 2 || (*r = Flx_oneroot(d, p)) == p
     694       24372 :           || ! double_eta_root(inv, r, *r, p, pi, s2));
     695             : }
     696             : 
     697             : long
     698       12280 : modfn_unambiguous_root(ulong *r, long inv, ulong j0, norm_eqn_t ne, GEN jdb)
     699             : {
     700       12280 :   pari_sp av = avma;
     701       12280 :   long p1, p2, v = ne->v, p1_depth;
     702       12280 :   ulong j1, p = ne->p, pi = ne->pi, s2 = ne->s2;
     703             :   GEN phi;
     704             : 
     705       12280 :   (void) modinv_degree(&p1, &p2, inv);
     706       12280 :   p1_depth = u_lval(v, p1);
     707             : 
     708       12280 :   phi = polmodular_db_getp(jdb, p1, p);
     709       12280 :   if (!next_surface_nbr(&j1, phi, p1, p1_depth, j0, NULL, p, pi))
     710           0 :     pari_err_BUG("modfn_unambiguous_root");
     711       12280 :   if (p2 == p1) {
     712        1989 :     if (!next_surface_nbr(&j1, phi, p1, p1_depth, j1, &j0, p, pi))
     713           0 :       pari_err_BUG("modfn_unambiguous_root");
     714             :   } else {
     715       10291 :     long p2_depth = u_lval(v, p2);
     716       10291 :     phi = polmodular_db_getp(jdb, p2, p);
     717       10291 :     if (!next_surface_nbr(&j1, phi, p2, p2_depth, j1, NULL, p, pi))
     718           0 :       pari_err_BUG("modfn_unambiguous_root");
     719             :   }
     720       14015 :   return gc_long(av, j1 != j0
     721       12265 :                      && !modinv_double_eta_from_2j(r, inv, j0, j1, p, pi, s2));
     722             : }
     723             : 
     724             : ulong
     725      166828 : modfn_root(ulong j, norm_eqn_t ne, long inv)
     726             : {
     727      166828 :   ulong f, p = ne->p, pi = ne->pi, s2 = ne->s2;
     728      166828 :   switch (inv) {
     729      155548 :     case INV_J:  return j;
     730        8020 :     case INV_G2: return Fl_sqrtl_pre(j, 3, p, pi);
     731        1538 :     case INV_F:  return modinv_f_from_j(j, p, pi, s2, 0);
     732         196 :     case INV_F2:
     733         196 :       f = modinv_f_from_j(j, p, pi, s2, 0);
     734         196 :       return Fl_sqr_pre(f, p, pi);
     735         357 :     case INV_F3: return modinv_f3_from_j(j, p, pi, s2);
     736         553 :     case INV_F4:
     737         553 :       f = modinv_f_from_j(j, p, pi, s2, 0);
     738         553 :       return Fl_sqr_pre(Fl_sqr_pre(f, p, pi), p, pi);
     739         616 :     case INV_F8: return modinv_f_from_j(j, p, pi, s2, 1);
     740             :   }
     741           0 :   if (modinv_is_double_eta(inv))
     742             :   {
     743           0 :     pari_sp av = avma;
     744           0 :     ulong f = modinv_double_eta_from_j(double_eta_Fl(inv,p), inv, j, p, pi, s2);
     745           0 :     return gc_ulong(av,f);
     746             :   }
     747             :   pari_err_BUG("modfn_root"); return ULONG_MAX;/*LCOV_EXCL_LINE*/
     748             : }
     749             : 
     750             : /* F = double_eta_raw(inv) */
     751             : long
     752        6209 : modinv_j_from_2double_eta(
     753             :   GEN F, long inv, ulong x0, ulong x1, ulong p, ulong pi)
     754             : {
     755             :   GEN f, g, d;
     756             : 
     757        6209 :   x0 = double_eta_power(inv, x0, p, pi);
     758        6209 :   x1 = double_eta_power(inv, x1, p, pi);
     759        6209 :   F = double_eta_raw_to_Fl(F, p);
     760        6209 :   f = Flx_double_eta_jpoly(F, x0, p, pi);
     761        6209 :   g = Flx_double_eta_jpoly(F, x1, p, pi);
     762        6209 :   d = Flx_gcd(f, g, p); /* >= 1 */
     763        6209 :   return degpol(d) == 1;
     764             : }
     765             : 
     766             : /* x root of (X^24 - 16)^3 - X^24 * j = 0 => j = (x^24 - 16)^3 / x^24 */
     767             : INLINE ulong
     768        1858 : modinv_j_from_f(ulong x, ulong n, ulong p, ulong pi)
     769             : {
     770        1858 :   ulong x24 = Fl_powu_pre(x, 24 / n, p, pi);
     771        1858 :   return Fl_div(Fl_powu_pre(Fl_sub(x24, 16 % p, p), 3, p, pi), x24, p);
     772             : }
     773             : /* should never be called if modinv_double_eta(inv) is true */
     774             : INLINE ulong
     775       56590 : modfn_preimage(ulong x, norm_eqn_t ne, long inv)
     776             : {
     777       56590 :   ulong p = ne->p, pi = ne->pi;
     778       56590 :   switch (inv) {
     779       48874 :     case INV_J:  return x;
     780        5858 :     case INV_G2: return Fl_powu_pre(x, 3, p, pi);
     781             :     /* NB: could replace these with a single call modinv_j_from_f(x,inv,p,pi)
     782             :      * but avoid the dependence on the actual value of inv */
     783         654 :     case INV_F:  return modinv_j_from_f(x, 1, p, pi);
     784         196 :     case INV_F2: return modinv_j_from_f(x, 2, p, pi);
     785         168 :     case INV_F3: return modinv_j_from_f(x, 3, p, pi);
     786         392 :     case INV_F4: return modinv_j_from_f(x, 4, p, pi);
     787         448 :     case INV_F8: return modinv_j_from_f(x, 8, p, pi);
     788             :   }
     789             :   pari_err_BUG("modfn_preimage"); return ULONG_MAX;/*LCOV_EXCL_LINE*/
     790             : }
     791             : 
     792             : /* SECTION: class group bb_group. */
     793             : 
     794             : INLINE GEN
     795      114996 : mkqfis(GEN a, ulong b, ulong c, GEN D) { retmkqfb(a, utoi(b), utoi(c), D); }
     796             : 
     797             : /* SECTION: dot-product-like functions on Fl's with precomputed inverse. */
     798             : 
     799             : /* Computes x0y1 + y0x1 (mod p); assumes p < 2^63. */
     800             : INLINE ulong
     801    52637208 : Fl_addmul2(
     802             :   ulong x0, ulong x1, ulong y0, ulong y1,
     803             :   ulong p, ulong pi)
     804             : {
     805    52637208 :   return Fl_addmulmul_pre(x0,y1,y0,x1,p,pi);
     806             : }
     807             : 
     808             : /* Computes x0y2 + x1y1 + x2y0 (mod p); assumes p < 2^62. */
     809             : INLINE ulong
     810     8864852 : Fl_addmul3(
     811             :   ulong x0, ulong x1, ulong x2, ulong y0, ulong y1, ulong y2,
     812             :   ulong p, ulong pi)
     813             : {
     814             :   ulong l0, l1, h0, h1;
     815             :   LOCAL_OVERFLOW;
     816             :   LOCAL_HIREMAINDER;
     817     8864852 :   l0 = mulll(x0, y2); h0 = hiremainder;
     818     8864852 :   l1 = mulll(x1, y1); h1 = hiremainder;
     819     8864852 :   l1 = addll(l0, l1); h1 = addllx(h0, h1);
     820     8864852 :   l0 = mulll(x2, y0); h0 = hiremainder;
     821     8864852 :   l1 = addll(l0, l1); h1 = addllx(h0, h1);
     822     8864852 :   return remll_pre(h1, l1, p, pi);
     823             : }
     824             : 
     825             : /* Computes x0y3 + x1y2 + x2y1 + x3y0 (mod p); assumes p < 2^62. */
     826             : INLINE ulong
     827     4881533 : Fl_addmul4(
     828             :   ulong x0, ulong x1, ulong x2, ulong x3,
     829             :   ulong y0, ulong y1, ulong y2, ulong y3,
     830             :   ulong p, ulong pi)
     831             : {
     832             :   ulong l0, l1, h0, h1;
     833             :   LOCAL_OVERFLOW;
     834             :   LOCAL_HIREMAINDER;
     835     4881533 :   l0 = mulll(x0, y3); h0 = hiremainder;
     836     4881533 :   l1 = mulll(x1, y2); h1 = hiremainder;
     837     4881533 :   l1 = addll(l0, l1); h1 = addllx(h0, h1);
     838     4881533 :   l0 = mulll(x2, y1); h0 = hiremainder;
     839     4881533 :   l1 = addll(l0, l1); h1 = addllx(h0, h1);
     840     4881533 :   l0 = mulll(x3, y0); h0 = hiremainder;
     841     4881533 :   l1 = addll(l0, l1); h1 = addllx(h0, h1);
     842     4881533 :   return remll_pre(h1, l1, p, pi);
     843             : }
     844             : 
     845             : /* Computes x0y4 + x1y3 + x2y2 + x3y1 + x4y0 (mod p); assumes p < 2^62. */
     846             : INLINE ulong
     847    24220931 : Fl_addmul5(
     848             :   ulong x0, ulong x1, ulong x2, ulong x3, ulong x4,
     849             :   ulong y0, ulong y1, ulong y2, ulong y3, ulong y4,
     850             :   ulong p, ulong pi)
     851             : {
     852             :   ulong l0, l1, h0, h1;
     853             :   LOCAL_OVERFLOW;
     854             :   LOCAL_HIREMAINDER;
     855    24220931 :   l0 = mulll(x0, y4); h0 = hiremainder;
     856    24220931 :   l1 = mulll(x1, y3); h1 = hiremainder;
     857    24220931 :   l1 = addll(l0, l1); h1 = addllx(h0, h1);
     858    24220931 :   l0 = mulll(x2, y2); h0 = hiremainder;
     859    24220931 :   l1 = addll(l0, l1); h1 = addllx(h0, h1);
     860    24220931 :   l0 = mulll(x3, y1); h0 = hiremainder;
     861    24220931 :   l1 = addll(l0, l1); h1 = addllx(h0, h1);
     862    24220931 :   l0 = mulll(x4, y0); h0 = hiremainder;
     863    24220931 :   l1 = addll(l0, l1); h1 = addllx(h0, h1);
     864    24220931 :   return remll_pre(h1, l1, p, pi);
     865             : }
     866             : 
     867             : /* A polmodular database for a given class invariant consists of a t_VEC whose
     868             :  * L-th entry is 0 or a GEN pointing to Phi_L.  This function produces a pair
     869             :  * of databases corresponding to the j-invariant and inv */
     870             : GEN
     871       15295 : polmodular_db_init(long inv)
     872             : {
     873       15295 :   const long LEN = 32;
     874       15295 :   GEN res = cgetg_block(3, t_VEC);
     875       15295 :   gel(res, 1) = zerovec_block(LEN);
     876       15295 :   gel(res, 2) = (inv == INV_J)? gen_0: zerovec_block(LEN);
     877       15295 :   return res;
     878             : }
     879             : 
     880             : void
     881       21472 : polmodular_db_add_level(GEN *DB, long L, long inv)
     882             : {
     883       21472 :   GEN db = gel(*DB, (inv == INV_J)? 1: 2);
     884       21472 :   long max_L = lg(db) - 1;
     885       21472 :   if (L > max_L) {
     886             :     GEN newdb;
     887          43 :     long i, newlen = 2 * L;
     888             : 
     889          43 :     newdb = cgetg_block(newlen + 1, t_VEC);
     890        1419 :     for (i = 1; i <= max_L; ++i) gel(newdb, i) = gel(db, i);
     891        2941 :     for (     ; i <= newlen; ++i) gel(newdb, i) = gen_0;
     892          43 :     killblock(db);
     893          43 :     gel(*DB, (inv == INV_J)? 1: 2) = db = newdb;
     894             :   }
     895       21472 :   if (typ(gel(db, L)) == t_INT) {
     896        6333 :     pari_sp av = avma;
     897        6333 :     GEN x = polmodular0_ZM(L, inv, NULL, NULL, 0, DB); /* may set db[L] */
     898        6333 :     GEN y = gel(db, L);
     899        6333 :     gel(db, L) = gclone(x);
     900        6333 :     if (typ(y) != t_INT) gunclone(y);
     901        6333 :     set_avma(av);
     902             :   }
     903       21472 : }
     904             : 
     905             : void
     906        3996 : polmodular_db_add_levels(GEN *db, long *levels, long k, long inv)
     907             : {
     908             :   long i;
     909        8688 :   for (i = 0; i < k; ++i) polmodular_db_add_level(db, levels[i], inv);
     910        3996 : }
     911             : 
     912             : GEN
     913      312021 : polmodular_db_for_inv(GEN db, long inv) { return gel(db, (inv==INV_J)? 1: 2); }
     914             : 
     915             : /* TODO: Also cache modpoly mod p for most recent p (avoid repeated
     916             :  * reductions in, for example, polclass.c:oneroot_of_classpoly(). */
     917             : GEN
     918      454722 : polmodular_db_getp(GEN db, long L, ulong p)
     919             : {
     920      454722 :   GEN f = gel(db, L);
     921      454722 :   if (isintzero(f)) pari_err_BUG("polmodular_db_getp");
     922      454721 :   return ZM_to_Flm(f, p);
     923             : }
     924             : 
     925             : /* SECTION: Table of discriminants to use. */
     926             : typedef struct {
     927             :   long GENcode0;  /* used when serializing the struct to a t_VECSMALL */
     928             :   long inv;      /* invariant */
     929             :   long L;        /* modpoly level */
     930             :   long D0;       /* fundamental discriminant */
     931             :   long D1;       /* chosen discriminant */
     932             :   long L0;       /* first generator norm */
     933             :   long L1;       /* second generator norm */
     934             :   long n1;       /* order of L0 in cl(D1) */
     935             :   long n2;       /* order of L0 in cl(D2) where D2 = L^2 D1 */
     936             :   long dl1;      /* m such that L0^m = L in cl(D1) */
     937             :   long dl2_0;    /* These two are (m, n) such that L0^m L1^n = form of norm L^2 in D2 */
     938             :   long dl2_1;    /* This n is always 1 or 0. */
     939             :   /* this part is not serialized */
     940             :   long nprimes;  /* number of primes needed for D1 */
     941             :   long cost;     /* cost to enumerate  subgroup of cl(L^2D): subgroup size is n2 if L1=0, 2*n2 o.w. */
     942             :   long bits;
     943             :   ulong *primes;
     944             :   ulong *traces;
     945             : } disc_info;
     946             : 
     947             : #define MODPOLY_MAX_DCNT    64
     948             : 
     949             : /* Flag for last parameter of discriminant_with_classno_at_least.
     950             :  * Warning: ignoring the sparse factor makes everything slower by
     951             :  * something like (sparse factor)^3. */
     952             : #define USE_SPARSE_FACTOR 0
     953             : #define IGNORE_SPARSE_FACTOR 1
     954             : 
     955             : static long
     956             : discriminant_with_classno_at_least(disc_info Ds[MODPOLY_MAX_DCNT], long L,
     957             :   long inv, GEN Q, long ignore_sparse);
     958             : 
     959             : /* SECTION: evaluation functions for modular polynomials of small level. */
     960             : 
     961             : /* Based on phi2_eval_ff() in Sutherland's classpoly programme.
     962             :  * Calculates Phi_2(X, j) (mod p) with 6M+7A (4 reductions, not
     963             :  * counting those for Phi_2) */
     964             : INLINE GEN
     965    24918727 : Flm_Fl_phi2_evalx(GEN phi2, ulong j, ulong p, ulong pi)
     966             : {
     967    24918727 :   GEN res = cgetg(6, t_VECSMALL);
     968             :   ulong j2, t1;
     969             : 
     970    24859848 :   res[1] = 0; /* variable name */
     971             : 
     972    24859848 :   j2 = Fl_sqr_pre(j, p, pi);
     973    24958105 :   t1 = Fl_add(j, coeff(phi2, 3, 1), p);
     974    24917571 :   t1 = Fl_addmul2(j, j2, t1, coeff(phi2, 2, 1), p, pi);
     975    24977121 :   res[2] = Fl_add(t1, coeff(phi2, 1, 1), p);
     976             : 
     977    24941170 :   t1 = Fl_addmul2(j, j2, coeff(phi2, 3, 2), coeff(phi2, 2, 2), p, pi);
     978    24991957 :   res[3] = Fl_add(t1, coeff(phi2, 2, 1), p);
     979             : 
     980    24956172 :   t1 = Fl_mul_pre(j, coeff(phi2, 3, 2), p, pi);
     981    24986247 :   t1 = Fl_add(t1, coeff(phi2, 3, 1), p);
     982    24954224 :   res[4] = Fl_sub(t1, j2, p);
     983             : 
     984    24933053 :   res[5] = 1;
     985    24933053 :   return res;
     986             : }
     987             : 
     988             : /* Based on phi3_eval_ff() in Sutherland's classpoly programme.
     989             :  * Calculates Phi_3(X, j) (mod p) with 13M+13A (6 reductions, not
     990             :  * counting those for Phi_3) */
     991             : INLINE GEN
     992     2960625 : Flm_Fl_phi3_evalx(GEN phi3, ulong j, ulong p, ulong pi)
     993             : {
     994     2960625 :   GEN res = cgetg(7, t_VECSMALL);
     995             :   ulong j2, j3, t1;
     996             : 
     997     2956527 :   res[1] = 0; /* variable name */
     998             : 
     999     2956527 :   j2 = Fl_sqr_pre(j, p, pi);
    1000     2963165 :   j3 = Fl_mul_pre(j, j2, p, pi);
    1001             : 
    1002     2963180 :   t1 = Fl_add(j, coeff(phi3, 4, 1), p);
    1003     8891726 :   res[2] = Fl_addmul3(j, j2, j3, t1,
    1004     2961506 :                       coeff(phi3, 3, 1), coeff(phi3, 2, 1), p, pi);
    1005             : 
    1006     5937428 :   t1 = Fl_addmul3(j, j2, j3, coeff(phi3, 4, 2),
    1007     2968714 :                   coeff(phi3, 3, 2), coeff(phi3, 2, 2), p, pi);
    1008     2969689 :   res[3] = Fl_add(t1, coeff(phi3, 2, 1), p);
    1009             : 
    1010     5934540 :   t1 = Fl_addmul3(j, j2, j3, coeff(phi3, 4, 3),
    1011     2967270 :                   coeff(phi3, 3, 3), coeff(phi3, 3, 2), p, pi);
    1012     2969089 :   res[4] = Fl_add(t1, coeff(phi3, 3, 1), p);
    1013             : 
    1014     2966676 :   t1 = Fl_addmul2(j, j2, coeff(phi3, 4, 3), coeff(phi3, 4, 2), p, pi);
    1015     2967553 :   t1 = Fl_add(t1, coeff(phi3, 4, 1), p);
    1016     2965264 :   res[5] = Fl_sub(t1, j3, p);
    1017             : 
    1018     2963632 :   res[6] = 1;
    1019     2963632 :   return res;
    1020             : }
    1021             : 
    1022             : /* Based on phi5_eval_ff() in Sutherland's classpoly programme.
    1023             :  * Calculates Phi_5(X, j) (mod p) with 33M+31A (10 reductions, not
    1024             :  * counting those for Phi_5) */
    1025             : INLINE GEN
    1026     4876985 : Flm_Fl_phi5_evalx(GEN phi5, ulong j, ulong p, ulong pi)
    1027             : {
    1028     4876985 :   GEN res = cgetg(9, t_VECSMALL);
    1029             :   ulong j2, j3, j4, j5, t1;
    1030             : 
    1031     4865064 :   res[1] = 0; /* variable name */
    1032             : 
    1033     4865064 :   j2 = Fl_sqr_pre(j, p, pi);
    1034     4880751 :   j3 = Fl_mul_pre(j, j2, p, pi);
    1035     4881735 :   j4 = Fl_sqr_pre(j2, p, pi);
    1036     4882089 :   j5 = Fl_mul_pre(j, j4, p, pi);
    1037             : 
    1038     4882525 :   t1 = Fl_add(j, coeff(phi5, 6, 1), p);
    1039    19510216 :   t1 = Fl_addmul5(j, j2, j3, j4, j5, t1,
    1040     9755108 :                   coeff(phi5, 5, 1), coeff(phi5, 4, 1),
    1041     4877554 :                   coeff(phi5, 3, 1), coeff(phi5, 2, 1),
    1042             :                   p, pi);
    1043     4886235 :   res[2] = Fl_add(t1, coeff(phi5, 1, 1), p);
    1044             : 
    1045    24401885 :   t1 = Fl_addmul5(j, j2, j3, j4, j5,
    1046     9760754 :                   coeff(phi5, 6, 2), coeff(phi5, 5, 2),
    1047     4880377 :                   coeff(phi5, 4, 2), coeff(phi5, 3, 2), coeff(phi5, 2, 2),
    1048             :                   p, pi);
    1049     4887469 :   res[3] = Fl_add(t1, coeff(phi5, 2, 1), p);
    1050             : 
    1051    24409180 :   t1 = Fl_addmul5(j, j2, j3, j4, j5,
    1052     9763672 :                   coeff(phi5, 6, 3), coeff(phi5, 5, 3),
    1053     4881836 :                   coeff(phi5, 4, 3), coeff(phi5, 3, 3), coeff(phi5, 3, 2),
    1054             :                   p, pi);
    1055     4888344 :   res[4] = Fl_add(t1, coeff(phi5, 3, 1), p);
    1056             : 
    1057    24416905 :   t1 = Fl_addmul5(j, j2, j3, j4, j5,
    1058     9766762 :                   coeff(phi5, 6, 4), coeff(phi5, 5, 4),
    1059     4883381 :                   coeff(phi5, 4, 4), coeff(phi5, 4, 3), coeff(phi5, 4, 2),
    1060             :                   p, pi);
    1061     4888924 :   res[5] = Fl_add(t1, coeff(phi5, 4, 1), p);
    1062             : 
    1063    24419660 :   t1 = Fl_addmul5(j, j2, j3, j4, j5,
    1064     9767864 :                   coeff(phi5, 6, 5), coeff(phi5, 5, 5),
    1065     4883932 :                   coeff(phi5, 5, 4), coeff(phi5, 5, 3), coeff(phi5, 5, 2),
    1066             :                   p, pi);
    1067     4889356 :   res[6] = Fl_add(t1, coeff(phi5, 5, 1), p);
    1068             : 
    1069    19536380 :   t1 = Fl_addmul4(j, j2, j3, j4,
    1070     9768190 :                   coeff(phi5, 6, 5), coeff(phi5, 6, 4),
    1071     4884095 :                   coeff(phi5, 6, 3), coeff(phi5, 6, 2),
    1072             :                   p, pi);
    1073     4889785 :   t1 = Fl_add(t1, coeff(phi5, 6, 1), p);
    1074     4884352 :   res[7] = Fl_sub(t1, j5, p);
    1075             : 
    1076     4880183 :   res[8] = 1;
    1077     4880183 :   return res;
    1078             : }
    1079             : 
    1080             : GEN
    1081    39676244 : Flm_Fl_polmodular_evalx(GEN phi, long L, ulong j, ulong p, ulong pi)
    1082             : {
    1083    39676244 :   switch (L) {
    1084    24907779 :     case 2: return Flm_Fl_phi2_evalx(phi, j, p, pi);
    1085     2960700 :     case 3: return Flm_Fl_phi3_evalx(phi, j, p, pi);
    1086     4877874 :     case 5: return Flm_Fl_phi5_evalx(phi, j, p, pi);
    1087     6929891 :     default: { /* not GC clean, but gerepileupto-safe */
    1088     6929891 :       GEN j_powers = Fl_powers_pre(j, L + 1, p, pi);
    1089     6994056 :       return Flm_Flc_mul_pre_Flx(phi, j_powers, p, pi, 0);
    1090             :     }
    1091             :   }
    1092             : }
    1093             : 
    1094             : /* SECTION: Velu's formula for the codmain curve (Fl case). */
    1095             : 
    1096             : INLINE ulong
    1097     1577776 : Fl_mul4(ulong x, ulong p)
    1098     1577776 : { return Fl_double(Fl_double(x, p), p); }
    1099             : 
    1100             : INLINE ulong
    1101       81120 : Fl_mul5(ulong x, ulong p)
    1102       81120 : { return Fl_add(x, Fl_mul4(x, p), p); }
    1103             : 
    1104             : INLINE ulong
    1105      788998 : Fl_mul8(ulong x, ulong p)
    1106      788998 : { return Fl_double(Fl_mul4(x, p), p); }
    1107             : 
    1108             : INLINE ulong
    1109      707966 : Fl_mul6(ulong x, ulong p)
    1110      707966 : { return Fl_sub(Fl_mul8(x, p), Fl_double(x, p), p); }
    1111             : 
    1112             : INLINE ulong
    1113       81116 : Fl_mul7(ulong x, ulong p)
    1114       81116 : { return Fl_sub(Fl_mul8(x, p), x, p); }
    1115             : 
    1116             : /* Given an elliptic curve E = [a4, a6] over F_p and a nonzero point
    1117             :  * pt on E, return the quotient E' = E/<P> = [a4_img, a6_img] */
    1118             : static void
    1119       81124 : Fle_quotient_from_kernel_generator(
    1120             :   ulong *a4_img, ulong *a6_img, ulong a4, ulong a6, GEN pt, ulong p, ulong pi)
    1121             : {
    1122       81124 :   pari_sp av = avma;
    1123       81124 :   ulong t = 0, w = 0;
    1124             :   GEN Q;
    1125             :   ulong xQ, yQ, tQ, uQ;
    1126             : 
    1127       81124 :   Q = gcopy(pt);
    1128             :   /* Note that, as L is odd, say L = 2n + 1, we necessarily have
    1129             :    * [(L - 1)/2]P = [n]P = [n - L]P = -[n + 1]P = -[(L + 1)/2]P.  This is
    1130             :    * what the condition Q[1] != xQ tests, so the loop will execute n times. */
    1131             :   do {
    1132      707841 :     xQ = uel(Q, 1);
    1133      707841 :     yQ = uel(Q, 2);
    1134             :     /* tQ = 6 xQ^2 + b2 xQ + b4
    1135             :      *    = 6 xQ^2 + 2 a4 (since b2 = 0 and b4 = 2 a4) */
    1136      707841 :     tQ = Fl_add(Fl_mul6(Fl_sqr_pre(xQ, p, pi), p), Fl_double(a4, p), p);
    1137      707878 :     uQ = Fl_add(Fl_mul4(Fl_sqr_pre(yQ, p, pi), p),
    1138             :                 Fl_mul_pre(tQ, xQ, p, pi), p);
    1139             : 
    1140      707863 :     t = Fl_add(t, tQ, p);
    1141      707836 :     w = Fl_add(w, uQ, p);
    1142      707821 :     Q = gerepileupto(av, Fle_add(pt, Q, a4, p));
    1143      707837 :   } while (uel(Q, 1) != xQ);
    1144             : 
    1145       81120 :   set_avma(av);
    1146             :   /* a4_img = a4 - 5 * t */
    1147       81117 :   *a4_img = Fl_sub(a4, Fl_mul5(t, p), p);
    1148             :   /* a6_img = a6 - b2 * t - 7 * w = a6 - 7 * w (since a1 = a2 = 0 ==> b2 = 0) */
    1149       81116 :   *a6_img = Fl_sub(a6, Fl_mul7(w, p), p);
    1150       81116 : }
    1151             : 
    1152             : /* SECTION: Calculation of modular polynomials. */
    1153             : 
    1154             : /* Given an elliptic curve [a4, a6] over FF_p, try to find a
    1155             :  * nontrivial L-torsion point on the curve by considering n times a
    1156             :  * random point; val controls the maximum L-valuation expected of n
    1157             :  * times a random point */
    1158             : static GEN
    1159      118463 : find_L_tors_point(
    1160             :   ulong *ival,
    1161             :   ulong a4, ulong a6, ulong p, ulong pi,
    1162             :   ulong n, ulong L, ulong val)
    1163             : {
    1164      118463 :   pari_sp av = avma;
    1165             :   ulong i;
    1166             :   GEN P, Q;
    1167             :   do {
    1168      119541 :     Q = random_Flj_pre(a4, a6, p, pi);
    1169      119546 :     P = Flj_mulu_pre(Q, n, a4, p, pi);
    1170      119541 :   } while (P[3] == 0);
    1171             : 
    1172      229927 :   for (i = 0; i < val; ++i) {
    1173      192593 :     Q = Flj_mulu_pre(P, L, a4, p, pi);
    1174      192586 :     if (Q[3] == 0) break;
    1175      111464 :     P = Q;
    1176             :   }
    1177      118456 :   if (ival) *ival = i;
    1178      118456 :   return gerepilecopy(av, P);
    1179             : }
    1180             : 
    1181             : static GEN
    1182       73684 : select_curve_with_L_tors_point(
    1183             :   ulong *a4, ulong *a6,
    1184             :   ulong L, ulong j, ulong n, ulong card, ulong val,
    1185             :   norm_eqn_t ne)
    1186             : {
    1187       73684 :   pari_sp av = avma;
    1188             :   ulong A4, A4t, A6, A6t;
    1189       73684 :   ulong p = ne->p, pi = ne->pi;
    1190             :   GEN P;
    1191       73684 :   if (card % L != 0) {
    1192           0 :     pari_err_BUG("select_curve_with_L_tors_point: "
    1193             :                  "Cardinality not divisible by L");
    1194             :   }
    1195             : 
    1196       73684 :   Fl_ellj_to_a4a6(j, p, &A4, &A6);
    1197       73685 :   Fl_elltwist_disc(A4, A6, ne->T, p, &A4t, &A6t);
    1198             : 
    1199             :   /* Either E = [a4, a6] or its twist has cardinality divisible by L
    1200             :    * because of the choice of p and t earlier on.  We find out which
    1201             :    * by attempting to find a point of order L on each.  See bot p16 of
    1202             :    * Sutherland 2012. */
    1203       37346 :   while (1) {
    1204             :     ulong i;
    1205      111029 :     P = find_L_tors_point(&i, A4, A6, p, pi, n, L, val);
    1206      111024 :     if (i < val)
    1207       73686 :       break;
    1208       37338 :     set_avma(av);
    1209       37346 :     lswap(A4, A4t);
    1210       37346 :     lswap(A6, A6t);
    1211             :   }
    1212       73686 :   *a4 = A4;
    1213       73686 :   *a6 = A6; return gerepilecopy(av, P);
    1214             : }
    1215             : 
    1216             : /* Return 1 if the L-Sylow subgroup of the curve [a4, a6] (mod p) is
    1217             :  * cyclic, return 0 if it is not cyclic with "high" probability (I
    1218             :  * guess around 1/L^3 chance it is still cyclic when we return 0).
    1219             :  *
    1220             :  * Based on Sutherland's velu.c:velu_verify_Sylow_cyclic() in classpoly-1.0.1 */
    1221             : INLINE long
    1222       41433 : verify_L_sylow_is_cyclic(long e, ulong a4, ulong a6, ulong p, ulong pi)
    1223             : {
    1224             :   /* Number of times to try to find a point with maximal order in the
    1225             :    * L-Sylow subgroup. */
    1226             :   enum { N_RETRIES = 3 };
    1227       41433 :   pari_sp av = avma;
    1228       41433 :   long i, res = 0;
    1229             :   GEN P;
    1230       67242 :   for (i = 0; i < N_RETRIES; ++i) {
    1231       59802 :     P = random_Flj_pre(a4, a6, p, pi);
    1232       59798 :     P = Flj_mulu_pre(P, e, a4, p, pi);
    1233       59806 :     if (P[3] != 0) { res = 1; break; }
    1234             :   }
    1235       41437 :   return gc_long(av,res);
    1236             : }
    1237             : 
    1238             : static ulong
    1239       73685 : find_noniso_L_isogenous_curve(
    1240             :   ulong L, ulong n,
    1241             :   norm_eqn_t ne, long e, ulong val, ulong a4, ulong a6, GEN init_pt, long verify)
    1242             : {
    1243             :   pari_sp ltop, av;
    1244       73685 :   ulong p = ne->p, pi = ne->pi, j_res = 0;
    1245       73685 :   GEN pt = init_pt;
    1246       73685 :   ltop = av = avma;
    1247        7439 :   while (1) {
    1248             :     /* c. Use Velu to calculate L-isogenous curve E' = E/<P> */
    1249             :     ulong a4_img, a6_img;
    1250       81124 :     ulong z2 = Fl_sqr_pre(pt[3], p, pi);
    1251       81130 :     pt = mkvecsmall2(Fl_div(pt[1], z2, p),
    1252       81128 :                      Fl_div(pt[2], Fl_mul_pre(z2, pt[3], p, pi), p));
    1253       81124 :     Fle_quotient_from_kernel_generator(&a4_img, &a6_img,
    1254             :                                        a4, a6, pt, p, pi);
    1255             : 
    1256             :     /* d. If j(E') = j_res has a different endo ring to j(E), then
    1257             :      *    return j(E').  Otherwise, go to b. */
    1258       81117 :     if (!verify || verify_L_sylow_is_cyclic(e, a4_img, a6_img, p, pi)) {
    1259       73681 :       j_res = Fl_ellj_pre(a4_img, a6_img, p, pi);
    1260       73687 :       break;
    1261             :     }
    1262             : 
    1263             :     /* b. Generate random point P on E of order L */
    1264        7439 :     set_avma(av);
    1265        7439 :     pt = find_L_tors_point(NULL, a4, a6, p, pi, n, L, val);
    1266             :   }
    1267       73687 :   return gc_ulong(ltop, j_res);
    1268             : }
    1269             : 
    1270             : /* Given a prime L and a j-invariant j (mod p), return the j-invariant
    1271             :  * of a curve which has a different endomorphism ring to j and is
    1272             :  * L-isogenous to j */
    1273             : INLINE ulong
    1274       73683 : compute_L_isogenous_curve(
    1275             :   ulong L, ulong n, norm_eqn_t ne,
    1276             :   ulong j, ulong card, ulong val, long verify)
    1277             : {
    1278             :   ulong a4, a6;
    1279             :   long e;
    1280             :   GEN pt;
    1281             : 
    1282       73683 :   if (ne->p < 5 || j == 0 || j == 1728 % ne->p)
    1283           0 :     pari_err_BUG("compute_L_isogenous_curve");
    1284       73683 :   pt = select_curve_with_L_tors_point(&a4, &a6, L, j, n, card, val, ne);
    1285       73685 :   e = card / L;
    1286       73685 :   if (e * L != card) pari_err_BUG("compute_L_isogenous_curve");
    1287             : 
    1288       73685 :   return find_noniso_L_isogenous_curve(L, n, ne, e, val, a4, a6, pt, verify);
    1289             : }
    1290             : 
    1291             : INLINE GEN
    1292       33997 : get_Lsqr_cycle(const disc_info *dinfo)
    1293             : {
    1294       33997 :   long i, n1 = dinfo->n1, L = dinfo->L;
    1295       33997 :   GEN cyc = cgetg(L, t_VECSMALL);
    1296       33996 :   cyc[1] = 0;
    1297      297012 :   for (i = 2; i <= L / 2; ++i) cyc[i] = cyc[i - 1] + n1;
    1298       33996 :   if ( ! dinfo->L1) {
    1299      114075 :     for ( ; i < L; ++i) cyc[i] = cyc[i - 1] + n1;
    1300             :   } else {
    1301       21117 :     cyc[L - 1] = 2 * dinfo->n2 - n1 / 2;
    1302      195817 :     for (i = L - 2; i > L / 2; --i) cyc[i] = cyc[i + 1] - n1;
    1303             :   }
    1304       33996 :   return cyc;
    1305             : }
    1306             : 
    1307             : INLINE void
    1308      483699 : update_Lsqr_cycle(GEN cyc, const disc_info *dinfo)
    1309             : {
    1310      483699 :   long i, L = dinfo->L;
    1311    15007677 :   for (i = 1; i < L; ++i) ++cyc[i];
    1312      483699 :   if (dinfo->L1 && cyc[L - 1] == 2 * dinfo->n2) {
    1313       19391 :     long n1 = dinfo->n1;
    1314      184915 :     for (i = L / 2 + 1; i < L; ++i) cyc[i] -= n1;
    1315             :   }
    1316      483699 : }
    1317             : 
    1318             : static ulong
    1319       33995 : oneroot_of_classpoly(GEN hilb, GEN factu, norm_eqn_t ne, GEN jdb)
    1320             : {
    1321       33995 :   pari_sp av = avma;
    1322       33995 :   ulong j0, p = ne->p, pi = ne->pi;
    1323       33995 :   long i, nfactors = lg(gel(factu, 1)) - 1;
    1324       33995 :   GEN hilbp = ZX_to_Flx(hilb, p);
    1325             : 
    1326             :   /* TODO: Work out how to use hilb with better invariant */
    1327       33992 :   j0 = Flx_oneroot_split(hilbp, p);
    1328       33996 :   if (j0 == p) {
    1329           0 :     pari_err_BUG("oneroot_of_classpoly: "
    1330             :                  "Didn't find a root of the class polynomial");
    1331             :   }
    1332       35662 :   for (i = 1; i <= nfactors; ++i) {
    1333        1666 :     long L = gel(factu, 1)[i];
    1334        1666 :     long val = gel(factu, 2)[i];
    1335        1666 :     GEN phi = polmodular_db_getp(jdb, L, p);
    1336        1665 :     val += z_lval(ne->v, L);
    1337        1665 :     j0 = descend_volcano(phi, j0, p, pi, 0, L, val, val);
    1338        1666 :     set_avma(av);
    1339             :   }
    1340       33996 :   return gc_ulong(av, j0);
    1341             : }
    1342             : 
    1343             : /* TODO: Precompute the classgp_pcp_t structs and link them to dinfo */
    1344             : INLINE void
    1345       33997 : make_pcp_surface(const disc_info *dinfo, classgp_pcp_t G)
    1346             : {
    1347       33997 :   long k = 1, datalen = 3 * k;
    1348             : 
    1349       33997 :   memset(G, 0, sizeof *G);
    1350             : 
    1351       33997 :   G->_data = cgetg(datalen + 1, t_VECSMALL);
    1352       33998 :   G->L = G->_data + 1;
    1353       33998 :   G->n = G->L + k;
    1354       33998 :   G->o = G->L + k;
    1355             : 
    1356       33998 :   G->k = k;
    1357       33998 :   G->h = G->enum_cnt = dinfo->n1;
    1358       33998 :   G->L[0] = dinfo->L0;
    1359       33998 :   G->n[0] = dinfo->n1;
    1360       33998 :   G->o[0] = dinfo->n1;
    1361       33998 : }
    1362             : 
    1363             : INLINE void
    1364       33994 : make_pcp_floor(const disc_info *dinfo, classgp_pcp_t G)
    1365             : {
    1366       33994 :   long k = dinfo->L1 ? 2 : 1, datalen = 3 * k;
    1367             : 
    1368       33994 :   memset(G, 0, sizeof *G);
    1369       33994 :   G->_data = cgetg(datalen + 1, t_VECSMALL);
    1370       33995 :   G->L = G->_data + 1;
    1371       33995 :   G->n = G->L + k;
    1372       33995 :   G->o = G->L + k;
    1373             : 
    1374       33995 :   G->k = k;
    1375       33995 :   G->h = G->enum_cnt = dinfo->n2 * k;
    1376       33995 :   G->L[0] = dinfo->L0;
    1377       33995 :   G->n[0] = dinfo->n2;
    1378       33995 :   G->o[0] = dinfo->n2;
    1379       33995 :   if (dinfo->L1) {
    1380       21117 :     G->L[1] = dinfo->L1;
    1381       21117 :     G->n[1] = 2;
    1382       21117 :     G->o[1] = 2;
    1383             :   }
    1384       33995 : }
    1385             : 
    1386             : INLINE GEN
    1387       33996 : enum_volcano_surface(const disc_info *dinfo, norm_eqn_t ne, ulong j0, GEN fdb)
    1388             : {
    1389       33996 :   pari_sp av = avma;
    1390             :   classgp_pcp_t G;
    1391       33996 :   make_pcp_surface(dinfo, G);
    1392       33998 :   return gerepileupto(av, enum_roots(j0, ne, fdb, G));
    1393             : }
    1394             : 
    1395             : INLINE GEN
    1396       33994 : enum_volcano_floor(long L, norm_eqn_t ne, ulong j0_pr, GEN fdb, const disc_info *dinfo)
    1397             : {
    1398       33994 :   pari_sp av = avma;
    1399             :   /* L^2 D is the discriminant for the order R = Z + L OO. */
    1400       33994 :   long DR = L * L * ne->D;
    1401       33994 :   long R_cond = L * ne->u; /* conductor(DR); */
    1402       33994 :   long w = R_cond * ne->v;
    1403             :   /* TODO: Calculate these once and for all in polmodular0_ZM(). */
    1404             :   classgp_pcp_t G;
    1405             :   norm_eqn_t eqn;
    1406       33994 :   memcpy(eqn, ne, sizeof *ne);
    1407       33994 :   eqn->D = DR;
    1408       33994 :   eqn->u = R_cond;
    1409       33994 :   eqn->v = w;
    1410       33994 :   make_pcp_floor(dinfo, G);
    1411       33995 :   return gerepileupto(av, enum_roots(j0_pr, eqn, fdb, G));
    1412             : }
    1413             : 
    1414             : INLINE void
    1415       16113 : carray_reverse_inplace(long *arr, long n)
    1416             : {
    1417       16113 :   long lim = n>>1, i;
    1418       16113 :   --n;
    1419      164175 :   for (i = 0; i < lim; i++) lswap(arr[i], arr[n - i]);
    1420       16113 : }
    1421             : 
    1422             : INLINE void
    1423      517715 : append_neighbours(GEN rts, GEN surface_js, long njs, long L, long m, long i)
    1424             : {
    1425      517715 :   long r_idx = (((i - 1) + m) % njs) + 1; /* (i + m) % njs */
    1426      517715 :   long l_idx = umodsu((i - 1) - m, njs) + 1; /* (i - m) % njs */
    1427      517696 :   rts[L] = surface_js[l_idx];
    1428      517696 :   rts[L + 1] = surface_js[r_idx];
    1429      517696 : }
    1430             : 
    1431             : INLINE GEN
    1432       36232 : roots_to_coeffs(GEN rts, ulong p, long L)
    1433             : {
    1434       36232 :   long i, k, lrts= lg(rts);
    1435       36232 :   GEN M = cgetg(L+2+1, t_MAT);
    1436      809872 :   for (i = 1; i <= L+2; ++i)
    1437      773652 :     gel(M, i) = cgetg(lrts, t_VECSMALL);
    1438      578000 :   for (i = 1; i < lrts; ++i) {
    1439      541885 :     pari_sp av = avma;
    1440      541885 :     GEN modpol = Flv_roots_to_pol(gel(rts, i), p, 0);
    1441    18539575 :     for (k = 1; k <= L + 2; ++k) mael(M, k, i) = modpol[k + 1];
    1442      541513 :     set_avma(av);
    1443             :   }
    1444       36115 :   return M;
    1445             : }
    1446             : 
    1447             : /* NB: Assumes indices are offset at 0, not at 1 like in GENs;
    1448             :  * i.e. indices[i] will pick out v[indices[i] + 1] from v. */
    1449             : INLINE void
    1450      517723 : vecsmall_pick(GEN res, GEN v, GEN indices)
    1451             : {
    1452             :   long i;
    1453    15636207 :   for (i = 1; i < lg(indices); ++i) res[i] = v[indices[i] + 1];
    1454      517723 : }
    1455             : 
    1456             : /* First element of surface_js must lie above the first element of floor_js.
    1457             :  * Reverse surface_js if it is not oriented in the same direction as floor_js */
    1458             : INLINE GEN
    1459       33998 : root_matrix(long L, const disc_info *dinfo, long njinvs, GEN surface_js,
    1460             :   GEN floor_js, ulong n, ulong card, ulong val, norm_eqn_t ne)
    1461             : {
    1462             :   pari_sp av;
    1463       33998 :   long i, m = dinfo->dl1, njs = lg(surface_js) - 1, inv = dinfo->inv, rev;
    1464       33998 :   GEN rt_mat = zero_Flm_copy(L + 1, njinvs), rts, cyc;
    1465       33997 :   av = avma;
    1466             : 
    1467       33997 :   i = 1;
    1468       33997 :   cyc = get_Lsqr_cycle(dinfo);
    1469       33996 :   rts = gel(rt_mat, i);
    1470       33996 :   vecsmall_pick(rts, floor_js, cyc);
    1471       33997 :   append_neighbours(rts, surface_js, njs, L, m, i);
    1472             : 
    1473       33994 :   i = 2;
    1474       33994 :   update_Lsqr_cycle(cyc, dinfo);
    1475       33996 :   rts = gel(rt_mat, i);
    1476       33996 :   vecsmall_pick(rts, floor_js, cyc);
    1477             : 
    1478             :   /* Fix orientation if necessary */
    1479       33994 :   if (modinv_is_double_eta(inv)) {
    1480             :     /* TODO: There is potential for refactoring between this,
    1481             :      * double_eta_initial_js and modfn_preimage. */
    1482        5697 :     pari_sp av0 = avma;
    1483        5697 :     ulong p = ne->p, pi = ne->pi, j;
    1484        5697 :     GEN F = double_eta_Fl(inv, p);
    1485        5697 :     pari_sp av = avma;
    1486        5697 :     ulong r1 = double_eta_power(inv, uel(rts, 1), p, pi);
    1487        5697 :     GEN r, f = Flx_double_eta_jpoly(F, r1, p, pi);
    1488        5697 :     if ((j = Flx_oneroot(f, p)) == p) pari_err_BUG("root_matrix");
    1489        5697 :     j = compute_L_isogenous_curve(L, n, ne, j, card, val, 0);
    1490        5697 :     set_avma(av);
    1491        5697 :     r1 = double_eta_power(inv, uel(surface_js, i), p, pi);
    1492        5697 :     f = Flx_double_eta_jpoly(F, r1, p, pi);
    1493        5697 :     r = Flx_roots(f, p);
    1494        5697 :     if (lg(r) != 3) pari_err_BUG("root_matrix");
    1495        5697 :     rev = (j != uel(r, 1)) && (j != uel(r, 2));
    1496        5697 :     set_avma(av0);
    1497             :   } else {
    1498             :     ulong j1pr, j1;
    1499       28297 :     j1pr = modfn_preimage(uel(rts, 1), ne, dinfo->inv);
    1500       28296 :     j1 = compute_L_isogenous_curve(L, n, ne, j1pr, card, val, 0);
    1501       28297 :     rev = j1 != modfn_preimage(uel(surface_js, i), ne, dinfo->inv);
    1502             :   }
    1503       33994 :   if (rev)
    1504       16113 :     carray_reverse_inplace(surface_js + 2, njs - 1);
    1505       33995 :   append_neighbours(rts, surface_js, njs, L, m, i);
    1506             : 
    1507      483720 :   for (i = 3; i <= njinvs; ++i) {
    1508      449723 :     update_Lsqr_cycle(cyc, dinfo);
    1509      449755 :     rts = gel(rt_mat, i);
    1510      449755 :     vecsmall_pick(rts, floor_js, cyc);
    1511      449747 :     append_neighbours(rts, surface_js, njs, L, m, i);
    1512             :   }
    1513       33997 :   set_avma(av); return rt_mat;
    1514             : }
    1515             : 
    1516             : INLINE void
    1517       36578 : interpolate_coeffs(GEN phi_modp, ulong p, GEN j_invs, GEN coeff_mat)
    1518             : {
    1519       36578 :   pari_sp av = avma;
    1520             :   long i;
    1521       36578 :   GEN pols = Flv_Flm_polint(j_invs, coeff_mat, p, 0);
    1522      812459 :   for (i = 1; i < lg(pols); ++i) {
    1523      775881 :     GEN pol = gel(pols, i);
    1524      775881 :     long k, maxk = lg(pol);
    1525    17627554 :     for (k = 2; k < maxk; ++k) coeff(phi_modp, k - 1, i) = pol[k];
    1526             :   }
    1527       36578 :   set_avma(av);
    1528       36583 : }
    1529             : 
    1530             : INLINE long
    1531      342526 : Flv_lastnonzero(GEN v)
    1532             : {
    1533             :   long i;
    1534    23787568 :   for (i = lg(v) - 1; i > 0; --i)
    1535    23786872 :     if (v[i]) break;
    1536      342526 :   return i;
    1537             : }
    1538             : 
    1539             : /* Assuming the matrix of coefficients in phi corresponds to polynomials
    1540             :  * phi_k^* satisfying Y^c phi_k^*(Y^s) for c in {0, 1, ..., s} satisfying
    1541             :  * c + Lk = L + 1 (mod s), change phi so that the coefficients are for the
    1542             :  * polynomials Y^c phi_k^*(Y^s) (s is the sparsity factor) */
    1543             : INLINE void
    1544       10913 : inflate_polys(GEN phi, long L, long s)
    1545             : {
    1546       10913 :   long k, deg = L + 1;
    1547             :   long maxr;
    1548       10913 :   maxr = nbrows(phi);
    1549      353482 :   for (k = 0; k <= deg; ) {
    1550      342569 :     long i, c = umodsu(L * (1 - k) + 1, s);
    1551             :     /* TODO: We actually know that the last nonzero element of gel(phi, k)
    1552             :      * can't be later than index n+1, where n is about (L + 1)/s. */
    1553      342540 :     ++k;
    1554     5451755 :     for (i = Flv_lastnonzero(gel(phi, k)); i > 0; --i) {
    1555     5109215 :       long r = c + (i - 1) * s + 1;
    1556     5109215 :       if (r > maxr) { coeff(phi, i, k) = 0; continue; }
    1557     5047143 :       if (r != i) {
    1558     4939837 :         coeff(phi, r, k) = coeff(phi, i, k);
    1559     4939837 :         coeff(phi, i, k) = 0;
    1560             :       }
    1561             :     }
    1562             :   }
    1563       10913 : }
    1564             : 
    1565             : INLINE void
    1566       41457 : Flv_powu_inplace_pre(GEN v, ulong n, ulong p, ulong pi)
    1567             : {
    1568             :   long i;
    1569      339286 :   for (i = 1; i < lg(v); ++i) v[i] = Fl_powu_pre(v[i], n, p, pi);
    1570       41457 : }
    1571             : 
    1572             : INLINE void
    1573       10913 : normalise_coeffs(GEN coeffs, GEN js, long L, long s, ulong p, ulong pi)
    1574             : {
    1575       10913 :   pari_sp av = avma;
    1576             :   long k;
    1577             :   GEN pows, modinv_js;
    1578             : 
    1579             :   /* NB: In fact it would be correct to return the coefficients "as is" when
    1580             :    * s = 1, but we make that an error anyway since this function should never
    1581             :    * be called with s = 1. */
    1582       10913 :   if (s <= 1) pari_err_BUG("normalise_coeffs");
    1583             : 
    1584             :   /* pows[i + 1] contains 1 / js[i + 1]^i for i = 0, ..., s - 1. */
    1585       10913 :   pows = cgetg(s + 1, t_VEC);
    1586       10913 :   gel(pows, 1) = const_vecsmall(lg(js) - 1, 1);
    1587       10913 :   modinv_js = Flv_inv_pre(js, p, pi);
    1588       10913 :   gel(pows, 2) = modinv_js;
    1589       39092 :   for (k = 3; k <= s; ++k) {
    1590       28179 :     gel(pows, k) = gcopy(modinv_js);
    1591       28179 :     Flv_powu_inplace_pre(gel(pows, k), k - 1, p, pi);
    1592             :   }
    1593             : 
    1594             :   /* For each column of coefficients coeffs[k] = [a0 .. an],
    1595             :    *   replace ai by ai / js[i]^c.
    1596             :    * Said in another way, normalise each row i of coeffs by
    1597             :    * dividing through by js[i - 1]^c (where c depends on i). */
    1598      353693 :   for (k = 1; k < lg(coeffs); ++k) {
    1599      342542 :     long i, c = umodsu(L * (1 - (k - 1)) + 1, s);
    1600      342540 :     GEN col = gel(coeffs, k), C = gel(pows, c + 1);
    1601     5836683 :     for (i = 1; i < lg(col); ++i)
    1602     5493903 :       col[i] = Fl_mul_pre(col[i], C[i], p, pi);
    1603             :   }
    1604       11151 :   set_avma(av);
    1605       10913 : }
    1606             : 
    1607             : INLINE void
    1608        5697 : double_eta_initial_js(
    1609             :   ulong *x0, ulong *x0pr, ulong j0, ulong j0pr, norm_eqn_t ne,
    1610             :   long inv, ulong L, ulong n, ulong card, ulong val)
    1611             : {
    1612        5697 :   pari_sp av0 = avma;
    1613        5697 :   ulong p = ne->p, pi = ne->pi, s2 = ne->s2;
    1614        5697 :   GEN F = double_eta_Fl(inv, p);
    1615        5697 :   pari_sp av = avma;
    1616             :   ulong j1pr, j1, r, t;
    1617             :   GEN f, g;
    1618             : 
    1619        5697 :   *x0pr = modinv_double_eta_from_j(F, inv, j0pr, p, pi, s2);
    1620        5697 :   t = double_eta_power(inv, *x0pr, p, pi);
    1621        5697 :   f = Flx_div_by_X_x(Flx_double_eta_jpoly(F, t, p, pi), j0pr, p, &r);
    1622        5697 :   if (r) pari_err_BUG("double_eta_initial_js");
    1623        5697 :   j1pr = Flx_deg1_root(f, p);
    1624        5697 :   set_avma(av);
    1625             : 
    1626        5697 :   j1 = compute_L_isogenous_curve(L, n, ne, j1pr, card, val, 0);
    1627        5696 :   f = Flx_double_eta_xpoly(F, j0, p, pi);
    1628        5697 :   g = Flx_double_eta_xpoly(F, j1, p, pi);
    1629             :   /* x0 is the unique common root of f and g */
    1630        5697 :   *x0 = Flx_deg1_root(Flx_gcd(f, g, p), p);
    1631        5697 :   set_avma(av0);
    1632             : 
    1633        5697 :   if ( ! double_eta_root(inv, x0, *x0, p, pi, s2))
    1634           0 :     pari_err_BUG("double_eta_initial_js");
    1635        5697 : }
    1636             : 
    1637             : /* This is Sutherland 2012, Algorithm 2.1, p16. */
    1638             : static GEN
    1639       33993 : polmodular_split_p_Flm(ulong L, GEN hilb, GEN factu, norm_eqn_t ne, GEN db,
    1640             :   const disc_info *dinfo)
    1641             : {
    1642             :   ulong j0, j0_rt, j0pr, j0pr_rt;
    1643       33993 :   ulong n, card, val, p = ne->p, pi = ne->pi;
    1644       33993 :   long s = modinv_sparse_factor(dinfo->inv);
    1645       33994 :   long nj_selected = ceil((L + 1)/(double)s) + 1;
    1646             :   GEN surface_js, floor_js, rts;
    1647             :   GEN phi_modp;
    1648             :   GEN jdb, fdb;
    1649       33994 :   long switched_signs = 0;
    1650             : 
    1651       33994 :   jdb = polmodular_db_for_inv(db, INV_J);
    1652       33994 :   fdb = polmodular_db_for_inv(db, dinfo->inv);
    1653             : 
    1654             :   /* Precomputation */
    1655       33995 :   card = p + 1 - ne->t;
    1656       33995 :   val = u_lvalrem(card, L, &n); /* n = card / L^{v_L(card)} */
    1657             : 
    1658       33994 :   j0 = oneroot_of_classpoly(hilb, factu, ne, jdb);
    1659       33995 :   j0pr = compute_L_isogenous_curve(L, n, ne, j0, card, val, 1);
    1660       33997 :   if (modinv_is_double_eta(dinfo->inv)) {
    1661        5697 :     double_eta_initial_js(&j0_rt, &j0pr_rt, j0, j0pr, ne, dinfo->inv,
    1662             :         L, n, card, val);
    1663             :   } else {
    1664       28301 :     j0_rt = modfn_root(j0, ne, dinfo->inv);
    1665       28302 :     j0pr_rt = modfn_root(j0pr, ne, dinfo->inv);
    1666             :   }
    1667       33997 :   surface_js = enum_volcano_surface(dinfo, ne, j0_rt, fdb);
    1668       33994 :   floor_js = enum_volcano_floor(L, ne, j0pr_rt, fdb, dinfo);
    1669       33998 :   rts = root_matrix(L, dinfo, nj_selected, surface_js, floor_js,
    1670             :                     n, card, val, ne);
    1671        2235 :   do {
    1672       36232 :     pari_sp btop = avma;
    1673             :     long i;
    1674             :     GEN coeffs, surf;
    1675             : 
    1676       36232 :     coeffs = roots_to_coeffs(rts, p, L);
    1677       36224 :     surf = vecsmall_shorten(surface_js, nj_selected);
    1678       36223 :     if (s > 1) {
    1679       10913 :       normalise_coeffs(coeffs, surf, L, s, p, pi);
    1680       10913 :       Flv_powu_inplace_pre(surf, s, p, pi);
    1681             :     }
    1682       36223 :     phi_modp = zero_Flm_copy(L + 2, L + 2);
    1683       36229 :     interpolate_coeffs(phi_modp, p, surf, coeffs);
    1684       36233 :     if (s > 1) inflate_polys(phi_modp, L, s);
    1685             : 
    1686             :     /* TODO: Calculate just this coefficient of X^L Y^L, so we can do this
    1687             :      * test, then calculate the other coefficients; at the moment we are
    1688             :      * sometimes doing all the roots-to-coeffs, normalisation and interpolation
    1689             :      * work twice. */
    1690       36233 :     if (ucoeff(phi_modp, L + 1, L + 1) == p - 1) break;
    1691             : 
    1692        2235 :     if (switched_signs) pari_err_BUG("polmodular_split_p_Flm");
    1693             : 
    1694        2235 :     set_avma(btop);
    1695       27031 :     for (i = 1; i < lg(rts); ++i) {
    1696       24796 :       surface_js[i] = Fl_neg(surface_js[i], p);
    1697       24796 :       coeff(rts, L, i) = Fl_neg(coeff(rts, L, i), p);
    1698       24796 :       coeff(rts, L + 1, i) = Fl_neg(coeff(rts, L + 1, i), p);
    1699             :     }
    1700        2235 :     switched_signs = 1;
    1701             :   } while (1);
    1702       33998 :   dbg_printf(4)("  Phi_%lu(X, Y) (mod %lu) = %Ps\n", L, p, phi_modp);
    1703             : 
    1704       33998 :   return phi_modp;
    1705             : }
    1706             : 
    1707             : INLINE void
    1708       33999 : norm_eqn_update(norm_eqn_t ne, GEN vne, GEN tp, long L)
    1709             : {
    1710       33999 :   ulong vL_sqr, vL, t = tp[1], p = tp[2];
    1711             :   long res;
    1712             : 
    1713       33999 :   ne->D = vne[1];
    1714       33999 :   ne->u = vne[2];
    1715       33999 :   ne->t = t;
    1716       33999 :   ne->p = p;
    1717       33999 :   ne->pi = get_Fl_red(p);
    1718       33999 :   ne->s2 = Fl_2gener_pre(p, ne->pi);
    1719             : 
    1720       33998 :   vL_sqr = (4 * p - t * t) / -ne->D;
    1721       33998 :   res = uissquareall(vL_sqr, &vL);
    1722       33997 :   if (!res || vL % L) pari_err_BUG("norm_eqn_update");
    1723       33997 :   ne->v = vL;
    1724             : 
    1725             :   /* select twisting parameter */
    1726       68511 :   do ne->T = random_Fl(p); while (krouu(ne->T, p) != -1);
    1727       33992 : }
    1728             : 
    1729             : INLINE void
    1730        2464 : Flv_deriv_pre_inplace(GEN v, long deg, ulong p, ulong pi)
    1731             : {
    1732        2464 :   long i, ln = lg(v), d = deg % p;
    1733       57222 :   for (i = ln - 1; i > 1; --i, --d) v[i] = Fl_mul_pre(v[i - 1], d, p, pi);
    1734        2464 :   v[1] = 0;
    1735        2464 : }
    1736             : 
    1737             : INLINE GEN
    1738        2674 : eval_modpoly_modp(GEN Tp, GEN j_powers, norm_eqn_t ne, int compute_derivs)
    1739             : {
    1740        2674 :   ulong p = ne->p, pi = ne->pi;
    1741        2674 :   long L = lg(j_powers) - 3;
    1742        2674 :   GEN j_pows_p = ZV_to_Flv(j_powers, p);
    1743        2674 :   GEN tmp = cgetg(2 + 2 * compute_derivs, t_VEC);
    1744             :   /* We wrap the result in this t_VEC Tp to trick the
    1745             :    * ZM_*_CRT() functions into thinking it's a matrix. */
    1746        2674 :   gel(tmp, 1) = Flm_Flc_mul_pre(Tp, j_pows_p, p, pi);
    1747        2674 :   if (compute_derivs) {
    1748        1232 :     Flv_deriv_pre_inplace(j_pows_p, L + 1, p, pi);
    1749        1232 :     gel(tmp, 2) = Flm_Flc_mul_pre(Tp, j_pows_p, p, pi);
    1750        1232 :     Flv_deriv_pre_inplace(j_pows_p, L + 1, p, pi);
    1751        1232 :     gel(tmp, 3) = Flm_Flc_mul_pre(Tp, j_pows_p, p, pi);
    1752             :   }
    1753        2674 :   return tmp;
    1754             : }
    1755             : 
    1756             : /* Parallel interface */
    1757             : GEN
    1758       33999 : polmodular_worker(GEN tp, ulong L, GEN hilb, GEN factu, GEN vne, GEN vinfo,
    1759             :                   long derivs, GEN j_powers, GEN fdb)
    1760             : {
    1761       33999 :   pari_sp av = avma;
    1762             :   norm_eqn_t ne;
    1763             :   GEN Tp;
    1764       33999 :   norm_eqn_update(ne, vne, tp, L);
    1765       33992 :   Tp = polmodular_split_p_Flm(L, hilb, factu, ne, fdb, (const disc_info*)vinfo);
    1766       33998 :   if (!isintzero(j_powers)) Tp = eval_modpoly_modp(Tp, j_powers, ne, derivs);
    1767       33997 :   return gerepileupto(av, Tp);
    1768             : }
    1769             : 
    1770             : static GEN
    1771       18382 : sympol_to_ZM(GEN phi, long L)
    1772             : {
    1773       18382 :   pari_sp av = avma;
    1774       18382 :   GEN res = zeromatcopy(L + 2, L + 2);
    1775       18382 :   long i, j, c = 1;
    1776       80115 :   for (i = 1; i <= L + 1; ++i)
    1777      204092 :     for (j = 1; j <= i; ++j, ++c)
    1778      142359 :       gcoeff(res, i, j) = gcoeff(res, j, i) = gel(phi, c);
    1779       18382 :   gcoeff(res, L + 2, 1) = gcoeff(res, 1, L + 2) = gen_1;
    1780       18382 :   return gerepilecopy(av, res);
    1781             : }
    1782             : 
    1783             : static GEN polmodular_small_ZM(long L, long inv, GEN *db);
    1784             : 
    1785             : INLINE long
    1786       21278 : modinv_max_internal_level(long inv)
    1787             : {
    1788       21278 :   switch (inv) {
    1789       18657 :     case INV_J: return 5;
    1790         364 :     case INV_G2: return 2;
    1791         443 :     case INV_F:
    1792             :     case INV_F2:
    1793             :     case INV_F4:
    1794         443 :     case INV_F8: return 5;
    1795         252 :     case INV_W2W5:
    1796         252 :     case INV_W2W5E2: return 7;
    1797         462 :     case INV_W2W3:
    1798             :     case INV_W2W3E2:
    1799             :     case INV_W3W3:
    1800         462 :     case INV_W3W7:  return 5;
    1801          63 :     case INV_W3W3E2:return 2;
    1802         729 :     case INV_F3:
    1803             :     case INV_W2W7:
    1804             :     case INV_W2W7E2:
    1805         729 :     case INV_W2W13: return 3;
    1806         308 :     case INV_W3W5:
    1807             :     case INV_W5W7:
    1808         308 :     case INV_W3W13: return 2;
    1809             :   }
    1810             :   pari_err_BUG("modinv_max_internal_level"); return LONG_MAX;/*LCOV_EXCL_LINE*/
    1811             : }
    1812             : static void
    1813          45 : db_add_levels(GEN *db, GEN P, long inv)
    1814          45 : { polmodular_db_add_levels(db, zv_to_longptr(P), lg(P)-1, inv); }
    1815             : 
    1816             : GEN
    1817       21166 : polmodular0_ZM(long L, long inv, GEN J, GEN Q, int compute_derivs, GEN *db)
    1818             : {
    1819       21166 :   pari_sp ltop = avma;
    1820       21166 :   long k, d, Dcnt, nprimes = 0;
    1821             :   GEN modpoly, plist, tp, j_powers;
    1822             :   disc_info Ds[MODPOLY_MAX_DCNT];
    1823       21166 :   long lvl = modinv_level(inv);
    1824       21166 :   if (ugcd(L, lvl) != 1)
    1825           7 :     pari_err_DOMAIN("polmodular0_ZM", "invariant",
    1826             :                     "incompatible with", stoi(L), stoi(lvl));
    1827             : 
    1828       21159 :   dbg_printf(1)("Calculating modular polynomial of level %lu for invariant %d\n", L, inv);
    1829       21159 :   if (L <= modinv_max_internal_level(inv)) return polmodular_small_ZM(L,inv,db);
    1830             : 
    1831        2630 :   Dcnt = discriminant_with_classno_at_least(Ds, L, inv, Q, USE_SPARSE_FACTOR);
    1832        5279 :   for (d = 0; d < Dcnt; d++) nprimes += Ds[d].nprimes;
    1833        2630 :   modpoly = cgetg(nprimes+1, t_VEC);
    1834        2630 :   plist = cgetg(nprimes+1, t_VECSMALL);
    1835        2630 :   tp = mkvec(mkvecsmall2(0,0));
    1836        2630 :   j_powers = gen_0;
    1837        2630 :   if (J) {
    1838          63 :     compute_derivs = !!compute_derivs;
    1839          63 :     j_powers = Fp_powers(J, L+1, Q);
    1840             :   }
    1841        5279 :   for (d = 0, k = 1; d < Dcnt; d++)
    1842             :   {
    1843        2649 :     disc_info *dinfo = &Ds[d];
    1844             :     struct pari_mt pt;
    1845        2649 :     const long D = dinfo->D1, DK = dinfo->D0;
    1846        2649 :     const ulong cond = usqrt(D / DK);
    1847        2649 :     long i, pending = 0;
    1848        2649 :     GEN worker, hilb, factu = factoru(cond);
    1849             : 
    1850        2649 :     polmodular_db_add_level(db, dinfo->L0, inv);
    1851        2649 :     if (dinfo->L1) polmodular_db_add_level(db, dinfo->L1, inv);
    1852        2649 :     dbg_printf(1)("Selected discriminant D = %ld = %ld^2 * %ld.\n", D,cond,DK);
    1853        2649 :     hilb = polclass0(DK, INV_J, 0, db);
    1854        2649 :     if (cond > 1) db_add_levels(db, gel(factu,1), INV_J);
    1855        2649 :     dbg_printf(1)("D = %ld, L0 = %lu, L1 = %lu, ", dinfo->D1, dinfo->L0, dinfo->L1);
    1856        2649 :     dbg_printf(1)("n1 = %lu, n2 = %lu, dl1 = %lu, dl2_0 = %lu, dl2_1 = %lu\n",
    1857             :           dinfo->n1, dinfo->n2, dinfo->dl1, dinfo->dl2_0, dinfo->dl2_1);
    1858        2649 :     dbg_printf(0)("Calculating modular polynomial of level %lu:", L);
    1859             : 
    1860        2649 :     worker = snm_closure(is_entry("_polmodular_worker"),
    1861             :                          mkvecn(8, utoi(L), hilb, factu, mkvecsmall2(D, cond),
    1862             :                                    (GEN)dinfo, stoi(compute_derivs), j_powers,
    1863             :                                    *db));
    1864        2649 :     mt_queue_start_lim(&pt, worker, dinfo->nprimes);
    1865       40768 :     for (i = 0; i < dinfo->nprimes || pending; i++)
    1866             :     {
    1867             :       long workid;
    1868             :       GEN done;
    1869       38119 :       if (i < dinfo->nprimes)
    1870             :       {
    1871       33999 :         mael(tp, 1, 1) = dinfo->traces[i];
    1872       33999 :         mael(tp, 1, 2) = dinfo->primes[i];
    1873             :       }
    1874       38119 :       mt_queue_submit(&pt, i, i < dinfo->nprimes? tp: NULL);
    1875       38119 :       done = mt_queue_get(&pt, &workid, &pending);
    1876       38119 :       if (done)
    1877             :       {
    1878       33999 :         plist[k] = dinfo->primes[workid];
    1879       33999 :         gel(modpoly, k) = done; k++;
    1880       33999 :         dbg_printf(0)(" %ld%%", k*100/nprimes);
    1881             :       }
    1882             :     }
    1883        2649 :     dbg_printf(0)("\n");
    1884        2649 :     mt_queue_end(&pt);
    1885        2649 :     killblock((GEN)dinfo->primes);
    1886             :   }
    1887        2630 :   modpoly = nmV_chinese_center(modpoly, plist, NULL);
    1888        2630 :   if (J) modpoly = FpM_red(modpoly, Q);
    1889        2630 :   return gerepileupto(ltop, modpoly);
    1890             : }
    1891             : 
    1892             : GEN
    1893       14637 : polmodular_ZM(long L, long inv)
    1894             : {
    1895             :   GEN db, Phi;
    1896             : 
    1897       14637 :   if (L < 2)
    1898           7 :     pari_err_DOMAIN("polmodular_ZM", "L", "<", gen_2, stoi(L));
    1899             : 
    1900             :   /* TODO: Handle nonprime L. Algorithm 1.1 and Corollary 3.4 in Sutherland,
    1901             :    * "Class polynomials for nonholomorphic modular functions" */
    1902       14630 :   if (! uisprime(L)) pari_err_IMPL("composite level");
    1903             : 
    1904       14623 :   db = polmodular_db_init(inv);
    1905       14623 :   Phi = polmodular0_ZM(L, inv, NULL, NULL, 0, &db);
    1906       14616 :   gunclone_deep(db); return Phi;
    1907             : }
    1908             : 
    1909             : GEN
    1910       14560 : polmodular_ZXX(long L, long inv, long vx, long vy)
    1911             : {
    1912       14560 :   pari_sp av = avma;
    1913       14560 :   GEN phi = polmodular_ZM(L, inv);
    1914             : 
    1915       14539 :   if (vx < 0) vx = 0;
    1916       14539 :   if (vy < 0) vy = 1;
    1917       14539 :   if (varncmp(vx, vy) >= 0)
    1918          14 :     pari_err_PRIORITY("polmodular_ZXX", pol_x(vx), "<=", vy);
    1919       14525 :   return gerepilecopy(av, RgM_to_RgXX(phi, vx, vy));
    1920             : }
    1921             : 
    1922             : INLINE GEN
    1923          56 : FpV_deriv(GEN v, long deg, GEN P)
    1924             : {
    1925          56 :   long i, ln = lg(v);
    1926          56 :   GEN dv = cgetg(ln, t_VEC);
    1927         392 :   for (i = ln-1; i > 1; i--, deg--) gel(dv, i) = Fp_mulu(gel(v, i-1), deg, P);
    1928          56 :   gel(dv, 1) = gen_0; return dv;
    1929             : }
    1930             : 
    1931             : GEN
    1932         119 : Fp_polmodular_evalx(long L, long inv, GEN J, GEN P, long v, int compute_derivs)
    1933             : {
    1934         119 :   pari_sp av = avma;
    1935             :   GEN db, phi;
    1936             : 
    1937         119 :   if (L <= modinv_max_internal_level(inv)) {
    1938             :     GEN tmp;
    1939          56 :     GEN phi = RgM_to_FpM(polmodular_ZM(L, inv), P);
    1940          56 :     GEN j_powers = Fp_powers(J, L + 1, P);
    1941          56 :     GEN modpol = RgV_to_RgX(FpM_FpC_mul(phi, j_powers, P), v);
    1942          56 :     if (compute_derivs) {
    1943          28 :       tmp = cgetg(4, t_VEC);
    1944          28 :       gel(tmp, 1) = modpol;
    1945          28 :       j_powers = FpV_deriv(j_powers, L + 1, P);
    1946          28 :       gel(tmp, 2) = RgV_to_RgX(FpM_FpC_mul(phi, j_powers, P), v);
    1947          28 :       j_powers = FpV_deriv(j_powers, L + 1, P);
    1948          28 :       gel(tmp, 3) = RgV_to_RgX(FpM_FpC_mul(phi, j_powers, P), v);
    1949             :     } else
    1950          28 :       tmp = modpol;
    1951          56 :     return gerepilecopy(av, tmp);
    1952             :   }
    1953             : 
    1954          63 :   db = polmodular_db_init(inv);
    1955          63 :   phi = polmodular0_ZM(L, inv, J, P, compute_derivs, &db);
    1956          63 :   phi = RgM_to_RgXV(phi, v);
    1957          63 :   gunclone_deep(db);
    1958          63 :   return gerepilecopy(av, compute_derivs? phi: gel(phi, 1));
    1959             : }
    1960             : 
    1961             : GEN
    1962         623 : polmodular(long L, long inv, GEN x, long v, long compute_derivs)
    1963             : {
    1964         623 :   pari_sp av = avma;
    1965             :   long tx;
    1966         623 :   GEN J = NULL, P = NULL, res = NULL, one = NULL;
    1967             : 
    1968         623 :   check_modinv(inv);
    1969         616 :   if (!x || gequalX(x)) {
    1970         483 :     long xv = 0;
    1971         483 :     if (x) xv = varn(x);
    1972         483 :     if (compute_derivs) pari_err_FLAG("polmodular");
    1973         476 :     return polmodular_ZXX(L, inv, xv, v);
    1974             :   }
    1975             : 
    1976         133 :   tx = typ(x);
    1977         133 :   if (tx == t_INTMOD) {
    1978          63 :     J = gel(x, 2);
    1979          63 :     P = gel(x, 1);
    1980          63 :     one = mkintmod(gen_1, P);
    1981          70 :   } else if (tx == t_FFELT) {
    1982          63 :     J = FF_to_FpXQ_i(x);
    1983          63 :     if (degpol(J) > 0)
    1984           7 :       pari_err_DOMAIN("polmodular", "x", "not in prime subfield ", gen_0, x);
    1985          56 :     J = constant_coeff(J);
    1986          56 :     P = FF_p_i(x);
    1987          56 :     one = p_to_FF(P, 0);
    1988             :   } else
    1989           7 :     pari_err_TYPE("polmodular", x);
    1990             : 
    1991         119 :   if (v < 0) v = 1;
    1992         119 :   res = Fp_polmodular_evalx(L, inv, J, P, v, compute_derivs);
    1993         119 :   return gerepileupto(av, gmul(res, one));
    1994             : }
    1995             : 
    1996             : /* SECTION: Modular polynomials of level <= MAX_INTERNAL_MODPOLY_LEVEL. */
    1997             : 
    1998             : /* These functions return a vector of coefficients of classical modular
    1999             :  * polynomials Phi_L(X,Y) of small level L.  The number of such coefficients is
    2000             :  * (L+1)(L+2)/2 since Phi is symmetric. We omit the common coefficient of
    2001             :  * X^{L+1} and Y^{L+1} since it is always 1. Use sympol_to_ZM() to get the
    2002             :  * corresponding desymmetrised matrix of coefficients */
    2003             : 
    2004             : /*  Phi2, the modular polynomial of level 2:
    2005             :  *
    2006             :  *  X^3 + X^2 * (-Y^2 + 1488*Y - 162000)
    2007             :  *      + X * (1488*Y^2 + 40773375*Y + 8748000000)
    2008             :  *      + Y^3 - 162000*Y^2 + 8748000000*Y - 157464000000000
    2009             :  *
    2010             :  *  [[3, 0, 1],
    2011             :  *   [2, 2, -1],
    2012             :  *   [2, 1, 1488],
    2013             :  *   [2, 0, -162000],
    2014             :  *   [1, 1, 40773375],
    2015             :  *   [1, 0, 8748000000],
    2016             :  *   [0, 0, -157464000000000]], */
    2017             : static GEN
    2018       15064 : phi2_ZV(void)
    2019             : {
    2020       15064 :   GEN phi2 = cgetg(7, t_VEC);
    2021       15064 :   gel(phi2, 1) = uu32toi(36662, 1908994048);
    2022       15064 :   setsigne(gel(phi2, 1), -1);
    2023       15064 :   gel(phi2, 2) = uu32toi(2, 158065408);
    2024       15064 :   gel(phi2, 3) = stoi(40773375);
    2025       15064 :   gel(phi2, 4) = stoi(-162000);
    2026       15064 :   gel(phi2, 5) = stoi(1488);
    2027       15064 :   gel(phi2, 6) = gen_m1;
    2028       15064 :   return phi2;
    2029             : }
    2030             : 
    2031             : /* L = 3
    2032             :  *
    2033             :  * [4, 0, 1],
    2034             :  * [3, 3, -1],
    2035             :  * [3, 2, 2232],
    2036             :  * [3, 1, -1069956],
    2037             :  * [3, 0, 36864000],
    2038             :  * [2, 2, 2587918086],
    2039             :  * [2, 1, 8900222976000],
    2040             :  * [2, 0, 452984832000000],
    2041             :  * [1, 1, -770845966336000000],
    2042             :  * [1, 0, 1855425871872000000000]
    2043             :  * [0, 0, 0]
    2044             :  *
    2045             :  * 1855425871872000000000 = 2^32 * (100 * 2^32 + 2503270400) */
    2046             : static GEN
    2047        1176 : phi3_ZV(void)
    2048             : {
    2049        1176 :   GEN phi3 = cgetg(11, t_VEC);
    2050        1176 :   pari_sp av = avma;
    2051        1176 :   gel(phi3, 1) = gen_0;
    2052        1176 :   gel(phi3, 2) = gerepileupto(av, shifti(uu32toi(100, 2503270400UL), 32));
    2053        1176 :   gel(phi3, 3) = uu32toi(179476562, 2147483648UL);
    2054        1176 :   setsigne(gel(phi3, 3), -1);
    2055        1176 :   gel(phi3, 4) = uu32toi(105468, 3221225472UL);
    2056        1176 :   gel(phi3, 5) = uu32toi(2072, 1050738688);
    2057        1176 :   gel(phi3, 6) = utoi(2587918086UL);
    2058        1176 :   gel(phi3, 7) = stoi(36864000);
    2059        1176 :   gel(phi3, 8) = stoi(-1069956);
    2060        1176 :   gel(phi3, 9) = stoi(2232);
    2061        1176 :   gel(phi3, 10) = gen_m1;
    2062        1176 :   return phi3;
    2063             : }
    2064             : 
    2065             : static GEN
    2066        1190 : phi5_ZV(void)
    2067             : {
    2068        1190 :   GEN phi5 = cgetg(22, t_VEC);
    2069        1190 :   gel(phi5, 1) = mkintn(5, 0x18c2cc9cUL, 0x484382b2UL, 0xdc000000UL, 0x0UL, 0x0UL);
    2070        1190 :   gel(phi5, 2) = mkintn(5, 0x2638fUL, 0x2ff02690UL, 0x68026000UL, 0x0UL, 0x0UL);
    2071        1190 :   gel(phi5, 3) = mkintn(5, 0x308UL, 0xac9d9a4UL, 0xe0fdab12UL, 0xc0000000UL, 0x0UL);
    2072        1190 :   setsigne(gel(phi5, 3), -1);
    2073        1190 :   gel(phi5, 4) = mkintn(5, 0x13UL, 0xaae09f9dUL, 0x1b5ef872UL, 0x30000000UL, 0x0UL);
    2074        1190 :   gel(phi5, 5) = mkintn(4, 0x1b802fa9UL, 0x77ba0653UL, 0xd2f78000UL, 0x0UL);
    2075        1190 :   gel(phi5, 6) = mkintn(4, 0xfbfdUL, 0x278e4756UL, 0xdf08a7c4UL, 0x40000000UL);
    2076        1190 :   gel(phi5, 7) = mkintn(4, 0x35f922UL, 0x62ccea6fUL, 0x153d0000UL, 0x0UL);
    2077        1190 :   gel(phi5, 8) = mkintn(4, 0x97dUL, 0x29203fafUL, 0xc3036909UL, 0x80000000UL);
    2078        1190 :   setsigne(gel(phi5, 8), -1);
    2079        1190 :   gel(phi5, 9) = mkintn(3, 0x56e9e892UL, 0xd7781867UL, 0xf2ea0000UL);
    2080        1190 :   gel(phi5, 10) = mkintn(3, 0x5d6dUL, 0xe0a58f4eUL, 0x9ee68c14UL);
    2081        1190 :   setsigne(gel(phi5, 10), -1);
    2082        1190 :   gel(phi5, 11) = mkintn(3, 0x1100dUL, 0x85cea769UL, 0x40000000UL);
    2083        1190 :   gel(phi5, 12) = mkintn(3, 0x1b38UL, 0x43cf461fUL, 0x3a900000UL);
    2084        1190 :   gel(phi5, 13) = mkintn(3, 0x14UL, 0xc45a616eUL, 0x4801680fUL);
    2085        1190 :   gel(phi5, 14) = uu32toi(0x17f4350UL, 0x493ca3e0UL);
    2086        1190 :   gel(phi5, 15) = uu32toi(0x183UL, 0xe54ce1f8UL);
    2087        1190 :   gel(phi5, 16) = uu32toi(0x1c9UL, 0x18860000UL);
    2088        1190 :   gel(phi5, 17) = uu32toi(0x39UL, 0x6f7a2206UL);
    2089        1190 :   setsigne(gel(phi5, 17), -1);
    2090        1190 :   gel(phi5, 18) = stoi(2028551200);
    2091        1190 :   gel(phi5, 19) = stoi(-4550940);
    2092        1190 :   gel(phi5, 20) = stoi(3720);
    2093        1190 :   gel(phi5, 21) = gen_m1;
    2094        1190 :   return phi5;
    2095             : }
    2096             : 
    2097             : static GEN
    2098         182 : phi5_f_ZV(void)
    2099             : {
    2100         182 :   GEN phi = zerovec(21);
    2101         182 :   gel(phi, 3) = stoi(4);
    2102         182 :   gel(phi, 21) = gen_m1;
    2103         182 :   return phi;
    2104             : }
    2105             : 
    2106             : static GEN
    2107          21 : phi3_f3_ZV(void)
    2108             : {
    2109          21 :   GEN phi = zerovec(10);
    2110          21 :   gel(phi, 3) = stoi(8);
    2111          21 :   gel(phi, 10) = gen_m1;
    2112          21 :   return phi;
    2113             : }
    2114             : 
    2115             : static GEN
    2116         119 : phi2_g2_ZV(void)
    2117             : {
    2118         119 :   GEN phi = zerovec(6);
    2119         119 :   gel(phi, 1) = stoi(-54000);
    2120         119 :   gel(phi, 3) = stoi(495);
    2121         119 :   gel(phi, 6) = gen_m1;
    2122         119 :   return phi;
    2123             : }
    2124             : 
    2125             : static GEN
    2126          56 : phi5_w2w3_ZV(void)
    2127             : {
    2128          56 :   GEN phi = zerovec(21);
    2129          56 :   gel(phi, 3) = gen_m1;
    2130          56 :   gel(phi, 10) = stoi(5);
    2131          56 :   gel(phi, 21) = gen_m1;
    2132          56 :   return phi;
    2133             : }
    2134             : 
    2135             : static GEN
    2136         112 : phi7_w2w5_ZV(void)
    2137             : {
    2138         112 :   GEN phi = zerovec(36);
    2139         112 :   gel(phi, 3) = gen_m1;
    2140         112 :   gel(phi, 15) = stoi(56);
    2141         112 :   gel(phi, 19) = stoi(42);
    2142         112 :   gel(phi, 24) = stoi(21);
    2143         112 :   gel(phi, 30) = stoi(7);
    2144         112 :   gel(phi, 36) = gen_m1;
    2145         112 :   return phi;
    2146             : }
    2147             : 
    2148             : static GEN
    2149          63 : phi5_w3w3_ZV(void)
    2150             : {
    2151          63 :   GEN phi = zerovec(21);
    2152          63 :   gel(phi, 3) = stoi(9);
    2153          63 :   gel(phi, 6) = stoi(-15);
    2154          63 :   gel(phi, 15) = stoi(5);
    2155          63 :   gel(phi, 21) = gen_m1;
    2156          63 :   return phi;
    2157             : }
    2158             : 
    2159             : static GEN
    2160         182 : phi3_w2w7_ZV(void)
    2161             : {
    2162         182 :   GEN phi = zerovec(10);
    2163         182 :   gel(phi, 3) = gen_m1;
    2164         182 :   gel(phi, 6) = stoi(3);
    2165         182 :   gel(phi, 10) = gen_m1;
    2166         182 :   return phi;
    2167             : }
    2168             : 
    2169             : static GEN
    2170          35 : phi2_w3w5_ZV(void)
    2171             : {
    2172          35 :   GEN phi = zerovec(6);
    2173          35 :   gel(phi, 3) = gen_1;
    2174          35 :   gel(phi, 6) = gen_m1;
    2175          35 :   return phi;
    2176             : }
    2177             : 
    2178             : static GEN
    2179          42 : phi5_w3w7_ZV(void)
    2180             : {
    2181          42 :   GEN phi = zerovec(21);
    2182          42 :   gel(phi, 3) = gen_m1;
    2183          42 :   gel(phi, 6) = stoi(10);
    2184          42 :   gel(phi, 8) = stoi(5);
    2185          42 :   gel(phi, 10) = stoi(35);
    2186          42 :   gel(phi, 13) = stoi(20);
    2187          42 :   gel(phi, 15) = stoi(10);
    2188          42 :   gel(phi, 17) = stoi(5);
    2189          42 :   gel(phi, 19) = stoi(5);
    2190          42 :   gel(phi, 21) = gen_m1;
    2191          42 :   return phi;
    2192             : }
    2193             : 
    2194             : static GEN
    2195          49 : phi3_w2w13_ZV(void)
    2196             : {
    2197          49 :   GEN phi = zerovec(10);
    2198          49 :   gel(phi, 3) = gen_m1;
    2199          49 :   gel(phi, 6) = stoi(3);
    2200          49 :   gel(phi, 8) = stoi(3);
    2201          49 :   gel(phi, 10) = gen_m1;
    2202          49 :   return phi;
    2203             : }
    2204             : 
    2205             : static GEN
    2206          21 : phi2_w3w3e2_ZV(void)
    2207             : {
    2208          21 :   GEN phi = zerovec(6);
    2209          21 :   gel(phi, 3) = stoi(3);
    2210          21 :   gel(phi, 6) = gen_m1;
    2211          21 :   return phi;
    2212             : }
    2213             : 
    2214             : static GEN
    2215          56 : phi2_w5w7_ZV(void)
    2216             : {
    2217          56 :   GEN phi = zerovec(6);
    2218          56 :   gel(phi, 3) = gen_1;
    2219          56 :   gel(phi, 5) = gen_2;
    2220          56 :   gel(phi, 6) = gen_m1;
    2221          56 :   return phi;
    2222             : }
    2223             : 
    2224             : static GEN
    2225          14 : phi2_w3w13_ZV(void)
    2226             : {
    2227          14 :   GEN phi = zerovec(6);
    2228          14 :   gel(phi, 3) = gen_m1;
    2229          14 :   gel(phi, 5) = gen_2;
    2230          14 :   gel(phi, 6) = gen_m1;
    2231          14 :   return phi;
    2232             : }
    2233             : 
    2234             : INLINE long
    2235         147 : modinv_parent(long inv)
    2236             : {
    2237         147 :   switch (inv) {
    2238          42 :     case INV_F2:
    2239             :     case INV_F4:
    2240          42 :     case INV_F8:     return INV_F;
    2241          14 :     case INV_W2W3E2: return INV_W2W3;
    2242          28 :     case INV_W2W5E2: return INV_W2W5;
    2243          63 :     case INV_W2W7E2: return INV_W2W7;
    2244           0 :     case INV_W3W3E2: return INV_W3W3;
    2245             :     default: pari_err_BUG("modinv_parent"); return -1;/*LCOV_EXCL_LINE*/
    2246             :   }
    2247             : }
    2248             : 
    2249             : /* TODO: Think of a better name than "parent power"; sheesh. */
    2250             : INLINE long
    2251         147 : modinv_parent_power(long inv)
    2252             : {
    2253         147 :   switch (inv) {
    2254          14 :     case INV_F4: return 4;
    2255          14 :     case INV_F8: return 8;
    2256         119 :     case INV_F2:
    2257             :     case INV_W2W3E2:
    2258             :     case INV_W2W5E2:
    2259             :     case INV_W2W7E2:
    2260         119 :     case INV_W3W3E2: return 2;
    2261             :     default: pari_err_BUG("modinv_parent_power"); return -1;/*LCOV_EXCL_LINE*/
    2262             :   }
    2263             : }
    2264             : 
    2265             : static GEN
    2266         147 : polmodular0_powerup_ZM(long L, long inv, GEN *db)
    2267             : {
    2268         147 :   pari_sp ltop = avma, av;
    2269             :   long s, D, nprimes, N;
    2270             :   GEN mp, pol, P, H;
    2271         147 :   long parent = modinv_parent(inv);
    2272         147 :   long e = modinv_parent_power(inv);
    2273             :   disc_info Ds[MODPOLY_MAX_DCNT];
    2274             :   /* FIXME: We throw away the table of fundamental discriminants here. */
    2275         147 :   long nDs = discriminant_with_classno_at_least(Ds, L, inv, NULL, IGNORE_SPARSE_FACTOR);
    2276         147 :   if (nDs != 1) pari_err_BUG("polmodular0_powerup_ZM");
    2277         147 :   D = Ds[0].D1;
    2278         147 :   nprimes = Ds[0].nprimes + 1;
    2279         147 :   mp = polmodular0_ZM(L, parent, NULL, NULL, 0, db);
    2280         147 :   H = polclass0(D, parent, 0, db);
    2281             : 
    2282         147 :   N = L + 2;
    2283         147 :   if (degpol(H) < N) pari_err_BUG("polmodular0_powerup_ZM");
    2284             : 
    2285         147 :   av = avma;
    2286         147 :   pol = ZM_init_CRT(zero_Flm_copy(N, L + 2), 1);
    2287         147 :   P = gen_1;
    2288         497 :   for (s = 1; s < nprimes; ++s) {
    2289             :     pari_sp av1, av2;
    2290         350 :     ulong p = Ds[0].primes[s-1], pi = get_Fl_red(p);
    2291             :     long i;
    2292             :     GEN Hrts, js, Hp, Phip, coeff_mat, phi_modp;
    2293             : 
    2294         350 :     phi_modp = zero_Flm_copy(N, L + 2);
    2295         350 :     av1 = avma;
    2296         350 :     Hp = ZX_to_Flx(H, p);
    2297         350 :     Hrts = Flx_roots(Hp, p);
    2298         350 :     if (lg(Hrts)-1 < N) pari_err_BUG("polmodular0_powerup_ZM");
    2299         350 :     js = cgetg(N + 1, t_VECSMALL);
    2300        2716 :     for (i = 1; i <= N; ++i)
    2301        2366 :       uel(js, i) = Fl_powu_pre(uel(Hrts, i), e, p, pi);
    2302             : 
    2303         350 :     Phip = ZM_to_Flm(mp, p);
    2304         350 :     coeff_mat = zero_Flm_copy(N, L + 2);
    2305         350 :     av2 = avma;
    2306        2716 :     for (i = 1; i <= N; ++i) {
    2307             :       long k;
    2308             :       GEN phi_at_ji, mprts;
    2309             : 
    2310        2366 :       phi_at_ji = Flm_Fl_polmodular_evalx(Phip, L, uel(Hrts, i), p, pi);
    2311        2366 :       mprts = Flx_roots(phi_at_ji, p);
    2312        2366 :       if (lg(mprts) != L + 2) pari_err_BUG("polmodular0_powerup_ZM");
    2313             : 
    2314        2366 :       Flv_powu_inplace_pre(mprts, e, p, pi);
    2315        2366 :       phi_at_ji = Flv_roots_to_pol(mprts, p, 0);
    2316             : 
    2317       19180 :       for (k = 1; k <= L + 2; ++k)
    2318       16814 :         ucoeff(coeff_mat, i, k) = uel(phi_at_ji, k + 1);
    2319        2366 :       set_avma(av2);
    2320             :     }
    2321             : 
    2322         350 :     interpolate_coeffs(phi_modp, p, js, coeff_mat);
    2323         350 :     set_avma(av1);
    2324             : 
    2325         350 :     (void) ZM_incremental_CRT(&pol, phi_modp, &P, p);
    2326         350 :     if (gc_needed(av, 2)) gerepileall(av, 2, &pol, &P);
    2327             :   }
    2328         147 :   killblock((GEN)Ds[0].primes); return gerepileupto(ltop, pol);
    2329             : }
    2330             : 
    2331             : /* Returns the modular polynomial with the smallest level for the given
    2332             :  * invariant, except if inv is INV_J, in which case return the modular
    2333             :  * polynomial of level L in {2,3,5}.  NULL is returned if the modular
    2334             :  * polynomial can be calculated using polmodular0_powerup_ZM. */
    2335             : INLINE GEN
    2336       18529 : internal_db(long L, long inv)
    2337             : {
    2338       18529 :   switch (inv) {
    2339       17430 :   case INV_J: switch (L) {
    2340       15064 :     case 2: return phi2_ZV();
    2341        1176 :     case 3: return phi3_ZV();
    2342        1190 :     case 5: return phi5_ZV();
    2343           0 :     default: break;
    2344             :   }
    2345         182 :   case INV_F: return phi5_f_ZV();
    2346          14 :   case INV_F2: return NULL;
    2347          21 :   case INV_F3: return phi3_f3_ZV();
    2348          14 :   case INV_F4: return NULL;
    2349         119 :   case INV_G2: return phi2_g2_ZV();
    2350          56 :   case INV_W2W3: return phi5_w2w3_ZV();
    2351          14 :   case INV_F8: return NULL;
    2352          63 :   case INV_W3W3: return phi5_w3w3_ZV();
    2353         112 :   case INV_W2W5: return phi7_w2w5_ZV();
    2354         182 :   case INV_W2W7: return phi3_w2w7_ZV();
    2355          35 :   case INV_W3W5: return phi2_w3w5_ZV();
    2356          42 :   case INV_W3W7: return phi5_w3w7_ZV();
    2357          14 :   case INV_W2W3E2: return NULL;
    2358          28 :   case INV_W2W5E2: return NULL;
    2359          49 :   case INV_W2W13: return phi3_w2w13_ZV();
    2360          63 :   case INV_W2W7E2: return NULL;
    2361          21 :   case INV_W3W3E2: return phi2_w3w3e2_ZV();
    2362          56 :   case INV_W5W7: return phi2_w5w7_ZV();
    2363          14 :   case INV_W3W13: return phi2_w3w13_ZV();
    2364             :   }
    2365           0 :   pari_err_BUG("internal_db");
    2366             :   return NULL;/*LCOV_EXCL_LINE*/
    2367             : }
    2368             : 
    2369             : /* NB: Should only be called if L <= modinv_max_internal_level(inv) */
    2370             : static GEN
    2371       18529 : polmodular_small_ZM(long L, long inv, GEN *db)
    2372             : {
    2373       18529 :   GEN f = internal_db(L, inv);
    2374       18529 :   if (!f) return polmodular0_powerup_ZM(L, inv, db);
    2375       18382 :   return sympol_to_ZM(f, L);
    2376             : }
    2377             : 
    2378             : /* Each function phi_w?w?_j() returns a vector V containing two
    2379             :  * vectors u and v, and a scalar k, which together represent the
    2380             :  * bivariate polnomial
    2381             :  *
    2382             :  *   phi(X, Y) = \sum_i u[i] X^i + Y \sum_i v[i] X^i + Y^2 X^k
    2383             :  */
    2384             : static GEN
    2385        1078 : phi_w2w3_j(void)
    2386             : {
    2387             :   GEN phi, phi0, phi1;
    2388        1078 :   phi = cgetg(4, t_VEC);
    2389             : 
    2390        1078 :   phi0 = cgetg(14, t_VEC);
    2391        1078 :   gel(phi0, 1) = gen_1;
    2392        1078 :   gel(phi0, 2) = utoineg(0x3cUL);
    2393        1078 :   gel(phi0, 3) = utoi(0x702UL);
    2394        1078 :   gel(phi0, 4) = utoineg(0x797cUL);
    2395        1078 :   gel(phi0, 5) = utoi(0x5046fUL);
    2396        1078 :   gel(phi0, 6) = utoineg(0x1be0b8UL);
    2397        1078 :   gel(phi0, 7) = utoi(0x28ef9cUL);
    2398        1078 :   gel(phi0, 8) = utoi(0x15e2968UL);
    2399        1078 :   gel(phi0, 9) = utoi(0x1b8136fUL);
    2400        1078 :   gel(phi0, 10) = utoi(0xa67674UL);
    2401        1078 :   gel(phi0, 11) = utoi(0x23982UL);
    2402        1078 :   gel(phi0, 12) = utoi(0x294UL);
    2403        1078 :   gel(phi0, 13) = gen_1;
    2404             : 
    2405        1078 :   phi1 = cgetg(13, t_VEC);
    2406        1078 :   gel(phi1, 1) = gen_0;
    2407        1078 :   gel(phi1, 2) = gen_0;
    2408        1078 :   gel(phi1, 3) = gen_m1;
    2409        1078 :   gel(phi1, 4) = utoi(0x23UL);
    2410        1078 :   gel(phi1, 5) = utoineg(0xaeUL);
    2411        1078 :   gel(phi1, 6) = utoineg(0x5b8UL);
    2412        1078 :   gel(phi1, 7) = utoi(0x12d7UL);
    2413        1078 :   gel(phi1, 8) = utoineg(0x7c86UL);
    2414        1078 :   gel(phi1, 9) = utoi(0x37c8UL);
    2415        1078 :   gel(phi1, 10) = utoineg(0x69cUL);
    2416        1078 :   gel(phi1, 11) = utoi(0x48UL);
    2417        1078 :   gel(phi1, 12) = gen_m1;
    2418             : 
    2419        1078 :   gel(phi, 1) = phi0;
    2420        1078 :   gel(phi, 2) = phi1;
    2421        1078 :   gel(phi, 3) = utoi(5); return phi;
    2422             : }
    2423             : 
    2424             : static GEN
    2425        3517 : phi_w3w3_j(void)
    2426             : {
    2427             :   GEN phi, phi0, phi1;
    2428        3517 :   phi = cgetg(4, t_VEC);
    2429             : 
    2430        3517 :   phi0 = cgetg(14, t_VEC);
    2431        3517 :   gel(phi0, 1) = utoi(0x2d9UL);
    2432        3517 :   gel(phi0, 2) = utoi(0x4fbcUL);
    2433        3517 :   gel(phi0, 3) = utoi(0x5828aUL);
    2434        3517 :   gel(phi0, 4) = utoi(0x3a7a3cUL);
    2435        3517 :   gel(phi0, 5) = utoi(0x1bd8edfUL);
    2436        3517 :   gel(phi0, 6) = utoi(0x8348838UL);
    2437        3517 :   gel(phi0, 7) = utoi(0x1983f8acUL);
    2438        3517 :   gel(phi0, 8) = utoi(0x14e4e098UL);
    2439        3517 :   gel(phi0, 9) = utoi(0x69ed1a7UL);
    2440        3517 :   gel(phi0, 10) = utoi(0xc3828cUL);
    2441        3517 :   gel(phi0, 11) = utoi(0x2696aUL);
    2442        3517 :   gel(phi0, 12) = utoi(0x2acUL);
    2443        3517 :   gel(phi0, 13) = gen_1;
    2444             : 
    2445        3517 :   phi1 = cgetg(13, t_VEC);
    2446        3517 :   gel(phi1, 1) = gen_0;
    2447        3517 :   gel(phi1, 2) = utoineg(0x1bUL);
    2448        3517 :   gel(phi1, 3) = utoineg(0x5d6UL);
    2449        3517 :   gel(phi1, 4) = utoineg(0x1c7bUL);
    2450        3517 :   gel(phi1, 5) = utoi(0x7980UL);
    2451        3517 :   gel(phi1, 6) = utoi(0x12168UL);
    2452        3517 :   gel(phi1, 7) = utoineg(0x3528UL);
    2453        3517 :   gel(phi1, 8) = utoineg(0x6174UL);
    2454        3517 :   gel(phi1, 9) = utoi(0x2208UL);
    2455        3517 :   gel(phi1, 10) = utoineg(0x41dUL);
    2456        3517 :   gel(phi1, 11) = utoi(0x36UL);
    2457        3517 :   gel(phi1, 12) = gen_m1;
    2458             : 
    2459        3517 :   gel(phi, 1) = phi0;
    2460        3517 :   gel(phi, 2) = phi1;
    2461        3517 :   gel(phi, 3) = gen_2; return phi;
    2462             : }
    2463             : 
    2464             : static GEN
    2465        3234 : phi_w2w5_j(void)
    2466             : {
    2467             :   GEN phi, phi0, phi1;
    2468        3234 :   phi = cgetg(4, t_VEC);
    2469             : 
    2470        3234 :   phi0 = cgetg(20, t_VEC);
    2471        3234 :   gel(phi0, 1) = gen_1;
    2472        3234 :   gel(phi0, 2) = utoineg(0x2aUL);
    2473        3234 :   gel(phi0, 3) = utoi(0x549UL);
    2474        3234 :   gel(phi0, 4) = utoineg(0x6530UL);
    2475        3234 :   gel(phi0, 5) = utoi(0x60504UL);
    2476        3234 :   gel(phi0, 6) = utoineg(0x3cbbc8UL);
    2477        3234 :   gel(phi0, 7) = utoi(0x1d1ee74UL);
    2478        3234 :   gel(phi0, 8) = utoineg(0x7ef9ab0UL);
    2479        3234 :   gel(phi0, 9) = utoi(0x12b888beUL);
    2480        3234 :   gel(phi0, 10) = utoineg(0x15fa174cUL);
    2481        3234 :   gel(phi0, 11) = utoi(0x615d9feUL);
    2482        3234 :   gel(phi0, 12) = utoi(0xbeca070UL);
    2483        3234 :   gel(phi0, 13) = utoineg(0x88de74cUL);
    2484        3234 :   gel(phi0, 14) = utoineg(0x2b3a268UL);
    2485        3234 :   gel(phi0, 15) = utoi(0x24b3244UL);
    2486        3234 :   gel(phi0, 16) = utoi(0xb56270UL);
    2487        3234 :   gel(phi0, 17) = utoi(0x25989UL);
    2488        3234 :   gel(phi0, 18) = utoi(0x2a6UL);
    2489        3234 :   gel(phi0, 19) = gen_1;
    2490             : 
    2491        3234 :   phi1 = cgetg(19, t_VEC);
    2492        3234 :   gel(phi1, 1) = gen_0;
    2493        3234 :   gel(phi1, 2) = gen_0;
    2494        3234 :   gel(phi1, 3) = gen_m1;
    2495        3234 :   gel(phi1, 4) = utoi(0x1eUL);
    2496        3234 :   gel(phi1, 5) = utoineg(0xffUL);
    2497        3234 :   gel(phi1, 6) = utoi(0x243UL);
    2498        3234 :   gel(phi1, 7) = utoineg(0xf3UL);
    2499        3234 :   gel(phi1, 8) = utoineg(0x5c4UL);
    2500        3234 :   gel(phi1, 9) = utoi(0x107bUL);
    2501        3234 :   gel(phi1, 10) = utoineg(0x11b2fUL);
    2502        3234 :   gel(phi1, 11) = utoi(0x48fa8UL);
    2503        3234 :   gel(phi1, 12) = utoineg(0x6ff7cUL);
    2504        3234 :   gel(phi1, 13) = utoi(0x4bf48UL);
    2505        3234 :   gel(phi1, 14) = utoineg(0x187efUL);
    2506        3234 :   gel(phi1, 15) = utoi(0x404cUL);
    2507        3234 :   gel(phi1, 16) = utoineg(0x582UL);
    2508        3234 :   gel(phi1, 17) = utoi(0x3cUL);
    2509        3234 :   gel(phi1, 18) = gen_m1;
    2510             : 
    2511        3234 :   gel(phi, 1) = phi0;
    2512        3234 :   gel(phi, 2) = phi1;
    2513        3234 :   gel(phi, 3) = utoi(7); return phi;
    2514             : }
    2515             : 
    2516             : static GEN
    2517        6591 : phi_w2w7_j(void)
    2518             : {
    2519             :   GEN phi, phi0, phi1;
    2520        6591 :   phi = cgetg(4, t_VEC);
    2521             : 
    2522        6591 :   phi0 = cgetg(26, t_VEC);
    2523        6591 :   gel(phi0, 1) = gen_1;
    2524        6591 :   gel(phi0, 2) = utoineg(0x24UL);
    2525        6591 :   gel(phi0, 3) = utoi(0x4ceUL);
    2526        6591 :   gel(phi0, 4) = utoineg(0x5d60UL);
    2527        6591 :   gel(phi0, 5) = utoi(0x62b05UL);
    2528        6591 :   gel(phi0, 6) = utoineg(0x47be78UL);
    2529        6591 :   gel(phi0, 7) = utoi(0x2a3880aUL);
    2530        6591 :   gel(phi0, 8) = utoineg(0x114bccf4UL);
    2531        6591 :   gel(phi0, 9) = utoi(0x4b95e79aUL);
    2532        6591 :   gel(phi0, 10) = utoineg(0xe2cfee1cUL);
    2533        6591 :   gel(phi0, 11) = uu32toi(0x1UL, 0xe43d1126UL);
    2534        6591 :   gel(phi0, 12) = uu32toineg(0x2UL, 0xf04dc6f8UL);
    2535        6591 :   gel(phi0, 13) = uu32toi(0x3UL, 0x5384987dUL);
    2536        6591 :   gel(phi0, 14) = uu32toineg(0x2UL, 0xa5ccbe18UL);
    2537        6591 :   gel(phi0, 15) = uu32toi(0x1UL, 0x4c52c8a6UL);
    2538        6591 :   gel(phi0, 16) = utoineg(0x2643fdecUL);
    2539        6591 :   gel(phi0, 17) = utoineg(0x49f5ab66UL);
    2540        6591 :   gel(phi0, 18) = utoi(0x33074d3cUL);
    2541        6591 :   gel(phi0, 19) = utoineg(0x6a3e376UL);
    2542        6591 :   gel(phi0, 20) = utoineg(0x675aa58UL);
    2543        6591 :   gel(phi0, 21) = utoi(0x2674005UL);
    2544        6591 :   gel(phi0, 22) = utoi(0xba5be0UL);
    2545        6591 :   gel(phi0, 23) = utoi(0x2644eUL);
    2546        6591 :   gel(phi0, 24) = utoi(0x2acUL);
    2547        6591 :   gel(phi0, 25) = gen_1;
    2548             : 
    2549        6591 :   phi1 = cgetg(25, t_VEC);
    2550        6591 :   gel(phi1, 1) = gen_0;
    2551        6591 :   gel(phi1, 2) = gen_0;
    2552        6591 :   gel(phi1, 3) = gen_m1;
    2553        6591 :   gel(phi1, 4) = utoi(0x1cUL);
    2554        6591 :   gel(phi1, 5) = utoineg(0x10aUL);
    2555        6591 :   gel(phi1, 6) = utoi(0x3f0UL);
    2556        6591 :   gel(phi1, 7) = utoineg(0x5d3UL);
    2557        6591 :   gel(phi1, 8) = utoi(0x3efUL);
    2558        6591 :   gel(phi1, 9) = utoineg(0x102UL);
    2559        6591 :   gel(phi1, 10) = utoineg(0x5c8UL);
    2560        6591 :   gel(phi1, 11) = utoi(0x102fUL);
    2561        6591 :   gel(phi1, 12) = utoineg(0x13f8aUL);
    2562        6591 :   gel(phi1, 13) = utoi(0x86538UL);
    2563        6591 :   gel(phi1, 14) = utoineg(0x1bbd10UL);
    2564        6591 :   gel(phi1, 15) = utoi(0x3614e8UL);
    2565        6591 :   gel(phi1, 16) = utoineg(0x42f793UL);
    2566        6591 :   gel(phi1, 17) = utoi(0x364698UL);
    2567        6591 :   gel(phi1, 18) = utoineg(0x1c7a10UL);
    2568        6591 :   gel(phi1, 19) = utoi(0x97cc8UL);
    2569        6591 :   gel(phi1, 20) = utoineg(0x1fc8aUL);
    2570        6591 :   gel(phi1, 21) = utoi(0x4210UL);
    2571        6591 :   gel(phi1, 22) = utoineg(0x524UL);
    2572        6591 :   gel(phi1, 23) = utoi(0x38UL);
    2573        6591 :   gel(phi1, 24) = gen_m1;
    2574             : 
    2575        6591 :   gel(phi, 1) = phi0;
    2576        6591 :   gel(phi, 2) = phi1;
    2577        6591 :   gel(phi, 3) = utoi(9); return phi;
    2578             : }
    2579             : 
    2580             : static GEN
    2581        2933 : phi_w2w13_j(void)
    2582             : {
    2583             :   GEN phi, phi0, phi1;
    2584        2933 :   phi = cgetg(4, t_VEC);
    2585             : 
    2586        2933 :   phi0 = cgetg(44, t_VEC);
    2587        2933 :   gel(phi0, 1) = gen_1;
    2588        2933 :   gel(phi0, 2) = utoineg(0x1eUL);
    2589        2933 :   gel(phi0, 3) = utoi(0x45fUL);
    2590        2933 :   gel(phi0, 4) = utoineg(0x5590UL);
    2591        2933 :   gel(phi0, 5) = utoi(0x64407UL);
    2592        2933 :   gel(phi0, 6) = utoineg(0x53a792UL);
    2593        2933 :   gel(phi0, 7) = utoi(0x3b21af3UL);
    2594        2933 :   gel(phi0, 8) = utoineg(0x20d056d0UL);
    2595        2933 :   gel(phi0, 9) = utoi(0xe02db4a6UL);
    2596        2933 :   gel(phi0, 10) = uu32toineg(0x4UL, 0xb23400b0UL);
    2597        2933 :   gel(phi0, 11) = uu32toi(0x14UL, 0x57fbb906UL);
    2598        2933 :   gel(phi0, 12) = uu32toineg(0x49UL, 0xcf80c00UL);
    2599        2933 :   gel(phi0, 13) = uu32toi(0xdeUL, 0x84ff421UL);
    2600        2933 :   gel(phi0, 14) = uu32toineg(0x244UL, 0xc500c156UL);
    2601        2933 :   gel(phi0, 15) = uu32toi(0x52cUL, 0x79162979UL);
    2602        2933 :   gel(phi0, 16) = uu32toineg(0xa64UL, 0x8edc5650UL);
    2603        2933 :   gel(phi0, 17) = uu32toi(0x1289UL, 0x4225bb41UL);
    2604        2933 :   gel(phi0, 18) = uu32toineg(0x1d89UL, 0x2a15229aUL);
    2605        2933 :   gel(phi0, 19) = uu32toi(0x2a3eUL, 0x4539f1ebUL);
    2606        2933 :   gel(phi0, 20) = uu32toineg(0x366aUL, 0xa5ea1130UL);
    2607        2933 :   gel(phi0, 21) = uu32toi(0x3f47UL, 0xa19fecb4UL);
    2608        2933 :   gel(phi0, 22) = uu32toineg(0x4282UL, 0x91a3c4a0UL);
    2609        2933 :   gel(phi0, 23) = uu32toi(0x3f30UL, 0xbaa305b4UL);
    2610        2933 :   gel(phi0, 24) = uu32toineg(0x3635UL, 0xd11c2530UL);
    2611        2933 :   gel(phi0, 25) = uu32toi(0x29e2UL, 0x89df27ebUL);
    2612        2933 :   gel(phi0, 26) = uu32toineg(0x1d03UL, 0x6509d48aUL);
    2613        2933 :   gel(phi0, 27) = uu32toi(0x11e2UL, 0x272cc601UL);
    2614        2933 :   gel(phi0, 28) = uu32toineg(0x9b0UL, 0xacd58ff0UL);
    2615        2933 :   gel(phi0, 29) = uu32toi(0x485UL, 0x608d7db9UL);
    2616        2933 :   gel(phi0, 30) = uu32toineg(0x1bfUL, 0xa941546UL);
    2617        2933 :   gel(phi0, 31) = uu32toi(0x82UL, 0x56e48b21UL);
    2618        2933 :   gel(phi0, 32) = uu32toineg(0x13UL, 0xc36b2340UL);
    2619        2933 :   gel(phi0, 33) = uu32toineg(0x5UL, 0x6637257aUL);
    2620        2933 :   gel(phi0, 34) = uu32toi(0x5UL, 0x40f70bd0UL);
    2621        2933 :   gel(phi0, 35) = uu32toineg(0x1UL, 0xf70842daUL);
    2622        2933 :   gel(phi0, 36) = utoi(0x53eea5f0UL);
    2623        2933 :   gel(phi0, 37) = utoi(0xda17bf3UL);
    2624        2933 :   gel(phi0, 38) = utoineg(0xaf246c2UL);
    2625        2933 :   gel(phi0, 39) = utoi(0x278f847UL);
    2626        2933 :   gel(phi0, 40) = utoi(0xbf5550UL);
    2627        2933 :   gel(phi0, 41) = utoi(0x26f1fUL);
    2628        2933 :   gel(phi0, 42) = utoi(0x2b2UL);
    2629        2933 :   gel(phi0, 43) = gen_1;
    2630             : 
    2631        2933 :   phi1 = cgetg(43, t_VEC);
    2632        2933 :   gel(phi1, 1) = gen_0;
    2633        2933 :   gel(phi1, 2) = gen_0;
    2634        2933 :   gel(phi1, 3) = gen_m1;
    2635        2933 :   gel(phi1, 4) = utoi(0x1aUL);
    2636        2933 :   gel(phi1, 5) = utoineg(0x111UL);
    2637        2933 :   gel(phi1, 6) = utoi(0x5e4UL);
    2638        2933 :   gel(phi1, 7) = utoineg(0x1318UL);
    2639        2933 :   gel(phi1, 8) = utoi(0x2804UL);
    2640        2933 :   gel(phi1, 9) = utoineg(0x3cd6UL);
    2641        2933 :   gel(phi1, 10) = utoi(0x467cUL);
    2642        2933 :   gel(phi1, 11) = utoineg(0x3cd6UL);
    2643        2933 :   gel(phi1, 12) = utoi(0x2804UL);
    2644        2933 :   gel(phi1, 13) = utoineg(0x1318UL);
    2645        2933 :   gel(phi1, 14) = utoi(0x5e3UL);
    2646        2933 :   gel(phi1, 15) = utoineg(0x10dUL);
    2647        2933 :   gel(phi1, 16) = utoineg(0x5ccUL);
    2648        2933 :   gel(phi1, 17) = utoi(0x100bUL);
    2649        2933 :   gel(phi1, 18) = utoineg(0x160e1UL);
    2650        2933 :   gel(phi1, 19) = utoi(0xd2cb0UL);
    2651        2933 :   gel(phi1, 20) = utoineg(0x4c85fcUL);
    2652        2933 :   gel(phi1, 21) = utoi(0x137cb98UL);
    2653        2933 :   gel(phi1, 22) = utoineg(0x3c75568UL);
    2654        2933 :   gel(phi1, 23) = utoi(0x95c69c8UL);
    2655        2933 :   gel(phi1, 24) = utoineg(0x131557bcUL);
    2656        2933 :   gel(phi1, 25) = utoi(0x20aacfd0UL);
    2657        2933 :   gel(phi1, 26) = utoineg(0x2f9164e6UL);
    2658        2933 :   gel(phi1, 27) = utoi(0x3b6a5e40UL);
    2659        2933 :   gel(phi1, 28) = utoineg(0x3ff54344UL);
    2660        2933 :   gel(phi1, 29) = utoi(0x3b6a9140UL);
    2661        2933 :   gel(phi1, 30) = utoineg(0x2f927fa6UL);
    2662        2933 :   gel(phi1, 31) = utoi(0x20ae6450UL);
    2663        2933 :   gel(phi1, 32) = utoineg(0x131cd87cUL);
    2664        2933 :   gel(phi1, 33) = utoi(0x967d1e8UL);
    2665        2933 :   gel(phi1, 34) = utoineg(0x3d48ca8UL);
    2666        2933 :   gel(phi1, 35) = utoi(0x14333b8UL);
    2667        2933 :   gel(phi1, 36) = utoineg(0x5406bcUL);
    2668        2933 :   gel(phi1, 37) = utoi(0x10c130UL);
    2669        2933 :   gel(phi1, 38) = utoineg(0x27ba1UL);
    2670        2933 :   gel(phi1, 39) = utoi(0x433cUL);
    2671        2933 :   gel(phi1, 40) = utoineg(0x4c6UL);
    2672        2933 :   gel(phi1, 41) = utoi(0x34UL);
    2673        2933 :   gel(phi1, 42) = gen_m1;
    2674             : 
    2675        2933 :   gel(phi, 1) = phi0;
    2676        2933 :   gel(phi, 2) = phi1;
    2677        2933 :   gel(phi, 3) = utoi(15); return phi;
    2678             : }
    2679             : 
    2680             : static GEN
    2681        1146 : phi_w3w5_j(void)
    2682             : {
    2683             :   GEN phi, phi0, phi1;
    2684        1146 :   phi = cgetg(4, t_VEC);
    2685             : 
    2686        1146 :   phi0 = cgetg(26, t_VEC);
    2687        1146 :   gel(phi0, 1) = gen_1;
    2688        1146 :   gel(phi0, 2) = utoi(0x18UL);
    2689        1146 :   gel(phi0, 3) = utoi(0xb4UL);
    2690        1146 :   gel(phi0, 4) = utoineg(0x178UL);
    2691        1146 :   gel(phi0, 5) = utoineg(0x2d7eUL);
    2692        1146 :   gel(phi0, 6) = utoineg(0x89b8UL);
    2693        1146 :   gel(phi0, 7) = utoi(0x35c24UL);
    2694        1146 :   gel(phi0, 8) = utoi(0x128a18UL);
    2695        1146 :   gel(phi0, 9) = utoineg(0x12a911UL);
    2696        1146 :   gel(phi0, 10) = utoineg(0xcc0190UL);
    2697        1146 :   gel(phi0, 11) = utoi(0x94368UL);
    2698        1146 :   gel(phi0, 12) = utoi(0x1439d0UL);
    2699        1146 :   gel(phi0, 13) = utoi(0x96f931cUL);
    2700        1146 :   gel(phi0, 14) = utoineg(0x1f59ff0UL);
    2701        1146 :   gel(phi0, 15) = utoi(0x20e7e8UL);
    2702        1146 :   gel(phi0, 16) = utoineg(0x25fdf150UL);
    2703        1146 :   gel(phi0, 17) = utoineg(0x7091511UL);
    2704        1146 :   gel(phi0, 18) = utoi(0x1ef52f8UL);
    2705        1146 :   gel(phi0, 19) = utoi(0x341f2de4UL);
    2706        1146 :   gel(phi0, 20) = utoi(0x25d72c28UL);
    2707        1146 :   gel(phi0, 21) = utoi(0x95d2082UL);
    2708        1146 :   gel(phi0, 22) = utoi(0xd2d828UL);
    2709        1146 :   gel(phi0, 23) = utoi(0x281f4UL);
    2710        1146 :   gel(phi0, 24) = utoi(0x2b8UL);
    2711        1146 :   gel(phi0, 25) = gen_1;
    2712             : 
    2713        1146 :   phi1 = cgetg(25, t_VEC);
    2714        1146 :   gel(phi1, 1) = gen_0;
    2715        1146 :   gel(phi1, 2) = gen_0;
    2716        1146 :   gel(phi1, 3) = gen_0;
    2717        1146 :   gel(phi1, 4) = gen_1;
    2718        1146 :   gel(phi1, 5) = utoi(0xfUL);
    2719        1146 :   gel(phi1, 6) = utoi(0x2eUL);
    2720        1146 :   gel(phi1, 7) = utoineg(0x1fUL);
    2721        1146 :   gel(phi1, 8) = utoineg(0x2dUL);
    2722        1146 :   gel(phi1, 9) = utoineg(0x5caUL);
    2723        1146 :   gel(phi1, 10) = utoineg(0x358UL);
    2724        1146 :   gel(phi1, 11) = utoi(0x2f1cUL);
    2725        1146 :   gel(phi1, 12) = utoi(0xd8eaUL);
    2726        1146 :   gel(phi1, 13) = utoineg(0x38c70UL);
    2727        1146 :   gel(phi1, 14) = utoineg(0x1a964UL);
    2728        1146 :   gel(phi1, 15) = utoi(0x93512UL);
    2729        1146 :   gel(phi1, 16) = utoineg(0x58f2UL);
    2730        1146 :   gel(phi1, 17) = utoineg(0x5af1eUL);
    2731        1146 :   gel(phi1, 18) = utoi(0x1afb8UL);
    2732        1146 :   gel(phi1, 19) = utoi(0xc084UL);
    2733        1146 :   gel(phi1, 20) = utoineg(0x7fcbUL);
    2734        1146 :   gel(phi1, 21) = utoi(0x1c89UL);
    2735        1146 :   gel(phi1, 22) = utoineg(0x32aUL);
    2736        1146 :   gel(phi1, 23) = utoi(0x2dUL);
    2737        1146 :   gel(phi1, 24) = gen_m1;
    2738             : 
    2739        1146 :   gel(phi, 1) = phi0;
    2740        1146 :   gel(phi, 2) = phi1;
    2741        1146 :   gel(phi, 3) = utoi(8); return phi;
    2742             : }
    2743             : 
    2744             : static GEN
    2745        2412 : phi_w3w7_j(void)
    2746             : {
    2747             :   GEN phi, phi0, phi1;
    2748        2412 :   phi = cgetg(4, t_VEC);
    2749             : 
    2750        2412 :   phi0 = cgetg(34, t_VEC);
    2751        2412 :   gel(phi0, 1) = gen_1;
    2752        2412 :   gel(phi0, 2) = utoineg(0x14UL);
    2753        2412 :   gel(phi0, 3) = utoi(0x82UL);
    2754        2412 :   gel(phi0, 4) = utoi(0x1f8UL);
    2755        2412 :   gel(phi0, 5) = utoineg(0x2a45UL);
    2756        2412 :   gel(phi0, 6) = utoi(0x9300UL);
    2757        2412 :   gel(phi0, 7) = utoi(0x32abeUL);
    2758        2412 :   gel(phi0, 8) = utoineg(0x19c91cUL);
    2759        2412 :   gel(phi0, 9) = utoi(0xc1ba9UL);
    2760        2412 :   gel(phi0, 10) = utoi(0x1788f68UL);
    2761        2412 :   gel(phi0, 11) = utoineg(0x2b1989cUL);
    2762        2412 :   gel(phi0, 12) = utoineg(0x7a92408UL);
    2763        2412 :   gel(phi0, 13) = utoi(0x1238d56eUL);
    2764        2412 :   gel(phi0, 14) = utoi(0x13dd66a0UL);
    2765        2412 :   gel(phi0, 15) = utoineg(0x2dbedca8UL);
    2766        2412 :   gel(phi0, 16) = utoineg(0x34282eb8UL);
    2767        2412 :   gel(phi0, 17) = utoi(0x2c2a54d2UL);
    2768        2412 :   gel(phi0, 18) = utoi(0x98db81a8UL);
    2769        2412 :   gel(phi0, 19) = utoineg(0x4088be8UL);
    2770        2412 :   gel(phi0, 20) = utoineg(0xe424a220UL);
    2771        2412 :   gel(phi0, 21) = utoineg(0x67bbb232UL);
    2772        2412 :   gel(phi0, 22) = utoi(0x7dd8bb98UL);
    2773        2412 :   gel(phi0, 23) = uu32toi(0x1UL, 0xcaff744UL);
    2774        2412 :   gel(phi0, 24) = utoineg(0x1d46a378UL);
    2775        2412 :   gel(phi0, 25) = utoineg(0x82fa50f7UL);
    2776        2412 :   gel(phi0, 26) = utoineg(0x700ef38cUL);
    2777        2412 :   gel(phi0, 27) = utoi(0x20aa202eUL);
    2778        2412 :   gel(phi0, 28) = utoi(0x299b3440UL);
    2779        2412 :   gel(phi0, 29) = utoi(0xa476c4bUL);
    2780        2412 :   gel(phi0, 30) = utoi(0xd80558UL);
    2781        2412 :   gel(phi0, 31) = utoi(0x28a32UL);
    2782        2412 :   gel(phi0, 32) = utoi(0x2bcUL);
    2783        2412 :   gel(phi0, 33) = gen_1;
    2784             : 
    2785        2412 :   phi1 = cgetg(33, t_VEC);
    2786        2412 :   gel(phi1, 1) = gen_0;
    2787        2412 :   gel(phi1, 2) = gen_0;
    2788        2412 :   gel(phi1, 3) = gen_0;
    2789        2412 :   gel(phi1, 4) = gen_m1;
    2790        2412 :   gel(phi1, 5) = utoi(0xeUL);
    2791        2412 :   gel(phi1, 6) = utoineg(0x31UL);
    2792        2412 :   gel(phi1, 7) = utoineg(0xeUL);
    2793        2412 :   gel(phi1, 8) = utoi(0x99UL);
    2794        2412 :   gel(phi1, 9) = utoineg(0x8UL);
    2795        2412 :   gel(phi1, 10) = utoineg(0x2eUL);
    2796        2412 :   gel(phi1, 11) = utoineg(0x5ccUL);
    2797        2412 :   gel(phi1, 12) = utoi(0x308UL);
    2798        2412 :   gel(phi1, 13) = utoi(0x2904UL);
    2799        2412 :   gel(phi1, 14) = utoineg(0x15700UL);
    2800        2412 :   gel(phi1, 15) = utoineg(0x2b9ecUL);
    2801        2412 :   gel(phi1, 16) = utoi(0xf0966UL);
    2802        2412 :   gel(phi1, 17) = utoi(0xb3cc8UL);
    2803        2412 :   gel(phi1, 18) = utoineg(0x38241cUL);
    2804        2412 :   gel(phi1, 19) = utoineg(0x8604cUL);
    2805        2412 :   gel(phi1, 20) = utoi(0x578a64UL);
    2806        2412 :   gel(phi1, 21) = utoineg(0x11a798UL);
    2807        2412 :   gel(phi1, 22) = utoineg(0x39c85eUL);
    2808        2412 :   gel(phi1, 23) = utoi(0x1a5084UL);
    2809        2412 :   gel(phi1, 24) = utoi(0xcdeb4UL);
    2810        2412 :   gel(phi1, 25) = utoineg(0xb0364UL);
    2811        2412 :   gel(phi1, 26) = utoi(0x129d4UL);
    2812        2412 :   gel(phi1, 27) = utoi(0x126fcUL);
    2813        2412 :   gel(phi1, 28) = utoineg(0x8649UL);
    2814        2412 :   gel(phi1, 29) = utoi(0x1aa2UL);
    2815        2412 :   gel(phi1, 30) = utoineg(0x2dfUL);
    2816        2412 :   gel(phi1, 31) = utoi(0x2aUL);
    2817        2412 :   gel(phi1, 32) = gen_m1;
    2818             : 
    2819        2412 :   gel(phi, 1) = phi0;
    2820        2412 :   gel(phi, 2) = phi1;
    2821        2412 :   gel(phi, 3) = utoi(10); return phi;
    2822             : }
    2823             : 
    2824             : static GEN
    2825         210 : phi_w3w13_j(void)
    2826             : {
    2827             :   GEN phi, phi0, phi1;
    2828         210 :   phi = cgetg(4, t_VEC);
    2829             : 
    2830         210 :   phi0 = cgetg(58, t_VEC);
    2831         210 :   gel(phi0, 1) = gen_1;
    2832         210 :   gel(phi0, 2) = utoineg(0x10UL);
    2833         210 :   gel(phi0, 3) = utoi(0x58UL);
    2834         210 :   gel(phi0, 4) = utoi(0x258UL);
    2835         210 :   gel(phi0, 5) = utoineg(0x270cUL);
    2836         210 :   gel(phi0, 6) = utoi(0x9c00UL);
    2837         210 :   gel(phi0, 7) = utoi(0x2b40cUL);
    2838         210 :   gel(phi0, 8) = utoineg(0x20e250UL);
    2839         210 :   gel(phi0, 9) = utoi(0x4f46baUL);
    2840         210 :   gel(phi0, 10) = utoi(0x1869448UL);
    2841         210 :   gel(phi0, 11) = utoineg(0xa49ab68UL);
    2842         210 :   gel(phi0, 12) = utoi(0x96c7630UL);
    2843         210 :   gel(phi0, 13) = utoi(0x4f7e0af6UL);
    2844         210 :   gel(phi0, 14) = utoineg(0xea093590UL);
    2845         210 :   gel(phi0, 15) = utoineg(0x6735bc50UL);
    2846         210 :   gel(phi0, 16) = uu32toi(0x5UL, 0x971a2e08UL);
    2847         210 :   gel(phi0, 17) = uu32toineg(0x6UL, 0x29c9d965UL);
    2848         210 :   gel(phi0, 18) = uu32toineg(0xdUL, 0xeb9aa360UL);
    2849         210 :   gel(phi0, 19) = uu32toi(0x26UL, 0xe9c0584UL);
    2850         210 :   gel(phi0, 20) = uu32toineg(0x1UL, 0xb0cadce8UL);
    2851         210 :   gel(phi0, 21) = uu32toineg(0x62UL, 0x73586014UL);
    2852         210 :   gel(phi0, 22) = uu32toi(0x66UL, 0xaf672e38UL);
    2853         210 :   gel(phi0, 23) = uu32toi(0x6bUL, 0x93c28cdcUL);
    2854         210 :   gel(phi0, 24) = uu32toineg(0x11eUL, 0x4f633080UL);
    2855         210 :   gel(phi0, 25) = uu32toi(0x3cUL, 0xcc42461bUL);
    2856         210 :   gel(phi0, 26) = uu32toi(0x17bUL, 0xdec0a78UL);
    2857         210 :   gel(phi0, 27) = uu32toineg(0x166UL, 0x910d8bd0UL);
    2858         210 :   gel(phi0, 28) = uu32toineg(0xd4UL, 0x47873030UL);
    2859         210 :   gel(phi0, 29) = uu32toi(0x204UL, 0x811828baUL);
    2860         210 :   gel(phi0, 30) = uu32toineg(0x50UL, 0x5d713960UL);
    2861         210 :   gel(phi0, 31) = uu32toineg(0x198UL, 0xa27e42b0UL);
    2862         210 :   gel(phi0, 32) = uu32toi(0xe1UL, 0x25685138UL);
    2863         210 :   gel(phi0, 33) = uu32toi(0xe3UL, 0xaa5774bbUL);
    2864         210 :   gel(phi0, 34) = uu32toineg(0xcfUL, 0x392a9a00UL);
    2865         210 :   gel(phi0, 35) = uu32toineg(0x81UL, 0xfb334d04UL);
    2866         210 :   gel(phi0, 36) = uu32toi(0xabUL, 0x59594a68UL);
    2867         210 :   gel(phi0, 37) = uu32toi(0x42UL, 0x356993acUL);
    2868         210 :   gel(phi0, 38) = uu32toineg(0x86UL, 0x307ba678UL);
    2869         210 :   gel(phi0, 39) = uu32toineg(0xbUL, 0x7a9e59dcUL);
    2870         210 :   gel(phi0, 40) = uu32toi(0x4cUL, 0x27935f20UL);
    2871         210 :   gel(phi0, 41) = uu32toineg(0x2UL, 0xe0ac9045UL);
    2872         210 :   gel(phi0, 42) = uu32toineg(0x24UL, 0x14495758UL);
    2873         210 :   gel(phi0, 43) = utoi(0x20973410UL);
    2874         210 :   gel(phi0, 44) = uu32toi(0x13UL, 0x99ff4e00UL);
    2875         210 :   gel(phi0, 45) = uu32toineg(0x1UL, 0xa710d34aUL);
    2876         210 :   gel(phi0, 46) = uu32toineg(0x7UL, 0xfe5405c0UL);
    2877         210 :   gel(phi0, 47) = uu32toi(0x1UL, 0xcdee0f8UL);
    2878         210 :   gel(phi0, 48) = uu32toi(0x2UL, 0x660c92a8UL);
    2879         210 :   gel(phi0, 49) = utoi(0x3f13a35aUL);
    2880         210 :   gel(phi0, 50) = utoineg(0xe4eb4ba0UL);
    2881         210 :   gel(phi0, 51) = utoineg(0x6420f4UL);
    2882         210 :   gel(phi0, 52) = utoi(0x2c624370UL);
    2883         210 :   gel(phi0, 53) = utoi(0xb31b814UL);
    2884         210 :   gel(phi0, 54) = utoi(0xdd3ad8UL);
    2885         210 :   gel(phi0, 55) = utoi(0x29278UL);
    2886         210 :   gel(phi0, 56) = utoi(0x2c0UL);
    2887         210 :   gel(phi0, 57) = gen_1;
    2888             : 
    2889         210 :   phi1 = cgetg(57, t_VEC);
    2890         210 :   gel(phi1, 1) = gen_0;
    2891         210 :   gel(phi1, 2) = gen_0;
    2892         210 :   gel(phi1, 3) = gen_0;
    2893         210 :   gel(phi1, 4) = gen_m1;
    2894         210 :   gel(phi1, 5) = utoi(0xdUL);
    2895         210 :   gel(phi1, 6) = utoineg(0x34UL);
    2896         210 :   gel(phi1, 7) = utoi(0x1aUL);
    2897         210 :   gel(phi1, 8) = utoi(0xf7UL);
    2898         210 :   gel(phi1, 9) = utoineg(0x16cUL);
    2899         210 :   gel(phi1, 10) = utoineg(0xddUL);
    2900         210 :   gel(phi1, 11) = utoi(0x28aUL);
    2901         210 :   gel(phi1, 12) = utoineg(0xddUL);
    2902         210 :   gel(phi1, 13) = utoineg(0x16cUL);
    2903         210 :   gel(phi1, 14) = utoi(0xf6UL);
    2904         210 :   gel(phi1, 15) = utoi(0x1dUL);
    2905         210 :   gel(phi1, 16) = utoineg(0x31UL);
    2906         210 :   gel(phi1, 17) = utoineg(0x5ceUL);
    2907         210 :   gel(phi1, 18) = utoi(0x2e4UL);
    2908         210 :   gel(phi1, 19) = utoi(0x252cUL);
    2909         210 :   gel(phi1, 20) = utoineg(0x1b34cUL);
    2910         210 :   gel(phi1, 21) = utoi(0xaf80UL);
    2911         210 :   gel(phi1, 22) = utoi(0x1cc5f9UL);
    2912         210 :   gel(phi1, 23) = utoineg(0x3e1aa5UL);
    2913         210 :   gel(phi1, 24) = utoineg(0x86d17aUL);
    2914         210 :   gel(phi1, 25) = utoi(0x2427264UL);
    2915         210 :   gel(phi1, 26) = utoineg(0x691c1fUL);
    2916         210 :   gel(phi1, 27) = utoineg(0x862ad4eUL);
    2917         210 :   gel(phi1, 28) = utoi(0xab21e1fUL);
    2918         210 :   gel(phi1, 29) = utoi(0xbc19ddcUL);
    2919         210 :   gel(phi1, 30) = utoineg(0x24331db8UL);
    2920         210 :   gel(phi1, 31) = utoi(0x972c105UL);
    2921         210 :   gel(phi1, 32) = utoi(0x363d7107UL);
    2922         210 :   gel(phi1, 33) = utoineg(0x39696450UL);
    2923         210 :   gel(phi1, 34) = utoineg(0x1bce7c48UL);
    2924         210 :   gel(phi1, 35) = utoi(0x552ecba0UL);
    2925         210 :   gel(phi1, 36) = utoineg(0x1c7771b8UL);
    2926         210 :   gel(phi1, 37) = utoineg(0x393029b8UL);
    2927         210 :   gel(phi1, 38) = utoi(0x3755be97UL);
    2928         210 :   gel(phi1, 39) = utoi(0x83402a9UL);
    2929         210 :   gel(phi1, 40) = utoineg(0x24d5be62UL);
    2930         210 :   gel(phi1, 41) = utoi(0xdb6d90aUL);
    2931         210 :   gel(phi1, 42) = utoi(0xa0ef177UL);
    2932         210 :   gel(phi1, 43) = utoineg(0x99ff162UL);
    2933         210 :   gel(phi1, 44) = utoi(0xb09e27UL);
    2934         210 :   gel(phi1, 45) = utoi(0x26a7adcUL);
    2935         210 :   gel(phi1, 46) = utoineg(0x116e2fcUL);
    2936         210 :   gel(phi1, 47) = utoineg(0x1383b5UL);
    2937         210 :   gel(phi1, 48) = utoi(0x35a9e7UL);
    2938         210 :   gel(phi1, 49) = utoineg(0x1082a0UL);
    2939         210 :   gel(phi1, 50) = utoineg(0x4696UL);
    2940         210 :   gel(phi1, 51) = utoi(0x19f98UL);
    2941         210 :   gel(phi1, 52) = utoineg(0x8bb3UL);
    2942         210 :   gel(phi1, 53) = utoi(0x18bbUL);
    2943         210 :   gel(phi1, 54) = utoineg(0x297UL);
    2944         210 :   gel(phi1, 55) = utoi(0x27UL);
    2945         210 :   gel(phi1, 56) = gen_m1;
    2946             : 
    2947         210 :   gel(phi, 1) = phi0;
    2948         210 :   gel(phi, 2) = phi1;
    2949         210 :   gel(phi, 3) = utoi(16); return phi;
    2950             : }
    2951             : 
    2952             : static GEN
    2953        2893 : phi_w5w7_j(void)
    2954             : {
    2955             :   GEN phi, phi0, phi1;
    2956        2893 :   phi = cgetg(4, t_VEC);
    2957             : 
    2958        2893 :   phi0 = cgetg(50, t_VEC);
    2959        2893 :   gel(phi0, 1) = gen_1;
    2960        2893 :   gel(phi0, 2) = utoi(0xcUL);
    2961        2893 :   gel(phi0, 3) = utoi(0x2aUL);
    2962        2893 :   gel(phi0, 4) = utoi(0x10UL);
    2963        2893 :   gel(phi0, 5) = utoineg(0x69UL);
    2964        2893 :   gel(phi0, 6) = utoineg(0x318UL);
    2965        2893 :   gel(phi0, 7) = utoineg(0x148aUL);
    2966        2893 :   gel(phi0, 8) = utoineg(0x17c4UL);
    2967        2893 :   gel(phi0, 9) = utoi(0x1a73UL);
    2968        2893 :   gel(phi0, 10) = gen_0;
    2969        2893 :   gel(phi0, 11) = utoi(0x338a0UL);
    2970        2893 :   gel(phi0, 12) = utoi(0x61698UL);
    2971        2893 :   gel(phi0, 13) = utoineg(0x96e8UL);
    2972        2893 :   gel(phi0, 14) = utoi(0x140910UL);
    2973        2893 :   gel(phi0, 15) = utoineg(0x45f6b4UL);
    2974        2893 :   gel(phi0, 16) = utoineg(0x309f50UL);
    2975        2893 :   gel(phi0, 17) = utoineg(0xef9f8bUL);
    2976        2893 :   gel(phi0, 18) = utoineg(0x283167cUL);
    2977        2893 :   gel(phi0, 19) = utoi(0x625e20aUL);
    2978        2893 :   gel(phi0, 20) = utoineg(0x16186350UL);
    2979        2893 :   gel(phi0, 21) = utoi(0x46861281UL);
    2980        2893 :   gel(phi0, 22) = utoineg(0x754b96a0UL);
    2981        2893 :   gel(phi0, 23) = uu32toi(0x1UL, 0x421ca02aUL);
    2982        2893 :   gel(phi0, 24) = uu32toineg(0x2UL, 0xdb76a5cUL);
    2983        2893 :   gel(phi0, 25) = uu32toi(0x4UL, 0xf6afd8eUL);
    2984        2893 :   gel(phi0, 26) = uu32toineg(0x6UL, 0xaafd3cb4UL);
    2985        2893 :   gel(phi0, 27) = uu32toi(0x8UL, 0xda2539caUL);
    2986        2893 :   gel(phi0, 28) = uu32toineg(0xfUL, 0x84343790UL);
    2987        2893 :   gel(phi0, 29) = uu32toi(0xfUL, 0x914ff421UL);
    2988        2893 :   gel(phi0, 30) = uu32toineg(0x19UL, 0x3c123950UL);
    2989        2893 :   gel(phi0, 31) = uu32toi(0x15UL, 0x381f722aUL);
    2990        2893 :   gel(phi0, 32) = uu32toineg(0x15UL, 0xe01c0c24UL);
    2991        2893 :   gel(phi0, 33) = uu32toi(0x19UL, 0x3360b375UL);
    2992        2893 :   gel(phi0, 34) = utoineg(0x59fda9c0UL);
    2993        2893 :   gel(phi0, 35) = uu32toi(0x20UL, 0xff55024cUL);
    2994        2893 :   gel(phi0, 36) = uu32toi(0x16UL, 0xcc600800UL);
    2995        2893 :   gel(phi0, 37) = uu32toi(0x24UL, 0x1879c898UL);
    2996        2893 :   gel(phi0, 38) = uu32toi(0x1cUL, 0x37f97498UL);
    2997        2893 :   gel(phi0, 39) = uu32toi(0x19UL, 0x39ec4b60UL);
    2998        2893 :   gel(phi0, 40) = uu32toi(0x10UL, 0x52c660d0UL);
    2999        2893 :   gel(phi0, 41) = uu32toi(0x9UL, 0xcab00333UL);
    3000        2893 :   gel(phi0, 42) = uu32toi(0x4UL, 0x7fe69be4UL);
    3001        2893 :   gel(phi0, 43) = uu32toi(0x1UL, 0xa0c6f116UL);
    3002        2893 :   gel(phi0, 44) = utoi(0x69244638UL);
    3003        2893 :   gel(phi0, 45) = utoi(0xed560f7UL);
    3004        2893 :   gel(phi0, 46) = utoi(0xe7b660UL);
    3005        2893 :   gel(phi0, 47) = utoi(0x29d8aUL);
    3006        2893 :   gel(phi0, 48) = utoi(0x2c4UL);
    3007        2893 :   gel(phi0, 49) = gen_1;
    3008             : 
    3009        2893 :   phi1 = cgetg(49, t_VEC);
    3010        2893 :   gel(phi1, 1) = gen_0;
    3011        2893 :   gel(phi1, 2) = gen_0;
    3012        2893 :   gel(phi1, 3) = gen_0;
    3013        2893 :   gel(phi1, 4) = gen_0;
    3014        2893 :   gel(phi1, 5) = gen_0;
    3015        2893 :   gel(phi1, 6) = gen_1;
    3016        2893 :   gel(phi1, 7) = utoi(0x7UL);
    3017        2893 :   gel(phi1, 8) = utoi(0x8UL);
    3018        2893 :   gel(phi1, 9) = utoineg(0x9UL);
    3019        2893 :   gel(phi1, 10) = gen_0;
    3020        2893 :   gel(phi1, 11) = utoineg(0x13UL);
    3021        2893 :   gel(phi1, 12) = utoineg(0x7UL);
    3022        2893 :   gel(phi1, 13) = utoineg(0x5ceUL);
    3023        2893 :   gel(phi1, 14) = utoineg(0xb0UL);
    3024        2893 :   gel(phi1, 15) = utoi(0x460UL);
    3025        2893 :   gel(phi1, 16) = utoineg(0x194bUL);
    3026        2893 :   gel(phi1, 17) = utoi(0x87c3UL);
    3027        2893 :   gel(phi1, 18) = utoi(0x3cdeUL);
    3028        2893 :   gel(phi1, 19) = utoineg(0xd683UL);
    3029        2893 :   gel(phi1, 20) = utoi(0x6099bUL);
    3030        2893 :   gel(phi1, 21) = utoineg(0x111ea8UL);
    3031        2893 :   gel(phi1, 22) = utoi(0xfa113UL);
    3032        2893 :   gel(phi1, 23) = utoineg(0x1a6561UL);
    3033        2893 :   gel(phi1, 24) = utoineg(0x1e997UL);
    3034        2893 :   gel(phi1, 25) = utoi(0x214e54UL);
    3035        2893 :   gel(phi1, 26) = utoineg(0x29c3f4UL);
    3036        2893 :   gel(phi1, 27) = utoi(0x67e102UL);
    3037        2893 :   gel(phi1, 28) = utoineg(0x227eaaUL);
    3038        2893 :   gel(phi1, 29) = utoi(0x191d10UL);
    3039        2893 :   gel(phi1, 30) = utoi(0x1a9cd5UL);
    3040        2893 :   gel(phi1, 31) = utoineg(0x58386fUL);
    3041        2893 :   gel(phi1, 32) = utoi(0x2e49f6UL);
    3042        2893 :   gel(phi1, 33) = utoineg(0x31194bUL);
    3043        2893 :   gel(phi1, 34) = utoi(0x9e07aUL);
    3044        2893 :   gel(phi1, 35) = utoi(0x260d59UL);
    3045        2893 :   gel(phi1, 36) = utoineg(0x189921UL);
    3046        2893 :   gel(phi1, 37) = utoi(0xeca4aUL);
    3047        2893 :   gel(phi1, 38) = utoineg(0xa3d9cUL);
    3048        2893 :   gel(phi1, 39) = utoineg(0x426daUL);
    3049        2893 :   gel(phi1, 40) = utoi(0x91875UL);
    3050        2893 :   gel(phi1, 41) = utoineg(0x3b55bUL);
    3051        2893 :   gel(phi1, 42) = utoineg(0x56f4UL);
    3052        2893 :   gel(phi1, 43) = utoi(0xcd1bUL);
    3053        2893 :   gel(phi1, 44) = utoineg(0x5159UL);
    3054        2893 :   gel(phi1, 45) = utoi(0x10f4UL);
    3055        2893 :   gel(phi1, 46) = utoineg(0x20dUL);
    3056        2893 :   gel(phi1, 47) = utoi(0x23UL);
    3057        2893 :   gel(phi1, 48) = gen_m1;
    3058             : 
    3059        2893 :   gel(phi, 1) = phi0;
    3060        2893 :   gel(phi, 2) = phi1;
    3061        2893 :   gel(phi, 3) = utoi(12); return phi;
    3062             : }
    3063             : 
    3064             : GEN
    3065       24014 : double_eta_raw(long inv)
    3066             : {
    3067       24014 :   switch (inv) {
    3068        1078 :     case INV_W2W3:
    3069        1078 :     case INV_W2W3E2: return phi_w2w3_j();
    3070        3517 :     case INV_W3W3:
    3071        3517 :     case INV_W3W3E2: return phi_w3w3_j();
    3072        3234 :     case INV_W2W5:
    3073        3234 :     case INV_W2W5E2: return phi_w2w5_j();
    3074        6591 :     case INV_W2W7:
    3075        6591 :     case INV_W2W7E2: return phi_w2w7_j();
    3076        1146 :     case INV_W3W5:   return phi_w3w5_j();
    3077        2412 :     case INV_W3W7:   return phi_w3w7_j();
    3078        2933 :     case INV_W2W13:  return phi_w2w13_j();
    3079         210 :     case INV_W3W13:  return phi_w3w13_j();
    3080        2893 :     case INV_W5W7:   return phi_w5w7_j();
    3081             :     default: pari_err_BUG("double_eta_raw"); return NULL;/*LCOV_EXCL_LINE*/
    3082             :   }
    3083             : }
    3084             : 
    3085             : /* SECTION: Select discriminant for given modpoly level. */
    3086             : 
    3087             : /* require an L1, useful for multi-threading */
    3088             : #define MODPOLY_USE_L1    1
    3089             : /* no bound on L1 other than the fixed bound MAX_L1 - needed to
    3090             :  * handle small L for certain invariants (but not for j) */
    3091             : #define MODPOLY_NO_MAX_L1 2
    3092             : /* don't use any auxilliary primes - needed to handle small L for
    3093             :  * certain invariants (but not for j) */
    3094             : #define MODPOLY_NO_AUX_L  4
    3095             : #define MODPOLY_IGNORE_SPARSE_FACTOR 8
    3096             : 
    3097             : INLINE double
    3098        2777 : modpoly_height_bound(long L, long inv)
    3099             : {
    3100             :   double nbits, nbits2;
    3101             :   double c;
    3102             :   long hf;
    3103             : 
    3104             :   /* proven bound (in bits), derived from: 6l*log(l)+16*l+13*sqrt(l)*log(l) */
    3105        2777 :   nbits = 6.0*L*log2(L)+16/M_LN2*L+8.0*sqrt((double)L)*log2(L);
    3106             :   /* alternative proven bound (in bits), derived from: 6l*log(l)+17*l */
    3107        2777 :   nbits2 = 6.0*L*log2(L)+17/M_LN2*L;
    3108        2777 :   if ( nbits2 < nbits ) nbits = nbits2;
    3109        2777 :   hf = modinv_height_factor(inv);
    3110        2777 :   if (hf > 1) {
    3111             :    /* IMPORTANT: when dividing by the height factor, we only want to reduce
    3112             :    terms related to the bound on j (the roots of Phi_l(X,y)), not terms arising
    3113             :    from binomial coefficients. These arise in lemmas 2 and 3 of the height
    3114             :    bound paper, terms of (log 2)*L and 2.085*(L+1) which we convert here to
    3115             :    binary logs */
    3116             :     /* Massive overestimate: if you care about speed, determine a good height
    3117             :      * bound empirically as done for INV_F below */
    3118        1669 :     nbits2 = nbits - 4.01*L -3.0;
    3119        1669 :     nbits = nbits2/hf + 4.01*L + 3.0;
    3120             :   }
    3121        2777 :   if (inv == INV_F) {
    3122         149 :     if (L < 30) c = 45;
    3123          35 :     else if (L < 100) c = 36;
    3124          21 :     else if (L < 300) c = 32;
    3125           7 :     else if (L < 600) c = 26;
    3126           0 :     else if (L < 1200) c = 24;
    3127           0 :     else if (L < 2400) c = 22;
    3128           0 :     else c = 20;
    3129         149 :     nbits = (6.0*L*log2(L) + c*L)/hf;
    3130             :   }
    3131        2777 :   return nbits;
    3132             : }
    3133             : 
    3134             : /* small enough to write the factorization of a smooth in a BIL bit integer */
    3135             : #define SMOOTH_PRIMES  ((BITS_IN_LONG >> 1) - 1)
    3136             : 
    3137             : #define MAX_ATKIN 255
    3138             : 
    3139             : /* Must have primes at least up to MAX_ATKIN */
    3140             : static const long PRIMES[] = {
    3141             :     0,   2,   3,   5,   7,  11,  13,  17,  19,  23,
    3142             :    29,  31,  37,  41,  43,  47,  53,  59,  61,  67,
    3143             :    71,  73,  79,  83,  89,  97, 101, 103, 107, 109,
    3144             :   113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
    3145             :   173, 179, 181, 191, 193, 197, 199, 211, 223, 227,
    3146             :   229, 233, 239, 241, 251, 257, 263, 269, 271, 277
    3147             : };
    3148             : 
    3149             : #define MAX_L1      255
    3150             : 
    3151             : typedef struct D_entry_struct {
    3152             :   ulong m;
    3153             :   long D, h;
    3154             : } D_entry;
    3155             : 
    3156             : /* Returns a form that generates the classes of norm p^2 in cl(p^2D)
    3157             :  * (i.e. one with order p-1), where p is an odd prime that splits in D
    3158             :  * and does not divide its conductor (but this is not verified) */
    3159             : INLINE GEN
    3160       65259 : qform_primeform2(long p, long D)
    3161             : {
    3162       65259 :   GEN a = sqru(p), Dp2 = mulis(a, D), M = Z_factor(utoipos(p - 1));
    3163       65259 :   pari_sp av = avma;
    3164             :   long k;
    3165             : 
    3166      132663 :   for (k = D & 1; k <= p; k += 2)
    3167             :   {
    3168      132663 :     long ord, c = (k * k - D) / 4;
    3169             :     GEN Q, q;
    3170             : 
    3171      132663 :     if (!(c % p)) continue;
    3172      114996 :     q = mkqfis(a, k * p, c, Dp2); Q = qfbred_i(q);
    3173             :     /* TODO: How do we know that Q has order dividing p - 1? If we don't, then
    3174             :      * the call to gen_order should be replaced with a call to something with
    3175             :      * fastorder semantics (i.e. return 0 if ord(Q) \ndiv M). */
    3176      114996 :     ord = itos(qfi_order(Q, M));
    3177      114996 :     if (ord == p - 1) {
    3178             :       /* TODO: This check that gen_order returned the correct result should be
    3179             :        * removed when gen_order is replaced with fastorder semantics. */
    3180       65259 :       if (qfb_equal1(gpowgs(Q, p - 1))) return q;
    3181           0 :       break;
    3182             :     }
    3183       49737 :     set_avma(av);
    3184             :   }
    3185           0 :   return NULL;
    3186             : }
    3187             : 
    3188             : /* Let n = #cl(D); return x such that [L0]^x = [L] in cl(D), or -1 if x was
    3189             :  * not found */
    3190             : INLINE long
    3191      172431 : primeform_discrete_log(long L0, long L, long n, long D)
    3192             : {
    3193      172431 :   pari_sp av = avma;
    3194      172431 :   GEN X, Q, R, DD = stoi(D);
    3195      172431 :   Q = primeform_u(DD, L0);
    3196      172431 :   R = primeform_u(DD, L);
    3197      172431 :   X = qfi_Shanks(R, Q, n);
    3198      172431 :   return gc_long(av, X? itos(X): -1);
    3199             : }
    3200             : 
    3201             : /* Return the norm of a class group generator appropriate for a discriminant
    3202             :  * that will be used to calculate the modular polynomial of level L and
    3203             :  * invariant inv.  Don't consider norms less than initial_L0 */
    3204             : static long
    3205        2777 : select_L0(long L, long inv, long initial_L0)
    3206             : {
    3207        2777 :   long L0, modinv_N = modinv_level(inv);
    3208             : 
    3209        2777 :   if (modinv_N % L == 0) pari_err_BUG("select_L0");
    3210             : 
    3211             :   /* TODO: Clean up these anomolous L0 choices */
    3212             : 
    3213             :   /* I've no idea why the discriminant-finding code fails with L0=5
    3214             :    * when L=19 and L=29, nor why L0=7 and L0=11 don't work for L=19
    3215             :    * either, nor why this happens for the otherwise unrelated
    3216             :    * invariants Weber-f and (2,3) double-eta. */
    3217        2777 :   if (inv == INV_W3W3E2 && L == 5) return 2;
    3218             : 
    3219        2763 :   if (inv == INV_F || inv == INV_F2 || inv == INV_F4 || inv == INV_F8
    3220        2502 :       || inv == INV_W2W3 || inv == INV_W2W3E2
    3221        2439 :       || inv == INV_W3W3 /* || inv == INV_W3W3E2 */) {
    3222         422 :     if (L == 19) return 13;
    3223         372 :     else if (L == 29 || L == 5) return 7;
    3224         309 :     return 5;
    3225             :   }
    3226        2341 :   if ((inv == INV_W2W5 || inv == INV_W2W5E2)
    3227         140 :       && (L == 7 || L == 19)) return 13;
    3228        2299 :   if ((inv == INV_W2W7 || inv == INV_W2W7E2)
    3229         358 :       && L == 11) return 13;
    3230        2271 :   if (inv == INV_W3W5) {
    3231          63 :     if (L == 7) return 13;
    3232          56 :     else if (L == 17) return 7;
    3233             :   }
    3234        2264 :   if (inv == INV_W3W7) {
    3235         140 :     if (L == 29 || L == 101) return 11;
    3236         112 :     if (L == 11 || L == 19) return 13;
    3237             :   }
    3238        2208 :   if (inv == INV_W5W7 && L == 17) return 3;
    3239             : 
    3240             :   /* L0 = smallest small prime different from L that doesn't divide modinv_N */
    3241        3161 :   for (L0 = unextprime(initial_L0 + 1);
    3242        3098 :        L0 == L || modinv_N % L0 == 0;
    3243         974 :        L0 = unextprime(L0 + 1))
    3244             :     ;
    3245        2187 :   return L0;
    3246             : }
    3247             : 
    3248             : /* Return the order of [L]^n in cl(D), where #cl(D) = ord. */
    3249             : INLINE long
    3250      924392 : primeform_exp_order(long L, long n, long D, long ord)
    3251             : {
    3252      924392 :   pari_sp av = avma;
    3253      924392 :   GEN Q = gpowgs(primeform_u(stoi(D), L), n);
    3254      924392 :   long m = itos(qfi_order(Q, Z_factor(stoi(ord))));
    3255      924392 :   return gc_long(av,m);
    3256             : }
    3257             : 
    3258             : /* If an ideal of norm modinv_deg is equivalent to an ideal of norm L0, we
    3259             :  * have an orientation ambiguity that we need to avoid. Note that we need to
    3260             :  * check all the possibilities (up to 8), but we can cheaply check inverses
    3261             :  * (so at most 2) */
    3262             : static long
    3263       33902 : orientation_ambiguity(long D1, long L0, long modinv_p1, long modinv_p2, long modinv_N)
    3264             : {
    3265       33902 :   pari_sp av = avma;
    3266       33902 :   long ambiguity = 0;
    3267       33902 :   GEN D = stoi(D1), Q1 = primeform_u(D, modinv_p1), Q2 = NULL;
    3268             : 
    3269       33902 :   if (modinv_p2 > 1)
    3270             :   {
    3271       33902 :     if (modinv_p1 == modinv_p2) Q1 = gsqr(Q1);
    3272             :     else
    3273             :     {
    3274       27792 :       GEN P2 = primeform_u(D, modinv_p2);
    3275       27792 :       GEN Q = gsqr(P2), R = gsqr(Q1);
    3276             :       /* check that p1^2 != p2^{+/-2}, since this leads to
    3277             :        * ambiguities when converting j's to f's */
    3278       27792 :       if (equalii(gel(Q,1), gel(R,1)) && absequalii(gel(Q,2), gel(R,2)))
    3279             :       {
    3280           0 :         dbg_printf(3)("Bad D=%ld, a^2=b^2 problem between modinv_p1=%ld and modinv_p2=%ld\n",
    3281             :                       D1, modinv_p1, modinv_p2);
    3282           0 :         ambiguity = 1;
    3283             :       }
    3284             :       else
    3285             :       { /* generate both p1*p2 and p1*p2^{-1} */
    3286       27792 :         Q2 = gmul(Q1, P2);
    3287       27792 :         P2 = ginv(P2);
    3288       27792 :         Q1 = gmul(Q1, P2);
    3289             :       }
    3290             :     }
    3291             :   }
    3292       33902 :   if (!ambiguity)
    3293             :   {
    3294       33902 :     GEN P = gsqr(primeform_u(D, L0));
    3295       33902 :     if (equalii(gel(P,1), gel(Q1,1))
    3296       32872 :         || (modinv_p2 && modinv_p1 != modinv_p2
    3297       26776 :                       && equalii(gel(P,1), gel(Q2,1)))) {
    3298        1487 :       dbg_printf(3)("Bad D=%ld, a=b^{+/-2} problem between modinv_N=%ld and L0=%ld\n",
    3299             :                     D1, modinv_N, L0);
    3300        1487 :       ambiguity = 1;
    3301             :     }
    3302             :   }
    3303       33902 :   return gc_long(av, ambiguity);
    3304             : }
    3305             : 
    3306             : static long
    3307      666592 : check_generators(
    3308             :   long *n1_, long *m_,
    3309             :   long D, long h, long n, long subgrp_sz, long L0, long L1)
    3310             : {
    3311      666592 :   long n1, m = primeform_exp_order(L0, n, D, h);
    3312      666592 :   if (m_) *m_ = m;
    3313      666592 :   n1 = n * m;
    3314      666592 :   if (!n1) pari_err_BUG("check_generators");
    3315      666592 :   *n1_ = n1;
    3316      666592 :   if (n1 < subgrp_sz/2 || ( ! L1 && n1 < subgrp_sz))  {
    3317       24640 :     dbg_printf(3)("Bad D1=%ld with n1=%ld, h1=%ld, L1=%ld: "
    3318             :                   "L0 and L1 don't span subgroup of size d in cl(D1)\n",
    3319             :                   D, n, h, L1);
    3320       24640 :     return 0;
    3321             :   }
    3322      641952 :   if (n1 < subgrp_sz && ! (n1 & 1)) {
    3323             :     int res;
    3324             :     /* check whether L1 is generated by L0, use the fact that it has order 2 */
    3325       15662 :     pari_sp av = avma;
    3326       15662 :     GEN D1 = stoi(D);
    3327       15662 :     GEN Q = gpowgs(primeform_u(D1, L0), n1 / 2);
    3328       15662 :     res = gequal(Q, qfbred_i(primeform_u(D1, L1)));
    3329       15662 :     set_avma(av);
    3330       15662 :     if (res) {
    3331        4683 :       dbg_printf(3)("Bad D1=%ld, with n1=%ld, h1=%ld, L1=%ld: "
    3332             :                     "L1 generated by L0 in cl(D1)\n", D, n, h, L1);
    3333        4683 :       return 0;
    3334             :     }
    3335             :   }
    3336      637269 :   return 1;
    3337             : }
    3338             : 
    3339             : /* Calculate solutions (p, t) to the norm equation
    3340             :  *   4 p = t^2 - v^2 L^2 D   (*)
    3341             :  * corresponding to the descriminant described by Dinfo.
    3342             :  *
    3343             :  * INPUT:
    3344             :  * - max: length of primes and traces
    3345             :  * - xprimes: p to exclude from primes (if they arise)
    3346             :  * - xcnt: length of xprimes
    3347             :  * - minbits: sum of log2(p) must be larger than this
    3348             :  * - Dinfo: discriminant, invariant and L for which we seek solutions to (*)
    3349             :  *
    3350             :  * OUTPUT:
    3351             :  * - primes: array of p in (*)
    3352             :  * - traces: array of t in (*)
    3353             :  * - totbits: sum of log2(p) for p in primes.
    3354             :  *
    3355             :  * RETURN:
    3356             :  * - the number of primes and traces found (these are always the same).
    3357             :  *
    3358             :  * NOTE: primes and traces are both NULL or both non-NULL.
    3359             :  * xprimes can be zero, in which case it is treated as empty. */
    3360             : static long
    3361       10045 : modpoly_pickD_primes(
    3362             :   ulong *primes, ulong *traces, long max, ulong *xprimes, long xcnt,
    3363             :   long *totbits, long minbits, disc_info *Dinfo)
    3364             : {
    3365             :   double bits;
    3366             :   long D, m, n, vcnt, pfilter, one_prime, inv;
    3367             :   ulong maxp;
    3368             :   ulong a1, a2, v, t, p, a1_start, a1_delta, L0, L1, L, absD;
    3369       10045 :   ulong FF_BITS = BITS_IN_LONG - 2; /* BITS_IN_LONG - NAIL_BITS */
    3370             : 
    3371       10045 :   D = Dinfo->D1; absD = -D;
    3372       10045 :   L0 = Dinfo->L0;
    3373       10045 :   L1 = Dinfo->L1;
    3374       10045 :   L = Dinfo->L;
    3375       10045 :   inv = Dinfo->inv;
    3376             : 
    3377             :   /* make sure pfilter and D don't preclude the possibility of p=(t^2-v^2D)/4 being prime */
    3378       10045 :   pfilter = modinv_pfilter(inv);
    3379       10045 :   if ((pfilter & IQ_FILTER_1MOD3) && ! (D % 3)) return 0;
    3380        9996 :   if ((pfilter & IQ_FILTER_1MOD4) && ! (D & 0xF)) return 0;
    3381             : 
    3382             :   /* Naively estimate the number of primes satisfying 4p=t^2-L^2D with
    3383             :    * t=2 mod L and pfilter. This is roughly
    3384             :    * #{t: t^2 < max p and t=2 mod L} / pi(max p) * filter_density,
    3385             :    * where filter_density is 1, 2, or 4 depending on pfilter.  If this quantity
    3386             :    * is already more than twice the number of bits we need, assume that,
    3387             :    * barring some obstruction, we should have no problem getting enough primes.
    3388             :    * In this case we just verify we can get one prime (which should always be
    3389             :    * true, assuming we chose D properly). */
    3390        9996 :   one_prime = 0;
    3391        9996 :   *totbits = 0;
    3392        9996 :   if (max <= 1 && ! one_prime) {
    3393        7198 :     p = ((pfilter & IQ_FILTER_1MOD3) ? 2 : 1) * ((pfilter & IQ_FILTER_1MOD4) ? 2 : 1);
    3394       14396 :     one_prime = (1UL << ((FF_BITS+1)/2)) * (log2(L*L*(-D))-1)
    3395        7198 :         > p*L*minbits*FF_BITS*M_LN2;
    3396        7198 :     if (one_prime) *totbits = minbits+1;   /* lie */
    3397             :   }
    3398             : 
    3399        9996 :   m = n = 0;
    3400        9996 :   bits = 0.0;
    3401        9996 :   maxp = 0;
    3402       25505 :   for (v = 1; v < 100 && bits < minbits; v++) {
    3403             :     /* Don't allow v dividing the conductor. */
    3404       22654 :     if (ugcd(absD, v) != 1) continue;
    3405             :     /* Avoid v dividing the level. */
    3406       22456 :     if (v > 2 && modinv_is_double_eta(inv) && ugcd(modinv_level(inv), v) != 1)
    3407         966 :       continue;
    3408             :     /* can't get odd p with D=1 mod 8 unless v is even */
    3409       21490 :     if ((v & 1) && (D & 7) == 1) continue;
    3410             :     /* disallow 4 | v for L0=2 (removing this restriction is costly) */
    3411       10626 :     if (L0 == 2 && !(v & 3)) continue;
    3412             :     /* can't get p=3mod4 if v^2D is 0 mod 16 */
    3413       10376 :     if ((pfilter & IQ_FILTER_1MOD4) && !((v*v*D) & 0xF)) continue;
    3414       10293 :     if ((pfilter & IQ_FILTER_1MOD3) && !(v%3) ) continue;
    3415             :     /* avoid L0-volcanos with nonzero height */
    3416       10235 :     if (L0 != 2 && ! (v % L0)) continue;
    3417             :     /* ditto for L1 */
    3418       10214 :     if (L1 && !(v % L1)) continue;
    3419       10214 :     vcnt = 0;
    3420       10214 :     if ((v*v*absD)/4 > (1L<<FF_BITS)/(L*L)) break;
    3421       10132 :     if (both_odd(v,D)) {
    3422           0 :       a1_start = 1;
    3423           0 :       a1_delta = 2;
    3424             :     } else {
    3425       10132 :       a1_start = ((v*v*D) & 7)? 2: 0;
    3426       10132 :       a1_delta = 4;
    3427             :     }
    3428      510697 :     for (a1 = a1_start; bits < minbits; a1 += a1_delta) {
    3429      507860 :       a2 = (a1*a1 + v*v*absD) >> 2;
    3430      507860 :       if (!(a2 % L)) continue;
    3431      433230 :       t = a1*L + 2;
    3432      433230 :       p = a2*L*L + t - 1;
    3433             :       /* double check calculation just in case of overflow or other weirdness */
    3434      433230 :       if (!odd(p) || t*t + v*v*L*L*absD != 4*p)
    3435           0 :         pari_err_BUG("modpoly_pickD_primes");
    3436      433230 :       if (p > (1UL<<FF_BITS)) break;
    3437      432998 :       if (xprimes) {
    3438      320754 :         while (m < xcnt && xprimes[m] < p) m++;
    3439      320326 :         if (m < xcnt && p == xprimes[m]) {
    3440           0 :           dbg_printf(1)("skipping duplicate prime %ld\n", p);
    3441           0 :           continue;
    3442             :         }
    3443             :       }
    3444      432998 :       if (!uisprime(p) || !modinv_good_prime(inv, p)) continue;
    3445       46633 :       if (primes) {
    3446       34373 :         if (n >= max) goto done;
    3447             :         /* TODO: Implement test to filter primes that lead to
    3448             :          * L-valuation != 2 */
    3449       34373 :         primes[n] = p;
    3450       34373 :         traces[n] = t;
    3451             :       }
    3452       46633 :       n++;
    3453       46633 :       vcnt++;
    3454       46633 :       bits += log2(p);
    3455       46633 :       if (p > maxp) maxp = p;
    3456       46633 :       if (one_prime) goto done;
    3457             :     }
    3458        3069 :     if (vcnt)
    3459        3066 :       dbg_printf(3)("%ld primes with v=%ld, maxp=%ld (%.2f bits)\n",
    3460             :                  vcnt, v, maxp, log2(maxp));
    3461             :   }
    3462        2851 : done:
    3463        9996 :   if (!n) {
    3464           9 :     dbg_printf(3)("check_primes failed completely for D=%ld\n", D);
    3465           9 :     return 0;
    3466             :   }
    3467        9987 :   dbg_printf(3)("D=%ld: Found %ld primes totalling %0.2f of %ld bits\n",
    3468             :              D, n, bits, minbits);
    3469        9987 :   if (!*totbits) *totbits = (long)bits;
    3470        9987 :   return n;
    3471             : }
    3472             : 
    3473             : #define MAX_VOLCANO_FLOOR_SIZE 100000000
    3474             : 
    3475             : static long
    3476        2779 : calc_primes_for_discriminants(disc_info Ds[], long Dcnt, long L, long minbits)
    3477             : {
    3478        2779 :   pari_sp av = avma;
    3479             :   long i, j, k, m, n, D1, pcnt, totbits;
    3480             :   ulong *primes, *Dprimes, *Dtraces;
    3481             : 
    3482             :   /* D1 is the discriminant with smallest absolute value among those we found */
    3483        2779 :   D1 = Ds[0].D1;
    3484        7189 :   for (i = 1; i < Dcnt; i++)
    3485        4410 :     if (Ds[i].D1 > D1) D1 = Ds[i].D1;
    3486             : 
    3487             :   /* n is an upper bound on the number of primes we might get. */
    3488        2779 :   n = ceil(minbits / (log2(L * L * (-D1)) - 2)) + 1;
    3489        2779 :   primes = (ulong *) stack_malloc(n * sizeof(*primes));
    3490        2779 :   Dprimes = (ulong *) stack_malloc(n * sizeof(*Dprimes));
    3491        2779 :   Dtraces = (ulong *) stack_malloc(n * sizeof(*Dtraces));
    3492        2798 :   for (i = 0, totbits = 0, pcnt = 0; i < Dcnt && totbits < minbits; i++)
    3493             :   {
    3494        5596 :     long np = modpoly_pickD_primes(Dprimes, Dtraces, n, primes, pcnt,
    3495        2798 :                                    &Ds[i].bits, minbits - totbits, Ds + i);
    3496        2798 :     ulong *T = (ulong *)newblock(2*np);
    3497        2798 :     Ds[i].nprimes = np;
    3498        2798 :     Ds[i].primes = T;    memcpy(T   , Dprimes, np * sizeof(*Dprimes));
    3499        2798 :     Ds[i].traces = T+np; memcpy(T+np, Dtraces, np * sizeof(*Dtraces));
    3500             : 
    3501        2798 :     totbits += Ds[i].bits;
    3502        2798 :     pcnt += np;
    3503             : 
    3504        2798 :     if (totbits >= minbits || i == Dcnt - 1) { Dcnt = i + 1; break; }
    3505             :     /* merge lists */
    3506         599 :     for (j = pcnt - np - 1, k = np - 1, m = pcnt - 1; m >= 0; m--) {
    3507         580 :       if (k >= 0) {
    3508         555 :         if (j >= 0 && primes[j] > Dprimes[k])
    3509         301 :           primes[m] = primes[j--];
    3510             :         else
    3511         254 :           primes[m] = Dprimes[k--];
    3512             :       } else {
    3513          25 :         primes[m] = primes[j--];
    3514             :       }
    3515             :     }
    3516             :   }
    3517        2779 :   if (totbits < minbits) {
    3518           2 :     dbg_printf(1)("Only obtained %ld of %ld bits using %ld discriminants\n",
    3519             :                   totbits, minbits, Dcnt);
    3520           4 :     for (i = 0; i < Dcnt; i++) killblock((GEN)Ds[i].primes);
    3521           2 :     Dcnt = 0;
    3522             :   }
    3523        2779 :   return gc_long(av, Dcnt);
    3524             : }
    3525             : 
    3526             : /* Select discriminant(s) to use when calculating the modular
    3527             :  * polynomial of level L and invariant inv.
    3528             :  *
    3529             :  * INPUT:
    3530             :  * - L: level of modular polynomial (must be odd)
    3531             :  * - inv: invariant of modular polynomial
    3532             :  * - L0: result of select_L0(L, inv)
    3533             :  * - minbits: height of modular polynomial
    3534             :  * - flags: see below
    3535             :  * - tab: result of scanD0(L0)
    3536             :  * - tablen: length of tab
    3537             :  *
    3538             :  * OUTPUT:
    3539             :  * - Ds: the selected discriminant(s)
    3540             :  *
    3541             :  * RETURN:
    3542             :  * - the number of Ds found
    3543             :  *
    3544             :  * The flags parameter is constructed by ORing zero or more of the
    3545             :  * following values:
    3546             :  * - MODPOLY_USE_L1: force use of second class group generator
    3547             :  * - MODPOLY_NO_AUX_L: don't use auxillary class group elements
    3548             :  * - MODPOLY_IGNORE_SPARSE_FACTOR: obtain D for which h(D) > L + 1
    3549             :  *   rather than h(D) > (L + 1)/s */
    3550             : static long
    3551        2779 : modpoly_pickD(disc_info Ds[MODPOLY_MAX_DCNT], long L, long inv,
    3552             :   long L0, long max_L1, long minbits, long flags, D_entry *tab, long tablen)
    3553             : {
    3554        2779 :   pari_sp ltop = avma, btop;
    3555             :   disc_info Dinfo;
    3556             :   pari_timer T;
    3557             :   long modinv_p1, modinv_p2; /* const after next line */
    3558        2779 :   const long modinv_deg = modinv_degree(&modinv_p1, &modinv_p2, inv);
    3559        2779 :   const long pfilter = modinv_pfilter(inv), modinv_N = modinv_level(inv);
    3560             :   long i, k, use_L1, Dcnt, D0_i, d, cost, enum_cost, best_cost, totbits;
    3561        2779 :   const double L_bits = log2(L);
    3562             : 
    3563        2779 :   if (!odd(L)) pari_err_BUG("modpoly_pickD");
    3564             : 
    3565        2779 :   timer_start(&T);
    3566        2779 :   if (flags & MODPOLY_IGNORE_SPARSE_FACTOR) d = L+2;
    3567        2632 :   else d = ceildivuu(L+1, modinv_sparse_factor(inv)) + 1;
    3568             : 
    3569             :   /* Now set level to 0 unless we will need to compute N-isogenies */
    3570        2779 :   dbg_printf(1)("Using L0=%ld for L=%ld, d=%ld, modinv_N=%ld, modinv_deg=%ld\n",
    3571             :                 L0, L, d, modinv_N, modinv_deg);
    3572             : 
    3573             :   /* We use L1 if (L0|L) == 1 or if we are forced to by flags. */
    3574        2779 :   use_L1 = (kross(L0,L) > 0 || (flags & MODPOLY_USE_L1));
    3575             : 
    3576        2779 :   Dcnt = best_cost = totbits = 0;
    3577        2779 :   dbg_printf(3)("use_L1=%ld\n", use_L1);
    3578        2779 :   dbg_printf(3)("minbits = %ld\n", minbits);
    3579             : 
    3580             :   /* Iterate over the fundamental discriminants for L0 */
    3581     1703694 :   for (D0_i = 0; D0_i < tablen; D0_i++)
    3582             :   {
    3583     1700915 :     D_entry D0_entry = tab[D0_i];
    3584     1700915 :     long m, n0, h0, deg, L1, H_cost, twofactor, D0 = D0_entry.D;
    3585             :     double D0_bits;
    3586     2595344 :     if (! modinv_good_disc(inv, D0)) continue;
    3587     1078771 :     dbg_printf(3)("D0=%ld\n", D0);
    3588             :     /* don't allow either modinv_p1 or modinv_p2 to ramify */
    3589     1078771 :     if (kross(D0, L) < 1
    3590      487333 :         || (modinv_p1 > 1 && kross(D0, modinv_p1) < 1)
    3591      482424 :         || (modinv_p2 > 1 && kross(D0, modinv_p2) < 1)) {
    3592      606751 :       dbg_printf(3)("Bad D0=%ld due to nonsplit L or ramified level\n", D0);
    3593      606751 :       continue;
    3594             :     }
    3595      472020 :     deg = D0_entry.h; /* class poly degree */
    3596      472020 :     h0 = ((D0_entry.m & 2) ? 2*deg : deg); /* class number */
    3597             :     /* (D0_entry.m & 1) is 1 if ord(L0) < h0 (hence = h0/2),
    3598             :      *                  is 0 if ord(L0) = h0 */
    3599      472020 :     n0 = h0 / ((D0_entry.m & 1) + 1); /* = ord(L0) */
    3600             : 
    3601             :     /* Look for L1: for each smooth prime p */
    3602      472020 :     L1 = 0;
    3603    11481487 :     for (i = 1 ; i <= SMOOTH_PRIMES; i++)
    3604             :     {
    3605    11102938 :       long p = PRIMES[i];
    3606    11102938 :       if (p <= L0) continue;
    3607             :       /* If 1 + (D0 | p) = 1, i.e. p | D0 */
    3608    10450679 :       if (((D0_entry.m >> (2*i)) & 3) == 1) {
    3609             :         /* XXX: Why (p | L) = -1?  Presumably so (L^2 v^2 D0 | p) = -1? */
    3610      337953 :         if (p <= max_L1 && modinv_N % p && kross(p,L) < 0) { L1 = p; break; }
    3611             :       }
    3612             :     }
    3613      472020 :     if (i > SMOOTH_PRIMES && (n0 < h0 || use_L1))
    3614             :     { /* Didn't find suitable L1 though we need one */
    3615      214220 :       dbg_printf(3)("Bad D0=%ld because there is no good L1\n", D0);
    3616      214220 :       continue;
    3617             :     }
    3618      257800 :     dbg_printf(3)("Good D0=%ld with L1=%ld, n0=%ld, h0=%ld, d=%ld\n",
    3619             :                   D0, L1, n0, h0, d);
    3620             : 
    3621             :     /* We're finished if we have sufficiently many discriminants that satisfy
    3622             :      * the cost requirement */
    3623      257800 :     if (totbits > minbits && best_cost && h0*(L-1) > 3*best_cost) break;
    3624             : 
    3625      257800 :     D0_bits = log2(-D0);
    3626             :     /* If L^2 D0 is too big to fit in a BIL bit integer, skip D0. */
    3627      257800 :     if (D0_bits + 2 * L_bits > (BITS_IN_LONG - 1)) continue;
    3628             : 
    3629             :     /* m is the order of L0^n0 in L^2 D0? */
    3630      257800 :     m = primeform_exp_order(L0, n0, L * L * D0, n0 * (L-1));
    3631      257800 :     if (m < (L-1)/2) {
    3632       73458 :       dbg_printf(3)("Bad D0=%ld because %ld is less than (L-1)/2=%ld\n",
    3633           0 :                     D0, m, (L - 1)/2);
    3634       73458 :       continue;
    3635             :     }
    3636             :     /* Heuristic.  Doesn't end up contributing much. */
    3637      184342 :     H_cost = 2 * deg * deg;
    3638             : 
    3639             :     /* 0xc = 0b1100, so D0_entry.m & 0xc == 1 + (D0 | 2) */
    3640      184342 :     if ((D0 & 7) == 5) /* D0 = 5 (mod 8) */
    3641        5820 :       twofactor = ((D0_entry.m & 0xc) ? 1 : 3);
    3642             :     else
    3643      178522 :       twofactor = 0;
    3644             : 
    3645      184342 :     btop = avma;
    3646             :     /* For each small prime... */
    3647      640330 :     for (i = 0; i <= SMOOTH_PRIMES; i++) {
    3648             :       long h1, h2, D1, D2, n1, n2, dl1, dl20, dl21, p, q, j;
    3649             :       double p_bits;
    3650      640225 :       set_avma(btop);
    3651             :       /* i = 0 corresponds to 1, which we do not want to skip! (i.e. DK = D) */
    3652      640225 :       if (i) {
    3653      901933 :         if (modinv_odd_conductor(inv) && i == 1) continue;
    3654      446387 :         p = PRIMES[i];
    3655             :         /* Don't allow large factors in the conductor. */
    3656      547543 :         if (p > max_L1) break;
    3657      363306 :         if (p == L0 || p == L1 || p == L || p == modinv_p1 || p == modinv_p2)
    3658      125840 :           continue;
    3659      237466 :         p_bits = log2(p);
    3660             :         /* h1 is the class number of D1 = q^2 D0, where q = p^j (j defined in the loop below) */
    3661      237466 :         h1 = h0 * (p - ((D0_entry.m >> (2*i)) & 0x3) + 1);
    3662             :         /* q is the smallest power of p such that h1 >= d ~ "L + 1". */
    3663      239851 :         for (j = 1, q = p; h1 < d; j++, q *= p, h1 *= p)
    3664             :           ;
    3665      237466 :         D1 = q * q * D0;
    3666             :         /* can't have D1 = 0 mod 16 and hope to get any primes congruent to 3 mod 4 */
    3667      237466 :         if ((pfilter & IQ_FILTER_1MOD4) && !(D1 & 0xF)) continue;
    3668             :       } else {
    3669             :         /* i = 0, corresponds to "p = 1". */
    3670      184342 :         h1 = h0;
    3671      184342 :         D1 = D0;
    3672      184342 :         p = q = j = 1;
    3673      184342 :         p_bits = 0;
    3674             :       }
    3675             :       /* include a factor of 4 if D1 is 5 mod 8 */
    3676             :       /* XXX: No idea why he does this. */
    3677      421738 :       if (twofactor && (q & 1)) {
    3678       14550 :         if (modinv_odd_conductor(inv)) continue;
    3679         518 :         D1 *= 4;
    3680         518 :         h1 *= twofactor;
    3681             :       }
    3682             :       /* heuristic early abort; we may miss good D1's, but this saves time */
    3683      407706 :       if (totbits > minbits && best_cost && h1*(L-1) > 2.2*best_cost) continue;
    3684             : 
    3685             :       /* log2(D0 * (p^j)^2 * L^2 * twofactor) > (BIL - 1) -- params too big. */
    3686      793730 :       if (D0_bits + 2*j*p_bits + 2*L_bits
    3687      396865 :           + (twofactor && (q & 1) ? 2.0 : 0.0) > (BITS_IN_LONG-1)) continue;
    3688             : 
    3689      395173 :       if (! check_generators(&n1, NULL, D1, h1, n0, d, L0, L1)) continue;
    3690             : 
    3691      379565 :       if (n1 >= h1) dl1 = -1; /* fill it in later */
    3692      169680 :       else if ((dl1 = primeform_discrete_log(L0, L, n1, D1)) < 0) continue;
    3693      272906 :       dbg_printf(3)("Good D0=%ld, D1=%ld with q=%ld, L1=%ld, n1=%ld, h1=%ld\n",
    3694             :                     D0, D1, q, L1, n1, h1);
    3695      272906 :       if (modinv_deg && orientation_ambiguity(D1, L0, modinv_p1, modinv_p2, modinv_N))
    3696        1487 :         continue;
    3697             : 
    3698      271419 :       D2 = L * L * D1;
    3699      271419 :       h2 = h1 * (L-1);
    3700             :       /* m is the order of L0^n1 in cl(D2) */
    3701      271419 :       if (!check_generators(&n2, &m, D2, h2, n1, d*(L-1), L0, L1)) continue;
    3702             : 
    3703             :       /* This restriction on m is not necessary, but simplifies life later */
    3704      257704 :       if (m < (L-1)/2 || (!L1 && m < L-1)) {
    3705      123455 :         dbg_printf(3)("Bad D2=%ld for D1=%ld, D0=%ld, with n2=%ld, h2=%ld, L1=%ld, "
    3706             :                       "order of L0^n1 in cl(D2) is too small\n", D2, D1, D0, n2, h2, L1);
    3707      123455 :         continue;
    3708             :       }
    3709      134249 :       dl20 = n1;
    3710      134249 :       dl21 = 0;
    3711      134249 :       if (m < L-1) {
    3712       65259 :         GEN Q1 = qform_primeform2(L, D1), Q2, X;
    3713       65259 :         if (!Q1) pari_err_BUG("modpoly_pickD");
    3714       65259 :         Q2 = primeform_u(stoi(D2), L1);
    3715       65259 :         Q2 = qfbcomp(Q1, Q2); /* we know this element has order L-1 */
    3716       65259 :         Q1 = primeform_u(stoi(D2), L0);
    3717       65259 :         k = ((n2 & 1) ? 2*n2 : n2)/(L-1);
    3718       65259 :         Q1 = gpowgs(Q1, k);
    3719       65259 :         X = qfi_Shanks(Q2, Q1, L-1);
    3720       65259 :         if (!X) {
    3721       10382 :           dbg_printf(3)("Bad D2=%ld for D1=%ld, D0=%ld, with n2=%ld, h2=%ld, L1=%ld, "
    3722             :               "form of norm L^2 not generated by L0 and L1\n",
    3723             :               D2, D1, D0, n2, h2, L1);
    3724       10382 :           continue;
    3725             :         }
    3726       54877 :         dl20 = itos(X) * k;
    3727       54877 :         dl21 = 1;
    3728             :       }
    3729      123867 :       if (! (m < L-1 || n2 < d*(L-1)) && n1 >= d && ! use_L1)
    3730       68624 :         L1 = 0;  /* we don't need L1 */
    3731             : 
    3732      123867 :       if (!L1 && use_L1) {
    3733           0 :         dbg_printf(3)("not using D2=%ld for D1=%ld, D0=%ld, with n2=%ld, h2=%ld, L1=%ld, "
    3734             :                    "because we don't need L1 but must use it\n",
    3735             :                    D2, D1, D0, n2, h2, L1);
    3736           0 :         continue;
    3737             :       }
    3738             :       /* don't allow zero dl21 with L1 for the moment, since
    3739             :        * modpoly doesn't handle it - we may change this in the future */
    3740      123867 :       if (L1 && ! dl21) continue;
    3741      123501 :       dbg_printf(3)("Good D0=%ld, D1=%ld, D2=%ld with s=%ld^%ld, L1=%ld, dl2=%ld, n2=%ld, h2=%ld\n",
    3742             :                  D0, D1, D2, p, j, L1, dl20, n2, h2);
    3743             : 
    3744             :       /* This estimate is heuristic and fiddling with the
    3745             :        * parameters 5 and 0.25 can change things quite a bit. */
    3746      123501 :       enum_cost = n2 * (5 * L0 * L0 + 0.25 * L1 * L1);
    3747      123501 :       cost = enum_cost + H_cost;
    3748      123501 :       if (best_cost && cost > 2.2*best_cost) break;
    3749       29092 :       if (best_cost && cost >= 0.99*best_cost) continue;
    3750             : 
    3751        7247 :       Dinfo.GENcode0 = evaltyp(t_VECSMALL)|evallg(13);
    3752        7247 :       Dinfo.inv = inv;
    3753        7247 :       Dinfo.L = L;
    3754        7247 :       Dinfo.D0 = D0;
    3755        7247 :       Dinfo.D1 = D1;
    3756        7247 :       Dinfo.L0 = L0;
    3757        7247 :       Dinfo.L1 = L1;
    3758        7247 :       Dinfo.n1 = n1;
    3759        7247 :       Dinfo.n2 = n2;
    3760        7247 :       Dinfo.dl1 = dl1;
    3761        7247 :       Dinfo.dl2_0 = dl20;
    3762        7247 :       Dinfo.dl2_1 = dl21;
    3763        7247 :       Dinfo.cost = cost;
    3764             : 
    3765        7247 :       if (!modpoly_pickD_primes(NULL, NULL, 0, NULL, 0, &Dinfo.bits, minbits, &Dinfo))
    3766          58 :         continue;
    3767        7189 :       dbg_printf(2)("Best D2=%ld, D1=%ld, D0=%ld with s=%ld^%ld, L1=%ld, "
    3768             :                  "n1=%ld, n2=%ld, cost ratio %.2f, bits=%ld\n",
    3769             :                  D2, D1, D0, p, j, L1, n1, n2,
    3770           0 :                  (double)cost/(d*(L-1)), Dinfo.bits);
    3771             :       /* Insert Dinfo into the Ds array.  Ds is sorted by ascending cost. */
    3772       36011 :       for (j = 0; j < Dcnt; j++)
    3773       33221 :         if (Dinfo.cost < Ds[j].cost) break;
    3774        7189 :       if (n2 > MAX_VOLCANO_FLOOR_SIZE && n2*(L1 ? 2 : 1) > 1.2* (d*(L-1)) ) {
    3775           0 :         dbg_printf(3)("Not using D1=%ld, D2=%ld for space reasons\n", D1, D2);
    3776           0 :         continue;
    3777             :       }
    3778        7189 :       if (j == Dcnt && Dcnt == MODPOLY_MAX_DCNT)
    3779           0 :         continue;
    3780        7189 :       totbits += Dinfo.bits;
    3781        7189 :       if (Dcnt == MODPOLY_MAX_DCNT) totbits -= Ds[Dcnt-1].bits;
    3782        7189 :       if (Dcnt < MODPOLY_MAX_DCNT) Dcnt++;
    3783        7189 :       if (n2 > MAX_VOLCANO_FLOOR_SIZE)
    3784           0 :         dbg_printf(3)("totbits=%ld, minbits=%ld\n", totbits, minbits);
    3785       15614 :       for (k = Dcnt-1; k > j; k--) Ds[k] = Ds[k-1];
    3786        7189 :       Ds[k] = Dinfo;
    3787        7189 :       best_cost = (totbits > minbits)? Ds[Dcnt-1].cost: 0;
    3788             :       /* if we were able to use D1 with s = 1, there is no point in
    3789             :        * using any larger D1 for the same D0 */
    3790        7189 :       if (!i) break;
    3791             :     } /* END FOR over small primes */
    3792             :   } /* END WHILE over D0's */
    3793        2779 :   dbg_printf(2)("  checked %ld of %ld fundamental discriminants to find suitable "
    3794             :                 "discriminant (Dcnt = %ld)\n", D0_i, tablen, Dcnt);
    3795        2779 :   if ( ! Dcnt) {
    3796           0 :     dbg_printf(1)("failed completely for L=%ld\n", L);
    3797           0 :     return 0;
    3798             :   }
    3799             : 
    3800        2779 :   Dcnt = calc_primes_for_discriminants(Ds, Dcnt, L, minbits);
    3801             : 
    3802             :   /* fill in any missing dl1's */
    3803        5575 :   for (i = 0 ; i < Dcnt; i++)
    3804        2796 :     if (Ds[i].dl1 < 0 &&
    3805        2751 :        (Ds[i].dl1 = primeform_discrete_log(L0, L, Ds[i].n1, Ds[i].D1)) < 0)
    3806           0 :         pari_err_BUG("modpoly_pickD");
    3807        2779 :   if (DEBUGLEVEL > 1+3) {
    3808           0 :     err_printf("Selected %ld discriminants using %ld msecs\n", Dcnt, timer_delay(&T));
    3809           0 :     for (i = 0 ; i < Dcnt ; i++)
    3810             :     {
    3811           0 :       GEN H = classno(stoi(Ds[i].D0));
    3812           0 :       long h0 = itos(H);
    3813           0 :       err_printf ("    D0=%ld, h(D0)=%ld, D=%ld, L0=%ld, L1=%ld, "
    3814             :           "cost ratio=%.2f, enum ratio=%.2f,",
    3815           0 :           Ds[i].D0, h0, Ds[i].D1, Ds[i].L0, Ds[i].L1,
    3816           0 :           (double)Ds[i].cost/(d*(L-1)),
    3817           0 :           (double)(Ds[i].n2*(Ds[i].L1 ? 2 : 1))/(d*(L-1)));
    3818           0 :       err_printf (" %ld primes, %ld bits\n", Ds[i].nprimes, Ds[i].bits);
    3819             :     }
    3820             :   }
    3821        2779 :   return gc_long(ltop, Dcnt);
    3822             : }
    3823             : 
    3824             : static int
    3825    13268758 : _qsort_cmp(const void *a, const void *b)
    3826             : {
    3827    13268758 :   D_entry *x = (D_entry *)a, *y = (D_entry *)b;
    3828             :   long u, v;
    3829             : 
    3830             :   /* u and v are the class numbers of x and y */
    3831    13268758 :   u = x->h * (!!(x->m & 2) + 1);
    3832    13268758 :   v = y->h * (!!(y->m & 2) + 1);
    3833             :   /* Sort by class number */
    3834    13268758 :   if (u < v) return -1;
    3835     9233001 :   if (u > v) return 1;
    3836             :   /* Sort by discriminant (which is < 0, hence the sign reversal) */
    3837     2770420 :   if (x->D > y->D) return -1;
    3838           0 :   if (x->D < y->D) return 1;
    3839           0 :   return 0;
    3840             : }
    3841             : 
    3842             : /* Build a table containing fundamental D, |D| <= maxD whose class groups
    3843             :  * - are cyclic generated by an element of norm L0
    3844             :  * - have class number at most maxh
    3845             :  * The table is ordered using _qsort_cmp above, which ranks the discriminants
    3846             :  * by class number, then by absolute discriminant.
    3847             :  *
    3848             :  * INPUT:
    3849             :  * - maxd: largest allowed discriminant
    3850             :  * - maxh: largest allowed class number
    3851             :  * - L0: norm of class group generator (2, 3, 5, or 7)
    3852             :  *
    3853             :  * OUTPUT:
    3854             :  * - tablelen: length of return value
    3855             :  *
    3856             :  * RETURN:
    3857             :  * - array of {D, h(D), kronecker symbols for small p} */
    3858             : static D_entry *
    3859        2779 : scanD0(long *tablelen, long *minD, long maxD, long maxh, long L0)
    3860             : {
    3861             :   pari_sp av;
    3862             :   D_entry *tab;
    3863             :   long d, cnt;
    3864             : 
    3865             :   /* NB: As seen in the loop below, the real class number of D can be */
    3866             :   /* 2*maxh if cl(D) is cyclic. */
    3867        2779 :   if (maxh < 0) pari_err_BUG("scanD0");
    3868             : 
    3869        2779 :   tab = (D_entry *) stack_malloc((maxD/4)*sizeof(*tab)); /* Overestimate */
    3870             :   /* d = 7, 11, 15, 19, 23, ... */
    3871     6947500 :   for (av = avma, d = *minD, cnt = 0; d <= maxD; d += 4, set_avma(av))
    3872             :   {
    3873             :     GEN DD, fa, ordL, f, q, e;
    3874     6944721 :     long i, j, k, n, h, L1, D = -d;
    3875             :     ulong m;
    3876             : 
    3877     6944721 :     if (kross(D, L0) < 1) continue;
    3878     3142330 :     fa = factoru(d); q = gel(fa, 1); k = lg(q) - 1;
    3879             :     /* restrict to possibly cyclic class groups */
    3880     3142330 :     if (k > 2) continue;
    3881     2457425 :     e = gel(fa, 2);
    3882     5859790 :     for (i = 1; i <= k; i++)
    3883     3745882 :       if (e[i] > 1) break;
    3884     2457425 :     if (i <= k) continue; /* restrict to square-free discriminant */
    3885             : 
    3886             :     /* L1 initially the first factor of d if small enough, otherwise ignored */
    3887     2113908 :     L1 = (k > 1 && q[1] <= MAX_L1)? q[1]: 0;
    3888             : 
    3889             :     /* Check if h(D) is too big */
    3890     2113908 :     h = hclassno6u(d) / 6;
    3891     2113908 :     if (h > 2*maxh || (!L1 && h > maxh)) continue;
    3892             : 
    3893             :     /* Check if ord(f) is not big enough to generate at least half the
    3894             :      * class group (where f is the L0-primeform). */
    3895     1982698 :     DD = stoi(D);
    3896     1982698 :     f = primeform_u(DD, L0);
    3897     1982698 :     ordL = qfi_order(qfbred_i(f), stoi(h));
    3898     1982698 :     n = itos(ordL);
    3899     1982698 :     if (n < h/2 || (!L1 && n < h)) continue;
    3900             : 
    3901             :     /* If f is big enough, great!  Otherwise, for each potential L1,
    3902             :      * do a discrete log to see if it is NOT in the subgroup generated
    3903             :      * by L0; stop as soon as such is found. */
    3904     1700915 :     for (j = 1;; j++) {
    3905     1923597 :       if (n == h || (L1 && !qfi_Shanks(primeform_u(DD, L1), f, n))) {
    3906     1613412 :         dbg_printf(2)("D0=%ld good with L1=%ld\n", D, L1);
    3907     1613412 :         break;
    3908             :       }
    3909      310185 :       if (!L1) break;
    3910      222682 :       L1 = (j <= k && k > 1 && q[j] <= MAX_L1 ? q[j] : 0);
    3911             :     }
    3912             :     /* The first bit of m indicates whether f generates a proper
    3913             :      * subgroup of cl(D) (hence implying that we need L1) or if f
    3914             :      * generates the whole class group. */
    3915     1700915 :     m = (n < h ? 1 : 0);
    3916             :     /* bits i and i+1 of m give the 2-bit number 1 + (D|p) where p is
    3917             :      * the ith prime. */
    3918    50419664 :     for (i = 1 ; i <= SMOOTH_PRIMES; i++)
    3919             :     {
    3920    48718749 :       ulong x  = (ulong) (1 + kross(D, PRIMES[i]));
    3921    48718749 :       m |= x << (2*i);
    3922             :     }
    3923             : 
    3924             :     /* Insert d, h and m into the table */
    3925     1700915 :     tab[cnt].D = D;
    3926     1700915 :     tab[cnt].h = h;
    3927     1700915 :     tab[cnt].m = m; cnt++;
    3928             :   }
    3929             : 
    3930             :   /* Sort the table */
    3931        2779 :   qsort(tab, cnt, sizeof(*tab), _qsort_cmp);
    3932        2779 :   *tablelen = cnt; *minD = d; return tab;
    3933             : }
    3934             : 
    3935             : /* Populate Ds with discriminants (and attached data) that can be
    3936             :  * used to calculate the modular polynomial of level L and invariant
    3937             :  * inv.  Return the number of discriminants found. */
    3938             : static long
    3939        2777 : discriminant_with_classno_at_least(disc_info bestD[MODPOLY_MAX_DCNT],
    3940             :   long L, long inv, GEN Q, long ignore_sparse)
    3941             : {
    3942             :   enum { SMALL_L_BOUND = 101 };
    3943        2777 :   long max_max_D = 160000 * (inv ? 2 : 1);
    3944             :   long minD, maxD, maxh, L0, max_L1, minbits, Dcnt, flags, s, d, i, tablen;
    3945             :   D_entry *tab;
    3946        2777 :   double eps, cost, best_eps = -1.0, best_cost = -1.0;
    3947             :   disc_info Ds[MODPOLY_MAX_DCNT];
    3948        2777 :   long best_cnt = 0;
    3949             :   pari_timer T;
    3950        2777 :   timer_start(&T);
    3951             : 
    3952        2777 :   s = modinv_sparse_factor(inv);
    3953        2777 :   d = ceildivuu(L+1, s) + 1;
    3954             : 
    3955             :   /* maxD of 10000 allows us to get a satisfactory discriminant in
    3956             :    * under 250ms in most cases. */
    3957        2777 :   maxD = 10000;
    3958             :   /* Allow the class number to overshoot L by 50%.  Must be at least
    3959             :    * 1.1*L, and higher values don't seem to provide much benefit,
    3960             :    * except when L is small, in which case it's necessary to get any
    3961             :    * discriminant at all in some cases. */
    3962        2777 :   maxh = (L / s < SMALL_L_BOUND) ? 10 * L : 1.5 * L;
    3963             : 
    3964        2777 :   flags = ignore_sparse ? MODPOLY_IGNORE_SPARSE_FACTOR : 0;
    3965        2777 :   L0 = select_L0(L, inv, 0);
    3966        2777 :   max_L1 = L / 2 + 2;    /* for L=11 we need L1=7 for j */
    3967        2777 :   minbits = modpoly_height_bound(L, inv);
    3968        2777 :   if (Q) minbits += expi(Q);
    3969        2777 :   minD = 7;
    3970             : 
    3971        5554 :   while ( ! best_cnt) {
    3972        2779 :     while (maxD <= max_max_D) {
    3973             :       /* TODO: Find a way to re-use tab when we need multiple modpolys */
    3974        2779 :       tab = scanD0(&tablen, &minD, maxD, maxh, L0);
    3975        2779 :       dbg_printf(1)("Found %ld potential fundamental discriminants\n", tablen);
    3976             : 
    3977        2779 :       Dcnt = modpoly_pickD(Ds, L, inv, L0, max_L1, minbits, flags, tab, tablen);
    3978        2779 :       eps = 0.0;
    3979        2779 :       cost = 0.0;
    3980             : 
    3981        2779 :       if (Dcnt) {
    3982        2777 :         long n1 = 0;
    3983        5573 :         for (i = 0; i < Dcnt; i++) {
    3984        2796 :           n1 = maxss(n1, Ds[i].n1);
    3985        2796 :           cost += Ds[i].cost;
    3986             :         }
    3987        2777 :         eps = (n1 * s - L) / (double)L;
    3988             : 
    3989        2777 :         if (best_cost < 0.0 || cost < best_cost) {
    3990        2777 :           if (best_cnt)
    3991           0 :             for (i = 0; i < best_cnt; i++) killblock((GEN)bestD[i].primes);
    3992        2777 :           (void) memcpy(bestD, Ds, Dcnt * sizeof(disc_info));
    3993        2777 :           best_cost = cost;
    3994        2777 :           best_cnt = Dcnt;
    3995        2777 :           best_eps = eps;
    3996             :           /* We're satisfied if n1 is within 5% of L. */
    3997        2777 :           if (L / s <= SMALL_L_BOUND || eps < 0.05) break;
    3998             :         } else {
    3999           0 :           for (i = 0; i < Dcnt; i++) killblock((GEN)Ds[i].primes);
    4000             :         }
    4001             :       } else {
    4002           2 :         if (log2(maxD) > BITS_IN_LONG - 2 * (log2(L) + 2))
    4003             :         {
    4004           0 :           char *err = stack_sprintf("modular polynomial of level %ld and invariant %ld",L,inv);
    4005           0 :           pari_err(e_ARCH, err);
    4006             :         }
    4007             :       }
    4008           2 :       maxD *= 2;
    4009           2 :       minD += 4;
    4010           2 :       dbg_printf(0)("  Doubling discriminant search space (closest: %.1f%%, cost ratio: %.1f)...\n", eps*100, cost/(double)(d*(L-1)));
    4011             :     }
    4012        2777 :     max_max_D *= 2;
    4013             :   }
    4014             : 
    4015        2777 :   if (DEBUGLEVEL > 3) {
    4016           0 :     pari_sp av = avma;
    4017           0 :     err_printf("Found discriminant(s):\n");
    4018           0 :     for (i = 0; i < best_cnt; ++i) {
    4019           0 :       long h = itos(classno(stoi(bestD[i].D1)));
    4020           0 :       set_avma(av);
    4021           0 :       err_printf("  D = %ld, h = %ld, u = %ld, L0 = %ld, L1 = %ld, n1 = %ld, n2 = %ld, cost = %ld\n",
    4022           0 :           bestD[i].D1, h, usqrt(bestD[i].D1 / bestD[i].D0), bestD[i].L0,
    4023           0 :           bestD[i].L1, bestD[i].n1, bestD[i].n2, bestD[i].cost);
    4024             :     }
    4025           0 :     err_printf("(off target by %.1f%%, cost ratio: %.1f)\n",
    4026           0 :                best_eps*100, best_cost/(double)(d*(L-1)));
    4027             :   }
    4028        2777 :   return best_cnt;
    4029             : }

Generated by: LCOV version 1.13