Browse Source

reply generator can be reused for multiply replies, simplepir improved too

Carlos Aguilar 6 years ago
parent
commit
afc60f0241

+ 12 - 7
apps/simplepir/simplePIR.cpp

@@ -41,7 +41,6 @@ bool run(DBHandler *db, uint64_t chosen_element, PIRParameters params){
   // We could have also defined PIRReplyGenerator *r_generator(params,*crypto,db);
   // But we prefer a pointer to show (below) how to use multiple generators for a given db
   PIRReplyGenerator *r_generator = new PIRReplyGenerator(params,*crypto,db);
-  r_generator->setPirParams(params);
 
   // In a real application the client would pop the queries from q with popQuery and 
   // send them through the network and the server would receive and push them into s 
@@ -78,6 +77,7 @@ bool run(DBHandler *db, uint64_t chosen_element, PIRParameters params){
    * DO NOT try to use the same reply generator more than once, this causes issues
    * ******************************************************************************/
 
+#if 0
   // Generate 3 replies from 3 queries
   for (int i = 0 ; i < 3 ; i++){
 
@@ -87,14 +87,18 @@ bool run(DBHandler *db, uint64_t chosen_element, PIRParameters params){
       free(reply_element_tmp);
     }
 
-    // Free memory and the r_generator object
-    r_generator->freeQueries();
-    //r_generator->freeResult();
+    // If you are unable to reuse a r_generator object (e.g. if you want 
+    // to change the crypto object) you can always recreate a new generator
     //delete r_generator;
-	
-    // Create and initialize a new generator object
     //r_generator = new PIRReplyGenerator(params,*crypto,db);
-    //r_generator->setPirParams(params);
+	
+    // In this example we want to use the same generator for 
+    // multiply queries. Before giving a new query to r_generator
+    // we must free the previous one. 
+    r_generator->freeQueries();
+    
+    // It is also possible to change the pir parameters with the 
+    // (unexposed) setPirParams(PIRParameters newparams) function
 	
     // Generate a new query
   	q_generator.generateQuery(chosen_element);
@@ -108,6 +112,7 @@ bool run(DBHandler *db, uint64_t chosen_element, PIRParameters params){
     // Generate again the reply
 	  r_generator->generateReply(imported_db);
   }
+#endif
 
 
   /******************************************************************************

+ 12 - 20
pir/libpir.cpp

@@ -78,28 +78,10 @@
 	: PIRReplyGeneratorNFL_internal (param,db)
   {
 		PIRReplyGeneratorNFL_internal::setCryptoMethod(&cryptoMethod_);
-    PIRReplyGeneratorNFL_internal::initQueriesBuffer();
     PIRReplyGeneratorNFL_internal::setPirParams(param);
-		nbRepliesToHandle=0;
-		nbRepliesGenerated=0;
-		currentReply=0;
-  }
-
-#if 0
-  void PIRReplyGenerator::reinit()
-  {
-		freeResult();
-    freeQueries();
-    
-    PIRReplyGeneratorNFL_internal::setCryptoMethod(&cryptoMethod_);
-    PIRReplyGeneratorNFL_internal::initQueriesBuffer();
-    PIRReplyGeneratorNFL_internal::setPirParams(param);
-		nbRepliesToHandle=0;
-		nbRepliesGenerated=0;
-		currentReply=0;
   }
-#endif
 
+  
   void PIRReplyGenerator::pushQuery(char* rawQuery) {
 		PIRReplyGeneratorNFL_internal::pushQuery(rawQuery);
   }
@@ -123,6 +105,13 @@
 
   void PIRReplyGenerator::generateReply(const imported_database* database)
   {
+    // Init
+		nbRepliesToHandle=0;
+		nbRepliesGenerated=0;
+		currentReply=0;
+    freeResult();
+
+    // Test memory
 		uint64_t usable_memory = getTotalSystemMemory();
 		nbRepliesGenerated=nbRepliesToHandle=computeReplySizeInChunks(database->beforeImportElementBytesize);
 		uint64_t polysize = cryptoMethod->getpolyDegree() * cryptoMethod->getnbModuli()*sizeof(uint64_t);
@@ -136,8 +125,11 @@
     
    	// The internal generator is locked by default waiting for the query to be received 
     // in this API we let the user deal with synchronisation so the lock is not needed
+    PIRReplyGeneratorNFL_internal::mutex.try_lock();
     PIRReplyGeneratorNFL_internal::mutex.unlock();
-
+    
+    // Define the reply size
+    repliesAmount = computeReplySizeInChunks(database->beforeImportElementBytesize);
 		PIRReplyGeneratorNFL_internal::generateReply();
 
   }

+ 49 - 35
pir/replyGenerator/PIRReplyGeneratorNFL_internal.cpp

@@ -41,8 +41,11 @@ PIRReplyGeneratorNFL_internal::PIRReplyGeneratorNFL_internal( PIRParameters& par
   lwe(false),
   currentMaxNbPolys(0),
   GenericPIRReplyGenerator(param,db),
+  queriesBuf(NULL),
   current_query_index(0),
-  current_dim_index(0)
+  current_dim_index(0),
+  input_data(NULL),
+  cryptoMethod(NULL)
 {
   // cryptoMethod will be set later by setCryptoMethod
 }
@@ -377,7 +380,7 @@ void PIRReplyGeneratorNFL_internal::generateReply()
   uint64_t old_poly_nbr = 1;
   
   // Allocate memory for the reply array
-  if (repliesArray != NULL) free(repliesArray);
+  if (repliesArray != NULL) freeResult();
   repliesArray = (char**)calloc(repliesAmount,sizeof(char*)); 
 
 
@@ -449,10 +452,13 @@ void PIRReplyGeneratorNFL_internal::generateReply()
     for (uint64_t j = 0 ; j < reply_elt_nbr ; j++) {
       for (uint64_t k = 0 ; (k < old_poly_nbr) && (i < pirParam.d - 1); k++){
         free(inter_reply[j][k].a);
+        inter_reply[j][k].a = NULL;
       }
       delete[] inter_reply[j];
+      inter_reply[j] = NULL;
     }
     delete[] inter_reply; // allocated with a 'new' above. 
+    inter_reply = NULL;
   }
 
   // Compute execution time
@@ -463,7 +469,6 @@ void PIRReplyGeneratorNFL_internal::generateReply()
 double PIRReplyGeneratorNFL_internal::generateReplySimulation(const PIRParameters& pir_params, uint64_t plaintext_nbr)
 {
   setPirParams((PIRParameters&)pir_params);
-  initQueriesBuffer();
   pushFakeQuery();
   
   importFakeData(plaintext_nbr);
@@ -489,7 +494,6 @@ double PIRReplyGeneratorNFL_internal::precomputationSimulation(const PIRParamete
 {
   NFLlib *nflptr = &(cryptoMethod->getnflInstance());
   setPirParams((PIRParameters&)pir_params);
-  initQueriesBuffer();
   pushFakeQuery();
   importFakeData(plaintext_nbr);
 
@@ -635,9 +639,7 @@ lwe_in_data* PIRReplyGeneratorNFL_internal::fromResulttoInData(lwe_cipher** inte
 														currentMaxNbPolys, 
 														cryptoMethod->getPublicParameters().getCiphertextBitsize(), 
 														in_data2b[i].nbPolys);
-      //delete[] inter_reply[i]; free in generateReplyGeneric
     }
-    //delete[] inter_reply;
     free(bufferOfBuffers);
 
     currentMaxNbPolys = in_data2b_nbr_polys; 
@@ -820,8 +822,11 @@ size_t PIRReplyGeneratorNFL_internal::getTotalSystemMemory()
 
 void PIRReplyGeneratorNFL_internal::setPirParams(PIRParameters& param)
 {
+  freeQueries();
+  freeQueriesBuffer();
   pirParam = param;
   cryptoMethod->setandgetAbsBitPerCiphertext(pirParam.n[0]);
+  initQueriesBuffer();
 }
 
 
@@ -834,18 +839,26 @@ void PIRReplyGeneratorNFL_internal::setCryptoMethod(CryptographicSystem* cm)
 
 void PIRReplyGeneratorNFL_internal::freeInputData()
 {
+#ifdef DEBUG
+  std:cout << "PIRReplyGeneratorNFL_internal: freeing input_data" << std::endl;
+#endif
   uint64_t theoretical_files_nbr = 1;
 	for (unsigned int i = 0 ; i < pirParam.d ; i++) theoretical_files_nbr *= pirParam.n[i];
 
-  for (unsigned int i = 0 ; i < theoretical_files_nbr ; i++){
-#ifdef DEBUG
-  printf( "PIRReplyGeneratorNFL_internal: freeing input_data[%d]\n",i);
-#endif
-    free(input_data[i].p[0]);
-    free(input_data[i].p);
+  if (input_data != NULL){
+    for (unsigned int i = 0 ; i < theoretical_files_nbr ; i++){
+      if (input_data[i].p != NULL){
+        if (input_data[i].p[0] != NULL){
+          free(input_data[i].p[0]);
+          input_data[i].p[0] = NULL;
+        }
+        free(input_data[i].p);
+        input_data[i].p = NULL;
+      }
+    }
+    delete[] input_data;
+    input_data = NULL;
   }
-  delete[] input_data;
-
 #ifdef DEBUG
   printf( "PIRReplyGeneratorNFL_internal: input_data freed\n");
 #endif
@@ -855,12 +868,15 @@ void PIRReplyGeneratorNFL_internal::freeQueries()
 {
   for (unsigned int i = 0; i < pirParam.d; i++)
   {
-    for (unsigned int j = 0 ; j < pirParam.n[i] ; j++) {
-		  if (queriesBuf[i][0][j].a != NULL){
+    for (unsigned int j = 0 ; j < pirParam.n[i] ; j++) 
+    {
+		  if (queriesBuf != NULL && queriesBuf[i] != NULL && queriesBuf[i][0][j].a != NULL)
+      {
         free(queriesBuf[i][0][j].a); //only free a because a and b and contingus, see pushQuery
         queriesBuf[i][0][j].a = NULL;
       }
-		  if (queriesBuf[i][1][j].a != NULL){
+		  if (queriesBuf != NULL && queriesBuf[i] != NULL && queriesBuf[i][1][j].a != NULL)
+      {
         free(queriesBuf[i][1][j].a); //only free a because a and b and contingus, see pushQuery
         queriesBuf[i][1][j].a = NULL;
       }
@@ -876,28 +892,24 @@ void PIRReplyGeneratorNFL_internal::freeQueries()
 
 void PIRReplyGeneratorNFL_internal::freeQueriesBuffer()
 {
-  for (unsigned int i = 0; i < pirParam.d; i++)
-  {
-    if (queriesBuf[i][0] != NULL){
-      delete[] queriesBuf[i][0]; //allocated in intQueriesBuf with new.
-      queriesBuf[i][0] = NULL;
-    }
-    if (queriesBuf[i][1] != NULL){
-      delete[] queriesBuf[i][1]; //allocated in intQueriesBuf with new.
-      queriesBuf[i][1] = NULL;
-    }
-    if (queriesBuf[i] != NULL){
-      delete[] queriesBuf[i]; //allocated in intQueriesBuf with new.
-      queriesBuf[i] = NULL;
+  if (queriesBuf != NULL){ 
+    for (unsigned int i = 0; i < pirParam.d; i++){
+      if (queriesBuf[i] != NULL){
+        if (queriesBuf[i][0] != NULL){
+          delete[] queriesBuf[i][0]; //allocated in intQueriesBuf with new.
+          queriesBuf[i][0] = NULL;
+        }
+        if (queriesBuf[i][1] != NULL){
+          delete[] queriesBuf[i][1]; //allocated in intQueriesBuf with new.
+          queriesBuf[i][1] = NULL;
+        }
+        delete[] queriesBuf[i]; //allocated in intQueriesBuf with new.
+        queriesBuf[i] = NULL;
+      }
     }
-    delete[] queriesBuf[i];
-  }
-  if (queriesBuf != NULL){
     delete[] queriesBuf; //allocated in intQueriesBuf with new.
     queriesBuf = NULL;
   }
-
-  delete[] queriesBuf;//allocated in intQueriesBuf with new.
 }
 
 void PIRReplyGeneratorNFL_internal::freeResult()
@@ -920,6 +932,8 @@ PIRReplyGeneratorNFL_internal::~PIRReplyGeneratorNFL_internal()
   freeQueries();
   freeQueriesBuffer();
   freeResult();
+  mutex.try_lock();
+  mutex.unlock();
 }
 
 

+ 1 - 1
pir/replyGenerator/PIRReplyGeneratorNFL_internal.hpp

@@ -50,8 +50,8 @@ private:
     void pushFakeQuery();
     void freeInputData();
     void freeFakeInputData();
-    void freeResult();
 protected:
+    void freeResult();
     void freeQueries();
     void freeQueriesBuffer();
     void generateReply();