forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
octetStream.cpp
267 lines (213 loc) · 4.84 KB
/
octetStream.cpp
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
#include <fcntl.h>
#include <sodium.h>
#include "octetStream.h"
#include <string.h>
#include "Networking/sockets.h"
#include "Networking/ssl_sockets.h"
#include "Tools/Exceptions.h"
#include "Networking/data.h"
#include "Networking/Player.h"
#include "Networking/Exchanger.h"
#include "Math/bigint.h"
#include "Tools/time-func.h"
#include "Tools/FlexBuffer.h"
void octetStream::reset()
{
data = 0;
len = mxlen = ptr = 0;
}
void octetStream::clear()
{
if (data)
delete[] data;
data = 0;
len = mxlen = ptr = 0;
}
void octetStream::assign(const octetStream& os)
{
if (os.len>=mxlen)
{
if (data)
delete[] data;
mxlen=os.mxlen;
data=new octet[mxlen];
}
len=os.len;
memcpy(data,os.data,len*sizeof(octet));
ptr=os.ptr;
}
octetStream::octetStream(size_t maxlen)
{
mxlen=maxlen; len=0; ptr=0;
data=new octet[mxlen];
}
octetStream::octetStream(size_t len, const octet* source) :
octetStream(len)
{
append(source, len);
}
octetStream::octetStream(const string& other) :
octetStream(other.size(), (const octet*)other.data())
{
}
octetStream::octetStream(const octetStream& os)
{
mxlen=os.mxlen;
len=os.len;
data=new octet[mxlen];
memcpy(data,os.data,len*sizeof(octet));
ptr=os.ptr;
}
octetStream::octetStream(FlexBuffer& buffer)
{
mxlen = buffer.capacity();
len = buffer.size();
data = (octet*)buffer.data();
ptr = buffer.ptr - buffer.data();
buffer.reset();
}
string octetStream::str() const
{
return string((char*) get_data(), get_length());
}
void octetStream::hash(octetStream& output) const
{
assert(output.mxlen >= crypto_generichash_blake2b_BYTES_MIN);
crypto_generichash(output.data, crypto_generichash_BYTES_MIN, data, len, NULL, 0);
output.len=crypto_generichash_BYTES_MIN;
}
octetStream octetStream::hash() const
{
octetStream h(crypto_generichash_BYTES_MIN);
hash(h);
return h;
}
bigint octetStream::check_sum(int req_bytes) const
{
unsigned char hash[req_bytes];
crypto_generichash(hash, req_bytes, data, len, NULL, 0);
bigint ans;
bigintFromBytes(ans,hash,req_bytes);
// cout << ans << "\n";
return ans;
}
bool octetStream::equals(const octetStream& a) const
{
if (len!=a.len) { return false; }
return memcmp(data, a.data, len) == 0;
}
void octetStream::append_random(size_t num)
{
resize(len+num);
randombytes_buf(data+len, num);
len+=num;
}
void octetStream::concat(const octetStream& os)
{
resize(len+os.len);
memcpy(data+len,os.data,os.len*sizeof(octet));
len+=os.len;
}
void octetStream::store_bytes(octet* x, const size_t l)
{
resize(len+4+l);
encode_length(data+len,l,4); len+=4;
memcpy(data+len,x,l*sizeof(octet));
len+=l;
}
void octetStream::get_bytes(octet* ans, size_t& length)
{
length = get_int(4);
memcpy(ans, consume(length), length * sizeof(octet));
}
void octetStream::store(int l)
{
resize(len+4);
encode_length(data+len,l,4);
len+=4;
}
void octetStream::get(int& l)
{
l = get_int(4);
}
void octetStream::store(const bigint& x)
{
size_t num=numBytes(x);
resize(len+num+5);
(data+len)[0]=0;
if (x<0) { (data+len)[0]=1; }
len++;
encode_length(data+len,num,4); len+=4;
bytesFromBigint(data+len,x,num);
len+=num;
}
void octetStream::get(bigint& ans)
{
int sign = *consume(1);
if (sign!=0 && sign!=1) { throw bad_value(); }
long length = get_int(4);
if (length!=0)
{
bigintFromBytes(ans, consume(length), length);
if (sign)
mpz_neg(ans.get_mpz_t(), ans.get_mpz_t());
}
else
ans=0;
}
void octetStream::store(const string& str)
{
store(str.length());
append((const octet*) str.data(), str.length());
}
void octetStream::get(string& str)
{
size_t size;
get(size);
str.assign((const char*) consume(size), size);
}
template<class T>
void octetStream::exchange(T send_socket, T receive_socket, octetStream& receive_stream) const
{
Exchanger<T> exchanger(send_socket, *this, receive_socket, receive_stream);
while (exchanger.round())
;
}
void octetStream::input(istream& s)
{
size_t size;
s.read((char*)&size, sizeof(size));
if (not s.good())
throw IO_Error("not enough data");
resize_min(size);
s.read((char*)data, size);
len = size;
if (not s.good())
throw IO_Error("not enough data");
}
void octetStream::output(ostream& s)
{
s.write((char*)&len, sizeof(len));
s.write((char*)data, len);
}
ostream& operator<<(ostream& s,const octetStream& o)
{
for (size_t i=0; i<o.len; i++)
{ int t0=o.data[i]&15;
int t1=o.data[i]>>4;
s << hex << t1 << t0 << dec;
}
return s;
}
octetStreams::octetStreams(const Player& P)
{
reset(P);
}
void octetStreams::reset(const Player& P)
{
resize(P.num_players());
for (auto& o : *this)
o.reset_write_head();
}
template void octetStream::exchange(int, int, octetStream&) const;
template void octetStream::exchange(ssl_socket*, ssl_socket*, octetStream&) const;