-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPaxos.h
566 lines (459 loc) · 16.6 KB
/
Paxos.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#pragma once
#include <cassert>
#include <vector>
#include <algorithm>
#include <ostream>
#include <memory>
#include <numeric>
#include <iomanip>
#include <cmath>
#include "Defines.h"
#include <cryptoTools/Common/Log.h>
#include <cryptoTools/Common/Timer.h>
#include <cryptoTools/Crypto/AES.h>
#include <cryptoTools/Crypto/PRNG.h>
#include <cryptoTools/Crypto/RandomOracle.h>
#include <libOTe/Tools/LDPC/Mtx.h>
#include "PxUtil.h"
namespace volePSI
{
struct PaxosParam {
// the type of dense columns.
enum DenseType
{
Binary,
GF128
};
u64 mSparseSize = 0,
mDenseSize = 0,
mWeight = 0,
mG = 0,
mSsp = 40;
DenseType mDt = GF128;
PaxosParam() = default;
PaxosParam(const PaxosParam&) = default;
PaxosParam& operator=(const PaxosParam&) = default;
PaxosParam(u64 numItems, u64 weight = 3, u64 ssp = 40, DenseType dt = DenseType::GF128)
{
init(numItems, weight, ssp, dt);
}
// computes the paxos parameters based the parameters.
void init(u64 numItems, u64 weight = 3, u64 ssp = 40, DenseType dt = DenseType::GF128);
// the size of the paxos data structure.
u64 size() const
{
return mSparseSize + mDenseSize;
}
};
// The core Paxos algorithm. The template parameter
// IdxType should be in {u8,u16,u32,u64} and large
// enough to fit the paxos size value.
template<typename IdxType>
class Paxos : public PaxosParam, public oc::TimerAdapter
{
public:
// the number of items to be encoded.
IdxType mNumItems = 0;
// the encoding/decoding seed.
block mSeed;
bool mVerbose = false;
bool mDebug = false;
// when decoding, add the decoded value to the
// output, as opposed to overwriting.
bool mAddToDecode = false;
// the method for generating the row data based on the input value.
PaxosHash<IdxType> mHasher;
// an allocate used for the encoding algorithm
std::unique_ptr<u8[]> mAllocation;
u64 mAllocationSize = 0;
// The dense part of the paxos matrix
span<block> mDense;
// The sparse part of the paxos matrix
MatrixView<IdxType> mRows;
// the sparse columns of the matrix
span<span<IdxType>> mCols;
// the memory used to store the column data.
span<IdxType> mColBacking;
// A data structure used to track the current weight of the rows.s
WeightData<IdxType> mWeightSets;
Paxos() = default;
Paxos(const Paxos&) = default;
Paxos(Paxos&&) = default;
Paxos& operator=(const Paxos&) = default;
Paxos& operator=(Paxos&&) = default;
// initialize the paxos with the given parameters.
void init(u64 numItems, u64 weight, u64 ssp, PaxosParam::DenseType dt, block seed)
{
PaxosParam p(numItems, weight, ssp, dt);
init(numItems, p, seed);
}
// initialize the paxos with the given parameters.
void init(u64 numItems, PaxosParam p, block seed);
// solve/encode the given inputs,value pair. The paxos data
// structure is written to output. input,value should be numItems
// in size, output should be Paxos::size() in size. If the paxos
// should be randomized, then provide a PRNG.
template<typename ValueType>
void solve(span<const block> inputs, span<const ValueType> values, span<ValueType> output, oc::PRNG* prng = nullptr)
{
setInput(inputs);
encode<ValueType>(values, output, prng);
}
// solve/encode the given inputs,value pair. The paxos data
// structure is written to output. input,value should have numItems
// rows, output should have Paxos::size() rows. All should have the
// same number of columns. If the paxos should be randomized, then
// provide a PRNG.
template<typename ValueType>
void solve(span<const block> inputs, MatrixView<const ValueType> values, MatrixView<ValueType> output, oc::PRNG* prng = nullptr)
{
setInput(inputs);
encode<ValueType>(values, output, prng);
}
// set the input keys which define the paxos matrix. After that,
// encode can be called more than once.
void setInput(span<const block> inputs);
// encode the given inputs,value pair based on the already set input. The paxos data
// structure is written to output. input,value should be numItems
// in size, output should be Paxos::size() in size. If the paxos
// should be randomized, then provide a PRNG.
template<typename ValueType>
void encode(span<const ValueType> values, span<ValueType> output, oc::PRNG* prng = nullptr)
{
PxVector<const ValueType> V(values);
PxVector<ValueType> P(output);
auto h = P.defaultHelper();
encode(V, P, h, prng);
}
// encode the given inputs,value pair based on the already set input. The paxos data
// structure is written to output. input,value should have numItems
// rows, output should have Paxos::size() rows. All should have the
// same number of columns. If the paxos should be randomized, then
// provide a PRNG.
template<typename ValueType>
void encode(MatrixView<const ValueType> values, MatrixView<ValueType> output, oc::PRNG* prng = nullptr)
{
if (values.cols() != output.cols())
throw RTE_LOC;
if (values.cols() == 1)
{
// reduce matrix to span if possible.
encode(span<const ValueType>(values), span<ValueType>(output), prng);
}
else if (
values.cols() * sizeof(ValueType) % sizeof(block) == 0 &&
std::is_same<ValueType, block>::value == false)
{
// reduce ValueType to block if possible.
auto n = values.rows();
auto m = values.cols() * sizeof(ValueType) / sizeof(block);
encode<block>(
MatrixView<const block>((block*)values.data(), n, m),
MatrixView<block>((block*)output.data(), n, m),
prng);
}
else
{
PxMatrix<const ValueType> V(values);
PxMatrix<ValueType> P(output);
auto h = P.defaultHelper();
encode(V, P, h, prng);
}
}
// encode the given input with the given paxos p. Vec and ConstVec should
// meet the PxVector concept... Helper used to perform operations on values.
template<typename Vec, typename ConstVec, typename Helper>
void encode(ConstVec& values, Vec& output, Helper& h, oc::PRNG* prng = nullptr);
// Decode the given input based on the data paxos structure p. The
// output is written to values.
template<typename ValueType>
void decode(span<const block> input, span<ValueType> values, span<const ValueType> p);
// Decode the given input based on the data paxos structure p. The
// output is written to values. values and p should have the same
// number of columns.
template<typename ValueType>
void decode(span<const block> input, MatrixView<ValueType> values, MatrixView<const ValueType> p);
// decode the given input with the given paxos p. Vec and ConstVec should
// meet the PxVector concept... Helper used to perform operations on values.
template<typename Helper, typename Vec, typename ConstVec>
void decode(span<const block> input, Vec& values, ConstVec& p, Helper& h);
struct Triangulization
{
PaxosPermutation<IdxType> mPerm;
u64 mGap = 0;
oc::SparseMtx mH;
oc::SparseMtx getA() const;
oc::SparseMtx getC() const;
oc::SparseMtx getB() const;
oc::SparseMtx getD() const;
oc::SparseMtx getE() const;
oc::SparseMtx getF() const;
};
// returns a printable version of the paxos matrix after
// beign triangulized. setInput(...) should be called first.
Triangulization getTriangulization();
////////////////////////////////////////
// private functions
////////////////////////////////////////
// allocate the memory needed to triangulate.
void allocate();
// decodes 32 instances. rows should contain the row indicies, dense the dense
// part. values is where the values are written to. p is the Paxos, h is the value op. helper.
template<typename ValueType, typename Helper, typename Vec>
void decode32(const IdxType* rows, const block* dense, ValueType* values, Vec& p, Helper& h);
// decodes 8 instances. rows should contain the row indicies, dense the dense
// part. values is where the values are written to. p is the Paxos, h is the value op. helper.
template<typename ValueType, typename Helper, typename Vec>
void decode8(const IdxType* rows, const block* dense, ValueType* values, Vec& p, Helper& h);
// decodes one instances. rows should contain the row indicies, dense the dense
// part. values is where the values are written to. p is the Paxos, h is the value op. helper.
template<typename ValueType, typename Helper, typename Vec>
void decode1(
const IdxType* rows,
const block* dense,
ValueType* values,
Vec& p,
Helper& h);
// manually set the row indicies and the dense values.
void setInput(MatrixView<IdxType> rows, span<block> dense);
// manually set the row indicies and the dense values. In
// addition, provide the memory that us needed to to perform
// encoding.
void setInput(
MatrixView<IdxType> rows,
span<block> dense,
span<span<IdxType>> cols,
span<IdxType> colBacking,
span<IdxType> colWeights);
// perform the tringulization algorithm for encoding. This
// populates mainRows,mainCols with the rows/columns of C
// gapRows are all the rows that are in the gap.
void triangulate(
std::vector<IdxType>& mainRows,
std::vector<IdxType>& mainCols,
std::vector<std::array<IdxType, 2>>& gapRows);
// once triangulated, this is used to assign values
// to output (paxos).
template<typename Vec, typename ConstVec, typename Helper>
void backfill(
span<IdxType> mainRows,
span<IdxType> mainCols,
span<std::array<IdxType, 2>> gapRows,
ConstVec& values,
Vec& output,
Helper& h,
oc::PRNG* prng);
// once triangulated, this is used to assign values
// to output (paxos). Use the gf128 dense algorithm.
template<typename Vec, typename ConstVec, typename Helper>
void backfillGf128(
span<IdxType> mainRows,
span<IdxType> mainCols,
span<std::array<IdxType, 2>> gapRows,
ConstVec& values,
Vec& output,
Helper& h,
oc::PRNG* prng);
// once triangulated, this is used to assign values
// to output (paxos). Use the classic binary dense algorithm.
template<typename Vec, typename ConstVec, typename Helper>
void backfillBinary(
span<IdxType> mainRows,
span<IdxType> mainCols,
span<std::array<IdxType, 2>> gapRows,
ConstVec& values,
Vec& output,
Helper&h,
oc::PRNG* prng);
// helper function used for getTriangulization();
std::pair<PaxosPermutation<IdxType>, u64> computePermutation(
span<IdxType> mainRows,
span<IdxType> mainCols,
span<std::array<IdxType, 2>> gapRows,
bool withDense);
// helper function used for getTriangulization(); returns
// the rows/columns permuted by perm.
oc::SparseMtx getH(PaxosPermutation<IdxType>& perm) const;
// helper function that generates the column data given that
// the row data has been populated (via setInput(...)).
void rebuildColumns(span<IdxType> colWeights, u64 totalWeight);
// A sparse representation of the F * C^-1 matrix.
struct FCInv
{
FCInv(u64 n)
: mMtx(n)
{}
std::vector<std::vector<IdxType>> mMtx;
};
// returns the sparse representation of the F * C^-1 matrix.
FCInv getFCInv(
span<IdxType> mainRows,
span<IdxType> mainCols,
span<std::array<IdxType, 2>> gapRows) const;
// returns which columns are used for the gap. This
// is only used for binary dense method.
std::vector<u64> getGapCols(
FCInv& fcinv,
span<std::array<IdxType, 2>> gapRows) const;
// returns x2' = x2 - D' r - FC^-1 x1
template<typename Vec, typename ConstVec, typename Helper>
Vec getX2Prime(
FCInv &fcinv,
span<std::array<IdxType, 2>> gapRows,
span<u64> gapCols,
const ConstVec& X,
const Vec& P,
Helper& h);
// returns E' = -FC^-1B + E
oc::DenseMtx getEPrime(
FCInv &fcinv,
span<std::array<IdxType, 2>> gapRows,
span<u64> gapCols);
template<typename Vec, typename Helper>
void randomizeDenseCols(Vec&, Helper&, span<u64> gapCols, oc::PRNG* prng);
};
// a binned version of paxos. Internally calls paxos.
class Baxos
{
public:
u64 mNumItems = 0, mNumBins = 0, mItemsPerBin = 0, mWeight = 0, mSsp = 0;
// the parameters used on a single bim.
PaxosParam mPaxosParam;
block mSeed;
// when decoding, add the decoded value to the
// output, as opposed to overwriting.
bool mAddToDecode = false;
// initialize the paxos with the given parameter.
void init(u64 numItems, u64 binSize, u64 weight, u64 ssp, PaxosParam::DenseType dt, block seed)
{
mNumItems = numItems;
mWeight = weight;
mNumBins = (numItems + binSize - 1) / binSize;
mItemsPerBin = getBinSize(mNumBins, mNumItems, ssp + std::log2(mNumBins));
mSsp = ssp;
mSeed = seed;
mPaxosParam.init(mItemsPerBin, weight, ssp, dt);
}
// solve the system for the given input vectors.
// inputs are the keys
// values are the desired values that inputs should decode to.
// output is the paxos.
// prng should be non-null if randomized paxos is desired.
template<typename ValueType>
void solve(
span<const block> inputs,
span<const ValueType> values,
span<ValueType> output,
oc::PRNG* prng = nullptr,
u64 numThreads = 0);
// solve the system for the given input matrices.
// inputs are the keys
// values are the desired values that inputs should decode to.
// output is the paxos.
// prng should be non-null if randomized paxos is desired.
template<typename ValueType>
void solve(
span<const block> inputs,
MatrixView<const ValueType> values,
MatrixView<ValueType> output,
oc::PRNG* prng = nullptr,
u64 numThreads = 0);
// solve/encode the system.
template<typename Vec, typename ConstVec, typename Helper>
void solve(
span<const block> inputs,
ConstVec& values,
Vec& output,
oc::PRNG* prng,
u64 numThreads,
Helper& h);
// decode a single input given the paxos p.
template<typename ValueType>
ValueType decode(const block& input, span<const ValueType> p)
{
ValueType r;
decode(span<const block>(&input, 1), span<ValueType>(&r, 1), p);
return r;
}
// decode the given input vector and write the result to values.
// inputs are the keys.
// values are the output.
// p is the paxos vector.
template<typename ValueType>
void decode(span<const block> input, span<ValueType> values, span<const ValueType> p, u64 numThreads = 0);
// decode the given input matrix and write the result to values.
// inputs are the keys.
// values are the output.
// p is the paxos matrix.
template<typename ValueType>
void decode(span<const block> input, MatrixView<ValueType> values, MatrixView<const ValueType> p, u64 numThreads = 0);
template<typename Vec, typename ConstVec, typename Helper>
void decode(
span<const block> inputs,
Vec& values,
ConstVec& p,
Helper& h,
u64 numThreads);
//////////////////////////////////////////
// private impl
//////////////////////////////////////////
// solve/encode the system.
template<typename IdxType, typename Vec, typename ConstVec, typename Helper>
void implParSolve(
span<const block> inputs,
ConstVec& values,
Vec& output,
oc::PRNG* prng,
u64 numThreads,
Helper& h);
// create the desired number of threads and split up the work.
template<typename IdxType, typename Vec, typename ConstVec, typename Helper>
void implParDecode(
span<const block> inputs,
Vec& values,
ConstVec& p,
Helper& h,
u64 numThreads);
// decode the given inputs based on the paxos p. The output is written to values.
template<typename IdxType, typename Vec, typename ConstVec, typename Helper>
void implDecodeBatch(span<const block> inputs, Vec& values, ConstVec& p, Helper& h);
// decode the given inputs based on the paxos p. The output is written to values.
// this differs from implDecode in that all inputs must be for the same paxos bin.
template<typename IdxType, typename Vec, typename ConstVec, typename Helper>
void implDecodeBin(
u64 binIdx,
span<block> hashes,
Vec& values,
Vec& valuesBuff,
span<u64> inIdxs,
ConstVec& p,
Helper& h,
Paxos<IdxType>& paxos);
// the size of the paxos.
u64 size()
{
return u64(mNumBins * (mPaxosParam.mSparseSize + mPaxosParam.mDenseSize));
}
static u64 getBinSize(u64 numBins, u64 numItems, u64 ssp);
u64 binIdxCompress(const block& h)
{
return (h.get<u64>(0) ^ h.get<u64>(1) ^ h.get<u32>(3));
}
u64 modNumBins(const block& h)
{
return binIdxCompress(h) % mNumBins;
}
};
// invert a gf128 matrix.
Matrix<block> gf128Inv(Matrix<block> mtx);
// multiply two gf128 matricies.
Matrix<block> gf128Mul(const Matrix<block>& m0, const Matrix<block>& m1);
template<typename IdxType>
std::ostream& operator<<(std::ostream& o, const Paxos<IdxType>& p);
//template<typename IdxType>
//std::ostream& operator<<(std::ostream& o, const PaxosDiff<IdxType>& s);
}
// Since paxos is a template, we include the impl file.
#ifndef NO_INCLUDE_PAXOS_IMPL
#include "PaxosImpl.h"
#endif // !NO_INCLUDE_PAXOS_IMPL