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

Generated by: LCOV version 1.16