-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.h
276 lines (242 loc) · 7.97 KB
/
base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* File: base.h
* Author: ph4r05
*
* Created on May 16, 2014, 2:06 PM
*/
#ifndef BASE_H
#define BASE_H
#include <iostream>
#include <iomanip>
#include <cstring>
#include <bitset>
#include <vector>
#include <random> // std::default_random_engine
#include <algorithm> // std::move_backward
#include <array>
#include <iterator>
#include <NTL/mat_GF2.h>
#include <NTL/vec_GF2.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
// Determine whether we are building for a 64-bit platform.
// _LP64: http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
#if defined(_M_X64) || defined(__amd64__) || defined(_LP64) || defined(_ILP64)
#define COMPILER_X64
#endif
// Define ulong type.
#if defined(COMPILER_X64)
// Compilation for 64 bit platform.
typedef unsigned long long ULONG;
#define SIZEOF_ULONG 8
#define FULL_ULONG 0xffffffffffffffffull
#define ULONG1 ((ULONG) 1ull)
#else
typedef unsigned long ULONG;
#define SIZEOF_ULONG 4
#define FULL_ULONG 0xfffffffful
#define ULONG1 1ul
#endif
// Shifts term to the right 8*shift, and takes the lower 8 bits (corresponds to the input
// representation with unsigned char).
#define MASK_TERM(trm, shift) (((trm)>>(8*(shift))) & 0xfful)
// Generates 8bit mask with the given offset (in terms of bytes)
#define GET_MASK(offset) (((ULONG) 0xfful) << (8*(offset)))
// Reads input (uchar) to the term on the given offset.
// Offset is in terms of bytes.
// Value produced is term as befure, but on the correct place input is substituted.
#define READ_TERM_1(trm, input, offset) (((trm) & (~(GET_MASK(offset)))) | (((ULONG)(input))<<((offset)*8)))
// Evaluate internal representation of a term, using correct size of an internal type.
#if defined(COMPILER_X64)
// Compilation for 64 bit platform.
#else
// Determine if long is of length 4 B
#if defined(__SIZEOF_LONG__) && __SIZEOF_LONG__ == 4
// Assume we are compiling for 32 bit platform, if macro for size of long is defined,
// and is of size 4 bytes, use macro to evaluate 4 B type.
#endif
#endif
// Using default implementation by function call.
// System is neither x86_64 nor GCC having __SIZEOF_LONG__ set to 4.
//#ifndef
//#define TERM_ITEM_EVAL_GENOME(trm, input)
//#endif
// Fast ceiling function for integers.
#define OWN_CEIL(x) ( (((int)(x)) < (x)) ? ((int)(x))+1 : ((int)(x)) )
#define OWN_FLOOR(x) ( (((int)(x)) < (x)) ? ((int)(x))-1 : ((int)(x)) )
// Maximal base size for FGb (number of polynomials).
#define FGb_MAXI_BASE 100000
/**
* Reads 8-bit buffer to the 64 bit buffer.
* iBuff has to be big enough to fit the input buffer.
*
* @param input input buffer to read.
* @param size size of the input buffer in bytes to read.
* @param iBuff destination buffer.
*/
void readUcharToUlong(const uchar * input, uint size, ULONG * iBuff);
/**
* Reads 64 bit buffer to the 8 bit buffer.
*
* @param output output buffer to write.
* @param size size of the input buffer to read in bytes.
* @param iBuff input buffer to read (from LSB).
*/
void readUlongToUchar(uchar * output, uint size, const ULONG * iBuff);
/**
* Fills given buffer with random values.
* @param buffer
* @param size
*/
void randomBuffer(uchar * buffer, uint size);
/**
* Computes Hamming weight of the numeric type.
* Nifty parallel counting.
*
* For more inspiration take a look at http://bisqwit.iki.fi/source/misc/bitcounting/
* @return
*/
template<class TestType>
uint hamming_weight_fast(TestType k){
TestType n = k;
const unsigned TEST_BITS = sizeof(TestType) * 8;
TestType m1 = (~(TestType)0) / 3; // Binary 01010101...
TestType m2 = (~(TestType)0) / 5; // Binary 00110011...
TestType m4 = (~(TestType)0) / 17; // Binary 00001111...
TestType h01 = (~(TestType)0) / 255; // Hex 0101...
n = (n & m1) + ((n >> 1) & m1);
n = (n & m2) + ((n >> 2) & m2);
n = (n & m4) + ((n >> 4) & m4);
return (n * h01) >> (TEST_BITS-8);
// ^incidentally same as n % 255
}
/**
* Computes Hamming weight of the numeric type.
* For more inspiration take a look at http://bisqwit.iki.fi/source/misc/bitcounting/
*
* @param N
* @return
*/
template<class T>
uint hamming_weight(T n){
uint result=0;
while(n){
result++;
n &= n-1; // Zero the lowest-order one-bit
}
return result;
}
/**
* Computes hamming weight of the numeric array
* @param n
* @param size
* @return
*/
template<class T>
uint hamming_weight_array(const T * arr, uint size){
uint result=0;
for(uint i=0; i<size; i++){
T n = arr[i];
while(n){
result++;
n &= n-1; // Zero the lowest-order one-bit
}
}
return result;
}
template<class T>
uint hamming_weight_array(const std::vector<T> & inp, uint offset, uint size){
uint result=0;
for(uint i=0; i<size; i++){
T n = inp[offset+i];
while(n){
result++;
n &= n-1; // Zero the lowest-order one-bit
}
}
return result;
}
template<class T>
void dumpHex(std::ostream & c, const std::vector<T> & inp, unsigned int size, bool endl=1) {
c << std::showbase // show the 0x prefix
<< std::internal // fill between the prefix and the number
<< std::setfill('0'); // fill with 0s
for(unsigned int i = 0; i < size; i++){
const ULONG toDisp = static_cast<const ULONG>(inp[i]);
if (toDisp==0){
c << std::hex << "0x" << std::setw(2 * sizeof(T)) << 0 << " ";
} else {
c << std::hex << std::setw(2 * sizeof(T)+2) << toDisp << " ";
}
}
if (endl){
c << std::endl;
}
}
template<class T>
void dumpHex(std::ostream & c, const T * inp, unsigned int size, bool endl=1) {
c << std::showbase // show the 0x prefix
<< std::internal // fill between the prefix and the number
<< std::setfill('0'); // fill with 0s
for(unsigned int i = 0; i < size; i++){
const ULONG toDisp = static_cast<const ULONG>(inp[i]);
if (toDisp==0){
c << std::hex << "0x" << std::setw(2 * sizeof(T)) << 0 << " ";
} else {
c << std::hex << std::setw(2 * sizeof(T)+2) << toDisp << " ";
}
}
if (endl){
c << std::endl;
}
}
template<class T>
void dumpBin(std::ostream & c, const T * inp, unsigned int size, bool endl=1) {
for(unsigned int i = 0; i < size; i++){
const ULONG toDisp = static_cast<const ULONG>(inp[i]);
std::bitset<sizeof(T)*8> x(toDisp);
c << std::dec << std::setw(8 * sizeof(T)) << x << "b ";
}
if (endl){
c << std::endl;
}
}
template<class T>
void dumpBin(std::ostream & c, const std::vector<T> & inp, unsigned int size, bool endl=1) {
for(unsigned int i = 0; i < size; i++){
const ULONG toDisp = static_cast<const ULONG>(inp[i]);
std::bitset<sizeof(T)*8> x(toDisp);
c << std::dec << std::setw(8 * sizeof(T)) << x << "b ";
}
if (endl){
c << std::endl;
}
}
inline void dumpUcharHex(std::ostream & c, const uchar* inp, unsigned int size, bool endl=1){ dumpHex(c, inp, size, endl); }
inline void dumpUlongHex(std::ostream & c, const ULONG* inp, unsigned int size, bool endl=1){ dumpHex(c, inp, size, endl); }
void dumpUchar (std::ostream & c, const uchar * inp, unsigned int size, bool endl=1);
/**
* Computes number of bits that matches.
*
* @param a
* @param b
* @param bitPosStart
* @param bitPosEnd
* @return
*/
uint numBitMatches(const uchar * a, const uchar * b, uint bitPosStart, uint bitPosEnd, uint offsetA=0, uint offsetB=0);
/**
* Solves linear system. A does not have to be a square matrix.
* M = [A|c].
*
* After algorithm finishes, M contains the solution in the last column.
*
* @param M Output matrix, solution.
* @param A Input matrix to solve. Linear variables.
* @param b Constant vector for the linear equations.
* @param w Maximal rank to go to.
* @return
*/
long gaussPh4r05(NTL::mat_GF2& M, const NTL::mat_GF2& A, const NTL::vec_GF2 & b, long w);
#endif /* BASE_H */