forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subroutines.h
159 lines (128 loc) · 4.5 KB
/
Subroutines.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
#ifndef _Subroutines
#define _Subroutines
/* Defines subroutines for use in both KeyGen and Offline phase
* Mainly focused around commiting and decommitting to various
* bits of data
*/
#include "Tools/random.h"
#include "Networking/Player.h"
#include "Tools/Commit.h"
#include "Subroutines.hpp"
/* Run just the Open Protocol for data[i][j] of type octetStream
* 0 <= i < num_runs
* 0 <= j < num_players
* On output data[i][j] contains all the data
* If dont!=-1 then dont open this run
*/
void Open(vector< vector<octetStream> >& data,
const vector< vector<octetStream> >& Comm_data,
const vector<octetStream>& My_Open_data,
const Player& P,int num_runs,int dont=-1);
/* This one takes a vector open which contains 0 and 1
* If 1 then we open this value, otherwise we do not
*/
void Open(vector< vector<octetStream> >& data,
const vector< vector<octetStream> >& Comm_data,
const vector<octetStream>& My_Open_data,
const vector<int> open,
const Player& P,int num_runs);
/* This runs the Commit and Open Protocol for data[i][j] of type T
* 0 <= i < num_runs
* 0 <= j < num_players
* On input data[i][j] is only defined for j=my_number
*/
template<class T>
void Commit_And_Open(vector< vector<T> >& data,const Player& P,int num_runs);
template<class T>
void Commit_And_Open(vector<T>& data,const Player& P);
void Commit_And_Open_(vector<octetStream>& datas,const Player& P);
template<class T>
void Commit_And_Open(vector<T>& data,const Player& P)
{
vector<octetStream> datas(P.num_players());
data[P.my_num()].pack(datas[P.my_num()]);
Commit_And_Open_(datas, P);
for (int i = 0; i < P.num_players(); i++)
data[i].unpack(datas[i]);
}
template<class T>
void Transmit_Data(vector< vector<T> >& data,const Player& P,int num_runs);
/* Functions to Commit and Open a Challenge Value */
void Commit_To_Challenge(vector<unsigned int>& e,
vector<octetStream>& Comm_e,vector<octetStream>& Open_e,
const Player& P,int num_runs);
int Open_Challenge(vector<unsigned int>& e,vector<octetStream>& Open_e,
const vector<octetStream>& Comm_e,
const Player& P,int num_runs);
/* Function to create a shared random value for T=gfp/gf2n */
template<class T>
void Create_Random(T& ans,const Player& P);
template<class T>
T Create_Random(const Player& P)
{
T res;
Create_Random(res, P);
return res;
}
/* Produce a random seed of length len */
void Create_Random_Seed(octet* seed,const PlayerBase& P,int len);
/* Functions to Commit to Seed Values
* This also initialises the PRNG's in G
*/
void Commit_To_Seeds(vector<PRNG>& G,
vector< vector<octetStream> >& seeds,
vector< vector<octetStream> >& Comm_seeds,
vector<octetStream>& Open_seeds,
const Player& P,int num_runs);
/* Run just the Commit Protocol for data[i][j] of type T
* 0 <= i < num_runs
* 0 <= j < num_players
* On input data[i][j] is only defined for j=my_number
*/
template<class T>
void Commit(vector< vector<octetStream> >& Comm_data,
vector<octetStream>& Open_data,
const vector< vector<T> >& data,const Player& P,int num_runs)
{
octetStream os;
int my_number=P.my_num();
for (int i=0; i<num_runs; i++)
{ os.reset_write_head();
data[i][my_number].pack(os);
Comm_data[i].resize(P.num_players());
Commit(Comm_data[i][my_number],Open_data[i],os,my_number);
P.Broadcast_Receive(Comm_data[i]);
}
}
/* Run just the Open Protocol for data[i][j] of type T
* 0 <= i < num_runs
* 0 <= j < num_players
* On output data[i][j] contains all the data
* If dont!=-1 then dont open this run
*/
template<class T>
void Open(vector< vector<T> >& data,
const vector< vector<octetStream> >& Comm_data,
const vector<octetStream>& My_Open_data,
const Player& P,int num_runs,int dont=-1)
{
octetStream os;
int my_number=P.my_num();
int num_players=P.num_players();
vector<octetStream> Open_data(num_players);
for (int i=0; i<num_runs; i++)
{ if (i!=dont)
{ Open_data[my_number]=My_Open_data[i];
P.Broadcast_Receive(Open_data);
for (int j=0; j<num_players; j++)
{ if (j!=my_number)
{ if (!Open(os,Comm_data[i][j],Open_data[j],j))
{ throw invalid_commitment(); }
os.reset_read_head();
data[i][j].unpack(os);
}
}
}
}
}
#endif