Browse Source

Add DBVectorProcessor class to load DB from vector

Add a DBVectorProcessor object which loads the database from a vector
object stored in memory.
mandragore 4 years ago
parent
commit
8c02c28cad

+ 43 - 7
apps/simplepir/simplePIR.cpp

@@ -211,7 +211,7 @@ int main(int argc, char * argv[]) {
 
   // Simple test
   std::cout << "======================================================================" << std::endl;
-  std::cout << "Test 1/7: database_size = 1ULL<<30; nb_files = 20;" << std::endl;
+  std::cout << "Test 1/9: database_size = 1ULL<<30; nb_files = 20;" << std::endl;
   std::cout << "params.alpha = 1; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl; 
   std::cout << "======================================================================" << std::endl;
   database_size = 1ULL<<20; nb_files = 20; maxFileBytesize = database_size/nb_files;
@@ -228,7 +228,7 @@ int main(int argc, char * argv[]) {
   // WARNING we must provide the representation of the database GIVEN recursion and aggregation
   // as here we have 100 elements and aggregate them in a unique group we have params.n[0]=1
   std::cout << "======================================================================" << std::endl;
-  std::cout << "Test 2/7: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
+  std::cout << "Test 2/9: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
   std::cout << "params.alpha = 100; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl; 
   std::cout << "======================================================================" << std::endl;
   database_size = 1ULL<<25; nb_files = 100; maxFileBytesize = database_size/nb_files;
@@ -240,7 +240,7 @@ int main(int argc, char * argv[]) {
 
   // Test with recursion 2
   std::cout << "======================================================================" << std::endl;
-  std::cout << "Test 3/7: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
+  std::cout << "Test 3/9: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
   std::cout << "params.alpha = 1; params.d = 2; crypto_params = LWE:80:2048:120;" << std::endl; 
   std::cout << "======================================================================" << std::endl;
   database_size = 1ULL<<25; nb_files = 100; maxFileBytesize = database_size/nb_files;
@@ -252,7 +252,7 @@ int main(int argc, char * argv[]) {
   
   // Test with recursion 2 and aggregation
   std::cout << "======================================================================" << std::endl;
-  std::cout << "Test 4/7: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
+  std::cout << "Test 4/9: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
   std::cout << "params.alpha = 2; params.d = 2; crypto_params = LWE:80:2048:120;" << std::endl; 
   std::cout << "======================================================================" << std::endl;
   database_size = 1ULL<<25; nb_files = 100; maxFileBytesize = database_size/nb_files;
@@ -264,7 +264,7 @@ int main(int argc, char * argv[]) {
   
   // Test with recursion 3
   std::cout << "======================================================================" << std::endl;
-  std::cout << "Test 5/7: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
+  std::cout << "Test 5/9: database_size = 1ULL<<25; nb_files = 100;" << std::endl;
   std::cout << "params.alpha = 1; params.d = 3; crypto_params = LWE:80:2048:120;" << std::endl; 
   std::cout << "======================================================================" << std::endl;
   database_size = 1ULL<<25; nb_files = 100; maxFileBytesize = database_size/nb_files;
@@ -276,7 +276,7 @@ int main(int argc, char * argv[]) {
   
   // Test with a DBDirectoryProcessor splitting a big real file
   std::cout << "======================================================================" << std::endl;
-  std::cout << "Test 6/7: DBDirectoryProcessor with split; database_size = 1ULL<<25; nb_files = 4;" << std::endl;
+  std::cout << "Test 6/9: DBDirectoryProcessor with split; database_size = 1ULL<<25; nb_files = 4;" << std::endl;
   std::cout << "params.alpha = 1; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl; 
   std::cout << "======================================================================" << std::endl;
   database_size = 1ULL<<25; nb_files = 4; maxFileBytesize = database_size/nb_files;
@@ -292,7 +292,7 @@ int main(int argc, char * argv[]) {
   
   // Test with a DBDirectoryProcessor reading real files
   std::cout << "======================================================================" << std::endl;
-  std::cout << "Test 7/7: DBDirectoryProcessor without split;" << std::endl;
+  std::cout << "Test 7/9: DBDirectoryProcessor without split;" << std::endl;
   std::cout << "params.alpha = 1; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl; 
   std::cout << "======================================================================" << std::endl;
   DBDirectoryProcessor db7;
@@ -307,6 +307,42 @@ int main(int argc, char * argv[]) {
     tests_failed |= run(&db7, chosen_element, params);
   }
 
+  // Test with a DBVectorProcessor
+  std::cout << "======================================================================" << std::endl;
+  std::cout << "Test 8/9: DBVectorProcessor;" << std::endl;
+  std::cout << "params.alpha = 1; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl;
+  std::cout << "======================================================================" << std::endl;
+  std::vector<element_t> elements1;
+  elements1.push_back(Element("first file", 10, (char*) "first data"));
+  elements1.push_back(Element("second file", 11, (char*) "second data"));
+  elements1.push_back(Element("third file", 8, (char*) "3rd data"));
+
+  DBVectorProcessor db8(elements1);
+
+  chosen_element = 0;
+  params.alpha = 1; params.d = 1;
+  params.crypto_params = "LWE:80:2048:120";
+  tests_failed |= run(&db8, chosen_element, params);
+
+  // Test with a DBVectorProcessor
+  std::cout << "======================================================================" << std::endl;
+  std::cout << "Test 9/9: DBVectorProcessor;" << std::endl;
+  std::cout << "params.alpha = 2; params.d = 1; crypto_params = LWE:80:2048:120;" << std::endl;
+  std::cout << "======================================================================" << std::endl;
+  std::vector<element_t> elements2;
+  elements2.push_back(Element("first file", 10, (char*) "first data"));
+  elements2.push_back(Element("second file", 11, (char*) "second data"));
+  elements2.push_back(Element("third file", 8, (char*) "3rd data"));
+  elements2.push_back(Element("fourth file", 14, (char*) "fourth db data"));
+  elements2.push_back(Element("fifth file", 13, (char*) "fifth db data"));
+
+  DBVectorProcessor db9(elements2);
+
+  chosen_element = 0;
+  params.alpha = 2; params.d = 1;
+  params.crypto_params = "LWE:80:2048:120";
+  tests_failed |= run(&db8, chosen_element, params);
+
   if (tests_failed) 
   {
     std::cout << "WARNING : at least one tests failed" << std::endl;

+ 1 - 0
libpir.hpp

@@ -11,6 +11,7 @@
 #include <crypto/HomomorphicCrypto.hpp>
 #include "pir/dbhandlers/DBGenerator.hpp"
 #include "pir/dbhandlers/DBDirectoryProcessor.hpp"
+#include "pir/dbhandlers/DBVectorProcessor.hpp"
 #include <stdint.h>
 
 

+ 1 - 1
pir/dbhandlers/CMakeLists.txt

@@ -3,4 +3,4 @@ cmake_minimum_required(VERSION 2.6.0)
 include_directories(..)
 include_directories(../..)
 
-add_library(pir_dbhandlers STATIC DBDirectoryProcessor.cpp DBGenerator.cpp DBHandler.cpp)
+add_library(pir_dbhandlers STATIC DBDirectoryProcessor.cpp DBGenerator.cpp DBVectorProcessor.cpp DBHandler.cpp)

+ 110 - 0
pir/dbhandlers/DBVectorProcessor.cpp

@@ -0,0 +1,110 @@
+/* Copyright (C) 2017 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
+ *
+ * This file is written by Konstantinos Andrikopoulos
+ *
+ * This file is part of XPIR.
+ *
+ * XPIR is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * XPIR is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XPIR.  If not, see <http://www.gnu.org/licenses/>.
+ *
+*/
+
+#include "DBVectorProcessor.hpp"
+
+Element::Element(std::string e_name, uint64_t e_size, char* e_data) :
+name(e_name),
+data_size(e_size),
+data(NULL)
+{
+    data = (char*) malloc(data_size);
+    memcpy(data, e_data, data_size);
+}
+
+
+DBVectorProcessor::DBVectorProcessor(std::vector<element_t>& vector_db) :
+elements(vector_db)
+{
+   maxFileByteSize = 0;
+
+   for (auto e : elements)
+   {
+       if (e.data_size > maxFileByteSize)
+       {
+           maxFileByteSize = e.data_size;
+       }
+   }
+}
+
+DBVectorProcessor::~DBVectorProcessor() {}
+
+std::string DBVectorProcessor::getCatalog(const bool typeOfCatalog) {
+    std::string buf;
+    if(typeOfCatalog) {
+        buf = std::to_string((unsigned int)0) + "\n";
+        buf += std::to_string(getNbStream()) + "\n";
+        for (auto e : elements)
+        {
+            //auto e = elements[i];
+            buf += e.name + "\n" + std::to_string(e.data_size) + "\n";
+        }
+        return buf;
+    }
+    else {
+        buf = std::to_string((unsigned int)1) + "\n";
+        buf += std::to_string(getNbStream());
+        buf += std::to_string(getmaxFileBytesize()) + "\n";
+        return buf;
+    }
+}
+
+uint64_t DBVectorProcessor::getNbStream() {
+    return elements.size();
+}
+
+uint64_t DBVectorProcessor::getmaxFileBytesize() {
+    return maxFileByteSize;
+}
+
+bool DBVectorProcessor::openStream(uint64_t streamNb, uint64_t requested_offset) {
+    if(openStreamOffsets.count(streamNb)) {
+        return false;
+    }
+
+    char* stream = elements[streamNb].data + requested_offset;
+    openStreamOffsets.insert( std::pair<uint64_t, char*>(streamNb, stream));
+    return true;
+}
+
+uint64_t DBVectorProcessor::readStream(uint64_t streamNb, char * buf, uint64_t size) {
+    element_t e = elements[streamNb];
+    char* stream = openStreamOffsets[streamNb];
+    uint64_t sizeRead = stream - e.data;
+    uint64_t sizeRemaining = e.data_size - sizeRead;
+
+    if(sizeRemaining >= size) {
+        memcpy(buf, stream, size);
+        stream += size;
+    }
+    else {
+        memcpy(buf, stream, sizeRemaining);
+        bzero(buf + sizeRemaining, size - sizeRemaining);
+        stream += sizeRemaining;
+    }
+
+    openStreamOffsets[streamNb] = stream;
+    return size;
+}
+
+void DBVectorProcessor::closeStream(uint64_t streamNb) {
+    openStreamOffsets.erase(streamNb);
+}

+ 62 - 0
pir/dbhandlers/DBVectorProcessor.hpp

@@ -0,0 +1,62 @@
+/* Copyright (C) 2017 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
+ *
+ * This file is written by Konstantinos Andrikopoulos
+ *
+ * This file is part of XPIR.
+ *
+ *  XPIR is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  XPIR is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with XPIR.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef DEF_DBVECPROC
+#define DEF_DBVECPROC
+
+#include "DBHandler.hpp"
+
+#include <vector>
+#include <map>
+#include <tuple>
+#include <string.h>
+
+
+struct Element {
+    Element(std::string, uint64_t, char*);
+    std::string name;
+    size_t data_size;
+    char *data;
+};
+
+typedef Element element_t;
+
+class DBVectorProcessor : public DBHandler
+{
+private:
+    std::vector<element_t>& elements;
+    std::map<uint64_t, char*> openStreamOffsets;
+    uint64_t maxFileByteSize;
+
+public:
+    DBVectorProcessor(std::vector<element_t>& vector_db);
+    virtual ~DBVectorProcessor();
+
+    std::string getCatalog(const bool typeOfCatalog);
+
+	uint64_t getNbStream();
+	uint64_t getmaxFileBytesize();
+
+	bool openStream(uint64_t streamNb, uint64_t requested_offset);
+	uint64_t readStream(uint64_t streamNb, char * buf, uint64_t size);
+	void closeStream(uint64_t streamNb);
+};
+
+#endif /* DEF_DBVECPROC */