forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitVector.h
327 lines (285 loc) · 7.95 KB
/
BitVector.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#ifndef _BITVECTOR
#define _BITVECTOR
/* Vector of bits */
#include <iostream>
#include <vector>
using namespace std;
#include <stdlib.h>
#include <assert.h>
#include "Tools/Exceptions.h"
#include "Networking/data.h"
// just for util functions
#include "Math/gf2nlong.h"
#include "Math/FixedVec.h"
#include "Tools/intrinsics.h"
class PRNG;
class octetStream;
template <int K>
class Z2;
template <class U, class V>
class Rectangle;
class BitVector
{
octet* bytes;
size_t nbytes;
size_t nbits;
public:
void assign(const BitVector& K);
void assign_bytes(char* new_bytes, int len)
{
resize(len*8);
memcpy(bytes, new_bytes, len);
}
void assign_zero()
{
memset(bytes, 0, nbytes);
}
// only grows, never destroys
void resize(size_t new_nbits)
{
if (nbits != new_nbits)
{
int new_nbytes = DIV_CEIL(new_nbits,8);
if (nbits < new_nbits)
{
octet* tmp = new octet[new_nbytes];
memcpy(tmp, bytes, nbytes);
delete[] bytes;
bytes = tmp;
}
nbits = new_nbits;
nbytes = new_nbytes;
/*
// use realloc to preserve original contents
if (new_nbits < nbits)
{
memcpy(tmp, bytes, new_nbytes);
}
else
{
memset(tmp, 0, new_nbytes);
memcpy(tmp, bytes, nbytes);
}*/
// realloc may fail on size 0
/*if (new_nbits == 0)
{
free(bytes);
bytes = (octet*) malloc(0);//new octet[0];
//free(bytes);
return;
}
bytes = (octet*)realloc(bytes, nbytes);
if (bytes == NULL)
{
cerr << "realloc failed\n";
exit(1);
}*/
/*delete[] bytes;
nbits = new_nbits;
nbytes = DIV_CEIL(nbits, 8);
bytes = new octet[nbytes];*/
}
}
void resize_zero(size_t new_nbits);
unsigned int size() const { return nbits; }
unsigned int size_bytes() const { return nbytes; }
octet* get_ptr() { return bytes; }
const octet* get_ptr() const { return bytes; }
const void* get_ptr_to_byte(size_t i, size_t block_size) const;
const void* get_ptr_to_bit(size_t i, size_t block_size) const;
BitVector(size_t n=0)
{
nbits = n;
nbytes = DIV_CEIL(nbits, 8);
bytes = new octet[nbytes];
assign_zero();
}
BitVector(const BitVector& K)
{
bytes = new octet[K.nbytes];
nbytes = K.nbytes;
nbits = K.nbits;
assign(K);
}
BitVector(const void* other, size_t bitsize) : BitVector()
{
resize(bitsize);
avx_memcpy(bytes, other, nbytes);
}
~BitVector() {
//cout << "Destroy, size = " << nbytes << endl;
delete[] bytes;
}
BitVector& operator=(const BitVector& K)
{
if (this!=&K) { assign(K); }
return *this;
}
BitVector& operator=(const octetStream other);
void swap(BitVector& other)
{
std::swap(nbits, other.nbits);
std::swap(nbytes, other.nbytes);
std::swap(bytes, other.bytes);
}
class Access
{
BitVector& v;
int i;
public:
Access(BitVector& v, int i) : v(v), i(i) {}
bool get() const { return v.get_bit(i); }
void operator=(bool b) { v.set_bit(i, b); }
void operator=(const Access& other) { *this = other.get(); }
void operator^=(const Access& other) { *this = get() ^ other.get(); }
bool operator==(const Access& other) const { return get() == other.get(); }
operator bool() const { return get(); }
};
bool operator[](int i) const { return get_bit(i); }
Access operator[](int i) { return {*this, i}; }
octet get_byte(int i) const { return bytes[i]; }
void set_byte(int i, octet b) { bytes[i] = b; }
// get the i-th 64-bit word
word get_word(int i) const { return *(word*)(bytes + i*8); }
void set_word(int i, word w)
{
int offset = i * sizeof(word);
memcpy(bytes + offset, (octet*)&w, sizeof(word));
}
int128 get_int128(int i) const { return _mm_loadu_si128((__m128i*)bytes + i); }
void set_int128(int i, int128 a) { *((__m128i*)bytes + i) = a.a; }
template <class T>
T get_portion(int i) const;
template <class T>
void set_portion(int i, const T& a);
template <class T>
void set(const T& a);
template <class T, int L>
void set(const FixedVec<T, L>& a);
bool get_bit(int i) const
{
#ifdef CHECK_SIZE
if (i >= (int)nbits)
throw out_of_range("BitVector access: " + to_string(i) + "/" + to_string(nbits));
#endif
return (bytes[i/8] >> (i % 8)) & 1;
}
void set_bit(int i,unsigned int a)
{
if ((size_t)i >= nbits)
throw overflow("BitVector", i, nbits);
int j = i/8, k = i&7;
if (a==1)
{ bytes[j] |= (octet)(1UL<<k); }
else
{ bytes[j] &= (octet)~(1UL<<k); }
}
void add(const BitVector& A, const BitVector& B)
{
if (A.nbits != B.nbits)
{ throw invalid_length(); }
resize(A.nbits);
for (unsigned int i=0; i < nbytes; i++)
{
bytes[i] = A.bytes[i] ^ B.bytes[i];
}
}
void add(const BitVector& A)
{
if (nbits != A.nbits)
{ throw invalid_length(); }
int nwords = nbytes / 8;
for (int i = 0; i < nwords; i++)
((word*)bytes)[i] ^= ((word*)A.bytes)[i];
for (unsigned int i = nwords * 8; i < nbytes; i++)
{
bytes[i] ^= A.bytes[i];
}
}
BitVector operator&(const BitVector& other) const;
bool parity() const;
bool equals(const BitVector& K) const
{
if (nbits != K.nbits)
{ throw invalid_length(); }
for (unsigned int i = 0; i < nbytes; i++)
{ if (bytes[i] != K.bytes[i]) { return false; } }
return true;
}
bool operator==(const BitVector& other)
{
return equals(other);
}
void append(const BitVector& other, size_t length);
void randomize(PRNG& G);
template <class T>
void randomize_blocks(PRNG& G);
// randomize bytes a, ..., a+nb-1
void randomize_at(int a, int nb, PRNG& G);
void output(ostream& s,bool human) const;
void input(istream& s,bool human);
// Pack and unpack in native format
// i.e. Dont care about conversion to human readable form
void pack(octetStream& o) const;
void unpack(octetStream& o);
string str(size_t end = SIZE_MAX) const
{
stringstream ss;
ss << hex;
for(size_t i(0);i < min(nbytes, end);++i)
ss << (int)bytes[i] << " ";
return ss.str();
}
};
template <class T>
T inline BitVector::get_portion(int i) const
{
if (T::size_in_bits() == 1)
return get_bit(i);
else
{
T res;
res.assign(&bytes[T::size() * i]);
return res;
}
}
template <class T>
void inline BitVector::set_portion(int i, const T& a)
{
memcpy(bytes + a.size() * i, a.get_ptr(), a.size());
}
template <class T, int L>
void BitVector::set(const FixedVec<T, L>& a)
{
resize(8 * a.size());
size_t base = 0;
for (int i = 0; i < L; i++)
{
memcpy(bytes + base, a[i].get_ptr(), a[i].size());
base += a[i].size();
}
}
template <class T>
void inline BitVector::set(const T& a)
{
resize(8 * a.size());
memcpy(bytes, a.get_ptr(), a.size());
}
template<class T>
inline void BitVector::randomize_blocks(PRNG& G)
{
if (T::size_in_bits() == 1)
{
G.get_octets(bytes, nbytes);
}
else
{
T tmp;
for (size_t i = 0; i < (nbytes / T::size()); i++)
{
tmp.randomize(G);
memcpy(bytes + i * T::size(), tmp.get_ptr(), T::size());
}
}
}
#endif