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

Generated by: LCOV version 1.16