Merge branch 'master' into bug-4403-remove-polyfill
[maxima.git] / share / contrib / gf / gf_manual.tex
blobcc8f99e18627d0b27160af3ac328fbfca93c1151
1 \documentclass[a4paper,11pt,leqno,fleqn]{artikel3}
3 \usepackage[dvips]{color}
4 %\definecolor{backgray}{gray}{0.925}
5 %\definecolor{verylightgray}{gray}{0.95}
7 \usepackage{fullpage, fancyvrb, amssymb, listings, url}
8 %\usepackage[breqn, inline]{emaxima}
9 %\usepackage[cmbase]{flexisym}
10 %% \usepackage{breqn}
11 %% \setkeys{breqn}{compact}
13 \lstset{
14 language={},
15 keepspaces=true,
16 xleftmargin=3mm,
17 xrightmargin=3mm,
18 basicstyle=\ttfamily,
19 frame=tb,
20 framesep=1mm,
21 framerule=0.5pt,
22 frameround=tttt,
23 columns=flexible %,
24 %backgroundcolor=\color[gray]{0.9}
29 \newcommand{\N}{\noindent}
30 \newcommand{\D}{\displaystyle}
31 \newcommand{\bc}{\begin{center}}
32 \newcommand{\ec}{\end{center}}
33 \newcommand{\bv}{\begin{verbatim}}
34 \newcommand{\ev}{\end{verbatim}}
35 \newcommand{\tr}[1]{\textcolor{red}{#1}}
36 \newcommand{\tb}[1]{\textcolor{blue}{#1}}
37 \newcommand{\rb}[1]{\raisebox{2mm}[0mm][1mm]{#1}}
38 \newcommand{\rbb}[1]{\raisebox{-4mm}[0mm][9mm]{#1}}
40 \title{Finite Fields Computations in Maxima}
42 \author{
43 \begin{tabular}{lr} Fabrizio Caruso & \url{caruso@dm.unipi.it} \\
44 Jacopo D'Aurizio & \url{elianto84@gmail.com} \\
45 Alasdair McAndrew & \url{amca01@gmail.com} \\
46 Volker van Nek & \url{volkervannek@gmail.com}
47 \end{tabular}
50 \date{April, 2008 - July, 2013}
52 \begin{document}
53 \maketitle
56 This file documents a Maxima package for computations in finite fields.
57 It is suitable for teaching and exploration.
58 The first version of the package was based on the paper
59 ``Finite Fields Manipulations in Macsyma'' by Kevin Rowley and Robert
60 Silverman, SIGSAM 1989, but for which the source code is long gone.
61 Meanwhile it contains lots of new features
62 and optimizations implemented by Fabrizio Caruso and Jacopo D'Aurizio.
65 A full review was done in 2012 and 2013 by Volker van Nek. Most of the functions described below
66 became core functions and some function names have been modified.
67 If you use a version of Maxima prior to 5.31 please refer to an appropriate
68 version of this file or alternatively load the necessary files from current sources.
69 These are \texttt{src/numth.lisp} (all basic Galois Fields functions)
70 and \texttt{share/contrib/gf/gf.mac} (square and cubic roots).
71 If speed matters compile these two files and load the binaries.
74 In version 5.29 and later only for root computations it is necessary to
75 load \texttt{gf.mac}.
78 Tests for basic computations in Galois Fields are located in
79 \texttt{src/rtest\_numth.mac}, tests for root computations in
80 \texttt{share/contrib/gf/gf\_test.mac}. Tests can be performed by \\
81 \texttt{batch(<path\_to\_test\_file>, test)}.
84 \section*{Getting started}
85 All user commands are prefixed with ``\verb!gf_!''. All you need to start is
86 to enter the parameters for your field. All fields in this package are of the
87 form
89 \mathbb{F}_p[x]/{m(x)}
91 where $p$ is a prime number and $m(x)$ is an polynomial irreducible over
92 $\mathbb{F}_p$. If the degree of $m(x)$ is $n$, the the finite
93 field will contain $p^n$ elements, each element being a polynomial of degree
94 strictly less than $n$, and all coefficients being in $\{0,1,\ldots,p-1\}$.
95 Such a field is called a \emph{finite field} or \emph{Galois field} of order
96 $p^n$, and is denoted $\mathbb{F}_{p^n}$. Note that although there are many different
97 irreducible polynomials to choose from, if $m(x)$ and $n(x)$ are different
98 polynomials irreducible over $\mathbb{F}_p$ and of the same degree,
99 then the fields
101 \mathbb{F}_p[x]/{m(x)}
105 \mathbb{F}_p[x]/{n(x)}
107 are isomorphic.
109 In these fields, addition and subtraction are performed on the coefficients
110 modulo $p$, and multiplication and division modulo $m(x)$.
112 Given a prime number $p$ and a polynomial $m(x)$
113 you can create a field by using the command ``\verb!gf_set_data(p, m(x))!''.
114 \verb!gf_set_data! checks that $p$ is prime, and it also checks
115 whether $m(x)$ is irreducible over $\mathbb{F}_p$. If these conditions are met,
116 a primitive element in this field is computed and some pre-calculations are performed.
117 Maxima returns a Lisp structure containing the fields data which is
118 suitable for later use by ``\verb!gf_set_again(gf_data)!'' if needed again (see below).
119 Some of these data can be viewed by ``\verb!gf_info()!'' and ``\verb!gf_infolist()!''.
121 %\begin{maximasession}
122 % \maximaoutput*
123 % \i1. gf_set_data(2, x^4+x+1);\\
124 % \o1. [x, x^4+x+1]\\
125 %\end{maximasession}
127 \vspace*{2mm}
128 \begin{lstlisting}[escapechar=\#]
129 #\tr{(\%i1)}# F16 : gf_set_data(2, x^4+x+1);
130 #\tr{(\%o1)}\bc\rb{$\tb{Structure [GF-DATA]}$}\ec#
131 #\vspace{2mm}\tr{(\%i2)}# gf_info()$
132 #\rb{$\tb{characteristic\ =\ 2}$}#
133 #\rb{$\tb{reduction\ polynomial\ =\ x^4+x+1}$}#
134 #\rb{$\tb{primitive\ element\ =\ x}$}#
135 #\rb{$\tb{nr\ of\ elements\ =\ 16}$}#
136 #\rb{$\tb{nr\ of\ units\ =\ 15}$}#
137 #\rb{$\tb{nr\ of\ primitive\ elements\ =\ 8}$}#
138 \end{lstlisting}
140 In case there is no irreducible polynomial $m(x)$ available
141 it is sufficient to set an exponent instead.
142 E.g. ``\verb!gf_set_data(2, 4)!'' returns the same as ``\verb!gf_set_data(2, x^4+x+1)!''.
144 In addition to \verb!gf_set_data! there is a command ``\verb!gf_minimal_set(p, m(x))!''
145 to allow basic arithmetics without checking irreducibility and
146 without computing a primitive element.
149 \bigskip
151 Having set up the field, we can now perform arithmetic on field elements:
154 \paragraph{Addition/subtraction.}
156 These are performed with the commands ``\verb!gf_add!'' and ``\verb!gf_sub!''.
157 In the particular field entered above, since all arithmetic of coefficients is
158 performed modulo 2, addition and subtraction are equivalent:
160 \vspace*{2mm}
161 \begin{lstlisting}[escapechar=\#]
162 #\tr{(\%i3)}# a : x^3+x;
163 #\tr{(\%o3)}\bc\rb{$\tb{x^3+x}$}\ec#
164 #\tr{(\%i4)}# b : x^3+x^2+1;
165 #\tr{(\%o4)}\bc\rb{$\tb{x^3+x^2+1}$}\ec#
166 #\tr{(\%i5)}# gf_add(a, b);
167 #\tr{(\%o5)}\bc\rb{$\tb{x^2+x+1}$}\ec#
168 \end{lstlisting}
171 \paragraph{Multiplication.}
173 This is performed with the command ``\verb!gf_mult!'':
175 \vspace*{2mm}
176 \begin{lstlisting}[escapechar=\#]
177 #\tr{(\%i6)}# gf_mult(a, b);
178 #\tr{(\%o6)}\bc\rb{$\tb{x^3+x+1}$}\ec#
179 \end{lstlisting}
182 \paragraph{Inversion and division.}
184 The inverse of a field element $p(x)$ is the element $q(x)$ for which their
185 product is equal to 1 (modulo $m(x)$). This is performed by
186 ``\verb!gf_inv!''. In a finite field, division is defined as multiplying by
187 the inverse; thus
189 a(x)/b(x)=a(x)(b(x))^{-1}.
191 These operations are performed with the commands ``\verb!gf_inv!'' and
192 ``\verb!gf_div!'':
194 \vspace*{2mm}
195 \begin{lstlisting}[escapechar=\#]
196 #\tr{(\%i7)}# gf_inv(b);
197 #\tr{(\%o7)}\bc\rb{$\tb{x^2}$}\ec#
198 #\tr{(\%i8)}# gf_div(a, b);
199 #\tr{(\%o8)}\bc\rb{$\tb{x^3+x^2+x}$}\ec#
200 #\tr{(\%i9)}# gf_mult(a, gf_inv(b));
201 #\tr{(\%o9)}\bc\rb{$\tb{x^3+x^2+x}$}\ec#
202 \end{lstlisting}
205 \paragraph{Exponentiation.}
207 To raise a field element to an integer power, use ``\verb!gf_exp!'':
209 \vspace*{2mm}
210 \begin{lstlisting}[escapechar=\#]
211 #\tr{(\%i10)}# gf_exp(a, 14);
212 #\tr{(\%o10)}\bc\rb{$\tb{x^3+x^2}$}\ec#
213 #\tr{(\%i11)}# gf_exp(a, 15);
214 #\tr{(\%o11)}\bc\rb{$\tb{1}$}\ec#
215 \end{lstlisting}
218 \paragraph{Random elements.}
220 Finally, a random element can be obtained with ``\verb!gf_random()!'':
222 \vspace*{2mm}
223 \begin{lstlisting}[escapechar=\#]
224 #\tr{(\%i12)}# makelist(gf_random(), i,1,3);
225 #\tr{(\%o12)}\bc\rb{$\tb{[\,x^2+x+1,\ x^2+x,\ x^3\,]}$}\ec#
226 \end{lstlisting}
229 \section*{Primitive elements, powers and logarithms}
231 The non-zero elements of a finite field form a multiplicative group; a
232 generator of this group is a \emph{primitive element} in the field. The
233 command ``\verb!gf_primitive()!'' returns the already computed primitive element:
235 \vspace*{2mm}
236 \begin{lstlisting}[escapechar=\#]
237 #\tr{(\%i13)}# gf_primitive();
238 #\tr{(\%o13)}\bc\rb{$\tb{x}$}\ec#
239 \end{lstlisting}
241 Given that any non-zero element in the field can be expressed as a power of
242 this primitive element, this power is the \emph{index} of the element; its
243 value is obtained with ``\verb!gf_index!'':
245 \vspace*{2mm}
246 \begin{lstlisting}[escapechar=\#]
247 #\tr{(\%i14)}# a : x^3+x#\$#
248 #\tr{(\%i15)}# gf_index(a);
249 #\tr{(\%o15)}\bc\rb{$\tb{9}$}\ec#
250 #\tr{(\%i16)}# is(a = gf_exp(x, 9));
251 #\tr{(\%o16)}\bc\rb{$\tb{true}$}\ec#
252 \end{lstlisting}
254 Since every element of the field can be represented as a polynomial
256 a_{n-1}x^{n-1}+a_{n-2}x^{n-2}+\cdots+a_2x^2+a_1x+a_0
258 where every coefficient $a_i$ satisfies $0\le a_i\le p-1$, a field element can
259 also be considered as a list:
261 [a_{n-1},a_{n-2},\ldots,a_2,a_1,a_0].
263 This list can be considered as the ``digits'' of a number in base $p$, in
264 which the field element is equivalent to the number
266 a_{n-1}p^{n-1}+a_{n-2}p^{n-2}+\cdots+a_2p^2+a_1p+a_0.
268 Thus every polynomial is equivalent to a number between 0 and $p^n-1$; this
269 number is obtained by ``\texttt{gf\_p2n}''.
270 The reverse direction is given by ``\texttt{gf\_n2p}''.
272 Since every non-zero field element $a=a(x)$ is both equivalent to a number $A$
273 and a power $i$ of a primitive element $e$, we can create an array of powers
274 corresponding to particular numbers. This array, \texttt{gf\_powers},
275 which is created by \verb!gf_make_logs!,
276 is defined as follows:
277 its $i$-th element (starting with zero) is the numerical form of the $i$-th power of the
278 primitive element. Thus, if
280 a(x)\equiv A\equiv e^i
282 where $e$ is the primitive element, then the $i$-th element of \texttt{gf\_powers} is
283 $A$. By definition we have $e^{p^n-1}=1$.
285 The numbers $A$ run over all integers from 1 to $p^n-1$, and the powers $i$
286 run over all the integers from 0 to $p^n-1$, there is a corresponding
287 ``logarithm'' array, \texttt{gf\_logs}.
288 The logarithm table may be considered to be
289 indexed from 0 to $p^n-1$, and its $i$-th element (ignoring the 0-th) is the power
290 corresponding to that element.
292 The last array returned by \verb!gf_make_logs! is \verb!gf_zech_logs!
293 which enables efficient addition.
295 \vspace*{2mm}
296 \begin{lstlisting}[escapechar=\#]
297 #\tr{(\%i17)}# map(listarray, gf_make_logs());
298 #\vspace{-4mm}#
299 #\tr{(\%o17)}\bc\rb{$\tb{
300 [\,\,[1,2,4,8,3,6,12,11,5,10,7,14,15,13,9,1],
301 }$}\ec#
302 #\bc\hspace*{1mm}\rb{$\tb{
303 [false,0,1,4,2,8,5,10,3,14,9,7,6,13,11,12],
304 }$}\ec#
305 #\bc\hspace*{1mm}\rb{$\tb{
306 [false,4,8,14,1,10,13,9,2,7,5,12,11,6,3,false]\,\,]
307 }$}\ec#
308 #\tr{(\%i18)}# c : gf_exp(x, 4);
309 #\tr{(\%o18)}\bc\rb{$\tb{x + 1}$}\ec#
310 #\tr{(\%i19)}# gf_p2n(c);
311 #\tr{(\%o19)}\bc\rb{$\tb{3}$}\ec#
312 #\tr{(\%i20)}# gf_index(c);
313 #\tr{(\%o20)}\bc\rb{$\tb{4}$}\ec#
314 #\tr{(\%i21)}# gf_logs[3];
315 #\tr{(\%o21)}\bc\rb{$\tb{4}$}\ec#
316 #\tr{(\%i22)}# gf_powers[4];
317 #\tr{(\%o22)}\bc\rb{$\tb{3}$}\ec#
318 \end{lstlisting}
320 The creation of the arrays \texttt{gf\_logs} and \texttt{gf\_powers}
321 only has to be done once.
324 \paragraph{Logarithms.}
326 The array \texttt{gf\_logs} contains the logarithm of any non-zero element
327 with respect to the primitive element \texttt{e} of the field.
328 The same holds for \texttt{gf\_ind}. The logarithm
329 of any element relative to the base of another can be obtained by the
330 command ``\verb!gf_log!'':
332 \vspace*{2mm}
333 \begin{lstlisting}[escapechar=\#]
334 #\tr{(\%i23)}# a : x^3+x#\$#
335 #\tr{(\%i24)}# b : x^3+x^2+1#\$#
336 #\tr{(\%i25)}# gf_log(a, b);
337 #\tr{(\%o25)}\vspace*{2mm}\bc\rb{$\tb{3}$}\ec#
338 #\tr{(\%i26)}# is(a = gf_exp(b, 3));
339 #\tr{(\%o26)}\bc\rb{$\tb{true}$}\ec#
340 \end{lstlisting}
342 We conclude that, in our field, $a=b^{3}$.
345 \paragraph{Primitive elements.}
347 A given field will have many primitive elements, and the command \\
348 ``\verb!gf_primitive_p!'' tests whether an element is primitive:
350 \vspace*{2mm}
351 \begin{lstlisting}[escapechar=\#]
352 #\tr{(\%i27)}# gf_primitive_p(a);
353 #\tr{(\%o27)}\vspace{0mm}\bc\rb{$\tb{false}$}\ec#
354 #\tr{(\%i28)}# gf_primitive_p(b);
355 #\tr{(\%o28)}\bc\rb{$\tb{true}$}\ec#
356 \end{lstlisting}
359 \paragraph{Order.}
361 By definition, any element $a$ of the field will satisfy $a^{p^n-1}=1$. The
362 \emph{order} of $a$ is the \emph{lowest} power $m$ for which $a^m=1$. It will
363 be a factor of $p^n-1$, and is obtained with ``\verb!gf_order!'':
365 \vspace*{2mm}
366 \begin{lstlisting}[escapechar=\#]
367 #\tr{(\%i29)}# gf_order(a);
368 #\tr{(\%o29)}\bc\rb{$\tb{5}$}\ec#
369 #\tr{(\%i30)}# gf_order(b);
370 #\tr{(\%o30)}\bc\rb{$\tb{15}$}\ec#
371 \end{lstlisting}
374 \section*{Minimal polynomials}
376 Associated with every element $a\in GF(p^n)$ is a polynomial $p(x)$ which
377 satisfies:
378 \begin{enumerate}
379 \item $p(a)=0$,
380 \item the coefficient of the highest power in $p(x)$ is one,
381 \item for any other polynomial $q(x)$ with $q(a)=0$, $p(x)$ is a divisor of $q(x)$.
382 \end{enumerate}
383 The polynomial $p(x)$ is thus, in a very strict sense, the \emph{smallest}
384 polynomial which has $a$ as a root. It is the \emph{minimal polynomial} of
385 $a$. The command ``\verb!gf_minimal_poly!'' calculates it:
387 \vspace*{2mm}
388 \begin{lstlisting}[escapechar=\#]
389 #\tr{(\%i31)}# a : x^3+x#\$#
390 #\tr{(\%i32)}# p : gf_minimal_poly(a);
391 #\tr{(\%o32)}\bc\rb{$\tb{z^4+z^3+z^2+z+1}$}\ec#
392 \end{lstlisting}
394 To check this, substitute $a$ for $z$ in $p$:
396 \vspace*{2mm}
397 \begin{lstlisting}[escapechar=\#]
398 #\tr{(\%i33)}# subst(a, z, p);
399 #\tr{(\%o33)}\bc\rb{$\tb{(x^3+x)^4+(x^3+x)^3+(x^3+x)^2+x^3+x+1}$}\ec#
400 #\tr{(\%i34)}# gf_eval(%);
401 #\tr{(\%o34)}\bc\rb{$\tb{0}$}\ec#
402 \end{lstlisting}
405 \section*{An application: the Chor-Rivest knapsack cryptosystem}
407 The Chor-Rivest knapsack cryptosystem is the only knapsack cryptosystem which
408 doesn't use modular arithmetic; instead it uses the arithmetic of finite
409 fields. Although it has been broken, it is still a very good example of
410 finite field arithmetic.
412 Assuming the two protagonists are Alice and Bob, and Alice wishes to set up a
413 public key for Bob to encrypt messages to her. Alice chooses a finite field
414 $\mathbb{F}_{p^n}=\mathbb{F}_p[x]/m(x)$, and a random primitive element $g(x)$. She
415 then computes $a_i=\log_{g(x)}(x+i)$ for every $i\in\mathbb{F}_p$. She
416 selects a random integer $d$ for which $0\le d\le p^n-2$, and computes
417 $c_i=(a_i+d)\pmod{p^n-1}$. Her public key is the sequence $c_i$, with the
418 parameters $p$ and $n$.
420 To encrypt a message to Alice, Bob encodes the message as binary blocks of
421 length $p$ which contain $n$ ones. Given one such block
422 $M=(M_0,M_1,\ldots,M_{p-1})$, Bob creates the cipher text
424 c=\sum_{i=0}^{p-1}M_ic_i\pmod{p^n-1}
426 which he sends to Alice.
428 To decrypt $c$, Alice first computes $r=(c-nd)\pmod{p^n-1}$, and then computes
429 $u(x)=g(x)^r\pmod{m(x)}$. She then computes $s(x)=u(x)+m(x)$ and factors $s$
430 into linear factors $x+t_i$. The $t_i$ values are the positions of the ones
431 in the message block $M$.
433 Actually, the complete cryptosystem also involves a permutation, which is
434 applied to the sequence $a_i$ to create $c_i$. But for this example we are
435 just interested in the field arithmetic.
437 We shall choose the example given in chapter 8 of HAC, but without the
438 permutation. Here the field is
440 GF(7^4)=\mathbb{F}_7[x]/(x^4+3x^3+5x^2+6x+2)
442 and the primitive element chosen is $g(x)=3x^3+3x^2+6$ and the random integer
443 $d$ is 1702.
445 First, Alice must compute her public key:
447 \vspace*{2mm}
448 \begin{lstlisting}[escapechar=\#]
449 #\tr{(\%i35)}# gf_set_data(7, x^4+3*x^3+5*x^2+6*x+2)#\$#
450 #\tr{(\%i36)}# g : 3*x^3+3*x^2+6#\$#
451 #\tr{(\%i37)}# gf_primitive_p(g);
452 #\tr{(\%o37)}\bc\rb{$\tb{true}$}\ec#
453 #\tr{(\%i38)}# a : makelist(gf_log(x+i, g), i,0,6);
454 #\tr{(\%o38)}\bc\rb{$\tb{[\,1028,\,1935,\,2054,\,1008,\,379,\,1780,\,223\,]}$}\ec#
455 #\tr{(\%i39)}# d : 1702#\$#
456 #\tr{(\%i40)}# c : makelist(mod(a[i] + d, gf_order()), i,1,7);
457 #\tr{(\%o40)}\bc\rb{$\tb{[\,330,\,1237,\,1356,\,310,\,2081,\,1082,\,1925\,]}$}\ec#
458 \end{lstlisting}
460 Now Bob can encrypt a message to Alice; suppose one such block is
461 $[1,0,1,1,0,0,1]$, which is a block of length 7 which contains exactly 4 ones.
463 \vspace*{2mm}
464 \begin{lstlisting}[escapechar=\#]
465 #\tr{(\%i41)}# M : [1,0,1,1,0,0,1];
466 #\tr{(\%o41)}\bc\rb{$\tb{[\,1,\,0,\,1,\,1,\,0,\,0,\,1\,]}$}\ec#
467 #\tr{(\%i42)}# c : mod(sum(M[i] * c[i], i,1,7), gf_order());
468 #\tr{(\%o42)}\bc\rb{$\tb{1521}$}\ec#
469 \end{lstlisting}
471 This last value is the ciphertext. Alice now needs to decrypt it:
473 \vspace*{-4mm}
474 \begin{lstlisting}[escapechar=\#]
475 #\tr{(\%i43)}# r : mod(c - gf_exponent() * d, gf_order());
476 #\tr{(\%o43)}\bc\rb{$\tb{1913}$}\ec#
477 #\tr{(\%i44)}# u : gf_exp(g, r);
478 #\tr{(\%o44)}\bc\rb{$\tb{x^3+3\,x^2+2\,x+5}$}\ec#
479 #\tr{(\%i45)}# s : u + gf_reduction();
480 #\tr{(\%o45)}\bc\rb{$\tb{x^4+4\,x^3+8\,x^2+8\,x+7}$}\ec#
481 #\tr{(\%i46)}# gf_factor(s);
482 #\tr{(\%o46)}\vspace{0mm}\bc\rb{$\tb{x\ (x + 2)\ (x + 3)\ (x + 6)}$}\ec#
483 \end{lstlisting}
485 The $t_i$ values are $0,2,3,6$ and these are the positions of the ones in $M$.
487 \vspace*{-3mm}
488 \section*{Matrices}
490 There are commands for dealing with matrices over finite fields. E.g.
491 ``\verb!gf_invert_by_lu!'' for inverting a matrix, and ``\verb!gf_matmult!'' for
492 multiplying matrices.
494 \vspace*{2mm}
495 \begin{lstlisting}[escapechar=\#]
496 #\tr{(\%i47)}# gf_set_again(F16)#\$#
497 #\tr{(\%i48)}# m : matrix([1,x^3+1], [x^2+1,x]);
498 #\tr{(\%o48)}\bc\rbb{$\tb{\D{\left(\begin{array}[h]{cc}1&x^3+1\\x^2+1&x\end{array}\right)}}$}\vspace{3mm}\ec#
499 #\tr{(\%i49)}# m_inv : gf_invert_by_lu(m);
500 #\tr{(\%o49)}\bc\rbb{$\tb{\D{\left(\begin{array}[h]{cc}x^2&1\\x^3+x&x\end{array}\right)}}$}\vspace{3mm}\ec#
501 #\tr{(\%i50)}# gf_matmult(m, m_inv);
502 #\tr{(\%o50)}\bc\rbb{$\tb{\D{\left(\begin{array}[h]{cc}1&0\\0&1\end{array}\right)}}$}\vspace{0mm}\ec#
503 \end{lstlisting}
505 \vspace*{-3mm}
506 \section*{Normal bases}
508 Any field $GF(p^n)$ may be considered as a vector space over
509 $\mathbb{F}_p$; one basis is the set
511 \{1,x,x^2,\ldots,x^{n-1}\}
513 which is called the \emph{polynomial basis}. A \emph{normal element} is a
514 field element $e$ for which the set
516 \{e,e^p,e^{p^2},\ldots,e^{p^{n-1}}\}
518 forms a basis. There are several commands for dealing with normal elements
519 and bases. The command ``\verb!gf_random_normal()!'' finds a normal element by
520 simply picking field elements at random and testing each one for normality.
521 Although this is a probabilistic algorithm, in practice it works very quickly:
524 \vspace*{2mm}
525 \begin{lstlisting}[escapechar=\#]
526 #\tr{(\%i51)}# gf_set_data(2, x^10+x^3+1)#\$#
527 #\tr{(\%i52)}# p : gf_random_normal();
528 #\tr{(\%o52)}\vspace{0mm}\bc\rb{$\tb{x^9+x^8+x^7+x^6+x^5+x}$}\ec#
529 \end{lstlisting}
531 The command ``\verb!gf_normal()!'' is a brute force search through all
532 field elements; in general it is slower than \verb!gf_random_normal()!.
534 Having found a normal element the command ``\verb!gf_normal_basis()!'' produces a
535 matrix the rows of which are the coefficients of the basis elements
536 $e^{p^k}$. This command takes an optional parameter; a polynomial $p$. If
537 present, \verb!gf_normal_basis()! checks if the field element is normal, and if
538 so, produces the matrix, otherwise prints an error message. If the parameter
539 is not given, \verb!gf_normal_basis()! first finds a normal element, and then
540 uses that element to produce the matrix.
542 With the normal basis, the command ``\verb!gf_normal_basis_rep(p, mat)!'' produces the
543 normal basis representation of \texttt{p}, with respect to the basis
544 \texttt{mat}, as a list of coefficients. One attraction of using normal bases
545 is that much arithmetic can be simplified; for example, in a normal basis
546 representation, a power of the prime $p$ is equivalent to a shift of
547 coefficients:
549 \vspace*{2mm}
550 \begin{lstlisting}[escapechar=\#]
551 #\tr{(\%i53)}# m : gf_normal_basis(p)#\$#
552 #\tr{(\%i54)}# a : gf_random();
553 #\tr{(\%o54)}\vspace{0mm}\bc\rb{$\tb{x^9+x^5+x^3+x^2+1}$}\ec#
554 #\tr{(\%i55)}# gf_normal_basis_rep(a, m);
555 #\tr{(\%o55)}\bc\rb{$\tb{[\,1,\,0,\,1,\,0,\,0,\,1,\,1,\,0,\,0,\,0\,]}$}\ec#
556 #\tr{(\%i56)}# gf_normal_basis_rep(gf_exp(a, 2), m);
557 #\tr{(\%o56)}\bc\rb{$\tb{[\,1,\,1,\,0,\,1,\,0,\,1,\,0,\,1,\,1,\,1\,]}$}\ec#
558 \end{lstlisting}
561 \section*{Large fields}
563 \texttt{gf\_set\_data} computes and stores powers $x^{p^k}$.
564 In case \texttt{gf\_set\_data} was called \verb!gf_exp! uses the technique
565 of modular composition. Otherwise \verb!gf_exp! performs ``repeated squaring''.
566 \verb!gf_index! resp. \verb!gf_log! use a
567 Pohlig-Hellman reduction and Brent's version of Pollard Rho.
569 \vspace*{2mm}
570 \begin{lstlisting}[escapechar=\#]
571 #\tr{(\%i57)}# gf_set_data(2, x^20+x^3+1);
572 #\tr{(\%o57)}\bc\rb{$\tb{Structure [GF-DATA]}$}\ec#
573 #\vspace{2mm}\tr{(\%i58)}# gf_info()$
574 #\rb{$\tb{characteristic\ =\ 2}$}#
575 #\rb{$\tb{reduction\ polynomial\ =\ x^{20}+x^3+1}$}#
576 #\rb{$\tb{primitive\ element\ =\ x}$}#
577 #\rb{$\tb{nr\ of\ elements\ =\ 1048576}$}#
578 #\rb{$\tb{nr\ of\ units\ =\ 1048575}$}#
579 #\rb{$\tb{nr\ of\ primitive\ elements\ =\ 480000}$}#
580 #\tr{(\%i59)}# a : x^15+x^5+1;
581 #\tr{(\%o59)}\bc\rb{$\tb{x^{15}+x^5+1}$}\ec#
582 #\tr{(\%i60)}# index : gf_index(a);
583 #\tr{(\%o60)}\bc\rb{$\tb{720548}$}\ec#
584 #\tr{(\%i61)}# gf_exp(gf_primitive(), index);
585 #\tr{(\%o61)}\bc\rb{$\tb{x^{15}+x^5+1}$}\ec#
586 #\tr{(\%i62)}# gf_exp(a, 3^12);
587 #\tr{(\%o62)}\bc\rb{$\tb{x^{17}+x^{16}+x^{13}+x^{12}+x^{11}+x^3+x^2+x}$}\ec#
588 \end{lstlisting}
591 \section*{Field extensions - the AES mixed columns operation}
593 Above we described that an element of a finite field $GF(p^n)$ can be interpreted as a polynomial
594 $f(x) = c_{n-1}\,x^{n-1} + c_{n-2}\,x^{n-2} + \cdot \cdot +\,c_1\,x + c_0$
595 of degree $n-1$ with coefficients in the prime field $GF(p)$.
597 An element of an extension field $GF(q^k)$ where $q$ is a power of $p$, $q = p^n$,
598 can be interpreted as a polynomial of degree $k-1$ with coefficients in
599 the base field $GF(p^n)$ which means that the coefficients itself can be interpreted
600 as polynomials of degree $n-1$ over $GF(p)$ and all coefficient arithmetic
601 is carried out in $GF(p^n)$.
603 The reduction polynomial of degree $k$ used to define $GF(q^k)$
604 must be irreducible over $GF(p^n)$. Otherwise the defined extension is no field and
605 not every non-zero element is invertible.
607 The AES mixed columns operation is for example defined by a multiplication
608 in $GF(256^4)$ where the non-irreducible polynomial $x^4+1$ is used for reduction.
609 The element $a = 3\,x^3+x^2+x+2$ used for the mixed columns multiplication is invertible,
610 $a^{-1} = B\,x^3+D\,x^2+9\,x+E$ in base $16$, so the inverse operation is guaranteed.
612 The following session shows the mixed columns operation applied to one column represented
613 by the list $[30, 5D, BF, D4]$. (These four bytes are taken from the example on page 33 of the
614 AES specification document fips-197.pdf and they are the first four bytes that are modified
615 by the mixed columns operation there.)
617 User commands for field extensions are prefixed with ``\verb!ef_!'' and for nearly every
618 \texttt{gf}-function there is a corresponding \texttt{ef}-function.
619 The polynomial \texttt{m(x)} used for reduction can be defined by
620 ``\verb!ef_minimal_set(m(x))!'' or ``\verb!ef_set_data(m(x))!''.
621 The \texttt{ef}-functions then refer to the
622 previously by \texttt{gf\_set\_data} defined field as the base field.
624 \vspace*{4mm}
625 \begin{lstlisting}[escapechar=\#]
626 #\tr{(\%i1)}# gf_set_data(2, x^8+x^4+x^3+x+1)#\$#
627 #\tr{(\%i2)}# ef_irreducible_p(x^4+1);
628 #\tr{(\%o2)}\bc\rb{$\tb{false}$}\ec#
629 #\tr{(\%i3)}# ef_minimal_set(x^4+1);
630 #\tr{(\%o3)}\bc\rb{$\tb{reduction\ polynomial\ =\ x^4+1}$}\ec#
631 #\tr{(\%i4)}# ibase : obase : 16#\$#
632 #\tr{(\%i5)}# p : ef_l2p([30, 5D, 0BF, 0D4]);
633 #\tr{(\%o5)}\bc\rb{$\tb{30\ x^3+5D\ x^2+0BF\ x+0D4}$}\ec#
634 #\tr{(\%i6)}# a : 3*x^3+x^2+x+2;
635 #\tr{(\%o6)}\bc\rb{$\tb{3\ x^3+x^2+x+2}$}\ec#
636 #\tr{(\%i7)}# ef_p2l(pa : ef_mult(p, a));
637 #\tr{(\%o7)}\bc\rb{$\tb{[0E5,\ 81,\ 66,\ 4]}$}\ec#
638 #\tr{(\%i8)}# ai : ef_inv(a);
639 #\tr{(\%o8)}\bc\rb{$\tb{0B\ x^3+0D\ x^2+9\ x+0E}$}\ec#
640 #\tr{(\%i9)}# ef_mult(ai, a);
641 #\tr{(\%o9)}\bc\rb{$\tb{1}$}\ec#
642 #\tr{(\%i10)}# ef_mult(ai, pa);
643 #\tr{(\%o10)}\bc\rb{$\tb{30\ x^3+5D\ x^2+0BF\ x+0D4}$}\ec#
644 #\tr{(\%i11)}# ibase : obase : 10.#\$#
645 \end{lstlisting}
649 \section*{Square and cube roots}
650 Multiple algorithms have been implemented in order to solve the square and cube root extraction problem over $\mathbb{F}_p$; all of them basically perform an exponentiation in a extension field (ie $\mathbb{F}_{p^2}=\mathbb{F}_{p}[x]/(x^2+bx+a)$ or $\mathbb{F}_{p^3}=\mathbb{F}_{p}[x]/(x^3-bx-a)$) through a repeated-squaring scheme, reaching so a complexity of $O(n \log(p))$ multiplications in $\mathbb{F}_p$; however, due to some differences in the representation and multiplication of elements in the extension field, they do not have exactly the same running time:
651 \begin{enumerate}
652 \item \verb!msqrt(a,p)! returns the two square roots of $a$ in $\mathbb{F}_p$ (if they exist) representing every $k$-th power of $x$ in $\mathbb{F}_{p}[x]/(x^2+bx+a)$ as the first column of the matrix $M^k$, where $M$ is the companion matrix associated with the polynomial $x^2+bx+a$ and $b^2-4a$ is a quadratic non-residue in $\mathbb{F}_p^*$. It requires $5 \log_2(p)$ multiplications in $\mathbb{F}_p$.
653 \item \verb!ssqrt(a,p)! returns the two square roots of $a$ in $\mathbb{F}_p$ (if they exist) using Shanks algorithm. It requires $5 \log_2(p)$ multiplications in $\mathbb{F}_p$.
654 \item \verb!gf_sqrt(a,p)! returns the two square roots of $a$ in $\mathbb{F}_p$ (if they exist) using the Muller algorithm (an improved, shifted version of Cipolla-Lehmer's) and should reach the best performance, requiring only $2 \log_2(p)$ multiplications in $\mathbb{F}_p$.
655 \item \verb!mcbrt(a,p)! returns the cube roots of $a$ in $\mathbb{F}_p$ (if they exist) representing every $k$-th power of $x$ in $\mathbb{F}_{p}[x]/(x^3+bx+a)$ as the vector $(M_{2,2},M_{2,3},M_{3,2})$ in the matrix $M^k$, where $M$ is the companion matrix associated with the polynomial $x^3+bx+a$, irreducible over $\mathbb{F}_p$ (Stickelberger-Redei irreducibility test for cubic polynomials is used). It requires $10 \log_2(p)$ multiplications in $\mathbb{F}_p$.
656 \item \verb!scbrt(a,p)! follows the same multiplication steps of \verb!mcbrt(a,p)!, using a simpler polynomial representation for the elements of the field extension. It requires about $11 \log_2(p)$ multiplications in $\mathbb{F}_p$.
657 \item \verb!gf_cbrt(a,p)! returns the cube roots of $a$ in $\mathbb{F}_p$ (if they exist) using the generalized Shanks algorithm: it's pretty fast, requiring about $4 \log_2(p)$ multiplications in $\mathbb{F}_p$, being so the candidate choice for cube root extraction.
658 \end{enumerate}
659 Other implemented routines, using about the same ideas, are:
660 \begin{enumerate}
661 \item \verb!lucas(n)!, returning the $n$-th Lucas number through a Muller-like scheme; it requires exactly $2$ squarings and $3$ sums for each bit in the binary representation of $n$, having so a bit-complexity bounded by $2\log_2(n)^{3+\varepsilon}$, with $\varepsilon$ depending on the adopted integer squaring algorithm.
662 \item \verb!qsplit(p)! and \verb!csplit(p)!, splitting a prime $p$ over $\mathbb{Z}[i]$ and $\mathbb{Z}[\omega]$, ie finding $(a,b)$ such that $p=a^2+b^2$ (this is possible only when $p$ is in the form $4k+1$) or $p=a^2+ab+b^2$ (this is possible only when $p$ is in the form $3k+1$), by the reduction of a binary quadratic form of the proper discriminant. They have the same complexity of the computation of a single Jacobi symbol, $O(\log(p)^2)$ bit-operations.
663 \end{enumerate}
664 \vspace{1cm}
666 In Maxima 5.29 and later \verb!lucas! is a core function.
668 \vspace*{2mm}
669 \begin{lstlisting}[escapechar=\#]
670 #\tr{(\%i1)}# lucas(141);
671 #\tr{(\%o1)}\bc\rb{$\tb{293263001536128903730947142076}$}\ec#
672 \end{lstlisting}
674 \vspace*{4mm}
675 All the other functions listed above need to be loaded by ``\verb!load(gf)!'':
677 \pagebreak
678 \begin{lstlisting}[escapechar=\#]
679 #\tr{(\%i2)}# load(gf)#\$#
680 #\tr{(\%i3)}# msqrt(64, 1789); ssqrt(64, 1789); gf_sqrt(64, 1789);
681 #\tr{(\%o3)}\bc\rb{$\tb{[\,1781,\,8\,]}$}\ec#
682 #\tr{(\%o4)}\bc\rb{$\tb{[\,8,\,1781\,]}$}\ec#
683 #\tr{(\%o5)}\bc\rb{$\tb{[\,1781,\,8\,]}$}\ec#
684 #\tr{(\%i6)}# mcbrt(64, 1789); scbrt(64, 1789); gf_cbrt(64, 1789);
685 #\tr{(\%o6)}\bc\rb{$\tb{[\,4,\,608,\,1177\,]}$}\ec#
686 #\tr{(\%o7)}\bc\rb{$\tb{[\,4,\,608,\,1177\,]}$}\ec#
687 #\tr{(\%o8)}\bc\rb{$\tb{[\,4,\,1177,\,608\,]}$}\ec#
688 #\tr{(\%i9)}# gf_factor(x^3-64, 1789);
689 #\tr{(\%o9)}\bc\rb{$\tb{(x + 612)\ (x + 1181)\ (x + 1785)}$}\ec#
690 #\tr{(\%i10)}# map(lambda([n], n - 1789), %);
691 #\tr{(\%o10)}\bc\rb{$\tb{(x - 1177)\ (x - 608)\ (x - 4)}$}\ec#
692 #\tr{(\%i11)}# qsplit(1789);
693 #\tr{(\%o11)}\bc\rb{$\tb{[\,5,\,42\,]}$}\ec#
694 #\tr{(\%i12)}# csplit(1789);
695 #\tr{(\%o12)}\bc\rb{$\tb{[\,12,\,35\,]}$}\ec#
696 \end{lstlisting}
698 \end{document}
700 %%% Local Variables:
701 %%% mode: latex
702 %%% TeX-master: "finite_fields"
703 %%% End: