2 * GF-Complete: A Comprehensive Open Source Library for Galois Field Arithmetic
3 * James S. Plank, Ethan L. Miller, Kevin M. Greenan,
4 * Benjamin A. Arnold, John A. Burnum, Adam W. Disney, Allen C. McBride.
8 * Performs unit testing for gf arithmetic
13 #ifdef HAVE_POSIX_MEMALIGN
15 #define _XOPEN_SOURCE 600
27 #include "gf_complete.h"
29 #include "gf_method.h"
31 #include "gf_general.h"
33 #define REGION_SIZE (16384)
34 #define RMASK (0x00000000ffffffffLL)
35 #define LMASK (0xffffffff00000000LL)
39 fprintf(stderr, "Unit test failed.\n");
40 fprintf(stderr, "%s\n", s);
44 char *BM = "Bad Method: ";
48 fprintf(stderr, "usage: gf_unit w tests seed [method] - does unit testing in GF(2^w)\n");
49 fprintf(stderr, "\n");
50 fprintf(stderr, "Legal w are: 1 - 32, 64 and 128\n");
51 fprintf(stderr, " 128 is hex only (i.e. '128' will be an error - do '128h')\n");
52 fprintf(stderr, "\n");
53 fprintf(stderr, "Tests may be any combination of:\n");
54 fprintf(stderr, " A: All\n");
55 fprintf(stderr, " S: Single operations (multiplication/division)\n");
56 fprintf(stderr, " R: Region operations\n");
57 fprintf(stderr, " V: Verbose Output\n");
58 fprintf(stderr, "\n");
59 fprintf(stderr, "Use -1 for time(0) as a seed.\n");
60 fprintf(stderr, "\n");
62 fprintf(stderr, "%s", BM);
64 } else if (s != NULL) {
65 fprintf(stderr, "%s\n", s);
70 void SigHandler(int v)
72 fprintf(stderr, "Problem: SegFault!\n");
77 int main(int argc, char **argv)
79 signal(SIGSEGV, SigHandler);
81 int w, i, verbose, single, region, top;
82 int s_start, d_start, bytes, xor, alignment_test;
86 gf_general_t *a, *b, *c, *d;
87 uint8_t a8, b8, c8, *mult4 = NULL, *mult8 = NULL;
88 uint16_t a16, b16, c16, *log16 = NULL, *alog16 = NULL;
89 char as[50], bs[50], cs[50], ds[50];
91 char *ra, *rb, *rc, *rd, *target;
93 #ifndef HAVE_POSIX_MEMALIGN
94 char *malloc_ra, *malloc_rb, *malloc_rc, *malloc_rd;
98 if (argc < 4) usage(NULL);
100 if (sscanf(argv[1], "%d", &w) == 0){
104 if (sscanf(argv[3], "%ld", &t0) == 0) usage("Bad seed\n");
105 if (t0 == -1) t0 = time(0);
108 if (w > 32 && w != 64 && w != 128) usage("Bad w");
110 if (create_gf_from_argv(&gf, w, argc, argv, 4) == 0) {
115 for (i = 1; i < argc; i++) {
116 printf ("%s ", argv[i]);
118 printf("/ size (bytes): %d\n", gf_size(&gf));
120 for (i = 0; i < strlen(argv[2]); i++) {
121 if (strchr("ASRV", argv[2][i]) == NULL) usage("Bad test\n");
124 h = (gf_internal_t *) gf.scratch;
125 a = (gf_general_t *) malloc(sizeof(gf_general_t));
126 b = (gf_general_t *) malloc(sizeof(gf_general_t));
127 c = (gf_general_t *) malloc(sizeof(gf_general_t));
128 d = (gf_general_t *) malloc(sizeof(gf_general_t));
130 #if HAVE_POSIX_MEMALIGN
131 if (posix_memalign((void **) &ra, 16, sizeof(char)*REGION_SIZE))
133 if (posix_memalign((void **) &rb, 16, sizeof(char)*REGION_SIZE))
135 if (posix_memalign((void **) &rc, 16, sizeof(char)*REGION_SIZE))
137 if (posix_memalign((void **) &rd, 16, sizeof(char)*REGION_SIZE))
140 //15 bytes extra to make sure it's 16byte aligned
141 malloc_ra = (char *) malloc(sizeof(char)*REGION_SIZE+15);
142 malloc_rb = (char *) malloc(sizeof(char)*REGION_SIZE+15);
143 malloc_rc = (char *) malloc(sizeof(char)*REGION_SIZE+15);
144 malloc_rd = (char *) malloc(sizeof(char)*REGION_SIZE+15);
145 ra = (uint8_t *) (((uintptr_t) malloc_ra + 15) & ~((uintptr_t) 0xf));
146 rb = (uint8_t *) (((uintptr_t) malloc_rb + 15) & ~((uintptr_t) 0xf));
147 rc = (uint8_t *) (((uintptr_t) malloc_rc + 15) & ~((uintptr_t) 0xf));
148 rd = (uint8_t *) (((uintptr_t) malloc_rd + 15) & ~((uintptr_t) 0xf));
153 for (i = 0; i < w; i++) mask |= (1 << i);
156 verbose = (strchr(argv[2], 'V') != NULL);
157 single = (strchr(argv[2], 'S') != NULL || strchr(argv[2], 'A') != NULL);
158 region = (strchr(argv[2], 'R') != NULL || strchr(argv[2], 'A') != NULL);
160 if (!gf_init_hard(&gf_def, w, GF_MULT_DEFAULT, GF_REGION_DEFAULT, GF_DIVIDE_DEFAULT,
161 (h->mult_type != GF_MULT_COMPOSITE) ? h->prim_poly : 0, 0, 0, NULL, NULL))
162 problem("No default for this value of w");
165 mult4 = gf_w4_get_mult_table(&gf);
167 mult8 = gf_w8_get_mult_table(&gf);
168 } else if (w == 16) {
169 log16 = gf_w16_get_log_table(&gf);
170 alog16 = gf_w16_get_mult_alog_table(&gf);
173 if (verbose) printf("Seed: %ld\n", t0);
177 if (gf.multiply.w32 == NULL) problem("No multiplication operation defined.");
178 if (verbose) { printf("Testing single multiplications/divisions.\n"); fflush(stdout); }
180 top = (1 << w)*(1 << w);
184 for (i = 0; i < top; i++) {
186 a->w32 = i % (1 << w);
189 //Allen: the following conditions were being run 10 times each. That didn't seem like nearly enough to
190 //me for these special cases, so I converted to doing this mod stuff to easily make the number of times
191 //run both larger and proportional to the total size of the run.
196 gf_general_set_zero(a, w);
197 gf_general_set_random(b, w, 1);
200 gf_general_set_random(a, w, 1);
201 gf_general_set_zero(b, w);
204 gf_general_set_one(a, w);
205 gf_general_set_random(b, w, 1);
208 gf_general_set_random(a, w, 1);
209 gf_general_set_one(b, w);
212 gf_general_set_random(a, w, 1);
213 gf_general_set_random(b, w, 1);
217 //Allen: the following special cases for w=64 are based on the code below for w=128.
218 //These w=64 cases are based on Dr. Plank's suggestion because some of the methods for w=64
219 //involve splitting it in two. I think they're less likely to give errors than the 128-bit case
220 //though, because the 128 bit case is always split in two.
221 //As with w=128, I'm arbitrarily deciding to do this sort of thing with a quarter of the cases
225 case 0: if (!gf_general_is_one(a, w)) a->w64 &= RMASK; break;
226 case 1: if (!gf_general_is_one(a, w)) a->w64 &= LMASK; break;
227 case 2: if (!gf_general_is_one(a, w)) a->w64 &= RMASK; if (!gf_general_is_one(b, w)) b->w64 &= RMASK; break;
228 case 3: if (!gf_general_is_one(a, w)) a->w64 &= RMASK; if (!gf_general_is_one(b, w)) b->w64 &= LMASK; break;
229 case 4: if (!gf_general_is_one(a, w)) a->w64 &= LMASK; if (!gf_general_is_one(b, w)) b->w64 &= RMASK; break;
230 case 5: if (!gf_general_is_one(a, w)) a->w64 &= LMASK; if (!gf_general_is_one(b, w)) b->w64 &= LMASK; break;
231 case 6: if (!gf_general_is_one(b, w)) b->w64 &= RMASK; break;
232 case 7: if (!gf_general_is_one(b, w)) b->w64 &= LMASK; break;
236 //Allen: for w=128, we have important special cases where one half or the other of the number is all
237 //zeros. The probability of hitting such a number randomly is 1^-64, so if we don't force these cases
238 //we'll probably never hit them. This could be implemented more efficiently by changing the set-random
239 //function for w=128, but I think this is easier to follow.
240 //I'm arbitrarily deciding to do this sort of thing with a quarter of the cases
244 case 0: if (!gf_general_is_one(a, w)) a->w128[0] = 0; break;
245 case 1: if (!gf_general_is_one(a, w)) a->w128[1] = 0; break;
246 case 2: if (!gf_general_is_one(a, w)) a->w128[0] = 0; if (!gf_general_is_one(b, w)) b->w128[0] = 0; break;
247 case 3: if (!gf_general_is_one(a, w)) a->w128[0] = 0; if (!gf_general_is_one(b, w)) b->w128[1] = 0; break;
248 case 4: if (!gf_general_is_one(a, w)) a->w128[1] = 0; if (!gf_general_is_one(b, w)) b->w128[0] = 0; break;
249 case 5: if (!gf_general_is_one(a, w)) a->w128[1] = 0; if (!gf_general_is_one(b, w)) b->w128[1] = 0; break;
250 case 6: if (!gf_general_is_one(b, w)) b->w128[0] = 0; break;
251 case 7: if (!gf_general_is_one(b, w)) b->w128[1] = 0; break;
255 gf_general_multiply(&gf, a, b, c);
257 /* If w is 4, 8 or 16, then there are inline multiplication/division methods.
260 if (w == 4 && mult4 != NULL) {
263 c8 = GF_W4_INLINE_MULTDIV(mult4, a8, b8);
265 printf("Error in inline multiplication. %d * %d. Inline = %d. Default = %d.\n",
271 if (w == 8 && mult8 != NULL) {
274 c8 = GF_W8_INLINE_MULTDIV(mult8, a8, b8);
276 printf("Error in inline multiplication. %d * %d. Inline = %d. Default = %d.\n",
282 if (w == 16 && log16 != NULL) {
285 c16 = GF_W16_INLINE_MULT(log16, alog16, a16, b16);
287 printf("Error in inline multiplication. %d * %d. Inline = %d. Default = %d.\n",
288 a16, b16, c16, c->w32);
289 printf("%d %d\n", log16[a16], log16[b16]);
290 top = log16[a16] + log16[b16];
291 printf("%d %d\n", top, alog16[top]);
296 /* If this is not composite, then first test against the default: */
298 if (h->mult_type != GF_MULT_COMPOSITE) {
299 gf_general_multiply(&gf_def, a, b, d);
301 if (!gf_general_are_equal(c, d, w)) {
302 gf_general_val_to_s(a, w, as, 1);
303 gf_general_val_to_s(b, w, bs, 1);
304 gf_general_val_to_s(c, w, cs, 1);
305 gf_general_val_to_s(d, w, ds, 1);
306 printf("Error in single multiplication (all numbers in hex):\n\n");
307 printf(" gf.multiply(gf, %s, %s) = %s\n", as, bs, cs);
308 printf(" The default gf multiplier returned %s\n", ds);
313 /* Now, we also need to double-check by other means, in case the default is wanky,
314 and when we're performing composite operations. Start with 0 and 1, where we know
315 what the result should be. */
317 if (gf_general_is_zero(a, w) || gf_general_is_zero(b, w) ||
318 gf_general_is_one(a, w) || gf_general_is_one(b, w)) {
319 if (((gf_general_is_zero(a, w) || gf_general_is_zero(b, w)) && !gf_general_is_zero(c, w)) ||
320 (gf_general_is_one(a, w) && !gf_general_are_equal(b, c, w)) ||
321 (gf_general_is_one(b, w) && !gf_general_are_equal(a, c, w))) {
322 gf_general_val_to_s(a, w, as, 1);
323 gf_general_val_to_s(b, w, bs, 1);
324 gf_general_val_to_s(c, w, cs, 1);
325 printf("Error in single multiplication (all numbers in hex):\n\n");
326 printf(" gf.multiply(gf, %s, %s) = %s, which is clearly wrong.\n", as, bs, cs);
331 /* Dumb check to make sure that it's not returning numbers that are too big: */
333 if (w < 32 && (c->w32 & mask) != c->w32) {
334 gf_general_val_to_s(a, w, as, 1);
335 gf_general_val_to_s(b, w, bs, 1);
336 gf_general_val_to_s(c, w, cs, 1);
337 printf("Error in single multiplication (all numbers in hex):\n\n");
338 printf(" gf.multiply.w32(gf, %s, %s) = %s, which is too big.\n", as, bs, cs);
342 /* Finally, let's check to see that multiplication and division work together */
344 if (!gf_general_is_zero(a, w)) {
345 gf_general_divide(&gf, c, a, d);
346 if (!gf_general_are_equal(b, d, w)) {
347 gf_general_val_to_s(a, w, as, 1);
348 gf_general_val_to_s(b, w, bs, 1);
349 gf_general_val_to_s(c, w, cs, 1);
350 gf_general_val_to_s(d, w, ds, 1);
351 printf("Error in single multiplication/division (all numbers in hex):\n\n");
352 printf(" gf.multiply(gf, %s, %s) = %s, but gf.divide(gf, %s, %s) = %s\n", as, bs, cs, cs, as, ds);
361 if (verbose) { printf("Testing region multiplications\n"); fflush(stdout); }
362 for (i = 0; i < 1024; i++) {
363 //Allen: changing to a switch thing as with the single ops to make things proportional
367 gf_general_set_zero(a, w);
370 gf_general_set_one(a, w);
373 gf_general_set_two(a, w);
376 gf_general_set_random(a, w, 1);
378 MOA_Fill_Random_Region(ra, REGION_SIZE);
379 MOA_Fill_Random_Region(rb, REGION_SIZE);
382 if (align == 0) align = 1;
383 if (align > 16) align = 16;
385 /* JSP - Cauchy test. When w < 32 & it doesn't equal 4, 8 or 16, the default is
386 equal to GF_REGION_CAUCHY, even if GF_REGION_CAUCHY is not set. We are testing
387 three alignments here:
389 1. Anything goes -- no alignment guaranteed.
390 2. Perfect alignment. Here src and dest must be aligned wrt each other,
391 and bytes must be a multiple of 16*w.
392 3. Imperfect alignment. Here we'll have src and dest be aligned wrt each
393 other, but bytes is simply a multiple of w. That means some XOR's will
394 be aligned, and some won't.
397 if ((h->region_type & GF_REGION_CAUCHY) || (w < 32 && w != 4 && w != 8 && w != 16)) {
398 alignment_test = (i%3);
400 s_start = MOA_Random_W(5, 1);
401 if (alignment_test == 0) {
402 d_start = MOA_Random_W(5, 1);
407 bytes = (d_start > s_start) ? REGION_SIZE - d_start : REGION_SIZE - s_start;
408 bytes -= MOA_Random_W(5, 1);
409 if (alignment_test == 1) {
410 bytes -= (bytes % (w*16));
412 bytes -= (bytes % w);
417 /* JSP - Otherwise, we're testing a non-cauchy test, and alignment
418 must be more strict. We have to make sure that the regions are
419 aligned wrt each other on 16-byte pointers. */
422 s_start = MOA_Random_W(5, 1) * align;
424 bytes = REGION_SIZE - s_start - MOA_Random_W(5, 1);
425 bytes -= (bytes % align);
427 if (h->mult_type == GF_MULT_COMPOSITE && (h->region_type & GF_REGION_ALTMAP)) {
430 target = (i/64)%2 ? rb : ra;
434 memcpy(rc, ra, REGION_SIZE);
435 memcpy(rd, target, REGION_SIZE);
436 gf_general_do_region_multiply(&gf, a, ra+s_start, target+d_start, bytes, xor);
437 gf_general_do_region_check(&gf, a, rc+s_start, rd+d_start, target+d_start, bytes, xor);
445 #ifdef HAVE_POSIX_MEMALIGN