BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mathctx.c
Go to the documentation of this file.
1 /*
2 This file is part of BitPunch
3 Copyright (C) 2014-2015 Frantisek Uhrecky <frantisek.uhrecky[what here]gmail.com>
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "mathctx.h"
19 #include "gf2x.h"
20 
21 #include <bitpunch/debugio.h>
22 #include <stdlib.h>
23 
25  BPU_T_GF2_16x b = 1;
26  int i = 0;
27  BPU_T_Math_Ctx *math_ctx;
28 
29  *ctx = (BPU_T_Math_Ctx *) calloc(1, sizeof(BPU_T_Math_Ctx));
30  if (!*ctx) {
31  BPU_printError("Can not malloc BPU_T_Math_Ctx");
32 
33  return -1;
34  }
35  math_ctx = *ctx;
36  // get group ord, number of elements
37  BPU_T_GF2_16x ord = ((1 << BPU_gf2xGetDeg(mod)) - 1);
38 
39  // alocate memory for tables
40  math_ctx->mod = mod;
41  math_ctx->mod_deg = BPU_gf2xGetDeg(mod);
42  math_ctx->log_table = (BPU_T_GF2_16x*) malloc(sizeof(BPU_T_GF2_16x) * (ord + 1));
43  math_ctx->exp_table = (BPU_T_GF2_16x*) malloc(sizeof(BPU_T_GF2_16x) * (ord + 1));
44 
45  // set ord
46  math_ctx->ord = ord;
47 
48  do {
49  math_ctx->exp_table[i] = b;
50  math_ctx->log_table[b] = i;
51 
52  b = BPU_gf2xMulMod(b, g, mod);
53  i++;
54  } while (b != 1);
55 
56  math_ctx->exp_table[ord] = 0;
57  math_ctx->log_table[0] = ord;
58 
59  if (i != ord) {
60  BPU_printError("element 0x%x is not generator", g);
61 
62  return 1;
63  }
64  return 0;
65 }
66 
68  if (!*ctx) {
69  return;
70  }
71  if ((*ctx)->exp_table) {
72  free((*ctx)->exp_table);
73  }
74  if ((*ctx)->log_table) {
75  free((*ctx)->log_table);
76  }
77  free(*ctx);
78  *ctx = NULL;
79 }
BPU_T_GF2_16x BPU_gf2xMulMod(BPU_T_GF2_16x a, BPU_T_GF2_16x b, BPU_T_GF2_16x mod)
Multiplication over Galois field, modulus mod.
Definition: gf2x.c:78
BPU_T_GF2_16x * exp_table
there are all elements referenced by i, so at i-th index is g^i element, g - generator ...
Definition: mathctx.h:33
BPU_T_GF2_16x mod
polynomial modulus
Definition: mathctx.h:35
void BPU_mathFreeCtx(BPU_T_Math_Ctx **ctx)
Free dynamiccaly or statically allocated Aritmetic_Data structure.
Definition: mathctx.c:67
int BPU_gf2xGetDeg(BPU_T_GF2_32x poly)
Get degree of polynomial over GF2.
Definition: gf2x.c:477
uint16_t BPU_T_GF2_16x
Definition: gf2xtypes.h:26
BPU_T_GF2_16x * log_table
there are all indexes referenced by element, so alpha elemnet (g^i) -> i
Definition: mathctx.h:34
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
int BPU_mathInitCtx(BPU_T_Math_Ctx **ctx, const BPU_T_GF2_16x g, const BPU_T_GF2_16x mod)
Precalculate logaritmic and exponencial tables and initialize structure Aritmetic_Data.
Definition: mathctx.c:24
int ord
group ord, number of elements
Definition: mathctx.h:37
uint8_t mod_deg
modulo degree, galois finite field GF(2^m)
Definition: mathctx.h:36