BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gf2xtypes.c
Go to the documentation of this file.
1 /*
2 This file is part of BitPunch
3 Copyright (C) 2013-2015 Frantisek Uhrecky <frantisek.uhrecky[what here]gmail.com>
4 Copyright (C) 2013-2014 Andrej Gulyas <andrej.guly[what here]gmail.com>
5 Copyright (C) 2013-2014 Marek Klein <kleinmrk[what here]gmail.com>
6 Copyright (C) 2013-2014 Filip Machovec <filipmachovec[what here]yahoo.com>
7 Copyright (C) 2013-2014 Jozef Kudlac <jozef[what here]kudlac.sk>
8 
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "gf2xtypes.h"
23 #include <bitpunch/debugio.h>
24 
25 #include <stdlib.h>
26 #include <string.h>
27 
28 int BPU_gf2xMatMalloc(BPU_T_GF2_16x_Matrix **m, int rows, int cols) {
29  int i;
30  *m = (BPU_T_GF2_16x_Matrix *) calloc(sizeof(BPU_T_GF2_16x_Matrix), 1);
31 
32  if (!*m) {
33  BPU_printError("allocation error");
34  return -1;
35  }
36  // rows
37  (*m)->k = rows;
38  // cols
39  (*m)->n = cols;
40  // allocate rows
41  (*m)->elements = (BPU_T_GF2_16x **) malloc(sizeof(BPU_T_GF2_16x*) * (*m)->k);
42 
43  if (!(*m)->elements) {
44  BPU_printError("BPU_mallocMatrix: can not allocate memory for matrix rows");
45 
46  return 1;
47  }
48  // allocate cols
49  for (i = 0; i < (*m)->k; i++) {
50  (*m)->elements[i] = (BPU_T_GF2_16x*) calloc(1, sizeof(BPU_T_GF2_16x) * (*m)->n);
51 
52  if (!(*m)->elements[i]) {
53  BPU_printError("allocation error");
54  return -2;
55  }
56  }
57  return 0;
58 }
59 
61  *vec = (BPU_T_GF2_16x_Vector *) calloc(sizeof(BPU_T_GF2_16x_Vector), 1);
62 
63  if (!*vec) {
64  BPU_printError("allocation error");
65  return -1;
66  }
67  (*vec)->len = size;
68  (*vec)->elements = (BPU_T_GF2_16x*)calloc(size, sizeof(BPU_T_GF2_16x));
69 
70  if (!(*vec)->elements) {
71  BPU_printError("allocation error");
72  return -2;
73  }
74  return 0;
75 }
76 
78  if (!*vec) {
79  return;
80  }
81  free((*vec)->elements);
82  free(*vec);
83  *vec = NULL;
84 }
85 
87  int i;
88 
89  if (!*m) {
90  return;
91  }
92  // first free cols
93  for (i = 0; i < (*m)->k; i++) {
94  free((*m)->elements[i]);
95  }
96  // then free rows
97  free((*m)->elements);
98  free((*m));
99  *m = NULL;
100 }
101 
103  if (!*p) {
104  return;
105  }
106  free((*p)->coef);
107  free(*p);
108  *p = NULL;
109 }
110 
111 int BPU_gf2xPolyMalloc(BPU_T_GF2_16x_Poly **p, int16_t max_deg) {
112  *p = (BPU_T_GF2_16x_Poly *) calloc(sizeof(BPU_T_GF2_16x_Poly), 1);
113 
114  if (!*p) {
115  BPU_printError("allocation error");
116  return -1;
117  }
118  return BPU_gf2xPolyMallocCoef(*p, max_deg);
119 }
120 
121 int BPU_gf2xPolyResize(BPU_T_GF2_16x_Poly *p, int16_t max_deg) {
122  if (p->coef) {
123  free(p->coef);
124  }
125  return BPU_gf2xPolyMallocCoef(p, max_deg);
126 }
127 
128 int BPU_gf2xPolyMallocCoef(BPU_T_GF2_16x_Poly *p, int16_t max_deg) {
129  // allocate memory
130  p->deg = -1;
131  p->max_deg = max_deg;
132 
133  if (p->max_deg < 0) {
134  BPU_printError("BPU_mallocPoly: max_deg must be at least 0");
135 
136  return -1;
137  }
138  p->coef = (BPU_T_GF2_16x*) calloc(max_deg + 1, sizeof(BPU_T_GF2_16x));
139 
140  if (!p->coef) {
141  BPU_printError("BPU_mallocPoly: can not allocate polynomial");
142 
143  return -1;
144  }
145  return 0;
146 }
147 
149  int i, j;
150  for (i = 0; i < mat->k; i++) {
151  for (j = 0; j < mat->n; j++) {
152  mat->elements[i][j] = 0;
153  }
154  }
155 }
void BPU_gf2xMatNull(BPU_T_GF2_16x_Matrix *mat)
Definition: gf2xtypes.c:148
int BPU_gf2xMatMalloc(BPU_T_GF2_16x_Matrix **m, int rows, int cols)
Allocate memory for matrix.
Definition: gf2xtypes.c:28
BPU_T_GF2_16x ** elements
all element of matrix
Definition: gf2xtypes.h:45
int BPU_gf2xPolyMalloc(BPU_T_GF2_16x_Poly **p, int16_t max_deg)
Definition: gf2xtypes.c:111
uint16_t BPU_T_GF2_16x
Definition: gf2xtypes.h:26
uint16_t n
cols
Definition: gf2xtypes.h:47
int BPU_gf2xPolyMallocCoef(BPU_T_GF2_16x_Poly *p, int16_t max_deg)
BPU_gf2xPolyMallocCoef Malloc internal coeficients for polynomial.
Definition: gf2xtypes.c:128
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
void BPU_gf2xMatFree(BPU_T_GF2_16x_Matrix **m)
Free dynamically or statically allocated matrix.
Definition: gf2xtypes.c:86
int16_t max_deg
degree
Definition: gf2xtypes.h:56
BPU_T_GF2_16x * coef
Polynomial over GF2m.
Definition: gf2xtypes.h:54
void BPU_gf2xPolyFree(BPU_T_GF2_16x_Poly **p)
Definition: gf2xtypes.c:102
uint16_t k
rows
Definition: gf2xtypes.h:46
int16_t deg
degree
Definition: gf2xtypes.h:55
int BPU_gf2xPolyResize(BPU_T_GF2_16x_Poly *p, int16_t max_deg)
BPU_gf2xPolyResize Resize polynomial, increase max deg.
Definition: gf2xtypes.c:121
void BPU_gf2xVecFree(BPU_T_GF2_16x_Vector **vec)
BPU_gf2xVecFree Free vector structure.
Definition: gf2xtypes.c:77
int BPU_gf2xVecMalloc(BPU_T_GF2_16x_Vector **vec, int size)
BPU_gf2xVecMalloc Malloc vector structure.
Definition: gf2xtypes.c:60