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 * Adds two numbers in gf_2^w
19 fprintf(stderr, "usage: gf_add a b w - does addition of a and b in GF(2^w)\n");
20 fprintf(stderr, " If w has an h on the end, treat a, b and the sum as hexadecimal (no 0x required)\n");
21 fprintf(stderr, "\n");
22 fprintf(stderr, " legal w are: 1-32, 64 and 128\n");
23 fprintf(stderr, " 128 is hex only (i.e. '128' will be an error - do '128h')\n");
25 if (s != NULL) fprintf(stderr, "%s", s);
29 int read_128(char *s, uint64_t *v)
38 if (sscanf(s + (l-16), "%llx", (long long unsigned int *) &(v[1])) == 0) return 0;
41 t = sscanf(s, "%llx", (long long unsigned int *) &(v[0]));
46 return sscanf(s, "%llx", (long long unsigned int *)&(v[1]));
51 void print_128(uint64_t *v)
54 printf("%llx", (long long unsigned int) v[0]);
55 printf("%016llx", (long long unsigned int) v[1]);
57 printf("%llx", (long long unsigned int) v[1]);
63 int main(int argc, char **argv)
66 uint32_t a, b, c, top;
67 uint64_t a64, b64, c64;
68 uint64_t a128[2], b128[2], c128[2];
71 if (argc != 4) usage(NULL);
72 if (sscanf(argv[3], "%d", &w) == 0) usage("Bad w\n");
74 if (w <= 0 || (w > 32 && w != 64 && w != 128)) usage("Bad w");
76 hex = (strchr(argv[3], 'h') != NULL);
78 if (!hex && w == 128) usage(NULL);
81 format = (hex) ? "%x" : "%u";
82 if (sscanf(argv[1], format, &a) == 0) usage("Bad a\n");
83 if (sscanf(argv[2], format, &b) == 0) usage("Bad b\n");
86 top = (w == 31) ? 0x80000000 : (1 << w);
87 if (w != 32 && a >= top) usage("a is too large\n");
88 if (w != 32 && b >= top) usage("b is too large\n");
96 format = (hex) ? "%llx" : "%llu";
97 if (sscanf(argv[1], format, &a64) == 0) usage("Bad a\n");
98 if (sscanf(argv[2], format, &b64) == 0) usage("Bad b\n");
104 } else if (w == 128) {
106 if (read_128(argv[1], a128) == 0) usage("Bad a\n");
107 if (read_128(argv[2], b128) == 0) usage("Bad b\n");
108 c128[0] = a128[0] ^ b128[0];
109 c128[1] = a128[1] ^ b128[1];