| 1 |
typedef __UINT16_TYPE__ uint16_t; |
| 2 |
typedef __UINT32_TYPE__ uint32_t; |
| 3 |
|
| 4 |
// Generates Galois Field GF(2^15) = GF(2)[x] mod (p * GF(2)[x]) |
| 5 |
#define POLY 0xe125 // 1 + x^2 + x^5 + x^8 + x^13 + x^14 + x^15 |
| 6 |
#define DEGREE 15 |
| 7 |
|
| 8 |
typedef uint16_t poly; |
| 9 |
|
| 10 |
#define MASK(b) (((poly) 1) << (b)) |
| 11 |
|
| 12 |
#ifndef BIT |
| 13 |
#define BIT 0 |
| 14 |
#endif |
| 15 |
|
| 16 |
// Calculate a * b mod p. |
| 17 |
poly pprod (poly a, poly b) |
| 18 |
{ |
| 19 |
const poly mask = MASK (DEGREE); |
| 20 |
poly c = 0; |
| 21 |
|
| 22 |
while (b) |
| 23 |
{ |
| 24 |
// Is bit i of b set? |
| 25 |
if (b & (1u << BIT)) |
| 26 |
{ |
| 27 |
//__asm ("" : "+r" (c)); |
| 28 |
c ^= a; // yes, then c := c + a * x^i |
| 29 |
} |
| 30 |
a <<= 1; // a := a*x |
| 31 |
|
| 32 |
if (a & mask) // a := a mod p |
| 33 |
a ^= POLY; |
| 34 |
|
| 35 |
b >>= 1; |
| 36 |
} |
| 37 |
|
| 38 |
return c; |
| 39 |
} |
| 40 |
|
| 41 |
uint32_t pprod32 (uint32_t a, uint32_t b) |
| 42 |
{ |
| 43 |
const uint32_t mask = MASK (DEGREE); |
| 44 |
uint32_t c = 0; |
| 45 |
|
| 46 |
while (b) |
| 47 |
{ |
| 48 |
// Is bit i of b set? |
| 49 |
if (b & (1u << BIT)) |
| 50 |
{ |
| 51 |
//__asm ("" : "+r" (c)); |
| 52 |
c ^= a; // yes, then c := c + a * x^i |
| 53 |
} |
| 54 |
a <<= 1; // a := a*x |
| 55 |
|
| 56 |
if (a & mask) // a := a mod p |
| 57 |
a ^= POLY; |
| 58 |
|
| 59 |
b >>= 1; |
| 60 |
} |
| 61 |
|
| 62 |
return c; |
| 63 |
} |