class_group 0.6.0

Rust library for building IQC: cryptography based on class groups (Cl) of imaginary quadratic orders
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
/* Copyright (C) 2000-2004  The PARI group.

This file is part of the PARI/GP package.

PARI/GP is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation. It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY WHATSOEVER.

Check the License for details. You should have received a copy of it, along
with the package; see the file 'COPYING'. If not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */

/*******************************************************************/
/*                                                                 */
/*            POLYNOMIAL FACTORIZATION IN A NUMBER FIELD           */
/*                                                                 */
/*******************************************************************/
#include "pari.h"
#include "paripriv.h"

static GEN nfsqff(GEN nf,GEN pol,long fl,GEN den);
static int nfsqff_use_Trager(long n, long dpol);

enum { FACTORS = 0, ROOTS, ROOTS_SPLIT };

/* for nf_bestlift: reconstruction of algebraic integers known mod P^k,
 * P maximal ideal above p */
typedef struct {
  long k;    /* input known mod P^k */
  GEN p, pk; /* p^k = denom(prk^-1) [ assume pr unramified ]*/
  GEN prk;   /* |.|^2 LLL-reduced basis (b_i) of P^k  (NOT T2-reduced) */
  GEN iprk;  /* den * prk^-1 */
  GEN GSmin; /* min |b_i^*|^2 */

  GEN Tp; /* Tpk mod p */
  GEN Tpk;
  GEN ZqProj;/* projector to Zp / P^k = Z/p^k[X] / Tpk */

  GEN tozk;
  GEN topow;
  GEN topowden; /* topow x / topowden = basistoalg(x) */
  GEN dn; /* NULL (we trust nf.zk) or a t_INT > 1 (an alg. integer has
             denominator dividing dn, when expressed on nf.zk */
} nflift_t;

typedef struct
{
  nflift_t *L;
  GEN nf;
  GEN pol, polbase; /* leading coeff is a t_INT */
  GEN fact;
  GEN Br, bound, ZC, BS_2;
} nfcmbf_t;

/*******************************************************************/
/*              RATIONAL RECONSTRUCTION (use ratlift)              */
/*******************************************************************/
/* NOT stack clean. a, b stay on the stack */
static GEN
lift_to_frac_tdenom(GEN t, GEN mod, GEN amax, GEN bmax, GEN denom, GEN tdenom)
{
  GEN a, b;
  if (signe(t) < 0) t = addii(t, mod); /* in case t is a centerlift */
  if (tdenom)
  {
    pari_sp av = avma;
    a = Fp_center_i(Fp_mul(t, tdenom, mod), mod, shifti(mod,-1));
    if (abscmpii(a, amax) < 0)
    {
      GEN d = gcdii(a, tdenom);
      a = diviiexact(a, d);
      b = diviiexact(tdenom, d);
      if (is_pm1(b)) { return gerepileuptoint(av, a); }
      return gerepilecopy(av, mkfrac(a, b));
    }
    avma = av;
  }
  if (!Fp_ratlift(t, mod, amax,bmax, &a,&b)
     || (denom && !dvdii(denom,b))
     || !is_pm1(gcdii(a,b))) return NULL;
  if (is_pm1(b)) { cgiv(b); return a; }
  return mkfrac(a, b);
}

static GEN
lift_to_frac(GEN t, GEN mod, GEN amax, GEN bmax, GEN denom)
{
  return lift_to_frac_tdenom(t, mod, amax, bmax, denom, NULL);
}

/* Compute rational lifting for all the components of M modulo mod.
 * Assume all Fp_ratlift preconditions are met; we allow centerlifts but in
 * that case are no longer stack clean. If one component fails, return NULL.
 * If denom != NULL, check that the denominators divide denom.
 *
 * We suppose gcd(mod, denom) = 1, then a and b are coprime; so we can use
 * mkfrac rather than gdiv */
GEN
FpC_ratlift(GEN P, GEN mod, GEN amax, GEN bmax, GEN denom)
{
  pari_sp ltop = avma;
  long j, l;
  GEN a, d, tdenom = NULL, Q = cgetg_copy(P, &l);
  if (l==1) return Q;
  for (j = 1; j < l; ++j)
  {
    a = lift_to_frac_tdenom(gel(P,j), mod, amax, bmax, denom, tdenom);
    if (!a) { avma = ltop; return NULL; }
    d = Q_denom(a);
    tdenom = tdenom ? cmpii(tdenom, d)<0? d: tdenom : d;
    gel(Q,j) = a;
  }
  return Q;
}

GEN
FpM_ratlift(GEN M, GEN mod, GEN amax, GEN bmax, GEN denom)
{
  pari_sp av = avma;
  long j, l = lg(M);
  GEN N = cgetg_copy(M, &l);
  if (l == 1) return N;
  for (j = 1; j < l; ++j)
  {
    GEN a = FpC_ratlift(gel(M, j), mod, amax, bmax, denom);
    if (!a) { avma = av; return NULL; }
    gel(N,j) = a;
  }
  return N;
}

GEN
FpX_ratlift(GEN P, GEN mod, GEN amax, GEN bmax, GEN denom)
{
  pari_sp ltop = avma;
  long j, l;
  GEN a, Q = cgetg_copy(P, &l);
  Q[1] = P[1];
  for (j = 2; j < l; ++j)
  {
    a = lift_to_frac(gel(P,j), mod, amax,bmax,denom);
    if (!a) { avma = ltop; return NULL; }
    gel(Q,j) = a;
  }
  return Q;
}

/*******************************************************************/
/*              GCD in K[X], K NUMBER FIELD                        */
/*******************************************************************/
/* P a non-zero ZXQX */
static GEN
lead_simplify(GEN P)
{
  GEN x = gel(P, lg(P)-1); /* x a non-zero ZX or t_INT */
  if (typ(x) == t_POL && !degpol(x)) x = gel(x,2);
  return is_pm1(x)? NULL: x;
}
/* P,Q in Z[X,Y], T in Z[Y] irreducible. compute GCD in Q[Y]/(T)[X].
 *
 * M. Encarnacion "On a modular Algorithm for computing GCDs of polynomials
 * over number fields" (ISSAC'94).
 *
 * We procede as follows
 *  1:compute the gcd modulo primes discarding bad primes as they are detected.
 *  2:reconstruct the result via FpM_ratlift, stoping as soon as we get weird
 *    denominators.
 *  3:if FpM_ratlift succeeds, try the full division.
 * Suppose accuracy is insufficient to get the result right: FpM_ratlift will
 * rarely succeed, and even if it does the polynomial we get has sensible
 * coefficients, so the full division will not be too costly.
 *
 * If not NULL, den must be a multiple of the denominator of the gcd,
 * for example the discriminant of T.
 *
 * NOTE: if T is not irreducible, nfgcd may loop forever, esp. if gcd | T */
GEN
nfgcd_all(GEN P, GEN Q, GEN T, GEN den, GEN *Pnew)
{
  pari_sp btop, ltop = avma;
  GEN lP, lQ, M, dsol, R, bo, sol, mod = NULL, lden = NULL;
  long vP = varn(P), vT = varn(T), dT = degpol(T), dM = 0, dR;
  forprime_t S;

  if (!signe(P)) { if (Pnew) *Pnew = pol_0(vT); return gcopy(Q); }
  if (!signe(Q)) { if (Pnew) *Pnew = pol_1(vT);   return gcopy(P); }
  /* Compute denominators */
  if ((lP = lead_simplify(P)) && (lQ = lead_simplify(Q)))
  {
    if (typ(lP) == t_INT && typ(lQ) == t_INT)
      lden = powiu(gcdii(lP, lQ), dT);
    else if (typ(lP) == t_INT)
      lden = gcdii(powiu(lP, dT), ZX_resultant(lQ, T));
    else if (typ(lQ) == t_INT)
      lden = gcdii(powiu(lQ, dT), ZX_resultant(lP, T));
    else
      lden = gcdii(ZX_resultant(lP, T), ZX_resultant(lQ, T));
    if (is_pm1(lden)) lden = NULL;
    den = mul_denom(den, lden);
  }
  init_modular_small(&S);
  btop = avma;
  for(;;)
  {
    ulong p = u_forprime_next(&S);
    GEN Tp;
    if (!p) pari_err_OVERFLOW("nfgcd [ran out of primes]");
    /*Discard primes dividing disc(T) or lc(PQ) */
    if (lden && !umodiu(lden, p)) continue;
    Tp = ZX_to_Flx(T,p);
    if (!Flx_is_squarefree(Tp, p)) continue;
    /*Discard primes when modular gcd does not exist*/
    if ((R = FlxqX_safegcd(ZXX_to_FlxX(P,p,vT),
                           ZXX_to_FlxX(Q,p,vT),
                           Tp, p)) == NULL) continue;
    dR = degpol(R);
    if (dR == 0) { avma = ltop; if (Pnew) *Pnew = P; return pol_1(vP); }
    if (mod && dR > dM) continue; /* p divides Res(P/gcd, Q/gcd). Discard. */

    R = FlxX_to_Flm(R, dT);
    /* previous primes divided Res(P/gcd, Q/gcd)? Discard them. */
    if (!mod || dR < dM) { M = ZM_init_CRT(R, p); mod = utoipos(p); dM = dR; continue; }
    (void)ZM_incremental_CRT(&M,R, &mod,p);
    if (gc_needed(btop, 1))
    {
      if (DEBUGMEM>1) pari_warn(warnmem,"nfgcd");
      gerepileall(btop, 2, &M, &mod);
    }
    /* I suspect it must be better to take amax > bmax*/
    bo = sqrti(shifti(mod, -1));
    if ((sol = FpM_ratlift(M, mod, bo, bo, den)) == NULL) continue;
    sol = RgM_to_RgXX(sol,vP,vT);
    dsol = Q_primpart(sol);

    if (!ZXQX_dvd(Q, dsol, T)) continue;
    if (Pnew)
    {
      *Pnew = RgXQX_pseudodivrem(P, dsol, T, &R);
      if (signe(R)) continue;
    }
    else
    {
      if (!ZXQX_dvd(P, dsol, T)) continue;
    }
    gerepileall(ltop, Pnew? 2: 1, &dsol, Pnew);
    return dsol; /* both remainders are 0 */
  }
}
GEN
nfgcd(GEN P, GEN Q, GEN T, GEN den)
{ return nfgcd_all(P,Q,T,den,NULL); }

int
nfissquarefree(GEN nf, GEN x)
{
  pari_sp av = avma;
  GEN g, y = RgX_deriv(x);
  if (RgX_is_rational(x))
    g = QX_gcd(x, y);
  else
  {
    GEN T = get_nfpol(nf,&nf);
    x = Q_primpart( liftpol_shallow(x) );
    y = Q_primpart( liftpol_shallow(y) );
    g = nfgcd(x, y, T, nf? nf_get_index(nf): NULL);
  }
  avma = av; return (degpol(g) == 0);
}

/*******************************************************************/
/*             FACTOR OVER (Z_K/pr)[X] --> FqX_factor              */
/*******************************************************************/
GEN
nffactormod(GEN nf, GEN x, GEN pr)
{
  long j, l, vx = varn(x), vn;
  pari_sp av = avma;
  GEN F, E, rep, xrd, modpr, T, p;

  nf = checknf(nf);
  vn = nf_get_varn(nf);
  if (typ(x)!=t_POL) pari_err_TYPE("nffactormod",x);
  if (varncmp(vx,vn) >= 0) pari_err_PRIORITY("nffactormod", x, ">=", vn);

  modpr = nf_to_Fq_init(nf, &pr, &T, &p);
  xrd = nfX_to_FqX(x, nf, modpr);
  rep = FqX_factor(xrd,T,p);
  settyp(rep, t_MAT);
  F = gel(rep,1); l = lg(F);
  E = gel(rep,2); settyp(E, t_COL);
  for (j = 1; j < l; j++) {
    gel(F,j) = FqX_to_nfX(gel(F,j), modpr);
    gel(E,j) = stoi(E[j]);
  }
  return gerepilecopy(av, rep);
}

/*******************************************************************/
/*               MAIN ROUTINES nfroots / nffactor                  */
/*******************************************************************/
static GEN
QXQX_normalize(GEN P, GEN T)
{
  GEN P0 = leading_coeff(P);
  long t = typ(P0);
  if (t == t_POL)
  {
    if (degpol(P0)) return RgXQX_RgXQ_mul(P, QXQ_inv(P0,T), T);
    P0 = gel(P0,2); t = typ(P0);
  }
  /* t = t_INT/t_FRAC */
  if (t == t_INT && is_pm1(P0) && signe(P0) > 0) return P; /* monic */
  return RgX_Rg_div(P, P0);
}
/* assume leading term of P is an integer */
static GEN
RgX_int_normalize(GEN P)
{
  GEN P0 = leading_coeff(P);
  /* cater for t_POL */
  if (typ(P0) == t_POL)
  {
    P0 = gel(P0,2); /* non-0 constant */
    P = shallowcopy(P);
    gel(P,lg(P)-1) = P0; /* now leading term is a t_INT */
  }
  if (typ(P0) != t_INT) pari_err_BUG("RgX_int_normalize");
  if (is_pm1(P0)) return signe(P0) > 0? P: RgX_neg(P);
  return RgX_Rg_div(P, P0);
}

/* discard change of variable if nf is of the form [nf,c] as return by nfinit
 * for non-monic polynomials */
static GEN
proper_nf(GEN nf)
{ return (lg(nf) == 3)? gel(nf,1): nf; }

/* if *pnf = NULL replace if by a "quick" K = nfinit(T), ensuring maximality
 * by small primes only. Return a multiplicative bound for the denominator of
 * algebraic integers in Z_K in terms of K.zk */
static GEN
fix_nf(GEN *pnf, GEN *pT, GEN *pA)
{
  GEN nf, NF, fa, P, Q, q, D, T = *pT;
  nfmaxord_t S;
  long i, l;

  if (*pnf) return gen_1;
  nfmaxord(&S, T, nf_PARTIALFACT);
  NF = nfinit_complete(&S, 0, DEFAULTPREC);
  *pnf = nf = proper_nf(NF);
  if (nf != NF) { /* t_POL defining base field changed (not monic) */
    GEN A = *pA, a = cgetg_copy(A, &l);
    GEN rev = gel(NF,2), pow, dpow;

    *pT = T = nf_get_pol(nf); /* need to update T */
    pow = QXQ_powers(lift_shallow(rev), degpol(T)-1, T);
    pow = Q_remove_denom(pow, &dpow);
    a[1] = A[1];
    for (i=2; i<l; i++) {
      GEN c = gel(A,i);
      if (typ(c) == t_POL) c = QX_ZXQV_eval(c, pow, dpow);
      gel(a,i) = c;
    }
    *pA = Q_primpart(a); /* need to update A */
  }

  D = nf_get_disc(nf);
  if (is_pm1(D)) return gen_1;
  fa = absZ_factor_limit(D, 0);
  P = gel(fa,1); q = gel(P, lg(P)-1);
  if (BPSW_psp(q)) return gen_1;
  /* nf_get_disc(nf) may be incorrect */
  P = nf_get_ramified_primes(nf);
  l = lg(P);
  Q = q; q = gen_1;
  for (i = 1; i < l; i++)
  {
    GEN p = gel(P,i);
    if (Z_pvalrem(Q, p, &Q) && !BPSW_psp(p)) q = mulii(q, p);
  }
  return q;
}

/* lt(A) is an integer; ensure it is not a constant t_POL. In place */
static void
ensure_lt_INT(GEN A)
{
  long n = lg(A)-1;
  GEN lt = gel(A,n);
  while (typ(lt) != t_INT) gel(A,n) = lt = gel(lt,2);
}

/* set B = A/gcd(A,A'), squarefree */
static GEN
get_nfsqff_data(GEN *pnf, GEN *pT, GEN *pA, GEN *pB, GEN *ptbad)
{
  GEN den, bad, D, B, A = *pA, T = *pT;
  long n = degpol(T);

  A = Q_primpart( QXQX_normalize(A, T) );
  if (nfsqff_use_Trager(n, degpol(A)))
  {
    *pnf = T;
    bad = den = ZX_disc(T);
    if (is_pm1(leading_coeff(T))) den = indexpartial(T, den);
  }
  else
  {
    den = fix_nf(pnf, &T, &A);
    bad = nf_get_index(*pnf);
    if (den != gen_1) bad = mulii(bad, den);
  }
  D = nfgcd_all(A, RgX_deriv(A), T, bad, &B);
  if (degpol(D)) B = Q_primpart( QXQX_normalize(B, T) );
  if (ptbad) *ptbad = bad;
  *pA = A;
  *pB = B; ensure_lt_INT(B);
  *pT = T; return den;
}

/* return the roots of pol in nf */
GEN
nfroots(GEN nf,GEN pol)
{
  pari_sp av = avma;
  GEN z, A, B, T, den;
  long d, dT;

  if (!nf) return nfrootsQ(pol);
  T = get_nfpol(nf, &nf);
  RgX_check_ZX(T,"nfroots");
  A = RgX_nffix("nfroots", T,pol,1);
  d = degpol(A);
  if (d < 0) pari_err_ROOTS0("nfroots");
  if (d == 0) return cgetg(1,t_VEC);
  if (d == 1)
  {
    A = QXQX_normalize(A,T);
    A = mkpolmod(gneg_i(gel(A,2)), T);
    return gerepilecopy(av, mkvec(A));
  }
  dT = degpol(T);
  if (dT == 1) return gerepileupto(av, nfrootsQ(simplify_shallow(A)));

  den = get_nfsqff_data(&nf, &T, &A, &B, NULL);
  if (RgX_is_ZX(B))
  {
    GEN v = gel(ZX_factor(B), 1);
    long i, l = lg(v), p = mael(factoru(dT),1,1); /* smallest prime divisor */
    z = cgetg(1, t_VEC);
    for (i = 1; i < l; i++)
    {
      GEN b = gel(v,i); /* irreducible / Q */
      long db = degpol(b);
      if (db != 1 && degpol(b) < p) continue;
      z = shallowconcat(z, nfsqff(nf, b, ROOTS, den));
    }
  }
  else
    z = nfsqff(nf,B, ROOTS, den);
  z = gerepileupto(av, QXQV_to_mod(z, T));
  gen_sort_inplace(z, (void*)&cmp_RgX, &cmp_nodata, NULL);
  return z;
}

static GEN
_norml2(GEN x) { return RgC_fpnorml2(x, DEFAULTPREC); }

/* return a minimal lift of elt modulo id, as a ZC */
static GEN
nf_bestlift(GEN elt, GEN bound, nflift_t *L)
{
  GEN u;
  long i,l = lg(L->prk), t = typ(elt);
  if (t != t_INT)
  {
    if (t == t_POL) elt = ZM_ZX_mul(L->tozk, elt);
    u = ZM_ZC_mul(L->iprk,elt);
    for (i=1; i<l; i++) gel(u,i) = diviiround(gel(u,i), L->pk);
  }
  else
  {
    u = ZC_Z_mul(gel(L->iprk,1), elt);
    for (i=1; i<l; i++) gel(u,i) = diviiround(gel(u,i), L->pk);
    elt = scalarcol(elt, l-1);
  }
  u = ZC_sub(elt, ZM_ZC_mul(L->prk, u));
  if (bound && gcmp(_norml2(u), bound) > 0) u = NULL;
  return u;
}

/* Warning: return L->topowden * (best lift). */
static GEN
nf_bestlift_to_pol(GEN elt, GEN bound, nflift_t *L)
{
  pari_sp av = avma;
  GEN u,v = nf_bestlift(elt,bound,L);
  if (!v) return NULL;
  if (ZV_isscalar(v))
  {
    if (L->topowden)
      u = mulii(L->topowden, gel(v,1));
    else
      u = icopy(gel(v,1));
    u = gerepileuptoint(av, u);
  }
  else
  {
    v = gclone(v); avma = av;
    u = RgV_dotproduct(L->topow, v);
    gunclone(v);
  }
  return u;
}

/* return the T->powden * (lift of pol with coefficients of T2-norm <= C)
 * if it exists. */
static GEN
nf_pol_lift(GEN pol, GEN bound, nflift_t *L)
{
  long i, l = lg(pol);
  GEN x = cgetg(l,t_POL);

  x[1] = pol[1];
  gel(x,l-1) = mul_content(gel(pol,l-1), L->topowden);
  for (i=l-2; i>1; i--)
  {
    GEN t = nf_bestlift_to_pol(gel(pol,i), bound, L);
    if (!t) return NULL;
    gel(x,i) = t;
  }
  return x;
}

static GEN
zerofact(long v)
{
  GEN z = cgetg(3, t_MAT);
  gel(z,1) = mkcol(pol_0(v));
  gel(z,2) = mkcol(gen_1); return z;
}

/* Return the factorization of A in Q[X]/(T) in rep [pre-allocated with
 * cgetg(3,t_MAT)], reclaiming all memory between avma and rep.
 * y is the vector of irreducible factors of B = Q_primpart( A/gcd(A,A') ).
 * Bad primes divide 'bad' */
static void
fact_from_sqff(GEN rep, GEN A, GEN B, GEN y, GEN T, GEN bad)
{
  pari_sp av = (pari_sp)rep;
  long n = lg(y)-1;
  GEN ex;

  if (A != B)
  { /* not squarefree */
    if (n == 1)
    { /* perfect power, simple ! */
      long e = degpol(A) / degpol(gel(y,1));
      y = gerepileupto(av, QXQXV_to_mod(y, T));
      ex = mkcol(utoipos(e));
    }
    else
    { /* compute valuations mod a prime of degree 1 (avoid coeff explosion) */
      GEN quo, p, r, Bp, lb = leading_coeff(B), E = cgetalloc(t_VECSMALL,n+1);
      pari_sp av1 = avma;
      ulong pp;
      long j;
      forprime_t S;
      u_forprime_init(&S, degpol(T), ULONG_MAX);
      for (; ; avma = av1)
      {
        pp = u_forprime_next(&S);
        if (! umodiu(bad,pp) || !umodiu(lb, pp)) continue;
        p = utoipos(pp);
        r = FpX_oneroot(T, p);
        if (!r) continue;
        Bp = FpXY_evalx(B, r, p);
        if (FpX_is_squarefree(Bp, p)) break;
      }

      quo = FpXY_evalx(Q_primpart(A), r, p);
      for (j=n; j>=2; j--)
      {
        GEN junk, fact = Q_remove_denom(gel(y,j), &junk);
        long e = 0;
        fact = FpXY_evalx(fact, r, p);
        for(;; e++)
        {
          GEN q = FpX_divrem(quo,fact,p,ONLY_DIVIDES);
          if (!q) break;
          quo = q;
        }
        E[j] = e;
      }
      E[1] = degpol(quo) / degpol(gel(y,1));
      y = gerepileupto(av, QXQXV_to_mod(y, T));
      ex = zc_to_ZC(E); pari_free((void*)E);
    }
  }
  else
  {
    y = gerepileupto(av, QXQXV_to_mod(y, T));
    ex = const_col(n, gen_1);
  }
  gel(rep,1) = y; settyp(y, t_COL);
  gel(rep,2) = ex;
}

/* return the factorization of x in nf */
GEN
nffactor(GEN nf,GEN pol)
{
  GEN bad, A, B, y, T, den, rep = cgetg(3, t_MAT);
  pari_sp av = avma;
  long dA;
  pari_timer ti;

  if (DEBUGLEVEL>2) { timer_start(&ti); err_printf("\nEntering nffactor:\n"); }
  T = get_nfpol(nf, &nf);
  RgX_check_ZX(T,"nffactor");
  A = RgX_nffix("nffactor",T,pol,1);
  dA = degpol(A);
  if (dA <= 0) {
    avma = (pari_sp)(rep + 3);
    return (dA == 0)? trivial_fact(): zerofact(varn(pol));
  }
  if (dA == 1) {
    GEN c;
    A = Q_primpart( QXQX_normalize(A, T) );
    A = gerepilecopy(av, A); c = gel(A,2);
    if (typ(c) == t_POL && degpol(c) > 0) gel(A,2) = mkpolmod(c, ZX_copy(T));
    gel(rep,1) = mkcol(A);
    gel(rep,2) = mkcol(gen_1); return rep;
  }
  if (degpol(T) == 1) return gerepileupto(av, QX_factor(simplify_shallow(A)));

  den = get_nfsqff_data(&nf, &T, &A, &B, &bad);
  if (DEBUGLEVEL>2) timer_printf(&ti, "squarefree test");
  if (RgX_is_ZX(B))
  {
    GEN v = gel(ZX_factor(B), 1);
    long i, l = lg(v);
    y = cgetg(1, t_VEC);
    for (i = 1; i < l; i++)
    {
      GEN b = gel(v,i); /* irreducible / Q */
      y = shallowconcat(y, nfsqff(nf, b, 0, den));
    }
  }
  else
    y = nfsqff(nf,B, 0, den);
  if (DEBUGLEVEL>3) err_printf("number of factor(s) found: %ld\n", lg(y)-1);

  fact_from_sqff(rep, A, B, y, T, bad);
  return sort_factor_pol(rep, cmp_RgX);
}

/* assume x scalar or t_COL, G t_MAT */
static GEN
arch_for_T2(GEN G, GEN x)
{
  return (typ(x) == t_COL)? RgM_RgC_mul(G,x)
                          : RgC_Rg_mul(gel(G,1),x);
}

/* polbase a zkX with t_INT leading coeff; return a bound for T_2(P),
 * P | polbase in C[X]. NB: Mignotte bound: A | S ==>
 *  |a_i| <= binom(d-1, i-1) || S ||_2 + binom(d-1, i) lc(S)
 *
 * Apply to sigma(S) for all embeddings sigma, then take the L_2 norm over
 * sigma, then take the sup over i */
static GEN
nf_Mignotte_bound(GEN nf, GEN polbase)
{ GEN lS = leading_coeff(polbase); /* t_INT */
  GEN p1, C, N2, binlS, bin;
  long prec = nf_get_prec(nf), n = nf_get_degree(nf), r1 = nf_get_r1(nf);
  long i, j, d = degpol(polbase);

  binlS = bin = vecbinomial(d-1);
  if (!isint1(lS)) binlS = ZC_Z_mul(bin,lS);

  N2 = cgetg(n+1, t_VEC);
  for (;;)
  {
    GEN G = nf_get_G(nf), matGS = cgetg(d+2, t_MAT);

    for (j=0; j<=d; j++) gel(matGS,j+1) = arch_for_T2(G, gel(polbase,j+2));
    matGS = shallowtrans(matGS);
    for (j=1; j <= r1; j++) /* N2[j] = || sigma_j(S) ||_2 */
    {
      GEN c = sqrtr( _norml2(gel(matGS,j)) );
      gel(N2,j) = c; if (!signe(c)) goto PRECPB;
    }
    for (   ; j <= n; j+=2)
    {
      GEN q1 = _norml2(gel(matGS, j));
      GEN q2 = _norml2(gel(matGS, j+1));
      GEN c = sqrtr( gmul2n(addrr(q1, q2), -1) );
      gel(N2,j) = gel(N2,j+1) = c; if (!signe(c)) goto PRECPB;
    }
    break; /* done */
PRECPB:
    prec = precdbl(prec);
    nf = nfnewprec_shallow(nf, prec);
    if (DEBUGLEVEL>1) pari_warn(warnprec, "nf_factor_bound", prec);
  }

  /* Take sup over 0 <= i <= d of
   * sum_j | binom(d-1, i-1) ||sigma_j(S)||_2 + binom(d-1,i) lc(S) |^2 */

  /* i = 0: n lc(S)^2 */
  C = mului(n, sqri(lS));
  /* i = d: sum_sigma ||sigma(S)||_2^2 */
  p1 = gnorml2(N2); if (gcmp(C, p1) < 0) C = p1;
  for (i = 1; i < d; i++)
  {
    GEN B = gel(bin,i), L = gel(binlS,i+1);
    GEN s = sqrr(addri(mulir(B, gel(N2,1)),  L)); /* j=1 */
    for (j = 2; j <= n; j++) s = addrr(s, sqrr(addri(mulir(B, gel(N2,j)), L)));
    if (mpcmp(C, s) < 0) C = s;
  }
  return C;
}

/* return a bound for T_2(P), P | polbase
 * max |b_i|^2 <= 3^{3/2 + d} / (4 \pi d) [P]_2,
 * where [P]_2 is Bombieri's 2-norm
 * Sum over conjugates */
static GEN
nf_Beauzamy_bound(GEN nf, GEN polbase)
{
  GEN lt, C, s, POL, bin;
  long d = degpol(polbase), n = nf_get_degree(nf), prec = nf_get_prec(nf);
  bin = vecbinomial(d);
  POL = polbase + 2;
  /* compute [POL]_2 */
  for (;;)
  {
    GEN G = nf_get_G(nf);
    long i;

    s = real_0(prec);
    for (i=0; i<=d; i++)
    {
      GEN c = gel(POL,i);
      if (gequal0(c)) continue;
      c = _norml2(arch_for_T2(G,c));
      if (!signe(c)) goto PRECPB;
      /* s += T2(POL[i]) / binomial(d,i) */
      s = addrr(s, divri(c, gel(bin,i+1)));
    }
    break;
PRECPB:
    prec = precdbl(prec);
    nf = nfnewprec_shallow(nf, prec);
    if (DEBUGLEVEL>1) pari_warn(warnprec, "nf_factor_bound", prec);
  }
  lt = leading_coeff(polbase);
  s = mulri(s, muliu(sqri(lt), n));
  C = powruhalf(stor(3,DEFAULTPREC), 3 + 2*d); /* 3^{3/2 + d} */
  return divrr(mulrr(C, s), mulur(d, mppi(DEFAULTPREC)));
}

static GEN
nf_factor_bound(GEN nf, GEN polbase)
{
  pari_sp av = avma;
  GEN a = nf_Mignotte_bound(nf, polbase);
  GEN b = nf_Beauzamy_bound(nf, polbase);
  if (DEBUGLEVEL>2)
  {
    err_printf("Mignotte bound: %Ps\n",a);
    err_printf("Beauzamy bound: %Ps\n",b);
  }
  return gerepileupto(av, gmin(a, b));
}

/* True nf; return Bs: if r a root of sigma_i(P), |r| < Bs[i] */
static GEN
nf_root_bounds(GEN nf, GEN P)
{
  long lR, i, j, l, prec, r1;
  GEN Ps, R, V;

  if (RgX_is_rational(P)) return polrootsbound(P, NULL);
  r1 = nf_get_r1(nf);
  P = Q_primpart(P);
  prec = ZXX_max_lg(P) + 1;
  l = lg(P);
  if (nf_get_prec(nf) >= prec)
    R = nf_get_roots(nf);
  else
    R = QX_complex_roots(nf_get_pol(nf), prec);
  lR = lg(R);
  V = cgetg(lR, t_VEC);
  Ps = cgetg(l, t_POL); /* sigma (P) */
  Ps[1] = P[1];
  for (j=1; j<lg(R); j++)
  {
    GEN r = gel(R,j);
    for (i=2; i<l; i++) gel(Ps,i) = poleval(gel(P,i), r);
    gel(V,j) = polrootsbound(Ps, NULL);
  }
  return mkvec2(vecslice(V,1,r1), vecslice(V,r1+1,lg(V)-1));
}

/* return B such that, if x = sum x_i K.zk[i] in O_K, then ||x||_2^2 <= B T_2(x)
 * den = multiplicative bound for denom(x) [usually NULL, for 1, but when we
 * use nf_PARTIALFACT K.zk may not generate O_K] */
static GEN
L2_bound(GEN nf, GEN den)
{
  GEN M, L, prep, T = nf_get_pol(nf), tozk = nf_get_invzk(nf);
  long prec = ZM_max_lg(tozk) + ZX_max_lg(T) + nbits2prec(degpol(T));
  (void)initgaloisborne(nf, den? den: gen_1, prec, &L, &prep, NULL);
  M = vandermondeinverse(L, RgX_gtofp(T,prec), den, prep);
  return RgM_fpnorml2(RgM_mul(tozk,M), DEFAULTPREC);
}

/* sum_i L[i]^p */
static GEN
normlp(GEN L, long p)
{
  long i, l = lg(L);
  GEN z;
  if (l == 1) return gen_0;
  z = gpowgs(gel(L,1), p);
  for (i=2; i<l; i++) z = gadd(z, gpowgs(gel(L,i), p));
  return z;
}
/* \sum_i deg(sigma_i) L[i]^p in dimension n (L may be a scalar
 * or [L1,L2], where Ld corresponds to the archimedean places of degree d) */
static GEN
normTp(GEN L, long p, long n)
{
  if (typ(L) != t_VEC) return gmulsg(n, gpowgs(L, p));
  return gadd(normlp(gel(L,1),p), gmul2n(normlp(gel(L,2),p), 1));
}

/* S = S0 + tS1, P = P0 + tP1 (Euclidean div. by t integer). For a true
 * factor (vS, vP), we have:
 *    | S vS + P vP |^2 < Btra
 * This implies | S1 vS + P1 vP |^2 < Bhigh, assuming t > sqrt(Btra).
 * d = dimension of low part (= [nf:Q])
 * n0 = bound for |vS|^2
 * */
static double
get_Bhigh(long n0, long d)
{
  double sqrtd = sqrt((double)d);
  double z = n0*sqrtd + sqrtd/2 * (d * (n0+1));
  z = 1. + 0.5 * z; return z * z;
}

typedef struct {
  GEN d;
  GEN dPinvS;   /* d P^(-1) S   [ integral ] */
  double **PinvSdbl; /* P^(-1) S as double */
  GEN S1, P1;   /* S = S0 + S1 q, idem P */
} trace_data;

/* S1 * u - P1 * round(P^-1 S u). K non-zero coords in u given by ind */
static GEN
get_trace(GEN ind, trace_data *T)
{
  long i, j, l, K = lg(ind)-1;
  GEN z, s, v;

  s = gel(T->S1, ind[1]);
  if (K == 1) return s;

  /* compute s = S1 u */
  for (j=2; j<=K; j++) s = ZC_add(s, gel(T->S1, ind[j]));

  /* compute v := - round(P^1 S u) */
  l = lg(s);
  v = cgetg(l, t_VECSMALL);
  for (i=1; i<l; i++)
  {
    double r, t = 0.;
    /* quick approximate computation */
    for (j=1; j<=K; j++) t += T->PinvSdbl[ ind[j] ][i];
    r = floor(t + 0.5);
    if (fabs(t + 0.5 - r) < 0.0001)
    { /* dubious, compute exactly */
      z = gen_0;
      for (j=1; j<=K; j++) z = addii(z, ((GEN**)T->dPinvS)[ ind[j] ][i]);
      v[i] = - itos( diviiround(z, T->d) );
    }
    else
      v[i] = - (long)r;
  }
  return ZC_add(s, ZM_zc_mul(T->P1, v));
}

static trace_data *
init_trace(trace_data *T, GEN S, nflift_t *L, GEN q)
{
  long e = gexpo(S), i,j, l,h;
  GEN qgood, S1, invd;

  if (e < 0) return NULL; /* S = 0 */

  qgood = int2n(e - 32); /* single precision check */
  if (cmpii(qgood, q) > 0) q = qgood;

  S1 = gdivround(S, q);
  if (gequal0(S1)) return NULL;

  invd = invr(itor(L->pk, DEFAULTPREC));

  T->dPinvS = ZM_mul(L->iprk, S);
  l = lg(S);
  h = lgcols(T->dPinvS);
  T->PinvSdbl = (double**)cgetg(l, t_MAT);
  for (j = 1; j < l; j++)
  {
    double *t = (double *) stack_malloc_align(h * sizeof(double), sizeof(double));
    GEN c = gel(T->dPinvS,j);
    pari_sp av = avma;
    T->PinvSdbl[j] = t;
    for (i=1; i < h; i++) t[i] = rtodbl(mulri(invd, gel(c,i)));
    avma = av;
  }

  T->d  = L->pk;
  T->P1 = gdivround(L->prk, q);
  T->S1 = S1; return T;
}

static void
update_trace(trace_data *T, long k, long i)
{
  if (!T) return;
  gel(T->S1,k)     = gel(T->S1,i);
  gel(T->dPinvS,k) = gel(T->dPinvS,i);
  T->PinvSdbl[k]   = T->PinvSdbl[i];
}

/* reduce coeffs mod (T,pk), then center mod pk */
static GEN
FqX_centermod(GEN z, GEN T, GEN pk, GEN pks2)
{
  long i, l;
  GEN y;
  if (!T) return centermod_i(z, pk, pks2);
  y = FpXQX_red(z, T, pk); l = lg(y);
  for (i = 2; i < l; i++)
  {
    GEN c = gel(y,i);
    if (typ(c) == t_INT)
      c = Fp_center_i(c, pk, pks2);
    else
      c = FpX_center_i(c, pk, pks2);
    gel(y,i) = c;
  }
  return y;
}

typedef struct {
  GEN lt, C, Clt, C2lt, C2ltpol;
} div_data;

static void
init_div_data(div_data *D, GEN pol, nflift_t *L)
{
  GEN C2lt, Clt, C = mul_content(L->topowden, L->dn);
  GEN lc = leading_coeff(pol), lt = is_pm1(lc)? NULL: absi_shallow(lc);
  if (C)
  {
    GEN C2 = sqri(C);
    if (lt) {
      C2lt = mulii(C2, lt);
      Clt = mulii(C,lt);
    } else {
      C2lt = C2;
      Clt = C;
    }
  }
  else
    C2lt = Clt = lt;
  D->lt = lt;
  D->C = C;
  D->Clt = Clt;
  D->C2lt = C2lt;
  D->C2ltpol = C2lt? RgX_Rg_mul(pol, C2lt): pol;
}
static void
update_target(div_data *D, GEN pol)
{ D->C2ltpol = D->Clt? RgX_Rg_mul(pol, D->Clt): pol; }

/* nb = number of modular factors; return a "good" K such that naive
 * recombination of up to maxK modular factors is not too costly */
long
cmbf_maxK(long nb)
{
  if (nb >  10) return 3;
  return nb-1;
}
/* Naive recombination of modular factors: combine up to maxK modular
 * factors, degree <= klim
 *
 * target = polynomial we want to factor
 * famod = array of modular factors.  Product should be congruent to
 * target/lc(target) modulo p^a
 * For true factors: S1,S2 <= p^b, with b <= a and p^(b-a) < 2^31 */
/* set *done = 1 if factorisation is known to be complete */
static GEN
nfcmbf(nfcmbf_t *T, long klim, long *pmaxK, int *done)
{
  GEN nf = T->nf, famod = T->fact, bound = T->bound;
  GEN ltdn, nfpol = nf_get_pol(nf);
  long K = 1, cnt = 1, i,j,k, curdeg, lfamod = lg(famod)-1, dnf = degpol(nfpol);
  pari_sp av0 = avma;
  GEN Tpk = T->L->Tpk, pk = T->L->pk, pks2 = shifti(pk,-1);
  GEN ind      = cgetg(lfamod+1, t_VECSMALL);
  GEN deg      = cgetg(lfamod+1, t_VECSMALL);
  GEN degsofar = cgetg(lfamod+1, t_VECSMALL);
  GEN fa       = cgetg(lfamod+1, t_VEC);
  const double Bhigh = get_Bhigh(lfamod, dnf);
  trace_data _T1, _T2, *T1, *T2;
  div_data D;
  pari_timer ti;

  timer_start(&ti);

  *pmaxK = cmbf_maxK(lfamod);
  init_div_data(&D, T->pol, T->L);
  ltdn = mul_content(D.lt, T->L->dn);
  {
    GEN q = ceil_safe(sqrtr(T->BS_2));
    GEN t1,t2, lt2dn = mul_content(ltdn, D.lt);
    GEN trace1   = cgetg(lfamod+1, t_MAT);
    GEN trace2   = cgetg(lfamod+1, t_MAT);
    for (i=1; i <= lfamod; i++)
    {
      pari_sp av = avma;
      GEN P = gel(famod,i);
      long d = degpol(P);

      deg[i] = d; P += 2;
      t1 = gel(P,d-1);/* = - S_1 */
      t2 = Fq_sqr(t1, Tpk, pk);
      if (d > 1) t2 = Fq_sub(t2, gmul2n(gel(P,d-2), 1), Tpk, pk);
      /* t2 = S_2 Newton sum */
      if (ltdn)
      {
        t1 = Fq_Fp_mul(t1, ltdn, Tpk, pk);
        t2 = Fq_Fp_mul(t2, lt2dn, Tpk, pk);
      }
      gel(trace1,i) = gclone( nf_bestlift(t1, NULL, T->L) );
      gel(trace2,i) = gclone( nf_bestlift(t2, NULL, T->L) ); avma = av;
    }
    T1 = init_trace(&_T1, trace1, T->L, q);
    T2 = init_trace(&_T2, trace2, T->L, q);
    for (i=1; i <= lfamod; i++) {
      gunclone(gel(trace1,i));
      gunclone(gel(trace2,i));
    }
  }
  degsofar[0] = 0; /* sentinel */

  /* ind runs through strictly increasing sequences of length K,
   * 1 <= ind[i] <= lfamod */
nextK:
  if (K > *pmaxK || 2*K > lfamod) goto END;
  if (DEBUGLEVEL > 3)
    err_printf("\n### K = %d, %Ps combinations\n", K,binomial(utoipos(lfamod), K));
  setlg(ind, K+1); ind[1] = 1;
  i = 1; curdeg = deg[ind[1]];
  for(;;)
  { /* try all combinations of K factors */
    for (j = i; j < K; j++)
    {
      degsofar[j] = curdeg;
      ind[j+1] = ind[j]+1; curdeg += deg[ind[j+1]];
    }
    if (curdeg <= klim) /* trial divide */
    {
      GEN t, y, q;
      pari_sp av;

      av = avma;
      if (T1)
      { /* d-1 test */
        t = get_trace(ind, T1);
        if (rtodbl(_norml2(t)) > Bhigh)
        {
          if (DEBUGLEVEL>6) err_printf(".");
          avma = av; goto NEXT;
        }
      }
      if (T2)
      { /* d-2 test */
        t = get_trace(ind, T2);
        if (rtodbl(_norml2(t)) > Bhigh)
        {
          if (DEBUGLEVEL>3) err_printf("|");
          avma = av; goto NEXT;
        }
      }
      avma = av;
      y = ltdn; /* full computation */
      for (i=1; i<=K; i++)
      {
        GEN q = gel(famod, ind[i]);
        if (y) q = gmul(y, q);
        y = FqX_centermod(q, Tpk, pk, pks2);
      }
      y = nf_pol_lift(y, bound, T->L);
      if (!y)
      {
        if (DEBUGLEVEL>3) err_printf("@");
        avma = av; goto NEXT;
      }
      /* y = topowden*dn*lt*\prod_{i in ind} famod[i] is apparently in O_K[X],
       * in fact in (Z[Y]/nf.pol)[X] due to multiplication by C = topowden*dn.
       * Try out this candidate factor */
      q = RgXQX_divrem(D.C2ltpol, y, nfpol, ONLY_DIVIDES);
      if (!q)
      {
        if (DEBUGLEVEL>3) err_printf("*");
        avma = av; goto NEXT;
      }
      /* Original T->pol in O_K[X] with leading coeff lt in Z,
       * y = C*lt \prod famod[i] is in O_K[X] with leading coeff in Z
       * q = C^2*lt*pol / y = C * (lt*pol) / (lt*\prod famod[i]) is a
       * K-rational factor, in fact in Z[Y]/nf.pol)[X] as above, with
       * leading term C*lt. */
      update_target(&D, q);
      gel(fa,cnt++) = D.C2lt? RgX_int_normalize(y): y; /* make monic */
      for (i=j=k=1; i <= lfamod; i++)
      { /* remove used factors */
        if (j <= K && i == ind[j]) j++;
        else
        {
          gel(famod,k) = gel(famod,i);
          update_trace(T1, k, i);
          update_trace(T2, k, i);
          deg[k] = deg[i]; k++;
        }
      }
      lfamod -= K;
      *pmaxK = cmbf_maxK(lfamod);
      if (lfamod < 2*K) goto END;
      i = 1; curdeg = deg[ind[1]];
      if (DEBUGLEVEL > 2)
      {
        err_printf("\n"); timer_printf(&ti, "to find factor %Ps",gel(fa,cnt-1));
        err_printf("remaining modular factor(s): %ld\n", lfamod);
      }
      continue;
    }

NEXT:
    for (i = K+1;;)
    {
      if (--i == 0) { K++; goto nextK; }
      if (++ind[i] <= lfamod - K + i)
      {
        curdeg = degsofar[i-1] + deg[ind[i]];
        if (curdeg <= klim) break;
      }
    }
  }
END:
  *done = 1;
  if (degpol(D.C2ltpol) > 0)
  { /* leftover factor */
    GEN q = D.C2ltpol;
    if (D.C2lt) q = RgX_int_normalize(q);
    if (lfamod >= 2*K)
    { /* restore leading coefficient [#930] */
      if (D.lt) q = RgX_Rg_mul(q, D.lt);
      *done = 0; /* ... may still be reducible */
    }
    setlg(famod, lfamod+1);
    gel(fa,cnt++) = q;
  }
  if (DEBUGLEVEL>6) err_printf("\n");
  setlg(fa, cnt);
  return gerepilecopy(av0, fa);
}

static GEN
nf_chk_factors(nfcmbf_t *T, GEN P, GEN M_L, GEN famod, GEN pk)
{
  GEN nf = T->nf, bound = T->bound;
  GEN nfT = nf_get_pol(nf);
  long i, r;
  GEN pol = P, list, piv, y;
  GEN Tpk = T->L->Tpk;
  div_data D;

  piv = ZM_hnf_knapsack(M_L);
  if (!piv) return NULL;
  if (DEBUGLEVEL>3) err_printf("ZM_hnf_knapsack output:\n%Ps\n",piv);

  r  = lg(piv)-1;
  list = cgetg(r+1, t_VEC);
  init_div_data(&D, pol, T->L);
  for (i = 1;;)
  {
    pari_sp av = avma;
    if (DEBUGLEVEL) err_printf("nf_LLL_cmbf: checking factor %ld\n", i);
    y = chk_factors_get(D.lt, famod, gel(piv,i), Tpk, pk);

    if (! (y = nf_pol_lift(y, bound, T->L)) ) return NULL;
    y = gerepilecopy(av, y);
    /* y is the candidate factor */
    pol = RgXQX_divrem(D.C2ltpol, y, nfT, ONLY_DIVIDES);
    if (!pol) return NULL;

    if (D.C2lt) y = RgX_int_normalize(y);
    gel(list,i) = y;
    if (++i >= r) break;

    update_target(&D, pol);
  }
  gel(list,i) = RgX_int_normalize(pol); return list;
}

static GEN
nf_to_Zq(GEN x, GEN T, GEN pk, GEN pks2, GEN proj)
{
  GEN y;
  if (typ(x) != t_COL) return centermodii(x, pk, pks2);
  if (!T)
  {
    y = ZV_dotproduct(proj, x);
    return centermodii(y, pk, pks2);
  }
  y = ZM_ZC_mul(proj, x);
  y = RgV_to_RgX(y, varn(T));
  return FpX_center_i(FpX_rem(y, T, pk), pk, pks2);
}

/* Assume P in nfX form, lc(P) != 0 mod p. Reduce P to Zp[X]/(T) mod p^a */
static GEN
ZqX(GEN P, GEN pk, GEN T, GEN proj)
{
  long i, l = lg(P);
  GEN z, pks2 = shifti(pk,-1);

  z = cgetg(l,t_POL); z[1] = P[1];
  for (i=2; i<l; i++) gel(z,i) = nf_to_Zq(gel(P,i),T,pk,pks2,proj);
  return normalizepol_lg(z, l);
}

static GEN
ZqX_normalize(GEN P, GEN lt, nflift_t *L)
{
  GEN R = lt? RgX_Rg_mul(P, Fp_inv(lt, L->pk)): P;
  return ZqX(R, L->pk, L->Tpk, L->ZqProj);
}

/* k allowing to reconstruct x, |x|^2 < C, from x mod pr^k */
/* return log [  2sqrt(C/d) * ( (3/2)sqrt(gamma) )^(d-1) ] ^d / log N(pr)
 * cf. Belabas relative van Hoeij algorithm, lemma 3.12 */
static double
bestlift_bound(GEN C, long d, double alpha, GEN p, long f)
{
  const double g = 1 / (alpha - 0.25); /* = 2 if alpha = 3/4 */
  GEN C4 = shiftr(gtofp(C,DEFAULTPREC), 2);
  double t, logp = log(gtodouble(p));
  if (f == d)
  { /* p inert, no LLL fudge factor: p^(2k) / 4 > C */
    t = 0.5 * rtodbl(mplog(C4));
    return ceil(t / logp);
  }
  /* (1/2)log (4C/d) + (d-1)(log 3/2 sqrt(gamma)) */
  t = 0.5 * rtodbl(mplog(divru(C4,d))) + (d-1) * log(1.5 * sqrt(g));
  return ceil((t * d) / (logp * f));
}

static GEN
get_R(GEN M)
{
  GEN R;
  long i, l, prec = nbits2prec( gexpo(M) + 64 );

  for(;;)
  {
    R = gaussred_from_QR(M, prec);
    if (R) break;
    prec = precdbl(prec);
  }
  l = lg(R);
  for (i=1; i<l; i++) gcoeff(R,i,i) = gen_1;
  return R;
}

static void
init_proj(nflift_t *L, GEN prkHNF, GEN nfT)
{
  if (degpol(L->Tp)>1)
  {
    GEN coTp = FpX_div(FpX_red(nfT, L->p), L->Tp,  L->p); /* Tp's cofactor */
    GEN z, proj;
    z = ZpX_liftfact(nfT, mkvec2(L->Tp, coTp), L->pk, L->p, L->k);
    L->Tpk = gel(z,1);
    proj = QXQV_to_FpM(L->topow, L->Tpk, L->pk);
    if (L->topowden)
      proj = FpM_red(ZM_Z_mul(proj, Fp_inv(L->topowden, L->pk)), L->pk);
    L->ZqProj = proj;
  }
  else
  {
    L->Tpk = NULL;
    L->ZqProj = dim1proj(prkHNF);
  }
}

/* Square of the radius of largest ball inscript in PRK's fundamental domain,
 *   whose orthogonalized vector's norms are the Bi
 * Rmax ^2 =  min 1/4T_i where T_i = sum_j ( s_ij^2 / B_j)
 * For p inert, S = Id, T_i = 1 / p^{2k} and Rmax = p^k / 2 */
static GEN
max_radius(GEN PRK, GEN B)
{
  GEN S, smax = gen_0;
  pari_sp av = avma;
  long i, j, d = lg(PRK)-1;

  S = RgM_inv( get_R(PRK) ); if (!S) pari_err_PREC("max_radius");
  for (i=1; i<=d; i++)
  {
    GEN s = gen_0;
    for (j=1; j<=d; j++)
      s = mpadd(s, mpdiv( mpsqr(gcoeff(S,i,j)), gel(B,j)));
    if (mpcmp(s, smax) > 0) smax = s;
  }
  return gerepileupto(av, ginv(gmul2n(smax, 2)));
}

static void
bestlift_init(long a, GEN nf, GEN C, nflift_t *L)
{
  const double alpha = 0.99; /* LLL parameter */
  const long d = nf_get_degree(nf);
  pari_sp av = avma, av2;
  GEN prk, PRK, iPRK, GSmin, T = L->Tp, p = L->p;
  long f = degpol(T);
  pari_timer ti;

  if (f == d)
  { /* inert p, much simpler */
    long a0 = bestlift_bound(C, d, alpha, p, f);
    GEN q;
    if (a < a0) a = a0; /* guarantees GSmin >= C */
    if (DEBUGLEVEL>2) err_printf("exponent %ld\n",a);
    q = powiu(p,a);
    PRK = prk = scalarmat_shallow(q, d);
    GSmin = shiftr(itor(q, DEFAULTPREC), -1);
    iPRK = matid(d); goto END;
  }
  timer_start(&ti);
  if (!a) a = (long)bestlift_bound(C, d, alpha, p, f);
  for (;; avma = av, a += (a==1)? 1: (a>>1)) /* roughly a *= 1.5 */
  {
    GEN B, q = powiu(p,a), Tq = FpXQ_powu(T, a, FpX_red(nf_get_pol(nf), q), q);
    if (DEBUGLEVEL>2) err_printf("exponent %ld\n",a);
    prk = idealhnf_two(nf, mkvec2(q, Tq));
    av2 = avma;
    PRK = ZM_lll_norms(prk, alpha, LLL_INPLACE, &B);
    GSmin = max_radius(PRK, B);
    if (gcmp(GSmin, C) >= 0) break;
  }
  gerepileall(av2, 2, &PRK, &GSmin);
  iPRK = ZM_inv(PRK, NULL);
  if (DEBUGLEVEL>2)
    err_printf("for this exponent, GSmin = %Ps\nTime reduction: %ld\n",
               GSmin, timer_delay(&ti));
END:
  L->k = a;
  L->pk = gcoeff(prk,1,1);
  L->prk = PRK;
  L->iprk = iPRK;
  L->GSmin= GSmin;
  init_proj(L, prk, nf_get_pol(nf));
}

/* Let X = Tra * M_L, Y = bestlift(X) return V s.t Y = X - PRK V
 * and set *eT2 = gexpo(Y)  [cf nf_bestlift, but memory efficient] */
static GEN
get_V(GEN Tra, GEN M_L, GEN PRK, GEN PRKinv, GEN pk, long *eT2)
{
  long i, e = 0, l = lg(M_L);
  GEN V = cgetg(l, t_MAT);
  *eT2 = 0;
  for (i = 1; i < l; i++)
  { /* cf nf_bestlift(Tra * c) */
    pari_sp av = avma, av2;
    GEN v, T2 = ZM_ZC_mul(Tra, gel(M_L,i));

    v = gdivround(ZM_ZC_mul(PRKinv, T2), pk); /* small */
    av2 = avma;
    T2 = ZC_sub(T2, ZM_ZC_mul(PRK, v));
    e = gexpo(T2); if (e > *eT2) *eT2 = e;
    avma = av2;
    gel(V,i) = gerepileupto(av, v); /* small */
  }
  return V;
}

static GEN
nf_LLL_cmbf(nfcmbf_t *T, long rec)
{
  const double BitPerFactor = 0.4; /* nb bits / modular factor */
  nflift_t *L = T->L;
  GEN famod = T->fact, ZC = T->ZC, Br = T->Br, P = T->pol, dn = T->L->dn;
  long dnf = nf_get_degree(T->nf), dP = degpol(P);
  long i, C, tmax, n0;
  GEN lP, Bnorm, Tra, T2, TT, CM_L, m, list, ZERO, Btra;
  double Bhigh;
  pari_sp av, av2;
  long ti_LLL = 0, ti_CF = 0;
  pari_timer ti2, TI;

  lP = absi_shallow(leading_coeff(P));
  if (is_pm1(lP)) lP = NULL;

  n0 = lg(famod) - 1;
 /* Lattice: (S PRK), small vector (vS vP). To find k bound for the image,
  * write S = S1 q + S0, P = P1 q + P0
  * |S1 vS + P1 vP|^2 <= Bhigh for all (vS,vP) assoc. to true factors */
  Btra = mulrr(ZC, mulur(dP*dP, normTp(Br, 2, dnf)));
  Bhigh = get_Bhigh(n0, dnf);
  C = (long)ceil(sqrt(Bhigh/n0)) + 1; /* C^2 n0 ~ Bhigh */
  Bnorm = dbltor( n0 * C * C + Bhigh );
  ZERO = zeromat(n0, dnf);

  av = avma;
  TT = const_vec(n0, NULL);
  Tra  = cgetg(n0+1, t_MAT);
  CM_L = scalarmat_s(C, n0);
  /* tmax = current number of traces used (and computed so far) */
  for(tmax = 0;; tmax++)
  {
    long a, b, bmin, bgood, delta, tnew = tmax + 1, r = lg(CM_L)-1;
    GEN M_L, q, CM_Lp, oldCM_L, S1, P1, VV;
    int first = 1;

    /* bound for f . S_k(genuine factor) = ZC * bound for T_2(S_tnew) */
    Btra = mulrr(ZC, mulur(dP*dP, normTp(Br, 2*tnew, dnf)));
    bmin = logint(ceil_safe(sqrtr(Btra)), gen_2) + 1;
    if (DEBUGLEVEL>2)
      err_printf("\nLLL_cmbf: %ld potential factors (tmax = %ld, bmin = %ld)\n",
                 r, tmax, bmin);

    /* compute Newton sums (possibly relifting first) */
    if (gcmp(L->GSmin, Btra) < 0)
    {
      GEN polred;

      bestlift_init((L->k)<<1, T->nf, Btra, L);
      polred = ZqX_normalize(T->polbase, lP, L);
      famod = ZqX_liftfact(polred, famod, L->Tpk, L->pk, L->p, L->k);
      for (i=1; i<=n0; i++) gel(TT,i) = NULL;
    }
    for (i=1; i<=n0; i++)
    {
      GEN h, lPpow = lP? powiu(lP, tnew): NULL;
      GEN z = polsym_gen(gel(famod,i), gel(TT,i), tnew, L->Tpk, L->pk);
      gel(TT,i) = z;
      h = gel(z,tnew+1);
      /* make Newton sums integral */
      lPpow = mul_content(lPpow, dn);
      if (lPpow)
        h = (typ(h) == t_INT)? Fp_mul(h, lPpow, L->pk): FpX_Fp_mul(h, lPpow, L->pk);
      gel(Tra,i) = nf_bestlift(h, NULL, L); /* S_tnew(famod) */
    }

    /* compute truncation parameter */
    if (DEBUGLEVEL>2) { timer_start(&ti2); timer_start(&TI); }
    oldCM_L = CM_L;
    av2 = avma;
    b = delta = 0; /* -Wall */
AGAIN:
    M_L = Q_div_to_int(CM_L, utoipos(C));
    VV = get_V(Tra, M_L, L->prk, L->iprk, L->pk, &a);
    if (first)
    { /* initialize lattice, using few p-adic digits for traces */
      bgood = (long)(a - maxss(32, (long)(BitPerFactor * r)));
      b = maxss(bmin, bgood);
      delta = a - b;
    }
    else
    { /* add more p-adic digits and continue reduction */
      if (a < b) b = a;
      b = maxss(b-delta, bmin);
      if (b - delta/2 < bmin) b = bmin; /* near there. Go all the way */
    }

    /* restart with truncated entries */
    q = int2n(b);
    P1 = gdivround(L->prk, q);
    S1 = gdivround(Tra, q);
    T2 = ZM_sub(ZM_mul(S1, M_L), ZM_mul(P1, VV));
    m = vconcat( CM_L, T2 );
    if (first)
    {
      first = 0;
      m = shallowconcat( m, vconcat(ZERO, P1) );
      /*     [ C M_L   0  ]
       * m = [            ]   square matrix
       *     [  T2'   PRK ]   T2' = Tra * M_L  truncated
       */
    }
    CM_L = LLL_check_progress(Bnorm, n0, m, b == bmin, /*dbg:*/ &ti_LLL);
    if (DEBUGLEVEL>2)
      err_printf("LLL_cmbf: (a,b) =%4ld,%4ld; r =%3ld -->%3ld, time = %ld\n",
                 a,b, lg(m)-1, CM_L? lg(CM_L)-1: 1, timer_delay(&TI));
    if (!CM_L) { list = mkcol(RgX_int_normalize(P)); break; }
    if (b > bmin)
    {
      CM_L = gerepilecopy(av2, CM_L);
      goto AGAIN;
    }
    if (DEBUGLEVEL>2) timer_printf(&ti2, "for this trace");

    i = lg(CM_L) - 1;
    if (i == r && ZM_equal(CM_L, oldCM_L))
    {
      CM_L = oldCM_L;
      avma = av2; continue;
    }

    CM_Lp = FpM_image(CM_L, utoipos(27449)); /* inexpensive test */
    if (lg(CM_Lp) != lg(CM_L))
    {
      if (DEBUGLEVEL>2) err_printf("LLL_cmbf: rank decrease\n");
      CM_L = ZM_hnf(CM_L);
    }

    if (i <= r && i*rec < n0)
    {
      pari_timer ti;
      if (DEBUGLEVEL>2) timer_start(&ti);
      list = nf_chk_factors(T, P, Q_div_to_int(CM_L,utoipos(C)), famod, L->pk);
      if (DEBUGLEVEL>2) ti_CF += timer_delay(&ti);
      if (list) break;
    }
    if (gc_needed(av,1))
    {
      if(DEBUGMEM>1) pari_warn(warnmem,"nf_LLL_cmbf");
      gerepileall(av, L->Tpk? 9: 8,
                      &CM_L,&TT,&Tra,&famod,&L->GSmin,&L->pk,&L->prk,&L->iprk,
                      &L->Tpk);
    }
    else CM_L = gerepilecopy(av2, CM_L);
  }
  if (DEBUGLEVEL>2)
    err_printf("* Time LLL: %ld\n* Time Check Factor: %ld\n",ti_LLL,ti_CF);
  return list;
}

static GEN
nf_combine_factors(nfcmbf_t *T, GEN polred, long klim)
{
  nflift_t *L = T->L;
  GEN res;
  long maxK;
  int done;
  pari_timer ti;

  if (DEBUGLEVEL>2) timer_start(&ti);
  T->fact = ZqX_liftfact(polred, T->fact, L->Tpk, L->pk, L->p, L->k);
  if (DEBUGLEVEL>2) timer_printf(&ti, "Hensel lift");
  res = nfcmbf(T, klim, &maxK, &done);
  if (DEBUGLEVEL>2) timer_printf(&ti, "Naive recombination");
  if (!done)
  {
    long l = lg(res)-1;
    GEN v;
    if (l > 1)
    {
      GEN den;
      T->pol = gel(res,l);
      T->polbase = Q_remove_denom(RgX_to_nfX(T->nf, T->pol), &den);
      if (den) { T->Br = gmul(T->Br, den); T->pol = RgX_Rg_mul(T->pol, den); }
    }
    v = nf_LLL_cmbf(T, maxK);
    /* remove last elt, possibly unfactored. Add all new ones. */
    setlg(res, l); res = shallowconcat(res, v);
  }
  return res;
}

static GEN
nf_DDF_roots(GEN pol, GEN polred, GEN nfpol, long fl, nflift_t *L)
{
  GEN z, Cltx_r, ltdn;
  long i, m, lz;
  div_data D;

  init_div_data(&D, pol, L);
  ltdn = mul_content(D.lt, L->dn);
  z = ZqX_roots(polred, L->Tpk, L->p, L->k);
  Cltx_r = deg1pol_shallow(D.Clt? D.Clt: gen_1, NULL, varn(pol));
  lz = lg(z);
  if (DEBUGLEVEL > 3) err_printf("Checking %ld roots:",lz-1);
  for (m=1,i=1; i<lz; i++)
  {
    GEN r = gel(z,i);
    int dvd;
    pari_sp av;
    if (DEBUGLEVEL > 3) err_printf(" %ld",i);
    /* lt*dn*topowden * r = Clt * r */
    r = nf_bestlift_to_pol(ltdn? gmul(ltdn,r): r, NULL, L);
    av = avma;
    gel(Cltx_r,2) = gneg(r); /* check P(r) == 0 */
    dvd = ZXQX_dvd(D.C2ltpol, Cltx_r, nfpol); /* integral */
    avma = av;
    /* don't go on with q, usually much larger that C2ltpol */
    if (dvd) {
      if (D.Clt) r = gdiv(r, D.Clt);
      gel(z,m++) = r;
    }
    else if (fl == ROOTS_SPLIT) return cgetg(1, t_VEC);
  }
  if (DEBUGLEVEL > 3) err_printf(" done\n");
  z[0] = evaltyp(t_VEC) | evallg(m);
  return z;
}

/* returns a few factors of T in Fp of degree <= maxf, NULL if none exist */
static GEN
get_good_factor(GEN T, ulong p, long maxf)
{
  pari_sp av = avma;
  GEN r, R = gel(Flx_factor(ZX_to_Flx(T,p),p), 1);
  if (maxf == 1)
  { /* degree 1 factors are best */
    r = gel(R,1);
    if (degpol(r) == 1) return mkvec(r);
  }
  else
  { /* otherwise, pick factor of largish degree */
    long i, j, dr, d = 0, l = lg(R);
    GEN v;
    if (l == 2) return mkvec(gel(R,1)); /* inert is fine */
    v = cgetg(l, t_VEC);
    for (i = j = 1; i < l; i++)
    {
      r = gel(R,i); dr = degpol(r);
      if (dr > maxf) break;
      if (dr != d) { gel(v,j++) = r; d = dr; }
    }
    setlg(v,j); if (j > 1) return v;
  }
  avma = av; return NULL; /* failure */
}

/* n = number of modular factors, f = residue degree; nold/fold current best
 * return 1 if new values are "better" than old ones */
static int
record(long nold, long n, long fold, long f)
{
  if (!nold) return 1; /* uninitialized */
  if (fold == f) return n < nold;
  /* if f increases, allow increasing n a little */
  if (fold < f) return n <= 20 || n < 1.1*nold;
  /* f decreases, only allow if decreasing n a lot */
  return n < 0.7*nold;
}
/* Optimization problem: factorization of polynomials over large Fq is slow,
 * BUT bestlift correspondingly faster.
 * Return maximal residue degree to be considered when picking a prime ideal */
static long
get_maxf(long nfdeg)
{
  long maxf = 1;
  if      (nfdeg >= 45) maxf =32;
  else if (nfdeg >= 30) maxf =16;
  else if (nfdeg >= 15) maxf = 8;
  return maxf;
}
/* number of maximal ideals to test before settling on best prime and number
 * of factors; B = [K:Q]*deg(P) */
static long
get_nbprimes(long B)
{
  if (B <= 128) return 5;
  if (B <= 1024) return 20;
  if (B <= 2048) return 65;
  return 100;
}
/* Select a prime ideal pr over which to factor pol.
 * Return the number of factors (or roots, according to flag fl) mod pr.
 * Set:
 *   lt: leading term of polbase (t_INT or NULL [ for 1 ])
 *   pr: a suitable maximal ideal
 *   Fa: factors found mod pr
 *   Tp: polynomial defining Fq/Fp */
static long
nf_pick_prime(GEN nf, GEN pol, long fl, GEN *lt, GEN *Tp, ulong *pp)
{
  GEN nfpol = nf_get_pol(nf), bad = mulii(nf_get_disc(nf), nf_get_index(nf));
  long nfdeg = degpol(nfpol), dpol = degpol(pol), nold = 0, fold = 1;
  long maxf = get_maxf(nfdeg), ct = get_nbprimes(nfdeg * dpol);
  ulong p;
  forprime_t S;
  pari_timer ti_pr;

  if (DEBUGLEVEL>3) timer_start(&ti_pr);
  *lt  = leading_coeff(pol); /* t_INT */
  if (gequal1(*lt)) *lt = NULL;
  *pp = 0;
  *Tp = NULL;
  (void)u_forprime_init(&S, 2, ULONG_MAX);
  /* select pr such that pol has the smallest number of factors, ct attempts */
  while ((p = u_forprime_next(&S)))
  {
    GEN vT;
    long n, i, l, ok = 0;
    ulong ltp = 0;

    if (! umodiu(bad,p)) continue;
    if (*lt) { ltp = umodiu(*lt, p); if (!ltp) continue; }
    vT = get_good_factor(nfpol, p, maxf);
    if (!vT) continue;
    l = lg(vT);
    for (i = 1; i < l; i++)
    {
      pari_sp av2 = avma;
      GEN T = gel(vT,i), red = RgX_to_FlxqX(pol, T, p);
      long f = degpol(T);
      if (f == 1)
      { /* degree 1 */
        red = FlxX_to_Flx(red);
        if (ltp) red = Flx_normalize(red, p);
        if (!Flx_is_squarefree(red, p)) { avma = av2; continue; }
        ok = 1;
        n = (fl == FACTORS)? Flx_nbfact(red,p): Flx_nbroots(red,p);
      }
      else
      {
        if (ltp) red = FlxqX_normalize(red, T, p);
        if (!FlxqX_is_squarefree(red, T, p)) { avma = av2; continue; }
        ok = 1;
        n = (fl == FACTORS)? FlxqX_nbfact(red,T,p): FlxqX_nbroots(red,T,p);
      }
      if (fl == ROOTS_SPLIT && n < dpol) return n; /* not split */
      if (n <= 1)
      {
        if (fl == FACTORS) return n; /* irreducible */
        if (!n) return 0; /* no root */
      }
      if (DEBUGLEVEL>3)
        err_printf("%3ld %s at prime (%ld,x^%ld+...)\n Time: %ld\n",
            n, (fl == FACTORS)? "factors": "roots", p,f, timer_delay(&ti_pr));

      if (fl == ROOTS && f==nfdeg) { *Tp = T; *pp = p; return n; }
      if (record(nold, n, fold, f)) { nold = n; fold = f; *Tp = T; *pp = p; }
      else avma = av2;
    }
    if (ok && --ct <= 0) break;
  }
  if (!nold) pari_err_OVERFLOW("nf_pick_prime [ran out of primes]");
  return nold;
}

/* Assume lt(T) is a t_INT and T square free. Return t_VEC of irred. factors */
static GEN
nfsqff_trager(GEN u, GEN T, GEN dent)
{
  long k = 0, i, lx;
  GEN U, P, x0, mx0, fa, n = ZX_ZXY_rnfequation(T, u, &k);
  int tmonic;
  if (DEBUGLEVEL>4) err_printf("nfsqff_trager: choosing k = %ld\n",k);

  /* n guaranteed to be squarefree */
  fa = ZX_DDF(Q_primpart(n)); lx = lg(fa);
  if (lx == 2) return mkvec(u);

  tmonic = is_pm1(leading_coeff(T));
  P = cgetg(lx,t_VEC);
  x0 = deg1pol_shallow(stoi(-k), gen_0, varn(T));
  mx0 = deg1pol_shallow(stoi(k), gen_0, varn(T));
  U = RgXQX_translate(u, mx0, T);
  if (!tmonic) U = Q_primpart(U);
  for (i=lx-1; i>0; i--)
  {
    GEN f = gel(fa,i), F = nfgcd(U, f, T, dent);
    F = RgXQX_translate(F, x0, T);
    /* F = gcd(f, u(t - x0)) [t + x0] = gcd(f(t + x0), u), more efficient */
    if (typ(F) != t_POL || degpol(F) == 0)
      pari_err_IRREDPOL("factornf [modulus]",T);
    gel(P,i) = QXQX_normalize(F, T);
  }
  gen_sort_inplace(P, (void*)&cmp_RgX, &gen_cmp_RgX, NULL);
  return P;
}

/* Factor polynomial a on the number field defined by polynomial T, using
 * Trager's trick */
GEN
polfnf(GEN a, GEN T)
{
  GEN rep = cgetg(3, t_MAT), A, B, y, dent, bad;
  long dA;
  int tmonic;

  if (typ(a)!=t_POL) pari_err_TYPE("polfnf",a);
  if (typ(T)!=t_POL) pari_err_TYPE("polfnf",T);
  T = Q_primpart(T); tmonic = is_pm1(leading_coeff(T));
  RgX_check_ZX(T,"polfnf");
  A = Q_primpart( QXQX_normalize(RgX_nffix("polfnf",T,a,1), T) );
  dA = degpol(A);
  if (dA <= 0)
  {
    avma = (pari_sp)(rep + 3);
    return (dA == 0)? trivial_fact(): zerofact(varn(A));
  }
  bad = dent = ZX_disc(T);
  if (tmonic) dent = indexpartial(T, dent);
  (void)nfgcd_all(A,RgX_deriv(A), T, dent, &B);
  if (degpol(B) != dA) B = Q_primpart( QXQX_normalize(B, T) );
  ensure_lt_INT(B);
  y = nfsqff_trager(B, T, dent);
  fact_from_sqff(rep, A, B, y, T, bad);
  return sort_factor_pol(rep, cmp_RgX);
}

static int
nfsqff_use_Trager(long n, long dpol)
{
  return dpol*3<n;
}

/* return the factorization of the square-free polynomial pol. Not memory-clean
   The coeffs of pol are in Z_nf and its leading term is a rational integer.
   deg(pol) > 0, deg(nfpol) > 1
   fl is either FACTORS (return factors), or ROOTS / ROOTS_SPLIT (return roots):
     - ROOTS, return only the roots of x in nf
     - ROOTS_SPLIT, as ROOTS if pol splits, [] otherwise
   den is usually 1, otherwise nf.zk is doubtful, and den bounds the
   denominator of an arbitrary element of Z_nf on nf.zk */
static GEN
nfsqff(GEN nf, GEN pol, long fl, GEN den)
{
  long n, nbf, dpol = degpol(pol);
  GEN C0, polbase;
  GEN N2, res, polred, lt, nfpol = typ(nf)==t_POL?nf:nf_get_pol(nf);
  ulong pp;
  nfcmbf_t T;
  nflift_t L;
  pari_timer ti, ti_tot;

  if (DEBUGLEVEL>2) { timer_start(&ti); timer_start(&ti_tot); }
  n = degpol(nfpol);
  /* deg = 1 => irreducible */
  if (dpol == 1) {
    if (fl == FACTORS) return mkvec(QXQX_normalize(pol, nfpol));
    return mkvec(gneg(gdiv(gel(pol,2),gel(pol,3))));
  }
  if (typ(nf)==t_POL || nfsqff_use_Trager(n,dpol))
  {
    GEN z;
    if (DEBUGLEVEL>2) err_printf("Using Trager's method\n");
    if (typ(nf) != t_POL) den =  mulii(den, nf_get_index(nf));
    z = nfsqff_trager(Q_primpart(pol), nfpol, den);
    if (fl != FACTORS) {
      long i, l = lg(z);
      for (i = 1; i < l; i++)
      {
        GEN LT, t = gel(z,i); if (degpol(t) > 1) break;
        LT = gel(t,3);
        if (typ(LT) == t_POL) LT = gel(LT,2); /* constant */
        gel(z,i) = gdiv(gel(t,2), negi(LT));
      }
      setlg(z, i);
      if (fl == ROOTS_SPLIT && i != l) return cgetg(1,t_VEC);
    }
    return z;
  }

  polbase = RgX_to_nfX(nf, pol);
  nbf = nf_pick_prime(nf, pol, fl, &lt, &L.Tp, &pp);
  if (L.Tp)
  {
    L.Tp = Flx_to_ZX(L.Tp);
    L.p = utoi(pp);
  }

  if (fl == ROOTS_SPLIT && nbf < dpol) return cgetg(1,t_VEC);
  if (nbf <= 1)
  {
    if (fl == FACTORS) return mkvec(QXQX_normalize(pol, nfpol)); /* irred. */
    if (!nbf) return cgetg(1,t_VEC); /* no root */
  }

  if (DEBUGLEVEL>2) {
    timer_printf(&ti, "choice of a prime ideal");
    err_printf("Prime ideal chosen: (%lu,x^%ld+...)\n", pp, degpol(L.Tp));
  }
  L.tozk = nf_get_invzk(nf);
  L.topow= nf_get_zkprimpart(nf);
  L.topowden = nf_get_zkden(nf);
  if (is_pm1(den)) den = NULL;
  L.dn = den;
  T.ZC = L2_bound(nf, den);
  T.Br = nf_root_bounds(nf, pol); if (lt) T.Br = gmul(T.Br, lt);

  /* C0 = bound for T_2(Q_i), Q | P */
  if (fl != FACTORS) C0 = normTp(T.Br, 2, n);
  else               C0 = nf_factor_bound(nf, polbase);
  T.bound = mulrr(T.ZC, C0); /* bound for |Q_i|^2 in Z^n on chosen Z-basis */

  N2 = mulur(dpol*dpol, normTp(T.Br, 4, n)); /* bound for T_2(lt * S_2) */
  T.BS_2 = mulrr(T.ZC, N2); /* bound for |S_2|^2 on chosen Z-basis */

  if (DEBUGLEVEL>2) {
    timer_printf(&ti, "bound computation");
    err_printf("  1) T_2 bound for %s: %Ps\n",
               fl == FACTORS?"factor": "root", C0);
    err_printf("  2) Conversion from T_2 --> | |^2 bound : %Ps\n", T.ZC);
    err_printf("  3) Final bound: %Ps\n", T.bound);
  }

  bestlift_init(0, nf, T.bound, &L);
  if (DEBUGLEVEL>2) timer_start(&ti);
  polred = ZqX_normalize(polbase, lt, &L); /* monic */

  if (fl != FACTORS) {
    GEN z = nf_DDF_roots(pol, polred, nfpol, fl, &L);
    if (lg(z) == 1) return cgetg(1, t_VEC);
    return z;
  }

  T.fact = gel(FqX_factor(polred, L.Tp, L.p), 1);
  if (DEBUGLEVEL>2)
    timer_printf(&ti, "splitting mod %Ps^%ld", L.p, degpol(L.Tp));
  T.L  = &L;
  T.polbase = polbase;
  T.pol   = pol;
  T.nf    = nf;
  res = nf_combine_factors(&T, polred, dpol-1);
  if (DEBUGLEVEL>2)
    err_printf("Total Time: %ld\n===========\n", timer_delay(&ti_tot));
  return res;
}

/* assume pol monic in nf.zk[X] */
GEN
nfroots_if_split(GEN *pnf, GEN pol)
{
  GEN T = get_nfpol(*pnf,pnf), den = fix_nf(pnf, &T, &pol);
  pari_sp av = avma;
  GEN z = nfsqff(*pnf, pol, ROOTS_SPLIT, den);
  if (lg(z) == 1) { avma = av; return NULL; }
  return gerepilecopy(av, z);
}

/*******************************************************************/
/*                                                                 */
/*              Roots of unity in a number field                   */
/*     (alternative to nfrootsof1 using factorization in K[X])     */
/*                                                                 */
/*******************************************************************/
/* Code adapted from nffactor. Structure of the algorithm; only step 1 is
 * specific to roots of unity.
 *
 * [Step 1]: guess roots via ramification. If trivial output this.
 * [Step 2]: select prime [p] unramified and ideal [pr] above
 * [Step 3]: evaluate the maximal exponent [k] such that the fondamental domain
 *           of a LLL-reduction of [prk] = pr^k contains a ball of radius larger
 *           than the norm of any root of unity.
 * [Step 3]: select a heuristic exponent,
 *           LLL reduce prk=pr^k and verify the exponent is sufficient,
 *           otherwise try a larger one.
 * [Step 4]: factor the cyclotomic polynomial mod [pr],
 *           Hensel lift to pr^k and find the representative in the ball
 *           If there is it is a primitive root */

/* Choose prime ideal unramified with "large" inertia degree */
static void
nf_pick_prime_for_units(GEN nf, nflift_t *L)
{
  GEN nfpol = nf_get_pol(nf), bad = mulii(nf_get_disc(nf), nf_get_index(nf));
  GEN ap = NULL, r = NULL;
  long nfdeg = degpol(nfpol), maxf = get_maxf(nfdeg);
  ulong pp;
  forprime_t S;

  (void)u_forprime_init(&S, 2, ULONG_MAX);
  while ( (pp = u_forprime_next(&S)) )
  {
    if (! umodiu(bad,pp)) continue;
    r = get_good_factor(nfpol, pp, maxf);
    if (r) break;
  }
  if (!r) pari_err_OVERFLOW("nf_pick_prime [ran out of primes]");
  r = gel(r,lg(r)-1); /* largest inertia degree */
  ap = utoipos(pp);
  L->p = ap;
  L->Tp = Flx_to_ZX(r);
  L->tozk = nf_get_invzk(nf);
  L->topow = nf_get_zkprimpart(nf);
  L->topowden = nf_get_zkden(nf);
}

/* *Heuristic* exponent k such that the fundamental domain of pr^k
 * should contain the ball of radius C */
static double
mybestlift_bound(GEN C)
{
  C = gtofp(C,DEFAULTPREC);
  return ceil(log(gtodouble(C)) / 0.2) + 3;
}

/* simplified nf_DDF_roots: polcyclo(n) monic in ZX either splits or has no
 * root in nf.
 * Return a root or NULL (no root) */
static GEN
nfcyclo_root(long n, GEN nfpol, nflift_t *L)
{
  GEN q, r, Cltx_r, pol = polcyclo(n,0), gn = utoipos(n);
  div_data D;

  init_div_data(&D, pol, L);
  (void)Fq_sqrtn(gen_1, gn, L->Tp, L->p, &r);
  /* r primitive n-th root of 1 in Fq */
  r = Zq_sqrtnlift(gen_1, gn, r, L->Tpk, L->p, L->k);
  /* lt*dn*topowden * r = Clt * r */
  r = nf_bestlift_to_pol(r, NULL, L);
  Cltx_r = deg1pol_shallow(D.Clt? D.Clt: gen_1, gneg(r), varn(pol));
  /* check P(r) == 0 */
  q = RgXQX_divrem(D.C2ltpol, Cltx_r, nfpol, ONLY_DIVIDES); /* integral */
  if (!q) return NULL;
  if (D.Clt) r = gdiv(r, D.Clt);
  return r;
}

/* Guesses the number of roots of unity in number field [nf].
 * Computes gcd of N(P)-1 for some primes. The value returned is a proven
 * multiple of the correct value. */
static long
guess_roots(GEN nf)
{
  long c = 0, nfdegree = nf_get_degree(nf), B = nfdegree + 20, l;
  ulong p = 2;
  GEN T = nf_get_pol(nf), D = nf_get_disc(nf), index = nf_get_index(nf);
  GEN nbroots = NULL;
  forprime_t S;
  pari_sp av;

  (void)u_forprime_init(&S, 3, ULONG_MAX);
  av = avma;
  /* result must be stationary (counter c) for at least B loops */
  for (l=1; (p = u_forprime_next(&S)); l++)
  {
    GEN old, F, pf_1, Tp;
    long i, nb, gcdf = 0;

    if (!umodiu(D,p) || !umodiu(index,p)) continue;
    Tp = ZX_to_Flx(T,p); /* squarefree */
    F = Flx_nbfact_by_degree(Tp, &nb, p);
    /* the gcd of the p^f - 1 is p^(gcd of the f's) - 1 */
    for (i = 1; i <= nfdegree; i++)
      if (F[i]) {
        gcdf = gcdf? ugcd(gcdf, i): i;
        if (gcdf == 1) break;
      }
    pf_1 = subiu(powuu(p, gcdf), 1);
    old = nbroots;
    nbroots = nbroots? gcdii(pf_1, nbroots): pf_1;
    if (DEBUGLEVEL>5)
      err_printf("p=%lu; gcf(f(P/p))=%ld; nbroots | %Ps",p, gcdf, nbroots);
    /* if same result go on else reset the stop counter [c] */
    if (old && equalii(nbroots,old))
    { if (!is_bigint(nbroots) && ++c > B) break; }
    else
      c = 0;
  }
  if (!nbroots) pari_err_OVERFLOW("guess_roots [ran out of primes]");
  if (DEBUGLEVEL>5) err_printf("%ld loops\n",l);
  avma = av; return itos(nbroots);
}

/* T(x) an irreducible ZX. Is it of the form Phi_n(c \pm x) ?
 * Return NULL if not, and a root of 1 of maximal order in Z[x]/(T) otherwise
 *
 * N.B. Set n_squarefree = 1 if n is squarefree, and 0 otherwise.
 * This last parameter is inconvenient, but it allows a cheap
 * stringent test. (n guessed from guess_roots())*/
static GEN
ZXirred_is_cyclo_translate(GEN T, long n_squarefree)
{
  long r, m, d = degpol(T);
  GEN T1, c = divis_rem(gel(T, d+1), d, &r); /* d-1 th coeff divisible by d ? */
  /* The trace coefficient of polcyclo(n) is \pm1 if n is square free, and 0
   * otherwise. */
  if (!n_squarefree)
  { if (r) return NULL; }
  else
  {
    if (r < -1)
    {
      r += d;
      c = subiu(c, 1);
    }
    else if (r == d-1)
    {
      r = -1;
      c = addiu(c, 1);
    }
    if (r != 1 && r != -1) return NULL;
  }
  if (signe(c)) /* presumably Phi_guess(c \pm x) */
    T = RgX_translate(T, negi(c));
  if (!n_squarefree) T = RgX_deflate_max(T, &m);
  /* presumably Phi_core(guess)(\pm x), cyclotomic iff original T was */
  T1 = ZX_graeffe(T);
  if (ZX_equal(T, T1)) /* T = Phi_n, n odd */
    return deg1pol_shallow(gen_m1, negi(c), varn(T));
  else if (ZX_equal(T1, ZX_z_unscale(T, -1))) /* T = Phi_{2n}, nodd */
    return deg1pol_shallow(gen_1, c, varn(T));
  return NULL;
}

static GEN
trivroots(void) { return mkvec2(gen_2, gen_m1); }
/* Number of roots of unity in number field [nf]. */
GEN
rootsof1(GEN nf)
{
  nflift_t L;
  GEN T, q, fa, LP, LE, C0, z, disc;
  pari_timer ti;
  long i, l, nbguessed, nbroots, nfdegree;
  pari_sp av;

  nf = checknf(nf);
  if (nf_get_r1(nf)) return trivroots();

  /* Step 1 : guess number of roots and discard trivial case 2 */
  if (DEBUGLEVEL>2) timer_start(&ti);
  nbguessed = guess_roots(nf);
  if (DEBUGLEVEL>2)
    timer_printf(&ti, "guessing roots of 1 [guess = %ld]", nbguessed);
  if (nbguessed == 2) return trivroots();

  nfdegree = nf_get_degree(nf);
  fa = factoru(nbguessed);
  LP = gel(fa,1); l = lg(LP);
  LE = gel(fa,2);
  disc = nf_get_disc(nf);
  for (i = 1; i < l; i++)
  {
    long p = LP[i];
    /* Degree and ramification test: find largest k such that Q(zeta_{p^k})
     * may be a subfield of K. Q(zeta_p^k) has degree (p-1)p^(k-1)
     * and v_p(discriminant) = ((p-1)k-1)p^(k-1); so we must have
     * v_p(disc_K) >= ((p-1)k-1) * n / (p-1) = kn - q, where q = n/(p-1) */
    if (p == 2)
    { /* the test simplifies a little in that case */
      long v, vnf, k;
      if (LE[i] == 1) continue;
      vnf = vals(nfdegree);
      v = vali(disc);
      for (k = minss(LE[i], vnf+1); k >= 1; k--)
        if (v >= nfdegree*(k-1)) { nbguessed >>= LE[i]-k; LE[i] = k; break; }
      /* N.B the test above always works for k = 1: LE[i] >= 1 */
    }
    else
    {
      long v, vnf, k;
      ulong r, q = udivuu_rem(nfdegree, p-1, &r);
      if (r) { nbguessed /= upowuu(p, LE[i]); LE[i] = 0; continue; }
      /* q = n/(p-1) */
      vnf = u_lval(q, p);
      v = Z_lval(disc, p);
      for (k = minss(LE[i], vnf+1); k >= 0; k--)
        if (v >= nfdegree*k-(long)q)
        { nbguessed /= upowuu(p, LE[i]-k); LE[i] = k; break; }
      /* N.B the test above always works for k = 0: LE[i] >= 0 */
    }
  }
  if (DEBUGLEVEL>2)
    timer_printf(&ti, "after ramification conditions [guess = %ld]", nbguessed);
  if (nbguessed == 2) return trivroots();
  av = avma;

  /* Step 1.5 : test if nf.pol == subst(polcyclo(nbguessed), x, \pm x+c) */
  T = nf_get_pol(nf);
  if (eulerphiu_fact(fa) == (ulong)nfdegree)
  {
    z = ZXirred_is_cyclo_translate(T, uissquarefree_fact(fa));
    if (DEBUGLEVEL>2) timer_printf(&ti, "checking for cyclotomic polynomial");
    if (z)
    {
      z = nf_to_scalar_or_basis(nf,z);
      return gerepilecopy(av, mkvec2(utoipos(nbguessed), z));
    }
    avma = av;
  }

  /* Step 2 : choose a prime ideal for local lifting */
  nf_pick_prime_for_units(nf, &L);
  if (DEBUGLEVEL>2)
    timer_printf(&ti, "choosing prime %Ps, degree %ld",
             L.p, L.Tp? degpol(L.Tp): 1);

  /* Step 3 : compute a reduced pr^k allowing lifting of local solutions */
  /* evaluate maximum L2 norm of a root of unity in nf */
  C0 = gmulsg(nfdegree, L2_bound(nf, gen_1));
  /* lift and reduce pr^k */
  if (DEBUGLEVEL>2) err_printf("Lift pr^k; GSmin wanted: %Ps\n",C0);
  bestlift_init((long)mybestlift_bound(C0), nf, C0, &L);
  L.dn = NULL;
  if (DEBUGLEVEL>2) timer_start(&ti);

  /* Step 4 : actual computation of roots */
  nbroots = 2; z = gen_m1;
  q = powiu(L.p,degpol(L.Tp));
  for (i = 1; i < l; i++)
  { /* for all prime power factors of nbguessed, find a p^k-th root of unity */
    long k, p = LP[i];
    for (k = minss(LE[i], Z_lval(subiu(q,1UL),p)); k > 0; k--)
    { /* find p^k-th roots */
      pari_sp av = avma;
      long pk = upowuu(p,k);
      GEN r;
      if (pk==2) continue; /* no need to test second roots ! */
      r = nfcyclo_root(pk, T, &L);
      if (DEBUGLEVEL>2) timer_printf(&ti, "for factoring Phi_%ld^%ld", p,k);
      if (r) {
        if (DEBUGLEVEL>2) err_printf("  %s root of unity found\n",uordinal(pk));
        if (p==2) { nbroots = pk; z = r; }
        else     { nbroots *= pk; z = nfmul(nf, z,r); }
        break;
      }
      avma = av;
      if (DEBUGLEVEL) pari_warn(warner,"rootsof1: wrong guess");
    }
  }
  return gerepilecopy(av, mkvec2(utoi(nbroots), z));
}

static long
zk_equal1(GEN y)
{
  if (typ(y) == t_INT) return equali1(y);
  return equali1(gel(y,1)) && ZV_isscalar(y);
}
/* x^w = 1 */
static GEN
is_primitive_root(GEN nf, GEN fa, GEN x, long w)
{
  GEN P = gel(fa,1);
  long i, l = lg(P);

  for (i = 1; i < l; i++)
  {
    long p = itos(gel(P,i));
    GEN y = nfpow_u(nf,x, w/p);
    if (zk_equal1(y) > 0) /* y = 1 */
    {
      if (p != 2 || !equali1(gcoeff(fa,i,2))) return NULL;
      x = gneg_i(x);
    }
  }
  return x;
}
GEN
rootsof1_kannan(GEN nf)
{
  pari_sp av = avma;
  long N, k, i, ws, prec;
  GEN z, y, d, list, w;

  nf = checknf(nf);
  if ( nf_get_r1(nf) ) return trivroots();

  N = nf_get_degree(nf); prec = nf_get_prec(nf);
  for (;;)
  {
    GEN R = R_from_QR(nf_get_G(nf), prec);
    if (R)
    {
      y = fincke_pohst(mkvec(R), utoipos(N), N * N, 0, NULL);
      if (y) break;
    }
    prec = precdbl(prec);
    if (DEBUGLEVEL) pari_warn(warnprec,"rootsof1",prec);
    nf = nfnewprec_shallow(nf,prec);
  }
  if (itos(ground(gel(y,2))) != N) pari_err_BUG("rootsof1 (bug1)");
  w = gel(y,1); ws = itos(w);
  if (ws == 2) { avma = av; return trivroots(); }

  d = Z_factor(w); list = gel(y,3); k = lg(list);
  for (i=1; i<k; i++)
  {
    z = is_primitive_root(nf, d, gel(list,i), ws);
    if (z) return gerepilecopy(av, mkvec2(w, z));
  }
  pari_err_BUG("rootsof1");
  return NULL; /* LCOV_EXCL_LINE */
}