[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[a472b9] | 2 | /* $Id: gfops.cc,v 1.5 1998-06-12 14:33:41 schmidt Exp $ */ |
---|
[2dd068] | 3 | |
---|
[bd7a8b] | 4 | #include <config.h> |
---|
| 5 | |
---|
| 6 | #include <stdio.h> |
---|
| 7 | #include <string.h> |
---|
[2dd068] | 8 | |
---|
| 9 | #include "assert.h" |
---|
[bd7a8b] | 10 | |
---|
[2dd068] | 11 | #include "cf_defs.h" |
---|
| 12 | #include "gf_tabutil.h" |
---|
| 13 | #include "cf_util.h" |
---|
| 14 | #include "canonicalform.h" |
---|
[bd7a8b] | 15 | #include "variable.h" |
---|
| 16 | #ifdef SINGULAR |
---|
| 17 | #include "singext.h" |
---|
| 18 | #endif |
---|
[a472b9] | 19 | #include "gfops.h" |
---|
[2dd068] | 20 | |
---|
| 21 | |
---|
[bd7a8b] | 22 | const int gf_maxtable = 32767; |
---|
| 23 | const int gf_maxbuffer = 200; |
---|
[2dd068] | 24 | |
---|
| 25 | const int gf_primes_len = 42; |
---|
| 26 | static unsigned short gf_primes [] = |
---|
| 27 | { |
---|
| 28 | 2, 3, 5, 7, 11, 13, 17, 19, |
---|
| 29 | 23, 29, 31, 37, 41, 43, 47, 53, |
---|
| 30 | 59, 61, 67, 71, 73, 79, 83, 89, |
---|
| 31 | 97, 101, 103, 107, 109, 113, 127, 131, |
---|
| 32 | 137, 139, 149, 151, 157, 163, 167, 173, |
---|
| 33 | 179, 181 |
---|
| 34 | }; |
---|
| 35 | |
---|
[bd7a8b] | 36 | int gf_q = 0; |
---|
| 37 | int gf_p = 0; |
---|
| 38 | int gf_n = 0; |
---|
| 39 | int gf_q1 = 0; |
---|
| 40 | int gf_m1 = 0; |
---|
| 41 | char gf_name = 'Z'; |
---|
| 42 | |
---|
| 43 | unsigned short * gf_table = 0; |
---|
| 44 | |
---|
[2dd068] | 45 | CanonicalForm gf_mipo = 0; |
---|
| 46 | |
---|
[bd7a8b] | 47 | static CanonicalForm |
---|
| 48 | intVec2CF ( int degree, int * coeffs, int level ) |
---|
| 49 | { |
---|
| 50 | int i; |
---|
| 51 | CanonicalForm result; |
---|
| 52 | for ( i = 0; i <= degree; i++ ) { |
---|
| 53 | result += CanonicalForm( coeffs[ i ] ) * power( Variable( level ), degree - i ); |
---|
| 54 | } |
---|
| 55 | return result; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | static void |
---|
| 59 | gf_get_table ( int p, int n ) |
---|
[2dd068] | 60 | { |
---|
[bd7a8b] | 61 | char buffer[gf_maxbuffer]; |
---|
| 62 | int q = ipower( p, n ); |
---|
[2dd068] | 63 | if ( gf_table == 0 ) |
---|
[bd7a8b] | 64 | gf_table = new unsigned short[gf_maxtable]; |
---|
| 65 | |
---|
| 66 | // do not read the table a second time |
---|
| 67 | if ( gf_q == q ) { |
---|
| 68 | return; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | #ifdef SINGULAR |
---|
| 72 | // just copy the table if Singular already read it |
---|
| 73 | if ( q == nfCharQ ) { |
---|
| 74 | gf_p = p; gf_n = n; |
---|
| 75 | gf_q = q; gf_q1 = q - 1; |
---|
| 76 | gf_m1 = nfM1; |
---|
| 77 | gf_mipo = intVec2CF( nfMinPoly[0], nfMinPoly + 1, 1 ); |
---|
| 78 | (void)memcpy( gf_table, nfPlus1Table, gf_q * sizeof( unsigned short ) ); |
---|
| 79 | gf_table[gf_q] = 0; |
---|
| 80 | return; |
---|
| 81 | } |
---|
| 82 | #endif |
---|
| 83 | |
---|
| 84 | // try to open file |
---|
| 85 | #ifndef SINGULAR |
---|
| 86 | sprintf( buffer, GFTABLEDIR "/gftable.%d.%d", p, n ); |
---|
[e9be05] | 87 | FILE * inputfile = fopen( buffer, "r" ); |
---|
[bd7a8b] | 88 | #else |
---|
[e9be05] | 89 | sprintf( buffer, "gftables/%d", q ); |
---|
| 90 | FILE * inputfile = feFopen( buffer, "r" ); |
---|
[bd7a8b] | 91 | #endif |
---|
[2dd068] | 92 | STICKYASSERT( inputfile, "can not open GF(q) table" ); |
---|
[bd7a8b] | 93 | |
---|
| 94 | // read ID |
---|
[2dd068] | 95 | char * bufptr; |
---|
[bd7a8b] | 96 | char * success; |
---|
| 97 | success = fgets( buffer, gf_maxbuffer, inputfile ); |
---|
| 98 | STICKYASSERT( success, "illegal table (reading ID)" ); |
---|
| 99 | STICKYASSERT( strcmp( buffer, "@@ factory GF(q) table @@\n" ) == 0, "illegal table" ); |
---|
| 100 | // read p and n from file |
---|
| 101 | int pFile, nFile; |
---|
| 102 | success = fgets( buffer, gf_maxbuffer, inputfile ); |
---|
| 103 | STICKYASSERT( success, "illegal table (reading p and n)" ); |
---|
| 104 | sscanf( buffer, "%d %d", &pFile, &nFile ); |
---|
| 105 | STICKYASSERT( p == pFile && n == nFile, "illegal table" ); |
---|
| 106 | // skip (sic!) factory-representation of mipo |
---|
| 107 | // and terminating "; " |
---|
[22743e] | 108 | bufptr = (char *)strchr( buffer, ';' ) + 2; |
---|
[bd7a8b] | 109 | // read simple representation of mipo |
---|
| 110 | int i, degree; |
---|
| 111 | sscanf( bufptr, "%d", °ree ); |
---|
[22743e] | 112 | bufptr = (char *)strchr( bufptr, ' ' ) + 1; |
---|
[bd7a8b] | 113 | int * mipo = new int[degree + 1]; |
---|
| 114 | for ( i = 0; i <= degree; i++ ) { |
---|
| 115 | sscanf( bufptr, "%d", mipo + i ); |
---|
[22743e] | 116 | bufptr = (char *)strchr( bufptr, ' ' ) + 1; |
---|
[bd7a8b] | 117 | } |
---|
| 118 | |
---|
| 119 | gf_p = p; gf_n = n; |
---|
| 120 | gf_q = q; gf_q1 = q-1; |
---|
| 121 | gf_mipo = intVec2CF( degree, mipo, 1 ); |
---|
| 122 | delete [] mipo; |
---|
| 123 | |
---|
| 124 | // now for the table |
---|
| 125 | int k, digs = gf_tab_numdigits62( gf_q ); |
---|
[2dd068] | 126 | i = 1; |
---|
| 127 | while ( i < gf_q ) { |
---|
[bd7a8b] | 128 | success = fgets( buffer, gf_maxbuffer, inputfile ); |
---|
| 129 | STICKYASSERT( strlen( buffer ) - 1 == (size_t)digs * 30, "illegal table" ); |
---|
[2dd068] | 130 | bufptr = buffer; |
---|
| 131 | k = 0; |
---|
| 132 | while ( i < gf_q && k < 30 ) { |
---|
| 133 | gf_table[i] = convertback62( bufptr, digs ); |
---|
| 134 | bufptr += digs; |
---|
| 135 | if ( gf_table[i] == gf_q ) |
---|
| 136 | if ( i == gf_q1 ) |
---|
| 137 | gf_m1 = 0; |
---|
| 138 | else |
---|
| 139 | gf_m1 = i; |
---|
| 140 | i++; k++; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | gf_table[0] = gf_table[gf_q1]; |
---|
[bd7a8b] | 144 | gf_table[gf_q] = 0; |
---|
| 145 | |
---|
| 146 | (void)fclose( inputfile ); |
---|
[2dd068] | 147 | } |
---|
| 148 | |
---|
[bd7a8b] | 149 | static bool |
---|
| 150 | gf_valid_combination ( int p, int n ) |
---|
[2dd068] | 151 | { |
---|
| 152 | int i = 0; |
---|
| 153 | while ( i < gf_primes_len && gf_primes[i] != p ) i++; |
---|
| 154 | if ( i == gf_primes_len ) |
---|
| 155 | return false; |
---|
| 156 | else { |
---|
| 157 | i = n; |
---|
| 158 | int a = 1; |
---|
[bd7a8b] | 159 | while ( a < gf_maxtable && i > 0 ) { |
---|
[2dd068] | 160 | a *= p; |
---|
| 161 | i--; |
---|
| 162 | } |
---|
[bd7a8b] | 163 | if ( i > 0 || a > gf_maxtable ) |
---|
[2dd068] | 164 | return false; |
---|
| 165 | else |
---|
| 166 | return true; |
---|
| 167 | } |
---|
| 168 | } |
---|
[bd7a8b] | 169 | |
---|
| 170 | void |
---|
| 171 | gf_setcharacteristic ( int p, int n, char name ) |
---|
[2dd068] | 172 | { |
---|
| 173 | ASSERT( gf_valid_combination( p, n ), "illegal immediate GF(q)" ); |
---|
| 174 | gf_name = name; |
---|
| 175 | gf_get_table( p, n ); |
---|
| 176 | } |
---|
| 177 | |
---|
[bd7a8b] | 178 | int |
---|
| 179 | gf_gf2ff ( int a ) |
---|
[2dd068] | 180 | { |
---|
| 181 | if ( gf_iszero( a ) ) |
---|
| 182 | return 0; |
---|
| 183 | else { |
---|
[bd7a8b] | 184 | // starting from z^0=1, step through the table |
---|
| 185 | // counting the steps until we hit z^a or z^0 |
---|
| 186 | // again. since we are working in char(p), the |
---|
| 187 | // latter is guaranteed to be fulfilled. |
---|
[2dd068] | 188 | int i = 0, ff = 1; |
---|
| 189 | do { |
---|
| 190 | if ( i == a ) |
---|
| 191 | return ff; |
---|
| 192 | ff++; |
---|
| 193 | i = gf_table[i]; |
---|
| 194 | } while ( i != 0 ); |
---|
| 195 | return -1; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
[bd7a8b] | 199 | bool |
---|
| 200 | gf_isff ( int a ) |
---|
[2dd068] | 201 | { |
---|
| 202 | if ( gf_iszero( a ) ) |
---|
| 203 | return true; |
---|
| 204 | else { |
---|
[bd7a8b] | 205 | // z^a in GF(p) iff (z^a)^p-1=1 |
---|
| 206 | return gf_isone( gf_power( a, gf_p - 1 ) ); |
---|
[2dd068] | 207 | } |
---|
| 208 | } |
---|