My Project
Loading...
Searching...
No Matches
Functions | Variables
gengftables-conway.cc File Reference

generate addition tables used by Factory to calculate in GF(q). More...

#include "config.h"
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <stdlib.h>
#include "cf_assert.h"
#include "gf_tabutil.h"
#include "cf_algorithm.h"
#include "cf_iter.h"

Go to the source code of this file.

Functions

int gf_tab_numdigits62 (int q)
 
bool isIrreducible (const CanonicalForm &f)
 bool isIrreducible ( const CanonicalForm & f ) More...
 
int exponent (const CanonicalForm &f, int q)
 int exponent ( const CanonicalForm & f, int q ) More...
 
bool findGenRec (int d, int n, int q, const CanonicalForm &m, const Variable &x, CanonicalForm &result)
 bool findGenRec ( int d, int n, int q, const CanonicalForm & m, const Variable & x, CanonicalForm & result ) More...
 
CanonicalForm findGen (int d, int q)
 CanonicalForm findGen ( int d, int q ) More...
 
void printTable (int d, int q, CanonicalForm mipo)
 void printTable ( int d, int q, CanonicalForm mipo ) More...
 
static CanonicalForm findGenNew (int n, int q)
 The new function for getting the minimal polynomials. More...
 
int main ()
 

Variables

const int maxtable = 65536
 constants. More...
 
const int primes_len = 54
 primes, primes_len: used to step through possible extensions More...
 
STATIC_VAR unsigned short primes []
 primes, primes_len: used to step through possible extensions More...
 

Detailed Description

generate addition tables used by Factory to calculate in GF(q).

Note
This may take quite a while ...

Definition in file gengftables-conway.cc.

Function Documentation

◆ exponent()

int exponent ( const CanonicalForm f,
int  q 
)

int exponent ( const CanonicalForm & f, int q )

exponent() - return e > 0 such that x^e == 1 mod f.

q: upper limit for e (?)

Definition at line 93 of file gengftables-conway.cc.

94{
95 Variable x = f.mvar();
96 int e = 1;
98 while ( e <= q && ! prod.isOne() ) {
99 e++;
100 prod = ( prod * x ) % f;
101 }
102 return e;
103}
Variable x
Definition: cfModGcd.cc:4082
FILE * f
Definition: checklibs.c:9
factory's main class
Definition: canonicalform.h:86
factory's class for variables
Definition: variable.h:33
fq_nmod_poly_t prod
Definition: facHensel.cc:100

◆ findGen()

CanonicalForm findGen ( int  d,
int  q 
)

CanonicalForm findGen ( int d, int q )

findGen() - find and return a generator of GF(q).

d: degree of extension q: the q in GF(q)

Definition at line 171 of file gengftables-conway.cc.

172{
173 Variable x( 1 );
175 cerr << "testing p = " << getCharacteristic() << ", d = " << d << " ... ";
176 cerr.flush();
177 bool ok = findGenRec( d, d, q, 0, x, result );
178 cerr << endl;
179 if ( ! ok )
180 return 0;
181 else
182 return result;
183}
int FACTORY_PUBLIC getCharacteristic()
Definition: cf_char.cc:70
return result
Definition: facAbsBiFact.cc:75
bool findGenRec(int d, int n, int q, const CanonicalForm &m, const Variable &x, CanonicalForm &result)
bool findGenRec ( int d, int n, int q, const CanonicalForm & m, const Variable & x,...

◆ findGenNew()

static CanonicalForm findGenNew ( int  n,
int  q 
)
static

The new function for getting the minimal polynomials.

It uses the Conway polynomials. It reads the polynomials from a file. The file contains all polynomials with p^k <= 2^16 but currently only polynomials with p^k <= 2^14 are used.

Parameters
nn is the exponent
qparameter q is not used. It is added to respect the old version

Definition at line 290 of file gengftables-conway.cc.

293{
294 CanonicalForm conway = 0;
295 Variable x( 1 );
296 int p = getCharacteristic();
297 int ntmp,ptmp,pos1,pos2,ii;
298 string ns, ps;
299 string LineSe,coef,PC;
300 int flag=1;
301 ifstream in("./ConwayList.txt");
302 getline(in,LineSe); // For the first line
303
304 string err="END"; //to check if we are at the end of the file
305 while((flag) && (err != LineSe))
306 {
307 getline(in,LineSe); //for the line: allConwayPolynomials := [
308 if(LineSe == err){
309 break;
310 }
311 pos1 = LineSe.find( ",", 0 );
312 pos2 = LineSe.find( ",", pos1 + 1); // we check where are the "," to now p and n of this line
313 ps = LineSe.substr(0, pos1);
314 ns = LineSe.substr(pos1 + 1,pos2 - pos1);
315 ptmp = atoi(ps.c_str()); //we have the value of p and n of these line
316 ntmp = atoi(ns.c_str());
317
318 if((ntmp==n)&&(ptmp==p)){flag=0;} // we check if they are our p and n to stop the search
319
320 }
321
322 if (err==LineSe) // If the Conway Polynomial is not in the list, there is an error.
323 {
324 //cout << "Error: This Conway polinomial is not in the list" << endl;
325 return(0);
326 }
327
328 // Read the polynomial from the file
329 pos1 = pos2 + 1;
330 pos2 = LineSe.find(",", pos1 + 1);
331 conway = atoi(LineSe.substr(pos1, pos2 - pos1).c_str()); // value of the constant term in PC=Conway Polynomial
332 pos1 = pos2;
333 pos2 = LineSe.find(",", pos1 + 1);
334
335 for(ii = 2; ii <= n; ii++)
336 {
337 coef = LineSe.substr(pos1 + 1,pos2 - pos1 - 1); //Coefficient of the monomial of degree ii-1
338 if(coef != "0")
339 {
340 conway = conway + atoi(coef.c_str()) * power(x, ii - 1) ; //We add this monomial to the Conway Polynomial
341 }
342 pos1 = pos2;
343 pos2 = LineSe.find( ",", pos1+1);
344 }
345
346 pos2 = LineSe.find( ",END", pos1 + 1); // To obtain the last coefficient we search "END" instead of ","
347 coef = LineSe.substr(pos1 + 1,pos2 - pos1 - 1);
348 conway = conway + atoi(coef.c_str()) * power(x, ii - 1) ; //We add the last monomial to the Conway Polynomial
349
350 in.close();
351
352 return(conway);
353
354}
CanonicalForm power(const CanonicalForm &f, int n)
exponentiation
int p
Definition: cfModGcd.cc:4078

◆ findGenRec()

bool findGenRec ( int  d,
int  n,
int  q,
const CanonicalForm m,
const Variable x,
CanonicalForm result 
)

bool findGenRec ( int d, int n, int q, const CanonicalForm & m, const Variable & x, CanonicalForm & result )

findGenRec() - find a generator of GF(q).

Returns true iff result is a valid generator.

d: degree of extension q: the q in GF(q) (q == p^d) x: generator should be a poly in x n, m: used to step recursively through all polys in FF(p) Initially, n == d and m == 0. If 0 <= n <= d we are in the process of building m, if n < 0 we built m and may test whether it generates GF(q). result: generator found

i: used to step through GF(p) p: current characteristic

Definition at line 125 of file gengftables-conway.cc.

128{
129 int i, p = getCharacteristic();
130 if ( n < 0 ) {
131 cerr << "."; cerr.flush();
132 // check whether m is irreducible
133 if ( isIrreducible( m ) ) {
134 cerr << "*"; cerr.flush();
135 // check whether m generates multiplicative group
136 if ( exponent( m, q ) == q - 1 ) {
137 result = m;
138 return true;
139 }
140 else
141 return false;
142 }
143 else
144 return false;
145 }
146 // for each monomial x^0, ..., x^n, ..., x^d, try all possible coefficients
147 else if ( n == d || n == 0 ) {
148 // we want to have a leading coefficient and a constant term,
149 // so start with coefficient >= 1
150 for ( i = 1; i < p; i++ )
151 if ( findGenRec( d, n-1, q, m + i * power( x, n ), x, result ) )
152 return true;
153 }
154 else {
155 for ( i = 0; i < p; i++ )
156 if ( findGenRec( d, n-1, q, m + i * power( x, n ), x, result ) )
157 return true;
158 }
159 return false;
160}
int m
Definition: cfEzgcd.cc:128
int i
Definition: cfEzgcd.cc:132
bool isIrreducible(const CanonicalForm &f)
bool isIrreducible ( const CanonicalForm & f )
#define exponent

◆ gf_tab_numdigits62()

int gf_tab_numdigits62 ( int  q)

Definition at line 12 of file gf_tabutil.cc.

13{
14 if ( q < 62 )
15 return 1;
16 else if ( q < 62*62 )
17 return 2;
18 else
19 return 3;
20}

◆ isIrreducible()

bool isIrreducible ( const CanonicalForm f)

bool isIrreducible ( const CanonicalForm & f )

isIrreducible() - return true iff f is irreducible.

Definition at line 77 of file gengftables-conway.cc.

78{
79 CFFList F = factorize( f );
80 if (F.getFirst().factor().inCoeffDomain())
81 F.removeFirst();
82 return F.length() == 1 && F.getFirst().exp() == 1;
83}
CFFList FACTORY_PUBLIC factorize(const CanonicalForm &f, bool issqrfree=false)
factorization over or
Definition: cf_factor.cc:405
T getFirst() const
Definition: ftmpl_list.cc:279
void removeFirst()
Definition: ftmpl_list.cc:287
int length() const
Definition: ftmpl_list.cc:273

◆ main()

int main ( )

Definition at line 358 of file gengftables-conway.cc.

359{
360 int i, p, q, n;
361 for ( i = 0; i < primes_len; i++ ) {
362 p = primes[i];
363 q = p;
364 n = 1;
366 while ( q < maxtable ) {
367 CanonicalForm f = findGenNew( n, q );
368 ASSERT( f != 0, "no generator found" );
369 printTable( n, q, f );
370 n++; q *= p;
371 }
372 }
373}
void FACTORY_PUBLIC setCharacteristic(int c)
Definition: cf_char.cc:28
#define ASSERT(expression, message)
Definition: cf_assert.h:99
const int maxtable
constants.
void printTable(int d, int q, CanonicalForm mipo)
void printTable ( int d, int q, CanonicalForm mipo )
STATIC_VAR unsigned short primes[]
primes, primes_len: used to step through possible extensions
const int primes_len
primes, primes_len: used to step through possible extensions
static CanonicalForm findGenNew(int n, int q)
The new function for getting the minimal polynomials.

◆ printTable()

void printTable ( int  d,
int  q,
CanonicalForm  mipo 
)

void printTable ( int d, int q, CanonicalForm mipo )

printTable - print +1 table of field GF(q).

d: degree of extension q: the q in GF(q) mipo: the minimal polynomial of the extension

p: current characteristic

Definition at line 197 of file gengftables-conway.cc.

198{
199 int i, p = getCharacteristic();
200
201 // open file to write to
202 ostrstream fname;
203 fname << "gftables/" << q << '\0';
204 char * fn = fname.str();
205 ofstream outfile;
206 outfile.open( fn, ios::out );
207 STICKYASSERT1( outfile, "can not open GF(q) table %s for writing", fn );
208 delete fn;
209
210 cerr << "mipo = " << mipo << ": ";
211 cerr << "generating multiplicative group ... ";
212 cerr.flush();
213
215 Variable x( 1 );
216
217 // fill T with powers of x
218 T[0] = 1;
219 for ( i = 1; i < q; i++ )
220 T[i] = ( T[i-1] * x ) % mipo;
221
222 cerr << "generating addition table ... ";
223 cerr.flush();
224
225 // brute force method
226 int * table = new int[maxtable];
228
229 for ( i = 0; i < q; i++ ) {
230 f = T[i] + 1;
231 int j = 0;
232 while ( j < q && T[j] != f ) j++;
233 table[i] = j;
234 }
235
236 cerr << "writing table ... ";
237 cerr.flush();
238
239 outfile << "@@ factory GF(q) table @@" << endl;
240 outfile << p << " " << d << " " << mipo << "; ";
241
242 // print simple reprenstation of mipo
243 outfile << d;
244 CFIterator MiPo = mipo;
245 for ( i = d; MiPo.hasTerms(); i--, MiPo++ ) {
246 int exp;
247 for ( exp = MiPo.exp(); exp < i; i-- )
248 outfile << " 0";
249 outfile << " " << MiPo.coeff();
250 }
251 // since mipo is irreducible, it has a constant term,
252 // so i == 0 at this point
253 outfile << endl;
254
255 int m = gf_tab_numdigits62( q );
256 char * outstr = new char[30*m+1];
257 outstr[30*m] = '\0';
258 i = 1;
259 while ( i < q ) {
260 int k = 0;
261 char * sptr = outstr;
262 while ( i < q && k < 30 ) {
263 convert62( table[i], m, sptr );
264 sptr += m;
265 k++; i++;
266 }
267 while ( k < 30 ) {
268 convert62( 0, m, sptr );
269 sptr += m;
270 k++;
271 }
272 outfile << outstr << endl;
273 }
274 outfile.close();
275
276 delete [] outstr;
277 delete [] T;
278 delete [] table;
279
280 cerr << endl;
281}
int k
Definition: cfEzgcd.cc:99
#define STICKYASSERT1(expression, message, parameter1)
Definition: cf_assert.h:66
class to iterate through CanonicalForm's
Definition: cf_iter.h:44
CF_NO_INLINE int exp() const
get the current exponent
CF_NO_INLINE CanonicalForm coeff() const
get the current coefficient
CF_NO_INLINE int hasTerms() const
check if iterator has reached the end of CanonicalForm
CanonicalForm mipo
Definition: facAlgExt.cc:57
int j
Definition: facHensel.cc:110
int gf_tab_numdigits62(int q)
Definition: gf_tabutil.cc:12
void convert62(int i, int n, char *p)
Definition: gf_tabutil.cc:32
STATIC_VAR jList * T
Definition: janet.cc:30
gmp_float exp(const gmp_float &a)
Definition: mpr_complex.cc:357

Variable Documentation

◆ maxtable

const int maxtable = 65536

constants.

maxtable: maximal size of a gf_table

Definition at line 48 of file gengftables-conway.cc.

◆ primes

STATIC_VAR unsigned short primes[]
Initial value:
=
{
2, 3, 5, 7, 11, 13, 17, 19,
23, 29, 31, 37, 41, 43, 47, 53,
59, 61, 67, 71, 73, 79, 83, 89,
97, 101, 103, 107, 109, 113, 127, 131,
137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223,
227, 229, 233, 239, 241, 251
}

primes, primes_len: used to step through possible extensions

Definition at line 60 of file gengftables-conway.cc.

◆ primes_len

const int primes_len = 54

primes, primes_len: used to step through possible extensions

Definition at line 54 of file gengftables-conway.cc.