BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
codectx.c
Go to the documentation of this file.
1 /*
2 This file is part of BitPunch
3 Copyright (C) 2015 Frantisek Uhrecky <frantisek.uhrecky[what here]gmail.com>
4 Copyright (C) 2015 Andrej Gulyas <andrej.guly[what here]gmail.com>
5 
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "codectx.h"
20 
21 #include <stdlib.h>
22 
23 #include <bitpunch/debugio.h>
24 #include <bitpunch/errorcodes.h>
25 #include <bitpunch/math/gf2types.h>
26 
27 // additional codes
30 
32  int tmp;
33  BPU_T_Code_Ctx *ctx_p;
34 
35  *ctx = (BPU_T_Code_Ctx *) calloc(1, sizeof(BPU_T_Code_Ctx));
36  if (!*ctx) {
37  BPU_printError("Can not malloc BPU_T_Code_Ctx");
38 
39  return BPU_EC_MALLOC_ERROR;
40  }
41  ctx_p = *ctx;
42  ctx_p->type = type;
43 
44  ctx_p->code_spec = (BPU_T_UN_Code_Spec *) calloc(1, sizeof(BPU_T_UN_Code_Spec));
45  if (!ctx_p->code_spec) {
46  BPU_printError("Can not malloc BPU_T_UN_Code_Spec");
47 
48  return BPU_EC_MALLOC_ERROR;
49  }
50  switch (type) {
51  case BPU_EN_CODE_GOPPA:
52 #ifdef BPU_CONF_ENCRYPTION
53  ctx_p->_encode = BPU_goppaEncode;
54 #endif
55 #ifdef BPU_CONF_DECRYPTION
56  ctx_p->_decode = BPU_goppaDecode;
57 #endif
58  tmp = BPU_codeInitMathCtx(&ctx_p->math_ctx, params->goppa->m, params->goppa->t, params->goppa->mod);
59  if (tmp) {
60  BPU_printError("Code math context initialization ERROR.");
61 
62  return tmp;
63  }
64  ctx_p->code_spec->goppa = (BPU_T_Goppa_Spec *) calloc(1, sizeof(BPU_T_Goppa_Spec));
65  if (!ctx_p->code_spec->goppa) {
66  BPU_printError("Can not malloc BPU_T_Goppa_Spec");
67 
68  return BPU_EC_MALLOC_ERROR;
69  }
70  ctx_p->code_spec->goppa->support_len = (1 << params->goppa->m); // ctx->math_ctx->ord + 1;
71  ctx_p->code_len = ctx_p->code_spec->goppa->support_len;
72  ctx_p->msg_len = ctx_p->code_spec->goppa->support_len - params->goppa->m*params->goppa->t; // n - m*t
73  ctx_p->t = params->goppa->t;
74 
75  break;
76  case BPU_EN_CODE_QCMDPC:
77  ctx_p->code_spec->qcmdpc = (BPU_T_Qcmdpc_Spec *) calloc(1, sizeof(BPU_T_Qcmdpc_Spec));
78 #ifdef BPU_CONF_ENCRYPTION
80  // ctx_p->code_spec->qcmdpc->G = (BPU_T_GF2_QC_Matrix *) malloc(sizeof(BPU_T_GF2_QC_Matrix));
81 #endif
82 #ifdef BPU_CONF_DECRYPTION
84  // ctx_p->code_spec->qcmdpc->H = (BPU_T_GF2_Sparse_Qc_Matrix *) malloc(sizeof(BPU_T_GF2_Sparse_Qc_Matrix));
85 #endif
86  if (!ctx_p->code_spec->qcmdpc) {
87  BPU_printError("Can not malloc BPU_T_Goppa_Spec");
88 
89  return BPU_EC_MALLOC_ERROR;
90  }
91  ctx_p->code_spec->qcmdpc->m = params->qcmdpc->m;
92  ctx_p->code_spec->qcmdpc->n0 = params->qcmdpc->n0;
93  ctx_p->code_spec->qcmdpc->w = params->qcmdpc->w;
94  ctx_p->code_len = params->qcmdpc->m * params->qcmdpc->n0;
95  ctx_p->msg_len = ctx_p->code_len - params->qcmdpc->m;
96  ctx_p->t = params->qcmdpc->t;
97 
98  break;
99  /* EXAMPLE please DO NOT REMOVE
100  case BPU_EN_CODE_*****:
101 #ifdef BPU_CONF_ENCRYPTION
102  ctx_p->_encode = FUNC_FROM_YOUR_FILE;
103 #endif
104 #ifdef BPU_CONF_DECRYPTION
105  ctx_p->_decode = FUNC_FROM_YOUR_FILE;
106 #endif
107  ctx->code_spec->YOURS = ALLOC OR NULL;
108 
109  ctx_p->code_len = LEN;
110  ctx_p->msg_len = LEN;
111  ctx_p->t = T;
112  break;
113  */
114  default:
115  BPU_printError("Code type not supported: %d", type);
117  }
118  // error vector
119  if (BPU_gf2VecMalloc(&ctx_p->e, ctx_p->code_len)) {
120  BPU_printError("can not allocate error vector");
121  return BPU_EC_MALLOC_ERROR;
122  }
123  return 0;
124 }
125 
126 int BPU_codeInitMathCtx(BPU_T_Math_Ctx **ctx, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod) {
127  int rc = 0;
128 
129  if (mod == (BPU_T_GF2_16x) -1) {
130  *ctx = (BPU_T_Math_Ctx *) calloc(1, sizeof(BPU_T_Math_Ctx));
131  if (!*ctx) {
132  BPU_printError("Can not malloc BPU_T_Math_Ctx");
133 
134  return -1;
135  }
136  (*ctx)->mod_deg = m;
137  }
138  else if (mod != 0) {
139  rc = BPU_mathInitCtx(ctx, (BPU_T_GF2_16x)2, mod);
140  }
141  else if (m == 5 && t == 5) {
143  }
144  else if (m == 6 && t == 6) {
146  }
147  else if (m == 6 && t == 7) {
149  }
150  else if (m == 11 && t == 50) {
152  }
153  else {
154  BPU_printError("Code params not supported. Supported only (m,t): (5,5), (6,6), (6,7), (11,50)");
155 
157  }
158  return rc;
159 }
160 
162  BPU_T_Code_Ctx *ctx_p = *ctx;
163 
164  if (!ctx_p) {
165  return;
166  }
167  switch (ctx_p->type) {
168  case BPU_EN_CODE_GOPPA:
170  free(ctx_p->code_spec->goppa);
171  break;
172  case BPU_EN_CODE_QCMDPC:
174  // free(ctx_p->code_spec->qcmdpc);
175  break;
176  default:
177  BPU_printError("Code type not supported: %d", ctx_p->type);
178  }
179  BPU_gf2VecFree(&ctx_p->e);
180  BPU_mathFreeCtx(&ctx_p->math_ctx);
181 
182  free(ctx_p->code_spec);
183  free(ctx_p);
184  *ctx = NULL;
185 }
186 
187 int BPU_codeInitParamsGoppa(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod) {
188  return BPU_goppaInitParams(&params->goppa, m, t, mod);
189 }
190 
192  BPU_goppaFreeParams(&params->goppa);
193 }
194 
195 int BPU_codeInitParamsQcmdpc(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t) {
196  return BPU_qcmdpcInitParams(&params->qcmdpc, m, n0, w, t);
197 }
198 
200  BPU_qcmdpcFreeParams(&params->qcmdpc);
201 }
enum _BPU_T_EN_Code_Types BPU_T_EN_Code_Types
Possible types of codes.
BPU_T_Qcmdpc_Spec * qcmdpc
Definition: codectx.h:43
BPU_T_Goppa_Params * goppa
Definition: codectx.h:48
int BPU_goppaInitParams(BPU_T_Goppa_Params **params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod)
BPU_goppaInitParams Initi Goppa code params.
Definition: goppatypes.c:36
int BPU_gf2VecMalloc(BPU_T_GF2_Vector **v, int len)
Definition: gf2types.c:97
void BPU_qcmdpcFreeParams(BPU_T_Qcmdpc_Params **params)
Definition: qcmdpctypes.c:43
int BPU_mecsQcmdpcDecrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx)
Definition: qcmdpc.c:65
BPU_T_Math_Ctx * math_ctx
Math context.
Definition: codectx.h:57
void BPU_mathFreeCtx(BPU_T_Math_Ctx **ctx)
Free dynamiccaly or statically allocated Aritmetic_Data structure.
Definition: mathctx.c:67
#define BPU_GF2_POLY_DEG_5
Definition: mathctx.h:24
void BPU_gf2VecFree(BPU_T_GF2_Vector **v)
Free dynamically or statically allocated vector.
Definition: gf2types.c:45
uint16_t t
error capability of code
Definition: goppatypes.h:40
void BPU_goppaFreeSpec(BPU_T_Goppa_Spec *spec)
BPU_goppaFreeSpec Free Goppa code internal structure.
Definition: goppatypes.c:27
BPU_T_Qcmdpc_Params * qcmdpc
Definition: codectx.h:49
uint16_t m
degree of mod polynomial
Definition: goppatypes.h:39
void BPU_codeFreeParamsGoppa(BPU_T_UN_Code_Params *params)
BPU_codeFreeParamsGoppa.
Definition: codectx.c:191
uint16_t BPU_T_GF2_16x
Definition: gf2xtypes.h:26
void BPU_codeFreeParamsQcmdpc(BPU_T_UN_Code_Params *params)
Definition: codectx.c:199
#define BPU_GF2_POLY_DEG_6
Definition: mathctx.h:25
#define BPU_EC_CODE_TYPE_NOT_SUPPORTED
Definition: errorcodes.h:27
uint16_t n0
number of cyclic matrices
Definition: qcmdpctypes.h:32
int BPU_goppaDecode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx)
Definition: goppa.c:66
int BPU_codeInitCtx(BPU_T_Code_Ctx **ctx, const BPU_T_UN_Code_Params *params, const BPU_T_EN_Code_Types type)
BPU_codeInitCtx Initialize (register) code functions encode, decode and code spec structure based on ...
Definition: codectx.c:31
uint16_t w
weight of parity-check matrix row
Definition: qcmdpctypes.h:42
uint16_t m
size of cyclic matrix
Definition: qcmdpctypes.h:40
uint16_t msg_len
Code dimenzion.
Definition: codectx.h:62
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
int BPU_codeInitParamsQcmdpc(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t)
Definition: codectx.c:195
uint16_t support_len
number of elements in support
Definition: goppatypes.h:32
int BPU_qcmdpcInitParams(BPU_T_Qcmdpc_Params **params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t)
Definition: qcmdpctypes.c:27
Code specifics union type.
Definition: codectx.h:41
BPU_T_GF2_Vector * e
Error vector.
Definition: codectx.h:58
uint16_t w
weight of parity-check matrix row
Definition: qcmdpctypes.h:33
int(* _decode)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx)
This is reference to decode function. It will be set in BPU_codeInitCtx.
Definition: codectx.h:56
#define BPU_GF2_POLY_DEG_11
Definition: mathctx.h:27
int(* _encode)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx)
This is reference to encode function. It will be set in BPU_codeInitCtx.
Definition: codectx.h:55
BPU_T_EN_Code_Types type
Definition: codectx.h:54
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
uint16_t n0
number of cyclic matrices
Definition: qcmdpctypes.h:41
uint16_t t
count of errors
Definition: qcmdpctypes.h:43
void BPU_codeFreeCtx(BPU_T_Code_Ctx **ctx)
BPU_codeFreeCtx.
Definition: codectx.c:161
void BPU_qcmdpcFreeSpec(BPU_T_Qcmdpc_Spec *spec)
Definition: qcmdpctypes.c:21
int BPU_mecsQcmdpcEncode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx)
Definition: qcmdpc.c:23
BPU_T_GF2_16x mod
Galois field polynomial.
Definition: goppatypes.h:41
int BPU_codeInitMathCtx(BPU_T_Math_Ctx **ctx, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod)
BPU_codeInitMathCtx.
Definition: codectx.c:126
uint16_t m
size of cyclic matrix
Definition: qcmdpctypes.h:31
int BPU_codeInitParamsGoppa(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod)
BPU_codeInitParamsGoppa.
Definition: codectx.c:187
#define BPU_EC_MALLOC_ERROR
Definition: errorcodes.h:24
int BPU_goppaEncode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx)
BPU_goppaEncode Encode message using goppa code.
Definition: goppa.c:32
#define BPU_EC_CODE_PARAMS_NOT_SUPPORTED
Definition: errorcodes.h:28
BPU_T_Goppa_Spec * goppa
Definition: codectx.h:42
uint8_t t
Error code correction capability.
Definition: codectx.h:63
uint16_t code_len
Code len.
Definition: codectx.h:61
void BPU_goppaFreeParams(BPU_T_Goppa_Params **params)
BPU_goppaFreeParams Free goppa code params.
Definition: goppatypes.c:51
BPU_T_UN_Code_Spec * code_spec
Code specific structure, like generator matrix, control matrix, gen. poly ...
Definition: codectx.h:59