From 636dfc412f33b05ecfdc375a146cf9cd773b38f2 Mon Sep 17 00:00:00 2001 From: cfif Date: Fri, 24 Jan 2025 13:22:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=BE=D1=81=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BD=D0=BE=D0=B2=D1=83=D1=8E=20=D0=BE=D1=80?= =?UTF-8?q?=D0=B3=D0=B0=D0=BD=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E=20GONEC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Galois256.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++ Galois256.h | 35 ++++++++++++++ modular.json | 10 ++++ 3 files changed, 173 insertions(+) create mode 100644 Galois256.c create mode 100644 Galois256.h create mode 100644 modular.json diff --git a/Galois256.c b/Galois256.c new file mode 100644 index 0000000..bf3a268 --- /dev/null +++ b/Galois256.c @@ -0,0 +1,128 @@ +// +// Created by zemon on 22.07.2022. +// +#include "Galois256.h" +#include +#include "stdint.h" + + +int32_t inline iGalois256Add(tGalois256 *env, int one, int two) { + return one ^ two; +} + +int32_t inline iGalois256Sub(tGalois256 *env, int one, int two) { + return one ^ two; +} + + +int32_t iGalois256PowValue(tGalois256 *env, int degree) { + int deg_in_bounds; + + if (degree < 0) { + deg_in_bounds = env->field_elements_limit - ((degree * -1) % env->field_elements_limit); + } else { + deg_in_bounds = degree % env->field_elements_limit; + } + + + return env->pow_table[deg_in_bounds]; +} + + +int32_t iGalois256LogValue(tGalois256 *env, int degree) { + + if ((degree <= 0) || (degree > env->field_elements_limit)) { +// printf("ERROR! Log out of bounds deg:%i in field len %i", degree, env->field_elements_limit); + return -1; + } + + return env->log_table[degree]; +} + + +int32_t iGalois256Mul(tGalois256 *env, int one, int two) { + if (0 == one || 0 == two) { + return 0; + } + return iGalois256PowValue(env, iGalois256LogValue(env, one) + iGalois256LogValue(env, two)); +} + + +int32_t iGalois256Div(tGalois256 *env, int dividable, int divider) { + + if (divider == 0) { +// printf("Division by zero %i/%i", dividable, divider); + return -1; + } + + if (dividable == 0) { + return 0; + } + return iGalois256PowValue( + env, + iGalois256LogValue(env, dividable) - iGalois256LogValue(env, divider) + ); +} + + +int32_t iGalois256Pow(tGalois256 *env, int base, int degree) { + + if (degree == 0) { + return 1; + } + + if (base == 0) { + return 0; + } + + int base_log = iGalois256LogValue(env, base); + + return iGalois256PowValue(env, degree * base_log); +} + + +void iGalois256GenTables(tGalois256 *env) { + + memset(env->log_table, 0, (env->field_elements_limit + 1) * sizeof(int32_t)); + memset(env->pow_table, 0, (env->field_elements_limit + 1) * sizeof(int32_t)); + + int mask = 1; + env->pow_table[env->base_degree] = 0; + + for (int i = 0; i < env->base_degree; i++) { + env->pow_table[i] = mask; + env->log_table[env->pow_table[i]] = i; + + if (env->irreducible_polynomial[i] != 0) { + env->pow_table[env->base_degree] ^= mask; + } + + mask <<= 1; + } + + + env->log_table[env->pow_table[env->base_degree]] = env->base_degree; + mask >>= 1; + + for (int i = env->base_degree + 1; i < env->field_elements_limit; i++) { + if (env->pow_table[i - 1] >= mask) { + env->pow_table[i] = env->pow_table[env->base_degree] ^ ((env->pow_table[i - 1] ^ mask) << 1); + } else { + env->pow_table[i] = env->pow_table[i - 1] << 1; + } + env->log_table[env->pow_table[i]] = i; + } + + env->log_table[0] = -1; +} + +static const uint8_t iGalois256BaseDegree = 8; +static const uint8_t iGalois256IrreduciblePolynomial[] = {1, 0, 1, 1, 1, 0, 0, 0, 1}; + +void iGalois256Init(tGalois256 *env) { + env->base_degree = iGalois256BaseDegree; + env->irreducible_polynomial = (uint8_t *) iGalois256IrreduciblePolynomial; + env->field_elements_limit = (0x01 << (uint16_t) iGalois256BaseDegree) - 1; + + iGalois256GenTables(env); +} \ No newline at end of file diff --git a/Galois256.h b/Galois256.h new file mode 100644 index 0000000..4b16e9e --- /dev/null +++ b/Galois256.h @@ -0,0 +1,35 @@ +// +// Created by zemon on 24.07.2022. +// + +#ifndef PDM_FIR_TEST_GALOIS256_H +#define PDM_FIR_TEST_GALOIS256_H + +#include "stdint.h" + +typedef struct { + uint8_t base_degree; + uint8_t *irreducible_polynomial; + int32_t field_elements_limit; + int32_t pow_table[256]; + int32_t log_table[256]; + +} tGalois256; + +void iGalois256Init(tGalois256 *env); + +int32_t iGalois256Add(tGalois256 *env, int one, int two); + +int32_t iGalois256Sub(tGalois256 *env, int one, int two); + +int32_t iGalois256Mul(tGalois256 *env, int one, int two); + +int32_t iGalois256Div(tGalois256 *env, int dividable, int divider); + +int32_t iGalois256Pow(tGalois256 *env, int base, int degree); + +int32_t iGalois256LogValue(tGalois256 *env, int degree); + +int32_t iGalois256PowValue(tGalois256 *env, int degree); + +#endif //PDM_FIR_TEST_GALOIS256_H diff --git a/modular.json b/modular.json new file mode 100644 index 0000000..0dd1901 --- /dev/null +++ b/modular.json @@ -0,0 +1,10 @@ +{ + "cmake": { + "inc_dirs": [ + "./" + ], + "srcs": [ + "./**.c" + ] + } +} \ No newline at end of file