This repository was archived by the owner on Mar 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGF.h
80 lines (57 loc) · 2.25 KB
/
GF.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#include <linux/types.h>
#include "poly.h"
// Galois field.
typedef struct GF {
uint8_t p; // Characteristic of the field GF(p).
poly_t *I; // Irreducible polynomial over GF(p)[x].
} GF_t;
// Element of the Galois field.
typedef struct GF_elem {
GF_t *GF; // Galois field.
poly_t *poly; // Element of the GF(p)[x]/(I).
} GF_elem_t;
// x^8 + x^4 + x^3 + x^2 + 1
extern GF_t GF2_8;
// x^16 + x^9 + x^8 + x^7 + x^6 + x^4 + x^3 + x^2 + 1
extern GF_t GF2_16;
// x^32 + x^22 + x^2 + x^1 + 1
extern GF_t GF2_32;
/* Initialize GF(P)[x]/(I) */
GF_t *GF_init_field(uint8_t p, poly_t I);
/* Destryo a field structure. */
void GF_destroy_field(GF_t *GF);
/* Destroy a given element of the Galois Field.
The field itself is left untouched. */
void GF_elem_destroy(GF_elem_t *a);
/* Given an array of polynomial coefficients of any length and GF(p)[x]/(I),
Return an element over that field. */
GF_elem_t *GF_elem_from_array(uint8_t deg, uint8_t *coeff, GF_t *GF);
/* Convert to and from uint8, uint16, uint32 to the element of the GF(2). */
GF_elem_t *GF_elem_from_uint8(uint8_t x);
GF_elem_t *GF_elem_from_uint16(uint16_t x);
GF_elem_t *GF_elem_from_uint32(uint32_t x);
uint8_t GF_elem_to_uint8(GF_elem_t *a);
uint16_t GF_elem_to_uint16(GF_elem_t *a);
uint32_t GF_elem_to_uint32(GF_elem_t *a);
/* Return 1 if the given fields are equal.
Meaning they have the same characteristic and irreducible polynomials. */
bool GF_eq(const GF_t *F, const GF_t *K);
/* Return a copy of an a. */
GF_elem_t *GF_elem_cpy(GF_elem_t *a);
/* res = a + b mod (I). */
void GF_elem_sum(GF_elem_t *res, GF_elem_t *a, GF_elem_t *b);
/* res = a - b mod (I). */
void GF_elem_diff(GF_elem_t *res, GF_elem_t *a, GF_elem_t *b);
/* res = a * b mod (I). */
void GF_elem_prod(GF_elem_t *res, GF_elem_t *a, GF_elem_t *b);
/* Calculate res: a = b * res mod (I). */
void GF_elem_div(GF_elem_t *res, GF_elem_t *a, GF_elem_t *b);
/* Return res = -a mod p. */
GF_elem_t *GF_elem_get_complement(GF_elem_t *a);
/* Calculate res: res * a = 1 mod (I). */
GF_elem_t *GF_elem_get_inverse(GF_elem_t *a);
/* Return neutral element of the given finite field. */
GF_elem_t *GF_elem_get_neutral(GF_t *GF);
/* Return unity element of the given finite field. */
GF_elem_t *GF_elem_get_unity(GF_t *GF);