Repository: sagemath/sage

Issue:
Incorrect assumptions about polynomial in `is_HCP()`
### Steps To Reproduce

The current code has the following snippet:

```py
        p = next_prime(p)
        n += 1
        fp = f.change_ring(GF(p))
        # Compute X^p-X mod fp
        z = fp.parent().gen()
        r = pow(z, p, fp) - z
        d = r.gcd(fp).degree()  # number of roots mod p
        if d == 0:
            continue
        if not fp.is_squarefree():
            continue
        if d < h and d not in h2list:
            return zero
        jp = fp.any_root(degree=1, assume_squarefree=True, assume_distinct_deg=True)
```

The problem is, `fp.any_root()` is called with `assume_distinct_deg` `True`. In this case, the polynomial is expected to be the product of irreducible polynomials of degree `degree` (in this case linear polynomials).

However, this is a typo. The polynomial is not of this form. The correct polynomial to use is the polynomial

```py
r = pow(z, p, fp) - z
r = r.gcd(fp)
```

As a result of this, tests such as:


```py
from sage.schemes.elliptic_curves.cm import is_HCP
set_random_seed(297388353221545796156853787333338705098)
is_HCP(hilbert_class_polynomial(-55))
```

Currently error.

This was introduced in the PR https://github.com/sagemath/sage/pull/37170 because in the refactoring `assume_distinct_deg` worked as intended where as the older function seemed to simply always find roots the slow way regardless of how the function was called.


### Expected Behavior

The function should work

### Actual Behavior

The function does not work

### Additional Information

This has been fixed in https://github.com/sagemath/sage/pull/37443. The only thing needed to do was to use the polynomial `r`, as intended, instead of the polynomial `fp`. This oversight lived in the code until recently purely because any_root() behaved weirdly before.

### Environment

```markdown
- **Sage Version**: SageMath version 10.3.beta8, Release Date: 2024-02-13
```


### Checklist

- [X] I have searched the existing issues for a bug report that matches the one I want to file, without success.
- [X] I have read the documentation and troubleshoot guide


Discussion:

Resolving patch:
diff --git a/src/sage/rings/finite_rings/conway_polynomials.py b/src/sage/rings/finite_rings/conway_polynomials.py
--- a/src/sage/rings/finite_rings/conway_polynomials.py
+++ b/src/sage/rings/finite_rings/conway_polynomials.py
@@ -239,7 +239,7 @@ def polynomial(self, n):
         # TODO: something like the following
         # gcds = [n.gcd(d) for d in self.nodes.keys()]
         # xi = { m: (...) for m in gcds }
-        xi = {q: self.polynomial(n//q).any_root(K, n//q, assume_squarefree=True, assume_distinct_deg=True)
+        xi = {q: self.polynomial(n//q).any_root(K, n//q, assume_squarefree=True, assume_equal_deg=True)
               for q in n.prime_divisors()}
 
         # The following is needed to ensure that in the concrete instantiation
diff --git a/src/sage/rings/polynomial/polynomial_quotient_ring.py b/src/sage/rings/polynomial/polynomial_quotient_ring.py
--- a/src/sage/rings/polynomial/polynomial_quotient_ring.py
+++ b/src/sage/rings/polynomial/polynomial_quotient_ring.py
@@ -2098,7 +2098,7 @@ def _isomorphic_ring(self):
             base_image = self.base_ring().modulus().change_ring(isomorphic_ring).any_root()
             base_to_isomorphic_ring = self.base_ring().hom([isomorphic_ring(base_image)])
             modulus = self.modulus().map_coefficients(base_to_isomorphic_ring)
-            gen = modulus.any_root(assume_squarefree=True, degree=1, assume_distinct_deg=True)
+            gen = modulus.any_root(assume_squarefree=True, degree=1, assume_equal_deg=True)
 
             homspace = Hom(self, isomorphic_ring)
             to_isomorphic_ring = homspace.__make_element_class__(SetMorphism)(homspace,
diff --git a/src/sage/schemes/elliptic_curves/cm.py b/src/sage/schemes/elliptic_curves/cm.py
--- a/src/sage/schemes/elliptic_curves/cm.py
+++ b/src/sage/schemes/elliptic_curves/cm.py
@@ -233,6 +233,13 @@ def is_HCP(f, check_monic_irreducible=True):
         sage: all(not is_HCP(hilbert_class_polynomial(D) + 1)
         ....:     for D in srange(-4,-100,-1) if D.is_discriminant())
         True
+
+    Ensure that :issue:`37471` is fixed::
+
+        sage: from sage.schemes.elliptic_curves.cm import is_HCP
+        sage: set_random_seed(297388353221545796156853787333338705098)
+        sage: is_HCP(hilbert_class_polynomial(-55))
+        -55
     """
     zero = ZZ(0)
     # optional check that input is monic and irreducible
@@ -264,14 +271,15 @@ def is_HCP(f, check_monic_irreducible=True):
         # Compute X^p-X mod fp
         z = fp.parent().gen()
         r = pow(z, p, fp) - z
-        d = r.gcd(fp).degree()  # number of roots mod p
+        r = r.gcd(fp)
+        d = r.degree()  # number of roots mod p
         if d == 0:
             continue
-        if not fp.is_squarefree():
+        if not r.is_squarefree():
             continue
         if d < h and d not in h2list:
             return zero
-        jp = fp.any_root(degree=1, assume_squarefree=True, assume_distinct_deg=True)
+        jp = r.any_root(degree=1, assume_squarefree=True, assume_equal_deg=True)
         E = EllipticCurve(j=jp)
         if E.is_supersingular():
             continue


Test patch: