forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitMatrix.hpp
175 lines (154 loc) · 3.97 KB
/
BitMatrix.hpp
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
/*
* BitMatrix.hpp
*
*/
#ifndef OT_BITMATRIX_HPP_
#define OT_BITMATRIX_HPP_
#include "BitMatrix.h"
#include "Tools/BitVector.h"
template <class U>
size_t Matrix<U>::vertical_size()
{
return squares.size() * U::N_ROWS;
}
template <class U>
bool Matrix<U>::operator==(Matrix<U>& other)
{
if (squares.size() != other.squares.size())
throw invalid_length("Matrix");
for (size_t i = 0; i < squares.size(); i++)
if (not(squares[i] == other.squares[i]))
return false;
return true;
}
template <class U>
bool Matrix<U>::operator!=(Matrix<U>& other)
{
return not (*this == other);
}
template <class U>
void Matrix<U>::randomize(PRNG& G)
{
for (size_t i = 0; i < squares.size(); i++)
squares[i].randomize(G);
}
template <class U>
void Matrix<U>::randomize(int row, PRNG& G)
{
for (size_t i = 0; i < squares.size(); i++)
squares[i].randomize(row, G);
}
template <class U>
void Matrix<U>::print_side_by_side(Matrix<U>& other)
{
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 64; j++)
cout << squares[0].get_bit(i,j);
cout << " ";
for (int j = 0; j < 64; j++)
cout << other.squares[0].get_bit(i,j);
cout << endl;
}
}
template <class U>
void Matrix<U>::print_conditional(BitVector& conditions)
{
for (int i = 0; i < 32; i++)
{
if (conditions.get_bit(i))
for (int j = 0; j < 65; j++)
cout << " ";
for (int j = 0; j < 64; j++)
cout << squares[0].get_bit(i,j);
if (!conditions.get_bit(i))
for (int j = 0; j < 65; j++)
cout << " ";
cout << endl;
}
}
template <class U>
void Matrix<U>::pack(octetStream& os) const
{
for (size_t i = 0; i < squares.size(); i++)
squares[i].pack(os);
}
template <class U>
void Matrix<U>::unpack(octetStream& os)
{
for (size_t i = 0; i < squares.size(); i++)
squares[i].unpack(os);
}
template <class U>
Slice<U>::Slice(U& bm, size_t start, size_t size) :
bm(bm), start(start)
{
end = start + size;
if (end > bm.squares.size())
{
stringstream ss;
ss << "Matrix slice (" << start << "," << end << ") larger than matrix (" << bm.squares.size() << ")";
throw invalid_argument(ss.str());
}
}
template <class U>
Slice<U>& Slice<U>::rsub(Slice<U>& other)
{
if (bm.squares.size() < other.end)
throw invalid_length("rsub");
for (size_t i = other.start; i < other.end; i++)
bm.squares[i].rsub(other.bm.squares[i]);
return *this;
}
template <class U>
Slice<U>& Slice<U>::sub(BitVector& other, int repeat)
{
if (end * U::PartType::n_columns() > other.size() * repeat)
throw invalid_length(to_string(U::PartType::n_columns()));
for (size_t i = start; i < end; i++)
{
if (repeat > 0)
bm.squares[i].sub(other.get_ptr_to_byte(i / repeat,
U::PartType::n_row_bytes()));
else
bm.squares[i].bit_sub(other, i * U::PartType::n_rows_allocated());
}
return *this;
}
template <class U>
void Slice<U>::randomize(int row, PRNG& G)
{
for (size_t i = start; i < end; i++)
bm.squares[i].randomize(row, G);
}
template <class U>
void Slice<U>::conditional_add(BitVector& conditions, U& other, bool useOffset)
{
for (size_t i = start; i < end; i++)
bm.squares[i].conditional_add(conditions, other.squares[i], useOffset * i);
}
template <class U>
template <class T>
void Slice<U>::print()
{
cout << "hex / value" << endl;
for (int i = 0; i < 16; i++)
{
cout << T(bm.squares[0].rows[i]) << endl;
}
cout << endl;
}
template <class U>
void Slice<U>::pack(octetStream& os) const
{
os.reserve(U::PartType::size() * (end - start));
for (size_t i = start; i < end; i++)
bm.squares[i].pack(os);
}
template <class U>
void Slice<U>::unpack(octetStream& os)
{
for (size_t i = start; i < end; i++)
bm.squares[i].unpack(os);
}
#endif /* OT_BITMATRIX_HPP_ */