File: gf_poly.c

package info (click to toggle)
gf-complete 1.0.2%2B2017.04.10.git.ea75cdf-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,272 kB
  • sloc: ansic: 16,714; sh: 573; makefile: 110
file content (275 lines) | stat: -rw-r--r-- 8,741 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * GF-Complete: A Comprehensive Open Source Library for Galois Field Arithmetic
 * James S. Plank, Ethan L. Miller, Kevin M. Greenan,
 * Benjamin A. Arnold, John A. Burnum, Adam W. Disney, Allen C. McBride.
 *
 * gf_poly.c - program to help find irreducible polynomials in composite fields,
 * using the Ben-Or algorithm.  
 * 
 * (This one was written by Jim) 
 * 
 * Please see the following paper for a description of the Ben-Or algorithm:
 * 
 * author    S. Gao and D. Panario
 * title     Tests and Constructions of Irreducible Polynomials over Finite Fields
 * booktitle Foundations of Computational Mathematics
 * year      1997
 * publisher Springer Verlag
 * pages     346-361
 * 
 * The basic technique is this.  You have a polynomial f(x) whose coefficients are
 * in a base field GF(2^w).  The polynomial is of degree n.  You need to do the 
 * following for all i from 1 to n/2:
 * 
 * Construct x^(2^w)^i modulo f.  That will be a polynomial of maximum degree n-1
 * with coefficients in GF(2^w).  You construct that polynomial by starting with x
 * and doubling it w times, each time taking the result modulo f.  Then you 
 * multiply that by itself i times, again each time taking the result modulo f.
 * 
 * When you're done, you need to "subtract" x -- since addition = subtraction = 
 * XOR, that means XOR x.  
 * 
 * Now, find the GCD of that last polynomial and f, using Euclid's algorithm.  If
 * the GCD is not one, then f is reducible.  If it is not reducible for each of
 * those i, then it is irreducible.
 * 
 * In this code, I am using a gf_general_t to represent elements of GF(2^w).  This
 * is so that I can use base fields that are GF(2^64) or GF(2^128). 
 * 
 * I have two main procedures.  The first is x_to_q_to_i_minus_x, which calculates
 * x^(2^w)^i - x, putting the result into a gf_general_t * called retval.
 * 
 * The second is gcd_one, which takes a polynomial of degree n and a second one
 * of degree n-1, and uses Euclid's algorithm to decide if their GCD == 1.
 * 
 * These can be made faster (e.g. calculate x^(2^w) once and store it).
 */

#include "gf_complete.h"
#include "gf_method.h"
#include "gf_general.h"
#include "gf_int.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

char *BM = "Bad Method: ";

void usage(char *s)
{
  fprintf(stderr, "usage: gf_poly w(base-field) method power:coef [ power:coef .. ]\n");
  fprintf(stderr, "\n");
  fprintf(stderr, "       use - for the default method.\n");
  fprintf(stderr, "       use 0x in front of the coefficient if it's in hex\n");
  fprintf(stderr, "       \n");
  fprintf(stderr, "       For example, to test whether x^2 + 2x + 1 is irreducible\n");
  fprintf(stderr, "       in GF(2^16), the call is:\n");
  fprintf(stderr, "       \n");
  fprintf(stderr, "       gf_poly 16 - 2:1 1:2 0:1\n");
  fprintf(stderr, "       \n");
  fprintf(stderr, "       See the user's manual for more information.\n");
  if (s != NULL) {
    fprintf(stderr, "\n");
    if (s == BM) {
      fprintf(stderr, "%s", s);
      gf_error();
    } else {
      fprintf(stderr, "%s\n", s);
    }
  }
  exit(1);
}

int gcd_one(gf_t *gf, int w, int n, gf_general_t *poly, gf_general_t *prod)
{
  gf_general_t *a, *b, zero, factor, p;
  int i, j, da, db;

  gf_general_set_zero(&zero, w);

  a = (gf_general_t *) malloc(sizeof(gf_general_t) * n+1);
  b = (gf_general_t *) malloc(sizeof(gf_general_t) * n);
  for (i = 0; i <= n; i++) gf_general_add(gf, &zero, poly+i, a+i);
  for (i = 0; i < n; i++) gf_general_add(gf, &zero, prod+i, b+i);

  da = n;
  while (1) {
    for (db = n-1; db >= 0 && gf_general_is_zero(b+db, w); db--) ;
    if (db < 0) return 0;
    if (db == 0) return 1;
    for (j = da; j >= db; j--) {
      if (!gf_general_is_zero(a+j, w)) {
        gf_general_divide(gf, a+j, b+db, &factor);
        for (i = 0; i <= db; i++) {
          gf_general_multiply(gf, b+i, &factor, &p); 
          gf_general_add(gf, &p, a+(i+j-db), a+(i+j-db));
        }
      }
    }
    for (i = 0; i < n; i++) {
      gf_general_add(gf, a+i, &zero, &p);
      gf_general_add(gf, b+i, &zero, a+i);
      gf_general_add(gf, &p, &zero, b+i);
    }
  }

}

void x_to_q_to_i_minus_x(gf_t *gf, int w, int n, gf_general_t *poly, int logq, int i, gf_general_t *retval)
{
  gf_general_t x;
  gf_general_t *x_to_q;
  gf_general_t *product;
  gf_general_t p, zero, factor;
  int j, k, lq;

  gf_general_set_zero(&zero, w);
  product = (gf_general_t *) malloc(sizeof(gf_general_t) * n*2);
  x_to_q = (gf_general_t *) malloc(sizeof(gf_general_t) * n);
  for (j = 0; j < n; j++) gf_general_set_zero(x_to_q+j, w);
  gf_general_set_one(x_to_q+1, w);

  for (lq = 0; lq < logq; lq++) {
    for (j = 0; j < n*2; j++) gf_general_set_zero(product+j, w);
    for (j = 0; j < n; j++) {
      for (k = 0; k < n; k++) {
        gf_general_multiply(gf, x_to_q+j, x_to_q+k, &p);
        gf_general_add(gf, product+(j+k), &p, product+(j+k));
      }
    }
    for (j = n*2-1; j >= n; j--) {
      if (!gf_general_is_zero(product+j, w)) {
        gf_general_add(gf, product+j, &zero, &factor);
        for (k = 0; k <= n; k++) {
          gf_general_multiply(gf, poly+k, &factor, &p);
          gf_general_add(gf, product+(j-n+k), &p, product+(j-n+k));
        }
      }
    }
    for (j = 0; j < n; j++) gf_general_add(gf, product+j, &zero, x_to_q+j);
  }
  for (j = 0; j < n; j++) gf_general_set_zero(retval+j, w);
  gf_general_set_one(retval, w);

  while (i > 0) {
    for (j = 0; j < n*2; j++) gf_general_set_zero(product+j, w);
    for (j = 0; j < n; j++) {
      for (k = 0; k < n; k++) {
        gf_general_multiply(gf, x_to_q+j, retval+k, &p);
        gf_general_add(gf, product+(j+k), &p, product+(j+k));
      }
    }
    for (j = n*2-1; j >= n; j--) {
      if (!gf_general_is_zero(product+j, w)) {
        gf_general_add(gf, product+j, &zero, &factor);
        for (k = 0; k <= n; k++) {
          gf_general_multiply(gf, poly+k, &factor, &p);
          gf_general_add(gf, product+(j-n+k), &p, product+(j-n+k));
        }
      }
    }
    for (j = 0; j < n; j++) gf_general_add(gf, product+j, &zero, retval+j);
    i--;
  }

  gf_general_set_one(&x, w);
  gf_general_add(gf, &x, retval+1, retval+1);

  free(product);
  free(x_to_q);
}

int main(int argc, char **argv)
{
  int w, i, power, n, ap, success;
  gf_t gf;
  gf_general_t *poly, *prod;
  char *string, *ptr;
  char buf[100];

  if (argc < 4) usage(NULL);

  if (sscanf(argv[1], "%d", &w) != 1 || w <= 0) usage("Bad w.");
  ap = create_gf_from_argv(&gf, w, argc, argv, 2);

  if (ap == 0) usage(BM);

  if (ap == argc) usage("No powers/coefficients given.");

  n = -1;
  for (i = ap; i < argc; i++) {
    if (strchr(argv[i], ':') == NULL || sscanf(argv[i], "%d:", &power) != 1) {
      string = (char *) malloc(sizeof(char)*(strlen(argv[i]+100)));
      sprintf(string, "Argument '%s' not in proper format of power:coefficient\n", argv[i]);
      usage(string);
    }
    if (power < 0) {
      usage("Can't have negative powers\n");
    } else {
      n = power;
    }
  }
  // in case the for-loop header fails
  assert (n >= 0);

  poly = (gf_general_t *) malloc(sizeof(gf_general_t)*(n+1));
  for (i = 0; i <= n; i++) gf_general_set_zero(poly+i, w);
  prod = (gf_general_t *) malloc(sizeof(gf_general_t)*n);

  for (i = ap; i < argc; i++) {
    sscanf(argv[i], "%d:", &power);
    ptr = strchr(argv[i], ':');
    ptr++;
    if (strncmp(ptr, "0x", 2) == 0) {
      success = gf_general_s_to_val(poly+power, w, ptr+2, 1);
    } else {
      success = gf_general_s_to_val(poly+power, w, ptr, 0);
    }
    if (success == 0) {
      string = (char *) malloc(sizeof(char)*(strlen(argv[i]+100)));
      sprintf(string, "Argument '%s' not in proper format of power:coefficient\n", argv[i]);
      usage(string);
    }
  }

  printf("Poly:");
  for (power = n; power >= 0; power--) {
    if (!gf_general_is_zero(poly+power, w)) {
      printf("%s", (power == n) ? " " : " + ");
      if (!gf_general_is_one(poly+power, w)) {
        gf_general_val_to_s(poly+power, w, buf, 1);
        if (n > 0) {
          printf("(0x%s)", buf);
        } else {
          printf("0x%s", buf);
        }
      }
      if (power == 0) {
        if (gf_general_is_one(poly+power, w)) printf("1");
      } else if (power == 1) {
        printf("x");
      } else {
        printf("x^%d", power);
      }
    }
  }
  printf("\n");

  if (!gf_general_is_one(poly+n, w)) {
    printf("\n");
    printf("Can't do Ben-Or, because the polynomial is not monic.\n");
    exit(0);
  }

  for (i = 1; i <= n/2; i++) {
    x_to_q_to_i_minus_x(&gf, w, n, poly, w, i, prod); 
    if (!gcd_one(&gf, w, n, poly, prod)) {
      printf("Reducible.\n");
      exit(0);
    }
  }
  
  printf("Irreducible.\n");
  exit(0);
}