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 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.11.0 lcov report (development 22860-5579deb0b) Lines: 838 862 97.2 %
Date: 2018-07-18 05:36:42 Functions: 59 60 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      118990 : hasse_bounds(long *low, long *high, long p)
      28             : {
      29      118990 :   long two_sqrt_p = usqrt(4*p);
      30      118990 :   *low = p + 1 - two_sqrt_p;
      31      118990 :   *high = p + 1 + two_sqrt_p;
      32      118990 : }
      33             : 
      34             : /* a / b : a and b are from factoru and b must divide a exactly */
      35             : INLINE GEN
      36        1560 : famatsmall_divexact(GEN a, GEN b)
      37             : {
      38        1560 :   GEN a1 = gel(a,1), a2 = gel(a,2), c1, c2;
      39        1560 :   GEN b1 = gel(b,1), b2 = gel(b,2);
      40        1560 :   long i, j, k, la = lg(a1);
      41        1560 :   c1 = cgetg(la, t_VECSMALL);
      42        1560 :   c2 = cgetg(la, t_VECSMALL);
      43        5590 :   for (i = j = k = 1; j < la; j++)
      44             :   {
      45        4030 :     c1[k] = a1[j];
      46        4030 :     c2[k] = a2[j];
      47        4030 :     if (a1[j] == b1[i]) { c2[k] -= b2[i++]; if (!c2[k]) continue; }
      48        2775 :     k++;
      49             :   }
      50        1560 :   setlg(c1, k);
      51        1560 :   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      140080 : 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      140080 :   pari_sp ltop = avma, av;
      63      140080 :   ulong a4t, a6t, p = ne->p, pi = ne->pi, T = ne->T, swapped = 0;
      64             :   long m0, m1, hasse_low, hasse_high;
      65             : 
      66      140080 :   if (p <= 11) {
      67          98 :     long card = (long)p + 1 - Fl_elltrace(a4, a6, p);
      68          98 :     return card == N0 || card == N1;
      69             :   }
      70             :   /* [a4, a6] is the given curve and [a4t, a6t] is its quadratic twist */
      71      139982 :   Fl_elltwist_disc(a4, a6, T, p, &a4t, &a6t);
      72             : 
      73      139982 :   m0 = m1 = 1;
      74      139982 :   if (N0 + N1 != 2 * (long)p + 2) pari_err_BUG("test_curve_order");
      75             : 
      76      139982 :   hasse_low = hasse[0];
      77      139982 :   hasse_high = hasse[1];
      78      139982 :   for (av = avma;;)
      79      105071 :   {
      80             :     GEN pt, Q, fa0;
      81             :     long a1, x, n_s;
      82             : 
      83      245053 :     pt = random_Flj_pre(a4, a6, p, pi);
      84      245053 :     Q = Flj_mulu_pre(pt, m0, a4, p, pi);
      85      245053 :     fa0 = m0 == 1? n0: famatsmall_divexact(n0, factoru(m0));
      86      245053 :     n_s = Flj_order_ufact(Q, N0 / m0, fa0, a4, p, pi);
      87      245053 :     if (n_s == 0) {
      88             :       /* If m0 divides N1 and m1 divides N0 and N0 < N1, then swap */
      89      105608 :       if (!swapped && N1 % m0 == 0 && N0 % m1 == 0) {
      90       84524 :         swapspec(n0, n1, N0, N1);
      91       84524 :         swapped = 1; continue;
      92             :       }
      93       21084 :       avma = ltop; return 0;
      94             :     }
      95             : 
      96      139445 :     m0 *= n_s;
      97      139445 :     a1 = (2 * p + 2) % m1;
      98      139445 :     x = (hasse_low + m0 - 1) / m0; /* using ceil(n/d) = (n + d - 1)/d */
      99      139445 :     x *= m0;
     100      271222 :     for ( ; x <= hasse_high; x += m0)
     101      152324 :       if ((x % m1) == a1 && x != N0 && x != N1) break;
     102             :     /* every x in N was either N0 or N1, so we return true */
     103      139445 :     if (x > hasse_high) { avma = ltop; return 1; }
     104             : 
     105       20547 :     lswap(a4, a4t);
     106       20547 :     lswap(a6, a6t);
     107       20547 :     lswap(m0, m1); avma = av;
     108             :   }
     109             : }
     110             : 
     111             : static GEN
     112      503592 : random_FleV(GEN x, GEN a6, ulong p, ulong pi)
     113      503592 : { 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      356970 : torsion_constraint(struct torctab_rec *torctab, long ltorc, double tormod[], long n, long m)
     632             : {
     633      356970 :   long i, b = -1;
     634      356970 :   double rb = -1.;
     635    43188612 :   for (i = 0 ; i < ltorc ; i++)
     636             :   {
     637    42831642 :     struct torctab_rec *ti = torctab + i;
     638    42831642 :     if ( ! (n%ti->m) && ( !ti->fix2 || (n%(2*ti->m)) ) && ( ! ti->fix3 || (n%(3*ti->m)) ) )
     639     3959544 :       if ( n == m || ( ! (m%ti->m) && ( !ti->fix2 || (m%(2*ti->m)) ) && ( ! ti->fix3 || (m%(3*ti->m)) ) ) )
     640             :       {
     641     3184361 :         double ri = ti->rating*tormod[ti->N];
     642     3184361 :         if ( b < 0 || ri < rb ) {  b = i; rb = ri; }
     643             :       }
     644             :   }
     645      356970 :   if (b < 0) pari_err_BUG("find_rating");
     646      356970 :   return b;
     647             : }
     648             : 
     649             : static void
     650      118990 : 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      118990 :   if ( (p%3)==2 ) {
     660       56164 :     if ( (p&3)==3 ) {
     661       27345 :       torctab = torctab1;
     662       27345 :       ltorc = sizeof(torctab1)/sizeof(*torctab1);
     663             :     } else {
     664       28819 :       torctab = torctab2;
     665       28819 :       ltorc = sizeof(torctab2)/sizeof(*torctab2);
     666             :     }
     667             :   } else {
     668       62826 :     if ( (p&3)==3 ) {
     669       34667 :       torctab = torctab3;
     670       34667 :       ltorc = sizeof(torctab3)/sizeof(*torctab3);
     671             :     } else {
     672       28159 :       torctab = torctab4;
     673       28159 :       ltorc = sizeof(torctab4)/sizeof(*torctab4);
     674             :     }
     675             :   }
     676      118990 :   for ( i = 0 ; i < 32 ; i++ ) tormod[i] = 1.0;
     677      118990 :   if ( (p%5)==1 ) tormod[5] = tormod[10] = tormod[15] = 6.0/5.0;
     678      118990 :   if ( (p%7)==1 ) tormod[7] = tormod[14] = 8.0/7.0;
     679      118990 :   if ( (p%11)== 1 ) tormod[11] = 12.0/11.0;
     680      118990 :   if ( (p%13)==1 ) tormod[13] = 14.0/13.0;
     681      118990 :   if ( (p%17)==1 ) tormod[17] = 18.0/17.0;
     682      118990 :   if ( (p%19)==1 ) tormod[19] = 20.0/19.0;
     683      118990 :   if ( (p%23)==1 ) tormod[23] = 24.0/23.0;
     684      118990 :   if ( (p%29)==1 ) tormod[29] = 30.0/29.0;
     685      118990 :   if ( (p%31)==1 ) tormod[31] = 32.0/31.0;
     686             : 
     687      118990 :   n1 = p+1-t;
     688      118990 :   n2 = p+1+t;
     689      118990 :   b12 = -1;
     690      118990 :   b1  = torsion_constraint(torctab, ltorc, tormod, n1, n1);
     691      118990 :   b2  = torsion_constraint(torctab, ltorc, tormod, n2, n2);
     692      118990 :   b12 = torsion_constraint(torctab, ltorc, tormod, n1, n2);
     693      118990 :   if ( b1 > b2 ) {
     694       54364 :     if ( torctab[b2].rating / TWIST_DOUBLE_RATIO > torctab[b12].rating )
     695       14513 :       *ptwist = 3;
     696             :     else
     697       39851 :       *ptwist = 2;
     698             :   } else
     699       64626 :     if ( torctab[b1].rating / TWIST_DOUBLE_RATIO > torctab[b12].rating )
     700       21131 :       *ptwist = 3;
     701             :     else
     702       43495 :       *ptwist = 1;
     703      118990 :   b = *ptwist ==1 ? b1: *ptwist ==2 ? b2: b12;
     704      118990 :   *ptor = torctab[b].N; *ps2 = torctab[b].s2_flag; *pt3 = torctab[b].t3_flag;
     705      118990 : }
     706             : 
     707             : /* This is Sutherland 2009 Algorithm 1.1 */
     708             : static long
     709      118997 : find_j_inv_with_given_trace(
     710             :   ulong *j_t, norm_eqn_t ne, long rho_inv, long max_curves)
     711             : {
     712      118997 :   pari_sp ltop = avma, av;
     713      118997 :   long curves_tested = 0, batch_size;
     714             :   long N0, N1, hasse[2];
     715             :   GEN n0, n1;
     716      118997 :   long i, found = 0;
     717      118997 :   ulong p = ne->p, pi = ne->pi;
     718      118997 :   long t = ne->t;
     719      118997 :   ulong p1 = p + 1, a4, a6, m, N;
     720             :   GEN A4, A6, tx, ty;
     721             :   int s2_flag, t3_flag, twist;
     722             : 
     723      118997 :   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      118990 :   N0 = (long)p1 - t; n0 = factoru(N0);
     729      118990 :   N1 = (long)p1 + t; n1 = factoru(N1);
     730             : 
     731      118990 :   best_torsion_constraint(p, t, &twist, &m, &s2_flag, &t3_flag);
     732      118990 :   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      118990 :   batch_size = 1.0 + rho_inv / (2.0 * m);
     737      118990 :   A4 = cgetg(batch_size + 1, t_VECSMALL);
     738      118990 :   A6 = cgetg(batch_size + 1, t_VECSMALL);
     739      118990 :   tx = cgetg(batch_size + 1, t_VECSMALL);
     740      118990 :   ty = cgetg(batch_size + 1, t_VECSMALL);
     741             : 
     742      118990 :   dbg_printf(2)("  Selected torsion constraint m = %lu and batch "
     743             :                 "size = %ld\n", m, batch_size);
     744      118990 :   hasse_bounds(&hasse[0], &hasse[1], p);
     745      118990 :   av = avma;
     746      741572 :   while (!found && (max_curves <= 0 || curves_tested < max_curves))
     747             :   {
     748             :     GEN Pp1, Pt;
     749     1007184 :     random_curves_with_m_torsion((ulong *)(A4 + 1), (ulong *)(A6 + 1),
     750      503592 :                                  (ulong *)(tx + 1), (ulong *)(ty + 1),
     751             :                                  batch_size, m, p);
     752      503592 :     Pp1 = random_FleV(A4, A6, p, pi);
     753      503592 :     Pt = gcopy(Pp1);
     754      503592 :     FleV_mulu_pre_inplace(Pp1, N, A4, p, pi);
     755      503592 :     if (twist >= 3) FleV_mulu_pre_inplace(Pt, t, A4,  p, pi);
     756     2590942 :     for (i = 1; i <= batch_size; ++i) {
     757     2206340 :       ++curves_tested;
     758     2206340 :       a4 = A4[i];
     759     2206340 :       a6 = A6[i]; if (a4 == 0 || a6 == 0) continue;
     760             : 
     761     2205098 :       if (( (twist >= 3 && mael(Pp1,i,1) == mael(Pt,i,1))
     762     2165709 :          || (twist < 3 && umael(Pp1,i,1) == p))
     763      140080 :           && test_curve_order(ne, a4, a6, N0, N1, n0, n1, hasse)) {
     764      118990 :         *j_t = Fl_ellj_pre(a4, a6, p, pi);
     765      118990 :         found = 1; break;
     766             :       }
     767             :     }
     768      503592 :     avma = av;
     769             :   }
     770      118990 :   avma = ltop; return curves_tested;
     771             : }
     772             : 
     773             : /*
     774             :  * SECTION: Functions for dealing with polycyclic presentations.
     775             :  */
     776             : 
     777             : static GEN
     778        4750 : next_generator(GEN DD, long D, ulong u, long filter, GEN *genred, long *P)
     779             : {
     780        4750 :   pari_sp av = avma;
     781        4750 :   ulong p = (ulong)*P;
     782             :   while (1)
     783             :   {
     784       10984 :     p = unextprime(p + 1);
     785        7867 :     if (p > LONG_MAX) pari_err_BUG("next_generator");
     786        7867 :     if (kross(D, (long)p) != -1 && u % p != 0 && filter % p != 0)
     787             :     {
     788        4750 :       GEN gen = primeform_u(DD, p);
     789             :       /* If gen is in the principal class, skip it */
     790        4750 :       *genred = redimag(gen);
     791        9500 :       if (!equali1(gel(*genred,1))) { *P = (long)p; return gen; }
     792           0 :       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       13301 : evec_ri(const long r[], long i)
     803       13301 : { 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       20161 : evec_reduce(long e[], const long n[], const long r[], long k)
     809             : {
     810             :   long i, j, q;
     811             :   const long *ri;
     812       20161 :   if (!k) return;
     813       48614 :   for (i = k - 1; i > 0; i--) {
     814       28453 :     if (e[i] >= n[i]) {
     815        8573 :       q = e[i] / n[i];
     816        8573 :       ri = evec_ri(r, i);
     817        8573 :       for (j = 0; j < i; j++) e[j] += q * ri[j];
     818        8573 :       e[i] -= q * n[i];
     819             :     }
     820             :   }
     821       20161 :   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         518 :     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       10542 :   for (i = 1; i < k; i++) index += e[i] * m[i - 1];
     843       10542 :   return index;
     844             : }
     845             : 
     846             : INLINE void
     847       21956 : evec_copy(long f[], const long e[], long k)
     848             : {
     849             :   long i;
     850       21956 :   for (i = 0; i < k; ++i) f[i] = e[i];
     851       21956 : }
     852             : 
     853             : INLINE void
     854        4242 : evec_clear(long e[], long k)
     855             : {
     856             :   long i;
     857        4242 :   for (i = 0; i < k; ++i) e[i] = 0;
     858        4242 : }
     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 :   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         735 :   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        4638 : evec_order(const long e[], const long n[], const long r[], long k)
     903             : {
     904        4638 :   pari_sp av = avma;
     905        4638 :   long *f = new_chunk(k);
     906             :   long i, j, o, m;
     907             : 
     908        4638 :   evec_copy(f, e, k);
     909       11916 :   for (o = 1, i = k - 1; i >= 0; i--) if (f[i])
     910             :   {
     911        5440 :     m = n[i] / ugcd(f[i], n[i]);
     912        5440 :     for (j = 0; j < k; j++) f[j] *= m;
     913        5440 :     evec_reduce(f, n, r, k);
     914        5440 :     o *= m;
     915             :   }
     916        4638 :   avma = av; return o;
     917             : }
     918             : 
     919             : /* Computes orders o[] for each generator using relative orders n[]
     920             :  * and power relations r[] */
     921             : INLINE void
     922        3647 : evec_orders(long o[], const long n[], const long r[], long k)
     923             : {
     924        3647 :   pari_sp av = avma;
     925        3647 :   long i, *e = new_chunk(k);
     926             : 
     927        3647 :   evec_clear(e, k);
     928        8285 :   for (i = 0; i < k; i++) {
     929        4638 :     e[i] = 1;
     930        4638 :     if (i) e[i - 1] = 0;
     931        4638 :     o[i] = evec_order(e, n, r, k);
     932             :   }
     933        3647 :   avma = av;
     934        3647 : }
     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        3647 : evec_n_to_m(long m[], const long n[], long k)
     958             : {
     959             :   long i;
     960        3647 :   m[0] = n[0];
     961        3647 :   for (i = 1; i < k; ++i) m[i] = m[i - 1] * n[i];
     962        3647 : }
     963             : 
     964             : 
     965             : /* Based on logfac() in Sutherland's classpoly package.
     966             :  * Ramanujan approximation to log(n!), accurate to O(1/n^3) */
     967             : INLINE double
     968           0 : logfac(long n)
     969             : {
     970           0 :   const double HALFLOGPI = 0.57236494292470008707171367567653;
     971           0 :   return n * log((double) n) - (double) n +
     972           0 :     log((double) n * (1.0 + 4.0 * n * (1.0 + 2.0 * n))) / 6.0 +
     973             :     HALFLOGPI;
     974             : }
     975             : 
     976             : /* This is based on Sutherland 2009, Lemma 8 (p31). */
     977             : static double
     978        3760 : upper_bound_on_classpoly_coeffs(long D, long h, GEN qfinorms)
     979             : {
     980        3760 :   const double LOG2E = 1.44269504088896340735992468100189;
     981        3760 :   pari_sp ltop = avma;
     982        3760 :   GEN C = dbltor(2114.567);
     983             :   double Mk, m, logbinom;
     984        3760 :   GEN tmp = mulrr(mppi(LOWDEFAULTPREC), sqrtr(stor(-D, LOWDEFAULTPREC)));
     985             :   /* We treat this case separately since the table is not initialised when
     986             :    * h = 1. This is the same as in the for loop below but with ak = 1. */
     987        3760 :   double log2Mk = dbllog2r(mpadd(mpexp(tmp), C));
     988        3760 :   double res = log2Mk;
     989        3760 :   ulong maxak = 1;
     990        3760 :   double log2Mh = log2Mk;
     991             : 
     992        3760 :   pari_sp btop = avma;
     993             :   long k;
     994       67423 :   for (k = 2; k <= h; ++k) {
     995       63663 :     ulong ak = uel(qfinorms, k);
     996             :     /* exp(tmp/a[k]) can overflow for even moderate discriminants, so we need
     997             :      * to use t_REALs instead of doubles.  Sutherland has a (more complicated)
     998             :      * implementation in the classpoly package which should be consulted if
     999             :      * this ever turns out to be a bottleneck.
    1000             :      *
    1001             :      * One idea to avoid t_REALs is the following: we have
    1002             :      * log(e^x + C) - x <= log(2) ~ 0.69 for x >= log(C) ~ 0.44 and
    1003             :      * the difference is basically zero for x slightly bigger than log(C).
    1004             :      * Hence for large discriminants, we have x = \pi\sqrt{-D}/ak >> log(C)
    1005             :      * and so we could approximate log(e^x + C) by x. */
    1006       63663 :     log2Mk = dbllog2r(mpadd(mpexp(divru(tmp, ak)), C));
    1007       63663 :     res += log2Mk;
    1008       63663 :     if (ak > maxak) { maxak = ak; log2Mh = log2Mk; }
    1009       63663 :     avma = btop;
    1010             :   }
    1011             : 
    1012        3760 :   Mk = pow(2.0, log2Mh);
    1013        3760 :   m = floor((h + 1)/(Mk + 1.0));
    1014             :   /* This line computes "log2(itos(binomialuu(h, m)))".  The smallest
    1015             :    * fundamental discriminant for which logbinom is not zero is
    1016             :    * -1579751. */
    1017        3760 :   logbinom = (m > 0 && m < h)
    1018           0 :     ? LOG2E * (logfac(h) - logfac(m) - logfac(h - m))
    1019        3760 :     : 0;
    1020        3760 :   avma = ltop;
    1021        3760 :   return res + logbinom - m * log2Mh + 2.0;
    1022             : }
    1023             : 
    1024             : INLINE long
    1025        1001 : distinct_inverses(const long f[], const long ef[], const long ei[],
    1026             :   const long n[], const long o[], const long r[], long k, long L0, long i)
    1027             : {
    1028        1001 :   pari_sp av = avma;
    1029             :   long j, *e2, *e3;
    1030             : 
    1031        1001 :   if ( ! ef[i] || (L0 && ef[0])) return 0;
    1032         574 :   for (j = i + 1; j < k; ++j)
    1033         455 :     if (ef[j]) break;
    1034         490 :   if (j < k) return 0;
    1035             : 
    1036         119 :   e2 = new_chunk(k);
    1037         119 :   evec_copy(e2, ef, i);
    1038         119 :   e2[i] = o[i] - ef[i];
    1039         119 :   for (j = i + 1; j < k; ++j) e2[j] = 0;
    1040         119 :   evec_reduce(e2, n, r, k);
    1041             : 
    1042         119 :   if (evec_equal(ef, e2, k)) { avma = av; return 0; }
    1043             : 
    1044         112 :   e3 = new_chunk(k);
    1045         112 :   evec_inverse_o(e3, ef, n, o, r, k);
    1046         112 :   if (evec_equal(e2, e3, k)) { avma = av; return 0; }
    1047             : 
    1048          91 :   if (f) {
    1049          14 :     evec_compose(e3, f, ei, n, r, k);
    1050          14 :     if (evec_equal(e2, e3, k)) { avma = av; return 0; }
    1051             : 
    1052          14 :     evec_inverse_o(e3, e3, n, o, r, k);
    1053          14 :     if (evec_equal(e2, e3, k)) { avma = av; return 0; }
    1054             :   }
    1055          91 :   avma = av; return 1;
    1056             : }
    1057             : 
    1058             : INLINE long
    1059        1239 : next_prime_evec(long *qq, long f[], const long m[], long k,
    1060             :   hashtable *tbl, long D, GEN DD, long u, long lvl, long ubound)
    1061             : {
    1062        1239 :   pari_sp av = avma;
    1063             :   hashentry *he;
    1064             :   GEN P;
    1065        1239 :   long idx, q = *qq;
    1066             : 
    1067        3164 :   do q = unextprime(q + 1);
    1068        3164 :   while (!(u % q) || kross(D, q) == -1 || !(lvl % q) || !(D % (q * q)));
    1069        1239 :   if (q > ubound) return 0;
    1070        1113 :   *qq = q;
    1071             : 
    1072             :   /* Get evec f corresponding to q */
    1073        1113 :   P = redimag(primeform_u(DD, q));
    1074        1113 :   he = hash_search(tbl, P);
    1075        1113 :   if (!he) pari_err_BUG("next_prime_evec");
    1076        1113 :   idx = itos((GEN) he->val);
    1077        1113 :   index_to_evec(f, idx, m, k);
    1078        1113 :   avma = av; return 1;
    1079             : }
    1080             : 
    1081             : /* Return 1 on success, 0 on failure. */
    1082             : static int
    1083         266 : orient_pcp(classgp_pcp_t G, long *ni, long D, long u, hashtable *tbl)
    1084             : {
    1085         266 :   pari_sp av = avma;
    1086             :   /* 199 seems to suffice, but can be increased if necessary */
    1087             :   enum { MAX_ORIENT_P = 199 };
    1088         266 :   const long *L = G->L, *n = G->n, *r = G->r, *m = G->m, *o = G->o;
    1089         266 :   long i, *ps = G->orient_p, *qs = G->orient_q, *reps = G->orient_reps;
    1090         266 :   long *ef, *e, *ei, *f, k = G->k, lvl = modinv_level(G->inv);
    1091         266 :   GEN DD = stoi(D);
    1092             : 
    1093         266 :   memset(ps, 0, k * sizeof(long));
    1094         266 :   memset(qs, 0, k * sizeof(long));
    1095         266 :   memset(reps, 0, k * k * sizeof(long));
    1096             : 
    1097         266 :   for (i = 0; i < k; ++i) { ps[i] = -1; if (o[i] > 2) break; }
    1098         266 :   for (++i; i < k; ++i) ps[i] = (o[i] > 2) ? 0 : -1; /* ps[i] = -!(o[i] > 2); */
    1099             : 
    1100         266 :   e = new_chunk(k);
    1101         266 :   ei = new_chunk(k);
    1102         266 :   f = new_chunk(k);
    1103             : 
    1104         686 :   for (i = 0; i < k; ++i) {
    1105             :     long p;
    1106         833 :     if (ps[i]) continue;
    1107          98 :     p = L[i];
    1108          98 :     ef = &reps[i * k];
    1109         693 :     while (!ps[i]) {
    1110         518 :       if (!next_prime_evec(&p, ef, m, k, tbl, D, DD, u, lvl, MAX_ORIENT_P))
    1111          21 :         break;
    1112         497 :       evec_inverse_o(ei, ef, n, o, r, k);
    1113         497 :       if (!distinct_inverses(NULL, ef, ei, n, o, r, k, G->L0, i)) continue;
    1114          77 :       ps[i] = p;
    1115          77 :       qs[i] = 1;
    1116             :     }
    1117          98 :     if (ps[i]) continue;
    1118             : 
    1119          21 :     p = unextprime(L[i] + 1);
    1120         154 :     while (!ps[i]) {
    1121             :       long q;
    1122             : 
    1123         119 :       if (!next_prime_evec(&p, e, m, k, tbl, D, DD, u, lvl, MAX_ORIENT_P))
    1124           7 :         break;
    1125         112 :       evec_inverse_o(ei, e, n, o, r, k);
    1126             : 
    1127         112 :       q = L[i];
    1128         728 :       while (!qs[i]) {
    1129         602 :         if (!next_prime_evec(&q, f, m, k, tbl, D, DD, u, lvl, p - 1)) break;
    1130         504 :         evec_compose(ef, e, f, n, r, k);
    1131         504 :         if (!distinct_inverses(f, ef, ei, n, o, r, k, G->L0, i)) continue;
    1132          14 :         ps[i] = p;
    1133          14 :         qs[i] = q;
    1134             :       }
    1135             :     }
    1136          21 :     if (!ps[i]) return 0;
    1137             :   }
    1138         259 :   if (ni) {
    1139         259 :     GEN N = qfb_nform(D, *ni);
    1140         259 :     hashentry *he = hash_search(tbl, N);
    1141         259 :     if (!he) pari_err_BUG("orient_pcp");
    1142         259 :     *ni = itos((GEN) he->val);
    1143             :   }
    1144         259 :   avma = av; return 1;
    1145             : }
    1146             : 
    1147             : /* We must avoid situations where L_i^{+/-2} = L_j^2 (or = L_0*L_j^2
    1148             :  * if ell0 flag is set), with |L_i| = |L_j| = 4 (or have 4th powers in
    1149             :  * <L0> but not 2nd powers in <L0>) and j < i */
    1150             : /* These cases cause problems when enumerating roots via gcds */
    1151             : /* returns the index of the first bad generator, or -1 if no bad
    1152             :  * generators are found */
    1153             : static long
    1154        3661 : classgp_pcp_check_generators(const long *n, long *r, long k, long L0)
    1155             : {
    1156        3661 :   pari_sp av = avma;
    1157             :   long *e1, i, i0, j, s;
    1158             :   const long *ei;
    1159             : 
    1160        3661 :   s = !!L0;
    1161        3661 :   e1 = new_chunk(k);
    1162             : 
    1163        4554 :   for (i = s + 1; i < k; i++) {
    1164         907 :     if (n[i] != 2) continue;
    1165         542 :     ei = evec_ri(r, i);
    1166         752 :     for (j = s; j < i; j++)
    1167         598 :       if (ei[j]) break;
    1168         542 :     if (j == i) continue;
    1169         916 :     for (i0 = s; i0 < i; i0++) {
    1170         542 :       if ((4 % n[i0])) continue;
    1171         231 :       evec_clear(e1, k);
    1172         231 :       e1[i0] = 4;
    1173         231 :       evec_reduce(e1, n, r, k);
    1174         623 :       for (j = s; j < i; j++)
    1175         434 :         if (e1[j]) break;
    1176         231 :       if (j < i) continue; /* L_i0^4 is not trivial or in <L_0> */
    1177         189 :       evec_clear(e1, k);
    1178         189 :       e1[i0] = 2;
    1179         189 :       evec_reduce(e1, n, r, k); /* compute L_i0^2 */
    1180         301 :       for (j = s; j < i; j++)
    1181         287 :         if (e1[j] != ei[j]) break;
    1182         189 :       if (j == i) return i;
    1183         175 :       evec_inverse(e1, e1, n, r, k); /* compute L_i0^{-2} */
    1184         273 :       for (j = s; j < i; j++)
    1185         273 :         if (e1[j] != ei[j]) break;
    1186         175 :       if (j == i) return i;
    1187             :     }
    1188             :   }
    1189        3647 :   avma = av; return -1;
    1190             : }
    1191             : 
    1192             : static void
    1193        3647 : pcp_alloc_and_set(
    1194             :   classgp_pcp_t G, const long *L, const long *n, const long *r, long k)
    1195             : {
    1196             :   /* classgp_pcp contains 6 arrays of length k (L, m, n, o, orient_p, orient_q),
    1197             :    * one of length binom(k, 2) (r) and one of length k^2 (orient_reps) */
    1198        3647 :   long rlen = k * (k - 1) / 2, datalen = 6 * k + rlen + k * k;
    1199        3647 :   G->_data = newblock(datalen);
    1200        3647 :   G->L = G->_data;
    1201        3647 :   G->m = G->L + k;
    1202        3647 :   G->n = G->m + k;
    1203        3647 :   G->o = G->n + k;
    1204        3647 :   G->r = G->o + k;
    1205        3647 :   G->orient_p = G->r + rlen;
    1206        3647 :   G->orient_q = G->orient_p + k;
    1207        3647 :   G->orient_reps = G->orient_q + k;
    1208        3647 :   G->k = k;
    1209             : 
    1210        3647 :   evec_copy(G->L, L, k);
    1211        3647 :   evec_copy(G->n, n, k);
    1212        3647 :   evec_copy(G->r, r, rlen);
    1213        3647 :   evec_orders(G->o, n, r, k);
    1214        3647 :   evec_n_to_m(G->m, n, k);
    1215        3647 : }
    1216             : 
    1217             : static void
    1218        3767 : classgp_pcp_clear(classgp_pcp_t G)
    1219        3767 : { if (G->_data) killblock(G->_data); }
    1220             : 
    1221             : /* This is Sutherland 2009, Algorithm 2.2 (p16). */
    1222             : static void
    1223        3760 : classgp_make_pcp(
    1224             :   classgp_pcp_t G, double *height, long *ni,
    1225             :   long h, long D, ulong u, long inv, long Lfilter, long orient)
    1226             : {
    1227             :   enum { MAX_GENS = 16, MAX_RLEN = MAX_GENS * (MAX_GENS - 1) / 2 };
    1228        3760 :   pari_sp av = avma, bv;
    1229        3760 :   long curr_p, h2, nelts, lvl = modinv_level(inv);
    1230             :   GEN DD, ident, T, v;
    1231             :   hashtable *tbl;
    1232             :   long i, L1, L2;
    1233             :   long k, L[MAX_GENS], n[MAX_GENS], r[MAX_RLEN];
    1234             : 
    1235        3760 :   memset(G, 0, sizeof *G);
    1236             : 
    1237        3760 :   G->D = D;
    1238        3760 :   G->h = h;
    1239        3760 :   G->inv = inv;
    1240        8003 :   G->L0 = (modinv_is_double_eta(inv) && modinv_ramified(D, inv))
    1241        3858 :     ? modinv_degree(NULL, NULL, inv) : 0;
    1242        3760 :   G->enum_cnt = h / (1 + !!G->L0);
    1243        3760 :   G->Lfilter = ulcm(Lfilter, lvl);
    1244             : 
    1245        3760 :   if (h == 1) {
    1246         120 :     if (G->L0) pari_err_BUG("classgp_pcp");
    1247         120 :     G->k = 0;
    1248         120 :     G->_data = NULL;
    1249         120 :     v = const_vecsmall(1, 1);
    1250         120 :     *height = upper_bound_on_classpoly_coeffs(D, h, v);
    1251             :     /* NB: No need to set *ni when h = 1 */
    1252         120 :     avma = av; return;
    1253             :   }
    1254             : 
    1255        3640 :   DD = stoi(D);
    1256        3640 :   bv = avma;
    1257             :   while (1) {
    1258        3682 :     k = 0;
    1259             :     /* Hash table has a QFI as a key and the (boxed) index of that QFI
    1260             :      * in T as its value */
    1261        3661 :     tbl = hash_create(h, (ulong(*)(void*)) hash_GEN,
    1262             :                          (int(*)(void*,void*))&gequal, 1);
    1263        3661 :     ident = redimag(primeform_u(DD, 1));
    1264        3661 :     hash_insert(tbl, ident, gen_0);
    1265             : 
    1266        3661 :     T = vectrunc_init(h + 1);
    1267        3661 :     vectrunc_append(T, ident);
    1268        3661 :     nelts = 1;
    1269        3661 :     curr_p = 1;
    1270             : 
    1271       12170 :     while (nelts < h) {
    1272             :       GEN gamma_i, beta;
    1273             :       hashentry *e;
    1274        4848 :       long N = glength(T), Tlen = N, ri = 1;
    1275             : 
    1276        4848 :       if (k == MAX_GENS) pari_err_IMPL("classgp_pcp");
    1277             : 
    1278        4848 :       if (nelts == 1 && G->L0) {
    1279          98 :         curr_p = G->L0;
    1280          98 :         gamma_i = qfb_nform(D, curr_p);
    1281          98 :         beta = redimag(gamma_i);
    1282         196 :         if (equali1(gel(beta, 1)))
    1283             :         {
    1284           0 :           curr_p = 1;
    1285           0 :           gamma_i = next_generator(DD, D, u, G->Lfilter, &beta, &curr_p);
    1286             :         }
    1287             :       } else
    1288        4750 :         gamma_i = next_generator(DD, D, u, G->Lfilter, &beta, &curr_p);
    1289       61282 :       while ((e = hash_search(tbl, beta)) == NULL) {
    1290             :         long j;
    1291      118588 :         for (j = 1; j <= N; ++j) {
    1292       67002 :           GEN t = qficomp(beta, gel(T, j));
    1293       67002 :           vectrunc_append(T, t);
    1294       67002 :           hash_insert(tbl, t, stoi(Tlen++));
    1295             :         }
    1296       51586 :         beta = qficomp(beta, gamma_i);
    1297       51586 :         ++ri;
    1298             :       }
    1299        4848 :       if (ri > 1) {
    1300             :         long j, si;
    1301        4666 :         L[k] = curr_p;
    1302        4666 :         n[k] = ri;
    1303        4666 :         nelts *= ri;
    1304             : 
    1305             :         /* This is to reset the curr_p counter when we have G->L0 != 0
    1306             :          * in the first position of L. */
    1307        4666 :         if (curr_p == G->L0) curr_p = 1;
    1308             : 
    1309        4666 :         N = 1;
    1310        4666 :         si = itos((GEN) e->val);
    1311        6000 :         for (j = 0; j < k; ++j) {
    1312        1334 :           evec_ri_mutate(r, k)[j] = (si / N) % n[j];
    1313        1334 :           N *= n[j];
    1314             :         }
    1315        4666 :         ++k;
    1316             :       }
    1317             :     }
    1318             : 
    1319        3661 :     if ((i = classgp_pcp_check_generators(n, r, k, G->L0)) < 0) {
    1320        3647 :       pcp_alloc_and_set(G, L, n, r, k);
    1321        3647 :       if (!orient || orient_pcp(G, ni, D, u, tbl)) break;
    1322           7 :       G->Lfilter *= G->L[0];
    1323           7 :       classgp_pcp_clear(G);
    1324          14 :     } else if (log2(G->Lfilter) + log2(L[i]) >= BITS_IN_LONG)
    1325           0 :       pari_err_IMPL("classgp_pcp");
    1326             :     else
    1327          14 :       G->Lfilter *= L[i];
    1328          21 :     avma = bv;
    1329             :   }
    1330             : 
    1331        3640 :   v = cgetg(h + 1, t_VECSMALL);
    1332        3640 :   v[1] = 1;
    1333        3640 :   for (i = 2; i <= h; ++i) uel(v,i) = itou(gmael(T,i,1));
    1334             : 
    1335        3640 :   h2 = G->L0 ? h / 2 : h;
    1336        3640 :   *height = upper_bound_on_classpoly_coeffs(D, h2, v);
    1337             : 
    1338             :   /* The norms of the last one or two generators. */
    1339        3640 :   L1 = L[k - 1];
    1340        3640 :   L2 = k > 1 ? L[k - 2] : 1;
    1341             :   /* 4 * L1^2 * L2^2 must fit in a ulong */
    1342        3640 :   if (2 * (1 + log2(L1) + log2(L2)) >= BITS_IN_LONG)
    1343           0 :     pari_err_IMPL("classgp_pcp");
    1344             : 
    1345        3640 :   if (G->L0 && (G->L[0] != G->L0 || G->o[0] != 2))
    1346           0 :     pari_err_BUG("classgp_pcp");
    1347             : 
    1348        3640 :   avma = av; return;
    1349             : }
    1350             : 
    1351             : INLINE ulong
    1352        3760 : classno_wrapper(long D)
    1353             : {
    1354        3760 :   pari_sp av = avma;
    1355        3760 :   GEN clsgp = quadclassunit0(stoi(D), 0, NULL, DEFAULTPREC);
    1356        3760 :   ulong h = itou(gel(clsgp, 1));
    1357        3760 :   avma = av; return h;
    1358             : }
    1359             : 
    1360             : /*
    1361             :  * SECTION: Functions for calculating class polynomials.
    1362             :  */
    1363             : 
    1364             : /* NB: Sutherland defines V_MAX to be 1200 with saying why. */
    1365             : #define V_MAX 1200
    1366             : 
    1367             : #define NSMALL_PRIMES 11
    1368             : static const long SMALL_PRIMES[11] = {
    1369             :   2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31
    1370             : };
    1371             : 
    1372             : static long
    1373       45913 : is_smooth_enough(ulong *factors, long v)
    1374             : {
    1375             :   long i;
    1376       45913 :   *factors = 0;
    1377      115282 :   for (i = 0; i < NSMALL_PRIMES; ++i) {
    1378      115275 :     long p = SMALL_PRIMES[i];
    1379      115275 :     if (v % p == 0) *factors |= 1UL << i;
    1380      115275 :     while (v % p == 0) v /= p;
    1381      115275 :     if (v == 1) break;
    1382             :   }
    1383       45913 :   return v == 1;
    1384             : }
    1385             : 
    1386             : /* Hurwitz class number of |D| assuming hclassno() and attached
    1387             :  * conversion to double costs much more than unegisfundamental(). */
    1388             : INLINE double
    1389       49666 : hclassno_wrapper(long D, long h)
    1390             : {
    1391             :   /* TODO: Can probably calculate hurwitz faster using -D, factor(u)
    1392             :    * and classno(D). */
    1393       49666 :   pari_sp av = avma;
    1394       49666 :   ulong abs_D = D < 0 ? -D : D;
    1395             :   double hurwitz;
    1396             : 
    1397       49666 :   if (h && unegisfundamental(abs_D))
    1398        3585 :     hurwitz = (double) h;
    1399             :   else
    1400       46081 :     hurwitz = rtodbl(gtofp(hclassno(utoi(abs_D)), DEFAULTPREC));
    1401       49666 :   avma = av; return hurwitz;
    1402             : }
    1403             : 
    1404             : 
    1405             : /* This is Sutherland 2009, Algorithm 2.1 (p8).
    1406             :  * NB: This function is not gerepileupto-safe. */
    1407             : static GEN
    1408        3760 : select_classpoly_prime_pool(
    1409             :   double min_prime_bits, double delta, classgp_pcp_t G)
    1410             : {
    1411             :   pari_sp av;
    1412        3760 :   double prime_bits = 0.0, hurwitz, z;
    1413             :   ulong i;
    1414             :   /* t_min[v] will hold the lower bound of the t we need to look at
    1415             :    * for a given v. */
    1416             :   ulong t_min[V_MAX], t_size_lim;
    1417             :   GEN res;
    1418        3760 :   long D = G->D, inv = G->inv;
    1419             : 
    1420        3760 :   if (delta <= 0) pari_err_BUG("select_suitable_primes");
    1421        3760 :   hurwitz = hclassno_wrapper(D, G->h);
    1422             : 
    1423        3760 :   res = cgetg(1, t_VEC);
    1424             :   /* Initialise t_min to be all 2's.  This avoids trace 0 and trace 1 curves */
    1425        3760 :   for (i = 0; i < V_MAX; ++i) t_min[i] = 2;
    1426             : 
    1427             :   /* maximum possible trace = sqrt(2^BIL - D) */
    1428        3760 :   t_size_lim = 2.0 * sqrt((double)((1UL << (BITS_IN_LONG - 2)) - (((ulong)-D) >> 2)));
    1429             : 
    1430        3760 :   av = avma;
    1431        3767 :   for (z = -D / (2.0 * hurwitz); ; z *= delta + 1.0) {
    1432             :     /* v_bound_aux = -4 z H(-D). */
    1433        3767 :     double v_bound_aux = -4.0 * z * hurwitz;
    1434             :     ulong v;
    1435        3767 :     dbg_printf(1)("z = %.2f\n", z);
    1436       45920 :     for (v = 1; ; ++v) {
    1437       45920 :       ulong pcount = 0, t, t_max, vfactors;
    1438       45920 :       ulong m_vsqr_D = v * v * (ulong)(-D);
    1439             :       /* hurwitz_ratio_bound = 11 * log(log(v + 4))^2 */
    1440       45920 :       double hurwitz_ratio_bound = log(log(v + 4.0)), max_p, H;
    1441       45920 :       hurwitz_ratio_bound *= 11.0 * hurwitz_ratio_bound;
    1442             : 
    1443       45920 :       if (v >= v_bound_aux * hurwitz_ratio_bound / D || v >= V_MAX) break;
    1444             : 
    1445       45913 :       if ( ! is_smooth_enough(&vfactors, v)) continue;
    1446       45906 :       H = hclassno_wrapper(m_vsqr_D, 0);
    1447             : 
    1448             :       /* t <= 2 sqrt(p) and p <= z H(-v^2 D) and
    1449             :        *
    1450             :        *   H(-v^2 D) < vH(-D) (11 log(log(v + 4))^2)
    1451             :        *
    1452             :        * This last term is v * hurwitz * hurwitz_ratio_bound. */
    1453       45906 :       max_p = z * v * hurwitz * hurwitz_ratio_bound;
    1454       45906 :       t_max = 2.0 * mindd(sqrt((double)((1UL << (BITS_IN_LONG - 2)) - (m_vsqr_D >> 2))),
    1455             :                           sqrt(max_p));
    1456    13382988 :       for (t = t_min[v]; t <= t_max; ++t) {
    1457    13337082 :         ulong possible_4p = t * t + m_vsqr_D;
    1458    13337082 :         if (possible_4p % 4 == 0) {
    1459     6669722 :           ulong possible_p = possible_4p / 4;
    1460     6669722 :           if (uisprime(possible_p) && modinv_good_prime(inv, possible_p)) {
    1461      242703 :             long p = possible_p;
    1462      242703 :             double rho_inv = p / H;
    1463             :             GEN hit;
    1464             : 
    1465      242703 :             hit = mkvecsmall5(p, t, v, (long)rho_inv, vfactors);
    1466             :             /* FIXME: Avoid doing GC for every prime as here. */
    1467      242703 :             res = gerepileupto(av, gconcat(res, hit));
    1468      242703 :             prime_bits += log2(p);
    1469      242703 :             ++pcount;
    1470             :           }
    1471             :         }
    1472             :       }
    1473       45906 :       t_min[v] = t_max + 1;
    1474             : 
    1475       45906 :       if (pcount) {
    1476       23289 :         dbg_printf(2)("  Found %lu primes for v = %lu.\n", pcount, v);
    1477       23289 :         if (gc_needed(av, 2))
    1478           0 :           res = gerepilecopy(av, res);
    1479             :       }
    1480       45906 :       if (prime_bits > min_prime_bits) {
    1481        3760 :         dbg_printf(1)("Found %ld primes; total size %.2f bits.\n",
    1482             :                     glength(res), prime_bits);
    1483        7520 :         return gerepilecopy(av, res);
    1484             :       }
    1485             :     }
    1486             :     /* Have we exhausted all possible solutions that fit in machine words? */
    1487           7 :     if (t_min[1] >= t_size_lim) {
    1488           0 :       char *err = stack_sprintf("class polynomial of discriminant %ld", D);
    1489           0 :       pari_err(e_ARCH, err);
    1490             :     }
    1491             :   }
    1492             : }
    1493             : 
    1494             : INLINE int
    1495     1057255 : cmp_small(long a, long b)
    1496     1057255 : { return a>b? 1: (a<b? -1: 0); }
    1497             : 
    1498             : static int
    1499     1057255 : primecmp(void *data, GEN v1, GEN v2)
    1500     1057255 : { (void)data; return cmp_small(v1[4], v2[4]); }
    1501             : 
    1502             : 
    1503             : static long
    1504        3760 : height_margin(long inv, long D)
    1505             : {
    1506             :   (void)D;
    1507             :   /* NB: avs just uses a height margin of 256 for everyone and everything. */
    1508        3760 :   if (inv == INV_F) return 64;  /* checked for discriminants up to -350000 */
    1509        3648 :   if (inv == INV_G2) return 5;
    1510        3431 :   if (inv != INV_J) return 256; /* TODO: This should be made more accurate */
    1511        2927 :   return 0;
    1512             : }
    1513             : 
    1514             : static GEN
    1515        3760 : select_classpoly_primes(
    1516             :   ulong *vfactors, ulong *biggest_v,
    1517             :   long k, double delta, classgp_pcp_t G, double height)
    1518             : {
    1519        3760 :   pari_sp av = avma;
    1520        3760 :   long i, s, D = G->D, inv = G->inv;
    1521             :   ulong biggest_p;
    1522             :   double prime_bits, min_prime_bits, b;
    1523             :   GEN prime_pool;
    1524             : 
    1525        3760 :   if (k < 2) pari_err_BUG("select_suitable_primes");
    1526             : 
    1527        3760 :   s = modinv_height_factor(inv);
    1528        3760 :   b = height / s + height_margin(inv, D);
    1529        3760 :   dbg_printf(1)("adjusted height = %.2f\n", b);
    1530        3760 :   min_prime_bits = k * b;
    1531             : 
    1532        3760 :   prime_pool = select_classpoly_prime_pool(min_prime_bits, delta, G);
    1533             : 
    1534             :   /* FIXME: Apply torsion constraints */
    1535             :   /* FIXME: Rank elts of res according to cost/benefit ratio */
    1536        3760 :   gen_sort_inplace(prime_pool, NULL, primecmp, NULL);
    1537             : 
    1538        3760 :   prime_bits = 0.0;
    1539        3760 :   biggest_p = gel(prime_pool, 1)[1];
    1540        3760 :   *biggest_v = gel(prime_pool, 1)[3];
    1541        3760 :   *vfactors = 0;
    1542      118389 :   for (i = 1; i < lg(prime_pool); ++i) {
    1543      118389 :     ulong p = gel(prime_pool, i)[1];
    1544      118389 :     ulong v = gel(prime_pool, i)[3];
    1545      118389 :     prime_bits += log2(p);
    1546      118389 :     *vfactors |= gel(prime_pool, i)[5];
    1547      118389 :     if (p > biggest_p) biggest_p = p;
    1548      118389 :     if (v > *biggest_v) *biggest_v = v;
    1549      118389 :     if (prime_bits > b) break;
    1550             :   }
    1551        3760 :   dbg_printf(1)("Selected %ld primes; largest is %lu ~ 2^%.2f\n",
    1552             :              i, biggest_p, log2(biggest_p));
    1553        3760 :   return gerepilecopy(av, vecslice0(prime_pool, 1, i));
    1554             : }
    1555             : 
    1556             : /* This is Sutherland 2009 Algorithm 1.2. */
    1557             : static long
    1558      118997 : oneroot_of_classpoly(
    1559             :   ulong *j_endo, int *endo_cert, ulong j, norm_eqn_t ne, GEN jdb)
    1560             : {
    1561      118997 :   pari_sp av = avma;
    1562             :   long nfactors, L_bound, i;
    1563      118997 :   ulong p = ne->p, pi = ne->pi;
    1564             :   GEN factw, factors, u_levels, vdepths;
    1565             : 
    1566      118997 :   if (j == 0 || j == 1728 % p) pari_err_BUG("oneroot_of_classpoly");
    1567             : 
    1568      118997 :   *endo_cert = 1;
    1569      118997 :   if (ne->u * ne->v == 1) { *j_endo = j; return 1; }
    1570             : 
    1571             :   /* TODO: Precalculate all this data further up */
    1572      116225 :   factw = factoru(ne->u * ne->v);
    1573      116225 :   factors = gel(factw, 1);
    1574      116225 :   nfactors = lg(factors) - 1;
    1575      116225 :   u_levels = cgetg(nfactors + 1, t_VECSMALL);
    1576      299053 :   for (i = 1; i <= nfactors; ++i)
    1577      182828 :     u_levels[i] = z_lval(ne->u, gel(factw, 1)[i]);
    1578      116225 :   vdepths = gel(factw, 2);
    1579             : 
    1580             :   /* FIXME: This should be bigger */
    1581      116225 :   L_bound = maxdd(log((double) -ne->D), (double)ne->v);
    1582             : 
    1583             :   /* Iterate over the primes L dividing w */
    1584      295141 :   for (i = 1; i <= nfactors; ++i) {
    1585      182828 :     pari_sp bv = avma;
    1586             :     GEN phi;
    1587      182828 :     long jlvl, lvl_diff, depth = vdepths[i];
    1588      182828 :     long L = factors[i];
    1589      182828 :     if (L > L_bound) { *endo_cert = 0; break; }
    1590             : 
    1591      178916 :     phi = polmodular_db_getp(jdb, L, p);
    1592             : 
    1593             :     /* TODO: See if I can reuse paths created in j_level_in_volcano()
    1594             :      * later in {ascend,descend}_volcano(), perhaps by combining the
    1595             :      * functions into one "adjust_level" function. */
    1596      178916 :     jlvl = j_level_in_volcano(phi, j, p, pi, L, depth);
    1597      178916 :     lvl_diff = u_levels[i] - jlvl;
    1598             : 
    1599      178916 :     if (lvl_diff < 0)
    1600             :       /* j's level is less than v(u) so we must ascend */
    1601      111026 :       j = ascend_volcano(phi, j, p, pi, jlvl, L, depth, -lvl_diff);
    1602       67890 :     else if (lvl_diff > 0)
    1603             :       /* otherwise j's level is greater than v(u) so we descend */
    1604         774 :       j = descend_volcano(phi, j, p, pi, jlvl, L, depth, lvl_diff);
    1605      178916 :     avma = bv;
    1606             :   }
    1607      116225 :   avma = av;
    1608             :   /* At this point the probability that j has the wrong endomorphism
    1609             :    * ring is about \sum_{p|u_compl} 1/p (and u_compl must be bigger
    1610             :    * than L_bound, so pretty big), so just return it and rely on
    1611             :    * detection code in enum_j_with_endo_ring().  Detection is that we
    1612             :    * hit a previously found j-invariant earlier than expected.  OR, we
    1613             :    * evaluate class polynomials of the suborders at j and if any are
    1614             :    * zero then j must be chosen again.  */
    1615      116225 :   *j_endo = j;
    1616      116225 :   return j != 0 && j != 1728 % p;
    1617             : }
    1618             : 
    1619             : INLINE long
    1620        3304 : vecsmall_isin_skip(GEN v, long x, long k)
    1621             : {
    1622        3304 :   long i, l = lg(v);
    1623      117467 :   for (i = k; i < l; ++i)
    1624      114163 :     if (v[i] == x) return i;
    1625        3304 :   return 0;
    1626             : }
    1627             : 
    1628             : INLINE ulong
    1629      237142 : select_twisting_param(ulong p)
    1630             : {
    1631             :   ulong T;
    1632      237142 :   do T = random_Fl(p); while (krouu(T, p) != -1);
    1633      118389 :   return T;
    1634             : }
    1635             : 
    1636             : INLINE void
    1637      118389 : setup_norm_eqn(norm_eqn_t ne, long D, long u, GEN norm_eqn)
    1638             : {
    1639      118389 :   ne->D = D;
    1640      118389 :   ne->u = u;
    1641      118389 :   ne->t = norm_eqn[2];
    1642      118389 :   ne->v = norm_eqn[3];
    1643      118389 :   ne->p = (ulong) norm_eqn[1];
    1644      118389 :   ne->pi = get_Fl_red(ne->p);
    1645      118389 :   ne->s2 = Fl_2gener_pre(ne->p, ne->pi);
    1646      118389 :   ne->T = select_twisting_param(ne->p);
    1647      118389 : }
    1648             : 
    1649             : INLINE ulong
    1650       15302 : Flv_powsum_pre(GEN v, ulong n, ulong p, ulong pi)
    1651             : {
    1652       15302 :   long i, l = lg(v);
    1653       15302 :   ulong psum = 0;
    1654      372134 :   for (i = 1; i < l; ++i)
    1655      356832 :     psum = Fl_add(psum, Fl_powu_pre(uel(v,i), n, p, pi), p);
    1656       15302 :   return psum;
    1657             : }
    1658             : 
    1659             : INLINE int
    1660        3760 : modinv_has_sign_ambiguity(long inv)
    1661             : {
    1662        3760 :   switch (inv) {
    1663             :   case INV_F:
    1664             :   case INV_F3:
    1665             :   case INV_W2W3E2:
    1666             :   case INV_W2W7E2:
    1667             :   case INV_W2W3:
    1668             :   case INV_W2W5:
    1669             :   case INV_W2W7:
    1670             :   case INV_W3W3:
    1671             :   case INV_W2W13:
    1672         525 :   case INV_W3W7: return 1;
    1673             :   }
    1674        3235 :   return 0;
    1675             : }
    1676             : 
    1677             : INLINE int
    1678        3276 : modinv_units(int inv)
    1679        3276 : { return modinv_is_double_eta(inv) || modinv_is_Weber(inv); }
    1680             : 
    1681             : INLINE int
    1682       10318 : adjust_signs(GEN js, ulong p, ulong pi, long inv, GEN T, long e)
    1683             : {
    1684       10318 :   long negate = 0;
    1685       10318 :   long h = lg(js) - 1;
    1686       13391 :   if ((h & 1) && modinv_units(inv)) {
    1687        3073 :     ulong prod = Flv_prod_pre(js, p, pi);
    1688        3073 :     if (prod != p - 1) {
    1689        1583 :       if (prod != 1) pari_err_BUG("adjust_signs: constant term is not +/-1");
    1690        1583 :       negate = 1;
    1691             :     }
    1692             :   } else {
    1693             :     ulong tp, t;
    1694        7245 :     tp = umodiu(T, p);
    1695        7245 :     t = Flv_powsum_pre(js, e, p, pi);
    1696        7245 :     if (t == 0) return 0;
    1697        7231 :     if (t != tp) {
    1698        3536 :       if (Fl_neg(t, p) != tp) pari_err_BUG("adjust_signs: incorrect trace");
    1699        3536 :       negate = 1;
    1700             :     }
    1701             :   }
    1702       10304 :   if (negate) Flv_neg_inplace(js, p);
    1703       10304 :   return 1;
    1704             : }
    1705             : 
    1706             : static ulong
    1707      118926 : find_jinv(
    1708             :   long *trace_tries, long *endo_tries, int *cert,
    1709             :   norm_eqn_t ne, long inv, long rho_inv, GEN jdb)
    1710             : {
    1711      118926 :   long found, ok = 1;
    1712             :   ulong j, r;
    1713             :   do {
    1714             :     do {
    1715             :       long tries;
    1716      118997 :       ulong j_t = 0;
    1717             :       /* TODO: Set batch size according to expected number of tries and
    1718             :        * experimental cost/benefit analysis. */
    1719      118997 :       tries = find_j_inv_with_given_trace(&j_t, ne, rho_inv, 0);
    1720      118997 :       if (j_t == 0)
    1721           0 :         pari_err_BUG("polclass0: Couldn't find j-invariant with given trace.");
    1722      118997 :       dbg_printf(2)("  j-invariant %ld has trace +/-%ld (%ld tries, 1/rho = %ld)\n",
    1723             :           j_t, ne->t, tries, rho_inv);
    1724      118997 :       *trace_tries += tries;
    1725             : 
    1726      118997 :       found = oneroot_of_classpoly(&j, cert, j_t, ne, jdb);
    1727      118997 :       ++*endo_tries;
    1728      118997 :     } while (!found);
    1729             : 
    1730      118997 :     if (modinv_is_double_eta(inv))
    1731       11718 :       ok = modfn_unambiguous_root(&r, inv, j, ne, jdb);
    1732             :     else
    1733      107279 :       r = modfn_root(j, ne, inv);
    1734      118997 :   } while (!ok);
    1735      118926 :   return r;
    1736             : }
    1737             : 
    1738             : static GEN
    1739      118389 : polclass_roots_modp(
    1740             :   long *n_trace_curves,
    1741             :   norm_eqn_t ne, long rho_inv, classgp_pcp_t G, GEN db)
    1742             : {
    1743      118389 :   pari_sp av = avma;
    1744      118389 :   ulong j = 0;
    1745      118389 :   long inv = G->inv, endo_tries = 0;
    1746             :   int endo_cert;
    1747             :   GEN res, jdb, fdb;
    1748             : 
    1749      118389 :   jdb = polmodular_db_for_inv(db, INV_J);
    1750      118389 :   fdb = polmodular_db_for_inv(db, inv);
    1751             : 
    1752      118389 :   dbg_printf(2)("p = %ld, t = %ld, v = %ld\n", ne->p, ne->t, ne->v);
    1753             : 
    1754             :   do {
    1755      118926 :     j = find_jinv(n_trace_curves, &endo_tries, &endo_cert, ne, inv, rho_inv, jdb);
    1756             : 
    1757      118926 :     res = enum_roots(j, ne, fdb, G);
    1758      118926 :     if ( ! res && endo_cert) pari_err_BUG("polclass_roots_modp");
    1759      118926 :     if (res && ! endo_cert && vecsmall_isin_skip(res, res[1], 2))
    1760             :     {
    1761           0 :       avma = av;
    1762           0 :       res = NULL;
    1763             :     }
    1764      118926 :   } while (!res);
    1765             : 
    1766      118389 :   dbg_printf(2)("  j-invariant %ld has correct endomorphism ring "
    1767             :              "(%ld tries)\n", j, endo_tries);
    1768      118389 :   dbg_printf(4)("  all such j-invariants: %Ps\n", res);
    1769      118389 :   return gerepileupto(av, res);
    1770             : }
    1771             : 
    1772             : INLINE int
    1773        2134 : modinv_inverted_involution(long inv)
    1774        2134 : { return modinv_is_double_eta(inv); }
    1775             : 
    1776             : INLINE int
    1777        2134 : modinv_negated_involution(long inv)
    1778             : { /* determined by trial and error */
    1779        2134 :   return inv == INV_F || inv == INV_W3W5 || inv == INV_W3W7
    1780        4007 :     || inv == INV_W3W3 || inv == INV_W5W7;
    1781             : }
    1782             : 
    1783             : /* Return true iff Phi_L(j0, j1) = 0. */
    1784             : INLINE long
    1785        3962 : verify_edge(ulong j0, ulong j1, ulong p, ulong pi, long L, GEN fdb)
    1786             : {
    1787        3962 :   pari_sp av = avma;
    1788        3962 :   GEN phi = polmodular_db_getp(fdb, L, p);
    1789        3962 :   GEN f = Flm_Fl_polmodular_evalx(phi, L, j1, p, pi);
    1790        3962 :   ulong r = Flx_eval_pre(f, j0, p, pi);
    1791        3962 :   avma = av; return !r;
    1792             : }
    1793             : 
    1794             : INLINE long
    1795         672 : verify_2path(
    1796             :   ulong j1, ulong j2, ulong p, ulong pi, long L1, long L2, GEN fdb)
    1797             : {
    1798         672 :   pari_sp av = avma;
    1799         672 :   GEN phi1 = polmodular_db_getp(fdb, L1, p);
    1800         672 :   GEN phi2 = polmodular_db_getp(fdb, L2, p);
    1801         672 :   GEN f = Flm_Fl_polmodular_evalx(phi1, L1, j1, p, pi);
    1802         672 :   GEN g = Flm_Fl_polmodular_evalx(phi2, L2, j2, p, pi);
    1803         672 :   GEN d = Flx_gcd(f, g, p);
    1804         672 :   long n = degpol(d);
    1805         672 :   if (n >= 2) n = Flx_nbroots(d, p);
    1806         672 :   avma = av; return n;
    1807             : }
    1808             : 
    1809             : static long
    1810        5908 : oriented_n_action(
    1811             :   const long *ni, classgp_pcp_t G, GEN v, ulong p, ulong pi, GEN fdb)
    1812             : {
    1813        5908 :   pari_sp av = avma;
    1814        5908 :   long i, j, k = G->k;
    1815        5908 :   long nr = k * (k - 1) / 2;
    1816        5908 :   const long *n = G->n, *m = G->m, *o = G->o, *r = G->r,
    1817        5908 :     *ps = G->orient_p, *qs = G->orient_q, *reps = G->orient_reps;
    1818        5908 :   long *signs = new_chunk(k);
    1819        5908 :   long *e = new_chunk(k);
    1820        5908 :   long *rels = new_chunk(nr);
    1821             : 
    1822        5908 :   evec_copy(rels, r, nr);
    1823             : 
    1824       15813 :   for (i = 0; i < k; ++i) {
    1825             :     /* If generator doesn't require orientation, continue; power rels already
    1826             :      * copied to *rels in initialisation */
    1827        9905 :     if (ps[i] <= 0) { signs[i] = 1; continue; }
    1828             :     /* Get rep of orientation element and express it in terms of the
    1829             :      * (partially) oriented presentation */
    1830        6503 :     for (j = 0; j < i; ++j) {
    1831        4186 :       long t = reps[i * k + j];
    1832        4186 :       e[j] = (signs[j] < 0 ? o[j] - t : t);
    1833             :     }
    1834        2317 :     e[j] = reps[i * k + j];
    1835        2317 :     for (++j; j < k; ++j) e[j] = 0;
    1836        2317 :     evec_reduce(e, n, rels, k);
    1837        2317 :     j = evec_to_index(e, m, k);
    1838             : 
    1839             :     /* FIXME: These calls to verify_edge recalculate powers of v[0]
    1840             :      * and v[j] over and over again, they also reduce Phi_{ps[i]} modulo p over
    1841             :      * and over again.  Need to cache these things! */
    1842        2317 :     if (qs[i] > 1)
    1843         672 :       signs[i] =
    1844         336 :         (verify_2path(uel(v,1), uel(v,j+1), p, pi, ps[i], qs[i], fdb) ? 1 : -1);
    1845             :     else
    1846             :       /* Verify ps[i]-edge to orient ith generator */
    1847        3962 :       signs[i] =
    1848        1981 :         (verify_edge(uel(v,1), uel(v,j+1), p, pi, ps[i], fdb) ? 1 : -1);
    1849             :     /* Update power relation */
    1850        6503 :     for (j = 0; j < i; ++j) {
    1851        4186 :       long t = evec_ri(r, i)[j];
    1852        4186 :       e[j] = (signs[i] * signs[j] < 0 ? o[j] - t : t);
    1853             :     }
    1854        2317 :     while (j < k) e[j++] = 0;
    1855        2317 :     evec_reduce(e, n, rels, k);
    1856        2317 :     for (j = 0; j < i; ++j) evec_ri_mutate(rels, i)[j] = e[j];
    1857             :     /* TODO: This is a sanity check, can be removed if everything is working */
    1858        8820 :     for (j = 0; j <= i; ++j) {
    1859        6503 :       long t = reps[i * k + j];
    1860        6503 :       e[j] = (signs[j] < 0 ? o[j] - t : t);
    1861             :     }
    1862        2317 :     while (j < k) e[j++] = 0;
    1863        2317 :     evec_reduce(e, n, rels, k);
    1864        2317 :     j = evec_to_index(e, m, k);
    1865        2317 :     if (qs[i] > 1) {
    1866         336 :       if (!verify_2path(uel(v,1), uel(v, j+1), p, pi, ps[i], qs[i], fdb))
    1867           0 :         pari_err_BUG("oriented_n_action");
    1868             :     } else {
    1869        1981 :       if (!verify_edge(uel(v,1), uel(v, j+1), p, pi, ps[i], fdb))
    1870           0 :         pari_err_BUG("oriented_n_action");
    1871             :     }
    1872             :   }
    1873             : 
    1874             :   /* Orient representation of [N] relative to the torsor <signs, rels> */
    1875        5908 :   for (i = 0; i < k; ++i) e[i] = (signs[i] < 0 ? o[i] - ni[i] : ni[i]);
    1876        5908 :   evec_reduce(e, n, rels, k);
    1877        5908 :   avma = av; return evec_to_index(e, m, k);
    1878             : }
    1879             : 
    1880             : /* F = double_eta_raw(inv) */
    1881             : INLINE void
    1882        5908 : adjust_orientation(GEN F, long inv, GEN v, long e, ulong p, ulong pi)
    1883             : {
    1884        5908 :   ulong j0 = uel(v, 1), je = uel(v, e);
    1885             : 
    1886        5908 :   if (!modinv_j_from_2double_eta(F, inv, NULL, j0, je, p, pi)) {
    1887        2134 :     if (modinv_inverted_involution(inv)) Flv_inv_pre_inplace(v, p, pi);
    1888        2134 :     if (modinv_negated_involution(inv)) Flv_neg_inplace(v, p);
    1889             :   }
    1890        5908 : }
    1891             : 
    1892             : static void
    1893         525 : polclass_psum(
    1894             :   GEN *psum, long *d, GEN roots, GEN primes, GEN pilist, ulong h, long inv)
    1895             : {
    1896             :   /* Number of consecutive CRT stabilisations before we assume we have
    1897             :    * the correct answer. */
    1898             :   enum { MIN_STAB_CNT = 3 };
    1899         525 :   pari_sp av = avma, btop;
    1900             :   GEN ps, psum_sqr, P;
    1901         525 :   long i, e, stabcnt, nprimes = lg(primes) - 1;
    1902             : 
    1903         525 :   if ((h & 1) && modinv_units(inv)) { *psum = gen_1; *d = 0; return; }
    1904         322 :   e = -1;
    1905         322 :   ps = cgetg(nprimes+1, t_VECSMALL);
    1906             :   do {
    1907         364 :     e += 2;
    1908        8421 :     for (i = 1; i <= nprimes; ++i)
    1909             :     {
    1910        8057 :       GEN roots_modp = gel(roots, i);
    1911        8057 :       ulong p = uel(primes, i), pi = uel(pilist, i);
    1912        8057 :       uel(ps, i) = Flv_powsum_pre(roots_modp, e, p, pi);
    1913             :     }
    1914         364 :     btop = avma;
    1915         364 :     psum_sqr = Z_init_CRT(0, 1);
    1916         364 :     P = gen_1;
    1917        2065 :     for (i = 1, stabcnt = 0; stabcnt < MIN_STAB_CNT && i <= nprimes; ++i)
    1918             :     {
    1919        1701 :       ulong p = uel(primes, i), pi = uel(pilist, i);
    1920        1701 :       ulong ps2 = Fl_sqr_pre(uel(ps, i), p, pi);
    1921        1701 :       ulong stab = Z_incremental_CRT(&psum_sqr, ps2, &P, p);
    1922             :       /* stabcnt = stab * (stabcnt + 1) */
    1923        1701 :       if (stab) ++stabcnt; else stabcnt = 0;
    1924        1701 :       if (gc_needed(av, 2)) gerepileall(btop, 2, &psum_sqr, &P);
    1925             :     }
    1926         364 :     if (stabcnt == 0 && nprimes >= MIN_STAB_CNT)
    1927           0 :       pari_err_BUG("polclass_psum");
    1928         364 :   } while (!signe(psum_sqr));
    1929             : 
    1930         322 :   if ( ! Z_issquareall(psum_sqr, psum)) pari_err_BUG("polclass_psum");
    1931             : 
    1932         322 :   dbg_printf(1)("Classpoly power sum (e = %ld) is %Ps; found with %.2f%% of the primes\n",
    1933           0 :       e, *psum, 100 * (i - 1) / (double) nprimes);
    1934         322 :   *psum = gerepileupto(av, *psum);
    1935         322 :   *d = e;
    1936             : }
    1937             : 
    1938             : static GEN
    1939          91 : polclass_small_disc(long D, long inv, long xvar)
    1940             : {
    1941          91 :   if (D == -3) return pol_x(xvar);
    1942          56 :   if (D == -4) {
    1943          56 :     switch (inv) {
    1944           7 :     case INV_J: return deg1pol(gen_1, stoi(-1728), xvar);
    1945          49 :     case INV_G2:return deg1pol(gen_1, stoi(-12), xvar);
    1946             :     default: /* no other invariants for which we can calculate H_{-4}(X) */
    1947           0 :       pari_err_BUG("polclass_small_disc");
    1948             :     }
    1949             :   }
    1950           0 :   return NULL;
    1951             : }
    1952             : 
    1953             : GEN
    1954        3851 : polclass0(long D, long inv, long xvar, GEN *db)
    1955             : {
    1956        3851 :   pari_sp av = avma;
    1957             :   GEN primes;
    1958        3851 :   long n_curves_tested = 0;
    1959             :   long nprimes, s, i, j, del, ni, orient;
    1960             :   GEN P, H, plist, pilist;
    1961             :   ulong u, L, maxL, vfactors, biggest_v;
    1962        3851 :   long h, p1, p2, filter = 1;
    1963             :   classgp_pcp_t G;
    1964             :   double height;
    1965             :   static const long k = 2;
    1966             :   static const double delta = 0.5;
    1967             : 
    1968        3851 :   if (D >= -4) return polclass_small_disc(D, inv, xvar);
    1969             : 
    1970        3760 :   (void) corediscs(D, &u);
    1971        3760 :   h = classno_wrapper(D);
    1972             : 
    1973        3760 :   dbg_printf(1)("D = %ld, conductor = %ld, inv = %ld\n", D, u, inv);
    1974             : 
    1975        3760 :   ni = modinv_degree(&p1, &p2, inv);
    1976        3760 :   orient = modinv_is_double_eta(inv) && kross(D, p1) && kross(D, p2);
    1977             : 
    1978        3760 :   classgp_make_pcp(G, &height, &ni, h, D, u, inv, filter, orient);
    1979        3760 :   primes = select_classpoly_primes(&vfactors, &biggest_v, k, delta, G, height);
    1980             : 
    1981             :   /* Prepopulate *db with all the modpolys we might need */
    1982             :   /* TODO: Clean this up; in particular, note that u is factored later on. */
    1983             :   /* This comes from L_bound in oneroot_of_classpoly() above */
    1984        3760 :   maxL = maxdd(log((double) -D), (double)biggest_v);
    1985        3760 :   if (u > 1) {
    1986         847 :     for (L = 2; L <= maxL; L = unextprime(L + 1))
    1987         672 :       if (!(u % L)) polmodular_db_add_level(db, L, INV_J);
    1988             :   }
    1989       15285 :   for (i = 0; vfactors; ++i) {
    1990       11525 :     if (vfactors & 1UL)
    1991       11203 :       polmodular_db_add_level(db, SMALL_PRIMES[i], INV_J);
    1992       11525 :     vfactors >>= 1;
    1993             :   }
    1994        3760 :   if (p1 > 1) polmodular_db_add_level(db, p1, INV_J);
    1995        3760 :   if (p2 > 1) polmodular_db_add_level(db, p2, INV_J);
    1996        3760 :   s = !!G->L0;
    1997        3760 :   polmodular_db_add_levels(db, G->L + s, G->k - s, inv);
    1998        3760 :   if (orient) {
    1999         672 :     for (i = 0; i < G->k; ++i)
    2000             :     {
    2001         413 :       if (G->orient_p[i] > 1) polmodular_db_add_level(db, G->orient_p[i], inv);
    2002         413 :       if (G->orient_q[i] > 1) polmodular_db_add_level(db, G->orient_q[i], inv);
    2003             :     }
    2004             :   }
    2005        3760 :   nprimes = lg(primes) - 1;
    2006        3760 :   H = cgetg(nprimes + 1, t_VEC);
    2007        3760 :   plist = cgetg(nprimes + 1, t_VECSMALL);
    2008        3760 :   pilist = cgetg(nprimes + 1, t_VECSMALL);
    2009      122149 :   for (i = 1; i <= nprimes; ++i) {
    2010      118389 :     long rho_inv = gel(primes, i)[4];
    2011             :     norm_eqn_t ne;
    2012      118389 :     setup_norm_eqn(ne, D, u, gel(primes, i));
    2013             : 
    2014      118389 :     gel(H, i) = polclass_roots_modp(&n_curves_tested, ne, rho_inv, G, *db);
    2015      118389 :     uel(plist, i) = ne->p;
    2016      118389 :     uel(pilist, i) = ne->pi;
    2017      118389 :     if (DEBUGLEVEL>2 && (i & 3L)==0) err_printf(" %ld%%", i*100/nprimes);
    2018             :   }
    2019        3760 :   dbg_printf(0)("\n");
    2020             : 
    2021        3760 :   if (orient) {
    2022         259 :     GEN nvec = new_chunk(G->k);
    2023         259 :     GEN fdb = polmodular_db_for_inv(*db, inv);
    2024         259 :     GEN F = double_eta_raw(inv);
    2025         259 :     index_to_evec((long *)nvec, ni, G->m, G->k);
    2026        6167 :     for (i = 1; i <= nprimes; ++i) {
    2027        5908 :       GEN v = gel(H, i);
    2028        5908 :       ulong p = uel(plist, i), pi = uel(pilist, i);
    2029        5908 :       long oni = oriented_n_action(nvec, G, v, p, pi, fdb);
    2030        5908 :       adjust_orientation(F, inv, v, oni + 1, p, pi);
    2031             :     }
    2032             :   }
    2033             : 
    2034        3760 :   if (modinv_has_sign_ambiguity(inv)) {
    2035             :     GEN psum;
    2036             :     long e;
    2037         525 :     polclass_psum(&psum, &e, H, plist, pilist, h, inv);
    2038       10843 :     for (i = 1; i <= nprimes; ++i) {
    2039       10318 :       GEN v = gel(H, i);
    2040       10318 :       ulong p = uel(plist, i), pi = uel(pilist, i);
    2041       10318 :       if (!adjust_signs(v, p, pi, inv, psum, e))
    2042          14 :         uel(plist, i) = 0;
    2043             :     }
    2044             :   }
    2045             : 
    2046      122149 :   for (i = 1, j = 1, del = 0; i <= nprimes; ++i) {
    2047      118389 :     GEN v = gel(H, i), pol;
    2048      118389 :     ulong p = uel(plist, i);
    2049      118389 :     if (!p) { del++; continue; }
    2050      118375 :     pol = Flv_roots_to_pol(v, p, xvar);
    2051      118375 :     uel(plist, j) = p;
    2052      118375 :     gel(H, j++) = Flx_to_Flv(pol, lg(pol) - 2);
    2053             :   }
    2054        3760 :   setlg(H,nprimes+1-del);
    2055        3760 :   setlg(plist,nprimes+1-del);
    2056        3760 :   classgp_pcp_clear(G);
    2057             : 
    2058        3760 :   dbg_printf(1)("Total number of curves tested: %ld\n", n_curves_tested);
    2059        3760 :   H = ncV_chinese_center(H, plist, &P);
    2060        3760 :   dbg_printf(1)("Result height: %.2f\n",
    2061             :              dbllog2r(itor(gsupnorm(H, DEFAULTPREC), DEFAULTPREC)));
    2062        3760 :   return gerepilecopy(av, RgV_to_RgX(H, xvar));
    2063             : }
    2064             : 
    2065             : void
    2066        1225 : check_modinv(long inv)
    2067             : {
    2068        1225 :   switch (inv) {
    2069             :   case INV_J:
    2070             :   case INV_F:
    2071             :   case INV_F2:
    2072             :   case INV_F3:
    2073             :   case INV_F4:
    2074             :   case INV_G2:
    2075             :   case INV_W2W3:
    2076             :   case INV_F8:
    2077             :   case INV_W3W3:
    2078             :   case INV_W2W5:
    2079             :   case INV_W2W7:
    2080             :   case INV_W3W5:
    2081             :   case INV_W3W7:
    2082             :   case INV_W2W3E2:
    2083             :   case INV_W2W5E2:
    2084             :   case INV_W2W13:
    2085             :   case INV_W2W7E2:
    2086             :   case INV_W3W3E2:
    2087             :   case INV_W5W7:
    2088             :   case INV_W3W13:
    2089        1211 :     break;
    2090             :   default:
    2091          14 :     pari_err_DOMAIN("polmodular", "inv", "invalid invariant", stoi(inv), gen_0);
    2092             :   }
    2093        1211 : }
    2094             : 
    2095             : GEN
    2096         623 : polclass(GEN DD, long inv, long xvar)
    2097             : {
    2098             :   GEN db, H;
    2099             :   long dummy, D;
    2100             : 
    2101         623 :   if (xvar < 0) xvar = 0;
    2102         623 :   check_quaddisc_imag(DD, &dummy, "polclass");
    2103         616 :   check_modinv(inv);
    2104             : 
    2105         609 :   D = itos(DD);
    2106         609 :   if (!modinv_good_disc(inv, D))
    2107           0 :     pari_err_DOMAIN("polclass", "D", "incompatible with given invariant", stoi(inv), DD);
    2108             : 
    2109         609 :   db = polmodular_db_init(inv);
    2110         609 :   H = polclass0(D, inv, xvar, &db);
    2111         609 :   gunclone_deep(db); return H;
    2112             : }

Generated by: LCOV version 1.13