Repository: sagemath/sage

Issue:
is_prime for ideals uses factorization, can be VERY slow
<div id="comment:0"></div>

The implementation of `is_prime()` for ideals uses pari `idealfactor()`. This is a bad idea if the norm of the ideal is hard to factor.

Pari has `ismaximalideal()`, but it is broken in pari 2.13.3. As a workaround, since a prime ideal has prime power norm, we avoid factorization except on this (easy) case.

---

Was: VERY slow doctest in `sage/rings/number_field/number_field.py`

With sage 9.5:

```
$ time sage -t --long --warn-long 60.0 --random-seed=40834229707572602474454926153679777311 src/sage/rings/number_field/number_field.py 
Running doctests with ID 2022-02-16-15-58-06-58d0ee96.
Using --optional=build,pip,sage,sage_spkg,void
Features to be detected: 4ti2,benzene,bliss,buckygen,conway_polynomials,csdp,database_cremona_ellcurve,database_cremona_mini_ellcurve,database_jones_numfield,database_knotinfo,dvipng,graphviz,imagemagick,jupymake,kenzo,latte_int,lrslib,mcqd,meataxe,pandoc,pdf2svg,plantri,pynormaliz,rubiks,sage.combinat,sage.geometry.polyhedron,sage.graphs,sage.plot,sage.rings.number_field,sage.rings.real_double,sage.symbolic,sage_numerical_backends_coin,sagemath_doc_html,sphinx,tdlib
Doctesting 1 file.
sage -t --long --warn-long 60.0 --random-seed=40834229707572602474454926153679777311 src/sage/rings/number_field/number_field.py
**********************************************************************
File "src/sage/rings/number_field/number_field.py", line 2180, in sage.rings.number_field.number_field.NumberField_generic.?
Warning, slow doctest:
    while not K.random_element().is_prime():  # long time
        pass
Test ran for 1319.62 s
    [2265 tests, 1367.95 s]
----------------------------------------------------------------------
All tests passed!
----------------------------------------------------------------------
Total time for all tests: 1368.3 seconds
    cpu time: 1364.7 seconds
    cumulative wall time: 1367.9 seconds
Features detected for doctesting: 

real	22m51.641s
user	22m47.153s
sys	0m1.272s
```

CC:  @yyyyx4 @fchapoton

Component: **number fields**

Author: **Gonzalo Tornaría, Lorenz Panny**

Reviewer: **Lorenz Panny**

_Issue created by migration from https://trac.sagemath.org/ticket/33360_




Discussion:
<div id="comment:1" align="right">comment:1</div>

Tracked this down to

```
sage: K.<a,b,c> = NumberField([x^2-2,x^2-3,x^2-5])
sage: t = ((-2611940*c + 1925290/7653)*b - 1537130/7653*c + 10130950)*a + (1343014/7653*c - 8349770)*b + 6477058*c - 2801449990/4002519
sage: %time t.is_prime()
CPU times: user 27min 27s, sys: 1.74 s, total: 27min 28s
Wall time: 27min 32s
False
```
Behind the scenes this is handled by pari as follows:

```
sage: %time K.pari_nf().idealfactor(K.ideal(t).pari_hnf())
CPU times: user 27min 30s, sys: 2.26 s, total: 27min 32s
Wall time: 27min 36s
[[2, [1, 0, -1, 0, 0, 0, 0, 0]~, 4, 2, [1, 0, 1, 1, -1, 0, 3, 0; 0, 1, 0, 0, 1, 2, 0, -3; 1, 0, -1, 1, -1, 0, -1, 0; 1, 0, -1, 3, -1, 2, 1, -2; 0, 0, 0, 0, 2, 2, 0, -2; 0, 1, 0, 0, 1, 0, 0, 1; 1, 0, -1, 1, -1, 0, 1, 0; 0, -1, 0, 0, -1, 0, 0, 1]], 6; [3, [-1, 0, -1, 0, 0, 1, 1, -1]~, 2, 2, [0, 0, 3, -3, 3, 0, 3, -3; 0, 0, 0, 0, -3, 0, 3, -6; 1, 0, -2, 1, 0, -1, -3, 0; -1, -2, 0, 3, 1, 5, -3, 0; 0, -2, 1, 1, 1, 4, 0, 0; 0, 1, -2, 1, 1, -2, 0, 3; 1, 1, -1, -1, 1, 0, 0, 0; -1, -2, 0, 0, 1, 2, 0, 0]], -2; [5, [-2, 0, 1, 0, 0, -1, -2, 1]~, 2, 2, [0, 0, 1, 1, -3, -4, 6, -3; 0, 0, -2, -2, 1, -2, 3, -9; 1, -2, -4, 2, -1, -1, -1, -2; 1, -2, -1, 5, 0, 7, -3, 4; 0, 0, 1, 1, 2, 6, -4, 2; -2, -1, -2, 1, 2, -3, 2, -1; 2, 1, -1, 1, -3, 0, 0, 0; -1, -3, -2, 2, -1, 0, 0, 0]], 1; [5, [2, 0, 1, 0, 0, -1, -2, 1]~, 2, 2, [0, 0, 2, 2, -1, 2, 3, 6; 0, 0, 1, 1, 2, 6, -6, 3; 2, 1, -2, 1, -3, 2, -2, 1; 2, 1, -3, 0, -5, -4, 4, -7; 0, 0, -2, -2, 1, -2, 2, -6; 1, 3, 4, -2, 1, 1, -1, 3; 1, -2, -2, 2, -1, 0, 0, 0; 2, 1, 1, -1, -2, 0, 0, 0]], 1; [13, [-1, 3, 4, -5, 0, 4, 6, -4]~, 1, 2, [-1, -3, 11, -4, 0, 7, -9, -12; -3, -4, 11, -4, -4, 14, 12, -3; 6, 6, 5, -3, 7, -7, -11, 11; -4, -9, 10, -6, 8, 3, 3, -10; -5, -10, 7, 1, -4, 2, 7, -14; 6, 12, -11, 4, -3, -5, -11, 22; -3, 4, -6, 1, 0, -5, -1, 3; -4, -1, 6, -1, -1, 10, 3, -4]], 1; [523, [-226, 0, -4, 0, 0, 4, -2, -4]~, 1, 2, [-219, 0, 239, -91, -57, -296, -390, -780; 0, -219, -148, -148, -91, -148, 780, -390; 129, -148, 41, -130, 390, -260, -239, -148; 19, -258, 390, -219, 650, 520, -387, 296; 0, -110, 260, 260, -349, 260, -296, 148; -148, -19, -520, 260, -130, -349, 148, 91; -130, 260, -129, 19, -167, 0, -219, 0; -260, -130, -148, 148, -19, 110, 0, -219]], -1; [523, [-12, 0, -4, 0, 0, 4, -2, -4]~, 1, 2, [-240, 0, -286, 527, -768, -482, 105, 210; 0, -240, -241, -241, 527, -241, -210, 105; -15, -241, -310, 35, -105, 70, 286, -241; 256, 30, -105, -240, -175, -140, 45, 482; 0, 271, -70, -70, -205, -70, -482, 241; -241, -256, 140, -70, 35, -205, 241, -527; 35, -70, 15, 256, -497, 0, -240, 0; 70, 35, -241, 241, -256, -271, 0, -240]], -1; [523, [104, 0, -4, 0, 0, 4, -2, -4]~, 1, 2, [-27, 0, -445, 176, -430, -508, -306, -612; 0, -27, -254, -254, 176, -777, 612, -306; -238, -254, 177, -102, 306, -204, 445, -254; -31, -47, 306, -27, 510, 408, -332, 1031; 0, 207, 204, 204, -129, 204, -508, 777; -254, -492, -408, 204, -102, -129, 254, -699; -102, 204, 238, -31, -223, 0, -27, 0; -204, -102, -254, 254, 31, -207, 0, -27]], -1; [523, [134, 0, -4, 0, 0, 4, -2, -4]~, 1, 2, [142, 0, -422, 229, -36, 386, 657, -255; 0, 142, 193, 193, 229, 193, 255, -912; -205, 193, -296, 219, -134, -85, 422, 193; 12, 410, -134, 665, -49, 693, 615, -386; 0, 217, 85, 85, 361, 608, 386, -193; 193, -12, -170, 85, 219, -162, -193, -229; 219, 85, 205, 12, 181, 0, 142, 0; -85, -304, 193, -193, -12, -217, 0, 142]], -1; [2551, [-1124, -912, -460, -456, 0, 4, -458, -4]~, 1, 2, [-870, 539, 3724, -5054, 6203, -906, 1911, 2565; 539, -331, -2055, 1149, -5054, -2236, -2565, 654; 798, -987, -2144, 637, -1492, 1394, -3724, -2055; -1060, -1777, -2031, 88, -1808, -752, -5960, 1087; 1068, -1858, -1394, -316, -233, -436, -906, 2236; -987, -189, 2249, -855, 637, -113, 2055, 1669; 637, -855, -798, -2128, 2209, 1068, -870, -539; 855, 218, -987, -81, 2128, 1858, -539, -331]], -1; [2551, [64, 537, -1011, -1007, 0, 4, -1009, -4]~, 1, 2, [970, 1217, -1448, 1138, 274, 1189, -3648, 3141; 1217, 2187, -223, 1412, 1138, 879, -3141, 6789; -586, 322, 3402, -1216, 169, 2264, 1448, -223; 821, 2274, -1048, -1292, 339, -4356, 2327, -2291; 545, 1407, -2264, 170, -246, -4526, 1189, -879; 322, -264, 3311, -1047, -1216, 4450, 223, -1671; -1216, -1047, 586, 276, 591, 545, 970, -1217; 1047, 2263, 322, -867, -276, -1407, -1217, 2187]], -1; [2551, [515, 912, 452, 456, 0, 4, 454, -4]~, 1, 2, [-331, -539, -1197, 882, -2937, -906, 654, 2565; -539, -870, 1149, -2055, 882, -1221, -2565, 1911; -504, 81, -767, 218, -1073, 316, 1197, 1149; -879, -1362, -534, -1289, -2467, -2668, -24, 3276; -1068, -375, -316, -1394, -113, -1274, -906, 1221; 81, -423, 1171, -855, 218, -233, -1149, -48; 218, -855, 504, 189, -1176, -1068, -331, 539; 855, 637, 81, 987, -189, 375, 539, -870]], -1; [2551, [601, -537, 1003, 1007, 0, 4, 1005, -4]~, 1, 2, [-364, -1217, -2550, 1671, -1894, 1189, -864, 3141; -1217, -1581, 1412, -223, 1671, 310, -3141, 4005; -1143, 867, 212, -288, -759, -170, 2550, 1412; -281, 1184, 458, -3204, -3023, -4934, 2860, -87; -545, 862, 170, -2264, -652, -2670, 1189, -310; 867, -276, 877, -1047, -288, -246, -1412, -1138; -288, -1047, 1143, 264, 58, -545, -364, 1217; 1047, 1335, 867, -322, -264, -862, 1217, -1581]], -1; [769047171088666806556287061350418679, [368757955557913964915819035850256425, 2, 1, 1, 0, 0, 1, 0]~, 1, 1, [-360391442740426872271373983027096114, 339229659894556457632623858390572228, -913406273986809062698337363348986235, 761380669442646963502839806530380265, -1770246848665485453162356776077448450, -1052964222139293116898955978494394023, -150747332518885405159643602698614955, 584447833742338549861091003834805192; 339229659894556457632623858390572228, -21161782845870414638750124636523886, -44098042916454627239439008947325838, -1008866179222838489659516969547068185, 761380669442646963502839806530380265, -1204989826683455216094453535312999993, -584447833742338549861091003834805192, 735195166261223955020734606533420147; -355143959510323720631278306722530735, -365687421685249248046131662480573287, -259893221061169935498278247894686144, -50249110839628468386547867566204985, -144566833741151048233815800378730079, 534045604475335974252987526335507292, 913406273986809062698337363348986235, -44098042916454627239439008947325838; -118471023802632999370911903629322684, -450603864746353147592457912920612685, -483796493635707505866439658769302307, -316475949105906868032209527713868920, -153118427374107221555609933092915, -345716395527039029001562880576642934, -291583552696646153396116171964013758, 2213856005906293705753970504860068178; -321589378768794620806692653533247449, 236672935707690721260366403093208051, -534045604475335974252987526335507292, 144413715313776941012260190445637164, -410640553580055340657921850593301099, -490130110840815970013823071022280098, -1052964222139293116898955978494394023, 1204989826683455216094453535312999993; -365687421685249248046131662480573287, -720831381195572968677409969203104022, 728861549056115490873351194280442356, -194815944580779516620363667944935064, -50249110839628468386547867566204985, 223903272574537570368161410874616163, 44098042916454627239439008947325838, -957504316903263689937776372296312073; -50249110839628468386547867566204985, -194815944580779516620363667944935064, 355143959510323720631278306722530735, 203118354966161621435780749903924765, -890395155420205490288605065917745501, -321589378768794620806692653533247449, -360391442740426872271373983027096114, -339229659894556457632623858390572228; 194815944580779516620363667944935064, 245065055420407985006911535511140049, -365687421685249248046131662480573287, 687276800454043868852824316013820736, -203118354966161621435780749903924765, -236672935707690721260366403093208051, -339229659894556457632623858390572228, -21161782845870414638750124636523886]], 1; [71327105507448528814867244847534692887701195071, [-14501610102522503320141753964447366602417725200, 2, 1, 1, 0, 0, 1, 0]~, 1, 1, [7761318800804569343815550448810303940131406039, 31828087824754162812918003413439842212493196735, 44565496459391224705057002427769369786093265661, -69761659314715199042978181349764967348985505503, 105630952039571932987025261740912569129431250164, 53556088248458745682645594489282322557506504732, 100462045860708106758655109460988626342758347266, 44690201717315897867085014806266952157805755790; 31828087824754162812918003413439842212493196735, 39589406625558732156733553862250146152624602774, 17686795523602011738598514098134720777060760071, 35869292724856733944047080391147601780445744661, -69761659314715199042978181349764967348985505503, 28359925393134771344724415567286724994614264890, -44690201717315897867085014806266952157805755790, -55771844143392208891570094654721674184952591476; 6456444534689083455711941168591257407733675273, 23747627924020252473748036195805681111522421601, -59213378439667501828621189191848780288374158805, 33487348620236035586218369820329542114252782422, -48384082526008001541913374755751859500188034352, 46724821730526128768613008348862159598428448665, -44565496459391224705057002427769369786093265661, 17686795523602011738598514098134720777060760071; -25591774989594817058483597992915894636164253585, -2239759199845407305297980868030510597913845727, -80212170350762164354831378169191701712681231087, 91667369960258837373475288567486912995194915688, -31452728607025804684690376277734334673630089547, 54112583347910336118269728247831974283193005789, -16205571066256453360332586860482644791479000771, -64229218117991505288771495958434326775060009551; 6060832400418240735149522097670960334461661530, -32048219524283900514195539161507152043897928858, -46724821730526128768613008348862159598428448665, 16931353918982196857222998478017524826557944805, 41248667421040604930033920269139846054384188461, 37181229428928139261046729769814449456635060984, 53556088248458745682645594489282322557506504732, -28359925393134771344724415567286724994614264890; 23747627924020252473748036195805681111522421601, 30204072458709335929459977364396938519256096874, 61621555636298094724308013284284476984363700595, -14896733905771965955695004935422317385935251930, 33487348620236035586218369820329542114252782422, 20998791911094662526210188977342921424307072282, -17686795523602011738598514098134720777060760071, 62252291982993236443655516525904090563154025732; 33487348620236035586218369820329542114252782422, -14896733905771965955695004935422317385935251930, -6456444534689083455711941168591257407733675273, -31652607390013057793633120090586854970625915115, 61461067714451551002530678384063496416609998246, 6060832400418240735149522097670960334461661530, 7761318800804569343815550448810303940131406039, -31828087824754162812918003413439842212493196735; 14896733905771965955695004935422317385935251930, -18590614714464069630523364884907224728317530492, 23747627924020252473748036195805681111522421601, -29808460324438493208897558293476641445984083131, 31652607390013057793633120090586854970625915115, 32048219524283900514195539161507152043897928858, -31828087824754162812918003413439842212493196735, 39589406625558732156733553862250146152624602774]], 1]
```
It is possible to use pari to test for primality much faster without factoring as follows:

```
sage: %time K.pari_nf().idealismaximal(K.ideal(t).pari_hnf())
CPU times: user 5.18 ms, sys: 0 ns, total: 5.18 ms
Wall time: 7.81 ms
0
```
I'll try to cook up a patch for `is_prime()` to use `idealismaximal()` instead of `idealfactor()`.

Another issue: during the call to `idealfactor()`, the one that takes 27 minutes, hitting control C doesn't work for me, but I'm using all system packages (in particular cypari, cysignals, python 3.10, etc) can someone confirm that Ctrl-C is broken on a standard build of sage?
<div id="comment:2" align="right">comment:2</div>

Ctrl-C works for me (I instantaneously get `KeyboardInterrupt`) with 9.6b1 on MacOS 11.5.2.
<div id="comment:3" align="right">comment:3</div>

For the record:

- I have a working patch for `is_prime()` that I'll polish and submit later
- About the issue with Ctrl-C, it turns out this is caused by a recent upgrade of prompt_toolkit (a system package) from 3.0.24 to 3.0.26. I didn't try with 3.0.25 (the bundled version in sage is 3.0.22 and that one works ok)
<div id="comment:5" align="right">comment:5</div>

Cf: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1576

Resolving patch:
diff --git a/src/sage/rings/number_field/number_field_ideal.py b/src/sage/rings/number_field/number_field_ideal.py
--- a/src/sage/rings/number_field/number_field_ideal.py
+++ b/src/sage/rings/number_field/number_field_ideal.py
@@ -996,16 +996,38 @@ def is_prime(self):
             False
             sage: K.ideal(17).is_prime()  # ramified
             False
+
+        TESTS:
+
+        Check that we do not factor the norm of the ideal, this used
+        to take half an hour, see :trac:`33360`::
+
+            sage: K.<a,b,c> = NumberField([x^2-2,x^2-3,x^2-5])
+            sage: t = (((-2611940*c + 1925290/7653)*b - 1537130/7653*c
+            ....:       + 10130950)*a + (1343014/7653*c - 8349770)*b
+            ....:       + 6477058*c - 2801449990/4002519)
+            sage: t.is_prime()
+            False
         """
         try:
             return self._pari_prime is not None
         except AttributeError:
-            F = self.factor()  # factorization with caching
-            if len(F) != 1 or F[0][1] != 1:
-                self._pari_prime = None
-            else:
-                self._pari_prime = F[0][0]._pari_prime
-            return self._pari_prime is not None
+            pass
+
+        K = self.number_field().pari_nf()
+        I = self.pari_hnf()
+
+        candidate = K.idealismaximal(I) or None
+
+        # PARI uses probabilistic primality testing inside idealismaximal().
+        if get_flag(None, 'arithmetic'):
+            # proof required, check using isprime()
+            if candidate and not candidate[0].isprime():
+                candidate = None
+
+        self._pari_prime = candidate
+
+        return self._pari_prime is not None
 
     def pari_prime(self):
         r"""


Test patch: