forked from PP-FM/ZKUNSAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
208 lines (169 loc) · 5.96 KB
/
main.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
#include <iostream>
#include "clause.h"
#include "clauseRAM.h"
#include "commons.h"
int port, party;
const int threads = 8;
int DEGREE = 4;
block *mac, *data;
uint64_t data_mac_pointer;
SVoleF2k <BoolIO<NetIO>> *svole;
F2kOSTriple <BoolIO<NetIO>> *ostriple;
uint64_t constant;
BoolIO <NetIO> *io;
int main(int argc, char **argv) {
constant = 1UL << (VAL_SZ - 2);
parse_party_and_port(argv, &party, &port);
BoolIO <NetIO> *ios[threads];
for (int i = 0; i < threads; ++i)
ios[i] = new BoolIO<NetIO>(new NetIO(party == ALICE ? nullptr : argv[3], port + i), party == ALICE);
char *prooffile = argv[4];
setup_zk_bool < BoolIO < NetIO >> (ios, threads, party);
ZKBoolCircExec <BoolIO<NetIO>> *exec = (ZKBoolCircExec < BoolIO < NetIO >> *)(CircuitExecution::circ_exec);
io = exec->ostriple->io;
ostriple = new F2kOSTriple <BoolIO<NetIO>>(party, exec->ostriple->threads, exec->ostriple->ios,
exec->ostriple->ferret, exec->ostriple->pool);
svole = ostriple->svole;
data_mac_pointer = 0;
uint64_t test_n = N_REG;
uint64_t mem_need = svole->byte_memory_need_inplace(test_n);
data = new block[PRE_F2K_BUFFER_MEM_SZ];
mac = new block[PRE_F2K_BUFFER_MEM_SZ];
svole->extend_inplace(data, mac, PRE_F2K_BUFFER_MEM_SZ);
cout << "----set up----" << endl;
GF2X P;
// random(P, 128);
SetCoeff(P, 128, 1);
SetCoeff(P, 7, 1);
SetCoeff(P, 2, 1);
SetCoeff(P, 1, 1);
SetCoeff(P, 0, 1);
GF2E::init(P);
int ncls = 0, nres = 0;
vector <CLS> clauses;
vector <SPT> supports;
vector <SPT> pivots;
if (party == ALICE) {
readproof(string(prooffile), DEGREE, clauses, supports, pivots, ncls, nres);
cout << string(prooffile) << endl;
cout << "----input proof----" << endl;
io->send_data(&nres, 4);
io->send_data(&ncls, 4);
io->send_data(&DEGREE, 4);
}
if (party == BOB) {
io->recv_data(&nres, 4);
io->recv_data(&ncls, 4);
io->recv_data(&DEGREE, 4);
clauses = vector<CLS>(ncls);
supports = vector<SPT>(ncls);
pivots = vector < vector < int64_t >> (ncls);
}
cout << "nres " << nres << endl;
cout << "ncls " << ncls << endl;
cout << "DEGREE " << DEGREE << endl;
//if ( ncls > 524287) return 0;
double cost_input = 0;
double cost_resolve = 0;
double cost_access = 0 ;
/*
* encode the formula and resolution proof
* */
auto timer_0 = chrono::high_resolution_clock::now();
vector<clause> raw_formula;
float delta = 0 ;
for (int i = 0; i < ncls; i++) {
delta = delta + 1;
if ((delta / ncls) > 0.1){
float progress = (float(i) / ncls);
delta = 0;
int barWidth = 70;
std::cout << "[";
int pos = barWidth * progress;
for (int i = 0; i < barWidth; ++i) {
if (i < pos) std::cout << "=";
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << "] " << int(progress * 100.0) << " %\r";
cout << endl;
}
vector <uint64_t> literals;
for (int64_t lit: clauses[i]) {
literals.push_back((wrap(lit)));
}
padding(literals);
clause c(literals);
raw_formula.push_back(c);
}
clauseRAM<BoolIO<NetIO>>* formula = new clauseRAM<BoolIO<NetIO>>(party, INDEX_SZ);
formula->init(raw_formula);
cout <<"finish input!\n";
auto timer_1 = chrono::high_resolution_clock::now();
cost_input = chrono::duration<double>(timer_1 - timer_0).count();
delta = 0;
for (int64_t i = ncls - nres; i < ncls; i++) {
delta = delta + 1;
if ((delta / nres) > 0.1){
float progress = (float(i) / ncls);
delta = 0;
int barWidth = 70;
std::cout << "[";
int pos = barWidth * progress;
for (int i = 0; i < barWidth; ++i) {
if (i < pos) std::cout << "=";
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << "] " << int(progress * 100.0) << " %\r";
cout << endl;
}
int64_t chain_length = 0L;
vector<uint64_t> pvt;
vector<Integer> chain;
if (party == ALICE) {
chain_length = supports[i].size();
io->send_data(&chain_length, 8);
}else{
io->recv_data(&chain_length, 8);
}
SPT s = supports[i];
PVT p = pivots[i];
if (party == BOB) {
for (int j = s.size(); j < chain_length; j++) {
s.push_back(0L);
}
for (int j = p.size(); j < chain_length - 1; j++) {
p.push_back(0L);
}
}
assert(s.size() == p.size() +1);
for (uint64_t index: s) {
chain.push_back(Integer(INDEX_SZ, index, ALICE));
}
pvt.push_back(0UL);
for(uint64_t pp: p){
pvt.push_back(pp);
}
auto cost = check_chain(chain, pvt, i, formula);
cost_resolve = cost_resolve + cost.second;
cost_access = cost_access + cost.first;
}
check_zero_MAC(zero_block, 1);
auto timer_4 = chrono::high_resolution_clock::now();
formula->check();
auto timer_5 = chrono::high_resolution_clock::now();
cost_access = cost_access + chrono::duration<double>(timer_1 - timer_0).count();
cout << "a "<< cost_access << " " << "r " << cost_resolve << " "<< "i "<< cost_input << " t "<< cost_access + cost_resolve + cost_input << endl;
bool cheat = finalize_zk_bool<BoolIO<NetIO>>();
if(cheat)error("cheat!\n");
uint64_t counter = 0;
for(int i = 0; i < threads; ++i) {
counter = ios[i]->counter + counter;
delete ios[i]->io;
delete ios[i];
}
cout << "communication:" << counter << endl;
cout << "----end----" << endl;
return 0;
}