/* GF-Matrix lib for finite field data computation over matrices INTRO This library can be used to treat data bytes as invertible elements and to use this elements as coefficients of a matrix, so you can "multiply files" remember matrices are not a unique factorization domain, so you cannot recover data unless you create cholesky factors HOW IT WORKS? Remember that in a Galois field every matrix entry is a polynomial of degree 7 with coefficients in Z2 i.e M is in Z2[x] and is expresed as a byte For example 0xa3 = 1010 0011 := x^7 + x^5 + x + 1 Multiplication and addition is implemented in this library for a finite field of 256 elements (polynomials in Z2[x] modulo 0xe5) using a faster technique calculating all the elements of the group with a generator (0xff) and with another table that saves the logarith base 0xff of all the elements in their position , so Log_ff[x] = y means that 0xff ** y = x (in GF256) and Exp table is Exp[x] = y means 0xff ** x = y (in GF256) , the tables were generated with my algorithm located at http://math.co.ro/C/gf256.c If you dont want this GF thing, don't use the macro GALOIS in compilation time. But remember that using this Galois field gives to the data a much richer structure to be manipulated algebraically Here are 200 bytes of data in 2 matrices and its multiplication A: 85 79 35 d7 00 fe e0 37 66 40 b2 d4 ee c1 12 0b a3 7a 3c f8 ef 91 1a cd 65 69 f7 0f 79 dc 30 30 30 2b b3 a8 6f 6d 02 7e fe 87 ea e7 ea 0f b9 78 c2 6e 2f fa 7a 67 54 6b d9 25 8e 41 c9 7c 09 e9 6d a2 5e aa 40 e2 85 49 4b 9f 7c bc 8b ac ab 39 a6 d0 7e 50 16 f7 35 18 c9 d9 d2 74 7d 37 ce f8 3e 62 cc 0e B: 8a b9 c8 f7 54 4d 3c 97 5d 53 af 88 99 41 02 fb 34 0f 76 43 c3 6a 1f 3a 2b c4 74 7e f2 14 d5 35 15 e9 5c 55 29 b1 9c 37 59 c2 5f e6 85 0d 5b 6a 10 8f fd 8b 30 e8 54 46 45 22 0c f7 00 0e bc ea ee e1 5b 52 ba cc 07 ea 90 4a a3 33 9e ea dd 88 14 4b 94 98 0a 81 4a 63 4c 25 90 cd da 50 8f 8b 2c af 24 bb AxB: 6a 21 96 35 30 46 12 13 56 38 1d 81 80 67 6d 18 c6 df 83 d2 ef 14 84 1c d1 2b 9d 64 e5 7c 45 7e e3 a9 06 c6 02 72 54 b0 f7 8c ab 57 95 78 be 35 9c 13 bd b6 f0 3d e7 39 09 03 5a a6 2c 36 ec 1b d9 d6 c7 dc e5 32 6a 63 19 e3 b7 98 48 85 dd 77 37 c4 e7 40 5b f1 a0 01 26 65 92 03 82 a5 0b a4 aa dd a2 0c COMPILATION Compile for Galois field arithmetic GF2^8: gcc -O2 -fomit-frame-pointer -DDEBUG -DGALOIS thisfile.c -o matrixgf For normal 64 bit arithmetic gcc -O2 -fomit-frame-pointer -DDEBUG thisfile.c -o matrixgf An example of usage can be found on main() AUTHOR Eduardo Ruiz Duarte rduarte@ciencias.unam.mx */ #include #include #include #ifdef DEBUG #define dprintf(fmt, args...) printf("%s:%s:%d: "fmt, __FILE__, __FUNCTION__, __LINE__, args) #else #define dprintf(fmt, args...) #endif int64_t seed; typedef struct matrix { #ifdef GALOIS uint8_t **coef; #else int64_t **coef; #endif uint8_t r; uint8_t c; } matrix_t; /* Nice way to do Galois field computation , you can find my generator of this tables on http://math.co.ro/C/gf256.c Eduardo Ruiz Duarte (beck) rduarte@ciencias.unam.mx */ /* Generator of GF<256> */ uint8_t gen = 0xff; /* Exp and Log tables with generator 255 */ uint8_t Exp_ff_tab[256] = { /* 0xff^n */ 0x01, 0xff, 0x13, 0x73, 0x1e, 0xe4, 0xd9, 0xf5, 0x4f, 0xcd, 0x4d, 0x28, 0x6b, 0xce, 0x57, 0x1d, 0xfe, 0xec, 0x60, 0x6d, 0xfa, 0x3d, 0x2c, 0xba, 0x82, 0x80, 0x65, 0x43, 0xa5, 0x99, 0x4a, 0xe3, 0x12, 0x8c, 0x0d, 0x97, 0xc7, 0x11, 0x96, 0x38, 0x02, 0xe5, 0x26, 0xe6, 0x3c, 0xd3, 0xa9, 0xf1, 0x9e, 0x81, 0x9a, 0x50, 0xd6, 0x87, 0xae, 0x3a, 0xe7, 0xc3, 0xc0, 0xda, 0xef, 0x7a, 0x58, 0x6f, 0x1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f2, 0xc7, 0x36, 0x63, 0x66, 0x88, 0xbf, 0xc3, 0xda, 0x7b, 0xab, 0x5c, 0xe3, 0x79, 0x17, 0xea, 0xaa, 0x55, 0xbc, 0xf1, 0x3a, 0xf4, 0xa5, 0x39, 0xc4, 0x64, 0xfa, 0x24, 0xd1, 0xa7, 0x42, 0xaf, 0xbe, 0x09, 0x0d, 0xd4, 0xc2, 0x51, 0xc9, 0x2d, 0x9b, 0x60, 0x34, 0x53, 0xb9, 0x06, 0x3b, 0x9f, 0x9e, 0x47, 0x67, 0xf3, 0x77, 0xdd, 0xcb, 0x1f, 0x05, 0x29, 0x2b, 0x38, 0x87, 0x7f, 0xac, 0xe0, 0x11, 0xcc, 0xfb, 0x3c, 0x7c, 0x2f, 0xd8, 0x8d, 0x65, 0x07, 0xb6, 0xde, 0xb8, 0x57, 0x14, 0xa4, 0xf6, 0xb5, 0x10, 0x01 }; /* Multiplication in Galois Field of characteristic 2 of order 256 with 512 bytes of memory and minimal cpu * Same irreducible polynomial, but faster */ uint8_t gmul (uint8_t a, uint8_t b) { /* Easy , a=g^n b=g^m a*b = g^(n+m) */ return (a == 0 || b == 0) ? 0 : Exp_ff_tab[(Log_ff_tab[a] + Log_ff_tab[b]) % 0xff]; } uint8_t gdiv (uint8_t a, uint8_t b) { /* Easy , a=g^n b=g^m a/b = g^n/b^m = g^(n-m) */ return Exp_ff_tab[(0xff + Log_ff_tab[a] - Log_ff_tab[b]) % 0xff]; } uint8_t gadd (uint8_t a, uint8_t b) { /* normal way, a and b are polynomials over Z2 so addition is Xor */ return a ^ b; } /* Some matrix functions for memory allocation, freeing, multiplication, addition, printing and random matrix generator */ void matrix_clear (matrix_t * a) { int8_t i, j; for (i = 0; i < a->r; i++) for (j = 0; j < a->c; j++) a->coef[i][j] = 0; } matrix_t * matrix_alloc (uint8_t rows, uint8_t cols) { matrix_t *ptr; int8_t i; ptr = calloc (1, sizeof (struct matrix)); ptr->coef = calloc (rows, sizeof (void *)); for (i = 0; i < rows; i++) #ifdef GALOIS ptr->coef[i] = calloc (cols, sizeof (uint8_t)); #else ptr->coef[i] = calloc (cols, sizeof (int64_t)); #endif ptr->r = rows; ptr->c = cols; return ptr; } void matrix_free (matrix_t * ptr) { int8_t i; for (i = 0; i < ptr->r; i++) free (ptr->coef[i]); free (ptr->coef); free (ptr); } int8_t matrix_mul (matrix_t * a, matrix_t * b, matrix_t * c) { int8_t i, j, k; if (!(a->c == b->r)) return 1; matrix_clear (c); for (i = 0; i < a->c; i++) for (j = 0; j < b->r; j++) for (k = 0; k < a->r; k++) #ifdef GALOIS c->coef[i][j] ^= gmul (a->coef[i][k], b->coef[k][j]); #else c->coef[i][j] += a->coef[i][k] * b->coef[k][j]; #endif c->r = a->r; c->c = b->c; } int8_t matrix_add (matrix_t * a, matrix_t * b, matrix_t * c) { int8_t i, j; if (!((a->c == b->c) && (a->r == b->r))) return 1; matrix_clear (c); for (i = 0; i < a->c; i++) for (j = 0; j < b->r; j++) #ifdef GALOIS c->coef[i][j] = gadd (a->coef[i][j], b->coef[i][j]); #else c->coef[i][j] = a->coef[i][j] + b->coef[i][j]; #endif c->r = a->r; c->c = a->c; return 0; } void matrix_print (matrix_t * a) { int8_t i, j; for (i = 0; i < a->r; i++) { for (j = 0; j < a->c; j++) #ifdef GALOIS printf ("%02x%c", a->coef[i][j], j == a->c - 1 ? 0xa : 0x20); #else printf ("%lu%c", a->coef[i][j], j == a->c - 1 ? 0xa : '\t'); #endif } printf ("\n"); return; } matrix_t * #ifdef GALOIS matrix_random (int8_t rows, int8_t cols) #else matrix_random (int8_t rows, int8_t cols, int64_t coef_max) #endif { matrix_t *r; int8_t i, j; r = matrix_alloc (rows, cols); matrix_clear (r); srand (getpid () * getppid () + seed); for (i = 0; i < r->c; i++) for (j = 0; j < r->r; j++) { #ifdef GALOIS r->coef[i][j] = rand () % 0xff; dprintf ("(%d,%d) = %02x\n", i, j, r->coef[i][j]); #else r->coef[i][j] = rand () % coef_max; dprintf ("(%d,%d) = %lu\n", i, j, r->coef[i][j]); #endif } seed = rand (); return r; } int main (void) { matrix_t *a, *b, *c; #ifdef GALOIS a = matrix_random (10, 10); b = matrix_random (10, 10); #else a = matrix_random (10, 10, 65535); b = matrix_random (10, 10, 65535); #endif c = matrix_alloc (10, 10); matrix_print (a); matrix_print (b); matrix_mul (a, b, c); matrix_print (c); matrix_free (a); matrix_free (b); matrix_free (c); return 0; }