[R&S] Eindige Velden in Sage

3801 days ago by Bert.Seghers

Eindige velden van priemorde $\mathbb{F}_p$: $\mathbb{Z}$ modulo $p$, $p$ priem.

p = 23 T = IntegerModRing(p) print T T 
       
Ring of integers modulo 23
\newcommand{\Bold}[1]{\mathbf{#1}}\ZZ/23\ZZ
Ring of integers modulo 23
\newcommand{\Bold}[1]{\mathbf{#1}}\ZZ/23\ZZ
T = GF(p) print T T 
       
Finite Field of size 23
\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{F}_{23}
Finite Field of size 23
\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{F}_{23}
T = ZZ.quotient(p*ZZ) print T T 
       
Ring of integers modulo 23
\newcommand{\Bold}[1]{\mathbf{#1}}\ZZ/23\ZZ
Ring of integers modulo 23
\newcommand{\Bold}[1]{\mathbf{#1}}\ZZ/23\ZZ
T.is_field(); T.is_prime_field() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{True}
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{True}
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{True}
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{True}
                                
T.random_element() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}6
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}6
                                
html(T.addition_table()) 
       
+  a b c d e f g h i j k l m n o p q r s t u v w
 +----------------------------------------------
a| a b c d e f g h i j k l m n o p q r s t u v w
b| b c d e f g h i j k l m n o p q r s t u v w a
c| c d e f g h i j k l m n o p q r s t u v w a b
d| d e f g h i j k l m n o p q r s t u v w a b c
e| e f g h i j k l m n o p q r s t u v w a b c d
f| f g h i j k l m n o p q r s t u v w a b c d e
g| g h i j k l m n o p q r s t u v w a b c d e f
h| h i j k l m n o p q r s t u v w a b c d e f g
i| i j k l m n o p q r s t u v w a b c d e f g h
j| j k l m n o p q r s t u v w a b c d e f g h i
k| k l m n o p q r s t u v w a b c d e f g h i j
l| l m n o p q r s t u v w a b c d e f g h i j k
m| m n o p q r s t u v w a b c d e f g h i j k l
n| n o p q r s t u v w a b c d e f g h i j k l m
o| o p q r s t u v w a b c d e f g h i j k l m n
p| p q r s t u v w a b c d e f g h i j k l m n o
q| q r s t u v w a b c d e f g h i j k l m n o p
r| r s t u v w a b c d e f g h i j k l m n o p q
s| s t u v w a b c d e f g h i j k l m n o p q r
t| t u v w a b c d e f g h i j k l m n o p q r s
u| u v w a b c d e f g h i j k l m n o p q r s t
v| v w a b c d e f g h i j k l m n o p q r s t u
w| w a b c d e f g h i j k l m n o p q r s t u v
\newcommand{\Bold}[1]{\mathbf{#1}}
                                
                            
+  a b c d e f g h i j k l m n o p q r s t u v w
 +----------------------------------------------
a| a b c d e f g h i j k l m n o p q r s t u v w
b| b c d e f g h i j k l m n o p q r s t u v w a
c| c d e f g h i j k l m n o p q r s t u v w a b
d| d e f g h i j k l m n o p q r s t u v w a b c
e| e f g h i j k l m n o p q r s t u v w a b c d
f| f g h i j k l m n o p q r s t u v w a b c d e
g| g h i j k l m n o p q r s t u v w a b c d e f
h| h i j k l m n o p q r s t u v w a b c d e f g
i| i j k l m n o p q r s t u v w a b c d e f g h
j| j k l m n o p q r s t u v w a b c d e f g h i
k| k l m n o p q r s t u v w a b c d e f g h i j
l| l m n o p q r s t u v w a b c d e f g h i j k
m| m n o p q r s t u v w a b c d e f g h i j k l
n| n o p q r s t u v w a b c d e f g h i j k l m
o| o p q r s t u v w a b c d e f g h i j k l m n
p| p q r s t u v w a b c d e f g h i j k l m n o
q| q r s t u v w a b c d e f g h i j k l m n o p
r| r s t u v w a b c d e f g h i j k l m n o p q
s| s t u v w a b c d e f g h i j k l m n o p q r
t| t u v w a b c d e f g h i j k l m n o p q r s
u| u v w a b c d e f g h i j k l m n o p q r s t
v| v w a b c d e f g h i j k l m n o p q r s t u
w| w a b c d e f g h i j k l m n o p q r s t u v
\newcommand{\Bold}[1]{\mathbf{#1}}
                                
html(T.multiplication_table()) 
       
*  a b c d e f g h i j k l m n o p q r s t u v w
 +----------------------------------------------
a| a a a a a a a a a a a a a a a a a a a a a a a
b| a b c d e f g h i j k l m n o p q r s t u v w
c| a c e g i k m o q s u w b d f h j l n p r t v
d| a d g j m p s v b e h k n q t w c f i l o r u
e| a e i m q u b f j n r v c g k o s w d h l p t
f| a f k p u c h m r w e j o t b g l q v d i n s
g| a g m s b h n t c i o u d j p v e k q w f l r
h| a h o v f m t d k r b i p w g n u e l s c j q
i| a i q b j r c k s d l t e m u f n v g o w h p
j| a j s e n w i r d m v h q c l u g p b k t f o
k| a k u h r e o b l v i s f p c m w j t g q d n
l| a l w k v j u i t h s g r f q e p d o c n b m
m| a m b n c o d p e q f r g s h t i u j v k w l
n| a n d q g t j w m c p f s i v l b o e r h u k
o| a o f t k b p g u l c q h v m d r i w n e s j
p| a p h w o g v n f u m e t l d s k c r j b q i
q| a q j c s l e u n g w p i b r k d t m f v o h
r| a r l f w q k e v p j d u o i c t n h b s m g
s| a s n i d v q l g b t o j e w r m h c u p k f
t| a t p l h d w s o k g c v r n j f b u q m i e
u| a u r o l i f c w t q n k h e b v s p m j g d
v| a v t r p n l j h f d b w u s q o m k i g e c
w| a w v u t s r q p o n m l k j i h g f e d c b
\newcommand{\Bold}[1]{\mathbf{#1}}
                                
                            
*  a b c d e f g h i j k l m n o p q r s t u v w
 +----------------------------------------------
a| a a a a a a a a a a a a a a a a a a a a a a a
b| a b c d e f g h i j k l m n o p q r s t u v w
c| a c e g i k m o q s u w b d f h j l n p r t v
d| a d g j m p s v b e h k n q t w c f i l o r u
e| a e i m q u b f j n r v c g k o s w d h l p t
f| a f k p u c h m r w e j o t b g l q v d i n s
g| a g m s b h n t c i o u d j p v e k q w f l r
h| a h o v f m t d k r b i p w g n u e l s c j q
i| a i q b j r c k s d l t e m u f n v g o w h p
j| a j s e n w i r d m v h q c l u g p b k t f o
k| a k u h r e o b l v i s f p c m w j t g q d n
l| a l w k v j u i t h s g r f q e p d o c n b m
m| a m b n c o d p e q f r g s h t i u j v k w l
n| a n d q g t j w m c p f s i v l b o e r h u k
o| a o f t k b p g u l c q h v m d r i w n e s j
p| a p h w o g v n f u m e t l d s k c r j b q i
q| a q j c s l e u n g w p i b r k d t m f v o h
r| a r l f w q k e v p j d u o i c t n h b s m g
s| a s n i d v q l g b t o j e w r m h c u p k f
t| a t p l h d w s o k g c v r n j f b u q m i e
u| a u r o l i f c w t q n k h e b v s p m j g d
v| a v t r p n l j h f d b w u s q o m k i g e c
w| a w v u t s r q p o n m l k j i h g f e d c b
\newcommand{\Bold}[1]{\mathbf{#1}}
                                
z = T.multiplicative_generator() z 
       
\newcommand{\Bold}[1]{\mathbf{#1}}5
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}5
                                

Polynomen over $\mathbb{F}_p$

R.<x> = PolynomialRing(T) print parent(x) parent(x) 
       
Univariate Polynomial Ring in x over Ring of integers modulo 23
\newcommand{\Bold}[1]{\mathbf{#1}}\ZZ/23\ZZ[x]
Univariate Polynomial Ring in x over Ring of integers modulo 23
\newcommand{\Bold}[1]{\mathbf{#1}}\ZZ/23\ZZ[x]
pol = x^5+13*x^4+5*x^3+20*x^2-1 pol 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x^{5} + 13 x^{4} + 5 x^{3} + 20 x^{2} + 22
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}x^{5} + 13 x^{4} + 5 x^{3} + 20 x^{2} + 22
                                
factor(pol) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}(x + 18) \cdot (x + 19) \cdot (x + 20) \cdot (x^{2} + 2 x + 5)
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}(x + 18) \cdot (x + 19) \cdot (x + 20) \cdot (x^{2} + 2 x + 5)
                                
var('y') poly = (y+18)*(y+19)*(y+20)*(y^2+2*y+5) print poly.expand() 
       
y^5 + 59*y^4 + 1201*y^3 + 9289*y^2 + 19090*y + 34200
y^5 + 59*y^4 + 1201*y^3 + 9289*y^2 + 19090*y + 34200
[mod(i[0],p) for i in poly.coefficients()] 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[22, 0, 20, 5, 13, 1\right]
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}\left[22, 0, 20, 5, 13, 1\right]
                                

Met polynomen kan je het algoritme van Euclides toepassen, je kan hun grootste gemene deler bepalen, en de polynomen $\mu(x)$ en $\lambda(x)$ waarvoor $\mu(x)a(x)+\lambda(x)b(x)=\text{ggd}(a(x),b(x))$.

a = R.random_element()*R.random_element() b = R.random_element()*R.random_element() print 'a =', a, '\nb =', b, '\nggd(a,b) =', gcd(a,b) 
       
a = 6*x^4 + 13*x^3 + 12*x^2 + 11*x + 20 
b = 13*x^4 + 21*x^3 + 6*x^2 + 2*x + 8 
ggd(a,b) = x + 16
a = 6*x^4 + 13*x^3 + 12*x^2 + 11*x + 20 
b = 13*x^4 + 21*x^3 + 6*x^2 + 2*x + 8 
ggd(a,b) = x + 16
bez = xgcd(a,b) bez 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(x + 16, 7 x^{2} + 22 x + 17, 18 x^{2} + 21 x + 17\right)
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}\left(x + 16, 7 x^{2} + 22 x + 17, 18 x^{2} + 21 x + 17\right)
                                
bez[1]*a+bez[2]*b 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x + 16
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}x + 16
                                

Beschouw nu het quotiënt van de polynoomring $R = \mathbb{F}_{23}[x]$ modulo het polynoom $b(x)$.

Q.<t> = R.quotient(b) print Q print b 
       
Univariate Quotient Polynomial Ring in t over Ring of integers modulo 23
with modulus x^4 + 14*x^3 + 4*x^2 + 9*x + 13
13*x^4 + 21*x^3 + 6*x^2 + 2*x + 8
Univariate Quotient Polynomial Ring in t over Ring of integers modulo 23 with modulus x^4 + 14*x^3 + 4*x^2 + 9*x + 13
13*x^4 + 21*x^3 + 6*x^2 + 2*x + 8
Q.is_field() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{False}
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{False}
                                
Q(b) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}0
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}0
                                
Q(-13*x^4) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}3 t^{3} + 6 t^{2} + 4 t + 9
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}3 t^{3} + 6 t^{2} + 4 t + 9
                                
b; b.change_variable_name(t) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}13 x^{4} + 21 x^{3} + 6 x^{2} + 2 x + 8
\newcommand{\Bold}[1]{\mathbf{#1}}0
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}13 x^{4} + 21 x^{3} + 6 x^{2} + 2 x + 8
\newcommand{\Bold}[1]{\mathbf{#1}}0
                                
html(factor(b)) 
       
(13) * (x + 8) * (x + 16) * (x + 17) * (x + 19)
\newcommand{\Bold}[1]{\mathbf{#1}}
                                
                            
(13) * (x + 8) * (x + 16) * (x + 17) * (x + 19)
\newcommand{\Bold}[1]{\mathbf{#1}}
                                
(t + 8) * (t + 16) * (t + 17) * (t + 19) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}0
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}0
                                

Wanneer we een irreducibel polynoom uitdelen, dus doen alsof die 0 is, krijgen we een eindig veld.

       
\newcommand{\Bold}[1]{\mathbf{#1}}\ZZ/23\ZZ[x]
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}\ZZ/23\ZZ[x]
                                

We zoeken een irreducibel polynoom door er enkele te proberen...

pol = R.random_element((4,7)) while (not pol.is_irreducible()): pol = R.random_element((4,7)) print pol pol.is_irreducible(); pol.is_primitive() 
       
14*x^4 + 11*x^3 + 9*x^2 + 11*x + 15
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{True}
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{False}
                                
                            
14*x^4 + 11*x^3 + 9*x^2 + 11*x + 15
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{True}
\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{False}
                                
F.<x> = R.quotient(pol) print F 
       
Univariate Quotient Polynomial Ring in x over Ring of integers modulo 23
with modulus x^4 + 9*x^3 + 22*x^2 + 9*x + 6
Univariate Quotient Polynomial Ring in x over Ring of integers modulo 23 with modulus x^4 + 9*x^3 + 22*x^2 + 9*x + 6
print F.is_field() print F.is_prime_field() print F.base_ring() print F.order(), '=', F.order().factor() print F 
       
True
False
Ring of integers modulo 23
3404825447 = 23^7
Univariate Quotient Polynomial Ring in x over Ring of integers modulo 23
with modulus x^7 + 18*x^6 + 2*x^5 + 22*x^4 + 10*x^3 + x^2 + 5*x + 3
True
False
Ring of integers modulo 23
3404825447 = 23^7
Univariate Quotient Polynomial Ring in x over Ring of integers modulo 23 with modulus x^7 + 18*x^6 + 2*x^5 + 22*x^4 + 10*x^3 + x^2 + 5*x + 3

Eindige velden $GF(q) = \mathbb{F}_q$, $q=p^h, p$ priem

Hoewel dit de manier is waarop ze eigenlijk gemaakt worden, is de kortste manier om deze velden in Sage te definiëren, de volgende:

F.<a> = GF(23^7, 'a', pol) print F print F.base_ring() F 
       
Finite Field in a of size 23^7
Finite Field of size 23
\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{F}_{23^{7}}
Finite Field in a of size 23^7
Finite Field of size 23
\newcommand{\Bold}[1]{\mathbf{#1}}\Bold{F}_{23^{7}}
F.polynomial() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}12 a^{7} + 9 a^{6} + a^{5} + 11 a^{4} + 5 a^{3} + 12 a^{2} + 14 a + 13
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}12 a^{7} + 9 a^{6} + a^{5} + 11 a^{4} + 5 a^{3} + 12 a^{2} + 14 a + 13
                                
F.multiplicative_generator() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}a
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}a
                                
F.random_element() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}8 a^{5} + 10 a^{4} + 15 a^{3} + 4 a^{2} + 6 a + 10
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}8 a^{5} + 10 a^{4} + 15 a^{3} + 4 a^{2} + 6 a + 10
                                

Nu iets kleiner, een eindig veld van orde 9. Als we geen irreduciebel polynoom opgeven, dan kiest Sage er zelf één.

q = 9 K.<a> = GF(q) K.polynomial() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}a^{2} + 2 a + 2
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}a^{2} + 2 a + 2
                                
for i in K: print i 
       
0
2*a
a + 1
a + 2
2
a
2*a + 2
2*a + 1
1
0
2*a
a + 1
a + 2
2
a
2*a + 2
2*a + 1
1

We stellen de Zech log tabel op.

(K(0)).log(a) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}0
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}0
                                
def theta(n): if a^n + 1 == 0: return Infinity else: return (a^n + 1).log(a) 
       
Table1 = [('$i$', '$\Theta(i)$'), (Infinity, 0)] Table2 = [(n, theta(n)) for n in [0..q-2]] print html.table(Table1 + Table2) 
       

i \Theta(i)
+\infty 0
0 4
1 2
2 7
3 6
4 +\infty
5 3
6 5
7 1
None

i \Theta(i)
+\infty 0
0 4
1 2
2 7
3 6
4 +\infty
5 3
6 5
7 1
None
a^6+a^3 
       
\newcommand{\Bold}[1]{\mathbf{#1}}a
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}a
                                
L = [x for x in K if x != 0 and x.multiplicative_order() == q-1] L 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[2 a, a + 2, a, 2 a + 1\right]
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}\left[2 a, a + 2, a, 2 a + 1\right]
                                

Polynomen en vergelijkingen over eindige velden

F.<u> = GF(8, x^3+x+1) R.<x> = PolynomialRing(F) print F 
       
Finite Field in u of size 2^3
Finite Field in u of size 2^3
print R.random_element() 
       
(u^2 + 1)*x^2 + (u^2 + u + 1)*x + u^2 + u + 1
(u^2 + 1)*x^2 + (u^2 + u + 1)*x + u^2 + u + 1
el = F.random_element() print el.trace(), (x^2+x+el).roots() 
       
0 [(u^2 + u, 1), (u^2 + u + 1, 1)]
0 [(u^2 + u, 1), (u^2 + u + 1, 1)]
pol = R.random_element(3) pol; pol.roots(multiplicities=False) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(u + 1\right) x^{3} + u x^{2} + u
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\right]
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}\left(u + 1\right) x^{3} + u x^{2} + u
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\right]
                                
pol.factor() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(u + 1\right) \cdot (x^{3} + \left(u^{2} + u + 1\right) x^{2} + u^{2} + u + 1)
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}\left(u + 1\right) \cdot (x^{3} + \left(u^{2} + u + 1\right) x^{2} + u^{2} + u + 1)
                                
A = (x^4+2*u*x+(u^2+u+1)) B = (u*x^2+(u+1)*x+1) 
       
xgcd(A,B) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left(1, x + u, \left(u^{2} + 1\right) x^{3} + \left(u + 1\right) x^{2} + u^{2}\right)
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}\left(1, x + u, \left(u^{2} + 1\right) x^{3} + \left(u + 1\right) x^{2} + u^{2}\right)
                                

Oefening 7.20

q = 2048 K.<v> = GF(q) R.<x> = K[] 
       
def po(t): return prod([ (x-r) for r in K if r != 0 and r.multiplicative_order() == t]) 
       
po(4) factor(2047) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}23 \cdot 89
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}23 \cdot 89
                                
po(2047).degree() euler_phi(2047) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}1936
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}1936
                                
 
       
prod(po(t) for t in divisors(q-1)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x^{2047} + 1
                                
                            
\newcommand{\Bold}[1]{\mathbf{#1}}x^{2047} + 1