Browse Source

indentation changes mainly

avadapal 1 year ago
parent
commit
17d88dd355

+ 10 - 10
preprocessing/Makefile

@@ -15,31 +15,31 @@ all: directories preprocessing0 preprocessing1 p2preprocessing
 debug: debugpreprocessing0 debugpreprocessing1 debugp2preprocessing
 verbose: verbosepreprocessing0 verbosepreprocessing1 verbosep2preprocessing
 
-preprocessing0: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h #dpf++/dpf.h  
+preprocessing0: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h filesio.h #dpf++/dpf.h  
 	$(CXX) $(CXXFLAGS) -o preprocessing0 preprocessing.cpp $(LIBS) $(FLAGS) -DPARTY=0 -I.
 
-preprocessing1: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h #dpf++/dpf.h  
+preprocessing1: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h filesio.h #dpf++/dpf.h  
 	$(CXX) $(CXXFLAGS) -o preprocessing1 preprocessing.cpp $(LIBS) $(FLAGS) -DPARTY=1 -I.
 
-p2preprocessing: p2preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h #dpf++/dpf.h  
+p2preprocessing: p2preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h filesio.h #dpf++/dpf.h  
 	$(CXX) $(CXXFLAGS) -o  p2preprocessing p2preprocessing.cpp $(LIBS) $(FLAGS) -I.
 
-debugpreprocessing0: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h #dpf++/dpf.h  
+debugpreprocessing0: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h filesio.h #dpf++/dpf.h  
 	$(CXX) $(CXXFLAGS) -o debugpreprocessing0 preprocessing.cpp $(LIBS) $(FLAGSDEBUG) -DPARTY=0 -I.
 
-debugpreprocessing1: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h #dpf++/dpf.h  
+debugpreprocessing1: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h filesio.h #dpf++/dpf.h  
 	$(CXX) $(CXXFLAGS) -o debugpreprocessing1 preprocessing.cpp $(LIBS) $(FLAGSDEBUG) -DPARTY=1 -I.
 
-debugp2preprocessing: p2preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h #dpf++/dpf.h  
+debugp2preprocessing: p2preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h filesio.h #dpf++/dpf.h  
 	$(CXX) $(CXXFLAGS) -o  debugp2preprocessing p2preprocessing.cpp $(LIBS) $(FLAGSDEBUG) -I.
 
-verbosepreprocessing0: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h #dpf++/dpf.h  
+verbosepreprocessing0: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h filesio.h #dpf++/dpf.h  
 	$(CXX) $(CXXFLAGS) -o verbosepreprocessing0 preprocessing.cpp $(LIBS) $(FLAGSVERBOSE) -DPARTY=0 -I.
 
-verbosepreprocessing1: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h #dpf++/dpf.h  
+verbosepreprocessing1: preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h filesio.h #dpf++/dpf.h  
 	$(CXX) $(CXXFLAGS) -o verbosepreprocessing1 preprocessing.cpp $(LIBS) $(FLAGSVERBOSE) -DPARTY=1 -I.
 
-verbosep2preprocessing: p2preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h #dpf++/dpf.h  
+verbosep2preprocessing: p2preprocessing.cpp block.h bitutils.h prg_aes_impl.h prg.h dpfgen.h mpc.h share-conversion.h aes.h filesio.h #dpf++/dpf.h  
 	$(CXX) $(CXXFLAGS) -o  verbosep2preprocessing p2preprocessing.cpp $(LIBS) $(FLAGSVERBOSE) -I.
 
 docs:
@@ -53,6 +53,6 @@ directories: $(BINDIR)
 # 	mkdir -p $(BINDIR)
 
 clean:
-	rm -f p2preprocessing preprocessing1 preprocessing0
+	rm -f p2preprocessing preprocessing1 preprocessing0 verbosepreprocessing0 verbosepreprocessing1 verbosep2preprocessing debugpreprocessing0 debugpreprocessing1 debugp2preprocessing
 
 

+ 35 - 303
preprocessing/dpfgen.h

@@ -27,252 +27,6 @@ void generate_random_targets(uint8_t ** target_share_read, size_t n_threads, boo
 	}
 }
 
-// void write_evalfull_outs_into_a_file(bool party, size_t i, size_t db_nitems,  int8_t ** flags, int64_t * outs_, __m128i * final_correction_word)
-// {
-//  if(!party) 
-//  {  
-//    	char const * p0_filename0;
-// 	char tmp[100];
-//    	p0_filename0 = "../duoram-online/party0_read_flags_b";
-// 	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-	
-// 	int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//    	int written = write(w0, flags[i], db_nitems * sizeof(flags[i][0]));
-//       if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w0);
-
-//  	 p0_filename0 = "../duoram-online/party0_read_flags_c";
-//    	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//    	 int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//      written = write(w1, flags[i], db_nitems * sizeof(flags[i][0]));
-//      if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w1);
-	
-// 	  p0_filename0 = "../duoram-online/party0_read_flags_d";
-// 	  	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//    	  int w2 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//    	   written = write(w2, flags[i], db_nitems * sizeof(flags[i][0]));
-      
-// 	  if(written<0) {
-//              perror("Write error");
-//        }  
-// 	  close(w2);
-
-
-// 	p0_filename0 = "../duoram-online/party0_write_flags_b";
-// 		strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//     int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//     written = write(w4, flags[i], db_nitems * sizeof(flags[i][0]));
-//       if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w4);
-
-//    p0_filename0 = "../duoram-online/party0_write_flags_c";
-//    	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//     int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//     written = write(w5, flags[i], db_nitems * sizeof(flags[i][0]));
-//       if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w5);
-// 	  p0_filename0 = "../duoram-online/party0_write_flags_d";
-// 	  	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//    	   int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//    	   written = write(w6, flags[i], db_nitems * sizeof(flags[i][0]));
-      
-// 	  if(written<0) {
-//              perror("Write error");
-//        }
-      
-// 	  close(w6);
-
-// 	p0_filename0 = "../duoram-online/party0_write_b";
-// 		strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//     w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//     written = write(w4, outs_, db_nitems * sizeof(outs_[0]));
-//       if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w4);
-
-//  	  p0_filename0 = "../duoram-online/party0_write_c";
-// 	  	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//      w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//     written = write(w5, outs_, db_nitems * sizeof(outs_[0]));
-//       if(written<0) {
-//              perror("Write error");
-//        }
-//     close(w5);
-
-//     p0_filename0 = "../duoram-online/party0_write_d";
-// 		strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//    	w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//    	written = write(w6, outs_, db_nitems * sizeof(outs_[0]));
-      
-// 	if(written<0) {
-//              perror("Write error");
-//        }
-      
-// 	  close(w6);
-	  
-//   }
-//   if(party) 
-//   {
-// 	 char const * p0_filename0;
-// 	 char tmp[100];
-//      p0_filename0 = "../duoram-online/party1_read_flags_b";
-// 	 strcpy(tmp, p0_filename0);
-//  	 strcat(tmp, std::to_string(i).c_str());
-     
-// 	 int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//      int written = write(w0, flags[i], db_nitems * sizeof(flags[i][0]));
-//      if(written<0) {
-//              perror("Write error");
-//        }
-     
-// 	 close(w0);	
-
-// 	 p0_filename0 = "../duoram-online/party1_read_flags_c";
-// 	 strcpy(tmp, p0_filename0);
-//  	 strcat(tmp, std::to_string(i).c_str());
-   	 
-// 	 int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-// 	 written = write(w1, flags[i], db_nitems * sizeof(flags[i][0]));
-// 	 if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w1);
-
-// 	  p0_filename0 = "../duoram-online/party1_read_flags_d";
-// 	  strcpy(tmp, p0_filename0);
-//  	  strcat(tmp, std::to_string(i).c_str());
-   	  
-// 	  int w2 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//    	   written = write(w2,flags[i], db_nitems * sizeof(flags[i][0]));
-      
-// 	  if(written<0) {
-//              perror("Write error");
-//        }
-      
-// 	  close(w2);
-
-
-// 	p0_filename0 = "../duoram-online/party1_write_flags_b";
-// 	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-    
-// 	int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//     written = write(w4,flags[i], db_nitems * sizeof(flags[i][0]));
-//       if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w4);
-
-//    	p0_filename0 = "../duoram-online/party1_write_flags_c";
-// 	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-    
-// 	int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//     written = write(w5, flags[i], db_nitems * sizeof(flags[i][0]));
-//      if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w5);
-	
-// 	p0_filename0 = "../duoram-online/party1_write_flags_d";
-// 	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-   	   
-// 	int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//    	written = write(w6, flags[i], db_nitems * sizeof(flags[i][0]));  
-// 	if(written<0) {
-//              perror("Write error");
-//        }
-      
-// 	close(w6);
-
-// 	p0_filename0 = "../duoram-online/party1_write_b";
-// 	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-
-//     w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//     written = write(w4, outs_, db_nitems * sizeof(outs_[0]));
-//       if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w4);
-
-//  //  char const * p0_filename0;
-//    p0_filename0 = "../duoram-online/party1_write_c";
-//    	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//     w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//     written = write(w5, outs_, db_nitems * sizeof(outs_[0]));
-//       if(written<0) {
-//              perror("Write error");
-//        }
-//       close(w5);
-// 	  p0_filename0 = "../duoram-online/party1_write_d";
-// 	  	strcpy(tmp, p0_filename0);
-//  	strcat(tmp, std::to_string(i).c_str());
-//    	    w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-//    	   written = write(w6, outs_, db_nitems * sizeof(outs_[0]));
-      
-// 	  if(written<0) {
-//              perror("Write error");
-//        }
-      
-// 	  close(w6);
-//   }
-
-//   	if(party)
-// 	{
-// 		char const * p1_filename0;
-// 		char tmp[100];
-// 		p1_filename0 = "../duoram-online/FCW1";
-// 		strcpy(tmp, p1_filename0);
-//  		strcat(tmp, std::to_string(i).c_str());
-		
-// 		int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-// 		int written = write(w0, &final_correction_word[0][0],  sizeof(final_correction_word[0][0]));
-// 		if(written<0) {
-// 				perror("Write error");
-// 		}
-// 		close(w0);
-// 	}
-	
-// 	if(!party)
-// 	{
-// 		char const * p0_filename0;
-// 		char tmp[100];
-// 		p0_filename0 = "../duoram-online/FCW0";
-// 		strcpy(tmp, p0_filename0);
-//  		strcat(tmp, std::to_string(i).c_str());
-		
-// 		int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-// 		int written = write(w0, &final_correction_word[0][0],  sizeof(final_correction_word[0][0]));
-// 		if(written<0) {
-// 				perror("Write error");
-// 		}
-// 		close(w0);
-// 	}
-// }
-
-
 struct cw_construction
 {
   __m128i rand_b, gamma_b;
@@ -288,32 +42,29 @@ struct BlindsCW
 
 void compute_CW_bits(tcp::socket& sout,   __m128i L, __m128i R, uint8_t bit, uint8_t &cwt_L, uint8_t &cwt_R)
 {
-		uint8_t advice_L = get_lsb(L) ^ bit;
-		uint8_t advice_R = get_lsb(R) ^ bit;
 
-			
-		
-		uint8_t advice[2];
-		uint8_t cwts[2];	
+	uint8_t advice_L = get_lsb(L) ^ bit;
+	uint8_t advice_R = get_lsb(R) ^ bit;
 
-		advice[0] = advice_L;
-		advice[1] = advice_R;
-			
-		boost::asio::write(sout, boost::asio::buffer(&advice, sizeof(advice)));
-		boost::asio::read(sout, boost::asio::buffer(&cwts, sizeof(cwts)));
-			
-		cwt_L = cwts[0];
-		cwt_R = cwts[1];
+	uint8_t advice[2];
+	uint8_t cwts[2];	
 
-		cwt_L = cwt_L ^ advice_L ^ 1;
-		cwt_R = cwt_R ^ advice_R;	
+	advice[0] = advice_L;
+	advice[1] = advice_R;
+		
+	boost::asio::write(sout, boost::asio::buffer(&advice, sizeof(advice)));
+	boost::asio::read(sout, boost::asio::buffer(&cwts, sizeof(cwts)));
+		
+	cwt_L = cwts[0];
+	cwt_R = cwts[1];
 
+	cwt_L = cwt_L ^ advice_L ^ 1;
+	cwt_R = cwt_R ^ advice_R;	
 }
 
 void compute_CW(tcp::socket& sout, tcp::socket& sin, __m128i L, __m128i R, uint8_t bit, __m128i & CW)
 {
 
-
 	cw_construction computecw;
 
 	//Communication from P2 
@@ -365,13 +116,10 @@ void compute_CW(tcp::socket& sout, tcp::socket& sin, __m128i L, __m128i R, uint8
 	 out_ ^= rand_b;
 	}
 
-	__m128i out_reconstruction; 
+	//__m128i out_reconstruction; 
 	boost::asio::write(sout, boost::asio::buffer(&out_, sizeof(out_)));
-	boost::asio::read(sout, boost::asio::buffer(&out_reconstruction, sizeof(out_reconstruction)));
-	out_reconstruction = out_ ^ out_reconstruction;
-	
-	CW = out_reconstruction;
-	
+	boost::asio::read(sout, boost::asio::buffer(&CW, sizeof(CW)));
+	CW = out_ ^ CW;
 
 	// The following asserts the correctness of ComputeCW
 	#ifdef DEBUG
@@ -433,11 +181,10 @@ static inline void traverse(const prgkey_t & prgkey, const node_t & seed,	node_t
  * @param party Party
  * @param socket_no 
  */
-inline void create_dpfs(  size_t db_nitems,
-							 const AES_KEY& prgkey,  uint8_t target_share[64], std::vector<socket_t>& socketsPb, std::vector<socket_t>& socketsP2,
-							 const size_t from, const size_t to, __m128i * output, int8_t * _t, __m128i& final_correction_word, bool party, size_t socket_no, size_t ind)
+inline void create_dpfs (size_t db_nitems,
+						 const AES_KEY& prgkey,  uint8_t target_share[64], std::vector<socket_t>& socketsPb, std::vector<socket_t>& socketsP2,
+						 const size_t from, const size_t to, __m128i * output, int8_t * _t, __m128i& final_correction_word, bool party, size_t socket_no, size_t ind = 0)
 {
-
     const size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
 	const bool  is_packed = (sizeof(leaf_t) < sizeof(node_t));
 	const size_t nodes_per_leaf = is_packed ? 1 : std::ceil(static_cast<double>(bits_per_leaf) / (sizeof(node_t) * CHAR_BIT));
@@ -468,11 +215,13 @@ inline void create_dpfs(  size_t db_nitems,
 	
 	__m128i * CW = (__m128i *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(__m128i));
 	
-	if(ind == 0)
-	{
-		std::cout << "root = "         << root[0] << " " << root[1] << std::endl;
-		std::cout << "t[curlayer][0] " << (int) t[curlayer][0] << std::endl;
-	}
+	#ifdef VERBOSE
+		if(ind == 0)
+		{
+			std::cout << "root = "         << root[0] << " " << root[1] << std::endl;
+			std::cout << "t[curlayer][0] " << (int) t[curlayer][0] << std::endl;
+		}
+	#endif
 
  
 	dpfP2 dpf_instance;
@@ -563,27 +312,28 @@ inline void create_dpfs(  size_t db_nitems,
 
  	final_correction_word = Gamma;  
 
-} // dpf::__evalinterval
+} // create_dpfs
 
 
 
 inline void evaluate_dpfs( size_t db_nitems,  dpfP2 dpfinstance,  const AES_KEY& prgkey,   const size_t from, const size_t to, 
-							__m128i * output, int8_t * _t, __m128i& final_correction_word, bool party,  
-							size_t ind)
+							__m128i * output, int8_t * _t,  bool party,  size_t ind)
 {
 
-    const size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
+ const size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
 	const bool  is_packed = (sizeof(leaf_t) < sizeof(node_t));
 	const size_t nodes_per_leaf = is_packed ? 1 : std::ceil(static_cast<double>(bits_per_leaf) / (sizeof(node_t) * CHAR_BIT));
 
-    const size_t depth = std::ceil(std::log2(db_nitems));
+ const size_t depth = std::ceil(std::log2(db_nitems));
 	const size_t nbits = std::ceil(std::log2(db_nitems));
 	const size_t nodes_in_interval = db_nitems-1; 
 	
 	__m128i root = dpfinstance.root;
 	__m128i * CW = (__m128i *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(__m128i));
+	
 	uint8_t * cwt_L = (uint8_t *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(uint8_t));
 	uint8_t * cwt_R = (uint8_t *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(uint8_t));
+	
 	for(size_t j = 0; j < depth; ++j)
 	{
 		CW[j] 	  = dpfinstance.CW[j];
@@ -592,7 +342,7 @@ inline void evaluate_dpfs( size_t db_nitems,  dpfP2 dpfinstance,  const AES_KEY&
 	}
 	
  
-    root =	set_lsb(root, party);
+ root =	set_lsb(root, party);
 
 	const size_t from_node = std::floor(static_cast<double>(from) / nodes_per_leaf);
 
@@ -659,24 +409,6 @@ inline void evaluate_dpfs( size_t db_nitems,  dpfP2 dpfinstance,  const AES_KEY&
 				s[curlayer][(2*j)+1] = clear_lsb(xor_if(s[curlayer][(2*j)+1], CW[layer], !t[1-curlayer][j]), 0b11);
 			}
 	}
-
-	__m128i Gamma  =  _mm_setzero_si128();
-	 
-	for (size_t i = 0; i < to + 1; ++i)
-	{
-	  Gamma[0] += output[i][0];	// the correction word for duoram update
-	  Gamma[1] += output[i][1]; // the correction word for share conversion
-	}
-		
-	if(party) 
-	{
-	  Gamma[0] = -Gamma[0];	 // the correction word for duoram update
-	  Gamma[1] = -Gamma[1];  // the correction word for share conversion
-	}
-	
-
- 	final_correction_word = Gamma; // final_correction_word + Gamma;
-
-} // dpf::__evalinterval
+} // evaluate_dpfs
 
 

+ 316 - 204
preprocessing/filesio.h

@@ -2,243 +2,355 @@ void write_evalfull_outs_into_a_file(bool party, size_t i, size_t db_nitems,  in
 {
  if(!party) 
  {  
-   	char const * p0_filename0;
-	char tmp[100];
-   	p0_filename0 = "../duoram-online/party0_read_flags_b";
-	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-	
-	int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-   	int written = write(w0, flags[i], db_nitems * sizeof(flags[i][0]));
-      if(written<0) {
-             perror("Write error");
-       }
-      close(w0);
-
- 	 p0_filename0 = "../duoram-online/party0_read_flags_c";
-   	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-   	 int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-     written = write(w1, flags[i], db_nitems * sizeof(flags[i][0]));
-     if(written<0) {
-             perror("Write error");
-       }
-      close(w1);
+   char const * p0_filename0;
+	  char tmp[100];
+   p0_filename0 = "../duoram-online/preprocflags/party0_read_flags_b";
+	  strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+	  int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   int written = write(w0, flags[i], db_nitems * sizeof(flags[i][0]));
+   if(written<0)  perror("Write error"); 
+   close(w0);
+
+ 	 p0_filename0 = "../duoram-online/preprocflags/party0_read_flags_c";
+   strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w1, flags[i], db_nitems * sizeof(flags[i][0]));
+   if(written<0)  perror("Write error"); 
+   close(w1);
 	
-	  p0_filename0 = "../duoram-online/party0_read_flags_d";
-	  	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-   	  int w2 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-   	   written = write(w2, flags[i], db_nitems * sizeof(flags[i][0]));
-      
-	  if(written<0) {
-             perror("Write error");
-       }  
+	  p0_filename0 = "../duoram-online/preprocflags/party0_read_flags_d";
+	  strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   int w2 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w2, flags[i], db_nitems * sizeof(flags[i][0]));   
+	  if(written<0)  perror("Write error"); 
 	  close(w2);
 
 
-	p0_filename0 = "../duoram-online/party0_write_flags_b";
-		strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-    int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-    written = write(w4, flags[i], db_nitems * sizeof(flags[i][0]));
-      if(written<0) {
-             perror("Write error");
-       }
-      close(w4);
+ 	 p0_filename0 = "../duoram-online/preprocflags/party0_write_flags_b";
+	 	strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w4, flags[i], db_nitems * sizeof(flags[i][0]));
+   if(written<0)  perror("Write error"); 
+   close(w4);
 
-   p0_filename0 = "../duoram-online/party0_write_flags_c";
-   	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-    int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-    written = write(w5, flags[i], db_nitems * sizeof(flags[i][0]));
-      if(written<0) {
-             perror("Write error");
-       }
-      close(w5);
-	  p0_filename0 = "../duoram-online/party0_write_flags_d";
-	  	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-   	   int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-   	   written = write(w6, flags[i], db_nitems * sizeof(flags[i][0]));
-      
-	  if(written<0) {
-             perror("Write error");
-       }
-      
+   p0_filename0 = "../duoram-online/preprocflags/party0_write_flags_c";
+   strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w5, flags[i], db_nitems * sizeof(flags[i][0]));
+   if(written<0)  perror("Write error"); 
+   close(w5);
+	  
+   p0_filename0 = "../duoram-online/preprocflags/party0_write_flags_d";
+	  strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w6, flags[i], db_nitems * sizeof(flags[i][0]));
+   if(written<0)  perror("Write error");    
 	  close(w6);
 
-	p0_filename0 = "../duoram-online/party0_write_b";
-		strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-    w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-    written = write(w4, outs_, db_nitems * sizeof(outs_[0]));
-      if(written<0) {
-             perror("Write error");
-       }
-      close(w4);
-
- 	  p0_filename0 = "../duoram-online/party0_write_c";
-	  	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-     w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-    written = write(w5, outs_, db_nitems * sizeof(outs_[0]));
-      if(written<0) {
-             perror("Write error");
-       }
-    close(w5);
+	  p0_filename0 = "../duoram-online/preprocflags/party0_write_b";
+	 	strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w4, outs_, db_nitems * sizeof(outs_[0])); 
+   if(written<0)  perror("Write error"); 
+   close(w4);
 
-    p0_filename0 = "../duoram-online/party0_write_d";
-		strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-   	w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-   	written = write(w6, outs_, db_nitems * sizeof(outs_[0]));
-      
-	if(written<0) {
-             perror("Write error");
-       }
-      
-	  close(w6);
-	  
+ 	 p0_filename0 = "../duoram-online/preprocflags/party0_write_c";
+	  strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w5, outs_, db_nitems * sizeof(outs_[0]));
+   if(written<0)  perror("Write error"); 
+   close(w5);
+
+   p0_filename0 = "../duoram-online/preprocflags/party0_write_d";
+		 strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w6, outs_, db_nitems * sizeof(outs_[0]));   
+   if(written<0)  perror("Write error");       
+	  close(w6);	  
   }
+
   if(party) 
   {
-	 char const * p0_filename0;
-	 char tmp[100];
-     p0_filename0 = "../duoram-online/party1_read_flags_b";
-	 strcpy(tmp, p0_filename0);
+ 	 char const * p0_filename0;
+	  char tmp[100];
+   p0_filename0 = "../duoram-online/preprocflags/party1_read_flags_b";
+	  strcpy(tmp, p0_filename0);
  	 strcat(tmp, std::to_string(i).c_str());
-     
-	 int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-     int written = write(w0, flags[i], db_nitems * sizeof(flags[i][0]));
-     if(written<0) {
-             perror("Write error");
-       }
-     
-	 close(w0);	
+   int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   int written = write(w0, flags[i], db_nitems * sizeof(flags[i][0]));
+   if(written<0)  perror("Write error"); 
+   close(w0);	
 
-	 p0_filename0 = "../duoram-online/party1_read_flags_c";
-	 strcpy(tmp, p0_filename0);
- 	 strcat(tmp, std::to_string(i).c_str());
-   	 
-	 int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-	 written = write(w1, flags[i], db_nitems * sizeof(flags[i][0]));
-	 if(written<0) {
-             perror("Write error");
-       }
-      close(w1);
-
-	  p0_filename0 = "../duoram-online/party1_read_flags_d";
+	  p0_filename0 = "../duoram-online/preprocflags/party1_read_flags_c";
 	  strcpy(tmp, p0_filename0);
- 	  strcat(tmp, std::to_string(i).c_str());
-   	  
+ 	 strcat(tmp, std::to_string(i).c_str());	 
+	  int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+	  written = write(w1, flags[i], db_nitems * sizeof(flags[i][0]));
+   if(written<0)  perror("Write error"); 
+   close(w1);
+
+	  p0_filename0 = "../duoram-online/preprocflags/party1_read_flags_d";
+	  strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str()); 	  
 	  int w2 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-   	   written = write(w2,flags[i], db_nitems * sizeof(flags[i][0]));
-      
-	  if(written<0) {
-             perror("Write error");
-       }
-      
-	  close(w2);
+   written = write(w2,flags[i], db_nitems * sizeof(flags[i][0]));    
+   if(written<0)  perror("Write error");      
+   close(w2);
 
 
-	p0_filename0 = "../duoram-online/party1_write_flags_b";
-	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-    
-	int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-    written = write(w4,flags[i], db_nitems * sizeof(flags[i][0]));
-      if(written<0) {
-             perror("Write error");
-       }
-      close(w4);
-
-   	p0_filename0 = "../duoram-online/party1_write_flags_c";
-	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-    
-	int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-    written = write(w5, flags[i], db_nitems * sizeof(flags[i][0]));
-     if(written<0) {
-             perror("Write error");
-       }
-      close(w5);
+	  p0_filename0 = "../duoram-online/preprocflags/party1_write_flags_b";
+	  strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());  
+	  int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w4,flags[i], db_nitems * sizeof(flags[i][0]));
+   if(written<0)  perror("Write error"); 
+   close(w4);
+
+   p0_filename0 = "../duoram-online/preprocflags/party1_write_flags_c";
+	  strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w5, flags[i], db_nitems * sizeof(flags[i][0]));
+   if(written<0)  perror("Write error"); 
+   close(w5);
 	
-	p0_filename0 = "../duoram-online/party1_write_flags_d";
-	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-   	   
-	int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-   	written = write(w6, flags[i], db_nitems * sizeof(flags[i][0]));  
-	if(written<0) {
-             perror("Write error");
-       }
-      
-	close(w6);
-
-	p0_filename0 = "../duoram-online/party1_write_b";
-	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
+  	p0_filename0 = "../duoram-online/preprocflags/party1_write_flags_d";
+  	strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());	   
+	  int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w6, flags[i], db_nitems * sizeof(flags[i][0]));    
+   if(written<0)  perror("Write error");    
+	  close(w6);
 
-    w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-    written = write(w4, outs_, db_nitems * sizeof(outs_[0]));
-      if(written<0) {
-             perror("Write error");
-       }
-      close(w4);
-
- //  char const * p0_filename0;
-   p0_filename0 = "../duoram-online/party1_write_c";
-   	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-    w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-    written = write(w5, outs_, db_nitems * sizeof(outs_[0]));
-      if(written<0) {
-             perror("Write error");
-       }
-      close(w5);
-	  p0_filename0 = "../duoram-online/party1_write_d";
-	  	strcpy(tmp, p0_filename0);
- 	strcat(tmp, std::to_string(i).c_str());
-   	    w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-   	   written = write(w6, outs_, db_nitems * sizeof(outs_[0]));
-      
-	  if(written<0) {
-             perror("Write error");
-       }
-      
+	  p0_filename0 = "../duoram-online/preprocflags/party1_write_b";
+	  strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w4, outs_, db_nitems * sizeof(outs_[0]));
+   if(written<0)  perror("Write error"); 
+   close(w4);
+
+   p0_filename0 = "../duoram-online/preprocflags/party1_write_c";
+   strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w5, outs_, db_nitems * sizeof(outs_[0]));
+   if(written<0)  perror("Write error"); 
+   close(w5);
+	   
+   p0_filename0 = "../duoram-online/preprocflags/party1_write_d";
+	  strcpy(tmp, p0_filename0);
+ 	 strcat(tmp, std::to_string(i).c_str());
+   w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   written = write(w6, outs_, db_nitems * sizeof(outs_[0]));    
+	  if(written<0)  perror("Write error");    
 	  close(w6);
+
   }
 
-  	if(party)
-	{
-		char const * p1_filename0;
-		char tmp[100];
-		p1_filename0 = "../duoram-online/FCW1";
-		strcpy(tmp, p1_filename0);
+  if(party)
+	 {
+		 char const * p1_filename0;
+		 char tmp[100];
+		 p1_filename0 = "../duoram-online/preprocflags/FCW1";
+	 	strcpy(tmp, p1_filename0);
  		strcat(tmp, std::to_string(i).c_str());
 		
-		int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
-		int written = write(w0, &final_correction_word[0][0],  sizeof(final_correction_word[0][0]));
-		if(written<0) {
-				perror("Write error");
-		}
-		close(w0);
+		 int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+		 int written = write(w0, &final_correction_word[0][0],  sizeof(final_correction_word[0][0]));
+		 if(written<0)	perror("Write error");
+		 close(w0);
 	}
 	
 	if(!party)
 	{
 		char const * p0_filename0;
 		char tmp[100];
-		p0_filename0 = "../duoram-online/FCW0";
+		p0_filename0 = "../duoram-online/preprocflags/FCW0";
 		strcpy(tmp, p0_filename0);
- 		strcat(tmp, std::to_string(i).c_str());
+ 	strcat(tmp, std::to_string(i).c_str());
 		
 		int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
 		int written = write(w0, &final_correction_word[0][0],  sizeof(final_correction_word[0][0]));
-		if(written<0) {
-				perror("Write error");
-		}
+		if(written<0) perror("Write error");
 		close(w0);
 	}
-}
+
+}
+
+
+
+
+void P2_write_evalfull_outs_into_a_file(bool party, size_t i, size_t db_nitems,  int8_t * flags, int64_t * outs_)
+{
+ if(!party) 
+ {  
+    char const * p0_filename0;
+	   char tmp[100];
+   	p0_filename0 = "../duoram-online/preprocflags/P2_party0_read_flags_b";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+	
+	   int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   	int written = write(w0, flags, db_nitems * sizeof(flags[i]));
+    if(written<0) perror("Write error");
+    close(w0);
+
+ 	  p0_filename0 = "../duoram-online/preprocflags/P2_party0_read_flags_c";
+    strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+    int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w1, flags, db_nitems * sizeof(flags[i]));
+    if(written<0) perror("Write error");
+    close(w1);
+	
+	   p0_filename0 = "../duoram-online/preprocflags/P2_party0_read_flags_d";
+	   strcpy(tmp, p0_filename0);
+   	strcat(tmp, std::to_string(i).c_str());
+    int w2 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w2, flags, db_nitems * sizeof(flags[i]));
+    if(written<0) perror("Write error");
+    close(w2);
+
+ 	  p0_filename0 = "../duoram-online/preprocflags/P2_party0_write_flags_b";
+	 	 strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+    int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w4, flags, db_nitems * sizeof(flags[i]));
+    if(written<0) perror("Write error");
+    close(w4);
+
+    p0_filename0 = "../duoram-online/preprocflags/P2_party0_write_flags_c";
+    strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+    int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w5, flags, db_nitems * sizeof(flags[i]));
+    if(written<0) perror("Write error");
+    close(w5);
+	  
+    p0_filename0 = "../duoram-online/preprocflags/P2_party0_write_flags_d";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+    int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w6, flags, db_nitems * sizeof(flags[i]));
+    if(written<0) perror("Write error"); 
+	   close(w6);
+
+    p0_filename0 = "../duoram-online/preprocflags/P2_party0_write_b";
+    strcpy(tmp, p0_filename0);
+    strcat(tmp, std::to_string(i).c_str());
+    w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w4, outs_, db_nitems * sizeof(outs_[i]));
+    if(written<0) perror("Write error"); 
+    close(w4);
+
+ 	  p0_filename0 = "../duoram-online/preprocflags/P2_party0_write_c";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+    w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w5, outs_, db_nitems * sizeof(outs_[i]));
+    if(written<0) perror("Write error"); 
+    close(w5);
+
+    p0_filename0 = "../duoram-online/preprocflags/P2_party0_write_d";
+		  strcpy(tmp, p0_filename0);
+   	strcat(tmp, std::to_string(i).c_str());
+    w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w6, outs_, db_nitems * sizeof(outs_[i]));
+    if(written<0) perror("Write error"); 
+ 	  close(w6);	  
+  }
+  
+  if(party) 
+  {
+	   char const * p0_filename0;
+	   char tmp[100];
+    p0_filename0 = "../duoram-online/preprocflags/P2_party1_read_flags_b";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+     
+	   int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    int written = write(w0, flags, db_nitems * sizeof(flags[i]));
+    if(written<0) perror("Write error");   
+	   close(w0);	
+
+	   p0_filename0 = "../duoram-online/preprocflags/P2_party1_read_flags_c";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+   	int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+	   written = write(w1, flags, db_nitems * sizeof(flags[i]));
+	   if(written<0)  perror("Write error");
+    close(w1);
+
+	   p0_filename0 = "../duoram-online/preprocflags/P2_party1_read_flags_d";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());  
+	   int w2 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w2,flags, db_nitems * sizeof(flags[i]));
+    if(written<0)  perror("Write error"); 
+    close(w2);
+
+	   p0_filename0 = "../duoram-online/preprocflags/P2_party1_write_flags_b";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+	   int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w4,flags, db_nitems * sizeof(flags[i]));
+    if(written<0)  perror("Write error"); 
+    close(w4);
+
+    p0_filename0 = "../duoram-online/preprocflags/P2_party1_write_flags_c";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+	   int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w5, flags, db_nitems * sizeof(flags[i]));
+    if(written<0)  perror("Write error"); 
+    close(w5);
+	
+	   p0_filename0 = "../duoram-online/preprocflags/P2_party1_write_flags_d";
+   	strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());   
+	   int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+   	written = write(w6, flags, db_nitems * sizeof(flags[i]));  
+    if(written<0)  perror("Write error");   
+   	close(w6);
+
+  	 p0_filename0 = "../duoram-online/preprocflags/P2_party1_write_b";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+    w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w4, outs_, db_nitems * sizeof(outs_[i]));
+    if(written<0)  perror("Write error"); 
+    close(w4);
+ 
+    p0_filename0 = "../duoram-online/preprocflags/P2_party1_write_c";
+   	strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+    w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w5, outs_, db_nitems * sizeof(outs_[i]));
+    if(written<0)  perror("Write error"); 
+    close(w5);
+	
+	   p0_filename0 = "../duoram-online/preprocflags/P2_party1_write_d";
+	   strcpy(tmp, p0_filename0);
+ 	  strcat(tmp, std::to_string(i).c_str());
+    w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
+    written = write(w6, outs_, db_nitems * sizeof(outs_[i]));
+    if(written<0)  perror("Write error");   
+	   close(w6);
+  }
+}
+
+
+
+
+
+

+ 18 - 20
preprocessing/mpc.h

@@ -2,33 +2,31 @@
 
 void du_attalah_P2(std::vector<socket_t>& sockets0, std::vector<socket_t>& sockets1, int socket_no = 0)
 {
-int64_t X0, X1, Y0, Y1,  gamma0, gamma1;
+ int64_t X0, X1, Y0, Y1,  gamma0, gamma1;
 
-     arc4random_buf(&X0, sizeof(int64_t));
-     arc4random_buf(&Y0, sizeof(int64_t));
-     arc4random_buf(&X1, sizeof(int64_t));
-     arc4random_buf(&Y1, sizeof(int64_t));
+  arc4random_buf(&X0, sizeof(int64_t));
+  arc4random_buf(&Y0, sizeof(int64_t));
+  arc4random_buf(&X1, sizeof(int64_t));
+  arc4random_buf(&Y1, sizeof(int64_t));
 
-    gamma0 = X0 * Y1;
-    gamma1 = X1 * Y0;
-    
+  gamma0 = X0 * Y1;
+  gamma1 = X1 * Y0;
  
-
-    boost::asio::write(sockets0[socket_no], boost::asio::buffer(&X0, sizeof(X0)));
-    boost::asio::write(sockets1[socket_no], boost::asio::buffer(&X1, sizeof(X1)));
-    
-    boost::asio::write(sockets0[socket_no], boost::asio::buffer(&Y0, sizeof(Y0)));
-    boost::asio::write(sockets1[socket_no], boost::asio::buffer(&Y1, sizeof(Y1)));
-    
-    boost::asio::write(sockets0[socket_no], boost::asio::buffer(&gamma0, sizeof(gamma0)));
-    boost::asio::write(sockets1[socket_no], boost::asio::buffer(&gamma1, sizeof(gamma1)));
+  boost::asio::write(sockets0[socket_no], boost::asio::buffer(&X0, sizeof(X0)));
+  boost::asio::write(sockets1[socket_no], boost::asio::buffer(&X1, sizeof(X1)));
+  
+  boost::asio::write(sockets0[socket_no], boost::asio::buffer(&Y0, sizeof(Y0)));
+  boost::asio::write(sockets1[socket_no], boost::asio::buffer(&Y1, sizeof(Y1)));
+  
+  boost::asio::write(sockets0[socket_no], boost::asio::buffer(&gamma0, sizeof(gamma0)));
+  boost::asio::write(sockets1[socket_no], boost::asio::buffer(&gamma1, sizeof(gamma1)));
 }
 
 int64_t du_attalah_Pb(int64_t rb, int64_t pm, tcp::socket& s2, tcp::socket& sb)
 {
-    int64_t gamma;
+ int64_t gamma;
 	int64_t X, Y;
-    int64_t rb_blinded, pm_blinded, pm_blinded_recv, rb_blinded_recv;
+ int64_t rb_blinded, pm_blinded, pm_blinded_recv, rb_blinded_recv;
 	boost::asio::read(s2, boost::asio::buffer(&X, sizeof(X)));
 	boost::asio::read(s2, boost::asio::buffer(&Y, sizeof(Y)));
 	boost::asio::read(s2, boost::asio::buffer(&gamma, sizeof(gamma)));
@@ -44,5 +42,5 @@ int64_t du_attalah_Pb(int64_t rb, int64_t pm, tcp::socket& s2, tcp::socket& sb)
 	
 	int64_t rb_prime = rb * (pm + (pm_blinded_recv)) - (Y * rb_blinded_recv) + gamma; 
 
-    return rb_prime;
+ return rb_prime;
 }

+ 2 - 2
preprocessing/network.h

@@ -12,7 +12,7 @@ using socket_t = boost::asio::ip::tcp::socket;
  
  void accept_conncections_from_Pb(boost::asio::io_context&io_context, std::vector<socket_t>& socketsPb, int port, size_t j)
 {
- 	 tcp::acceptor acceptor_a(io_context, tcp::endpoint(tcp::v4(), port));
+ 	tcp::acceptor acceptor_a(io_context, tcp::endpoint(tcp::v4(), port));
 	 tcp::socket sb_a(acceptor_a.accept());
 	 socketsPb[j] = std::move(sb_a); 
 }
@@ -21,7 +21,7 @@ using socket_t = boost::asio::ip::tcp::socket;
 void make_connections(bool& party, const std::string host1, 
 								 const std::string host2, boost::asio::io_context&io_context,  std::vector<socket_t>& socketsPb, std::vector<socket_t>& socketsP2, 	std::vector<int> ports, std::vector<int> ports2_1,	std::vector<int> ports2_0, size_t number_of_sockets)
 {
-	    tcp::resolver resolver(io_context);
+	 tcp::resolver resolver(io_context);
 
 		for(size_t j = 0; j < number_of_sockets + 1; ++j)
  	{

+ 35 - 21
preprocessing/p2preprocessing.cpp

@@ -13,35 +13,30 @@
 #include <bsd/stdlib.h> // arc4random_buf
 #include <x86intrin.h>  // SSE and AVX intrinsics
 #include <../boost/asio/thread_pool.hpp>
+#include <../boost/asio.hpp>
+#include <../boost/lexical_cast.hpp>
 #include <iostream> 
 #include <chrono>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <fstream>
-#include <boost/asio.hpp>
 #include <mutex>
-#include <boost/lexical_cast.hpp>
 #include "bitutils.h"
 #include "block.h"
 #include "prg_aes_impl.h" 
 #include "filesio.h"
+
 using boost::asio::ip::tcp;
 
 using namespace dpf;
  
 
 typedef __m128i leaf_type;
- 
- 
 typedef __m128i leaf_t;
 typedef __m128i node_t;
  
- 
-
- using socket_t = boost::asio::ip::tcp::socket;
- 
- 
+using socket_t = boost::asio::ip::tcp::socket;
 
 size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
 bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
@@ -53,10 +48,10 @@ size_t __depth(const size_t nitems) { return std::ceil(std::log2(std::ceil(stati
 #include "share-conversion.h"
 
 void mpc_gen(const size_t depth, AES_KEY& prgkey, const size_t db_nitems, const size_t n_threads, std::vector<socket_t>& sockets0, std::vector<socket_t>& sockets1, 
-             __m128i** output0, int8_t ** flags0, __m128i* final_correction_word0,  __m128i** output1, int8_t ** flags1, __m128i* final_correction_word1, 
+             __m128i** output0, int8_t ** flags0,    __m128i** output1, int8_t ** flags1,   size_t j,
             size_t socket_no = 0)
 {	
-	dpfP2 dpf_instance0, dpf_instance1;
+	 dpfP2 dpf_instance0, dpf_instance1;
   
   for(size_t j = 0; j < depth; ++j)
   {
@@ -110,9 +105,12 @@ void mpc_gen(const size_t depth, AES_KEY& prgkey, const size_t db_nitems, const
   boost::asio::read(sockets1[socket_no+1], boost::asio::buffer(&dpf_instance1, sizeof(dpfP2)));
  
 
-  evaluate_dpfs(db_nitems,  dpf_instance0,   prgkey,  0, db_nitems-1,	output0[0],  flags0[0],  final_correction_word0[0], false,  0);
-  evaluate_dpfs(db_nitems,  dpf_instance1,   prgkey,  0, db_nitems-1, output1[0], flags1[0], final_correction_word1[0], true,  0);
+  evaluate_dpfs(db_nitems,  dpf_instance0,   prgkey,  0, db_nitems-1,	output0[j],  flags0[j],  false,  0);
+  evaluate_dpfs(db_nitems,  dpf_instance1,   prgkey,  0, db_nitems-1, output1[j],  flags1[j],  true ,  0);
+
 
+  // P2_write_evalfull_outs_into_a_file(false, 0, db_nitems,  flags0[0], 	output0[0]);
+  // P2_write_evalfull_outs_into_a_file(true,  0, db_nitems,  flags1[0], 	output1[0]);
   #ifdef DEBUG
     for(size_t j = 0; j < db_nitems; ++j)
     {
@@ -142,7 +140,6 @@ int main(int argc, char* argv[])
   const size_t number_of_sockets = 5 * n_threads;
   const size_t db_nitems = 1ULL << atoi(argv[4]);
   const size_t depth = std::ceil(std::log2(db_nitems));
-
     
   std::vector<int> ports2_0;
   for(size_t j = 0; j < number_of_sockets; ++j) 
@@ -178,8 +175,8 @@ int main(int argc, char* argv[])
 
   pool2.join();
 
-  boost::asio::thread_pool pool(n_threads); 
-  __m128i * final_correction_word0 = (__m128i *) std::aligned_alloc(sizeof(__m256i), n_threads * sizeof(__m128i));
+  boost::asio::thread_pool pool(n_threads);  
+
 	__m128i ** output0 = (__m128i ** ) malloc(sizeof(__m128i *) * n_threads);
 	int8_t  ** flags0  = (int8_t ** ) malloc(sizeof(uint8_t *) * n_threads);
 	 
@@ -190,7 +187,7 @@ int main(int argc, char* argv[])
 	}
 
 
-  __m128i * final_correction_word1 = (__m128i *) std::aligned_alloc(sizeof(__m256i), n_threads * sizeof(__m128i));
+  
 	__m128i ** output1 = (__m128i ** ) malloc(sizeof(__m128i *) * n_threads);
 	int8_t  ** flags1  = (int8_t ** ) malloc(sizeof(uint8_t *) * n_threads);
 	 
@@ -202,25 +199,42 @@ int main(int argc, char* argv[])
   for(size_t j = 0; j < n_threads; ++j)
   {
      boost::asio::post(pool, std::bind(mpc_gen,  std::ref(depth), std::ref(aeskey), db_nitems, n_threads,  std::ref(sockets0), std::ref(sockets1), 
-                        output0, flags0, final_correction_word0,  output1, flags1, final_correction_word1, 5 * j));    
+                                       output0, flags0,  output1, flags1,  j, 5 * j));    
   }
 
   pool.join();
 
 
   boost::asio::thread_pool pool3(n_threads); 
+  int64_t ** leaves0    = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
+	int64_t ** leafbits0  = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads); 
+  int64_t ** leaves1    = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
+	int64_t ** leafbits1  = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads); 
+	
+  for(size_t j = 0; j < n_threads; ++j)
+	{
+		leaves0[j]    = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
+		leafbits0[j]  = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
+    leaves1[j]    = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
+		leafbits1[j]  = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
+	}
 
  /* The function convert_sharesP2 appears in share-conversion.h */
   for(size_t j = 0; j < n_threads; ++j)
   {
-     boost::asio::post(pool3, std::bind(convert_sharesP2, std::ref(sockets0), std::ref(sockets1), j));    
+     boost::asio::post(pool3, std::bind(convert_sharesP2, db_nitems,  output0, flags0,  output1, flags1, leaves0, leafbits0, leaves1, leafbits1,  std::ref(sockets0), std::ref(sockets1),j,  j));    
   }
 
   pool3.join(); 
   
   /* The function P2_xor_to_additive appears in share-conversion.h */
-  P2_xor_to_additive(sockets0, sockets1, 0);
-  
+  boost::asio::thread_pool pool4(n_threads); 
+  for(size_t j = 0; j < n_threads; ++j)
+  {
+     boost::asio::post(pool4,  std::bind(P2_xor_to_additive, std::ref(sockets0[j]), std::ref(sockets1[j]), j));
+  }
+  pool4.join();
+
   return 0;
 }
 

+ 35 - 18
preprocessing/preprocessing.cpp

@@ -24,6 +24,11 @@
 #include <future>
 #include <mutex>
 
+typedef unsigned char byte_t;
+typedef __m128i node_t;
+constexpr size_t leaf_size = 1;
+typedef __m128i leaf_type;
+typedef std::array<leaf_type, leaf_size> leaf_t;
 
 #include "bitutils.h"
 #include "block.h" 
@@ -33,13 +38,6 @@
 using boost::asio::ip::tcp;
 using socket_t = boost::asio::ip::tcp::socket;
  
-typedef unsigned char byte_t;
-
-typedef __m128i node_t;
-constexpr size_t leaf_size = 1;
-typedef __m128i leaf_type;
-
-typedef std::array<leaf_type, leaf_size> leaf_t;
 
 using namespace dpf;  // The namespace is found in bitutils.h
 
@@ -65,6 +63,7 @@ int main(int argc, char * argv[])
  
   	bool party;
 
+	/* The function make_connections appears in network.h */
 	make_connections(party, host1, host2,  io_context, socketsPb, socketsP2, ports,  ports2_1, ports2_0, number_of_sockets);
  
 	const size_t db_nitems = 1ULL << atoi(argv[4]);
@@ -90,20 +89,14 @@ int main(int argc, char * argv[])
 	boost::asio::thread_pool pool(n_threads);
 	boost::asio::thread_pool pool_share_conversion(n_threads);
 	
-	int64_t ** leaves = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
-	int64_t ** leafbits  = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
-	 
-	for(size_t j = 0; j < n_threads; ++j)
-	{
-		leaves[j] = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
-		leafbits[j]  = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
-	}
+
 	
 	// The following function call creates and evaluates DPFs at target_share_read[j] for j \in \{0, \ldots, n_threads}
 	// the flag vectors are stored in flags
 	// the leaves are stored in output
 	// the final correctionword is stored in final_correction_word
-	/* The function create_dpfs is defined dpf-gen.h*/
+
+	/* The function create_dpfs appears in dpf-gen.h*/
 	for(size_t j = 0; j < n_threads; ++j)
 	{
 		boost::asio::post(pool,  std::bind(create_dpfs,  db_nitems,	std::ref(aeskey),  target_share_read[j],  std::ref(socketsPb), std::ref(socketsP2), 0, db_nitems-1, 
@@ -128,6 +121,7 @@ int main(int argc, char * argv[])
 				output_reconstruction -= output[j][i][0];
 				if(output_reconstruction != 0) std::cout << i << "---> " << output_reconstruction << std::endl;
 			}
+
 			int64_t final_correction_word_reconstruction = 0;
 			boost::asio::write(socketsPb[0], boost::asio::buffer(&final_correction_word[j][0], sizeof(final_correction_word[j][0])));
 			boost::asio::read(socketsPb[0], boost::asio::buffer(&final_correction_word_reconstruction, sizeof(final_correction_word_reconstruction)));
@@ -137,6 +131,21 @@ int main(int argc, char * argv[])
 	#endif
  
 
+
+
+	/* 
+		leaves is a additive shares of the outputs (leaves of the DPF)
+		leafbits is the additive shares of flag bits of the DPFs
+	*/
+	int64_t ** leaves = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
+	int64_t ** leafbits  = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads); 
+	for(size_t j = 0; j < n_threads; ++j)
+	{
+		leaves[j] = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
+		leafbits[j]  = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
+	}
+
+	/* The function convert_shares appears in share-conversion.h */
 	for(size_t j = 0; j < n_threads; ++j)
 	{
 		boost::asio::post(pool_share_conversion,  std::bind(convert_shares, j, output, flags, n_threads, db_nitems, final_correction_word, 	leaves, leafbits, 
@@ -145,11 +154,19 @@ int main(int argc, char * argv[])
 	
 	pool_share_conversion.join();
 
- 	xor_to_additive( party, target_share_read, socketsPb, socketsP2);
 
+	boost::asio::thread_pool pool_xor_to_additive(n_threads); 
+
+	for(size_t j = 0; j < n_threads; ++j)
+	{
+ 		boost::asio::post(pool_xor_to_additive, std::bind(xor_to_additive, party, target_share_read[j], std::ref(socketsPb[j]), std::ref(socketsP2[j])));
+	}
+
+	pool_xor_to_additive.join();
+	
 	auto end = std::chrono::steady_clock::now();
 	std::chrono::duration<double> elapsed_seconds = end-start;
 	std::cout << "time to generate and evaluate " << n_threads << " dpfs of size 2^" << atoi(argv[4]) << " is: " << elapsed_seconds.count() << "s\n";
-
+	
 	return 0;
 }

+ 158 - 134
preprocessing/share-conversion.h

@@ -10,7 +10,76 @@ uint64_t binary_to_decimal(std::array<uint64_t, 64> inp)
 	return output;
 }
 
-void  P2_xor_to_additive(std::vector<socket_t>& sockets0, std::vector<socket_t>& sockets1, size_t socket_no)
+struct shareconversion
+{
+  int64_t PM, PM_recv, rb, FCWshare_reconstruction;
+};
+
+/**
+ * @brief This function is used to convert the XOR-ed shared flag bits into additive shares of the same
+ * 
+ * @param sockets0 
+ * @param sockets1 
+ * @param socket_no 
+ */
+void convert_sharesP2(size_t db_nitems, __m128i** output0, int8_t ** flags0,  __m128i** output1, int8_t ** flags1, 
+                    int64_t ** leaves0, int64_t ** leafbits0, int64_t ** leaves1, int64_t ** leafbits1,
+                    
+                     std::vector<socket_t>& sockets0, std::vector<socket_t>& sockets1, size_t i, size_t socket_no = 0)
+{ 
+ du_attalah_P2(sockets0, sockets1, socket_no); 
+
+ shareconversion shareconversionP0, shareconversionP1;
+ boost::asio::read(sockets0[i], boost::asio::buffer(&shareconversionP0, sizeof(shareconversion)));
+ boost::asio::read(sockets1[i], boost::asio::buffer(&shareconversionP1, sizeof(shareconversion)));
+ 
+ for(size_t j = 0; j < db_nitems; ++j)
+	{ 
+   output1[i][j] =  -output1[i][j];
+   flags1[i][j]  =  -flags1[i][j]; 	 
+	}
+
+ int64_t * flags0_  = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t)); 
+ int64_t * flags1_  = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
+ 
+ for(size_t j = 0; j < db_nitems; ++j)
+	{
+ 
+		  leaves0[i][j]  = output0[i][j][0];
+    leaves1[i][j]  = output1[i][j][0];
+
+    flags0_[j] = (flags0[i][j] * shareconversionP0.PM) + (flags0[i][j] * shareconversionP0.PM_recv) + (flags0[i][j] * shareconversionP0.rb); 
+		  flags0_[j] += output0[i][j][1]; 
+
+    flags1_[j] = (flags1[i][j] * shareconversionP1.PM) + (flags1[i][j] * shareconversionP1.PM_recv) + (flags1[i][j] * shareconversionP1.rb); 
+		  flags1_[j] += output1[i][j][1]; 
+
+	 
+		  flags0_[j] -= (flags0[i][j] * shareconversionP0.FCWshare_reconstruction);		
+		  flags1_[j] -= (flags1[i][j] * shareconversionP1.FCWshare_reconstruction);
+		 
+	 	 flags0[i][j] = flags0_[j];
+		  flags1[i][j] = flags1_[j];
+	
+    if(flags0[i][j] == 128 || flags0[i][j] == -128) flags0[i][j] = 0;
+		  leafbits0[i][j] = flags0[i][j]; 
+    if(flags1[i][j] == 128 || flags1[i][j] == -128) flags1[i][j] = 0;
+		  leafbits1[i][j] = flags1[i][j]; 
+	 }
+
+    #ifdef VERBOSE
+        for(size_t j = 0; j < db_nitems; ++j)
+        {		
+            int64_t leafbit_reconstruction = leafbits0[i][j] + leafbits1[i][j];
+            if(leafbit_reconstruction != 0) std::cout << std::dec << j << ":-> " << leafbit_reconstruction << std::endl;   
+        }
+    #endif
+      
+      P2_write_evalfull_outs_into_a_file(false, 0, db_nitems,  flags0[0], 	leaves0[0]);
+      P2_write_evalfull_outs_into_a_file(true,  0, db_nitems,  flags1[0], 	leaves1[0]);
+} 
+
+void  P2_xor_to_additive(tcp::socket& s0, tcp::socket& s1, size_t socket_no)
 {
    uint64_t x0, x1, y0, y1, gamma0, gamma1, alpha;
 
@@ -24,38 +93,37 @@ void  P2_xor_to_additive(std::vector<socket_t>& sockets0, std::vector<socket_t>&
    gamma0 = (x0 * y1) - alpha;
    gamma1 =  alpha;
 
+  #ifdef VERBOSE
+    std::cout << "x0 = " << x0 << std::endl;
+    std::cout << "x1 = " << x1 << std::endl;
+    std::cout << "gamma0 = " << gamma0 << std::endl;
+  #endif
 
-  std::cout << "x0 = " << x0 << std::endl;
-  std::cout << "x1 = " << x1 << std::endl;
-  std::cout << "gamma0 = " << gamma0 << std::endl;
-  boost::asio::write(sockets0[socket_no], boost::asio::buffer(&x0, sizeof(x0)));
+  boost::asio::write(s0, boost::asio::buffer(&x0, sizeof(x0)));
  
-  boost::asio::write(sockets0[socket_no], boost::asio::buffer(&gamma0, sizeof(gamma0)));
+  boost::asio::write(s0, boost::asio::buffer(&gamma0, sizeof(gamma0)));
 
-  boost::asio::write(sockets1[socket_no], boost::asio::buffer(&y1, sizeof(y1)));
-  boost::asio::write(sockets1[socket_no], boost::asio::buffer(&gamma1, sizeof(gamma1)));
+  boost::asio::write(s1, boost::asio::buffer(&y1, sizeof(y1)));
+  boost::asio::write(s1, boost::asio::buffer(&gamma1, sizeof(gamma1)));
 
 }
 
-int64_t xor_to_additive(bool party, uint8_t ** target_share_read, std::vector<socket_t>& socketsPb, std::vector<socket_t>& socketsP2)
+int64_t xor_to_additive(bool party, uint8_t * target_share_read, tcp::socket& sb, tcp::socket& s2)
 {
     const size_t logn = 64;
 
     
-    std::array<uint64_t, logn> b;
-    std::array<uint64_t, logn> b_blinded;
-    std::array<uint64_t, logn> b_recv;
-    
+    std::array<uint64_t, logn> b, b_blinded, b_recv;
+ 
     for(size_t j = 0; j < logn; ++j)
     {
-        // arc4random_buf(&b[j], sizeof(b[j]));
-        // b[j] = b[j] % 2;	
-        b[j] = target_share_read[0][j];
+        b[j] = target_share_read[j];
+
         #ifdef DEBUG
             uint8_t target_bit_rec;   
-            boost::asio::write(socketsPb[0], boost::asio::buffer(&target_share_read[0][j],   sizeof(uint8_t)));
-            boost::asio::read(socketsPb[0], boost::asio::buffer(&target_bit_rec,   sizeof(uint8_t)));
-            if(target_bit_rec != target_share_read[0][j]) std::cout << "XOR---->>>> " << j << std::endl;
+            boost::asio::write(sb, boost::asio::buffer(&target_share_read[j],   sizeof(uint8_t)));
+            boost::asio::read(sb, boost::asio::buffer(&target_bit_rec,   sizeof(uint8_t)));
+            if(target_bit_rec != target_share_read[j]) std::cout << "non-zero XOR index = " << j << std::endl;
         #endif
     }
 
@@ -63,24 +131,15 @@ int64_t xor_to_additive(bool party, uint8_t ** target_share_read, std::vector<so
         uint64_t b_ = binary_to_decimal(b);
         std::cout << "b_ = " << b_ << std::endl;
     #endif
-    //std::array<uint64_t, logn> c;
-
-
+    
     std::array<uint64_t, logn> c_mul;
     std::array<uint64_t, logn> d;
    
 
-
     
-
-    boost::asio::write(socketsPb[0], boost::asio::buffer(&b, logn * sizeof(b[0])));
-    boost::asio::read(socketsPb[0], boost::asio::buffer(&b_recv, logn * sizeof(b[0])));
-
-
-
     uint64_t BLIND, Gamma;
-    boost::asio::read(socketsP2[0], boost::asio::buffer(&BLIND, sizeof(uint64_t)));
-    boost::asio::read(socketsP2[0], boost::asio::buffer(&Gamma, sizeof(uint64_t)));
+    boost::asio::read(s2, boost::asio::buffer(&BLIND, sizeof(uint64_t)));
+    boost::asio::read(s2, boost::asio::buffer(&Gamma, sizeof(uint64_t)));
 
 
     for(size_t j = 0; j < logn; ++j)
@@ -88,44 +147,35 @@ int64_t xor_to_additive(bool party, uint8_t ** target_share_read, std::vector<so
         b_blinded[j] = b[j] + BLIND;
     }
 
-    //for(size_t j = 0; j < logn; ++j)
-    {
-        boost::asio::write(socketsPb[0], boost::asio::buffer(&b_blinded, logn * sizeof(b_blinded[0])));
-        boost::asio::read (socketsPb[0], boost::asio::buffer(&b_recv,  logn * sizeof(b_recv[0])));
-    }
+
+    boost::asio::write(sb, boost::asio::buffer(&b_blinded, logn * sizeof(b_blinded[0])));
+    boost::asio::read (sb, boost::asio::buffer(&b_recv,  logn * sizeof(b_recv[0])));
+ 
     
     #ifdef DEBUG
         std::cout << "BLIND = " << BLIND << std::endl;
         std::cout << "Gamma = " << Gamma << std::endl;
     #endif
     
+    uint64_t R_share = 0;
+    
     if(!party) 
     {
-            for(size_t j = 0; j < logn; ++j) 
-            {
-                    #ifdef DEBUG
-                        if(j ==  0)
-                        {
-                            std::cout << "b_recv[j] = " << b_recv[j] << std::endl;
-                            std::cout << "b[j] = " << b[j] << std::endl;
-                        }
-                    #endif
-                    c_mul[j] =  (b[j] * b_recv[j]) + Gamma;
-            }
+        for(size_t j = 0; j < logn; ++j) 
+        {
+            c_mul[j] =  (b[j] * b_recv[j]) + Gamma;
+            d[j] = (b[j] - 2 * c_mul[j]);
+            R_share += (1ULL << j) * d[j];
+        }
     }
 
     if(party)  
     {
         for(size_t j = 0; j < logn; ++j) 
         {       
-            #ifdef DEBUG
-                if(j ==  0)
-                    {
-                        std::cout << "BLIND = " << BLIND << std::endl;
-                        std::cout << "b_blinded[j] = " << b_blinded[j] << std::endl;
-                    }
-            #endif
             c_mul[j] = -(BLIND * b_recv[j]) + Gamma;
+            d[j] = (b[j] - 2 * c_mul[j]);
+            R_share += (1ULL << j) * d[j];
         }
     }
 
@@ -134,79 +184,52 @@ int64_t xor_to_additive(bool party, uint8_t ** target_share_read, std::vector<so
         {
         
             std::cout << "b = " << b[j] << std::endl;
-            
             uint64_t mul_Rec = 0;
-            boost::asio::write(socketsPb[0], boost::asio::buffer(&c_mul[j], sizeof(c_mul[j])));
-            boost::asio::read(socketsPb[0], boost::asio::buffer(&mul_Rec, sizeof(mul_Rec)));
+            boost::asio::write(sb, boost::asio::buffer(&c_mul[j], sizeof(c_mul[j])));
+            boost::asio::read(sb, boost::asio::buffer(&mul_Rec, sizeof(mul_Rec)));
             std::cout << "c_mul = " << c_mul[j] << std::endl;
             mul_Rec = mul_Rec + c_mul[j];
-
             std::cout << "mul_Rec = " << mul_Rec << std::endl;
         }
-    #endif
-
-    
-
-    for(size_t j = 0; j < logn; ++j)
-    {
-        d[j] = (b[j] - 2 * c_mul[j]);
-
-    }
+ 
+        std::array<uint64_t, logn> b_reconstruction_;
+        std::array<uint64_t, logn> d_reconstruction_;
+            std::array<uint64_t, logn> d_recv;
+        for(size_t j = 0; j < logn; ++j)
+        {
+            boost::asio::write(sb, boost::asio::buffer(&d[j], sizeof(d[j])));
+            boost::asio::read(sb, boost::asio::buffer(&d_recv[j], sizeof(d_recv[j])));
+        }
 
-    
+        boost::asio::write(sb, boost::asio::buffer(&b, logn * sizeof(b[0])));
+        boost::asio::read (sb, boost::asio::buffer(&b_recv,  logn * sizeof(b_recv[0])));
 
-    #ifdef DEBUG
-            std::array<uint64_t, logn> b_reconstruction_;
-            std::array<uint64_t, logn> d_reconstruction_;
-             std::array<uint64_t, logn> d_recv;
-            for(size_t j = 0; j < logn; ++j)
-            {
-                boost::asio::write(socketsPb[0], boost::asio::buffer(&d[j], sizeof(d[j])));
-                boost::asio::read(socketsPb[0], boost::asio::buffer(&d_recv[j], sizeof(d_recv[j])));
-            }
-
-                boost::asio::write(socketsPb[0], boost::asio::buffer(&b, logn * sizeof(b[0])));
-                boost::asio::read (socketsPb[0], boost::asio::buffer(&b_recv,  logn * sizeof(b_recv[0])));
-
-                for(size_t j = 0; j < logn; ++j)
-                {
-                    uint64_t d_reconstruction = d[j] + d_recv[j];
-                    d_reconstruction_[j] = d_reconstruction;
-                    uint64_t b_reconstruction = b[j] ^ b_recv[j];
-                    b_reconstruction_[j] = b_reconstruction;
-                    assert(d_reconstruction == b_reconstruction);
-                    //std::cout << d_reconstruction << " <----> " << d[j] << std::endl;
-                    //std::cout << d_reconstruction << " <-> " << b_reconstruction << std::endl;
-                }
+        for(size_t j = 0; j < logn; ++j)
+        {
+            uint64_t d_reconstruction = d[j] + d_recv[j];
+            d_reconstruction_[j] = d_reconstruction;
+            uint64_t b_reconstruction = b[j] ^ b_recv[j];
+            b_reconstruction_[j] = b_reconstruction;
+            assert(d_reconstruction == b_reconstruction);
+        }
         
         uint64_t b_value =	binary_to_decimal(b_reconstruction_);
         std::cout << "b_value = " << b_value << std::endl;
         std::cout << "logn = " << logn << std::endl;
-    #endif
-   
-
-    uint64_t R_share = 0;
-
-    for(size_t j = 0; j < logn; ++j)
-    {
-        R_share += (1ULL << j) * d[j];
-    }
-
-
-    #ifdef DEBUG
+ 
         std::cout << "R_share = " << R_share << std::endl;
         R_share = binary_to_decimal(d);
         std::cout << "R_share = " << R_share << std::endl;
         uint64_t R_share_reconstruction;
-        boost::asio::write(socketsPb[0], boost::asio::buffer(&R_share, sizeof(R_share)));
-        boost::asio::read(socketsPb[0], boost::asio::buffer(&R_share_reconstruction, sizeof(R_share_reconstruction)));
+        boost::asio::write(sb, boost::asio::buffer(&R_share, sizeof(R_share)));
+        boost::asio::read(sb, boost::asio::buffer(&R_share_reconstruction, sizeof(R_share_reconstruction)));
         R_share_reconstruction = R_share_reconstruction + R_share;
         std::cout << "R_share_reconstruction = " << R_share_reconstruction << std::endl;
         std::cout << "b_value                = " << b_value << std::endl;
         std::cout << "d_recons		 = " << binary_to_decimal(d_reconstruction_)<< std::endl;
     #endif
 
-        return R_share;
+    return R_share;
 }
 
 
@@ -245,15 +268,14 @@ void convert_shares(size_t i, __m128i ** output, int8_t ** flags, size_t n_threa
 		}
 	}
 
-	//int64_t rb_prime = du_attalah_Pb(rb, pm, s2, sb);
-
+ 
 	int64_t FCWshare = du_attalah_Pb(final_correction_word[i][1], pm, s2, sb);
 
  
 	
-    FCWshare+=rb;
+ FCWshare+=rb;
 	int64_t FCWshare_reconstruction;
- 	boost::asio::write(sb, boost::asio::buffer(&FCWshare, sizeof(FCWshare)));
+ boost::asio::write(sb, boost::asio::buffer(&FCWshare, sizeof(FCWshare)));
 	boost::asio::read(sb, boost::asio::buffer(&FCWshare_reconstruction, sizeof(FCWshare_reconstruction)));
 	FCWshare_reconstruction = FCWshare_reconstruction + FCWshare;
 
@@ -266,21 +288,23 @@ void convert_shares(size_t i, __m128i ** output, int8_t ** flags, size_t n_threa
 	boost::asio::read(sb, boost::asio::buffer(&PM_recv, sizeof(PM_recv)));
     
     int64_t * flags_  = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
-	int64_t * outs_  = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
+
 	
-	for(size_t j = 0; j < db_nitems; ++j)
-	{
-		outs_[j] = output[0][j][0];
-		leaves[i][j] = output[i][j][0];
 
-		#ifdef DEBUG
-			int64_t out_rec;
-			boost::asio::write(sb, boost::asio::buffer(&outs_[j], sizeof(outs_[j])));
-			boost::asio::read(sb, boost::asio::buffer(&out_rec, sizeof(out_rec)));
-			out_rec = out_rec + outs_[j];
 
-			if(out_rec != 0) std::cout << j << "-> " << out_rec << std::endl;
-		#endif
+    shareconversion P2_shareconversion;
+    P2_shareconversion.PM = pm;
+    P2_shareconversion.PM_recv = PM_recv;
+    P2_shareconversion.rb = rb;
+    P2_shareconversion.FCWshare_reconstruction = FCWshare_reconstruction;
+	
+    boost::asio::write(s2, boost::asio::buffer(&P2_shareconversion, sizeof(shareconversion)));
+    
+    for(size_t j = 0; j < db_nitems; ++j)
+	{
+		leaves[i][j]  = output[i][j][0];
+
+ 
 
 		flags_[j] = (flags[i][j] * pm) + (flags[i][j] * PM_recv) + (flags[i][j] * rb); 
  
@@ -288,11 +312,11 @@ void convert_shares(size_t i, __m128i ** output, int8_t ** flags, size_t n_threa
 
 		if(!party)
 		{	
-			flags_[j] -= (flags[i][j] * FCWshare_reconstruction);		
+		    flags_[j] -= (flags[i][j] * FCWshare_reconstruction);		
 		}
 		if(party)
 		{ 
-			flags_[j] -= (flags[i][j] * FCWshare_reconstruction);
+		    flags_[j] -= (flags[i][j] * FCWshare_reconstruction);
 		}
 	
 		#ifdef DEBUG
@@ -314,16 +338,16 @@ void convert_shares(size_t i, __m128i ** output, int8_t ** flags, size_t n_threa
 		leafbits[i][j] = flags[i][j];
 		
 		#ifdef DEBUG
-			int8_t flags_rec2;
-			boost::asio::write(sb, boost::asio::buffer(&flags[i][j], sizeof(flags[i][j])));
-			boost::asio::read(sb, boost::asio::buffer(&flags_rec2, sizeof(flags_rec2)));
-			flags_rec2 = flags_rec2 + flags[i][j];
-
-			if(flags_rec2 != 0)
-			{
-				std::cout << j << " ---> Flag Reconstruction = " << (int) flags_rec2 << std::endl;
-				if(flags_rec2 != 1) std::cout << (int) flags[i][j]  << "-> " << flags_[j] << std::endl;
-			}
+            int8_t flags_rec2;
+            boost::asio::write(sb, boost::asio::buffer(&flags[i][j], sizeof(flags[i][j])));
+            boost::asio::read(sb, boost::asio::buffer(&flags_rec2, sizeof(flags_rec2)));
+            flags_rec2 = flags_rec2 + flags[i][j];
+
+            if(flags_rec2 != 0)
+            {
+                std::cout << j << " ---> Flag Reconstruction = " << (int) flags_rec2 << std::endl;
+                if(flags_rec2 != 1) std::cout << (int) flags[i][j]  << "-> " << flags_[j] << std::endl;
+            }
 		#endif
 	}