瀏覽代碼

added new comments, more refactoring, changed Makefile to include flags VERBOSE and DEBUG

avadapal 2 年之前
父節點
當前提交
9afdd86d50
共有 5 個文件被更改,包括 360 次插入419 次删除
  1. 5 7
      duoram-online/duoram-read.h
  2. 253 123
      duoram-online/duoram-utils.h
  3. 14 50
      duoram-online/duoram-write.h
  4. 50 69
      duoram-online/duoram.cpp
  5. 38 170
      duoram-online/p2.cpp

+ 5 - 7
duoram-online/duoram-read.h

@@ -1,6 +1,6 @@
 auto DuoramRead(bool party, size_t db_nitems, int64_t ri, int64_t where_to_read, tcp::socket&s2, tcp::socket& s2_b, tcp::socket& sb)
 {
-    read_flags(party, db_nitems);
+    read_flags_for_reading(party, db_nitems);
 
     DB_t Gamma;
 		
@@ -42,21 +42,19 @@ auto DuoramRead(bool party, size_t db_nitems, int64_t ri, int64_t where_to_read,
 auto DuoramIndependentRead(bool party, size_t db_nitems, int64_t ri, DB_t * Gamma_reads, size_t * rotate, size_t r)
 {
 
-	read_flags(party, db_nitems); 
-	
+	read_flags_for_reading(party, db_nitems); 
+
 	DB_t output = 0;
 
 	for(size_t j = 0; j < db_nitems; ++j) reading_temp[j] =  DB[j] + updated_blinded_DB_recv[j];
 	
-	if(!party) output =   dot_product_with_bool(reading_temp, 	reading_b, db_nitems, rotate[r]) + 
+	if(!party) output =   	dot_product_with_bool(reading_temp, 	reading_b, db_nitems, rotate[r]) + 
 							dot_product_with_bool(updated_blinds, reading_b, db_nitems, rotate[r]) - 
 							dot_product_with_bool(updated_blinds, reading_c, db_nitems, rotate[r]) + Gamma_reads[r];
 
-	if(party)  output =   dot_product_with_bool(reading_temp, 	reading_c, db_nitems, rotate[r]) + 
+	if(party)  output =     dot_product_with_bool(reading_temp, 	reading_c, db_nitems, rotate[r]) + 
 							dot_product_with_bool(updated_blinds, reading_c, db_nitems, rotate[r]) - 
 							dot_product_with_bool(updated_blinds, reading_d, db_nitems, rotate[r]) + Gamma_reads[r];
 
-
     return output;
-
 }

+ 253 - 123
duoram-online/duoram-utils.h

@@ -192,14 +192,14 @@ void generate_flags_and_standard_basis_vectors2(bool party, size_t db_nitems)
  	{
       if(party)
       {
-        DB[j] 				= 0;
-        updated_DB[j] 		= 0;
-          blinded_DB_recv[j]  = 0; 
+        DB[j] 				      = 0;
+        updated_DB[j] 		  = 0;
+        blinded_DB_recv[j]  = 0; 
       }
       else
       {
-        DB[j] 				= 0;
-        updated_DB[j] 		= 0;
+        DB[j] 				      = 0;
+        updated_DB[j] 		  = 0;
         blinded_DB_recv[j]  = 0; 
       }
  	}
@@ -251,70 +251,6 @@ DB_t print_reconstruction(tcp::socket& sb, DB_t output)
 }
 
 
-template<typename T> 
-void rotate_by(T * orginal_vector, T * rotated_vector, size_t rotate_by, size_t db_nitems)
-{
-
- 			 for(size_t j = 0; j < db_nitems; ++j)
-			 {
-				
-			 	 rotated_vector[(j + rotate_by) % db_nitems] = orginal_vector[j];
-			 	 
-			 }
-}
-
-template<typename T1, typename T2> 
-void rotate_all( T1 * orginal_vector1, T1 * rotated_vector1,
-								 T2 * orginal_vector2, T2 * rotated_vector2,
-								 T1 * orginal_vector3, T1 * rotated_vector3,
-							   T2 * orginal_vector4, T2 * rotated_vector4,
-								 T1 * orginal_vector5, T1 * rotated_vector5, 
-								 T2 * orginal_vector6, T2 * rotated_vector6,
-								size_t rotate_by, size_t db_nitems)
-{
-
- 			 for(size_t j = 0; j < db_nitems; ++j)
-			 {
-				
-			 	 rotated_vector1[(j + rotate_by) % db_nitems] = orginal_vector1[j];
-			 	 rotated_vector2[(j + rotate_by) % db_nitems] = orginal_vector2[j];
-			 	 rotated_vector3[(j + rotate_by) % db_nitems] = orginal_vector3[j];
-			 	 rotated_vector4[(j + rotate_by) % db_nitems] = orginal_vector4[j];
-			 	 rotated_vector5[(j + rotate_by) % db_nitems] = orginal_vector5[j];
-			 	 rotated_vector6[(j + rotate_by) % db_nitems] = orginal_vector6[j];
-
-			 	 
-			 }
-}
-
-template<typename T1> 
-void rotate_all( T1 * orginal_vector1, T1 * rotated_vector1,
-								 T1 * orginal_vector3, T1 * rotated_vector3,
-							   T1 * orginal_vector5, T1 * rotated_vector5, 
-								size_t rotate_by, size_t db_nitems)
-{
-
- 			 for(size_t j = 0; j < db_nitems; ++j)
-			 {
-				
-			 	 rotated_vector1[(j + rotate_by) % db_nitems] = orginal_vector1[j];
-			 	 rotated_vector3[(j + rotate_by) % db_nitems] = orginal_vector3[j];
-			 	 rotated_vector5[(j + rotate_by) % db_nitems] = orginal_vector5[j];			 	 
-			 }
-}
-void read_final_cws(tcp::socket& in2)
-{
-
-	  read(in2, boost::asio::buffer(&one_shareA, sizeof(one_shareA)));
-	  read(in2, boost::asio::buffer(&one_shareB, sizeof(one_shareB)));
-	  read(in2, boost::asio::buffer(&one_shareC, sizeof(one_shareC)));
-	// in2 >> one_shareA;
-
- // 	in2 >> one_shareB;
- 	
- // 	in2 >> one_shareC;
-}
-
 int read_database_shares(bool party, size_t db_nitems)
 {
 	if(party)
@@ -345,12 +281,18 @@ int read_database_shares(bool party, size_t db_nitems)
 	return 0;
 }
 
+
+
+
+
+
+
 int read_flags(bool party, size_t db_nitems)
 {
 		if(!party)
-	{
-		int const in0 { open( "party0_read_flags_b", O_RDONLY ) };
-	 	size_t r = read(in0, reading_b,  db_nitems * sizeof(reading_b[0]));	
+  	{
+	 	 int const in0 { open( "party0_read_flags_b", O_RDONLY ) };
+	   size_t r = read(in0, reading_b,  db_nitems * sizeof(reading_b[0]));	
  		 if(r < 0) {
      perror("Read error");
      close(in0);
@@ -493,29 +435,248 @@ int read_flags(bool party, size_t db_nitems)
  		 }
 	}
 
-	// if(party) 
- //  {
- //    	printf("PARTY 1\n");
- //    	for(size_t j = 0; j < db_nitems; ++j)
- //    	{
- //    		if(writing_b[j]) pm0 += 1;
- //    		if(writing_c[j]) pm1 += 1;
- //    		if(writing_d[j]) pm2 += 1;
- //    	}
- //   }
- //   else
- // 	 {
- // 	 	 	printf("PARTY 0\n");
- //   		for(size_t j = 0; j < db_nitems; ++j)
- //    	{
-	//  	 	 	if(writing_b[j]) pm0 -= 1;
- //    		if(writing_c[j]) pm1 -= 1;
- //    		if(writing_d[j]) pm2 -= 1;
- //    	}
- //    }
+ 
+	return 0;	
+}
+
+
+int read_flags_for_reading(bool party, size_t db_nitems)
+{
+		if(!party)
+	{
+		int const in0 { open( "party0_read_flags_b", O_RDONLY ) };
+	 	size_t r = read(in0, reading_b,  db_nitems * sizeof(reading_b[0]));	
+ 		 if(r < 0) {
+     perror("Read error");
+     close(in0);
+     return 1;
+ 		 }
+  
+     int const in1 { open( "party0_read_flags_c", O_RDONLY ) };
+		 r = read(in1, reading_c,  db_nitems * sizeof(reading_c[0]));
+		 if(r < 0) {
+     perror("Read error");
+     close(in1);
+     return 1;
+ 		 }
+
+	   int const in2 { open( "party0_read_flags_d", O_RDONLY ) };
+		 r = read(in2, reading_d,  db_nitems * sizeof(reading_d[0]));	
+		 if(r < 0) {
+     perror("Read error");
+     close(in2);
+     return 1;
+     }
+	}
+
+	if(party)
+	{
+		 int const in0 { open( "party1_read_flags_b", O_RDONLY ) };
+	 	 size_t r = read(in0, reading_b,  db_nitems * sizeof(reading_b[0]));	
+	 	 if(r < 0) {
+     perror("Read error");
+     close(in0);
+     return 1;
+ 		 }
+ 	   int const in1 { open( "party1_read_flags_c", O_RDONLY ) };
+		 r = read(in1, reading_c,  db_nitems * sizeof(reading_c[0]));
+		 if(r < 0) {
+     perror("Read error");
+     close(in1);
+     return 1;
+ 		 }
+		 int const in2 { open( "party1_read_flags_d", O_RDONLY ) };
+		 r = read(in2, reading_d,  db_nitems * sizeof(reading_d[0]));	
+		 if(r < 0) {
+     perror("Read error");
+     close(in2);
+     return 1;
+ 		 }
+
+ 
+	}
+
+ 
 	return 0;	
 }
 
+int read_flags_for_writing(bool party, size_t db_nitems)
+{
+		if(!party)
+	{
+
+		 int const in0_w { open( "party0_write_flags_b", O_RDONLY ) };
+	   size_t r = read(in0_w, writing_b,  db_nitems * sizeof(writing_b[0]));	
+	 	 if(r < 0) {
+     perror("Read error");
+     close(in0_w);
+     return 1;
+ 		 }
+ 	   int const in1_w { open( "party0_write_flags_c", O_RDONLY ) };
+		 r = read(in1_w, writing_c,  db_nitems * sizeof(writing_c[0]));
+		 if(r < 0) {
+     perror("Read error");
+     close(in1_w);
+     return 1;
+ 		 }
+		 int const in2_w { open( "party0_write_flags_d", O_RDONLY ) };
+		 r = read(in2_w, writing_d,  db_nitems * sizeof(writing_d[0]));	
+		 if(r < 0) {
+     perror("Read error");
+     close(in2_w);
+     return 1;
+ 		 }
+
+ 
+	}
+
+	if(party)
+	{
+ 		 int const in0_w { open( "party1_write_flags_b", O_RDONLY ) };
+	 	 size_t r = read(in0_w, writing_b,  db_nitems * sizeof(writing_b[0]));	
+	 	 if(r < 0) {
+     perror("Read error");
+     close(in0_w);
+     return 1;
+ 		 }
+ 	   int const in1_w { open( "party1_write_flags_c", O_RDONLY ) };
+		 r = read(in1_w, writing_c,  db_nitems * sizeof(writing_c[0]));
+		 if(r < 0) {
+     perror("Read error");
+     close(in1_w);
+     return 1;
+ 		 }
+		 int const in2_w { open( "party1_write_flags_d", O_RDONLY ) };
+		 r = read(in2_w, writing_d,  db_nitems * sizeof(writing_d[0]));	
+		 if(r < 0) {
+     perror("Read error");
+     close(in2_w);
+     return 1;
+ 		 }
+
+
+ 
+	}
+
+ 
+	return 0;	
+}
+
+
+int read_flags_for_updating(bool party, size_t db_nitems)
+{
+	if(!party)
+	{
+		 int const in0_w_ { open( "party0_write_b", O_RDONLY ) };
+	 	 size_t r = read(in0_w_, b,  db_nitems * sizeof(b[0]));	
+	 	 if(r < 0) {
+     perror("Read error");
+     close(in0_w_);
+     return 1;
+ 		 }
+ 	   int const in1_w_ { open( "party0_write_c", O_RDONLY ) };
+		 r = read(in1_w_, c,  db_nitems * sizeof(c[0]));
+		 if(r < 0) {
+     perror("Read error");
+     close(in1_w_);
+     return 1;
+ 		 }
+		 int const in2_w_ { open( "party0_write_d", O_RDONLY ) };
+		 r = read(in2_w_, d,  db_nitems * sizeof(d[0]));	
+		 if(r < 0) {
+     perror("Read error");
+     close(in2_w_);
+     return 1;
+ 		 }
+	}
+
+	if(party)
+	{
+ 		 int const in0_w_ { open( "party1_write_b", O_RDONLY ) };
+	 	 size_t r = read(in0_w_, b,  db_nitems * sizeof(b[0]));	
+	 	 if(r < 0) {
+     perror("Read error");
+     close(in0_w_);
+     return 1;
+ 		 }
+ 	   int const in1_w_ { open( "party1_write_c", O_RDONLY ) };
+		 r = read(in1_w_, c,  db_nitems * sizeof(c[0]));
+		 if(r < 0) {
+     perror("Read error");
+     close(in1_w_);
+     return 1;
+ 		 }
+		 int const in2_w_ { open( "party1_write_d", O_RDONLY ) };
+		 r = read(in2_w_, d,  db_nitems * sizeof(d[0]));	
+		 if(r < 0) {
+     perror("Read error");
+     close(in2_w_);
+     return 1;
+ 		 }
+	}
+
+ 
+	return 0;	
+}
+
+ int read_flags_for_refreshing(size_t db_nitems)
+ {
+        int const in1_w { open( "party0_write_flags_c", O_RDONLY ) };
+        size_t r = read(in1_w, writing_c,  db_nitems * sizeof(writing_c[0]));
+        if(r < 0) {
+        perror("Read error");
+        close(in1_w);
+        return 1;
+        }
+        
+        int const in2_w { open( "party1_write_flags_d", O_RDONLY ) };
+        r = read(in2_w, writing_d,  db_nitems * sizeof(writing_d[0])); 
+        if(r < 0) {
+        perror("Read error");
+        close(in2_w);
+        return 1;
+        }
+
+        int const in1_w_ { open( "party0_write_c", O_RDONLY ) };
+        r = read(in1_w_, c,  db_nitems * sizeof(c[0]));
+        if(r < 0) {
+        perror("Read error");
+        close(in1_w_);
+        return 1;
+        }
+        
+        int const in2_w_ { open( "party1_write_d", O_RDONLY ) };
+        r = read(in2_w_, d,  db_nitems * sizeof(d[0]));  
+        if(r < 0) {
+        perror("Read error");
+        close(in2_w_);
+        return 1;
+        }
+
+        return 0;
+ }
+
+int read_flags_for_generating_cancellation_terms(size_t db_nitems)
+{
+     int const in2 { open( "party1_read_flags_d", O_RDONLY ) };
+     size_t r = read(in2, reading_d,  db_nitems * sizeof(reading_d[0])); 
+     if(r < 0) {
+     perror("Read error");
+     close(in2);
+     return 1;
+     }
+
+     int const in2_ { open( "party0_read_flags_c", O_RDONLY ) };
+     r = read(in2_, reading_c,  db_nitems * sizeof(reading_c[0])); 
+     if(r < 0) {
+     perror("Read error");
+     close(in2_);
+     return 1;
+     }
+   
+    return 0;
+}
+
 void generate_random_distinguished_points(bool party)
 {
 	if(party)
@@ -563,35 +724,4 @@ DB_t dot_product_with_bool(DB_t D[], int8_t flags[], size_t db_nitems, size_t ro
   return result;
 }
 
-// template<typename T1, typename T2>
-// T1 dot_product(T1 X[], T2 Y[])
-// {
-// 		T1 dot = 0;
-
- 
-// 		for(size_t j = 0; j < db_nitems; ++j)
-// 		{
-// 				dot ^= (X[j] & Y[j]);
-
-     
-// 		}
-
-// 			return dot;
-// }
-
-// void generate_cws(DB_t M0, DB_t final_cw_w, DB_t final_cw_u)
-// { 
-
-
-// 	final_cw_writing  = M0 ^ final_cw_w;	
-
-// 	final_cw_updating = M0 ^ final_cw_u;
- 
-// }
-
-
-  
-
-
-
  

+ 14 - 50
duoram-online/duoram-write.h

@@ -1,46 +1,16 @@
  
- 
- // void  write_into_the_database(DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t b[],  DB_t final_CW, bool party, size_t db_nitems)
- // {
-
- // 	//DB_t tmp = 0;
- // 	if(!party) final_CW = -final_CW;
- // 	for(size_t j = 0; j < db_nitems; ++j)
- // 	{
- // 		// if(flags[j] != 0) 
- // 		// {
- // 		//  tmp = final_CW;
- // 		// }
- // 		// else
- // 		// {
- // 		//  tmp = 0;
- // 		// }
- // 		DB[j] = DB[j] + b[j]  + ((flags[j] != 0) ? final_CW : 0);;
-
-	// 	// if(!party) updated_DB[j] = DB[j] + b[j]  - tmp;
- // 	//     if(party)  updated_DB[j] = DB[j] + b[j]  + tmp; 	    
- // 	}
- // }
- void  write_into_the_database(DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t b[],  DB_t final_CW, bool party, size_t db_nitems, size_t rotate_by = 0)
- {
+void  write_into_the_database(DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t b[],  DB_t final_CW, bool party, size_t db_nitems, size_t rotate_by = 0)
+{
 
- //	if(!party) final_CW = -final_CW;
-	
-	//std::cout << "rotate_by = " << rotate_by << std::endl;
-//	std::cout << "final_CW =  " << final_CW << std::endl;
  	for(size_t j = 0; j < db_nitems; ++j)
  	{ 
-// 	  DB[j] = DB[j] + b[(j + rotate_by) % db_nitems]  + ((flags[(j + rotate_by) % db_nitems] != 0) ? final_CW : 0);   
-	  const size_t ind = (j + rotate_by) % db_nitems;
-	  
-	  //DB_t tmp =  ( flags[(j + rotate_by) % db_nitems]   * final_CW);
-		DB_t tmp = (flags[ind] * final_CW);
-	   //std::cout << j <<  " --->> tmp = " << tmp << " ---- " << (int)flags[(j + rotate_by) % db_nitems] << " * " << final_CW <<  "----> ind = " << ind <<  std::endl;
-		DB[j] = DB[j] + b[ind]  + tmp;     
+	   const size_t ind = (j + rotate_by) % db_nitems;
+	 	DB_t tmp = (flags[ind] * final_CW);
+	 	DB[j] = DB[j] + b[ind]  + tmp;     
 		updated_DB[j] = DB[j];
-	   //DB[j] = DB[j] + b[(j + rotate_by) % db_nitems]  + tmp;     
  	}
- }
+}
+
 void duoram_update(tcp::socket& in2, tcp::socket& sb, DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t standard_basis[], DB_t M0, DB_t alpha0, bool party, size_t db_nitems, size_t offset = 0)
 {	 	
 
@@ -57,7 +27,6 @@ void duoram_update(tcp::socket& in2, tcp::socket& sb, DB_t DB[], DB_t updated_DB
 
 }
 
-
 void reconstruct_database(tcp::socket& sb, DB_t DB[], const size_t db_nitems)
 {
 		for(size_t j = 0; j < db_nitems; ++j)
@@ -76,17 +45,12 @@ auto DuoramUpdate(bool party, size_t db_nitems,   size_t rotate_by_,
 				  int8_t writing_c[], int8_t writing_d[], DB_t c[], DB_t d[])
 {
 
-			// final_CW = final_CW + message;
-			// read_flags(party, db_nitems);
-		 	
-		    // rotate_by_[w] = WritePb_[w].shift + WritePb_recv[w].shift;		 			
-			
-			// std::cout << "rotate_by_[w] = " << rotate_by_[w] << std::endl;
-   			
-			  write_into_the_database(DB, updated_DB, writing_b, b, final_CW, party, db_nitems, - rotate_by_);
+	read_flags_for_updating(party, db_nitems);
+	
+	write_into_the_database(DB, updated_DB, writing_b, b, final_CW, party, db_nitems, - rotate_by_);
 			
-			#ifdef ThreeParty
-				if(!party) duoram_refresh(writing_b, writing_c, writing_d, b, c, d,  final_CW, party, db_nitems, -rotate_by_); 			
-				if( party) duoram_refresh(writing_b, writing_d, writing_c, b, d, c,  final_CW, party, db_nitems, -rotate_by_); 
-			#endif
+	#ifdef ThreeParty
+		if(!party) duoram_refresh(writing_b, writing_c, writing_d, b, c, d,  final_CW, party, db_nitems, -rotate_by_); 			
+		if( party) duoram_refresh(writing_b, writing_d, writing_c, b, d, c,  final_CW, party, db_nitems, -rotate_by_); 
+	#endif
 }

+ 50 - 69
duoram-online/duoram.cpp

@@ -13,47 +13,32 @@
 #include <cstring>      // std::memcpy
 #include <bsd/stdlib.h> // arc4random_buf
 #include <x86intrin.h>  // SSE and AVX intrinsics
-#include "prg.h"
-#include "prg_aes_impl.h"
-
-
-const size_t ncores = 16;
-uint64_t progress[ncores] = {0};
- 
+#include <chrono>
 #include <thread>
-#include <iostream>
 #include <deque>
- 
 #include <../boost/asio.hpp>
 
 using boost::asio::ip::tcp;
-
-
-#include "block.h"
-#include <type_traits>  
-
-#include <chrono>
-
 using namespace std::chrono;
+using namespace std;
+using socket_t = boost::asio::ip::tcp::socket;
 
- 
- 
+#include "prg.h"
+#include "prg_aes_impl.h"
+#include "block.h"  
 #include "duoram-utils.h"
 #include "duoram-read.h"
 #include "duoram-refresh.h"
 #include "duoram-write.h"
 #include "readvectors.h"
-using namespace std;
 
-using socket_t = boost::asio::ip::tcp::socket;
+
  
- void accept_conncections_from_Pb(boost::asio::io_context&io_context, std::vector<socket_t>& sockets_, int port, size_t j)
+void accept_conncections_from_Pb(boost::asio::io_context&io_context, std::vector<socket_t>& sockets_, int port, size_t j)
 {
  	 tcp::acceptor acceptor_a(io_context, tcp::endpoint(tcp::v4(), port));
 	 tcp::socket sb_a(acceptor_a.accept());
-	 sockets_[j] = std::move(sb_a); 
-   
-  // sockets_.emplace_back(std::move(sb_a)); 
+	 sockets_[j] = std::move(sb_a);    
 }
 
  
@@ -61,26 +46,23 @@ using socket_t = boost::asio::ip::tcp::socket;
 int main(const int argc, const char * argv[])
 {
 	
-	 size_t expo =  atoi(argv[3]);
-	 size_t db_nitems = 1ULL << expo;
-  
+   size_t expo =  atoi(argv[3]);
+   size_t db_nitems = 1ULL << expo;
    size_t number_of_writes	   =  atoi(argv[4]);
    size_t number_of_ind_reads  =  atoi(argv[5]); 
    size_t number_of_dep_reads  =  atoi(argv[6]);
    size_t number_of_accesses   =  atoi(argv[7]);
    
    reading_temp = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
- 	
-  	DB = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
- 	updated_DB = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
- 	blinded_DB = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
+   DB = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
+   updated_DB = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
+   blinded_DB = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
   	
-	blinded_DB_recv = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
-	updated_blinded_DB_recv = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
-	
+    blinded_DB_recv = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
+    updated_blinded_DB_recv = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
 	b = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
-	c = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
-	d = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
+    c = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
+    d = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
 		
 	reading_b = (int8_t *) malloc(db_nitems * sizeof(int8_t));
 	reading_c = (int8_t *) malloc(db_nitems * sizeof(int8_t));
@@ -90,10 +72,7 @@ int main(const int argc, const char * argv[])
 	writing_d = (int8_t *) malloc(db_nitems * sizeof(int8_t));
 
  
-
-
- // size_t read_at[number_of_writes];
- // size_t rand_point[number_of_writes];
+ 
  size_t * rotate_by_ = new size_t[number_of_writes];
 
 
@@ -185,24 +164,19 @@ int main(const int argc, const char * argv[])
  
 	generate_random_distinguished_points(party);
   
-    AES_KEY aeskey;	 
-
- 	//read_final_cws(sockets_2[0]);
- 
-
- 
+    AES_KEY aeskey;	  
     blinds =	(DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));	
     updated_blinds =	(DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
 
-   	setup(DB, updated_DB, blinded_DB_recv, blinds, updated_blinds, updated_blinded_DB_recv, db_nitems, party);
+	size_t * where_to_write = new size_t[number_of_writes];
+	size_t * where_to_read_dependent = new size_t[number_of_dep_reads];
+	size_t * where_to_read_independent = new size_t[number_of_ind_reads];
+   
    	
-	auto start_total = std::chrono::steady_clock::now();
-	
+	auto start_total = std::chrono::steady_clock::now();	
+
+	setup(DB, updated_DB, blinded_DB_recv, blinds, updated_blinds, updated_blinded_DB_recv, db_nitems, party);
 
-		size_t * where_to_write = new size_t[number_of_writes];
-		size_t * where_to_read_dependent = new size_t[number_of_dep_reads];
-		size_t * where_to_read_independent = new size_t[number_of_ind_reads];
-	
 	for(size_t i = 0; i < number_of_accesses; ++i)
 	{
 		for(size_t j = 0; j < number_of_writes; ++j)
@@ -235,12 +209,11 @@ int main(const int argc, const char * argv[])
 
 		#ifdef VERBOSE
 			boost::asio::write(sockets_[0], boost::asio::buffer(&ri, sizeof(ri)));
-			boost::asio::read(sockets_[0], boost::asio::buffer(&ri_recv, sizeof(ri_recv)));
-		
+			boost::asio::read(sockets_[0], boost::asio::buffer(&ri_recv, sizeof(ri_recv)));		
 			int64_t ri_reconstruction = ri + ri_recv;
 			std::cout << "ri_reconstruction = " << ri_reconstruction << std::endl;
 		#endif
-		
+
 		#ifdef ThreeParty
 			DuORAM_Write * WritePb_     = new DuORAM_Write[number_of_writes];
 			DuORAM_Write * WritePb_recv = new DuORAM_Write[number_of_writes];
@@ -258,6 +231,7 @@ int main(const int argc, const char * argv[])
 			{
 				DB_t FCW_read = 0;
 
+				/*The definition of read_final_correction_word appears in duoram-utils.h*/ 	
 				read_final_correction_word(party, FCW_read);
 				
 				#ifdef VERBOSE
@@ -271,18 +245,21 @@ int main(const int argc, const char * argv[])
 				read(sockets_2[1], boost::asio::buffer(&Gamma[w],  sizeof(DB_t))); 
 				boost::asio::write(sockets_[0],  boost::asio::buffer(&WritePb_[w],    sizeof(DuORAM_Write)));
 				boost::asio::read(sockets_[0],   boost::asio::buffer(&WritePb_recv[w], sizeof(DuORAM_Write)));
-					
-				read_flags(party, db_nitems);
+
+				/*The definition of read_flags_for_writing appears in duoram-utils.h*/ 	
+				read_flags_for_writing(party, db_nitems);
 	
 				rotate_by_[w] = WritePb_[w].shift + WritePb_recv[w].shift;		 			
 
 				#ifdef VERBOSE 		
-					std::cout << "print database: " << std::endl;							
+					std::cout << "print database: " << std::endl;
+					/* The definition of reconstruct_database appears in duoram-utils.h */							
 					reconstruct_database(sockets_[0],  DB, db_nitems);
 				#endif
 
 				for(size_t j = 0; j < db_nitems; ++j) reading_temp[j] = DB[j] + updated_blinded_DB_recv[j];
-							
+				
+				/*The definition of dot_product_with_bool appears in duoram-write.h*/ 
 				if(!party) read_out[w] 		=   dot_product_with_bool(reading_temp,   writing_b, db_nitems, rotate_by_[w]) + 
 												dot_product_with_bool(updated_blinds, writing_b, db_nitems, rotate_by_[w]) - 
 												dot_product_with_bool(updated_blinds, writing_c, db_nitems, rotate_by_[w]) + Gamma[w];
@@ -291,19 +268,19 @@ int main(const int argc, const char * argv[])
 												dot_product_with_bool(updated_blinds, writing_c, db_nitems, rotate_by_[w])	- 
 												dot_product_with_bool(updated_blinds, writing_d, db_nitems, rotate_by_[w]) + Gamma[w];
 							
-				#ifdef DEBUG
+				#ifdef VERBOSE
 					std::cout << "read_out[" << w << "] = " << read_out[w] << std::endl;
 				#endif
 
-				#ifdef DEBUG
+				#ifdef VERBOSE
 				std::cout << "reconstructing the output: " << print_reconstruction(sockets_[0], read_out[w]) << "\n";	
 				#endif
 			
 				distinguised_value[0] = 80 * (1 + w);
 				update_message[w] = distinguised_value[0] - read_out[w]   + alpha0;
 
-					#ifdef DEBUG
-						std::cout << "-> The updated message shares is " << update_message[w] << std::endl;
+					#ifdef VERBOSE
+						std::cout << "The updated message shares is = " << update_message[w] << std::endl;
 					#endif 
 				boost::asio::write(sockets_2[2], boost::asio::buffer(&update_message[w],  sizeof(DB_t)));
 				boost::asio::write(sockets_[2], boost::asio::buffer(&update_message[w], sizeof(DB_t)));
@@ -311,17 +288,21 @@ int main(const int argc, const char * argv[])
 					
 				CW[w] = CW[w] + update_message[w];
 
-				#ifdef DEBUG
+				#ifdef VERBOSE
 					std::cout << "cw = " << CW[w] << std::endl;
 				#endif 
 			}
 		#endif
 
 		for(size_t w = 0; w < number_of_writes; ++w)
- 		{		 
+ 		{	
+			
+			/*The definition of DuoramUpdate appears in duoram-write.h*/  	 
 		  	DuoramUpdate(party,  db_nitems,  rotate_by_[w],  DB, updated_DB, writing_b, b,  CW[w],  update_message[w],  writing_c, writing_d, c, d);
+			
 			#ifdef DEBUG		
 				#ifdef ThreeParty
+					/*The definition debug_ appears in duoram-utils.h*/  
 					debug_(sockets_2[0], sockets_[0], db_nitems);
 				#endif
 			#endif
@@ -332,13 +313,13 @@ int main(const int argc, const char * argv[])
    	printf("elapsed_seconds_writes = %f\n",elapsed_seconds_writes.count());
 	
 	#ifdef VERBOSE
+		std::cout << "Reconstructing the database after doing " << number_of_writes << " writes " << std::endl;
 		reconstruct_database(sockets_[0],  DB, db_nitems);
 	#endif
-
 	// WRITES END.
-  	#ifdef ThreeParty
 
-		printf("\n\n================================= WRITES END =================================\n\n\n");	
+  	#ifdef ThreeParty
+		std::cout << std::endl << std::endl << "============== WRITES END ==============" << std::endl << std::endl;
  		auto start_ind_reads = std::chrono::steady_clock::now();
 		size_t *  WritePb_ind_reads = new size_t[number_of_ind_reads];
 		size_t *  WritePb_ind_reads_recv = new size_t[number_of_ind_reads];

+ 38 - 170
duoram-online/p2.cpp

@@ -13,10 +13,7 @@
 #include <bsd/stdlib.h> // arc4random_buf
 #include <x86intrin.h>  // SSE and AVX intrinsics
 #include "prg.h"
-#include "prg_aes_impl.h"
-
-#include <iostream>
- 
+#include "prg_aes_impl.h" 
 #include "block.h"
 #include <chrono>
 #include <sys/mman.h>
@@ -26,8 +23,8 @@
 #include <../boost/asio.hpp>
 #include <mutex>
 #include <../boost/lexical_cast.hpp>
-#include <memory>
-#include <../boost/asio.hpp>
+ 
+ 
  
 using boost::asio::ip::tcp;
 //#include "network.h"
@@ -56,53 +53,8 @@ const leaf_t val =   _mm_set1_epi8(0x01);
  using socket_t = boost::asio::ip::tcp::socket;
  
 
-
- using namespace std;
- 
-
+using namespace std;
  
-void quick_fix_send_ones_share(tcp::socket& out0, tcp::socket& out1)
-{
-
-  DB_t one = 47920;//0xffffffffffffffff;
-  
-  DB_t S0, S1;
-  arc4random_buf(&S0, sizeof(DB_t));
-  S1 = - S0 + one;
-  // out0 << S0; 
-  // out1 << S1;
-  
-  write(out0, boost::asio::buffer(&S0, sizeof(S0)));
-  write(out1, boost::asio::buffer(&S1, sizeof(S1)));
-    
-
-
-  //arc4random_buf(&S0, sizeof(DB_t));
-  S1 = - S0 + one;  
-  // out0 << S0; 
-  // out1 << S1;
- 
-  write(out0, boost::asio::buffer(&S0, sizeof(S0)));
-  write(out1, boost::asio::buffer(&S1, sizeof(S1)));
- 
- 
-
-
-  //arc4random_buf(&S0, sizeof(DB_t));
-  S1 = - S0 + one;
-  // out0 << S0; 
-  // out1 << S1;
-  write(out0, boost::asio::buffer(&S0, sizeof(S0)));
-  write(out1, boost::asio::buffer(&S1, sizeof(S1)));
- 
-}
-
-
-
-
- 
- 
-
 auto generate_cancelation_terms(int8_t c0[], int8_t d1[], size_t db_nitems, size_t rotate_by = 0)
 {
    DB_t Z0 =  dot_product_with_bool(X0, d1, db_nitems, rotate_by);
@@ -204,114 +156,33 @@ int main(int argc, char* argv[])
 
       pool2.join();
 
-    //  int const in1_w_ { open( "party0_write_c", O_RDONLY ) };
-    //  size_t r = read(in1_w_, c,  db_nitems * sizeof(c[0]));
-    //  if(r < 0) {
-    //  perror("Read error");
-    //  close(in1_w_);
-    //  return 1;
-    //  }
-    //  int const in2_w_ { open( "party1_write_d", O_RDONLY ) };
-    //  r = read(in2_w_, d,  db_nitems * sizeof(d[0]));  
-    //  if(r < 0) {
-    //  perror("Read error");
-    //  close(in2_w_);
-    //  return 1;
-    //  }
-
-    //  int const in1_w { open( "party0_write_flags_c", O_RDONLY ) };
-    //  r = read(in1_w, writing_c,  db_nitems * sizeof(writing_c[0]));
-    //  if(r < 0) {
-    //  perror("Read error");
-    //  close(in1_w);
-    //  return 1;
-    //  }
-    //  int const in2_w { open( "party1_write_flags_d", O_RDONLY ) };
-    //  r = read(in2_w, writing_d,  db_nitems * sizeof(writing_d[0])); 
-    //  if(r < 0) {
-    //  perror("Read error");
-    //  close(in2_w);
-    //  return 1;
-    //  }
-
-    int const in2 { open( "party1_read_flags_d", O_RDONLY ) };
-    size_t r = read(in2, reading_d,  db_nitems * sizeof(reading_d[0])); 
-     if(r < 0) {
-     perror("Read error");
-     close(in2);
-     return 1;
-     }
-
-     int const in2_ { open( "party0_read_flags_c", O_RDONLY ) };
-     r = read(in2_, reading_c,  db_nitems * sizeof(reading_c[0])); 
-     if(r < 0) {
-     perror("Read error");
-     close(in2_);
-     return 1;
-     }
 
  
-  
   X0 = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
   X1 = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
     
- 
- 
- 
-  for(size_t j = 0; j < db_nitems; ++j)
-  {
-    X0[j] = 0;
-    X1[j] = 0; 
-  }
+ for(size_t j = 0; j < db_nitems; ++j)
+ {
+   X0[j] = 0;
+   X1[j] = 0; 
+ }
 
  
-    DuORAM_Write * WriteP0_recv = new DuORAM_Write[number_of_ind_writes];
-    DuORAM_Write * WriteP1_recv = new DuORAM_Write[number_of_ind_writes];
-
-    DB_t * Gamma0_ = new DB_t[number_of_ind_writes];
-    DB_t * Gamma1_ = new DB_t[number_of_ind_writes];
-    DB_t * update_message0 = new DB_t[number_of_ind_writes];
-    DB_t * update_message1 = new DB_t[number_of_ind_writes];
-    DB_t * FCW = new DB_t[number_of_ind_writes];
-
-for(size_t i = 0; i < number_of_accesses; ++i)
-{
+ DuORAM_Write * WriteP0_recv = new DuORAM_Write[number_of_ind_writes];
+ DuORAM_Write * WriteP1_recv = new DuORAM_Write[number_of_ind_writes];
+
+ DB_t * Gamma0_ = new DB_t[number_of_ind_writes];
+ DB_t * Gamma1_ = new DB_t[number_of_ind_writes];
+ DB_t * update_message0 = new DB_t[number_of_ind_writes];
+ DB_t * update_message1 = new DB_t[number_of_ind_writes];
+ DB_t * FCW = new DB_t[number_of_ind_writes];
+ 
+ for(size_t i = 0; i < number_of_accesses; ++i)
+ {
     for(size_t w = 0; w < number_of_ind_writes; ++w)
     {
- 
+      read_flags_for_refreshing(db_nitems);
 
-        int const in1_w { open( "party0_write_flags_c", O_RDONLY ) };
-        size_t r = read(in1_w, writing_c,  db_nitems * sizeof(writing_c[0]));
-        if(r < 0) {
-        perror("Read error");
-        close(in1_w);
-        return 1;
-        }
-        
-        int const in2_w { open( "party1_write_flags_d", O_RDONLY ) };
-        r = read(in2_w, writing_d,  db_nitems * sizeof(writing_d[0])); 
-        if(r < 0) {
-        perror("Read error");
-        close(in2_w);
-        return 1;
-        }
-
-         int const in1_w_ { open( "party0_write_c", O_RDONLY ) };
-         r = read(in1_w_, c,  db_nitems * sizeof(c[0]));
-          if(r < 0) {
-          perror("Read error");
-          close(in1_w_);
-          return 1;
-          }
-          int const in2_w_ { open( "party1_write_d", O_RDONLY ) };
-          r = read(in2_w_, d,  db_nitems * sizeof(d[0]));  
-          if(r < 0) {
-          perror("Read error");
-          close(in2_w_);
-          return 1;
-          }
-      
-      
       size_t rotate_by_ = WriteP0_recv[w].shift + WriteP1_recv[w].shift;
      
       auto [Gamma0, Gamma1] = generate_cancelation_terms(writing_c, writing_d, db_nitems, rotate_by_);
@@ -325,8 +196,7 @@ for(size_t i = 0; i < number_of_accesses; ++i)
 
       boost::asio::read(sockets_0[2], boost::asio::buffer(&update_message0[w],  sizeof(DB_t))); 
       boost::asio::read(sockets_1[2], boost::asio::buffer(&update_message1[w],  sizeof(DB_t)));
-      FCW[w] = update_message0[w] + update_message1[w];
-      
+      FCW[w] = update_message0[w] + update_message1[w];      
     }
 
     for(size_t w = 0; w < number_of_ind_writes; ++w)
@@ -343,9 +213,6 @@ for(size_t i = 0; i < number_of_accesses; ++i)
       #endif
     }
 
-
-
-    
     DB_t * Gamma0_reads = new DB_t[number_of_ind_reads];
     DB_t * Gamma1_reads = new DB_t[number_of_ind_reads];
 
@@ -357,14 +224,15 @@ for(size_t i = 0; i < number_of_accesses; ++i)
     boost::asio::read(sockets_0[3], boost::asio::buffer(reads_shift_from_P0, number_of_ind_reads * sizeof(size_t))); 
     boost::asio::read(sockets_1[3], boost::asio::buffer(reads_shift_from_P1, number_of_ind_reads * sizeof(size_t)));
 
-    for(size_t r = 0; r < number_of_ind_reads; ++r) rotate[r] = reads_shift_from_P0[r] + reads_shift_from_P1[r];
-
     for(size_t r = 0; r < number_of_ind_reads; ++r)
     {  
       #ifdef DEBUG
         std::cout << "rotate[r] " << rotate[r] << std::endl;
       #endif
+      rotate[r] = reads_shift_from_P0[r] + reads_shift_from_P1[r];
 
+      read_flags_for_generating_cancellation_terms(db_nitems); 
+      
       auto [Gamma0, Gamma1] =   generate_cancelation_terms(reading_c, reading_d, db_nitems, rotate[r]);
      
       Gamma0_reads[r] = Gamma0;
@@ -374,19 +242,19 @@ for(size_t i = 0; i < number_of_accesses; ++i)
     boost::asio::write(sockets_0[4], boost::asio::buffer(Gamma0_reads, number_of_ind_reads * sizeof(DB_t)));  
     boost::asio::write(sockets_1[4], boost::asio::buffer(Gamma1_reads, number_of_ind_reads * sizeof(DB_t)));
 
-
-     for(size_t d = 0; d < number_of_dep_reads; ++d)
-     {  
-
-        size_t shifts0, shifts1, rotate;
-        boost::asio::read(sockets_0[5], boost::asio::buffer(&shifts0, sizeof(shifts0)));
-        boost::asio::read(sockets_1[5], boost::asio::buffer(&shifts1, sizeof(shifts1)));
-        rotate = shifts0 + shifts1;
-        auto [Gamma0, Gamma1] =   generate_cancelation_terms(reading_c, reading_d, db_nitems, rotate);
-        boost::asio::write(sockets_0[6], boost::asio::buffer(&Gamma0, sizeof(Gamma0)));  
-        boost::asio::write(sockets_1[6], boost::asio::buffer(&Gamma1, sizeof(Gamma1)));
-     }
-
+    for(size_t d = 0; d < number_of_dep_reads; ++d)
+    {  
+       size_t shifts0, shifts1, rotate;
+       boost::asio::read(sockets_0[5], boost::asio::buffer(&shifts0, sizeof(shifts0)));
+       boost::asio::read(sockets_1[5], boost::asio::buffer(&shifts1, sizeof(shifts1)));
+       rotate = shifts0 + shifts1;
+        
+       read_flags_for_generating_cancellation_terms(db_nitems);
+        
+       auto [Gamma0, Gamma1] =   generate_cancelation_terms(reading_c, reading_d, db_nitems, rotate);
+       boost::asio::write(sockets_0[6], boost::asio::buffer(&Gamma0, sizeof(Gamma0)));  
+       boost::asio::write(sockets_1[6], boost::asio::buffer(&Gamma1, sizeof(Gamma1)));
+    }
 }
   return 0;
 }