Skip to content

Instantly share code, notes, and snippets.

@kanav99
Created March 15, 2022 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanav99/42d7890a7d0bec4120c769417d34d8e5 to your computer and use it in GitHub Desktop.
Save kanav99/42d7890a7d0bec4120c769417d34d8e5 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
uint8_t gf256_add(uint8_t x, uint8_t y) {
return x ^ y;
}
uint8_t gf256_sub(uint8_t x, uint8_t y) {
return x ^ y;
}
uint8_t gf256_mul(uint8_t x, uint8_t y) {
uint8_t r = 0;
for(int i = 0; i < 8; ++i) {
r ^= (x * (y & 1));
x = (x << 1) ^ ((x >> 7) * 0x1b);
y >>= 1;
}
return r;
}
uint8_t gf256_div(uint8_t x, uint8_t y) {
assert(y != 0);
uint8_t r = 0;
uint8_t res;
do {
if (gf256_mul(r, y) == x) {
res = r;
}
r++;
} while(r);
return res;
}
void lagrange_interpolation(int M, uint8_t *x, uint8_t *y, uint8_t out[32])
{
memset(out, 0, 32);
for (int i = 0; i < M; ++i) {
uint8_t l = 1;
for (int j = 0; j < M; ++j) {
if (i != j) {
l = gf256_div(gf256_mul(l, x[j]), gf256_sub(x[i], x[j]));
}
}
for(int j = 0; j < 32; ++j) {
out[j] = gf256_add(out[j], gf256_mul(l, y[i * 32 + j]));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment