BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gf2types.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 "gf2types.h"
23 #include <bitpunch/debugio.h>
24 
25 #include <stdlib.h>
26 #include <string.h>
27 
28 
30  int i;
31 
32  if (!*m) {
33  return;
34  }
35  // first free cols
36  for (i = 0; i < (*m)->k; i++) {
37  free((*m)->elements[i]);
38  }
39  // then free rows
40  free((*m)->elements);
41  free((*m));
42  *m = NULL;
43 }
44 
46  if (!*v) {
47  return;
48  }
49  free((*v)->elements);
50  free(*v);
51  *v = NULL;
52 }
53 
54 int BPU_gf2MatMalloc(BPU_T_GF2_Matrix **m, int rows, int cols) {
55  int i;
56 
57  *m = (BPU_T_GF2_Matrix *) calloc(sizeof(BPU_T_GF2_Matrix), 1);
58 
59  if (!*m) {
60  BPU_printError("allocation error");
61  return -1;
62  }
63  // element size
64  (*m)->element_bit_size = sizeof(BPU_T_GF2)*8;
65 
66  // rows
67  (*m)->k = rows;
68  // cols
69  (*m)->n = cols;
70 
71  // calc how many elements of set size will be in one row
72  int modul = 0;
73  if ( cols % (*m)->element_bit_size > 0) {
74  modul = 1;
75  }
76  (*m)->elements_in_row = cols/(*m)->element_bit_size + modul;
77 
78  // allocate rows
79  (*m)->elements = (BPU_T_GF2**) malloc(sizeof(BPU_T_GF2*) * (*m)->k);
80 
81  if (!(*m)->elements) {
82  BPU_printError("can not allocate memory for matrix rows");
83  return -1;
84  }
85  // allocate cols
86  for (i = 0; i < (*m)->k; i++) {
87  (*m)->elements[i] = (BPU_T_GF2*) calloc(1, sizeof(BPU_T_GF2) * (*m)->elements_in_row);
88 
89  if (!(*m)->elements[i]) {
90  BPU_printError("can not allocate memory for matrix cols");
91  return -2;
92  }
93  }
94  return 0;
95 }
96 
98  *v = (BPU_T_GF2_Vector *) calloc(sizeof(BPU_T_GF2_Vector), 1);
99 
100  if (!*v) {
101  BPU_printError("allocation error");
102  return -1;
103  }
104  return BPU_gf2VecMallocElements(*v, len);
105 }
106 
108  if (v->elements) {
109  free(v->elements);
110  }
111  return BPU_gf2VecMallocElements(v, len);
112 }
113 
115  // element size in bits
116  v->element_bit_size = sizeof(BPU_T_GF2) * 8;
117 
118  // len
119  v->len = len;
120 
121  // calc how many elements of set size will be in one row
122  int modul = 0;
123 
124  if ( len % v->element_bit_size > 0) {
125  modul = 1;
126  }
127  v->elements_in_row = len / v->element_bit_size + modul;
128 
129  // allocate elemtens
130  v->elements = (BPU_T_GF2*) calloc(1, sizeof(BPU_T_GF2) * v->elements_in_row);
131 
132  if (!v->elements) {
133  BPU_printError("can not allocate memory for vector of len %d", len);
134 
135  return -1;
136  }
137  return 0;
138 }
139 
141  // allocate indexes
142  p->index = (uint32_t*) malloc(weight*sizeof(uint32_t));
143  // set weight
144  p->weight = weight;
145 }
146 
148  free(p->index);
149 
150  if (is_dyn)
151  free(p);
152 }
153 
154 void BPU_gf2SparseQcMatrixMalloc(BPU_T_GF2_Sparse_Qc_Matrix *v, int element_count, int element_size, int isVertical) {
155  // allocate matrices
156  v->matrices = (BPU_T_GF2_Sparse_Poly*) malloc(element_count*sizeof(BPU_T_GF2_Sparse_Poly));
157 
158  // set sizes depended on orientation
159  if (isVertical) {
160  v->k = element_count * element_size;
161  v->n = element_size;
162  }
163  else {
164  v->k = element_size;
165  v->n = element_count * element_size;
166  }
167 
168  // set others
169  v->element_count = element_count;
170  v->element_size = element_size;
171  v->isVertical = isVertical;
172 }
173 
175  int i;
176 
177  // free matrices
178  for (i = 0; i < v->element_count; i++)
179  BPU_gf2SparsePolyFree(&v->matrices[i], is_dyn);
180 
181  free(v->matrices);
182 
183  if (is_dyn)
184  free (v);
185 }
186 
187 void BPU_gf2PolyFree(BPU_T_GF2_Poly *p, int is_dyn) {
188  free(p->elements);
189 
190  if (is_dyn) {
191  free(p);
192  }
193 }
194 
196  // element size in bits
197  p->element_bit_size = sizeof(BPU_T_GF2) * 8;
198 
199  // len
200  p->len = len;
201 
202  // calc how many elements of set size will be in one row
203  int modul = 0;
204 
205  if ( len % p->element_bit_size > 0) {
206  modul = 1;
207  }
208  p->elements_in_row = len / p->element_bit_size + modul;
209 
210  // allocate elemtens
211  p->elements = (BPU_T_GF2*) calloc(1, sizeof(BPU_T_GF2) * p->elements_in_row);
212 
213  if (!p->elements) {
214  BPU_printError("can not allocate memory for vector of len %d", len);
215  return 1;
216  }
217  return 0;
218 }
219 
220 int BPU_gf2QcMatrixMalloc(BPU_T_GF2_QC_Matrix *v, int element_count, int element_size, int isVertical, int is_I_appended) {
221  int err = 0;
222 
223  // check isVertical
224  if (isVertical != 0 && isVertical != 1) {
225  return -1;
226  }
227 
228  // allocate matrices
229  v->matrices = (BPU_T_GF2_Poly*) malloc(element_count*sizeof(BPU_T_GF2_Poly));
230 
231  // set sizes depended on orientation
232  if (isVertical) {
233  v->k = element_count * element_size;
234  v->n = element_size;
235  }
236  else {
237  v->k = element_size;
238  v->n = element_count * element_size;
239  }
240 
241  // set others
242  v->element_count = element_count;
243  v->element_size = element_size;
244  v->is_I_appended = is_I_appended;
245  v->isVertical = isVertical;
246 
247  return err;
248 }
249 
250 // free QC binary matrix
252  int i;
253 
254  // free matrices
255  for (i = 0; i < v->element_count; i++) {
256  BPU_gf2PolyFree(&v->matrices[i], 0);
257  }
258 
259  free(v->matrices);
260 
261  if (is_dyn)
262  free (v);
263 }
int BPU_gf2VecMalloc(BPU_T_GF2_Vector **v, int len)
Definition: gf2types.c:97
uint32_t weight
weight of polynomial
Definition: gf2types.h:64
BPU_T_GF2_Sparse_Poly * matrices
all cyclic matrices of matrix
Definition: gf2types.h:91
uint8_t isVertical
if 1, elements are in vertical orientation, if 0 horizontal orientation
Definition: gf2types.h:94
uint32_t k
rows of whole matrix
Definition: gf2types.h:80
void BPU_gf2VecFree(BPU_T_GF2_Vector **v)
Free dynamically or statically allocated vector.
Definition: gf2types.c:45
uint16_t element_count
number of cyclic matrices
Definition: gf2types.h:92
void BPU_gf2PolyFree(BPU_T_GF2_Poly *p, int is_dyn)
Definition: gf2types.c:187
uint32_t element_size
size of cyclic matrix
Definition: gf2types.h:77
uint8_t is_I_appended
if matrix has identity matrix on left
Definition: gf2types.h:81
uint32_t * index
indexes of set coefficients. 0 is coefficient for x^0
Definition: gf2types.h:63
void BPU_gf2QcMatrixFree(BPU_T_GF2_QC_Matrix *v, int is_dyn)
Definition: gf2types.c:251
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
uint32_t k
rows of whole matrix
Definition: gf2types.h:96
uint32_t BPU_T_GF2
Definition: gf2types.h:26
uint32_t n
cols of whole matrix
Definition: gf2types.h:79
uint32_t element_size
size of cyclic matrix
Definition: gf2types.h:93
void BPU_gf2SparseQcMatrixFree(BPU_T_GF2_Sparse_Qc_Matrix *v, int is_dyn)
Definition: gf2types.c:174
BPU_T_GF2 * elements
all element of matrix
Definition: gf2types.h:33
void BPU_gf2SparseQcMatrixMalloc(BPU_T_GF2_Sparse_Qc_Matrix *v, int element_count, int element_size, int isVertical)
Definition: gf2types.c:154
int BPU_gf2VecResize(BPU_T_GF2_Vector *v, int len)
BPU_gf2VecResize Resize vecor.
Definition: gf2types.c:107
uint16_t element_count
number of cyclic matrices
Definition: gf2types.h:76
uint16_t elements_in_row
number of elements in one row
Definition: gf2types.h:35
int BPU_gf2MatMalloc(BPU_T_GF2_Matrix **m, int rows, int cols)
Definition: gf2types.c:54
uint32_t len
cols
Definition: gf2types.h:36
int BPU_gf2VecMallocElements(BPU_T_GF2_Vector *v, int len)
BPU_gf2VecMallocElements Malloc vector elements and set to zero.
Definition: gf2types.c:114
int BPU_gf2PolyMalloc(BPU_T_GF2_Poly *p, int len)
Definition: gf2types.c:195
void BPU_gf2SparsePolyMalloc(BPU_T_GF2_Sparse_Poly *p, int weight)
Definition: gf2types.c:140
BPU_T_GF2_Poly * matrices
all cyclic matrices of matrix
Definition: gf2types.h:75
void BPU_gf2SparsePolyFree(BPU_T_GF2_Sparse_Poly *p, int is_dyn)
Definition: gf2types.c:147
int BPU_gf2QcMatrixMalloc(BPU_T_GF2_QC_Matrix *v, int element_count, int element_size, int isVertical, int is_I_appended)
Definition: gf2types.c:220
void BPU_gf2MatFree(BPU_T_GF2_Matrix **m)
Free dynamically or statically allocated matrix.
Definition: gf2types.c:29
uint8_t element_bit_size
element size, is sizeof(BPU_T_GF2) i.e. 64 bits
Definition: gf2types.h:34
uint8_t isVertical
if 1, elements are in vertical orientation, if 0 horizontal orientation
Definition: gf2types.h:78
uint32_t n
cols of whole matrix
Definition: gf2types.h:95