Opened 23 months ago

# Better warn users against creating finite field with modulus not a polynomial

Reported by: Owned by: dimpase critical sage-9.5 finite rings slelievre N/A

Before this ticket:

```sage: K.<a> = GF(2^6, modulus=x^6 + x^4 + x^3 + x + 1)
sage: x = a^9
sage: K.<a> = GF(2^6, modulus=x^6 + x^4 + x^3 + x + 1)
Traceback (most recent call last):
...
ValueError: the degree of the modulus does not equal the degree of the field
```

Initial report.

Run the following fragment twice in the same Sage session.

```sage: K.<a> = GF(2^6, modulus=x^6 + x^4 + x^3 + x + 1)
sage: x = a^9
sage: y = a^21
sage: z = 1/x
sage: v = vector([y^i for i in range(7)])
sage: S = matrix([[(z^i)^j for j in range(7)] for i in range(7)])
sage: w = S*v
sage: w
```

The 1st run succeeds, the 2nd and later runs produce

```ValueError                                Traceback (most recent call last)
<ipython-input-7-e412ac7787ee> in <module>()
----> 1 K = GF(Integer(2)**Integer(6), modulus=x**Integer(6) + x**Integer(4) + x**Integer(3) + x + Integer(1), names=('a',)); (a,) = K._first_ngens(1)
2 x = a**Integer(9)
3 y = a**Integer(21)
4 z = Integer(1)/x
5 v = vector([y^i for i in range(Integer(7))])

/mnt/opt/Sage/sage-dev/local/lib/python3.7/site-packages/sage/structure/factory.pyx in sage.structure.factory.UniqueFactory.__call__ (build/cythonized/sage/structure/factory.c:2162)()
365             False
366         """
--> 367         key, kwds = self.create_key_and_extra_args(*args, **kwds)
368         version = self.get_version(sage_version)
369         return self.get_object(version, key, kwds)

/mnt/opt/Sage/sage-dev/local/lib/python3.7/site-packages/sage/rings/finite_rings/finite_field_constructor.py in create_key_and_extra_args(self, order, name, modulus, names, impl, proof, check_irreducible, prefix, repr, elem_cache, **kwds)
583
584                     if modulus.degree() != n:
--> 585                         raise ValueError("the degree of the modulus does not equal the degree of the field")
586                     if check_irreducible and not modulus.is_irreducible():
587                         raise ValueError("finite field modulus must be irreducible but it is not")

ValueError: the degree of the modulus does not equal the degree of the field
```

Note that this does not happen if modulus is not given.

This is a variable name clash. Somehow, using 'x' for modulus creates a problem. If I instead define `K` as

```sage: T.<xx> = GF(2)[]
sage: K.<a> = GF(2^6, modulus=xx^6 + xx^4 + xx^3 + xx + 1)
```

then everything works as expected.

### comment:1 Changed 23 months ago by dimpase

• Description modified (diff)

### comment:2 Changed 23 months ago by dimpase

perhaps making it imposible to use a symbolic variable to define modulus will do the trick. Indeed, if I do

```T.<x>=GF(2)[]
K.<a>=GF(2^6, modulus=x^6 + x^4 + x^3 + x + 1)
```

then the error goes away.

Strangely, `x` is the only symbolic variable that is accepted this way. Trying to use a symbolic variable with a different name does not work

```sage: var('tt'); K.<a>=GF(2^6, modulus=tt^6 + tt^4 + tt^3 + tt + 1)
...
TypeError: tt is not a variable of Univariate Polynomial Ring in x over Finite Field of size 2 (using GF2X)
```

Does GF2X do something too smart with `x`?

### comment:3 follow-up: ↓ 7 Changed 23 months ago by roed

I don't think this is a bug. On the first run, you're taking advantage of the fact that `x` is predefined as a symbolic variable (and indeed, it's the only such variable). The constructor for finite fields can take elements of the symbolic ring, though polynomials are better. On the second, you've redefined `x`.

The error is triggered by just the following lines:

```K.<a>=GF(2^6, modulus=x^6 + x^4 + x^3 + x + 1)
x = a^9
K.<a>=GF(2^6, modulus=x^6 + x^4 + x^3 + x + 1)
```

Perhaps a better error message in the case that modulus is a finite field element would be appropriate.

Last edited 23 months ago by roed (previous) (diff)

### comment:4 Changed 21 months ago by embray

• Milestone changed from sage-9.0 to sage-9.1

Ticket retargeted after milestone closed

### comment:5 Changed 17 months ago by mkoeppe

• Milestone changed from sage-9.1 to sage-9.2

### comment:6 Changed 11 months ago by mkoeppe

• Milestone changed from sage-9.2 to sage-9.3

### comment:7 in reply to: ↑ 3 Changed 6 months ago by slelievre

• Description modified (diff)
• Summary changed from breakage in finite field with explicit modulus to Better warn users against creating finite field with modulus not a polynomial

I don't think this is a bug. [...]

The error is triggered by just the following lines:

```K.<a>=GF(2^6, modulus=x^6 + x^4 + x^3 + x + 1)
x = a^9
K.<a>=GF(2^6, modulus=x^6 + x^4 + x^3 + x + 1)
```

Perhaps a better error message in the case that modulus is a finite field element would be appropriate.

I agree. This ticket could do one or both of the following:

• include a cautionary example in the docstring for `FiniteField` (aka `GF`) (I recommend that)
• make the error message more helpful, such as: "check that the polynomial variable used in the modulus was indeed a polynomial variable, and not redefined to be a field element for instance" (maybe not needed if the docstring has a cautionary example)

### comment:8 Changed 6 months ago by mkoeppe

• Milestone changed from sage-9.3 to sage-9.4

Sage development has entered the release candidate phase for 9.3. Setting a new milestone for this ticket based on a cursory review.

### comment:9 Changed 5 weeks ago by mkoeppe

• Milestone changed from sage-9.4 to sage-9.5
Note: See TracTickets for help on using tickets.