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.14.0 lcov report (development 27775-aca467eab2) Lines: 800 827 96.7 %
Date: 2022-07-03 07:33:15 Functions: 58 59 98.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13