forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OTExtension.h
118 lines (95 loc) · 3.26 KB
/
OTExtension.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
#ifndef _OTEXTENSION
#define _OTEXTENSION
#include "OT/BaseOT.h"
#include "Tools/Exceptions.h"
#include "Networking/Player.h"
#include "Tools/time-func.h"
#include <stdlib.h>
#include <assert.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
//#define OTEXT_TIMER
//#define OTEXT_DEBUG
class OTExtension
{
public:
map<string,long long> times;
OTExtension(const BaseOT& baseOT, TwoPartyPlayer* player, bool passive);
OTExtension(TwoPartyPlayer* player,
OT_ROLE role=BOTH,
bool passive=false)
: passive_only(passive), nbaseOTs(-1),
ot_role(role), player(player)
{
}
void init(const BitVector& baseReceiverInput,
const vector< array<BitVector, 2> >& baseSenderInput,
const vector<BitVector>& baseReceiverOutput)
{
nbaseOTs = baseReceiverInput.size();
this->baseReceiverInput = baseReceiverInput;
if (baseSenderInput.size() != baseReceiverOutput.size())
throw runtime_error("mismatch in number of base OTs");
assert(baseReceiverInput.size() == baseSenderInput.size());
G_sender.resize(nbaseOTs, vector<PRNG>(2));
G_receiver.resize(nbaseOTs);
// set up PRGs for expanding the seed OTs
for (int i = 0; i < nbaseOTs; i++)
{
assert(baseSenderInput.at(i).size() == 2);
assert(baseSenderInput.at(i)[0].size_bytes() >= AES_BLK_SIZE);
assert(baseSenderInput.at(i)[1].size_bytes() >= AES_BLK_SIZE);
assert(baseReceiverOutput.at(i).size_bytes() >= AES_BLK_SIZE);
if (ot_role & RECEIVER)
{
if (baseSenderInput[i][0].get_int128(0) == baseSenderInput[i][1].get_int128(0))
throw runtime_error("base sender outputs are the same");
G_sender[i][0].SetSeed(baseSenderInput[i][0].get_ptr());
G_sender[i][1].SetSeed(baseSenderInput[i][1].get_ptr());
}
if (ot_role & SENDER)
{
G_receiver[i].SetSeed(baseReceiverOutput[i].get_ptr());
}
#ifdef OTEXT_DEBUG
// sanity check for base OTs
vector<octetStream> os(2);
BitVector t0(128);
if (ot_role & RECEIVER)
{
// send both inputs to test
baseSenderInput[i][0].pack(os[0]);
baseSenderInput[i][1].pack(os[0]);
}
send_if_ot_receiver(player, os, ot_role);
if (ot_role & SENDER)
{
// sender checks results
t0.unpack(os[1]);
if (baseReceiverInput.get_bit(i) == 1)
t0.unpack(os[1]);
if (!t0.equals(baseReceiverOutput[i]))
{
cerr << "Incorrect base OT\n";
exit(1);
}
}
os[0].reset_write_head();
os[1].reset_write_head();
#endif
}
}
void set_role(OT_ROLE role) { ot_role = role; }
protected:
BitVector baseReceiverInput;
bool passive_only;
int nbaseOTs;
OT_ROLE ot_role;
TwoPartyPlayer* player;
vector< vector<PRNG> > G_sender;
vector<PRNG> G_receiver;
};
#endif