BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
perm.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 "perm.h"
23 #include "int.h"
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 
28 #include <bitpunch/prng/prng.h>
29 #include <bitpunch/debugio.h>
30 
31 #ifdef BPU_CONF_PRINT
32 void BPU_printPerm(const BPU_T_Perm_Vector *permutation) {
33  int i;
34 
35  fprintf(stderr, "Perm (%4d): ", permutation->size);
36  for (i = 0; i < permutation->size; i++) {
37  fprintf(stderr, "%i ",permutation->elements[i]);
38  }
39  fprintf(stderr, "\n");
40 }
41 #endif // BPU_CONF_PRINT
42 
44  int i;
45  uint32_t rand_value;
46 
47  for (i = 0; i < permutation->size; i++) {
48  permutation->elements[i] = i;
49  }
50  for (i = 0; i < permutation->size; i++) {
51  rand_value = BPU_prngGetRand(i, permutation->size);
52  BPU_permSwap(&permutation->elements[i], &permutation->elements[rand_value]);
53  }
54  return 0;
55 }
56 
59  tmp = *a;
60  *a = *b;
61  *b = tmp;
62 }
63 
65  int i;
66 
67  if (out->size != in->size) {
68  BPU_printError("permutation size error");
69 
70  return -1;
71  }
72  for (i = 0; i < in->size; i++) {
73  out->elements[in->elements[i]] = i;
74  }
75  return 0;
76 }
77 
78 
79 int BPU_permPermute(BPU_T_Perm_Vector *to_permute, const BPU_T_Perm_Vector *permutation) {
80  int i;
81  BPU_T_Perm_Vector *new_permutation;
82 
83  // check if the size is correct
84  if (to_permute->size != permutation->size){
85  return -1;
86  }
87  // allocate new permutation
88  if (BPU_permMalloc(&new_permutation, to_permute->size) != 0){
89  return -2;
90  }
91  // copy the permutation
92  for (i = 0; i < to_permute->size; i++) {
93  new_permutation->elements[i] = to_permute->elements[i];
94  }
95  // permute
96  for (i = 0; i < permutation->size; i++) { // row loop
97  to_permute->elements[i] = new_permutation->elements[permutation->elements[i]];
98  }
99  BPU_permFree(&new_permutation);
100 
101  return 0;
102 }
103 
105  int i, j;
106 
107  for (i = 0; i < p->size; i++) {
108  for (j = 0; j < p->size; j++) {
109  if (i != j && p->elements[i] == p->elements[j]) {
110  BPU_printError("permutation is not valid");
111 #ifdef BPU_CONF_PRINT
112  BPU_printPerm(p);
113 #endif
114  return 1;
115  }
116  }
117  }
118  return 0;
119 }
BPU_T_Perm_Element * elements
permutation vector
Definition: permtypes.h:32
void BPU_permFree(BPU_T_Perm_Vector **p)
Free dynamically or statically alocated permutation vector.
Definition: permtypes.c:27
int BPU_permPermute(BPU_T_Perm_Vector *to_permute, const BPU_T_Perm_Vector *permutation)
Definition: perm.c:79
void BPU_permSwap(BPU_T_Perm_Element *a, BPU_T_Perm_Element *b)
BPU_swap.
Definition: perm.c:57
uint32_t BPU_prngGetRand(int from, int to)
Get random unsigned int 32 value from given range (from <= return <= to)
Definition: prng.c:28
int BPU_permRandomize(BPU_T_Perm_Vector *permutation)
Definition: perm.c:43
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
void BPU_printPerm(const BPU_T_Perm_Vector *permutation)
Definition: perm.c:32
BPU_T_Perm_Element size
permutation size
Definition: permtypes.h:33
int BPU_permGetInv(BPU_T_Perm_Vector *out, const BPU_T_Perm_Vector *in)
Definition: perm.c:64
uint16_t BPU_T_Perm_Element
Definition of permutation element type.
Definition: permtypes.h:24
int BPU_permMalloc(BPU_T_Perm_Vector **p, int size)
Allocate permutation vector elements of size size.
Definition: permtypes.c:35
int BPU_permIsValid(const BPU_T_Perm_Vector *p)
BPU_permIsValid Check wheter is permutaion valid.
Definition: perm.c:104