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 - polclass.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25406-bf255ab81b) Lines: 833 858 97.1 %
Date: 2020-06-04 05:59:24 Functions: 58 59 98.3 %
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. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : #define dbg_printf(lvl) if (DEBUGLEVEL >= (lvl) + 3) err_printf
      18             : 
      19             : /*
      20             :  * SECTION: Functions dedicated to finding a j-invariant with a given
      21             :  * trace.
      22             :  */
      23             : 
      24             : /* TODO: This code is shared with
      25             :  * torsion_compatible_with_characteristic() in 'torsion.c'. */
      26             : static void
      27      119354 : hasse_bounds(long *low, long *high, long p)
      28             : {
      29      119354 :   long two_sqrt_p = usqrt(4*p);
      30      119354 :   *low = p + 1 - two_sqrt_p;
      31      119354 :   *high = p + 1 + two_sqrt_p;
      32      119354 : }
      33             : 
      34             : /* a / b : a and b are from factoru and b must divide a exactly */
      35             : INLINE GEN
      36        2192 : famatsmall_divexact(GEN a, GEN b)
      37             : {
      38        2192 :   GEN a1 = gel(a,1), a2 = gel(a,2), c1, c2;
      39        2192 :   GEN b1 = gel(b,1), b2 = gel(b,2);
      40        2192 :   long i, j, k, la = lg(a1);
      41        2192 :   c1 = cgetg(la, t_VECSMALL);
      42        2192 :   c2 = cgetg(la, t_VECSMALL);
      43        8018 :   for (i = j = k = 1; j < la; j++)
      44             :   {
      45        5826 :     c1[k] = a1[j];
      46        5826 :     c2[k] = a2[j];
      47        5826 :     if (a1[j] == b1[i]) { c2[k] -= b2[i++]; if (!c2[k]) continue; }
      48        3737 :     k++;
      49             :   }
      50        2192 :   setlg(c1, k);
      51        2192 :   setlg(c2, k); return mkvec2(c1,c2);
      52             : }
      53             : 
      54             : /* This is Sutherland, 2009, TestCurveOrder.
      55             :  *
      56             :  * [a4, a6] and p specify an elliptic curve over FF_p.  N0,N1 are the two
      57             :  * possible curve orders, and n0,n1 their factoru */
      58             : static long
      59      140885 : test_curve_order(norm_eqn_t ne, ulong a4, ulong a6,
      60             :   long N0, long N1, GEN n0, GEN n1, const long hasse[2])
      61             : {
      62      140885 :   pari_sp ltop = avma, av;
      63      140885 :   ulong a4t, a6t, p = ne->p, pi = ne->pi, T = ne->T, swapped = 0;
      64             :   long m0, m1, hasse_low, hasse_high;
      65             : 
      66      140885 :   if (p <= 11) {
      67         140 :     long card = (long)p + 1 - Fl_elltrace(a4, a6, p);
      68         140 :     return card == N0 || card == N1;
      69             :   }
      70             :   /* [a4, a6] is the given curve and [a4t, a6t] is its quadratic twist */
      71      140745 :   Fl_elltwist_disc(a4, a6, T, p, &a4t, &a6t);
      72             : 
      73      140745 :   m0 = m1 = 1;
      74      140745 :   if (N0 + N1 != 2 * (long)p + 2) pari_err_BUG("test_curve_order");
      75             : 
      76      140745 :   hasse_low = hasse[0];
      77      140745 :   hasse_high = hasse[1];
      78      140745 :   for (av = avma;;)
      79      107271 :   {
      80             :     GEN pt, Q, fa0;
      81             :     long a1, x, n_s;
      82             : 
      83      248016 :     pt = random_Flj_pre(a4, a6, p, pi);
      84      248016 :     Q = Flj_mulu_pre(pt, m0, a4, p, pi);
      85      248016 :     fa0 = m0 == 1? n0: famatsmall_divexact(n0, factoru(m0));
      86      248016 :     n_s = Flj_order_ufact(Q, N0 / m0, fa0, a4, p, pi);
      87      248016 :     if (n_s == 0) {
      88             :       /* If m0 divides N1 and m1 divides N0 and N0 < N1, then swap */
      89      106881 :       if (!swapped && N1 % m0 == 0 && N0 % m1 == 0) {
      90       85377 :         swapspec(n0, n1, N0, N1);
      91       85377 :         swapped = 1; continue;
      92             :       }
      93       21504 :       return gc_long(ltop,0);
      94             :     }
      95             : 
      96      141135 :     m0 *= n_s;
      97      141135 :     a1 = (2 * p + 2) % m1;
      98      141135 :     x = (hasse_low + m0 - 1) / m0; /* using ceil(n/d) = (n + d - 1)/d */
      99      141135 :     x *= m0;
     100      275018 :     for ( ; x <= hasse_high; x += m0)
     101      155777 :       if ((x % m1) == a1 && x != N0 && x != N1) break;
     102             :     /* every x in N was either N0 or N1, so we return true */
     103      141135 :     if (x > hasse_high) return gc_long(ltop,1);
     104             : 
     105       21894 :     lswap(a4, a4t);
     106       21894 :     lswap(a6, a6t);
     107       21894 :     lswap(m0, m1); set_avma(av);
     108             :   }
     109             : }
     110             : 
     111             : static GEN
     112      506850 : random_FleV(GEN x, GEN a6, ulong p, ulong pi)
     113     2945350 : { pari_APPLY_type(t_VEC, random_Fle_pre(uel(x,i), uel(a6,i), p, pi)) }
     114             : 
     115             : /**
     116             :  * START Code from AVSs "torcosts.h"
     117             :  */
     118             : 
     119             : struct torctab_rec {
     120             :   int m;
     121             :   int fix2, fix3;
     122             :   int N;
     123             :   int s2_flag;
     124             :   int t3_flag;
     125             :   double rating;
     126             : };
     127             : 
     128             : /*
     129             :   These costs assume p=2 mod 3, 3 mod 4 and not 1 mod N
     130             : */
     131             : 
     132             : static struct torctab_rec torctab1[] = {
     133             : { 11, 1, 1, 11, 1, 1, 0.047250 },
     134             : { 33, 1, 0, 11, 1, 2, 0.047250 },
     135             : { 22, 1, 1, 11, 3, 1, 0.055125 },
     136             : { 66, 1, 0, 11, 3, 2, 0.055125 },
     137             : { 11, 1, 0, 11, 1, 0, 0.058000 },
     138             : { 13, 1, 1, 13, 1, 1, 0.058542 },
     139             : { 39, 1, 0, 13, 1, 2, 0.058542 },
     140             : { 22, 0, 1, 11, 2, 1, 0.061333 },
     141             : { 66, 0, 0, 11, 2, 2, 0.061333 },
     142             : { 22, 1, 0, 11, 3, 0, 0.061750 },
     143             : { 14, 1, 1, 14, 3, 1, 0.062500 },
     144             : { 42, 1, 0, 14, 3, 2, 0.062500 },
     145             : { 26, 1, 1, 13, 3, 1, 0.064583 },
     146             : { 78, 1, 0, 13, 3, 2, 0.064583 },
     147             : { 28, 0, 1, 14, 4, 1, 0.065625 },
     148             : { 84, 0, 0, 14, 4, 2, 0.065625 },
     149             : { 7, 1, 1, 7, 1, 1, 0.068750 },
     150             : { 13, 1, 0, 13, 1, 0, 0.068750 },
     151             : { 21, 1, 0, 7, 1, 2, 0.068750 },
     152             : { 26, 1, 0, 13, 3, 0, 0.069583 },
     153             : { 17, 1, 1, 17, 1, 1, 0.069687 },
     154             : { 51, 1, 0, 17, 1, 2, 0.069687 },
     155             : { 11, 0, 1, 11, 0, 1, 0.072500 },
     156             : { 33, 0, 0, 11, 0, 2, 0.072500 },
     157             : { 44, 1, 0, 11, 130, 0, 0.072667 },
     158             : { 52, 0, 1, 13, 4, 1, 0.073958 },
     159             : { 156, 0, 0, 13, 4, 2, 0.073958 },
     160             : { 34, 1, 1, 17, 3, 1, 0.075313 },
     161             : { 102, 1, 0, 17, 3, 2, 0.075313 },
     162             : { 15, 1, 0, 15, 1, 0, 0.075625 },
     163             : { 13, 0, 1, 13, 0, 1, 0.076667 },
     164             : { 39, 0, 0, 13, 0, 2, 0.076667 },
     165             : { 44, 0, 0, 11, 4, 0, 0.076667 },
     166             : { 30, 1, 0, 15, 3, 0, 0.077188 },
     167             : { 22, 0, 0, 11, 2, 0, 0.077333 },
     168             : { 34, 1, 0, 17, 3, 0, 0.077969 },
     169             : { 17, 1, 0, 17, 1, 0, 0.078750 },
     170             : { 14, 0, 1, 14, 0, 1, 0.080556 },
     171             : { 28, 0, 0, 14, 4, 0, 0.080556 },
     172             : { 42, 0, 0, 14, 0, 2, 0.080556 },
     173             : { 7, 1, 0, 7, 1, 0, 0.080833 },
     174             : { 9, 1, 0, 9, 1, 0, 0.080833 },
     175             : { 68, 0, 1, 17, 4, 1, 0.081380 },
     176             : { 204, 0, 0, 17, 4, 2, 0.081380 },
     177             : { 52, 0, 0, 13, 4, 0, 0.082292 },
     178             : { 10, 1, 1, 10, 3, 1, 0.084687 },
     179             : { 17, 0, 1, 17, 0, 1, 0.084687 },
     180             : { 51, 0, 0, 17, 0, 2, 0.084687 },
     181             : { 20, 0, 1, 10, 4, 1, 0.085938 },
     182             : { 60, 0, 0, 10, 4, 2, 0.085938 },
     183             : { 19, 1, 1, 19, 1, 1, 0.086111 },
     184             : { 57, 1, 0, 19, 1, 2, 0.086111 },
     185             : { 68, 0, 0, 17, 4, 0, 0.088281 },
     186             : { 38, 1, 1, 19, 3, 1, 0.089514 },
     187             : { 114, 1, 0, 19, 3, 2, 0.089514 },
     188             : { 20, 0, 0, 10, 4, 0, 0.090625 },
     189             : { 36, 0, 0, 18, 4, 0, 0.090972 },
     190             : { 26, 0, 0, 13, 2, 0, 0.091667 },
     191             : { 11, 0, 0, 11, 0, 0, 0.092000 },
     192             : { 19, 1, 0, 19, 1, 0, 0.092778 },
     193             : { 38, 1, 0, 19, 3, 0, 0.092778 },
     194             : { 14, 1, 0, 7, 3, 0, 0.092917 },
     195             : { 18, 1, 0, 9, 3, 0, 0.092917 },
     196             : { 76, 0, 1, 19, 4, 1, 0.095255 },
     197             : { 228, 0, 0, 19, 4, 2, 0.095255 },
     198             : { 10, 0, 1, 10, 0, 1, 0.096667 },
     199             : { 13, 0, 0, 13, 0, 0, 0.096667 },
     200             : { 30, 0, 0, 10, 0, 2, 0.096667 },
     201             : { 19, 0, 1, 19, 0, 1, 0.098333 },
     202             : { 57, 0, 0, 19, 0, 2, 0.098333 },
     203             : { 17, 0, 0, 17, 0, 0, 0.100000 },
     204             : { 23, 1, 1, 23, 1, 1, 0.100227 },
     205             : { 69, 1, 0, 23, 1, 2, 0.100227 },
     206             : { 7, 0, 1, 7, 0, 1, 0.100833 },
     207             : { 21, 0, 0, 7, 0, 2, 0.100833 },
     208             : { 76, 0, 0, 19, 4, 0, 0.102083 },
     209             : { 14, 0, 0, 14, 0, 0, 0.102222 },
     210             : { 18, 0, 0, 9, 2, 0, 0.102222 },
     211             : { 5, 1, 1, 5, 1, 1, 0.103125 },
     212             : { 46, 1, 1, 23, 3, 1, 0.104318 },
     213             : { 138, 1, 0, 23, 3, 2, 0.104318 },
     214             : { 23, 1, 0, 23, 1, 0, 0.105682 },
     215             : { 46, 1, 0, 23, 3, 0, 0.106705 },
     216             : { 92, 0, 1, 23, 4, 1, 0.109091 },
     217             : { 276, 0, 0, 23, 4, 2, 0.109091 },
     218             : { 19, 0, 0, 19, 0, 0, 0.110000 },
     219             : { 23, 0, 1, 23, 0, 1, 0.112273 },
     220             : { 69, 0, 0, 23, 0, 2, 0.112273 },
     221             : { 7, 0, 0, 7, 0, 0, 0.113333 },
     222             : { 9, 0, 0, 9, 0, 0, 0.113333 },
     223             : { 92, 0, 0, 23, 4, 0, 0.113826 },
     224             : { 16, 0, 1, 16, 0, 1, 0.118125 },
     225             : { 48, 0, 0, 16, 0, 2, 0.118125 },
     226             : { 5, 1, 0, 5, 1, 0, 0.121250 },
     227             : { 15, 0, 0, 15, 0, 0, 0.121250 },
     228             : { 10, 0, 0, 10, 0, 0, 0.121667 },
     229             : { 23, 0, 0, 23, 0, 0, 0.123182 },
     230             : { 12, 0, 0, 12, 0, 0, 0.141667 },
     231             : { 5, 0, 1, 5, 0, 1, 0.145000 },
     232             : { 16, 0, 0, 16, 0, 0, 0.145000 },
     233             : { 8, 0, 1, 8, 0, 1, 0.151250 },
     234             : { 29, 1, 1, 29, 1, 1, 0.153036 },
     235             : { 87, 1, 0, 29, 1, 2, 0.153036 },
     236             : { 25, 0, 0, 25, 0, 0, 0.155000 },
     237             : { 58, 1, 1, 29, 3, 1, 0.156116 },
     238             : { 174, 1, 0, 29, 3, 2, 0.156116 },
     239             : { 29, 1, 0, 29, 1, 0, 0.157500 },
     240             : { 58, 1, 0, 29, 3, 0, 0.157500 },
     241             : { 116, 0, 1, 29, 4, 1, 0.161086 },
     242             : { 29, 0, 1, 29, 0, 1, 0.163393 },
     243             : { 87, 0, 0, 29, 0, 2, 0.163393 },
     244             : { 116, 0, 0, 29, 4, 0, 0.163690 },
     245             : { 5, 0, 0, 5, 0, 0, 0.170000 },
     246             : { 8, 0, 0, 8, 0, 0, 0.170000 },
     247             : { 29, 0, 0, 29, 0, 0, 0.171071 },
     248             : { 31, 1, 1, 31, 1, 1, 0.186583 },
     249             : { 93, 1, 0, 31, 1, 2, 0.186583 },
     250             : { 62, 1, 1, 31, 3, 1, 0.189750 },
     251             : { 186, 1, 0, 31, 3, 2, 0.189750 },
     252             : { 31, 1, 0, 31, 1, 0, 0.191333 },
     253             : { 62, 1, 0, 31, 3, 0, 0.192167 },
     254             : { 124, 0, 1, 31, 4, 1, 0.193056 },
     255             : { 31, 0, 1, 31, 0, 1, 0.195333 },
     256             : { 93, 0, 0, 31, 0, 2, 0.195333 },
     257             : { 124, 0, 0, 31, 4, 0, 0.197917 },
     258             : { 2, 1, 1, 2, 3, 1, 0.200000 },
     259             : { 6, 1, 0, 2, 3, 2, 0.200000 },
     260             : { 31, 0, 0, 31, 0, 0, 0.206667 },
     261             : { 4, 1, 1, 4, 130, 1, 0.214167 },
     262             : { 6, 0, 0, 6, 0, 0, 0.226667 },
     263             : { 3, 1, 0, 3, 1, 0, 0.230000 },
     264             : { 4, 0, 1, 4, 0, 1, 0.241667 },
     265             : { 4, 1, 0, 2, 130, 0, 0.266667 },
     266             : { 4, 0, 0, 4, 0, 0, 0.283333 },
     267             : { 3, 0, 0, 3, 0, 0, 0.340000 },
     268             : { 1, 1, 1, 1, 1, 1, 0.362500 },
     269             : { 2, 0, 1, 2, 0, 1, 0.386667 },
     270             : { 1, 1, 0, 1, 1, 0, 0.410000 },
     271             : { 2, 0, 0, 2, 0, 0, 0.453333 },
     272             : };
     273             : 
     274             : static struct torctab_rec torctab2[] = {
     275             : { 11, 1, 1, 11, 1, 1, 0.047250 },
     276             : { 33, 1, 0, 11, 1, 2, 0.047250 },
     277             : { 22, 1, 1, 11, 3, 1, 0.055125 },
     278             : { 66, 1, 0, 11, 3, 2, 0.055125 },
     279             : { 13, 1, 1, 13, 1, 1, 0.057500 },
     280             : { 39, 1, 0, 13, 1, 2, 0.057500 },
     281             : { 11, 1, 0, 11, 1, 0, 0.058000 },
     282             : { 22, 0, 1, 11, 2, 1, 0.061333 },
     283             : { 66, 0, 0, 11, 2, 2, 0.061333 },
     284             : { 14, 1, 1, 14, 3, 1, 0.061458 },
     285             : { 42, 1, 0, 14, 3, 2, 0.061458 },
     286             : { 22, 1, 0, 11, 3, 0, 0.061750 },
     287             : { 26, 1, 1, 13, 3, 1, 0.064062 },
     288             : { 78, 1, 0, 13, 3, 2, 0.064062 },
     289             : { 28, 0, 1, 14, 4, 1, 0.065625 },
     290             : { 84, 0, 0, 14, 4, 2, 0.065625 },
     291             : { 13, 1, 0, 13, 1, 0, 0.066667 },
     292             : { 26, 1, 0, 13, 3, 0, 0.069583 },
     293             : { 17, 1, 1, 17, 1, 1, 0.069687 },
     294             : { 51, 1, 0, 17, 1, 2, 0.069687 },
     295             : { 11, 0, 1, 11, 0, 1, 0.070000 },
     296             : { 33, 0, 0, 11, 0, 2, 0.070000 },
     297             : { 7, 1, 1, 7, 1, 1, 0.070417 },
     298             : { 21, 1, 0, 7, 1, 2, 0.070417 },
     299             : { 15, 1, 0, 15, 1, 0, 0.072500 },
     300             : { 52, 0, 1, 13, 4, 1, 0.073090 },
     301             : { 156, 0, 0, 13, 4, 2, 0.073090 },
     302             : { 34, 1, 1, 17, 3, 1, 0.074219 },
     303             : { 102, 1, 0, 17, 3, 2, 0.074219 },
     304             : { 7, 1, 0, 7, 1, 0, 0.076667 },
     305             : { 13, 0, 1, 13, 0, 1, 0.076667 },
     306             : { 39, 0, 0, 13, 0, 2, 0.076667 },
     307             : { 44, 0, 0, 11, 4, 0, 0.076667 },
     308             : { 17, 1, 0, 17, 1, 0, 0.077188 },
     309             : { 22, 0, 0, 11, 2, 0, 0.077333 },
     310             : { 34, 1, 0, 17, 3, 0, 0.077969 },
     311             : { 30, 1, 0, 15, 3, 0, 0.080312 },
     312             : { 14, 0, 1, 14, 0, 1, 0.080556 },
     313             : { 28, 0, 0, 14, 4, 0, 0.080556 },
     314             : { 42, 0, 0, 14, 0, 2, 0.080556 },
     315             : { 9, 1, 0, 9, 1, 0, 0.080833 },
     316             : { 68, 0, 1, 17, 4, 1, 0.081380 },
     317             : { 204, 0, 0, 17, 4, 2, 0.081380 },
     318             : { 52, 0, 0, 13, 4, 0, 0.082292 },
     319             : { 10, 1, 1, 10, 3, 1, 0.083125 },
     320             : { 20, 0, 1, 10, 4, 1, 0.083333 },
     321             : { 60, 0, 0, 10, 4, 2, 0.083333 },
     322             : { 17, 0, 1, 17, 0, 1, 0.084687 },
     323             : { 51, 0, 0, 17, 0, 2, 0.084687 },
     324             : { 19, 1, 1, 19, 1, 1, 0.084722 },
     325             : { 57, 1, 0, 19, 1, 2, 0.084722 },
     326             : { 11, 0, 0, 11, 0, 0, 0.087000 },
     327             : { 68, 0, 0, 17, 4, 0, 0.088281 },
     328             : { 38, 1, 1, 19, 3, 1, 0.090139 },
     329             : { 114, 1, 0, 19, 3, 2, 0.090139 },
     330             : { 36, 0, 0, 18, 4, 0, 0.090972 },
     331             : { 19, 1, 0, 19, 1, 0, 0.091389 },
     332             : { 26, 0, 0, 13, 2, 0, 0.091667 },
     333             : { 13, 0, 0, 13, 0, 0, 0.092500 },
     334             : { 38, 1, 0, 19, 3, 0, 0.092778 },
     335             : { 14, 1, 0, 7, 3, 0, 0.092917 },
     336             : { 18, 1, 0, 9, 3, 0, 0.092917 },
     337             : { 20, 0, 0, 10, 4, 0, 0.095833 },
     338             : { 76, 0, 1, 19, 4, 1, 0.096412 },
     339             : { 228, 0, 0, 19, 4, 2, 0.096412 },
     340             : { 17, 0, 0, 17, 0, 0, 0.096875 },
     341             : { 19, 0, 1, 19, 0, 1, 0.098056 },
     342             : { 57, 0, 0, 19, 0, 2, 0.098056 },
     343             : { 23, 1, 1, 23, 1, 1, 0.100682 },
     344             : { 69, 1, 0, 23, 1, 2, 0.100682 },
     345             : { 7, 0, 1, 7, 0, 1, 0.100833 },
     346             : { 21, 0, 0, 7, 0, 2, 0.100833 },
     347             : { 30, 0, 0, 15, 2, 0, 0.100833 },
     348             : { 76, 0, 0, 19, 4, 0, 0.102083 },
     349             : { 14, 0, 0, 14, 0, 0, 0.102222 },
     350             : { 5, 1, 1, 5, 1, 1, 0.103125 },
     351             : { 46, 1, 1, 23, 3, 1, 0.104034 },
     352             : { 138, 1, 0, 23, 3, 2, 0.104034 },
     353             : { 23, 1, 0, 23, 1, 0, 0.104545 },
     354             : { 7, 0, 0, 7, 0, 0, 0.105000 },
     355             : { 10, 0, 1, 10, 0, 1, 0.105000 },
     356             : { 16, 0, 1, 16, 0, 1, 0.105417 },
     357             : { 48, 0, 0, 16, 0, 2, 0.105417 },
     358             : { 46, 1, 0, 23, 3, 0, 0.106705 },
     359             : { 18, 0, 0, 9, 2, 0, 0.107778 },
     360             : { 92, 0, 1, 23, 4, 1, 0.108239 },
     361             : { 276, 0, 0, 23, 4, 2, 0.108239 },
     362             : { 19, 0, 0, 19, 0, 0, 0.110000 },
     363             : { 23, 0, 1, 23, 0, 1, 0.111136 },
     364             : { 69, 0, 0, 23, 0, 2, 0.111136 },
     365             : { 9, 0, 0, 9, 0, 0, 0.113333 },
     366             : { 10, 0, 0, 10, 0, 0, 0.113333 },
     367             : { 92, 0, 0, 23, 4, 0, 0.113826 },
     368             : { 5, 1, 0, 5, 1, 0, 0.115000 },
     369             : { 15, 0, 0, 15, 0, 0, 0.115000 },
     370             : { 23, 0, 0, 23, 0, 0, 0.120909 },
     371             : { 8, 0, 1, 8, 0, 1, 0.126042 },
     372             : { 24, 0, 0, 8, 0, 2, 0.126042 },
     373             : { 16, 0, 0, 16, 0, 0, 0.127188 },
     374             : { 8, 0, 0, 8, 0, 0, 0.141667 },
     375             : { 25, 0, 1, 25, 0, 1, 0.144000 },
     376             : { 5, 0, 1, 5, 0, 1, 0.151250 },
     377             : { 12, 0, 0, 12, 0, 0, 0.152083 },
     378             : { 29, 1, 1, 29, 1, 1, 0.153929 },
     379             : { 87, 1, 0, 29, 1, 2, 0.153929 },
     380             : { 25, 0, 0, 25, 0, 0, 0.155000 },
     381             : { 58, 1, 1, 29, 3, 1, 0.155045 },
     382             : { 174, 1, 0, 29, 3, 2, 0.155045 },
     383             : { 29, 1, 0, 29, 1, 0, 0.156429 },
     384             : { 58, 1, 0, 29, 3, 0, 0.157857 },
     385             : { 116, 0, 1, 29, 4, 1, 0.158631 },
     386             : { 116, 0, 0, 29, 4, 0, 0.163542 },
     387             : { 29, 0, 1, 29, 0, 1, 0.164286 },
     388             : { 87, 0, 0, 29, 0, 2, 0.164286 },
     389             : { 29, 0, 0, 29, 0, 0, 0.169286 },
     390             : { 5, 0, 0, 5, 0, 0, 0.170000 },
     391             : { 31, 1, 1, 31, 1, 1, 0.187000 },
     392             : { 93, 1, 0, 31, 1, 2, 0.187000 },
     393             : { 62, 1, 1, 31, 3, 1, 0.188500 },
     394             : { 186, 1, 0, 31, 3, 2, 0.188500 },
     395             : { 31, 1, 0, 31, 1, 0, 0.191333 },
     396             : { 62, 1, 0, 31, 3, 0, 0.192083 },
     397             : { 124, 0, 1, 31, 4, 1, 0.193472 },
     398             : { 31, 0, 1, 31, 0, 1, 0.196167 },
     399             : { 93, 0, 0, 31, 0, 2, 0.196167 },
     400             : { 124, 0, 0, 31, 4, 0, 0.197083 },
     401             : { 2, 1, 1, 2, 3, 1, 0.200000 },
     402             : { 6, 1, 0, 2, 3, 2, 0.200000 },
     403             : { 31, 0, 0, 31, 0, 0, 0.205000 },
     404             : { 6, 0, 0, 6, 0, 0, 0.226667 },
     405             : { 3, 1, 0, 3, 1, 0, 0.230000 },
     406             : { 4, 0, 1, 4, 0, 1, 0.241667 },
     407             : { 4, 0, 0, 4, 0, 0, 0.283333 },
     408             : { 3, 0, 0, 3, 0, 0, 0.340000 },
     409             : { 1, 1, 1, 1, 1, 1, 0.362500 },
     410             : { 2, 0, 1, 2, 0, 1, 0.370000 },
     411             : { 1, 1, 0, 1, 1, 0, 0.385000 },
     412             : { 2, 0, 0, 2, 0, 0, 0.453333 },
     413             : };
     414             : 
     415             : static struct torctab_rec torctab3[] = {
     416             : { 66, 1, 0, 11, 3, 2, 0.040406 },
     417             : { 33, 1, 0, 11, 1, 2, 0.043688 },
     418             : { 78, 1, 0, 13, 3, 2, 0.045391 },
     419             : { 132, 1, 0, 11, 130, 2, 0.046938 },
     420             : { 39, 1, 0, 13, 1, 2, 0.047656 },
     421             : { 102, 1, 0, 17, 3, 2, 0.049922 },
     422             : { 42, 1, 0, 14, 3, 2, 0.050000 },
     423             : { 51, 1, 0, 17, 1, 2, 0.051680 },
     424             : { 132, 0, 0, 11, 4, 2, 0.052188 },
     425             : { 156, 1, 0, 13, 130, 2, 0.053958 },
     426             : { 156, 0, 0, 13, 4, 2, 0.054818 },
     427             : { 84, 1, 0, 14, 130, 2, 0.055000 },
     428             : { 15, 1, 0, 15, 1, 0, 0.056719 },
     429             : { 204, 0, 0, 17, 4, 2, 0.057227 },
     430             : { 114, 1, 0, 19, 3, 2, 0.057500 },
     431             : { 11, 1, 0, 11, 1, 0, 0.058000 },
     432             : { 66, 0, 0, 11, 2, 2, 0.058000 },
     433             : { 57, 1, 0, 19, 1, 2, 0.059062 },
     434             : { 30, 1, 0, 15, 3, 0, 0.059063 },
     435             : { 84, 0, 0, 14, 4, 2, 0.060677 },
     436             : { 22, 1, 0, 11, 3, 0, 0.061750 },
     437             : { 78, 0, 0, 13, 2, 2, 0.063542 },
     438             : { 228, 0, 0, 19, 4, 2, 0.063889 },
     439             : { 21, 1, 0, 7, 1, 2, 0.065000 },
     440             : { 138, 1, 0, 23, 3, 2, 0.065028 },
     441             : { 69, 1, 0, 23, 1, 2, 0.066903 },
     442             : { 13, 1, 0, 13, 1, 0, 0.068750 },
     443             : { 102, 0, 0, 17, 2, 2, 0.068906 },
     444             : { 26, 1, 0, 13, 3, 0, 0.069583 },
     445             : { 51, 0, 0, 17, 0, 2, 0.070312 },
     446             : { 60, 1, 0, 15, 130, 0, 0.071094 },
     447             : { 276, 0, 0, 23, 4, 2, 0.071236 },
     448             : { 39, 0, 0, 13, 0, 2, 0.071250 },
     449             : { 33, 0, 0, 11, 0, 2, 0.072750 },
     450             : { 44, 1, 0, 11, 130, 0, 0.073500 },
     451             : { 60, 0, 0, 15, 4, 0, 0.073828 },
     452             : { 9, 1, 0, 9, 1, 0, 0.074097 },
     453             : { 30, 0, 0, 15, 2, 0, 0.075625 },
     454             : { 57, 0, 0, 19, 0, 2, 0.075625 },
     455             : { 7, 1, 0, 7, 1, 0, 0.076667 },
     456             : { 44, 0, 0, 11, 4, 0, 0.076667 },
     457             : { 22, 0, 0, 11, 2, 0, 0.077333 },
     458             : { 17, 1, 0, 17, 1, 0, 0.078750 },
     459             : { 34, 1, 0, 17, 3, 0, 0.078750 },
     460             : { 69, 0, 0, 23, 0, 2, 0.079943 },
     461             : { 28, 0, 0, 14, 4, 0, 0.080556 },
     462             : { 42, 0, 0, 14, 0, 2, 0.080833 },
     463             : { 52, 0, 0, 13, 4, 0, 0.082292 },
     464             : { 14, 1, 1, 14, 3, 1, 0.083333 },
     465             : { 36, 0, 0, 18, 4, 0, 0.083391 },
     466             : { 18, 1, 0, 9, 3, 0, 0.085174 },
     467             : { 68, 0, 0, 17, 4, 0, 0.089583 },
     468             : { 15, 0, 0, 15, 0, 0, 0.090938 },
     469             : { 19, 1, 0, 19, 1, 0, 0.091389 },
     470             : { 26, 0, 0, 13, 2, 0, 0.091667 },
     471             : { 11, 0, 0, 11, 0, 0, 0.092000 },
     472             : { 13, 0, 0, 13, 0, 0, 0.092500 },
     473             : { 38, 1, 0, 19, 3, 0, 0.092778 },
     474             : { 14, 1, 0, 7, 3, 0, 0.092917 },
     475             : { 18, 0, 0, 9, 2, 0, 0.093704 },
     476             : { 174, 1, 0, 29, 3, 2, 0.095826 },
     477             : { 20, 0, 0, 10, 4, 0, 0.095833 },
     478             : { 96, 1, 0, 16, 133, 2, 0.096562 },
     479             : { 21, 0, 0, 21, 0, 0, 0.096875 },
     480             : { 87, 1, 0, 29, 1, 2, 0.096964 },
     481             : { 17, 0, 0, 17, 0, 0, 0.100000 },
     482             : { 348, 0, 0, 29, 4, 2, 0.100558 },
     483             : { 76, 0, 0, 19, 4, 0, 0.100926 },
     484             : { 14, 0, 0, 14, 0, 0, 0.102222 },
     485             : { 9, 0, 0, 9, 0, 0, 0.103889 },
     486             : { 46, 1, 0, 23, 3, 0, 0.105114 },
     487             : { 23, 1, 0, 23, 1, 0, 0.105682 },
     488             : { 48, 0, 0, 16, 0, 2, 0.106406 },
     489             : { 87, 0, 0, 29, 0, 2, 0.107545 },
     490             : { 19, 0, 0, 19, 0, 0, 0.107778 },
     491             : { 7, 0, 0, 7, 0, 0, 0.113333 },
     492             : { 10, 0, 0, 10, 0, 0, 0.113333 },
     493             : { 92, 0, 0, 23, 4, 0, 0.113636 },
     494             : { 12, 0, 0, 12, 0, 0, 0.114062 },
     495             : { 5, 1, 0, 5, 1, 0, 0.115000 },
     496             : { 186, 1, 0, 31, 3, 2, 0.115344 },
     497             : { 93, 1, 0, 31, 1, 2, 0.118125 },
     498             : { 23, 0, 0, 23, 0, 0, 0.120909 },
     499             : { 93, 0, 0, 31, 0, 2, 0.128250 },
     500             : { 16, 0, 0, 16, 0, 0, 0.138750 },
     501             : { 25, 0, 0, 25, 0, 0, 0.155000 },
     502             : { 58, 1, 0, 29, 3, 0, 0.155714 },
     503             : { 29, 1, 0, 29, 1, 0, 0.158214 },
     504             : { 3, 1, 0, 3, 1, 0, 0.163125 },
     505             : { 116, 0, 0, 29, 4, 0, 0.163690 },
     506             : { 5, 0, 0, 5, 0, 0, 0.170000 },
     507             : { 6, 0, 0, 6, 0, 0, 0.170000 },
     508             : { 8, 0, 0, 8, 0, 0, 0.170000 },
     509             : { 29, 0, 0, 29, 0, 0, 0.172857 },
     510             : { 31, 1, 0, 31, 1, 0, 0.191333 },
     511             : { 62, 1, 0, 31, 3, 0, 0.191750 },
     512             : { 124, 0, 0, 31, 4, 0, 0.197917 },
     513             : { 31, 0, 0, 31, 0, 0, 0.201667 },
     514             : { 3, 0, 0, 3, 0, 0, 0.236250 },
     515             : { 4, 0, 0, 4, 0, 0, 0.262500 },
     516             : { 2, 1, 1, 2, 3, 1, 0.317187 },
     517             : { 1, 1, 0, 1, 1, 0, 0.410000 },
     518             : { 2, 0, 0, 2, 0, 0, 0.453333 },
     519             : };
     520             : 
     521             : static struct torctab_rec torctab4[] = {
     522             : { 66, 1, 0, 11, 3, 2, 0.041344 },
     523             : { 33, 1, 0, 11, 1, 2, 0.042750 },
     524             : { 78, 1, 0, 13, 3, 2, 0.045781 },
     525             : { 39, 1, 0, 13, 1, 2, 0.046875 },
     526             : { 264, 1, 0, 11, 131, 2, 0.049043 },
     527             : { 42, 1, 0, 14, 3, 2, 0.050000 },
     528             : { 102, 1, 0, 17, 3, 2, 0.050508 },
     529             : { 51, 1, 0, 17, 1, 2, 0.051094 },
     530             : { 528, 1, 0, 11, 132, 2, 0.052891 },
     531             : { 132, 0, 0, 11, 4, 2, 0.052969 },
     532             : { 168, 1, 0, 14, 131, 2, 0.053965 },
     533             : { 156, 0, 0, 13, 4, 2, 0.054948 },
     534             : { 336, 1, 0, 14, 132, 2, 0.056120 },
     535             : { 15, 1, 0, 15, 1, 0, 0.056719 },
     536             : { 66, 0, 0, 11, 2, 2, 0.057000 },
     537             : { 114, 1, 0, 19, 3, 2, 0.057812 },
     538             : { 11, 1, 0, 11, 1, 0, 0.058000 },
     539             : { 204, 0, 0, 17, 4, 2, 0.058203 },
     540             : { 57, 1, 0, 19, 1, 2, 0.058542 },
     541             : { 84, 0, 0, 14, 4, 2, 0.059375 },
     542             : { 30, 1, 0, 15, 3, 0, 0.061406 },
     543             : { 22, 1, 0, 11, 3, 0, 0.063000 },
     544             : { 78, 0, 0, 13, 2, 2, 0.063542 },
     545             : { 138, 1, 0, 23, 3, 2, 0.064815 },
     546             : { 21, 1, 0, 7, 1, 2, 0.065000 },
     547             : { 228, 0, 0, 19, 4, 2, 0.065104 },
     548             : { 69, 1, 0, 23, 1, 2, 0.066477 },
     549             : { 13, 1, 0, 13, 1, 0, 0.068750 },
     550             : { 102, 0, 0, 17, 2, 2, 0.068906 },
     551             : { 51, 0, 0, 17, 0, 2, 0.069141 },
     552             : { 26, 1, 0, 13, 3, 0, 0.070625 },
     553             : { 276, 0, 0, 23, 4, 2, 0.071236 },
     554             : { 39, 0, 0, 13, 0, 2, 0.071250 },
     555             : { 33, 0, 0, 11, 0, 2, 0.072750 },
     556             : { 60, 0, 0, 15, 4, 0, 0.073828 },
     557             : { 9, 1, 0, 9, 1, 0, 0.074097 },
     558             : { 57, 0, 0, 19, 0, 2, 0.074583 },
     559             : { 30, 0, 0, 15, 2, 0, 0.075625 },
     560             : { 44, 0, 0, 11, 4, 0, 0.076667 },
     561             : { 17, 1, 0, 17, 1, 0, 0.077188 },
     562             : { 22, 0, 0, 11, 2, 0, 0.077333 },
     563             : { 69, 0, 0, 23, 0, 2, 0.080114 },
     564             : { 36, 0, 0, 18, 4, 0, 0.080208 },
     565             : { 34, 1, 0, 17, 3, 0, 0.080312 },
     566             : { 28, 0, 0, 14, 4, 0, 0.080556 },
     567             : { 7, 1, 0, 7, 1, 0, 0.080833 },
     568             : { 52, 0, 0, 13, 4, 0, 0.082292 },
     569             : { 42, 0, 0, 14, 0, 2, 0.082500 },
     570             : { 14, 1, 1, 14, 3, 1, 0.083333 },
     571             : { 15, 0, 0, 15, 0, 0, 0.086250 },
     572             : { 18, 1, 0, 9, 3, 0, 0.087083 },
     573             : { 26, 0, 0, 13, 2, 0, 0.088889 },
     574             : { 68, 0, 0, 17, 4, 0, 0.089583 },
     575             : { 48, 1, 0, 16, 132, 2, 0.089844 },
     576             : { 19, 1, 0, 19, 1, 0, 0.091389 },
     577             : { 11, 0, 0, 11, 0, 0, 0.092000 },
     578             : { 38, 1, 0, 19, 3, 0, 0.092917 },
     579             : { 18, 0, 0, 9, 2, 0, 0.093704 },
     580             : { 14, 1, 0, 7, 3, 0, 0.095000 },
     581             : { 96, 1, 0, 16, 133, 2, 0.095391 },
     582             : { 20, 0, 0, 10, 4, 0, 0.095833 },
     583             : { 174, 1, 0, 29, 3, 2, 0.095893 },
     584             : { 13, 0, 0, 13, 0, 0, 0.096667 },
     585             : { 17, 0, 0, 17, 0, 0, 0.096875 },
     586             : { 21, 0, 0, 21, 0, 0, 0.096875 },
     587             : { 87, 1, 0, 29, 1, 2, 0.097366 },
     588             : { 48, 0, 0, 16, 0, 2, 0.097969 },
     589             : { 24, 1, 0, 12, 131, 0, 0.098789 },
     590             : { 76, 0, 0, 19, 4, 0, 0.100926 },
     591             : { 348, 0, 0, 29, 4, 2, 0.101116 },
     592             : { 14, 0, 0, 14, 0, 0, 0.102222 },
     593             : { 9, 0, 0, 9, 0, 0, 0.103889 },
     594             : { 23, 1, 0, 23, 1, 0, 0.104545 },
     595             : { 46, 1, 0, 23, 3, 0, 0.105682 },
     596             : { 12, 0, 0, 12, 0, 0, 0.106250 },
     597             : { 87, 0, 0, 29, 0, 2, 0.108348 },
     598             : { 19, 0, 0, 19, 0, 0, 0.110000 },
     599             : { 7, 0, 0, 7, 0, 0, 0.113333 },
     600             : { 10, 0, 0, 10, 0, 0, 0.113333 },
     601             : { 92, 0, 0, 23, 4, 0, 0.113826 },
     602             : { 186, 1, 0, 31, 3, 2, 0.116094 },
     603             : { 93, 1, 0, 31, 1, 2, 0.116813 },
     604             : { 23, 0, 0, 23, 0, 0, 0.120909 },
     605             : { 5, 1, 0, 5, 1, 0, 0.121250 },
     606             : { 93, 0, 0, 31, 0, 2, 0.127625 },
     607             : { 16, 0, 0, 16, 0, 0, 0.132917 },
     608             : { 8, 0, 0, 8, 0, 0, 0.141667 },
     609             : { 25, 0, 0, 25, 0, 0, 0.152500 },
     610             : { 58, 1, 0, 29, 3, 0, 0.157946 },
     611             : { 29, 1, 0, 29, 1, 0, 0.158393 },
     612             : { 116, 0, 0, 29, 4, 0, 0.162946 },
     613             : { 3, 1, 0, 3, 1, 0, 0.163125 },
     614             : { 29, 0, 0, 29, 0, 0, 0.169286 },
     615             : { 5, 0, 0, 5, 0, 0, 0.170000 },
     616             : { 6, 0, 0, 6, 0, 0, 0.170000 },
     617             : { 31, 1, 0, 31, 1, 0, 0.191333 },
     618             : { 62, 1, 0, 31, 3, 0, 0.192083 },
     619             : { 124, 0, 0, 31, 4, 0, 0.196389 },
     620             : { 31, 0, 0, 31, 0, 0, 0.205000 },
     621             : { 3, 0, 0, 3, 0, 0, 0.255000 },
     622             : { 4, 0, 0, 4, 0, 0, 0.262500 },
     623             : { 2, 1, 1, 2, 3, 1, 0.325000 },
     624             : { 1, 1, 0, 1, 1, 0, 0.385000 },
     625             : { 2, 0, 0, 2, 0, 0, 0.420000 },
     626             : };
     627             : 
     628             : #define TWIST_DOUBLE_RATIO              (9.0/16.0)
     629             : 
     630             : static long
     631      358062 : torsion_constraint(struct torctab_rec *torctab, long ltorc, double tormod[], long n, long m)
     632             : {
     633      358062 :   long i, b = -1;
     634      358062 :   double rb = -1.;
     635    43328493 :   for (i = 0 ; i < ltorc ; i++)
     636             :   {
     637    42970431 :     struct torctab_rec *ti = torctab + i;
     638    42970431 :     if ( ! (n%ti->m) && ( !ti->fix2 || (n%(2*ti->m)) ) && ( ! ti->fix3 || (n%(3*ti->m)) ) )
     639     3967587 :       if ( n == m || ( ! (m%ti->m) && ( !ti->fix2 || (m%(2*ti->m)) ) && ( ! ti->fix3 || (m%(3*ti->m)) ) ) )
     640             :       {
     641     3190920 :         double ri = ti->rating*tormod[ti->N];
     642     3190920 :         if ( b < 0 || ri < rb ) {  b = i; rb = ri; }
     643             :       }
     644             :   }
     645      358062 :   if (b < 0) pari_err_BUG("find_rating");
     646      358062 :   return b;
     647             : }
     648             : 
     649             : static void
     650      119354 : best_torsion_constraint(ulong p, long t, int *ptwist, ulong *ptor, int *ps2, int *pt3)
     651             : {
     652             :   struct torctab_rec *torctab;
     653             :   double tormod[32];
     654             :   long ltorc;
     655             :   long n1, n2;
     656             :   long b, b1, b2, b12;
     657             :   long i;
     658             : 
     659      119354 :   if ( (p%3)==2 ) {
     660       56409 :     if ( (p&3)==3 ) {
     661       27492 :       torctab = torctab1;
     662       27492 :       ltorc = sizeof(torctab1)/sizeof(*torctab1);
     663             :     } else {
     664       28917 :       torctab = torctab2;
     665       28917 :       ltorc = sizeof(torctab2)/sizeof(*torctab2);
     666             :     }
     667             :   } else {
     668       62945 :     if ( (p&3)==3 ) {
     669       34737 :       torctab = torctab3;
     670       34737 :       ltorc = sizeof(torctab3)/sizeof(*torctab3);
     671             :     } else {
     672       28208 :       torctab = torctab4;
     673       28208 :       ltorc = sizeof(torctab4)/sizeof(*torctab4);
     674             :     }
     675             :   }
     676     3938682 :   for ( i = 0 ; i < 32 ; i++ ) tormod[i] = 1.0;
     677      119354 :   if ( (p%5)==1 ) tormod[5] = tormod[10] = tormod[15] = 6.0/5.0;
     678      119354 :   if ( (p%7)==1 ) tormod[7] = tormod[14] = 8.0/7.0;
     679      119354 :   if ( (p%11)== 1 ) tormod[11] = 12.0/11.0;
     680      119354 :   if ( (p%13)==1 ) tormod[13] = 14.0/13.0;
     681      119354 :   if ( (p%17)==1 ) tormod[17] = 18.0/17.0;
     682      119354 :   if ( (p%19)==1 ) tormod[19] = 20.0/19.0;
     683      119354 :   if ( (p%23)==1 ) tormod[23] = 24.0/23.0;
     684      119354 :   if ( (p%29)==1 ) tormod[29] = 30.0/29.0;
     685      119354 :   if ( (p%31)==1 ) tormod[31] = 32.0/31.0;
     686             : 
     687      119354 :   n1 = p+1-t;
     688      119354 :   n2 = p+1+t;
     689      119354 :   b12 = -1;
     690      119354 :   b1  = torsion_constraint(torctab, ltorc, tormod, n1, n1);
     691      119354 :   b2  = torsion_constraint(torctab, ltorc, tormod, n2, n2);
     692      119354 :   b12 = torsion_constraint(torctab, ltorc, tormod, n1, n2);
     693      119354 :   if ( b1 > b2 ) {
     694       54504 :     if ( torctab[b2].rating / TWIST_DOUBLE_RATIO > torctab[b12].rating )
     695       14541 :       *ptwist = 3;
     696             :     else
     697       39963 :       *ptwist = 2;
     698             :   } else
     699       64850 :     if ( torctab[b1].rating / TWIST_DOUBLE_RATIO > torctab[b12].rating )
     700       21173 :       *ptwist = 3;
     701             :     else
     702       43677 :       *ptwist = 1;
     703      119354 :   b = *ptwist ==1 ? b1: *ptwist ==2 ? b2: b12;
     704      119354 :   *ptor = torctab[b].N; *ps2 = torctab[b].s2_flag; *pt3 = torctab[b].t3_flag;
     705      119354 : }
     706             : 
     707             : /* This is Sutherland 2009 Algorithm 1.1 */
     708             : static long
     709      119361 : find_j_inv_with_given_trace(
     710             :   ulong *j_t, norm_eqn_t ne, long rho_inv, long max_curves)
     711             : {
     712      119361 :   pari_sp ltop = avma, av;
     713      119361 :   long curves_tested = 0, batch_size;
     714             :   long N0, N1, hasse[2];
     715             :   GEN n0, n1;
     716      119361 :   long i, found = 0;
     717      119361 :   ulong p = ne->p, pi = ne->pi;
     718      119361 :   long t = ne->t;
     719      119361 :   ulong p1 = p + 1, a4, a6, m, N;
     720             :   GEN A4, A6, tx, ty;
     721             :   int s2_flag, t3_flag, twist;
     722             : 
     723      119361 :   if (p == 2 || p == 3) {
     724           7 :     if (t == 0) pari_err_BUG("find_j_inv_with_given_trace");
     725           7 :     *j_t = t; return 1;
     726             :   }
     727             : 
     728      119354 :   N0 = (long)p1 - t; n0 = factoru(N0);
     729      119354 :   N1 = (long)p1 + t; n1 = factoru(N1);
     730             : 
     731      119354 :   best_torsion_constraint(p, t, &twist, &m, &s2_flag, &t3_flag);
     732      119354 :   N = p1 - (twist<3 ? (twist==1 ? t: -t): 0);
     733             : 
     734             :   /* Select batch size so that we have roughly a 50% chance of finding
     735             :    * a good curve in a batch. */
     736      119354 :   batch_size = 1.0 + rho_inv / (2.0 * m);
     737      119354 :   A4 = cgetg(batch_size + 1, t_VECSMALL);
     738      119354 :   A6 = cgetg(batch_size + 1, t_VECSMALL);
     739      119354 :   tx = cgetg(batch_size + 1, t_VECSMALL);
     740      119354 :   ty = cgetg(batch_size + 1, t_VECSMALL);
     741             : 
     742      119354 :   dbg_printf(2)("  Selected torsion constraint m = %lu and batch "
     743             :                 "size = %ld\n", m, batch_size);
     744      119354 :   hasse_bounds(&hasse[0], &hasse[1], p);
     745      119354 :   av = avma;
     746      626204 :   while (!found && (max_curves <= 0 || curves_tested < max_curves))
     747             :   {
     748             :     GEN Pp1, Pt;
     749     1013700 :     random_curves_with_m_torsion((ulong *)(A4 + 1), (ulong *)(A6 + 1),
     750      506850 :                                  (ulong *)(tx + 1), (ulong *)(ty + 1),
     751             :                                  batch_size, m, p);
     752      506850 :     Pp1 = random_FleV(A4, A6, p, pi);
     753      506850 :     Pt = gcopy(Pp1);
     754      506850 :     FleV_mulu_pre_inplace(Pp1, N, A4, p, pi);
     755      506850 :     if (twist >= 3) FleV_mulu_pre_inplace(Pt, t, A4,  p, pi);
     756     2593154 :     for (i = 1; i <= batch_size; ++i) {
     757     2205658 :       ++curves_tested;
     758     2205658 :       a4 = A4[i];
     759     2205658 :       a6 = A6[i]; if (a4 == 0 || a6 == 0) continue;
     760             : 
     761     2203977 :       if (( (twist >= 3 && mael(Pp1,i,1) == mael(Pt,i,1))
     762     2164808 :          || (twist < 3 && umael(Pp1,i,1) == p))
     763      140885 :           && test_curve_order(ne, a4, a6, N0, N1, n0, n1, hasse)) {
     764      119354 :         *j_t = Fl_ellj_pre(a4, a6, p, pi);
     765      119354 :         found = 1; break;
     766             :       }
     767             :     }
     768      506850 :     set_avma(av);
     769             :   }
     770      119354 :   return gc_long(ltop, curves_tested);
     771             : }
     772             : 
     773             : /*
     774             :  * SECTION: Functions for dealing with polycyclic presentations.
     775             :  */
     776             : 
     777             : static GEN
     778        4771 : next_generator(GEN DD, long D, ulong u, long filter, GEN *genred, long *P)
     779             : {
     780        4771 :   pari_sp av = avma;
     781        4771 :   ulong p = (ulong)*P;
     782             :   while (1)
     783             :   {
     784        7923 :     p = unextprime(p + 1);
     785        7923 :     if (p > LONG_MAX) pari_err_BUG("next_generator");
     786        7923 :     if (kross(D, (long)p) != -1 && u % p != 0 && filter % p != 0)
     787             :     {
     788        4771 :       GEN gen = primeform_u(DD, p);
     789             :       /* If gen is in the principal class, skip it */
     790        4771 :       *genred = redimag(gen);
     791        4771 :       if (!equali1(gel(*genred,1))) { *P = (long)p; return gen; }
     792           0 :       set_avma(av);
     793             :     }
     794             :   }
     795             : }
     796             : 
     797             : INLINE long *
     798        5520 : evec_ri_mutate(long r[], long i)
     799        5520 : { return r + (i * (i - 1) >> 1); }
     800             : 
     801             : INLINE const long *
     802       13269 : evec_ri(const long r[], long i)
     803       13269 : { return r + (i * (i - 1) >> 1); }
     804             : 
     805             : /* Reduces evec e so that e[i] < n[i] (assume e[i] >= 0) using pcp(n,r,k).
     806             :  * No check for overflow, this could be an issue for large groups */
     807             : INLINE void
     808       20182 : evec_reduce(long e[], const long n[], const long r[], long k)
     809             : {
     810             :   long i, j, q;
     811             :   const long *ri;
     812       20182 :   if (!k) return;
     813       48635 :   for (i = k - 1; i > 0; i--) {
     814       28453 :     if (e[i] >= n[i]) {
     815        8541 :       q = e[i] / n[i];
     816        8541 :       ri = evec_ri(r, i);
     817       21525 :       for (j = 0; j < i; j++) e[j] += q * ri[j];
     818        8541 :       e[i] -= q * n[i];
     819             :     }
     820             :   }
     821       20182 :   e[0] %= n[0];
     822             : }
     823             : 
     824             : /* Computes e3 = log(a^e1*a^e2) in terms of the given polycyclic
     825             :  * presentation (here a denotes the implicit vector of generators) */
     826             : INLINE void
     827         518 : evec_compose(long e3[],
     828             :   const long e1[], const long e2[], const long n[],const long r[], long k)
     829             : {
     830             :     long i;
     831        2128 :     for (i = 0; i < k; i++) e3[i] = e1[i] + e2[i];
     832         518 :     evec_reduce(e3, n, r, k);
     833         518 : }
     834             : 
     835             : /* Converts an evec to an integer index corresponding to the
     836             :  * multi-radix representation of the evec with moduli corresponding to
     837             :  * the subgroup orders m[i] */
     838             : INLINE long
     839       10542 : evec_to_index(const long e[], const long m[], long k)
     840             : {
     841       10542 :   long i, index = e[0];
     842       25221 :   for (i = 1; i < k; i++) index += e[i] * m[i - 1];
     843       10542 :   return index;
     844             : }
     845             : 
     846             : INLINE void
     847       22040 : evec_copy(long f[], const long e[], long k)
     848             : {
     849             :   long i;
     850       47313 :   for (i = 0; i < k; ++i) f[i] = e[i];
     851       22040 : }
     852             : 
     853             : INLINE void
     854        4263 : evec_clear(long e[], long k)
     855             : {
     856             :   long i;
     857       11029 :   for (i = 0; i < k; ++i) e[i] = 0;
     858        4263 : }
     859             : 
     860             : /* e1 and e2 may overlap */
     861             : /* Note that this function is not very efficient because it does not know the
     862             :  * orders of the elements in the presentation, only the relative orders */
     863             : INLINE void
     864         175 : evec_inverse(long e2[], const long e1[], const long n[], const long r[], long k)
     865             : {
     866         175 :   pari_sp av = avma;
     867             :   long i, *e3, *e4;
     868             : 
     869         175 :   e3 = new_chunk(k);
     870         175 :   e4 = new_chunk(k);
     871         175 :   evec_clear(e4, k);
     872         175 :   evec_copy(e3, e1, k);
     873             :   /* We have e1 + e4 = e3 which we maintain throughout while making e1
     874             :    * the zero vector */
     875         812 :   for (i = k - 1; i >= 0; i--) if (e3[i])
     876             :   {
     877          35 :     e4[i] += n[i] - e3[i];
     878          35 :     evec_reduce(e4, n, r, k);
     879          35 :     e3[i] = n[i];
     880          35 :     evec_reduce(e3, n, r, k);
     881             :   }
     882         175 :   evec_copy(e2, e4, k);
     883         175 :   set_avma(av);
     884         175 : }
     885             : 
     886             : /* e1 and e2 may overlap */
     887             : /* This is a faster way to compute inverses, if the presentation
     888             :  * element orders are known (these are specified in the array o, the
     889             :  * array n holds the relative orders) */
     890             : INLINE void
     891         735 : evec_inverse_o(
     892             :   long e2[],
     893             :   const long e1[], const long n[], const long o[], const long r[], long k)
     894             : {
     895             :   long j;
     896        3311 :   for (j = 0; j < k; j++) e2[j] = (e1[j] ? o[j] - e1[j] : 0);
     897         735 :   evec_reduce(e2, n, r, k);
     898         735 : }
     899             : 
     900             : /* Computes the order of the group element a^e using the pcp (n,r,k) */
     901             : INLINE long
     902        4659 : evec_order(const long e[], const long n[], const long r[], long k)
     903             : {
     904        4659 :   pari_sp av = avma;
     905        4659 :   long *f = new_chunk(k);
     906             :   long i, j, o, m;
     907             : 
     908        4659 :   evec_copy(f, e, k);
     909       11958 :   for (o = 1, i = k - 1; i >= 0; i--) if (f[i])
     910             :   {
     911        5461 :     m = n[i] / ugcd(f[i], n[i]);
     912       14938 :     for (j = 0; j < k; j++) f[j] *= m;
     913        5461 :     evec_reduce(f, n, r, k);
     914        5461 :     o *= m;
     915             :   }
     916        4659 :   return gc_long(av,o);
     917             : }
     918             : 
     919             : /* Computes orders o[] for each generator using relative orders n[]
     920             :  * and power relations r[] */
     921             : INLINE void
     922        3668 : evec_orders(long o[], const long n[], const long r[], long k)
     923             : {
     924        3668 :   pari_sp av = avma;
     925        3668 :   long i, *e = new_chunk(k);
     926             : 
     927        3668 :   evec_clear(e, k);
     928        8327 :   for (i = 0; i < k; i++) {
     929        4659 :     e[i] = 1;
     930        4659 :     if (i) e[i - 1] = 0;
     931        4659 :     o[i] = evec_order(e, n, r, k);
     932             :   }
     933        3668 :   set_avma(av);
     934        3668 : }
     935             : 
     936             : INLINE int
     937         259 : evec_equal(const long e1[], const long e2[], long k)
     938             : {
     939             :   long j;
     940         371 :   for (j = 0; j < k; ++j)
     941         343 :     if (e1[j] != e2[j]) break;
     942         259 :   return j == k;
     943             : }
     944             : 
     945             : INLINE void
     946        1372 : index_to_evec(long e[], long index, const long m[], long k)
     947             : {
     948             :   long i;
     949        4130 :   for (i = k - 1; i > 0; --i) {
     950        2758 :     e[i] = index / m[i - 1];
     951        2758 :     index -= e[i] * m[i - 1];
     952             :   }
     953        1372 :   e[0] = index;
     954        1372 : }
     955             : 
     956             : INLINE void
     957        3668 : evec_n_to_m(long m[], const long n[], long k)
     958             : {
     959             :   long i;
     960        3668 :   m[0] = n[0];
     961        4659 :   for (i = 1; i < k; ++i) m[i] = m[i - 1] * n[i];
     962        3668 : }
     963             : 
     964             : /* Based on logfac() in Sutherland's classpoly package.
     965             :  * Ramanujan approximation to log(n!), accurate to O(1/n^3) */
     966             : INLINE double
     967           0 : logfac(long n)
     968             : {
     969           0 :   const double HALFLOGPI = 0.57236494292470008707171367567653;
     970           0 :   return n * log((double) n) - (double) n +
     971           0 :     log((double) n * (1.0 + 4.0 * n * (1.0 + 2.0 * n))) / 6.0 +
     972             :     HALFLOGPI;
     973             : }
     974             : 
     975             : /* This is based on Sutherland 2009, Lemma 8 (p31). */
     976             : static double
     977        3844 : upper_bound_on_classpoly_coeffs(long D, long h, GEN qfinorms)
     978             : {
     979        3844 :   const double LOG2E = 1.44269504088896340735992468100189;
     980        3844 :   pari_sp ltop = avma;
     981        3844 :   GEN C = dbltor(2114.567);
     982             :   double Mk, m, logbinom;
     983        3844 :   GEN tmp = mulrr(mppi(LOWDEFAULTPREC), sqrtr(utor(-D, LOWDEFAULTPREC)));
     984             :   /* We treat this case separately since the table is not initialised when
     985             :    * h = 1. This is the same as in the for loop below but with ak = 1. */
     986        3844 :   double log2Mk = dbllog2r(mpadd(mpexp(tmp), C));
     987        3844 :   double res = log2Mk;
     988        3844 :   ulong maxak = 1;
     989        3844 :   double log2Mh = log2Mk;
     990             : 
     991        3844 :   pari_sp btop = avma;
     992             :   long k;
     993       67528 :   for (k = 2; k <= h; ++k) {
     994       63684 :     ulong ak = uel(qfinorms, k);
     995             :     /* exp(tmp/a[k]) can overflow for even moderate discriminants, so we need
     996             :      * to use t_REALs instead of doubles.  Sutherland has a (more complicated)
     997             :      * implementation in the classpoly package which should be consulted if
     998             :      * this ever turns out to be a bottleneck.
     999             :      *
    1000             :      * One idea to avoid t_REALs is the following: we have
    1001             :      * log(e^x + C) - x <= log(2) ~ 0.69 for x >= log(C) ~ 0.44 and
    1002             :      * the difference is basically zero for x slightly bigger than log(C).
    1003             :      * Hence for large discriminants, we have x = \pi\sqrt{-D}/ak >> log(C)
    1004             :      * and so we could approximate log(e^x + C) by x. */
    1005       63684 :     log2Mk = dbllog2r(mpadd(mpexp(divru(tmp, ak)), C));
    1006       63684 :     res += log2Mk;
    1007       63684 :     if (ak > maxak) { maxak = ak; log2Mh = log2Mk; }
    1008       63684 :     set_avma(btop);
    1009             :   }
    1010             : 
    1011        3844 :   Mk = pow(2.0, log2Mh);
    1012        3844 :   m = floor((h + 1)/(Mk + 1.0));
    1013             :   /* This line computes "log2(itos(binomialuu(h, m)))".  The smallest
    1014             :    * fundamental discriminant for which logbinom is not zero is
    1015             :    * -1579751. */
    1016           0 :   logbinom = (m > 0 && m < h)
    1017           0 :     ? LOG2E * (logfac(h) - logfac(m) - logfac(h - m))
    1018        3844 :     : 0;
    1019        3844 :   set_avma(ltop);
    1020        3844 :   return res + logbinom - m * log2Mh + 2.0;
    1021             : }
    1022             : 
    1023             : INLINE long
    1024        1001 : distinct_inverses(const long f[], const long ef[], const long ei[],
    1025             :   const long n[], const long o[], const long r[], long k, long L0, long i)
    1026             : {
    1027        1001 :   pari_sp av = avma;
    1028             :   long j, *e2, *e3;
    1029             : 
    1030        1001 :   if ( ! ef[i] || (L0 && ef[0])) return 0;
    1031         574 :   for (j = i + 1; j < k; ++j)
    1032         455 :     if (ef[j]) break;
    1033         490 :   if (j < k) return 0;
    1034             : 
    1035         119 :   e2 = new_chunk(k);
    1036         119 :   evec_copy(e2, ef, i);
    1037         119 :   e2[i] = o[i] - ef[i];
    1038         175 :   for (j = i + 1; j < k; ++j) e2[j] = 0;
    1039         119 :   evec_reduce(e2, n, r, k);
    1040             : 
    1041         119 :   if (evec_equal(ef, e2, k)) return gc_long(av,0);
    1042             : 
    1043         112 :   e3 = new_chunk(k);
    1044         112 :   evec_inverse_o(e3, ef, n, o, r, k);
    1045         112 :   if (evec_equal(e2, e3, k)) return gc_long(av,0);
    1046             : 
    1047          91 :   if (f) {
    1048          14 :     evec_compose(e3, f, ei, n, r, k);
    1049          14 :     if (evec_equal(e2, e3, k)) return gc_long(av,0);
    1050             : 
    1051          14 :     evec_inverse_o(e3, e3, n, o, r, k);
    1052          14 :     if (evec_equal(e2, e3, k)) return gc_long(av,0);
    1053             :   }
    1054          91 :   return gc_long(av,1);
    1055             : }
    1056             : 
    1057             : INLINE long
    1058        1239 : next_prime_evec(long *qq, long f[], const long m[], long k,
    1059             :   hashtable *tbl, long D, GEN DD, long u, long lvl, long ubound)
    1060             : {
    1061        1239 :   pari_sp av = avma;
    1062             :   hashentry *he;
    1063             :   GEN P;
    1064        1239 :   long idx, q = *qq;
    1065             : 
    1066        3164 :   do q = unextprime(q + 1);
    1067        3164 :   while (!(u % q) || kross(D, q) == -1 || !(lvl % q) || !(D % (q * q)));
    1068        1239 :   if (q > ubound) return 0;
    1069        1113 :   *qq = q;
    1070             : 
    1071             :   /* Get evec f corresponding to q */
    1072        1113 :   P = redimag(primeform_u(DD, q));
    1073        1113 :   he = hash_search(tbl, P);
    1074        1113 :   if (!he) pari_err_BUG("next_prime_evec");
    1075        1113 :   idx = itos((GEN) he->val);
    1076        1113 :   index_to_evec(f, idx, m, k);
    1077        1113 :   return gc_long(av,1);
    1078             : }
    1079             : 
    1080             : /* Return 1 on success, 0 on failure. */
    1081             : static int
    1082         266 : orient_pcp(classgp_pcp_t G, long *ni, long D, long u, hashtable *tbl)
    1083             : {
    1084         266 :   pari_sp av = avma;
    1085             :   /* 199 seems to suffice, but can be increased if necessary */
    1086             :   enum { MAX_ORIENT_P = 199 };
    1087         266 :   const long *L = G->L, *n = G->n, *r = G->r, *m = G->m, *o = G->o;
    1088         266 :   long i, *ps = G->orient_p, *qs = G->orient_q, *reps = G->orient_reps;
    1089         266 :   long *ef, *e, *ei, *f, k = G->k, lvl = modinv_level(G->inv);
    1090         266 :   GEN DD = stoi(D);
    1091             : 
    1092         266 :   memset(ps, 0, k * sizeof(long));
    1093         266 :   memset(qs, 0, k * sizeof(long));
    1094         266 :   memset(reps, 0, k * k * sizeof(long));
    1095             : 
    1096         329 :   for (i = 0; i < k; ++i) { ps[i] = -1; if (o[i] > 2) break; }
    1097         371 :   for (++i; i < k; ++i) ps[i] = (o[i] > 2) ? 0 : -1; /* ps[i] = -!(o[i] > 2); */
    1098             : 
    1099         266 :   e = new_chunk(k);
    1100         266 :   ei = new_chunk(k);
    1101         266 :   f = new_chunk(k);
    1102             : 
    1103         686 :   for (i = 0; i < k; ++i) {
    1104             :     long p;
    1105         504 :     if (ps[i]) continue;
    1106          98 :     p = L[i];
    1107          98 :     ef = &reps[i * k];
    1108         595 :     while (!ps[i]) {
    1109         518 :       if (!next_prime_evec(&p, ef, m, k, tbl, D, DD, u, lvl, MAX_ORIENT_P))
    1110          21 :         break;
    1111         497 :       evec_inverse_o(ei, ef, n, o, r, k);
    1112         497 :       if (!distinct_inverses(NULL, ef, ei, n, o, r, k, G->L0, i)) continue;
    1113          77 :       ps[i] = p;
    1114          77 :       qs[i] = 1;
    1115             :     }
    1116          98 :     if (ps[i]) continue;
    1117             : 
    1118          21 :     p = unextprime(L[i] + 1);
    1119         133 :     while (!ps[i]) {
    1120             :       long q;
    1121             : 
    1122         119 :       if (!next_prime_evec(&p, e, m, k, tbl, D, DD, u, lvl, MAX_ORIENT_P))
    1123           7 :         break;
    1124         112 :       evec_inverse_o(ei, e, n, o, r, k);
    1125             : 
    1126         112 :       q = L[i];
    1127         616 :       while (!qs[i]) {
    1128         602 :         if (!next_prime_evec(&q, f, m, k, tbl, D, DD, u, lvl, p - 1)) break;
    1129         504 :         evec_compose(ef, e, f, n, r, k);
    1130         504 :         if (!distinct_inverses(f, ef, ei, n, o, r, k, G->L0, i)) continue;
    1131          14 :         ps[i] = p;
    1132          14 :         qs[i] = q;
    1133             :       }
    1134             :     }
    1135          21 :     if (!ps[i]) return 0;
    1136             :   }
    1137         259 :   if (ni) {
    1138         259 :     GEN N = qfb_nform(D, *ni);
    1139         259 :     hashentry *he = hash_search(tbl, N);
    1140         259 :     if (!he) pari_err_BUG("orient_pcp");
    1141         259 :     *ni = itos((GEN) he->val);
    1142             :   }
    1143         259 :   return gc_bool(av,1);
    1144             : }
    1145             : 
    1146             : /* We must avoid situations where L_i^{+/-2} = L_j^2 (or = L_0*L_j^2
    1147             :  * if ell0 flag is set), with |L_i| = |L_j| = 4 (or have 4th powers in
    1148             :  * <L0> but not 2nd powers in <L0>) and j < i */
    1149             : /* These cases cause problems when enumerating roots via gcds */
    1150             : /* returns the index of the first bad generator, or -1 if no bad
    1151             :  * generators are found */
    1152             : static long
    1153        3682 : classgp_pcp_check_generators(const long *n, long *r, long k, long L0)
    1154             : {
    1155        3682 :   pari_sp av = avma;
    1156             :   long *e1, i, i0, j, s;
    1157             :   const long *ei;
    1158             : 
    1159        3682 :   s = !!L0;
    1160        3682 :   e1 = new_chunk(k);
    1161             : 
    1162        4575 :   for (i = s + 1; i < k; i++) {
    1163         907 :     if (n[i] != 2) continue;
    1164         542 :     ei = evec_ri(r, i);
    1165         752 :     for (j = s; j < i; j++)
    1166         598 :       if (ei[j]) break;
    1167         542 :     if (j == i) continue;
    1168         916 :     for (i0 = s; i0 < i; i0++) {
    1169         542 :       if ((4 % n[i0])) continue;
    1170         231 :       evec_clear(e1, k);
    1171         231 :       e1[i0] = 4;
    1172         231 :       evec_reduce(e1, n, r, k);
    1173         623 :       for (j = s; j < i; j++)
    1174         434 :         if (e1[j]) break;
    1175         231 :       if (j < i) continue; /* L_i0^4 is not trivial or in <L_0> */
    1176         189 :       evec_clear(e1, k);
    1177         189 :       e1[i0] = 2;
    1178         189 :       evec_reduce(e1, n, r, k); /* compute L_i0^2 */
    1179         301 :       for (j = s; j < i; j++)
    1180         287 :         if (e1[j] != ei[j]) break;
    1181         189 :       if (j == i) return i;
    1182         175 :       evec_inverse(e1, e1, n, r, k); /* compute L_i0^{-2} */
    1183         273 :       for (j = s; j < i; j++)
    1184         273 :         if (e1[j] != ei[j]) break;
    1185         175 :       if (j == i) return i;
    1186             :     }
    1187             :   }
    1188        3668 :   return gc_long(av,-1);
    1189             : }
    1190             : 
    1191             : static void
    1192        3668 : pcp_alloc_and_set(
    1193             :   classgp_pcp_t G, const long *L, const long *n, const long *r, long k)
    1194             : {
    1195             :   /* classgp_pcp contains 6 arrays of length k (L, m, n, o, orient_p, orient_q),
    1196             :    * one of length binom(k, 2) (r) and one of length k^2 (orient_reps) */
    1197        3668 :   long rlen = k * (k - 1) / 2, datalen = 6 * k + rlen + k * k;
    1198        3668 :   G->_data = newblock(datalen);
    1199        3668 :   G->L = G->_data;
    1200        3668 :   G->m = G->L + k;
    1201        3668 :   G->n = G->m + k;
    1202        3668 :   G->o = G->n + k;
    1203        3668 :   G->r = G->o + k;
    1204        3668 :   G->orient_p = G->r + rlen;
    1205        3668 :   G->orient_q = G->orient_p + k;
    1206        3668 :   G->orient_reps = G->orient_q + k;
    1207        3668 :   G->k = k;
    1208             : 
    1209        3668 :   evec_copy(G->L, L, k);
    1210        3668 :   evec_copy(G->n, n, k);
    1211        3668 :   evec_copy(G->r, r, rlen);
    1212        3668 :   evec_orders(G->o, n, r, k);
    1213        3668 :   evec_n_to_m(G->m, n, k);
    1214        3668 : }
    1215             : 
    1216             : static void
    1217        3851 : classgp_pcp_clear(classgp_pcp_t G)
    1218        3851 : { if (G->_data) killblock(G->_data); }
    1219             : 
    1220             : /* This is Sutherland 2009, Algorithm 2.2 (p16). */
    1221             : static void
    1222        3844 : classgp_make_pcp(
    1223             :   classgp_pcp_t G, double *height, long *ni,
    1224             :   long h, long D, ulong u, long inv, long Lfilter, long orient)
    1225             : {
    1226             :   enum { MAX_GENS = 16, MAX_RLEN = MAX_GENS * (MAX_GENS - 1) / 2 };
    1227        3844 :   pari_sp av = avma, bv;
    1228        3844 :   long curr_p, h2, nelts, lvl = modinv_level(inv);
    1229             :   GEN DD, ident, T, v;
    1230             :   hashtable *tbl;
    1231             :   long i, L1, L2;
    1232             :   long k, L[MAX_GENS], n[MAX_GENS], r[MAX_RLEN];
    1233             : 
    1234        3844 :   memset(G, 0, sizeof *G);
    1235             : 
    1236        3844 :   G->D = D;
    1237        3844 :   G->h = h;
    1238        3844 :   G->inv = inv;
    1239        4327 :   G->L0 = (modinv_is_double_eta(inv) && modinv_ramified(D, inv))
    1240        4327 :     ? modinv_degree(NULL, NULL, inv) : 0;
    1241        3844 :   G->enum_cnt = h / (1 + !!G->L0);
    1242        3844 :   G->Lfilter = ulcm(Lfilter, lvl);
    1243             : 
    1244        3844 :   if (h == 1) {
    1245         183 :     if (G->L0) pari_err_BUG("classgp_pcp");
    1246         183 :     G->k = 0;
    1247         183 :     G->_data = NULL;
    1248         183 :     v = const_vecsmall(1, 1);
    1249         183 :     *height = upper_bound_on_classpoly_coeffs(D, h, v);
    1250             :     /* NB: No need to set *ni when h = 1 */
    1251         183 :     set_avma(av); return;
    1252             :   }
    1253             : 
    1254        3661 :   DD = stoi(D);
    1255        3661 :   bv = avma;
    1256             :   while (1) {
    1257          21 :     k = 0;
    1258             :     /* Hash table has a QFI as a key and the (boxed) index of that QFI
    1259             :      * in T as its value */
    1260        3682 :     tbl = hash_create(h, (ulong(*)(void*)) hash_GEN,
    1261             :                          (int(*)(void*,void*))&gequal, 1);
    1262        3682 :     ident = redimag(primeform_u(DD, 1));
    1263        3682 :     hash_insert(tbl, ident, gen_0);
    1264             : 
    1265        3682 :     T = vectrunc_init(h + 1);
    1266        3682 :     vectrunc_append(T, ident);
    1267        3682 :     nelts = 1;
    1268        3682 :     curr_p = 1;
    1269             : 
    1270        8551 :     while (nelts < h) {
    1271             :       GEN gamma_i, beta;
    1272             :       hashentry *e;
    1273        4869 :       long N = glength(T), Tlen = N, ri = 1;
    1274             : 
    1275        4869 :       if (k == MAX_GENS) pari_err_IMPL("classgp_pcp");
    1276             : 
    1277        4869 :       if (nelts == 1 && G->L0) {
    1278          98 :         curr_p = G->L0;
    1279          98 :         gamma_i = qfb_nform(D, curr_p);
    1280          98 :         beta = redimag(gamma_i);
    1281          98 :         if (equali1(gel(beta, 1)))
    1282             :         {
    1283           0 :           curr_p = 1;
    1284           0 :           gamma_i = next_generator(DD, D, u, G->Lfilter, &beta, &curr_p);
    1285             :         }
    1286             :       } else
    1287        4771 :         gamma_i = next_generator(DD, D, u, G->Lfilter, &beta, &curr_p);
    1288       56476 :       while ((e = hash_search(tbl, beta)) == NULL) {
    1289             :         long j;
    1290      118630 :         for (j = 1; j <= N; ++j) {
    1291       67023 :           GEN t = qficomp(beta, gel(T, j));
    1292       67023 :           vectrunc_append(T, t);
    1293       67023 :           hash_insert(tbl, t, stoi(Tlen++));
    1294             :         }
    1295       51607 :         beta = qficomp(beta, gamma_i);
    1296       51607 :         ++ri;
    1297             :       }
    1298        4869 :       if (ri > 1) {
    1299             :         long j, si;
    1300        4687 :         L[k] = curr_p;
    1301        4687 :         n[k] = ri;
    1302        4687 :         nelts *= ri;
    1303             : 
    1304             :         /* This is to reset the curr_p counter when we have G->L0 != 0
    1305             :          * in the first position of L. */
    1306        4687 :         if (curr_p == G->L0) curr_p = 1;
    1307             : 
    1308        4687 :         N = 1;
    1309        4687 :         si = itos((GEN) e->val);
    1310        6021 :         for (j = 0; j < k; ++j) {
    1311        1334 :           evec_ri_mutate(r, k)[j] = (si / N) % n[j];
    1312        1334 :           N *= n[j];
    1313             :         }
    1314        4687 :         ++k;
    1315             :       }
    1316             :     }
    1317             : 
    1318        3682 :     if ((i = classgp_pcp_check_generators(n, r, k, G->L0)) < 0) {
    1319        3668 :       pcp_alloc_and_set(G, L, n, r, k);
    1320        3668 :       if (!orient || orient_pcp(G, ni, D, u, tbl)) break;
    1321           7 :       G->Lfilter *= G->L[0];
    1322           7 :       classgp_pcp_clear(G);
    1323          14 :     } else if (log2(G->Lfilter) + log2(L[i]) >= BITS_IN_LONG)
    1324           0 :       pari_err_IMPL("classgp_pcp");
    1325             :     else
    1326          14 :       G->Lfilter *= L[i];
    1327          21 :     set_avma(bv);
    1328             :   }
    1329             : 
    1330        3661 :   v = cgetg(h + 1, t_VECSMALL);
    1331        3661 :   v[1] = 1;
    1332       68913 :   for (i = 2; i <= h; ++i) uel(v,i) = itou(gmael(T,i,1));
    1333             : 
    1334        3661 :   h2 = G->L0 ? h / 2 : h;
    1335        3661 :   *height = upper_bound_on_classpoly_coeffs(D, h2, v);
    1336             : 
    1337             :   /* The norms of the last one or two generators. */
    1338        3661 :   L1 = L[k - 1];
    1339        3661 :   L2 = k > 1 ? L[k - 2] : 1;
    1340             :   /* 4 * L1^2 * L2^2 must fit in a ulong */
    1341        3661 :   if (2 * (1 + log2(L1) + log2(L2)) >= BITS_IN_LONG)
    1342           0 :     pari_err_IMPL("classgp_pcp");
    1343             : 
    1344        3661 :   if (G->L0 && (G->L[0] != G->L0 || G->o[0] != 2))
    1345           0 :     pari_err_BUG("classgp_pcp");
    1346             : 
    1347        3661 :   set_avma(av); return;
    1348             : }
    1349             : 
    1350             : INLINE ulong
    1351        3844 : classno_wrapper(long D)
    1352             : {
    1353        3844 :   pari_sp av = avma;
    1354        3844 :   GEN G = quadclassunit0(stoi(D), 0, NULL, DEFAULTPREC);
    1355        3844 :   return gc_ulong(av, itou(abgrp_get_no(G)));
    1356             : }
    1357             : 
    1358             : /*
    1359             :  * SECTION: Functions for calculating class polynomials.
    1360             :  */
    1361             : 
    1362             : #define NSMALL_PRIMES 11
    1363             : static const long SMALL_PRIMES[11] = {
    1364             :   2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31
    1365             : };
    1366             : 
    1367             : static long
    1368       46214 : is_smooth_enough(ulong *factors, long v)
    1369             : {
    1370             :   long i;
    1371       46214 :   *factors = 0;
    1372      115695 :   for (i = 0; i < NSMALL_PRIMES; ++i) {
    1373      115688 :     long p = SMALL_PRIMES[i];
    1374      115688 :     if (v % p == 0) *factors |= 1UL << i;
    1375      190533 :     while (v % p == 0) v /= p;
    1376      115688 :     if (v == 1) break;
    1377             :   }
    1378       46214 :   return v == 1;
    1379             : }
    1380             : 
    1381             : /* Hurwitz class number of |D| assuming hclassno() and attached
    1382             :  * conversion to double costs much more than unegisfundamental(). */
    1383             : INLINE double
    1384       50051 : hclassno_wrapper(long D, long h)
    1385             : {
    1386             :   /* TODO: Can probably calculate hurwitz faster using -D, factor(u)
    1387             :    * and classno(D). */
    1388       50051 :   pari_sp av = avma;
    1389       50051 :   ulong abs_D = D < 0 ? -D : D;
    1390             :   double hurwitz;
    1391             : 
    1392       50051 :   if (h && unegisfundamental(abs_D))
    1393        3669 :     hurwitz = (double) h;
    1394             :   else
    1395       46382 :     hurwitz = rtodbl(gtofp(hclassno(utoi(abs_D)), DEFAULTPREC));
    1396       50051 :   set_avma(av); return hurwitz;
    1397             : }
    1398             : 
    1399             : /* This is Sutherland 2009, Algorithm 2.1 (p8); delta > 0 */
    1400             : static GEN
    1401        3844 : select_classpoly_prime_pool(double min_bits, double delta, classgp_pcp_t G)
    1402             : { /* Sutherland defines V_MAX to be 1200 without saying why */
    1403        3844 :   const long V_MAX = 1200;
    1404        3844 :   pari_sp av = avma;
    1405        3844 :   double bits = 0.0, hurwitz, z;
    1406             :   ulong t_size_lim;
    1407        3844 :   long ires, D = G->D, inv = G->inv;
    1408             :   GEN res, t_min; /* t_min[v] = lower bound for the t we look at for that v */
    1409             : 
    1410        3844 :   hurwitz = hclassno_wrapper(D, G->h);
    1411             : 
    1412        3844 :   res = cgetg(128+1, t_VEC);
    1413        3844 :   ires = 1;
    1414             :   /* Initialise t_min to be all 2's.  This avoids trace 0 and trace 1 curves */
    1415        3844 :   t_min = const_vecsmall(V_MAX-1, 2);
    1416             : 
    1417             :   /* maximum possible trace = sqrt(2^BIL - D) */
    1418        3844 :   t_size_lim = 2.0 * sqrt((double)((1UL << (BITS_IN_LONG - 2)) - (((ulong)-D) >> 2)));
    1419             : 
    1420        3844 :   for (z = -D / (2.0 * hurwitz); ; z *= delta + 1.0)
    1421           7 :   { /* v_bound_aux = -4 z H(-D) */
    1422        3851 :     double v_bound_aux = -4.0 * z * hurwitz;
    1423             :     ulong v;
    1424        3851 :     dbg_printf(1)("z = %.2f\n", z);
    1425       46221 :     for (v = 1; v < V_MAX; v++)
    1426             :     {
    1427       46221 :       ulong p, t, t_max, vfactors, m_vsqr_D = v * v * (ulong)(-D);
    1428             :       /* hurwitz_ratio_bound = 11 * log(log(v + 4))^2 */
    1429       46221 :       double hurwitz_ratio_bound = log(log(v + 4.0)), max_p, H;
    1430             :       long ires0;
    1431       46221 :       hurwitz_ratio_bound *= 11.0 * hurwitz_ratio_bound;
    1432             : 
    1433       46221 :       if (v >= v_bound_aux * hurwitz_ratio_bound / D) break;
    1434       46214 :       if (!is_smooth_enough(&vfactors, v)) continue;
    1435       46207 :       H = hclassno_wrapper(m_vsqr_D, 0);
    1436             : 
    1437             :       /* t <= 2 sqrt(p) and p <= z H(-v^2 D) and
    1438             :        *   H(-v^2 D) < vH(-D) (11 log(log(v + 4))^2)
    1439             :        * This last term is v * hurwitz * hurwitz_ratio_bound. */
    1440       46207 :       max_p = z * v * hurwitz * hurwitz_ratio_bound;
    1441       46207 :       t_max = 2.0 * sqrt(mindd((1UL<<(BITS_IN_LONG-2)) - (m_vsqr_D>>2), max_p));
    1442       46207 :       t = t_min[v]; if ((t & 1) != (m_vsqr_D & 1)) t++;
    1443       46207 :       p = (t * t + m_vsqr_D) >> 2;
    1444       46207 :       ires0 = ires;
    1445     6719261 :       for (; t <= t_max; p += t+1, t += 2) /* 4p = t^2 - v^2*D */
    1446     6673054 :         if (modinv_good_prime(inv,p) && uisprime(p))
    1447             :         {
    1448      243543 :           if (ires == lg(res)) res = vec_lengthen(res, lg(res) << 1);
    1449      243543 :           gel(res, ires++) = mkvecsmall5(p, t, v, (long)(p / H), vfactors);
    1450      243543 :           bits += log2(p);
    1451             :         }
    1452       46207 :       t_min[v] = t;
    1453             : 
    1454       46207 :       if (ires - ires0) {
    1455       23548 :         dbg_printf(2)("  Found %lu primes for v = %lu.\n", ires - ires0, v);
    1456             :       }
    1457       46207 :       if (bits > min_bits) {
    1458        3844 :         dbg_printf(1)("Found %ld primes; total size %.2f bits.\n", ires-1,bits);
    1459        3844 :         setlg(res, ires); return gerepilecopy(av, res);
    1460             :       }
    1461             :     }
    1462           7 :     if (uel(t_min,1) >= t_size_lim) {
    1463             :       /* exhausted all solutions that fit in ulong */
    1464           0 :       char *err = stack_sprintf("class polynomial of discriminant %ld", D);
    1465           0 :       pari_err(e_ARCH, err);
    1466             :     }
    1467             :   }
    1468             : }
    1469             : 
    1470             : static int
    1471     1058998 : primecmp(void *data, GEN v1, GEN v2)
    1472     1058998 : { (void)data; return cmpss(v1[4], v2[4]); }
    1473             : 
    1474             : static long
    1475        3844 : height_margin(long inv, long D)
    1476             : {
    1477             :   (void)D;
    1478             :   /* NB: avs just uses a height margin of 256 for everyone and everything. */
    1479        3844 :   if (inv == INV_F) return 64;  /* checked for discriminants up to -350000 */
    1480        3732 :   if (inv == INV_G2) return 5;
    1481        3445 :   if (inv != INV_J) return 256; /* TODO: This should be made more accurate */
    1482        2941 :   return 0;
    1483             : }
    1484             : 
    1485             : static GEN
    1486        3844 : select_classpoly_primes(
    1487             :   ulong *vfactors, ulong *biggest_v,
    1488             :   long k, double delta, classgp_pcp_t G, double height)
    1489             : {
    1490        3844 :   pari_sp av = avma;
    1491        3844 :   long i, s, D = G->D, inv = G->inv;
    1492             :   ulong biggest_p;
    1493             :   double prime_bits, min_prime_bits, b;
    1494             :   GEN prime_pool;
    1495             : 
    1496        3844 :   if (k < 2) pari_err_BUG("select_suitable_primes");
    1497             : 
    1498        3844 :   s = modinv_height_factor(inv);
    1499        3844 :   b = height / s + height_margin(inv, D);
    1500        3844 :   dbg_printf(1)("adjusted height = %.2f\n", b);
    1501        3844 :   min_prime_bits = k * b;
    1502             : 
    1503        3844 :   prime_pool = select_classpoly_prime_pool(min_prime_bits, delta, G);
    1504             : 
    1505             :   /* FIXME: Apply torsion constraints */
    1506             :   /* FIXME: Rank elts of res according to cost/benefit ratio */
    1507        3844 :   gen_sort_inplace(prime_pool, NULL, primecmp, NULL);
    1508             : 
    1509        3844 :   prime_bits = 0.0;
    1510        3844 :   biggest_p = gel(prime_pool, 1)[1];
    1511        3844 :   *biggest_v = gel(prime_pool, 1)[3];
    1512        3844 :   *vfactors = 0;
    1513      118753 :   for (i = 1; i < lg(prime_pool); ++i) {
    1514      118753 :     ulong p = gel(prime_pool, i)[1];
    1515      118753 :     ulong v = gel(prime_pool, i)[3];
    1516      118753 :     prime_bits += log2(p);
    1517      118753 :     *vfactors |= gel(prime_pool, i)[5];
    1518      118753 :     if (p > biggest_p) biggest_p = p;
    1519      118753 :     if (v > *biggest_v) *biggest_v = v;
    1520      118753 :     if (prime_bits > b) break;
    1521             :   }
    1522        3844 :   dbg_printf(1)("Selected %ld primes; largest is %lu ~ 2^%.2f\n",
    1523             :              i, biggest_p, log2(biggest_p));
    1524        3844 :   return gerepilecopy(av, vecslice0(prime_pool, 1, i));
    1525             : }
    1526             : 
    1527             : /* This is Sutherland 2009 Algorithm 1.2. */
    1528             : static long
    1529      119361 : oneroot_of_classpoly(
    1530             :   ulong *j_endo, int *endo_cert, ulong j, norm_eqn_t ne, GEN jdb)
    1531             : {
    1532      119361 :   pari_sp av = avma;
    1533             :   long nfactors, L_bound, i;
    1534      119361 :   ulong p = ne->p, pi = ne->pi;
    1535             :   GEN factw, factors, u_levels, vdepths;
    1536             : 
    1537      119361 :   if (j == 0 || j == 1728 % p) pari_err_BUG("oneroot_of_classpoly");
    1538             : 
    1539      119361 :   *endo_cert = 1;
    1540      119361 :   if (ne->u * ne->v == 1) { *j_endo = j; return 1; }
    1541             : 
    1542             :   /* TODO: Precalculate all this data further up */
    1543      116470 :   factw = factoru(ne->u * ne->v);
    1544      116470 :   factors = gel(factw, 1);
    1545      116470 :   nfactors = lg(factors) - 1;
    1546      116470 :   u_levels = cgetg(nfactors + 1, t_VECSMALL);
    1547      299543 :   for (i = 1; i <= nfactors; ++i)
    1548      183073 :     u_levels[i] = z_lval(ne->u, gel(factw, 1)[i]);
    1549      116470 :   vdepths = gel(factw, 2);
    1550             : 
    1551             :   /* FIXME: This should be bigger */
    1552      116470 :   L_bound = maxdd(log((double) -ne->D), (double)ne->v);
    1553             : 
    1554             :   /* Iterate over the primes L dividing w */
    1555      295631 :   for (i = 1; i <= nfactors; ++i) {
    1556      183073 :     pari_sp bv = avma;
    1557             :     GEN phi;
    1558      183073 :     long jlvl, lvl_diff, depth = vdepths[i];
    1559      183073 :     long L = factors[i];
    1560      183073 :     if (L > L_bound) { *endo_cert = 0; break; }
    1561             : 
    1562      179161 :     phi = polmodular_db_getp(jdb, L, p);
    1563             : 
    1564             :     /* TODO: See if I can reuse paths created in j_level_in_volcano()
    1565             :      * later in {ascend,descend}_volcano(), perhaps by combining the
    1566             :      * functions into one "adjust_level" function. */
    1567      179161 :     jlvl = j_level_in_volcano(phi, j, p, pi, L, depth);
    1568      179161 :     lvl_diff = u_levels[i] - jlvl;
    1569             : 
    1570      179161 :     if (lvl_diff < 0)
    1571             :       /* j's level is less than v(u) so we must ascend */
    1572      111221 :       j = ascend_volcano(phi, j, p, pi, jlvl, L, depth, -lvl_diff);
    1573       67940 :     else if (lvl_diff > 0)
    1574             :       /* otherwise j's level is greater than v(u) so we descend */
    1575         774 :       j = descend_volcano(phi, j, p, pi, jlvl, L, depth, lvl_diff);
    1576      179161 :     set_avma(bv);
    1577             :   }
    1578      116470 :   set_avma(av);
    1579             :   /* At this point the probability that j has the wrong endomorphism
    1580             :    * ring is about \sum_{p|u_compl} 1/p (and u_compl must be bigger
    1581             :    * than L_bound, so pretty big), so just return it and rely on
    1582             :    * detection code in enum_j_with_endo_ring().  Detection is that we
    1583             :    * hit a previously found j-invariant earlier than expected.  OR, we
    1584             :    * evaluate class polynomials of the suborders at j and if any are
    1585             :    * zero then j must be chosen again.  */
    1586      116470 :   *j_endo = j;
    1587      116470 :   return j != 0 && j != 1728 % p;
    1588             : }
    1589             : 
    1590             : INLINE long
    1591        3304 : vecsmall_isin_skip(GEN v, long x, long k)
    1592             : {
    1593        3304 :   long i, l = lg(v);
    1594      117467 :   for (i = k; i < l; ++i)
    1595      114163 :     if (v[i] == x) return i;
    1596        3304 :   return 0;
    1597             : }
    1598             : 
    1599             : INLINE ulong
    1600      238108 : select_twisting_param(ulong p)
    1601             : {
    1602             :   ulong T;
    1603      238108 :   do T = random_Fl(p); while (krouu(T, p) != -1);
    1604      118753 :   return T;
    1605             : }
    1606             : 
    1607             : INLINE void
    1608      118753 : setup_norm_eqn(norm_eqn_t ne, long D, long u, GEN norm_eqn)
    1609             : {
    1610      118753 :   ne->D = D;
    1611      118753 :   ne->u = u;
    1612      118753 :   ne->t = norm_eqn[2];
    1613      118753 :   ne->v = norm_eqn[3];
    1614      118753 :   ne->p = (ulong) norm_eqn[1];
    1615      118753 :   ne->pi = get_Fl_red(ne->p);
    1616      118753 :   ne->s2 = Fl_2gener_pre(ne->p, ne->pi);
    1617      118753 :   ne->T = select_twisting_param(ne->p);
    1618      118753 : }
    1619             : 
    1620             : INLINE ulong
    1621       15302 : Flv_powsum_pre(GEN v, ulong n, ulong p, ulong pi)
    1622             : {
    1623       15302 :   long i, l = lg(v);
    1624       15302 :   ulong psum = 0;
    1625      372134 :   for (i = 1; i < l; ++i)
    1626      356832 :     psum = Fl_add(psum, Fl_powu_pre(uel(v,i), n, p, pi), p);
    1627       15302 :   return psum;
    1628             : }
    1629             : 
    1630             : INLINE int
    1631        3844 : modinv_has_sign_ambiguity(long inv)
    1632             : {
    1633        3844 :   switch (inv) {
    1634         525 :   case INV_F:
    1635             :   case INV_F3:
    1636             :   case INV_W2W3E2:
    1637             :   case INV_W2W7E2:
    1638             :   case INV_W2W3:
    1639             :   case INV_W2W5:
    1640             :   case INV_W2W7:
    1641             :   case INV_W3W3:
    1642             :   case INV_W2W13:
    1643         525 :   case INV_W3W7: return 1;
    1644             :   }
    1645        3319 :   return 0;
    1646             : }
    1647             : 
    1648             : INLINE int
    1649        3276 : modinv_units(int inv)
    1650        3276 : { return modinv_is_double_eta(inv) || modinv_is_Weber(inv); }
    1651             : 
    1652             : INLINE int
    1653       10318 : adjust_signs(GEN js, ulong p, ulong pi, long inv, GEN T, long e)
    1654             : {
    1655       10318 :   long negate = 0;
    1656       10318 :   long h = lg(js) - 1;
    1657       13391 :   if ((h & 1) && modinv_units(inv)) {
    1658        3073 :     ulong prod = Flv_prod_pre(js, p, pi);
    1659        3073 :     if (prod != p - 1) {
    1660        1556 :       if (prod != 1) pari_err_BUG("adjust_signs: constant term is not +/-1");
    1661        1556 :       negate = 1;
    1662             :     }
    1663             :   } else {
    1664             :     ulong tp, t;
    1665        7245 :     tp = umodiu(T, p);
    1666        7245 :     t = Flv_powsum_pre(js, e, p, pi);
    1667        7245 :     if (t == 0) return 0;
    1668        7231 :     if (t != tp) {
    1669        3538 :       if (Fl_neg(t, p) != tp) pari_err_BUG("adjust_signs: incorrect trace");
    1670        3538 :       negate = 1;
    1671             :     }
    1672             :   }
    1673       10304 :   if (negate) Flv_neg_inplace(js, p);
    1674       10304 :   return 1;
    1675             : }
    1676             : 
    1677             : static ulong
    1678      119290 : find_jinv(
    1679             :   long *trace_tries, long *endo_tries, int *cert,
    1680             :   norm_eqn_t ne, long inv, long rho_inv, GEN jdb)
    1681             : {
    1682      119290 :   long found, ok = 1;
    1683             :   ulong j, r;
    1684             :   do {
    1685             :     do {
    1686             :       long tries;
    1687      119361 :       ulong j_t = 0;
    1688             :       /* TODO: Set batch size according to expected number of tries and
    1689             :        * experimental cost/benefit analysis. */
    1690      119361 :       tries = find_j_inv_with_given_trace(&j_t, ne, rho_inv, 0);
    1691      119361 :       if (j_t == 0)
    1692           0 :         pari_err_BUG("polclass0: Couldn't find j-invariant with given trace.");
    1693      119361 :       dbg_printf(2)("  j-invariant %ld has trace +/-%ld (%ld tries, 1/rho = %ld)\n",
    1694             :           j_t, ne->t, tries, rho_inv);
    1695      119361 :       *trace_tries += tries;
    1696             : 
    1697      119361 :       found = oneroot_of_classpoly(&j, cert, j_t, ne, jdb);
    1698      119361 :       ++*endo_tries;
    1699      119361 :     } while (!found);
    1700             : 
    1701      119361 :     if (modinv_is_double_eta(inv))
    1702       11718 :       ok = modfn_unambiguous_root(&r, inv, j, ne, jdb);
    1703             :     else
    1704      107643 :       r = modfn_root(j, ne, inv);
    1705      119361 :   } while (!ok);
    1706      119290 :   return r;
    1707             : }
    1708             : 
    1709             : static GEN
    1710      118753 : polclass_roots_modp(
    1711             :   long *n_trace_curves,
    1712             :   norm_eqn_t ne, long rho_inv, classgp_pcp_t G, GEN db)
    1713             : {
    1714      118753 :   pari_sp av = avma;
    1715      118753 :   ulong j = 0;
    1716      118753 :   long inv = G->inv, endo_tries = 0;
    1717             :   int endo_cert;
    1718             :   GEN res, jdb, fdb;
    1719             : 
    1720      118753 :   jdb = polmodular_db_for_inv(db, INV_J);
    1721      118753 :   fdb = polmodular_db_for_inv(db, inv);
    1722             : 
    1723      118753 :   dbg_printf(2)("p = %ld, t = %ld, v = %ld\n", ne->p, ne->t, ne->v);
    1724             : 
    1725             :   do {
    1726      119290 :     j = find_jinv(n_trace_curves, &endo_tries, &endo_cert, ne, inv, rho_inv, jdb);
    1727             : 
    1728      119290 :     res = enum_roots(j, ne, fdb, G);
    1729      119290 :     if ( ! res && endo_cert) pari_err_BUG("polclass_roots_modp");
    1730      119290 :     if (res && ! endo_cert && vecsmall_isin_skip(res, res[1], 2))
    1731             :     {
    1732           0 :       set_avma(av);
    1733           0 :       res = NULL;
    1734             :     }
    1735      119290 :   } while (!res);
    1736             : 
    1737      118753 :   dbg_printf(2)("  j-invariant %ld has correct endomorphism ring "
    1738             :              "(%ld tries)\n", j, endo_tries);
    1739      118753 :   dbg_printf(4)("  all such j-invariants: %Ps\n", res);
    1740      118753 :   return gerepileupto(av, res);
    1741             : }
    1742             : 
    1743             : INLINE int
    1744        2134 : modinv_inverted_involution(long inv)
    1745        2134 : { return modinv_is_double_eta(inv); }
    1746             : 
    1747             : INLINE int
    1748        2134 : modinv_negated_involution(long inv)
    1749             : { /* determined by trial and error */
    1750        2134 :   return inv == INV_F || inv == INV_W3W5 || inv == INV_W3W7
    1751        4268 :     || inv == INV_W3W3 || inv == INV_W5W7;
    1752             : }
    1753             : 
    1754             : /* Return true iff Phi_L(j0, j1) = 0. */
    1755             : INLINE long
    1756        3962 : verify_edge(ulong j0, ulong j1, ulong p, ulong pi, long L, GEN fdb)
    1757             : {
    1758        3962 :   pari_sp av = avma;
    1759        3962 :   GEN phi = polmodular_db_getp(fdb, L, p);
    1760        3962 :   GEN f = Flm_Fl_polmodular_evalx(phi, L, j1, p, pi);
    1761        3962 :   return gc_long(av, Flx_eval_pre(f, j0, p, pi) == 0);
    1762             : }
    1763             : 
    1764             : INLINE long
    1765         672 : verify_2path(
    1766             :   ulong j1, ulong j2, ulong p, ulong pi, long L1, long L2, GEN fdb)
    1767             : {
    1768         672 :   pari_sp av = avma;
    1769         672 :   GEN phi1 = polmodular_db_getp(fdb, L1, p);
    1770         672 :   GEN phi2 = polmodular_db_getp(fdb, L2, p);
    1771         672 :   GEN f = Flm_Fl_polmodular_evalx(phi1, L1, j1, p, pi);
    1772         672 :   GEN g = Flm_Fl_polmodular_evalx(phi2, L2, j2, p, pi);
    1773         672 :   GEN d = Flx_gcd(f, g, p);
    1774         672 :   long n = degpol(d);
    1775         672 :   if (n >= 2) n = Flx_nbroots(d, p);
    1776         672 :   return gc_long(av, n);
    1777             : }
    1778             : 
    1779             : static long
    1780        5908 : oriented_n_action(
    1781             :   const long *ni, classgp_pcp_t G, GEN v, ulong p, ulong pi, GEN fdb)
    1782             : {
    1783        5908 :   pari_sp av = avma;
    1784        5908 :   long i, j, k = G->k;
    1785        5908 :   long nr = k * (k - 1) / 2;
    1786        5908 :   const long *n = G->n, *m = G->m, *o = G->o, *r = G->r,
    1787        5908 :     *ps = G->orient_p, *qs = G->orient_q, *reps = G->orient_reps;
    1788        5908 :   long *signs = new_chunk(k);
    1789        5908 :   long *e = new_chunk(k);
    1790        5908 :   long *rels = new_chunk(nr);
    1791             : 
    1792        5908 :   evec_copy(rels, r, nr);
    1793             : 
    1794       15813 :   for (i = 0; i < k; ++i) {
    1795             :     /* If generator doesn't require orientation, continue; power rels already
    1796             :      * copied to *rels in initialisation */
    1797        9905 :     if (ps[i] <= 0) { signs[i] = 1; continue; }
    1798             :     /* Get rep of orientation element and express it in terms of the
    1799             :      * (partially) oriented presentation */
    1800        6503 :     for (j = 0; j < i; ++j) {
    1801        4186 :       long t = reps[i * k + j];
    1802        4186 :       e[j] = (signs[j] < 0 ? o[j] - t : t);
    1803             :     }
    1804        2317 :     e[j] = reps[i * k + j];
    1805        3472 :     for (++j; j < k; ++j) e[j] = 0;
    1806        2317 :     evec_reduce(e, n, rels, k);
    1807        2317 :     j = evec_to_index(e, m, k);
    1808             : 
    1809             :     /* FIXME: These calls to verify_edge recalculate powers of v[0]
    1810             :      * and v[j] over and over again, they also reduce Phi_{ps[i]} modulo p over
    1811             :      * and over again.  Need to cache these things! */
    1812        2317 :     if (qs[i] > 1)
    1813         336 :       signs[i] =
    1814         336 :         (verify_2path(uel(v,1), uel(v,j+1), p, pi, ps[i], qs[i], fdb) ? 1 : -1);
    1815             :     else
    1816             :       /* Verify ps[i]-edge to orient ith generator */
    1817        1981 :       signs[i] =
    1818        1981 :         (verify_edge(uel(v,1), uel(v,j+1), p, pi, ps[i], fdb) ? 1 : -1);
    1819             :     /* Update power relation */
    1820        6503 :     for (j = 0; j < i; ++j) {
    1821        4186 :       long t = evec_ri(r, i)[j];
    1822        4186 :       e[j] = (signs[i] * signs[j] < 0 ? o[j] - t : t);
    1823             :     }
    1824        5789 :     while (j < k) e[j++] = 0;
    1825        2317 :     evec_reduce(e, n, rels, k);
    1826        6503 :     for (j = 0; j < i; ++j) evec_ri_mutate(rels, i)[j] = e[j];
    1827             :     /* TODO: This is a sanity check, can be removed if everything is working */
    1828        8820 :     for (j = 0; j <= i; ++j) {
    1829        6503 :       long t = reps[i * k + j];
    1830        6503 :       e[j] = (signs[j] < 0 ? o[j] - t : t);
    1831             :     }
    1832        3472 :     while (j < k) e[j++] = 0;
    1833        2317 :     evec_reduce(e, n, rels, k);
    1834        2317 :     j = evec_to_index(e, m, k);
    1835        2317 :     if (qs[i] > 1) {
    1836         336 :       if (!verify_2path(uel(v,1), uel(v, j+1), p, pi, ps[i], qs[i], fdb))
    1837           0 :         pari_err_BUG("oriented_n_action");
    1838             :     } else {
    1839        1981 :       if (!verify_edge(uel(v,1), uel(v, j+1), p, pi, ps[i], fdb))
    1840           0 :         pari_err_BUG("oriented_n_action");
    1841             :     }
    1842             :   }
    1843             : 
    1844             :   /* Orient representation of [N] relative to the torsor <signs, rels> */
    1845       15813 :   for (i = 0; i < k; ++i) e[i] = (signs[i] < 0 ? o[i] - ni[i] : ni[i]);
    1846        5908 :   evec_reduce(e, n, rels, k);
    1847        5908 :   return gc_long(av, evec_to_index(e,m,k));
    1848             : }
    1849             : 
    1850             : /* F = double_eta_raw(inv) */
    1851             : INLINE void
    1852        5908 : adjust_orientation(GEN F, long inv, GEN v, long e, ulong p, ulong pi)
    1853             : {
    1854        5908 :   ulong j0 = uel(v, 1), je = uel(v, e);
    1855             : 
    1856        5908 :   if (!modinv_j_from_2double_eta(F, inv, NULL, j0, je, p, pi)) {
    1857        2134 :     if (modinv_inverted_involution(inv)) Flv_inv_pre_inplace(v, p, pi);
    1858        2134 :     if (modinv_negated_involution(inv)) Flv_neg_inplace(v, p);
    1859             :   }
    1860        5908 : }
    1861             : 
    1862             : static void
    1863         525 : polclass_psum(
    1864             :   GEN *psum, long *d, GEN roots, GEN primes, GEN pilist, ulong h, long inv)
    1865             : {
    1866             :   /* Number of consecutive CRT stabilisations before we assume we have
    1867             :    * the correct answer. */
    1868             :   enum { MIN_STAB_CNT = 3 };
    1869         525 :   pari_sp av = avma, btop;
    1870             :   GEN ps, psum_sqr, P;
    1871         525 :   long i, e, stabcnt, nprimes = lg(primes) - 1;
    1872             : 
    1873         525 :   if ((h & 1) && modinv_units(inv)) { *psum = gen_1; *d = 0; return; }
    1874         322 :   e = -1;
    1875         322 :   ps = cgetg(nprimes+1, t_VECSMALL);
    1876             :   do {
    1877         364 :     e += 2;
    1878        8421 :     for (i = 1; i <= nprimes; ++i)
    1879             :     {
    1880        8057 :       GEN roots_modp = gel(roots, i);
    1881        8057 :       ulong p = uel(primes, i), pi = uel(pilist, i);
    1882        8057 :       uel(ps, i) = Flv_powsum_pre(roots_modp, e, p, pi);
    1883             :     }
    1884         364 :     btop = avma;
    1885         364 :     psum_sqr = Z_init_CRT(0, 1);
    1886         364 :     P = gen_1;
    1887        2065 :     for (i = 1, stabcnt = 0; stabcnt < MIN_STAB_CNT && i <= nprimes; ++i)
    1888             :     {
    1889        1701 :       ulong p = uel(primes, i), pi = uel(pilist, i);
    1890        1701 :       ulong ps2 = Fl_sqr_pre(uel(ps, i), p, pi);
    1891        1701 :       ulong stab = Z_incremental_CRT(&psum_sqr, ps2, &P, p);
    1892             :       /* stabcnt = stab * (stabcnt + 1) */
    1893        1701 :       if (stab) ++stabcnt; else stabcnt = 0;
    1894        1701 :       if (gc_needed(av, 2)) gerepileall(btop, 2, &psum_sqr, &P);
    1895             :     }
    1896         364 :     if (stabcnt == 0 && nprimes >= MIN_STAB_CNT)
    1897           0 :       pari_err_BUG("polclass_psum");
    1898         364 :   } while (!signe(psum_sqr));
    1899             : 
    1900         322 :   if ( ! Z_issquareall(psum_sqr, psum)) pari_err_BUG("polclass_psum");
    1901             : 
    1902         322 :   dbg_printf(1)("Classpoly power sum (e = %ld) is %Ps; found with %.2f%% of the primes\n",
    1903           0 :       e, *psum, 100 * (i - 1) / (double) nprimes);
    1904         322 :   *psum = gerepileupto(av, *psum);
    1905         322 :   *d = e;
    1906             : }
    1907             : 
    1908             : static GEN
    1909         406 : polclass_small_disc(long D, long inv, long xvar)
    1910             : {
    1911         406 :   if (D == -3) return pol_x(xvar);
    1912         140 :   if (D == -4) {
    1913         140 :     switch (inv) {
    1914           7 :     case INV_J: return deg1pol(gen_1, stoi(-1728), xvar);
    1915         133 :     case INV_G2:return deg1pol(gen_1, stoi(-12), xvar);
    1916           0 :     default: /* no other invariants for which we can calculate H_{-4}(X) */
    1917           0 :       pari_err_BUG("polclass_small_disc");
    1918             :     }
    1919             :   }
    1920           0 :   return NULL;
    1921             : }
    1922             : 
    1923             : GEN
    1924        4250 : polclass0(long D, long inv, long xvar, GEN *db)
    1925             : {
    1926        4250 :   pari_sp av = avma;
    1927             :   GEN primes;
    1928        4250 :   long n_curves_tested = 0;
    1929             :   long nprimes, s, i, j, del, ni, orient;
    1930             :   GEN P, H, plist, pilist;
    1931             :   ulong u, L, maxL, vfactors, biggest_v;
    1932        4250 :   long h, p1, p2, filter = 1;
    1933             :   classgp_pcp_t G;
    1934             :   double height;
    1935             :   static const long k = 2;
    1936             :   static const double delta = 0.5;
    1937             : 
    1938        4250 :   if (D >= -4) return polclass_small_disc(D, inv, xvar);
    1939             : 
    1940        3844 :   (void) corediscs(D, &u);
    1941        3844 :   h = classno_wrapper(D);
    1942             : 
    1943        3844 :   dbg_printf(1)("D = %ld, conductor = %ld, inv = %ld\n", D, u, inv);
    1944             : 
    1945        3844 :   ni = modinv_degree(&p1, &p2, inv);
    1946        3844 :   orient = modinv_is_double_eta(inv) && kross(D, p1) && kross(D, p2);
    1947             : 
    1948        3844 :   classgp_make_pcp(G, &height, &ni, h, D, u, inv, filter, orient);
    1949        3844 :   primes = select_classpoly_primes(&vfactors, &biggest_v, k, delta, G, height);
    1950             : 
    1951             :   /* Prepopulate *db with all the modpolys we might need */
    1952             :   /* TODO: Clean this up; in particular, note that u is factored later on. */
    1953             :   /* This comes from L_bound in oneroot_of_classpoly() above */
    1954        3844 :   maxL = maxdd(log((double) -D), (double)biggest_v);
    1955        3844 :   if (u > 1) {
    1956         847 :     for (L = 2; L <= maxL; L = unextprime(L + 1))
    1957         672 :       if (!(u % L)) polmodular_db_add_level(db, L, INV_J);
    1958             :   }
    1959       15509 :   for (i = 0; vfactors; ++i) {
    1960       11665 :     if (vfactors & 1UL)
    1961       11322 :       polmodular_db_add_level(db, SMALL_PRIMES[i], INV_J);
    1962       11665 :     vfactors >>= 1;
    1963             :   }
    1964        3844 :   if (p1 > 1) polmodular_db_add_level(db, p1, INV_J);
    1965        3844 :   if (p2 > 1) polmodular_db_add_level(db, p2, INV_J);
    1966        3844 :   s = !!G->L0;
    1967        3844 :   polmodular_db_add_levels(db, G->L + s, G->k - s, inv);
    1968        3844 :   if (orient) {
    1969         672 :     for (i = 0; i < G->k; ++i)
    1970             :     {
    1971         413 :       if (G->orient_p[i] > 1) polmodular_db_add_level(db, G->orient_p[i], inv);
    1972         413 :       if (G->orient_q[i] > 1) polmodular_db_add_level(db, G->orient_q[i], inv);
    1973             :     }
    1974             :   }
    1975        3844 :   nprimes = lg(primes) - 1;
    1976        3844 :   H = cgetg(nprimes + 1, t_VEC);
    1977        3844 :   plist = cgetg(nprimes + 1, t_VECSMALL);
    1978        3844 :   pilist = cgetg(nprimes + 1, t_VECSMALL);
    1979      122597 :   for (i = 1; i <= nprimes; ++i) {
    1980      118753 :     long rho_inv = gel(primes, i)[4];
    1981             :     norm_eqn_t ne;
    1982      118753 :     setup_norm_eqn(ne, D, u, gel(primes, i));
    1983             : 
    1984      118753 :     gel(H, i) = polclass_roots_modp(&n_curves_tested, ne, rho_inv, G, *db);
    1985      118753 :     uel(plist, i) = ne->p;
    1986      118753 :     uel(pilist, i) = ne->pi;
    1987      118753 :     if (DEBUGLEVEL>2 && (i & 3L)==0) err_printf(" %ld%%", i*100/nprimes);
    1988             :   }
    1989        3844 :   dbg_printf(0)("\n");
    1990             : 
    1991        3844 :   if (orient) {
    1992         259 :     GEN nvec = new_chunk(G->k);
    1993         259 :     GEN fdb = polmodular_db_for_inv(*db, inv);
    1994         259 :     GEN F = double_eta_raw(inv);
    1995         259 :     index_to_evec((long *)nvec, ni, G->m, G->k);
    1996        6167 :     for (i = 1; i <= nprimes; ++i) {
    1997        5908 :       GEN v = gel(H, i);
    1998        5908 :       ulong p = uel(plist, i), pi = uel(pilist, i);
    1999        5908 :       long oni = oriented_n_action(nvec, G, v, p, pi, fdb);
    2000        5908 :       adjust_orientation(F, inv, v, oni + 1, p, pi);
    2001             :     }
    2002             :   }
    2003             : 
    2004        3844 :   if (modinv_has_sign_ambiguity(inv)) {
    2005             :     GEN psum;
    2006             :     long e;
    2007         525 :     polclass_psum(&psum, &e, H, plist, pilist, h, inv);
    2008       10843 :     for (i = 1; i <= nprimes; ++i) {
    2009       10318 :       GEN v = gel(H, i);
    2010       10318 :       ulong p = uel(plist, i), pi = uel(pilist, i);
    2011       10318 :       if (!adjust_signs(v, p, pi, inv, psum, e))
    2012          14 :         uel(plist, i) = 0;
    2013             :     }
    2014             :   }
    2015             : 
    2016      122597 :   for (i = 1, j = 1, del = 0; i <= nprimes; ++i) {
    2017      118753 :     GEN v = gel(H, i), pol;
    2018      118753 :     ulong p = uel(plist, i);
    2019      118753 :     if (!p) { del++; continue; }
    2020      118739 :     pol = Flv_roots_to_pol(v, p, xvar);
    2021      118739 :     uel(plist, j) = p;
    2022      118739 :     gel(H, j++) = Flx_to_Flv(pol, lg(pol) - 2);
    2023             :   }
    2024        3844 :   setlg(H,nprimes+1-del);
    2025        3844 :   setlg(plist,nprimes+1-del);
    2026        3844 :   classgp_pcp_clear(G);
    2027             : 
    2028        3844 :   dbg_printf(1)("Total number of curves tested: %ld\n", n_curves_tested);
    2029        3844 :   H = ncV_chinese_center(H, plist, &P);
    2030        3844 :   dbg_printf(1)("Result height: %.2f\n",
    2031             :              dbllog2r(itor(gsupnorm(H, DEFAULTPREC), DEFAULTPREC)));
    2032        3844 :   return gerepilecopy(av, RgV_to_RgX(H, xvar));
    2033             : }
    2034             : 
    2035             : void
    2036        1225 : check_modinv(long inv)
    2037             : {
    2038        1225 :   switch (inv) {
    2039        1211 :   case INV_J:
    2040             :   case INV_F:
    2041             :   case INV_F2:
    2042             :   case INV_F3:
    2043             :   case INV_F4:
    2044             :   case INV_G2:
    2045             :   case INV_W2W3:
    2046             :   case INV_F8:
    2047             :   case INV_W3W3:
    2048             :   case INV_W2W5:
    2049             :   case INV_W2W7:
    2050             :   case INV_W3W5:
    2051             :   case INV_W3W7:
    2052             :   case INV_W2W3E2:
    2053             :   case INV_W2W5E2:
    2054             :   case INV_W2W13:
    2055             :   case INV_W2W7E2:
    2056             :   case INV_W3W3E2:
    2057             :   case INV_W5W7:
    2058             :   case INV_W3W13:
    2059        1211 :     break;
    2060          14 :   default:
    2061          14 :     pari_err_DOMAIN("polmodular", "inv", "invalid invariant", stoi(inv), gen_0);
    2062             :   }
    2063        1211 : }
    2064             : 
    2065             : GEN
    2066         623 : polclass(GEN DD, long inv, long xvar)
    2067             : {
    2068             :   GEN db, H;
    2069             :   long dummy, D;
    2070             : 
    2071         623 :   if (xvar < 0) xvar = 0;
    2072         623 :   check_quaddisc_imag(DD, &dummy, "polclass");
    2073         616 :   check_modinv(inv);
    2074             : 
    2075         609 :   D = itos(DD);
    2076         609 :   if (!modinv_good_disc(inv, D))
    2077           0 :     pari_err_DOMAIN("polclass", "D", "incompatible with given invariant", stoi(inv), DD);
    2078             : 
    2079         609 :   db = polmodular_db_init(inv);
    2080         609 :   H = polclass0(D, inv, xvar, &db);
    2081         609 :   gunclone_deep(db); return H;
    2082             : }

Generated by: LCOV version 1.13