BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gf2.h
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-2015 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 #ifndef BPU_GF2_H
23 #define BPU_GF2_H
24 
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include <bitpunch/config.h>
29 #include "permtypes.h"
30 #include "gf2types.h"
31 #include "int.h"
32 
33 #ifdef BPU_CONF_PRINT
34 /* ==================================== Print functions ==================================== */
35 // TODO: Be enable to turn off print function, to not compile in in code
42 void BPU_printBinaryMsb(uint32_t in, int len);
44 
51 void BPU_printBinaryMsbLn(uint32_t in, int len);
53 
60 void BPU_printBinaryMsb32(uint32_t in);
62 
69 void BPU_printBinaryMsb32Ln(uint32_t in);
71 
78 void BPU_printBinaryLsb(uint32_t in, int len);
80 
87 void BPU_printBinaryLsbLn(uint32_t in, int len);
89 
96 void BPU_printBinaryLsb32(uint32_t in);
98 
105 void BPU_printBinary32LsbLn(uint32_t in);
107 
112 void BPU_printGf2Mat(const BPU_T_GF2_Matrix *m);
113 
118 void BPU_printGf2Vec(const BPU_T_GF2_Vector *v);
119 
124 void BPU_printGf2VecMsb(const BPU_T_GF2_Vector* v);
125 
130 void BPU_printGf2VecOnes(const BPU_T_GF2_Vector *vec);
131 
132 
138 
145 
150 void BPU_printGf2Poly(const BPU_T_GF2_Poly* v);
151 
157 
163 
164 /* ------------------------------------ Print functions ------------------------------------ */
165 #endif // BPU_CONF_PRINT
166 
172 #define BPU_getBit(w, n) ((int)((((uint32_t)w) >> (n)) & 1u))
174 
182 #define BPU_gf2MatGetBit(m_pointer, s, t) (BPU_getBit((m_pointer)->elements[s][(t) / (m_pointer)->element_bit_size], (t) % (m_pointer)->element_bit_size))
184 
191 #define BPU_gf2VecGetBit(v_pointer, i) (BPU_getBit((v_pointer)->elements[(i) / (v_pointer)->element_bit_size], (i) % (v_pointer)->element_bit_size))
193 
201 #define BPU_gf2MatSetBit(m_pointer, s, t, bit) if (bit) { \
202  (m_pointer)->elements[s][t / (m_pointer)->element_bit_size] |= ((BPU_T_GF2) 1) << ((t) % (m_pointer)->element_bit_size);\
203  } \
204  else { \
205  /* this is like: 00101111 ^ 00000100 = 00101011 */\
206  (m_pointer)->elements[s][(t) / (m_pointer)->element_bit_size] &= ((BPU_T_GF2) (0xFFFFFFFFu)) ^ (((BPU_T_GF2) 1) << ((t) % (m_pointer)->element_bit_size));\
207  }
208 
215 #define BPU_gf2VecSetBit(v_pointer, i, bit) if (bit) { \
216  (v_pointer)->elements[(i) / (v_pointer)->element_bit_size] |= ((BPU_T_GF2) 1) << ((i) % (v_pointer)->element_bit_size);\
217  } \
218  else { \
219  /* this is like: 00101111 ^ 00000100 = 00101011 */\
220  (v_pointer)->elements[(i) / (v_pointer)->element_bit_size] &= ((BPU_T_GF2) (0xFFFFFFFFu)) ^ (((BPU_T_GF2) 1) << ((i) % (v_pointer)->element_bit_size));\
221  }
222 
229 #define BPU_gf2MatCopyRowToVec(v_pointer, m_pointer, row) memcpy((void *) ((v_pointer)->elements), (void *) ((m_pointer)->elements[row]), (v_pointer)->element_bit_size / 8 * (v_pointer)->elements_in_row)
231 
238 #define BPU_gf2PolyGetBit(poly, bit) ((poly->elements[bit/poly->element_bit_size] >> (bit%poly->element_bit_size)) & 1ul)
239 
247 
255 
262 int BPU_gf2VecPermute(BPU_T_GF2_Vector *vec, const BPU_T_Perm_Vector *permutation);
263 
271 BPU_T_GF2 BPU_gf2MatGetMaskedBit(const BPU_T_GF2_Matrix *m, uint32_t row, uint32_t bit);
272 
279 BPU_T_GF2 BPU_gf2VecGetMaskedBit(const BPU_T_GF2_Vector *vec, uint32_t bit);
280 
288 
295 void BPU_gf2Swap(BPU_T_GF2 *a, BPU_T_GF2 *b);
296 
304 void BPU_gf2MatSwapRows(BPU_T_GF2_Matrix *mat, int i, int j);
305 
313 int BPU_gf2MatFindRow(const BPU_T_GF2_Matrix *mat, int i, int start_index);
314 
322 int BPU_gf2MatFindCol(const BPU_T_GF2_Matrix *mat, int i, int start_index);
323 
331 
339 int BPU_gf2VecConcat(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *vec1, const BPU_T_GF2_Vector *vec2);
341 
350 int BPU_gf2VecCrop(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const int start, const int length);
351 
360 int BPU_gf2MatGetRowAsGf2Vec(BPU_T_GF2_Vector *out, const BPU_T_GF2_Matrix *in, int row);
361 
367 void BPU_gf2VecCopy(BPU_T_GF2_Vector *dest, const BPU_T_GF2_Vector *src);
369 
376 int BPU_gf2VecCmp(const BPU_T_GF2_Vector *v1, const BPU_T_GF2_Vector *v2);
377 
385 void BPU_gf2MatXorRows(BPU_T_GF2_Matrix *mat, int i, int j);
386 
393 int BPU_gf2VecXor(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in);
395 
402 int BPU_gf2VecRand(BPU_T_GF2_Vector *out, int w);
403 
413 
421 int BPU_gf2MatCrop(BPU_T_GF2_Matrix *m, uint16_t width);
423 
429 uint8_t BPU_getParity(BPU_T_GF2 dword);
430 
431 /************************************************
432 POLYNOMIAL UTILS
433 ************************************************/
434 
440 int BPU_gf2PolyIsZero(const BPU_T_GF2_Poly *a);
441 
448 void BPU_gf2PolySetDeg(BPU_T_GF2_Poly *a, int deg);
449 
456 
462 void BPU_gf2PolyShiftLeft(BPU_T_GF2_Poly *a, int shift_count);
463 
469 
475 void BPU_gf2PolyTransp(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in);
476 
477 
478 /************************************************
479 POLYNOMIAL MATH
480 ************************************************/
481 
487 
497 void BPU_gf2PolyAdd(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in, int crop);
498 
510 void BPU_gf2PolyMulMod(const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b, BPU_T_GF2_Poly *c, const BPU_T_GF2_Poly *m, int crop);
511 
522 
534 
543 int BPU_gf2PolyInv(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *mod);
544 
545 
546 /************************************************
547 SPARSE POLYNOMIAL UTILS
548 ************************************************/
549 
556 
562 void BPU_gf2PolyCopy(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in);
563 
572 int BPU_gf2PolyInitRand(BPU_T_GF2_Poly *out, int l, int w, int set_deg);
573 
574 
575 /************************************************
576 SPARSE POLYNOMIAL MATH
577 ************************************************/
578 
586 
595 
596 
597 /************************************************
598 QUASI-CYCLIC MATRIX UTILS
599 ************************************************/
600 
607 
615 int BPU_gf2QcMatrixToSparse(BPU_T_GF2_Sparse_Qc_Matrix *out, const BPU_T_GF2_QC_Matrix *in, const int wi[]);
616 
617 
618 /************************************************
619 SPARSE QUASI-CYCLIC MATRIX UTILS
620 ************************************************/
621 
629 
636 
637 #endif // BPU_GF2_H
void BPU_printBinaryMsb32Ln(uint32_t in)
Print number as binary string in big endian so msb is first printed.
Definition: gf2.c:48
void BPU_gf2SparsePolyCopy(BPU_T_GF2_Sparse_Poly *out, const BPU_T_GF2_Sparse_Poly *in)
Definition: gf2.c:642
void BPU_gf2PolyAdd(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in, int crop)
Definition: gf2.c:806
int BPU_gf2VecCmp(const BPU_T_GF2_Vector *v1, const BPU_T_GF2_Vector *v2)
BPU_gf2VecCmp Compare two vectors.
Definition: gf2.c:467
int BPU_gf2PolyGetHighestBitPos(BPU_T_GF2_Poly *a)
Definition: gf2.c:733
int BPU_gf2MatCopy(BPU_T_GF2_Matrix *out, const BPU_T_GF2_Matrix *in)
BPU_gf2MatCopyCreate copy of input matrix.
Definition: gf2.c:271
void BPU_gf2Swap(BPU_T_GF2 *a, BPU_T_GF2 *b)
Definition: gf2.c:340
void BPU_gf2PolyShiftRightOne(BPU_T_GF2_Poly *a)
Definition: gf2.c:792
void BPU_gf2PolyTransp(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in)
Definition: gf2.c:1061
int BPU_gf2VecCrop(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const int start, const int length)
Definition: gf2.c:424
void BPU_printBinaryLsb(uint32_t in, int len)
Print number as binary string in little endian so lsb is first printed.
Definition: gf2.c:52
void BPU_printGf2Vec(const BPU_T_GF2_Vector *v)
Definition: gf2.c:100
void BPU_gf2VecCopy(BPU_T_GF2_Vector *dest, const BPU_T_GF2_Vector *src)
Copy VectorGF2.
Definition: gf2.c:454
int BPU_gf2MatCrop(BPU_T_GF2_Matrix *m, uint16_t width)
Crop matrix GF2 from left.
Definition: gf2.c:552
void BPU_printBinaryMsb(uint32_t in, int len)
Print number as binary string in big endian so msb is first printed.
Definition: gf2.c:31
int BPU_gf2PolyInv(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *mod)
Definition: gf2.c:1042
int BPU_gf2MatFindRow(const BPU_T_GF2_Matrix *mat, int i, int start_index)
Definition: gf2.c:356
int BPU_gf2MatTransp(BPU_T_GF2_Matrix *out, const BPU_T_GF2_Matrix *in)
Definition: gf2.c:323
void BPU_printBinaryMsb32(uint32_t in)
Print number as binary string in big endian so msb is first printed.
Definition: gf2.c:44
void BPU_gf2PolyCopy(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in)
Definition: gf2.c:586
void BPU_gf2SparsePolyAdd(BPU_T_GF2_Poly *out, const BPU_T_GF2_Sparse_Poly *in)
Definition: gf2.c:830
void BPU_gf2QcMatrixTransp(BPU_T_GF2_QC_Matrix *out, const BPU_T_GF2_QC_Matrix *in)
Definition: gf2.c:1076
void BPU_printGf2Mat(const BPU_T_GF2_Matrix *m)
Definition: gf2.c:73
void BPU_printBinaryLsb32(uint32_t in)
Print number as binary string in little endian so lsb is first printed.
Definition: gf2.c:65
uint32_t BPU_T_GF2
Definition: gf2types.h:26
void BPU_gf2PolyExtEuclidA(BPU_T_GF2_Poly *d, BPU_T_GF2_Poly *s, BPU_T_GF2_Poly *t, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b, const BPU_T_GF2_Poly *m)
Definition: gf2.c:951
int BPU_gf2VecRand(BPU_T_GF2_Vector *out, int w)
Definition: gf2.c:240
void BPU_gf2PolyMulMod(const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b, BPU_T_GF2_Poly *c, const BPU_T_GF2_Poly *m, int crop)
Definition: gf2.c:876
int BPU_gf2MatPermute(BPU_T_GF2_Matrix *m, BPU_T_Perm_Vector *permutation)
Definition: gf2.c:526
void BPU_printGf2SparsePoly(const BPU_T_GF2_Sparse_Poly *v)
Definition: gf2.c:155
void BPU_gf2MatXorRows(BPU_T_GF2_Matrix *mat, int i, int j)
Definition: gf2.c:518
void BPU_gf2PolyMulX(BPU_T_GF2_Poly *a)
Definition: gf2.c:772
void BPU_gf2SparseQcMatrixGetRow(BPU_T_GF2_Sparse_Poly *p, const BPU_T_GF2_Sparse_Qc_Matrix *m, int row_num)
Definition: gf2.c:852
void BPU_printBinaryMsbLn(uint32_t in, int len)
Print number as binary string in big endian so msb is first printed.
Definition: gf2.c:39
int BPU_gf2MatGetRowAsGf2Vec(BPU_T_GF2_Vector *out, const BPU_T_GF2_Matrix *in, int row)
Definition: gf2.c:444
void BPU_gf2PolySetDeg(BPU_T_GF2_Poly *a, int deg)
Definition: gf2.c:747
int BPU_gf2MatFindCol(const BPU_T_GF2_Matrix *mat, int i, int start_index)
Definition: gf2.c:366
void BPU_printGf2QcMatrix(const BPU_T_GF2_QC_Matrix *v)
Definition: gf2.c:205
int BPU_gf2PolyInitRand(BPU_T_GF2_Poly *out, int l, int w, int set_deg)
Definition: gf2.c:626
void BPU_printGf2PolyForMatrix(const BPU_T_GF2_Poly *v)
Definition: gf2.c:165
int BPU_gf2VecMulMat(BPU_T_GF2_Vector *x, const BPU_T_GF2_Vector *v, const BPU_T_GF2_Matrix *b)
Definition: gf2.c:495
uint8_t BPU_getParity(BPU_T_GF2 dword)
BPU_getParity Get parity of word.
Definition: gf2.c:576
void BPU_gf2SparseQcMatrixTransp(BPU_T_GF2_Sparse_Qc_Matrix *out, const BPU_T_GF2_Sparse_Qc_Matrix *in)
Definition: gf2.c:652
int BPU_gf2SparsePolyAndHW(const BPU_T_GF2_Poly *a, const BPU_T_GF2_Sparse_Poly *b)
Definition: gf2.c:839
int BPU_gf2VecConcat(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *vec1, const BPU_T_GF2_Vector *vec2)
Concats two vectors without allocation ouput.
Definition: gf2.c:399
void BPU_gf2PolyShiftLeft(BPU_T_GF2_Poly *a, int shift_count)
Definition: gf2.c:681
BPU_T_GF2 BPU_gf2VecGetMaskedBit(const BPU_T_GF2_Vector *vec, uint32_t bit)
Definition: gf2.c:314
int BPU_gf2MatMakeSystematic(BPU_T_GF2_Matrix *inout)
It brings Matrix GF2 into the systematic form -> with I on the left side.
Definition: gf2.c:376
void BPU_printGf2SparseQcMatrix(const BPU_T_GF2_Sparse_Qc_Matrix *v)
Definition: gf2.c:225
int BPU_gf2PolyIsZero(const BPU_T_GF2_Poly *a)
Definition: gf2.c:1087
void BPU_gf2MatSwapRows(BPU_T_GF2_Matrix *mat, int i, int j)
Definition: gf2.c:347
BPU_T_GF2 BPU_gf2MatGetMaskedBit(const BPU_T_GF2_Matrix *m, uint32_t row, uint32_t bit)
Definition: gf2.c:305
int BPU_gf2VecXor(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in)
Xor two Vectors GF2 and store result in first vector.
Definition: gf2.c:481
int BPU_gf2VecPermute(BPU_T_GF2_Vector *vec, const BPU_T_Perm_Vector *permutation)
Definition: gf2.c:289
void BPU_printGf2Poly(const BPU_T_GF2_Poly *v)
Definition: gf2.c:184
void BPU_printBinaryLsbLn(uint32_t in, int len)
Print number as binary string in little endian so lsb is first printed.
Definition: gf2.c:60
void BPU_gf2PolyDiv(BPU_T_GF2_Poly *q, BPU_T_GF2_Poly *r, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b)
Definition: gf2.c:912
void BPU_printBinary32LsbLn(uint32_t in)
Print number as binary string in little endian so lsb is first printed.
Definition: gf2.c:69
int BPU_gf2QcMatrixToSparse(BPU_T_GF2_Sparse_Qc_Matrix *out, const BPU_T_GF2_QC_Matrix *in, const int wi[])
Definition: gf2.c:599
void BPU_printGf2VecMsb(const BPU_T_GF2_Vector *v)
BPU_printGf2VecMsb Most significant bit is printed first.
Definition: gf2.c:122
void BPU_printGf2VecOnes(const BPU_T_GF2_Vector *vec)
BPU_printGf2VecOnes Print only ones.
Definition: gf2.c:144