Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - polclass.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23036-b751c0af5) Lines: 833 856 97.3 %
Date: 2018-09-26 05:46:06 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 :       return gc_long(ltop,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) return gc_long(ltop,1);
     104             : 
     105       20547 :     lswap(a4, a4t);
     106       20547 :     lswap(a6, a6t);
     107       20547 :     lswap(m0, m1); set_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 :     set_avma(av);
     769             :   }
     770      118990 :   return gc_long(ltop, 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 :       set_avma(av);
     793             :     }
     794             :   }
     795             : }
     796             : 
     797             : INLINE long *
     798        5520 : evec_ri_mutate(long r[], long i)
     799        5520 : { return r + (i * (i - 1) >> 1); }
     800             : 
     801             : INLINE const long *
     802       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 :   set_avma(av);
     884         175 : }
     885             : 
     886             : /* e1 and e2 may overlap */
     887             : /* This is a faster way to compute inverses, if the presentation
     888             :  * element orders are known (these are specified in the array o, the
     889             :  * array n holds the relative orders) */
     890             : INLINE void
     891         735 : evec_inverse_o(
     892             :   long e2[],
     893             :   const long e1[], const long n[], const long o[], const long r[], long k)
     894             : {
     895             :   long j;
     896         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 :   return gc_long(av,o);
     917             : }
     918             : 
     919             : /* Computes orders o[] for each generator using relative orders n[]
     920             :  * and power relations r[] */
     921             : INLINE void
     922        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 :   set_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(utor(-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 :     set_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 :   set_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)) return gc_long(av,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)) return gc_long(av,0);
    1047             : 
    1048          91 :   if (f) {
    1049          14 :     evec_compose(e3, f, ei, n, r, k);
    1050          14 :     if (evec_equal(e2, e3, k)) return gc_long(av,0);
    1051             : 
    1052          14 :     evec_inverse_o(e3, e3, n, o, r, k);
    1053          14 :     if (evec_equal(e2, e3, k)) return gc_long(av,0);
    1054             :   }
    1055          91 :   return gc_long(av,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 :   return gc_long(av,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 :   return gc_bool(av,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 :   return gc_long(av,-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 :     set_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 :     set_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 :   set_avma(av); return;
    1349             : }
    1350             : 
    1351             : INLINE ulong
    1352        3760 : classno_wrapper(long D)
    1353             : {
    1354        3760 :   pari_sp av = avma;
    1355        3760 :   GEN G = quadclassunit0(stoi(D), 0, NULL, DEFAULTPREC);
    1356        3760 :   return gc_ulong(av, itou(abgrp_get_no(G)));
    1357             : }
    1358             : 
    1359             : /*
    1360             :  * SECTION: Functions for calculating class polynomials.
    1361             :  */
    1362             : 
    1363             : #define NSMALL_PRIMES 11
    1364             : static const long SMALL_PRIMES[11] = {
    1365             :   2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31
    1366             : };
    1367             : 
    1368             : static long
    1369       45913 : is_smooth_enough(ulong *factors, long v)
    1370             : {
    1371             :   long i;
    1372       45913 :   *factors = 0;
    1373      115282 :   for (i = 0; i < NSMALL_PRIMES; ++i) {
    1374      115275 :     long p = SMALL_PRIMES[i];
    1375      115275 :     if (v % p == 0) *factors |= 1UL << i;
    1376      115275 :     while (v % p == 0) v /= p;
    1377      115275 :     if (v == 1) break;
    1378             :   }
    1379       45913 :   return v == 1;
    1380             : }
    1381             : 
    1382             : /* Hurwitz class number of |D| assuming hclassno() and attached
    1383             :  * conversion to double costs much more than unegisfundamental(). */
    1384             : INLINE double
    1385       49666 : hclassno_wrapper(long D, long h)
    1386             : {
    1387             :   /* TODO: Can probably calculate hurwitz faster using -D, factor(u)
    1388             :    * and classno(D). */
    1389       49666 :   pari_sp av = avma;
    1390       49666 :   ulong abs_D = D < 0 ? -D : D;
    1391             :   double hurwitz;
    1392             : 
    1393       49666 :   if (h && unegisfundamental(abs_D))
    1394        3585 :     hurwitz = (double) h;
    1395             :   else
    1396       46081 :     hurwitz = rtodbl(gtofp(hclassno(utoi(abs_D)), DEFAULTPREC));
    1397       49666 :   set_avma(av); return hurwitz;
    1398             : }
    1399             : 
    1400             : 
    1401             : /* This is Sutherland 2009, Algorithm 2.1 (p8); delta > 0 */
    1402             : static GEN
    1403        3760 : select_classpoly_prime_pool(double min_bits, double delta, classgp_pcp_t G)
    1404             : { /* Sutherland defines V_MAX to be 1200 without saying why */
    1405        3760 :   const long V_MAX = 1200;
    1406        3760 :   pari_sp av = avma;
    1407        3760 :   double bits = 0.0, hurwitz, z;
    1408             :   ulong t_size_lim;
    1409        3760 :   long ires, D = G->D, inv = G->inv;
    1410             :   GEN res, t_min; /* t_min[v] = lower bound for the t we look at for that v */
    1411             : 
    1412        3760 :   hurwitz = hclassno_wrapper(D, G->h);
    1413             : 
    1414        3760 :   res = cgetg(128+1, t_VEC);
    1415        3760 :   ires = 1;
    1416             :   /* Initialise t_min to be all 2's.  This avoids trace 0 and trace 1 curves */
    1417        3760 :   t_min = const_vecsmall(V_MAX-1, 2);
    1418             : 
    1419             :   /* maximum possible trace = sqrt(2^BIL - D) */
    1420        3760 :   t_size_lim = 2.0 * sqrt((double)((1UL << (BITS_IN_LONG - 2)) - (((ulong)-D) >> 2)));
    1421             : 
    1422        3767 :   for (z = -D / (2.0 * hurwitz); ; z *= delta + 1.0)
    1423           7 :   { /* v_bound_aux = -4 z H(-D) */
    1424        3767 :     double v_bound_aux = -4.0 * z * hurwitz;
    1425             :     ulong v;
    1426        3767 :     dbg_printf(1)("z = %.2f\n", z);
    1427       45920 :     for (v = 1; v < V_MAX; v++)
    1428             :     {
    1429       45920 :       ulong p, t, t_max, vfactors, m_vsqr_D = v * v * (ulong)(-D);
    1430             :       /* hurwitz_ratio_bound = 11 * log(log(v + 4))^2 */
    1431       45920 :       double hurwitz_ratio_bound = log(log(v + 4.0)), max_p, H;
    1432             :       long ires0;
    1433       45920 :       hurwitz_ratio_bound *= 11.0 * hurwitz_ratio_bound;
    1434             : 
    1435       45920 :       if (v >= v_bound_aux * hurwitz_ratio_bound / D) break;
    1436       45913 :       if (!is_smooth_enough(&vfactors, v)) continue;
    1437       45906 :       H = hclassno_wrapper(m_vsqr_D, 0);
    1438             : 
    1439             :       /* t <= 2 sqrt(p) and p <= z H(-v^2 D) and
    1440             :        *   H(-v^2 D) < vH(-D) (11 log(log(v + 4))^2)
    1441             :        * This last term is v * hurwitz * hurwitz_ratio_bound. */
    1442       45906 :       max_p = z * v * hurwitz * hurwitz_ratio_bound;
    1443       45906 :       t_max = 2.0 * sqrt(mindd((1UL<<(BITS_IN_LONG-2)) - (m_vsqr_D>>2), max_p));
    1444       45906 :       t = t_min[v]; if ((t & 1) != (m_vsqr_D & 1)) t++;
    1445       45906 :       p = (t * t + m_vsqr_D) >> 2;
    1446       45906 :       ires0 = ires;
    1447     6715628 :       for (; t <= t_max; p += t+1, t += 2) /* 4p = t^2 - v^2*D */
    1448     6669722 :         if (modinv_good_prime(inv,p) && uisprime(p))
    1449             :         {
    1450      242703 :           if (ires == lg(res)) res = vec_lengthen(res, lg(res) << 1);
    1451      242703 :           gel(res, ires++) = mkvecsmall5(p, t, v, (long)(p / H), vfactors);
    1452      242703 :           bits += log2(p);
    1453             :         }
    1454       45906 :       t_min[v] = t;
    1455             : 
    1456       45906 :       if (ires - ires0) {
    1457       23289 :         dbg_printf(2)("  Found %lu primes for v = %lu.\n", ires - ires0, v);
    1458             :       }
    1459       45906 :       if (bits > min_bits) {
    1460        3760 :         dbg_printf(1)("Found %ld primes; total size %.2f bits.\n", ires-1,bits);
    1461        7520 :         setlg(res, ires); return gerepilecopy(av, res);
    1462             :       }
    1463             :     }
    1464           7 :     if (uel(t_min,1) >= t_size_lim) {
    1465             :       /* exhausted all solutions that fit in ulong */
    1466           0 :       char *err = stack_sprintf("class polynomial of discriminant %ld", D);
    1467           0 :       pari_err(e_ARCH, err);
    1468             :     }
    1469             :   }
    1470             : }
    1471             : 
    1472             : INLINE int
    1473     1057255 : cmp_small(long a, long b)
    1474     1057255 : { return a>b? 1: (a<b? -1: 0); }
    1475             : 
    1476             : static int
    1477     1057255 : primecmp(void *data, GEN v1, GEN v2)
    1478     1057255 : { (void)data; return cmp_small(v1[4], v2[4]); }
    1479             : 
    1480             : 
    1481             : static long
    1482        3760 : height_margin(long inv, long D)
    1483             : {
    1484             :   (void)D;
    1485             :   /* NB: avs just uses a height margin of 256 for everyone and everything. */
    1486        3760 :   if (inv == INV_F) return 64;  /* checked for discriminants up to -350000 */
    1487        3648 :   if (inv == INV_G2) return 5;
    1488        3431 :   if (inv != INV_J) return 256; /* TODO: This should be made more accurate */
    1489        2927 :   return 0;
    1490             : }
    1491             : 
    1492             : static GEN
    1493        3760 : select_classpoly_primes(
    1494             :   ulong *vfactors, ulong *biggest_v,
    1495             :   long k, double delta, classgp_pcp_t G, double height)
    1496             : {
    1497        3760 :   pari_sp av = avma;
    1498        3760 :   long i, s, D = G->D, inv = G->inv;
    1499             :   ulong biggest_p;
    1500             :   double prime_bits, min_prime_bits, b;
    1501             :   GEN prime_pool;
    1502             : 
    1503        3760 :   if (k < 2) pari_err_BUG("select_suitable_primes");
    1504             : 
    1505        3760 :   s = modinv_height_factor(inv);
    1506        3760 :   b = height / s + height_margin(inv, D);
    1507        3760 :   dbg_printf(1)("adjusted height = %.2f\n", b);
    1508        3760 :   min_prime_bits = k * b;
    1509             : 
    1510        3760 :   prime_pool = select_classpoly_prime_pool(min_prime_bits, delta, G);
    1511             : 
    1512             :   /* FIXME: Apply torsion constraints */
    1513             :   /* FIXME: Rank elts of res according to cost/benefit ratio */
    1514        3760 :   gen_sort_inplace(prime_pool, NULL, primecmp, NULL);
    1515             : 
    1516        3760 :   prime_bits = 0.0;
    1517        3760 :   biggest_p = gel(prime_pool, 1)[1];
    1518        3760 :   *biggest_v = gel(prime_pool, 1)[3];
    1519        3760 :   *vfactors = 0;
    1520      118389 :   for (i = 1; i < lg(prime_pool); ++i) {
    1521      118389 :     ulong p = gel(prime_pool, i)[1];
    1522      118389 :     ulong v = gel(prime_pool, i)[3];
    1523      118389 :     prime_bits += log2(p);
    1524      118389 :     *vfactors |= gel(prime_pool, i)[5];
    1525      118389 :     if (p > biggest_p) biggest_p = p;
    1526      118389 :     if (v > *biggest_v) *biggest_v = v;
    1527      118389 :     if (prime_bits > b) break;
    1528             :   }
    1529        3760 :   dbg_printf(1)("Selected %ld primes; largest is %lu ~ 2^%.2f\n",
    1530             :              i, biggest_p, log2(biggest_p));
    1531        3760 :   return gerepilecopy(av, vecslice0(prime_pool, 1, i));
    1532             : }
    1533             : 
    1534             : /* This is Sutherland 2009 Algorithm 1.2. */
    1535             : static long
    1536      118997 : oneroot_of_classpoly(
    1537             :   ulong *j_endo, int *endo_cert, ulong j, norm_eqn_t ne, GEN jdb)
    1538             : {
    1539      118997 :   pari_sp av = avma;
    1540             :   long nfactors, L_bound, i;
    1541      118997 :   ulong p = ne->p, pi = ne->pi;
    1542             :   GEN factw, factors, u_levels, vdepths;
    1543             : 
    1544      118997 :   if (j == 0 || j == 1728 % p) pari_err_BUG("oneroot_of_classpoly");
    1545             : 
    1546      118997 :   *endo_cert = 1;
    1547      118997 :   if (ne->u * ne->v == 1) { *j_endo = j; return 1; }
    1548             : 
    1549             :   /* TODO: Precalculate all this data further up */
    1550      116225 :   factw = factoru(ne->u * ne->v);
    1551      116225 :   factors = gel(factw, 1);
    1552      116225 :   nfactors = lg(factors) - 1;
    1553      116225 :   u_levels = cgetg(nfactors + 1, t_VECSMALL);
    1554      299053 :   for (i = 1; i <= nfactors; ++i)
    1555      182828 :     u_levels[i] = z_lval(ne->u, gel(factw, 1)[i]);
    1556      116225 :   vdepths = gel(factw, 2);
    1557             : 
    1558             :   /* FIXME: This should be bigger */
    1559      116225 :   L_bound = maxdd(log((double) -ne->D), (double)ne->v);
    1560             : 
    1561             :   /* Iterate over the primes L dividing w */
    1562      295141 :   for (i = 1; i <= nfactors; ++i) {
    1563      182828 :     pari_sp bv = avma;
    1564             :     GEN phi;
    1565      182828 :     long jlvl, lvl_diff, depth = vdepths[i];
    1566      182828 :     long L = factors[i];
    1567      182828 :     if (L > L_bound) { *endo_cert = 0; break; }
    1568             : 
    1569      178916 :     phi = polmodular_db_getp(jdb, L, p);
    1570             : 
    1571             :     /* TODO: See if I can reuse paths created in j_level_in_volcano()
    1572             :      * later in {ascend,descend}_volcano(), perhaps by combining the
    1573             :      * functions into one "adjust_level" function. */
    1574      178916 :     jlvl = j_level_in_volcano(phi, j, p, pi, L, depth);
    1575      178916 :     lvl_diff = u_levels[i] - jlvl;
    1576             : 
    1577      178916 :     if (lvl_diff < 0)
    1578             :       /* j's level is less than v(u) so we must ascend */
    1579      111026 :       j = ascend_volcano(phi, j, p, pi, jlvl, L, depth, -lvl_diff);
    1580       67890 :     else if (lvl_diff > 0)
    1581             :       /* otherwise j's level is greater than v(u) so we descend */
    1582         774 :       j = descend_volcano(phi, j, p, pi, jlvl, L, depth, lvl_diff);
    1583      178916 :     set_avma(bv);
    1584             :   }
    1585      116225 :   set_avma(av);
    1586             :   /* At this point the probability that j has the wrong endomorphism
    1587             :    * ring is about \sum_{p|u_compl} 1/p (and u_compl must be bigger
    1588             :    * than L_bound, so pretty big), so just return it and rely on
    1589             :    * detection code in enum_j_with_endo_ring().  Detection is that we
    1590             :    * hit a previously found j-invariant earlier than expected.  OR, we
    1591             :    * evaluate class polynomials of the suborders at j and if any are
    1592             :    * zero then j must be chosen again.  */
    1593      116225 :   *j_endo = j;
    1594      116225 :   return j != 0 && j != 1728 % p;
    1595             : }
    1596             : 
    1597             : INLINE long
    1598        3304 : vecsmall_isin_skip(GEN v, long x, long k)
    1599             : {
    1600        3304 :   long i, l = lg(v);
    1601      117467 :   for (i = k; i < l; ++i)
    1602      114163 :     if (v[i] == x) return i;
    1603        3304 :   return 0;
    1604             : }
    1605             : 
    1606             : INLINE ulong
    1607      237142 : select_twisting_param(ulong p)
    1608             : {
    1609             :   ulong T;
    1610      237142 :   do T = random_Fl(p); while (krouu(T, p) != -1);
    1611      118389 :   return T;
    1612             : }
    1613             : 
    1614             : INLINE void
    1615      118389 : setup_norm_eqn(norm_eqn_t ne, long D, long u, GEN norm_eqn)
    1616             : {
    1617      118389 :   ne->D = D;
    1618      118389 :   ne->u = u;
    1619      118389 :   ne->t = norm_eqn[2];
    1620      118389 :   ne->v = norm_eqn[3];
    1621      118389 :   ne->p = (ulong) norm_eqn[1];
    1622      118389 :   ne->pi = get_Fl_red(ne->p);
    1623      118389 :   ne->s2 = Fl_2gener_pre(ne->p, ne->pi);
    1624      118389 :   ne->T = select_twisting_param(ne->p);
    1625      118389 : }
    1626             : 
    1627             : INLINE ulong
    1628       15302 : Flv_powsum_pre(GEN v, ulong n, ulong p, ulong pi)
    1629             : {
    1630       15302 :   long i, l = lg(v);
    1631       15302 :   ulong psum = 0;
    1632      372134 :   for (i = 1; i < l; ++i)
    1633      356832 :     psum = Fl_add(psum, Fl_powu_pre(uel(v,i), n, p, pi), p);
    1634       15302 :   return psum;
    1635             : }
    1636             : 
    1637             : INLINE int
    1638        3760 : modinv_has_sign_ambiguity(long inv)
    1639             : {
    1640        3760 :   switch (inv) {
    1641             :   case INV_F:
    1642             :   case INV_F3:
    1643             :   case INV_W2W3E2:
    1644             :   case INV_W2W7E2:
    1645             :   case INV_W2W3:
    1646             :   case INV_W2W5:
    1647             :   case INV_W2W7:
    1648             :   case INV_W3W3:
    1649             :   case INV_W2W13:
    1650         525 :   case INV_W3W7: return 1;
    1651             :   }
    1652        3235 :   return 0;
    1653             : }
    1654             : 
    1655             : INLINE int
    1656        3276 : modinv_units(int inv)
    1657        3276 : { return modinv_is_double_eta(inv) || modinv_is_Weber(inv); }
    1658             : 
    1659             : INLINE int
    1660       10318 : adjust_signs(GEN js, ulong p, ulong pi, long inv, GEN T, long e)
    1661             : {
    1662       10318 :   long negate = 0;
    1663       10318 :   long h = lg(js) - 1;
    1664       13391 :   if ((h & 1) && modinv_units(inv)) {
    1665        3073 :     ulong prod = Flv_prod_pre(js, p, pi);
    1666        3073 :     if (prod != p - 1) {
    1667        1583 :       if (prod != 1) pari_err_BUG("adjust_signs: constant term is not +/-1");
    1668        1583 :       negate = 1;
    1669             :     }
    1670             :   } else {
    1671             :     ulong tp, t;
    1672        7245 :     tp = umodiu(T, p);
    1673        7245 :     t = Flv_powsum_pre(js, e, p, pi);
    1674        7245 :     if (t == 0) return 0;
    1675        7231 :     if (t != tp) {
    1676        3536 :       if (Fl_neg(t, p) != tp) pari_err_BUG("adjust_signs: incorrect trace");
    1677        3536 :       negate = 1;
    1678             :     }
    1679             :   }
    1680       10304 :   if (negate) Flv_neg_inplace(js, p);
    1681       10304 :   return 1;
    1682             : }
    1683             : 
    1684             : static ulong
    1685      118926 : find_jinv(
    1686             :   long *trace_tries, long *endo_tries, int *cert,
    1687             :   norm_eqn_t ne, long inv, long rho_inv, GEN jdb)
    1688             : {
    1689      118926 :   long found, ok = 1;
    1690             :   ulong j, r;
    1691             :   do {
    1692             :     do {
    1693             :       long tries;
    1694      118997 :       ulong j_t = 0;
    1695             :       /* TODO: Set batch size according to expected number of tries and
    1696             :        * experimental cost/benefit analysis. */
    1697      118997 :       tries = find_j_inv_with_given_trace(&j_t, ne, rho_inv, 0);
    1698      118997 :       if (j_t == 0)
    1699           0 :         pari_err_BUG("polclass0: Couldn't find j-invariant with given trace.");
    1700      118997 :       dbg_printf(2)("  j-invariant %ld has trace +/-%ld (%ld tries, 1/rho = %ld)\n",
    1701             :           j_t, ne->t, tries, rho_inv);
    1702      118997 :       *trace_tries += tries;
    1703             : 
    1704      118997 :       found = oneroot_of_classpoly(&j, cert, j_t, ne, jdb);
    1705      118997 :       ++*endo_tries;
    1706      118997 :     } while (!found);
    1707             : 
    1708      118997 :     if (modinv_is_double_eta(inv))
    1709       11718 :       ok = modfn_unambiguous_root(&r, inv, j, ne, jdb);
    1710             :     else
    1711      107279 :       r = modfn_root(j, ne, inv);
    1712      118997 :   } while (!ok);
    1713      118926 :   return r;
    1714             : }
    1715             : 
    1716             : static GEN
    1717      118389 : polclass_roots_modp(
    1718             :   long *n_trace_curves,
    1719             :   norm_eqn_t ne, long rho_inv, classgp_pcp_t G, GEN db)
    1720             : {
    1721      118389 :   pari_sp av = avma;
    1722      118389 :   ulong j = 0;
    1723      118389 :   long inv = G->inv, endo_tries = 0;
    1724             :   int endo_cert;
    1725             :   GEN res, jdb, fdb;
    1726             : 
    1727      118389 :   jdb = polmodular_db_for_inv(db, INV_J);
    1728      118389 :   fdb = polmodular_db_for_inv(db, inv);
    1729             : 
    1730      118389 :   dbg_printf(2)("p = %ld, t = %ld, v = %ld\n", ne->p, ne->t, ne->v);
    1731             : 
    1732             :   do {
    1733      118926 :     j = find_jinv(n_trace_curves, &endo_tries, &endo_cert, ne, inv, rho_inv, jdb);
    1734             : 
    1735      118926 :     res = enum_roots(j, ne, fdb, G);
    1736      118926 :     if ( ! res && endo_cert) pari_err_BUG("polclass_roots_modp");
    1737      118926 :     if (res && ! endo_cert && vecsmall_isin_skip(res, res[1], 2))
    1738             :     {
    1739           0 :       set_avma(av);
    1740           0 :       res = NULL;
    1741             :     }
    1742      118926 :   } while (!res);
    1743             : 
    1744      118389 :   dbg_printf(2)("  j-invariant %ld has correct endomorphism ring "
    1745             :              "(%ld tries)\n", j, endo_tries);
    1746      118389 :   dbg_printf(4)("  all such j-invariants: %Ps\n", res);
    1747      118389 :   return gerepileupto(av, res);
    1748             : }
    1749             : 
    1750             : INLINE int
    1751        2134 : modinv_inverted_involution(long inv)
    1752        2134 : { return modinv_is_double_eta(inv); }
    1753             : 
    1754             : INLINE int
    1755        2134 : modinv_negated_involution(long inv)
    1756             : { /* determined by trial and error */
    1757        2134 :   return inv == INV_F || inv == INV_W3W5 || inv == INV_W3W7
    1758        4007 :     || inv == INV_W3W3 || inv == INV_W5W7;
    1759             : }
    1760             : 
    1761             : /* Return true iff Phi_L(j0, j1) = 0. */
    1762             : INLINE long
    1763        3962 : verify_edge(ulong j0, ulong j1, ulong p, ulong pi, long L, GEN fdb)
    1764             : {
    1765        3962 :   pari_sp av = avma;
    1766        3962 :   GEN phi = polmodular_db_getp(fdb, L, p);
    1767        3962 :   GEN f = Flm_Fl_polmodular_evalx(phi, L, j1, p, pi);
    1768        3962 :   return gc_long(av, Flx_eval_pre(f, j0, p, pi) == 0);
    1769             : }
    1770             : 
    1771             : INLINE long
    1772         672 : verify_2path(
    1773             :   ulong j1, ulong j2, ulong p, ulong pi, long L1, long L2, GEN fdb)
    1774             : {
    1775         672 :   pari_sp av = avma;
    1776         672 :   GEN phi1 = polmodular_db_getp(fdb, L1, p);
    1777         672 :   GEN phi2 = polmodular_db_getp(fdb, L2, p);
    1778         672 :   GEN f = Flm_Fl_polmodular_evalx(phi1, L1, j1, p, pi);
    1779         672 :   GEN g = Flm_Fl_polmodular_evalx(phi2, L2, j2, p, pi);
    1780         672 :   GEN d = Flx_gcd(f, g, p);
    1781         672 :   long n = degpol(d);
    1782         672 :   if (n >= 2) n = Flx_nbroots(d, p);
    1783         672 :   return gc_long(av, n);
    1784             : }
    1785             : 
    1786             : static long
    1787        5908 : oriented_n_action(
    1788             :   const long *ni, classgp_pcp_t G, GEN v, ulong p, ulong pi, GEN fdb)
    1789             : {
    1790        5908 :   pari_sp av = avma;
    1791        5908 :   long i, j, k = G->k;
    1792        5908 :   long nr = k * (k - 1) / 2;
    1793        5908 :   const long *n = G->n, *m = G->m, *o = G->o, *r = G->r,
    1794        5908 :     *ps = G->orient_p, *qs = G->orient_q, *reps = G->orient_reps;
    1795        5908 :   long *signs = new_chunk(k);
    1796        5908 :   long *e = new_chunk(k);
    1797        5908 :   long *rels = new_chunk(nr);
    1798             : 
    1799        5908 :   evec_copy(rels, r, nr);
    1800             : 
    1801       15813 :   for (i = 0; i < k; ++i) {
    1802             :     /* If generator doesn't require orientation, continue; power rels already
    1803             :      * copied to *rels in initialisation */
    1804        9905 :     if (ps[i] <= 0) { signs[i] = 1; continue; }
    1805             :     /* Get rep of orientation element and express it in terms of the
    1806             :      * (partially) oriented presentation */
    1807        6503 :     for (j = 0; j < i; ++j) {
    1808        4186 :       long t = reps[i * k + j];
    1809        4186 :       e[j] = (signs[j] < 0 ? o[j] - t : t);
    1810             :     }
    1811        2317 :     e[j] = reps[i * k + j];
    1812        2317 :     for (++j; j < k; ++j) e[j] = 0;
    1813        2317 :     evec_reduce(e, n, rels, k);
    1814        2317 :     j = evec_to_index(e, m, k);
    1815             : 
    1816             :     /* FIXME: These calls to verify_edge recalculate powers of v[0]
    1817             :      * and v[j] over and over again, they also reduce Phi_{ps[i]} modulo p over
    1818             :      * and over again.  Need to cache these things! */
    1819        2317 :     if (qs[i] > 1)
    1820         672 :       signs[i] =
    1821         336 :         (verify_2path(uel(v,1), uel(v,j+1), p, pi, ps[i], qs[i], fdb) ? 1 : -1);
    1822             :     else
    1823             :       /* Verify ps[i]-edge to orient ith generator */
    1824        3962 :       signs[i] =
    1825        1981 :         (verify_edge(uel(v,1), uel(v,j+1), p, pi, ps[i], fdb) ? 1 : -1);
    1826             :     /* Update power relation */
    1827        6503 :     for (j = 0; j < i; ++j) {
    1828        4186 :       long t = evec_ri(r, i)[j];
    1829        4186 :       e[j] = (signs[i] * signs[j] < 0 ? o[j] - t : t);
    1830             :     }
    1831        2317 :     while (j < k) e[j++] = 0;
    1832        2317 :     evec_reduce(e, n, rels, k);
    1833        2317 :     for (j = 0; j < i; ++j) evec_ri_mutate(rels, i)[j] = e[j];
    1834             :     /* TODO: This is a sanity check, can be removed if everything is working */
    1835        8820 :     for (j = 0; j <= i; ++j) {
    1836        6503 :       long t = reps[i * k + j];
    1837        6503 :       e[j] = (signs[j] < 0 ? o[j] - t : t);
    1838             :     }
    1839        2317 :     while (j < k) e[j++] = 0;
    1840        2317 :     evec_reduce(e, n, rels, k);
    1841        2317 :     j = evec_to_index(e, m, k);
    1842        2317 :     if (qs[i] > 1) {
    1843         336 :       if (!verify_2path(uel(v,1), uel(v, j+1), p, pi, ps[i], qs[i], fdb))
    1844           0 :         pari_err_BUG("oriented_n_action");
    1845             :     } else {
    1846        1981 :       if (!verify_edge(uel(v,1), uel(v, j+1), p, pi, ps[i], fdb))
    1847           0 :         pari_err_BUG("oriented_n_action");
    1848             :     }
    1849             :   }
    1850             : 
    1851             :   /* Orient representation of [N] relative to the torsor <signs, rels> */
    1852        5908 :   for (i = 0; i < k; ++i) e[i] = (signs[i] < 0 ? o[i] - ni[i] : ni[i]);
    1853        5908 :   evec_reduce(e, n, rels, k);
    1854        5908 :   return gc_long(av, evec_to_index(e,m,k));
    1855             : }
    1856             : 
    1857             : /* F = double_eta_raw(inv) */
    1858             : INLINE void
    1859        5908 : adjust_orientation(GEN F, long inv, GEN v, long e, ulong p, ulong pi)
    1860             : {
    1861        5908 :   ulong j0 = uel(v, 1), je = uel(v, e);
    1862             : 
    1863        5908 :   if (!modinv_j_from_2double_eta(F, inv, NULL, j0, je, p, pi)) {
    1864        2134 :     if (modinv_inverted_involution(inv)) Flv_inv_pre_inplace(v, p, pi);
    1865        2134 :     if (modinv_negated_involution(inv)) Flv_neg_inplace(v, p);
    1866             :   }
    1867        5908 : }
    1868             : 
    1869             : static void
    1870         525 : polclass_psum(
    1871             :   GEN *psum, long *d, GEN roots, GEN primes, GEN pilist, ulong h, long inv)
    1872             : {
    1873             :   /* Number of consecutive CRT stabilisations before we assume we have
    1874             :    * the correct answer. */
    1875             :   enum { MIN_STAB_CNT = 3 };
    1876         525 :   pari_sp av = avma, btop;
    1877             :   GEN ps, psum_sqr, P;
    1878         525 :   long i, e, stabcnt, nprimes = lg(primes) - 1;
    1879             : 
    1880         525 :   if ((h & 1) && modinv_units(inv)) { *psum = gen_1; *d = 0; return; }
    1881         322 :   e = -1;
    1882         322 :   ps = cgetg(nprimes+1, t_VECSMALL);
    1883             :   do {
    1884         364 :     e += 2;
    1885        8421 :     for (i = 1; i <= nprimes; ++i)
    1886             :     {
    1887        8057 :       GEN roots_modp = gel(roots, i);
    1888        8057 :       ulong p = uel(primes, i), pi = uel(pilist, i);
    1889        8057 :       uel(ps, i) = Flv_powsum_pre(roots_modp, e, p, pi);
    1890             :     }
    1891         364 :     btop = avma;
    1892         364 :     psum_sqr = Z_init_CRT(0, 1);
    1893         364 :     P = gen_1;
    1894        2065 :     for (i = 1, stabcnt = 0; stabcnt < MIN_STAB_CNT && i <= nprimes; ++i)
    1895             :     {
    1896        1701 :       ulong p = uel(primes, i), pi = uel(pilist, i);
    1897        1701 :       ulong ps2 = Fl_sqr_pre(uel(ps, i), p, pi);
    1898        1701 :       ulong stab = Z_incremental_CRT(&psum_sqr, ps2, &P, p);
    1899             :       /* stabcnt = stab * (stabcnt + 1) */
    1900        1701 :       if (stab) ++stabcnt; else stabcnt = 0;
    1901        1701 :       if (gc_needed(av, 2)) gerepileall(btop, 2, &psum_sqr, &P);
    1902             :     }
    1903         364 :     if (stabcnt == 0 && nprimes >= MIN_STAB_CNT)
    1904           0 :       pari_err_BUG("polclass_psum");
    1905         364 :   } while (!signe(psum_sqr));
    1906             : 
    1907         322 :   if ( ! Z_issquareall(psum_sqr, psum)) pari_err_BUG("polclass_psum");
    1908             : 
    1909         322 :   dbg_printf(1)("Classpoly power sum (e = %ld) is %Ps; found with %.2f%% of the primes\n",
    1910           0 :       e, *psum, 100 * (i - 1) / (double) nprimes);
    1911         322 :   *psum = gerepileupto(av, *psum);
    1912         322 :   *d = e;
    1913             : }
    1914             : 
    1915             : static GEN
    1916          91 : polclass_small_disc(long D, long inv, long xvar)
    1917             : {
    1918          91 :   if (D == -3) return pol_x(xvar);
    1919          56 :   if (D == -4) {
    1920          56 :     switch (inv) {
    1921           7 :     case INV_J: return deg1pol(gen_1, stoi(-1728), xvar);
    1922          49 :     case INV_G2:return deg1pol(gen_1, stoi(-12), xvar);
    1923             :     default: /* no other invariants for which we can calculate H_{-4}(X) */
    1924           0 :       pari_err_BUG("polclass_small_disc");
    1925             :     }
    1926             :   }
    1927           0 :   return NULL;
    1928             : }
    1929             : 
    1930             : GEN
    1931        3851 : polclass0(long D, long inv, long xvar, GEN *db)
    1932             : {
    1933        3851 :   pari_sp av = avma;
    1934             :   GEN primes;
    1935        3851 :   long n_curves_tested = 0;
    1936             :   long nprimes, s, i, j, del, ni, orient;
    1937             :   GEN P, H, plist, pilist;
    1938             :   ulong u, L, maxL, vfactors, biggest_v;
    1939        3851 :   long h, p1, p2, filter = 1;
    1940             :   classgp_pcp_t G;
    1941             :   double height;
    1942             :   static const long k = 2;
    1943             :   static const double delta = 0.5;
    1944             : 
    1945        3851 :   if (D >= -4) return polclass_small_disc(D, inv, xvar);
    1946             : 
    1947        3760 :   (void) corediscs(D, &u);
    1948        3760 :   h = classno_wrapper(D);
    1949             : 
    1950        3760 :   dbg_printf(1)("D = %ld, conductor = %ld, inv = %ld\n", D, u, inv);
    1951             : 
    1952        3760 :   ni = modinv_degree(&p1, &p2, inv);
    1953        3760 :   orient = modinv_is_double_eta(inv) && kross(D, p1) && kross(D, p2);
    1954             : 
    1955        3760 :   classgp_make_pcp(G, &height, &ni, h, D, u, inv, filter, orient);
    1956        3760 :   primes = select_classpoly_primes(&vfactors, &biggest_v, k, delta, G, height);
    1957             : 
    1958             :   /* Prepopulate *db with all the modpolys we might need */
    1959             :   /* TODO: Clean this up; in particular, note that u is factored later on. */
    1960             :   /* This comes from L_bound in oneroot_of_classpoly() above */
    1961        3760 :   maxL = maxdd(log((double) -D), (double)biggest_v);
    1962        3760 :   if (u > 1) {
    1963         847 :     for (L = 2; L <= maxL; L = unextprime(L + 1))
    1964         672 :       if (!(u % L)) polmodular_db_add_level(db, L, INV_J);
    1965             :   }
    1966       15285 :   for (i = 0; vfactors; ++i) {
    1967       11525 :     if (vfactors & 1UL)
    1968       11203 :       polmodular_db_add_level(db, SMALL_PRIMES[i], INV_J);
    1969       11525 :     vfactors >>= 1;
    1970             :   }
    1971        3760 :   if (p1 > 1) polmodular_db_add_level(db, p1, INV_J);
    1972        3760 :   if (p2 > 1) polmodular_db_add_level(db, p2, INV_J);
    1973        3760 :   s = !!G->L0;
    1974        3760 :   polmodular_db_add_levels(db, G->L + s, G->k - s, inv);
    1975        3760 :   if (orient) {
    1976         672 :     for (i = 0; i < G->k; ++i)
    1977             :     {
    1978         413 :       if (G->orient_p[i] > 1) polmodular_db_add_level(db, G->orient_p[i], inv);
    1979         413 :       if (G->orient_q[i] > 1) polmodular_db_add_level(db, G->orient_q[i], inv);
    1980             :     }
    1981             :   }
    1982        3760 :   nprimes = lg(primes) - 1;
    1983        3760 :   H = cgetg(nprimes + 1, t_VEC);
    1984        3760 :   plist = cgetg(nprimes + 1, t_VECSMALL);
    1985        3760 :   pilist = cgetg(nprimes + 1, t_VECSMALL);
    1986      122149 :   for (i = 1; i <= nprimes; ++i) {
    1987      118389 :     long rho_inv = gel(primes, i)[4];
    1988             :     norm_eqn_t ne;
    1989      118389 :     setup_norm_eqn(ne, D, u, gel(primes, i));
    1990             : 
    1991      118389 :     gel(H, i) = polclass_roots_modp(&n_curves_tested, ne, rho_inv, G, *db);
    1992      118389 :     uel(plist, i) = ne->p;
    1993      118389 :     uel(pilist, i) = ne->pi;
    1994      118389 :     if (DEBUGLEVEL>2 && (i & 3L)==0) err_printf(" %ld%%", i*100/nprimes);
    1995             :   }
    1996        3760 :   dbg_printf(0)("\n");
    1997             : 
    1998        3760 :   if (orient) {
    1999         259 :     GEN nvec = new_chunk(G->k);
    2000         259 :     GEN fdb = polmodular_db_for_inv(*db, inv);
    2001         259 :     GEN F = double_eta_raw(inv);
    2002         259 :     index_to_evec((long *)nvec, ni, G->m, G->k);
    2003        6167 :     for (i = 1; i <= nprimes; ++i) {
    2004        5908 :       GEN v = gel(H, i);
    2005        5908 :       ulong p = uel(plist, i), pi = uel(pilist, i);
    2006        5908 :       long oni = oriented_n_action(nvec, G, v, p, pi, fdb);
    2007        5908 :       adjust_orientation(F, inv, v, oni + 1, p, pi);
    2008             :     }
    2009             :   }
    2010             : 
    2011        3760 :   if (modinv_has_sign_ambiguity(inv)) {
    2012             :     GEN psum;
    2013             :     long e;
    2014         525 :     polclass_psum(&psum, &e, H, plist, pilist, h, inv);
    2015       10843 :     for (i = 1; i <= nprimes; ++i) {
    2016       10318 :       GEN v = gel(H, i);
    2017       10318 :       ulong p = uel(plist, i), pi = uel(pilist, i);
    2018       10318 :       if (!adjust_signs(v, p, pi, inv, psum, e))
    2019          14 :         uel(plist, i) = 0;
    2020             :     }
    2021             :   }
    2022             : 
    2023      122149 :   for (i = 1, j = 1, del = 0; i <= nprimes; ++i) {
    2024      118389 :     GEN v = gel(H, i), pol;
    2025      118389 :     ulong p = uel(plist, i);
    2026      118389 :     if (!p) { del++; continue; }
    2027      118375 :     pol = Flv_roots_to_pol(v, p, xvar);
    2028      118375 :     uel(plist, j) = p;
    2029      118375 :     gel(H, j++) = Flx_to_Flv(pol, lg(pol) - 2);
    2030             :   }
    2031        3760 :   setlg(H,nprimes+1-del);
    2032        3760 :   setlg(plist,nprimes+1-del);
    2033        3760 :   classgp_pcp_clear(G);
    2034             : 
    2035        3760 :   dbg_printf(1)("Total number of curves tested: %ld\n", n_curves_tested);
    2036        3760 :   H = ncV_chinese_center(H, plist, &P);
    2037        3760 :   dbg_printf(1)("Result height: %.2f\n",
    2038             :              dbllog2r(itor(gsupnorm(H, DEFAULTPREC), DEFAULTPREC)));
    2039        3760 :   return gerepilecopy(av, RgV_to_RgX(H, xvar));
    2040             : }
    2041             : 
    2042             : void
    2043        1225 : check_modinv(long inv)
    2044             : {
    2045        1225 :   switch (inv) {
    2046             :   case INV_J:
    2047             :   case INV_F:
    2048             :   case INV_F2:
    2049             :   case INV_F3:
    2050             :   case INV_F4:
    2051             :   case INV_G2:
    2052             :   case INV_W2W3:
    2053             :   case INV_F8:
    2054             :   case INV_W3W3:
    2055             :   case INV_W2W5:
    2056             :   case INV_W2W7:
    2057             :   case INV_W3W5:
    2058             :   case INV_W3W7:
    2059             :   case INV_W2W3E2:
    2060             :   case INV_W2W5E2:
    2061             :   case INV_W2W13:
    2062             :   case INV_W2W7E2:
    2063             :   case INV_W3W3E2:
    2064             :   case INV_W5W7:
    2065             :   case INV_W3W13:
    2066        1211 :     break;
    2067             :   default:
    2068          14 :     pari_err_DOMAIN("polmodular", "inv", "invalid invariant", stoi(inv), gen_0);
    2069             :   }
    2070        1211 : }
    2071             : 
    2072             : GEN
    2073         623 : polclass(GEN DD, long inv, long xvar)
    2074             : {
    2075             :   GEN db, H;
    2076             :   long dummy, D;
    2077             : 
    2078         623 :   if (xvar < 0) xvar = 0;
    2079         623 :   check_quaddisc_imag(DD, &dummy, "polclass");
    2080         616 :   check_modinv(inv);
    2081             : 
    2082         609 :   D = itos(DD);
    2083         609 :   if (!modinv_good_disc(inv, D))
    2084           0 :     pari_err_DOMAIN("polclass", "D", "incompatible with given invariant", stoi(inv), DD);
    2085             : 
    2086         609 :   db = polmodular_db_init(inv);
    2087         609 :   H = polclass0(D, inv, xvar, &db);
    2088         609 :   gunclone_deep(db); return H;
    2089             : }

Generated by: LCOV version 1.13