# HG changeset patch
# User Luis Felipe Tabera Alonso <lftabera@yahoo.es>
# Parent 9e29a3d84c48c399daaf3920bcb8b17273a0e876
#11784 Add more sanity checks to Galois Field constructor.
diff git a/sage/rings/finite_rings/constructor.py b/sage/rings/finite_rings/constructor.py
a

b


250  250  sage: K.<a> = GF(5**5, name='a', modulus=x^5  x ) 
251  251  Traceback (most recent call last): 
252  252  ... 
253   ValueError: finite field modulus must be irreducible but it is not 
 253  ValueError: finite field modulus must be irreducible but it is not. 
254  254  
255  255  You can't accidentally fool the constructor into thinking the modulus 
256  256  is irreducible when it isn't mod p, since it actually tests 
257   irreducibility modulo p. 
 257  irreducibility modulo p. Also, the modulus has to be of the right degree. 
258  258  
259  259  :: 
260  260  
261  261  sage: F.<x> = QQ[] 
262   sage: factor(x^5+2) 
 262  sage: factor(x^5 + 2) 
263  263  x^5 + 2 
264  264  sage: K.<a> = GF(5**5, name='a', modulus=x^5 + 2 ) 
265  265  Traceback (most recent call last): 
266  266  ... 
267   ValueError: finite field modulus must be irreducible but it is not 
 267  ValueError: finite field modulus must be irreducible but it is not. 
 268  sage: K.<a> = GF(5**5, name='a', modulus=x^3 + 3*x + 3) 
 269  Traceback (most recent call last): 
 270  ... 
 271  ValueError: The degree of the modulus does not correspond to the 
 272  cardinality of the field. 
268  273  
269  274  If you wish to live dangerously, you can tell the constructor not 
270  275  to test irreducibility using check_irreducible=False, but this can 
271  276  easily lead to crashes and hangs  so do not do it unless you know 
272   that the modulus really is irreducible! 
 277  that the modulus really is irreducible and has the correct degree! 
273  278  
274  279  :: 
275  280  
276  281  sage: F.<x> = GF(5)[] 
277  282  sage: K.<a> = GF(5**2, name='a', modulus=x^2 + 2, check_irreducible=False) 
278   
 283  
 284  :: 
 285  
 286  sage: L = GF(3**2, name='a', modulus=QQ[x](x  1), check_irreducible=False) 
 287  sage: L.list() # random 
 288  [0, a, 1, 2, 1, 2, 1, 2, 1] 
 289  
279  290  The order of a finite field must be a prime power:: 
280  291  
281  292  sage: GF(100) 
282  293  Traceback (most recent call last): 
283  294  ... 
284   ValueError: the order of a finite field must be a prime power 
 295  ValueError: the order of a finite field must be a prime power. 
285  296  
286  297  Finite fields with explicit random modulus are not cached:: 
287  298  
… 
… 

316  327  
317  328  :: 
318  329  
319   sage: k.<a> = GF(2^8,repr='int') 
 330  sage: k.<a> = GF(2^8, repr='int') 
320  331  sage: a 
321  332  2 
322  333  
… 
… 

336  347  with WithProof('arithmetic', proof): 
337  348  order = int(order) 
338  349  if order == 1: 
339   raise ValueError("the order of a finite field must be > 1") 
 350  raise ValueError("the order of a finite field must be > 1.") 
340  351  if not arith.is_prime_power(order): 
341   raise ValueError("the order of a finite field must be a prime power") 
 352  raise ValueError("the order of a finite field must be a prime power.") 
342  353  
343  354  if arith.is_prime(order): 
344  355  name = None 
… 
… 

349  360  if not names is None: name = names 
350  361  name = normalize_names(1,name) 
351  362  
352   p,n = arith.factor(order)[0] 
 363  p, n = arith.factor(order)[0] 
353  364  
354  365  if modulus is None or modulus == "default": 
355   if exists_conway_polynomial(p,n): 
 366  if exists_conway_polynomial(p, n): 
356  367  modulus = "conway" 
357  368  else: 
358   if p==2: 
 369  if p == 2: 
359  370  modulus = "minimal_weight" 
360  371  else: 
361  372  modulus = "random" 
… 
… 

370  381  elif sage.rings.polynomial.polynomial_element.is_Polynomial(modulus): 
371  382  modulus = modulus.change_variable_name('x') 
372  383  elif not isinstance(modulus, str): 
373   raise ValueError("Modulus parameter not understood") 
 384  raise ValueError("Modulus parameter not understood.") 
374  385  
375  386  return (order, name, modulus, impl, str(kwds), p, n, proof), kwds 
376  387  
… 
… 

426  437  if modulus.parent().base_ring().characteristic() == 0: 
427  438  modulus = modulus.change_ring(FiniteField(p)) 
428  439  if not modulus.is_irreducible(): 
429   raise ValueError, "finite field modulus must be irreducible but it is not" 
 440  raise ValueError("finite field modulus must be irreducible but it is not.") 
 441  if modulus.degree() != n: 
 442  raise ValueError("The degree of the modulus does not correspond to the cardinality of the field.") 
430  443  if name is None: 
431   raise TypeError, "you must specify the generator name" 
 444  raise TypeError("you must specify the generator name.") 
432  445  if order < zech_log_bound: 
433  446  # DO *NOT* use for prime subfield, since that would lead to 
434  447  # a circular reference in the call to ParentWithGens in the 
… 
… 

564  577  try: 
565  578  return R(sage.databases.conway.ConwayPolynomials()[p][n]) 
566  579  except KeyError: 
567   raise RuntimeError, "requested conway polynomial not in database." 
 580  raise RuntimeError("requested conway polynomial not in database.") 
568  581  
569  582  def exists_conway_polynomial(p, n): 
570  583  r""" 