github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/StadiumForWaterCarver/src/RemoteShuffler.cpp (about) 1 #include "RemoteShuffler.h" 2 3 #include "NIZKProof.h" 4 5 extern long mu; 6 extern long mu_h; 7 8 RemoteShuffler::RemoteShuffler(const vector<long> &config, 9 vector<vector<Cipher_elg> *> *ciphers, ElGammal *reenc_key, int m_in, int n_in, 10 bool owner) : config_(config), time_rw_p(0), time_rw_v(0), time_cm(0), 11 c(ciphers), C(nullptr), pi(nullptr), R(nullptr), m(m_in), n(n_in), time_p(0), 12 P(nullptr), owner_(owner), flow_flag_(false) 13 { 14 15 mu = 4; 16 mu_h = 2 * mu - 1; 17 18 m_r_ = m / mu; 19 20 flow_flag_ = (m_r_ == 4); 21 key = new ElGammal(*reenc_key); 22 permute_and_reencrypt(reenc_key); 23 } 24 25 RemoteShuffler::RemoteShuffler(const vector<long> &config, 26 vector<vector<Cipher_elg> *> *ciphers, ElGammal *reenc_key, int m_in, int n_in, 27 vector<vector<vector<long> *> *> *pi_in, vector<vector<ZZ> *> *R_in, 28 bool owner) : config_(config), time_rw_p(0), time_rw_v(0), time_cm(0), 29 c(ciphers), C(nullptr), pi(pi_in), R(R_in), m(m_in), n(n_in), 30 time_p(0), P(nullptr), owner_(owner), flow_flag_(false) 31 { 32 33 mu = 4; 34 mu_h = 2 * mu - 1; 35 36 m_r_ = m / mu; 37 38 flow_flag_ = (m_r_ == 4); 39 key = new ElGammal(*reenc_key); 40 permute_and_reencrypt(reenc_key); 41 } 42 43 RemoteShuffler::~RemoteShuffler() 44 { 45 if (owner_) 46 { 47 if (c != nullptr) 48 Functions::delete_vector(c); 49 } 50 if (C != nullptr) 51 Functions::delete_vector(C); 52 if (pi != nullptr) 53 Functions::delete_vector(pi); 54 if (R != nullptr) 55 Functions::delete_vector(R); 56 if (P != nullptr) 57 delete P; 58 if (verifier_ != nullptr) 59 delete verifier_; 60 delete key; 61 } 62 63 string RemoteShuffler::get_public_vector() 64 { 65 return P->get_public_vector(); 66 } 67 68 void RemoteShuffler::reverse_permutation(vector<long> &reversed) 69 { 70 reversed = Functions::permutation2d_to_vector(pi, m, n); 71 } 72 73 vector<vector<Cipher_elg> *> *RemoteShuffler::permute_and_reencrypt(ElGammal *reenc_key) 74 { 75 //auto tstart = high_resolution_clock::now(); 76 if (pi == nullptr) 77 { 78 pi = new vector<vector<vector<long> *> *>(m); 79 Permutation::perm_matrix(pi, n, m); 80 } 81 if (R == nullptr) 82 { 83 R = new vector<vector<ZZ> *>(m); 84 Functions::randomEl(R, m, n); 85 } 86 C = new vector<vector<Cipher_elg> *>(m); 87 Functions::reencryptCipher(C, c, pi, R, m, n, reenc_key); 88 //auto tstop = high_resolution_clock::now(); 89 //duration<double> ttime= tstop-tstart; 90 // cout << "To permute and rerandomize the ciphertexts took " << ttime.count() << " second(s)." << endl; 91 return C; 92 } 93 94 string RemoteShuffler::create_nizk() 95 { 96 NIZKProof proof; 97 98 string v; 99 ZZ c, r; 100 string input_for_next_prover; 101 //cout << "nizk 01" << endl; 102 P = new Prover_toom(m_r_, C, R, pi, config_, m, n, key); 103 verifier_ = new VerifierClient(config_, m, n, this->c, C, key, false, false); 104 v = round1(&input_for_next_prover, &c, &r); 105 proof.add_new_step(v, c, r); 106 v = round3(input_for_next_prover, &input_for_next_prover, &c, &r); 107 //cout << "nizk 02" << endl; 108 proof.add_new_step(v, c, r); 109 //cout << "nizk 022" << endl; 110 if (verifier_->flow_flag_) 111 { 112 //cout << "nizk 0222" << endl; 113 v = round5(input_for_next_prover, &input_for_next_prover, &c, &r); 114 //cout << "nizk 0223" << endl; 115 proof.add_new_step(v, c, r); 116 //cout << "nizk 0224" << endl; 117 v = round7(input_for_next_prover, &input_for_next_prover, &c, &r); 118 //cout << "nizk 0225" << endl; 119 proof.add_new_step(v, c, r); 120 } 121 else 122 { 123 //cout << "nizk 0226" << endl; 124 v = round5red(input_for_next_prover, &input_for_next_prover, &c, &r); 125 proof.add_new_step(v, c, r); 126 //cout << "nizk 0227" << endl; 127 v = round5red_1(input_for_next_prover, &input_for_next_prover, &c, &r); 128 proof.add_new_step(v, c, r); 129 //cout << "nizk 0228" << endl; 130 v = round7red(input_for_next_prover, &input_for_next_prover, &c, &r); 131 proof.add_new_step(v, c, r); 132 } 133 //cout << "nizk 03" << endl; 134 v = round9(input_for_next_prover); 135 proof.add_final_step(v); 136 //cout << "nizk 04" << endl; 137 return proof.proof(); 138 } 139 140 string RemoteShuffler::round1(string *input_for_next_prover, ZZ *challenge, ZZ *randomness) 141 { 142 time_p = 0; 143 auto tstart = high_resolution_clock::now(); 144 string input_to_V = P->round_1(); 145 auto tstop = high_resolution_clock::now(); 146 double ttime = duration<double>(tstop - tstart).count(); 147 // cout << "Time for round 1 " << ttime << " second(s)." << endl; 148 149 time_p += ttime; 150 *input_for_next_prover = verifier_->round2(input_to_V, challenge, randomness); 151 return input_to_V; 152 } 153 154 string RemoteShuffler::round3(const string &input_file, string *input_for_next_prover, ZZ *challenge, ZZ *randomness) 155 { 156 auto tstart = high_resolution_clock::now(); 157 string input_to_V = P->round_3(input_file); 158 auto tstop = high_resolution_clock::now(); 159 double ttime = duration<double>(tstop - tstart).count(); 160 // cout << "Time for round 3 " << ttime << " second(s)." << endl; 161 time_p += ttime; 162 *input_for_next_prover = verifier_->round4(input_to_V, challenge, randomness); 163 164 return input_to_V; 165 } 166 167 string RemoteShuffler::round5(const string &input_file, string *input_for_next_prover, ZZ *challenge, ZZ *randomness) 168 { 169 auto tstart = high_resolution_clock::now(); 170 string input_to_V = P->round_5(input_file); 171 auto tstop = high_resolution_clock::now(); 172 double ttime = duration<double>(tstop - tstart).count(); 173 // cout << "Time for round 5 " << ttime << " second(s)." << endl; 174 175 *input_for_next_prover = verifier_->round6(input_to_V, challenge, randomness); 176 177 time_p += ttime; 178 return input_to_V; 179 } 180 181 string RemoteShuffler::round5red(const string &input_file, string *input_for_next_prover, ZZ *challenge, ZZ *randomness) 182 { 183 auto tstart = high_resolution_clock::now(); 184 string input_to_V = P->round_5_red(input_file); 185 m_r_ /= mu; 186 auto tstop = high_resolution_clock::now(); 187 double ttime = duration<double>(tstop - tstart).count(); 188 // cout << "Time for round 5 " << ttime << " second(s)." << endl; 189 *input_for_next_prover = verifier_->round6red(input_to_V, challenge, randomness); 190 191 time_p += ttime; 192 return input_to_V; 193 } 194 195 string RemoteShuffler::round5red_1(const string &input_file, string *input_for_next_prover, ZZ *challenge, ZZ *randomness) 196 { 197 auto tstart = high_resolution_clock::now(); 198 string input_to_V = P->round_5_red1(input_file); 199 auto tstop = high_resolution_clock::now(); 200 double ttime = duration<double>(tstop - tstart).count(); 201 // cout << "Time for round 5 " << ttime << " second(s)." << endl; 202 *input_for_next_prover = verifier_->round6red_1(input_to_V, challenge, randomness); 203 time_p += ttime; 204 return input_to_V; 205 } 206 207 string RemoteShuffler::round7(const string &input_file, string *input_for_next_prover, ZZ *challenge, ZZ *randomness) 208 { 209 auto tstart = high_resolution_clock::now(); 210 string input_to_V = P->round_7(input_file); 211 auto tstop = high_resolution_clock::now(); 212 double ttime = duration<double>(tstop - tstart).count(); 213 // cout << "Time for round 7 " << ttime << " second(s)." << endl; 214 *input_for_next_prover = verifier_->round8(input_to_V, challenge, randomness); 215 time_p += ttime; 216 return input_to_V; 217 } 218 219 string RemoteShuffler::round7red(const string &input_file, string *input_for_next_prover, ZZ *challenge, ZZ *randomness) 220 { 221 auto tstart = high_resolution_clock::now(); 222 string input_to_V = P->round_7_red(input_file); 223 auto tstop = high_resolution_clock::now(); 224 double ttime = duration<double>(tstop - tstart).count(); 225 // cout << "Time for round 7 " << ttime << " second(s)." << endl; 226 *input_for_next_prover = verifier_->round8(input_to_V, challenge, randomness); 227 time_p += ttime; 228 return input_to_V; 229 } 230 231 string RemoteShuffler::round9(const string &input_file) 232 { 233 string output_file; 234 auto tstart = high_resolution_clock::now(); 235 output_file = P->round_9(input_file); 236 auto tstop = high_resolution_clock::now(); 237 double ttime = duration<double>(tstop - tstart).count(); 238 // cout << "Time for round 9 " << ttime << " second(s)." << endl; 239 time_p += ttime; 240 241 //ofstream ost("shuffle_with_toom_cook_P.txt", ios::app); 242 //ost<< time_p<<endl; 243 //ost.close(); 244 245 return output_file; 246 } 247 248 void RemoteShuffler::print_state() const 249 { 250 cout << "+++++++" << endl; 251 cout << m_r_ << endl; 252 cout << mu << endl; 253 cout << mu_h << endl; 254 cout << "-------" << endl; 255 }