You are on page 1of 1

#include <stdio.

h>
#include <stdlib.h>
#include <stdint.h>

// Define the modulus polynomial (x^4 + x + 1) in GF(2^4)


#define MODULUS 0x13

// Function to compute the inverse of a polynomial in GF(2^4)


uint8_t inverse(uint8_t a)
{
uint8_t b = MODULUS, x = 0, y = 1, u = 1, v = 0;
while (a != 0) {
uint8_t q = b / a;
uint8_t r = b % a;
b = a;
a = r;
uint8_t tmp = x ^ (u * q);
x = u;
u = tmp;
tmp = y ^ (v * q);
y = v;
v = tmp;
}
return x;
}

int main()
{
// Define the polynomial x^2 + 1
uint8_t a = 0x05; // equivalent to x^2 + 1 in binary

// Compute the inverse of x^2 + 1 modulo x^4 + x + 1 in GF(2^4)


uint8_t inv_a = inverse(a);

// Print the result in hexadecimal format


printf("Inverse of x^2 + 1 mod x^4 + x + 1 is %02X in GF(2^4)\n", inv_a);

return 0;
}

// The result is
// Inverse of x^2 + 1 mod x^4 + x + 1 is 0B in GF(2^4)
// 0B is the hexadecimal representaion of x^3 + x + 1

You might also like