Commit 65f0d7ee authored by Gerard Ryan's avatar Gerard Ryan

Cherry pick demo update from development release

parent 4e93ef85
#Makefile to build and install gmp in palisade
#//TODO: rerunning make on this file forces rebuilds.
# we should put checks in to prevent that.
all: gmp_unpack gmp_config gmp_make gmp_check gmp_install
clean: clean_gmp
TAR ?= tar
#note change the following directory to the correct one on your install
PALISADE_DIR := $(PWD)
GMP_VER :=6.1.2
#check if the palisade directory is correct
ifneq ($(wildcard $(PALISADE_DIR)), )
$(info Makefile.gmp running)
else
$(info Makefile.gmp needs to be updated for correct PALISADE_DIR directory)
endif
THIRD_PARTY_DIR := $(PALISADE_DIR)/third-party
DISTROS_DIR := $(THIRD_PARTY_DIR)/distros
GMP_DISTRO_DIR := $(DISTROS_DIR)/gmp-$(GMP_VER)
gmp_unpack:
@echo 'Unpacking gmp for Palisade into $(GMP_DISTRO_DIR)'
cd $(DISTROS_DIR);\
$(TAR) xf ./gmp-$(GMP_VER).tar.lz
gmp_config:
@echo 'Configuring gmp for Palisade in $(GMP_DISTRO_DIR)'
cd $(GMP_DISTRO_DIR); \
./configure --prefix=$(THIRD_PARTY_DIR);
gmp_make:
@echo 'making gmp for Palisade in $(GMP_DISTRO_DIR)'
cd $(GMP_DISTRO_DIR); \
$(MAKE)
gmp_check:
@echo 'checking gmp for Palisade in $(GMP_DISTRO_DIR)'
cd $(GMP_DISTRO_DIR); \
$(MAKE) check
gmp_install:
@echo 'installing gmp for Palisade in $(THIRD_PARTY_DIR)'
cd $(GMP_DISTRO_DIR); \
$(MAKE) install
clean_gmp:
@echo 'Cleaning gmp installation'
cd $(DISTROS_DIR);\
$(RM) -rf gmp-$(GMP_VER)
cd $(THIRD_PARTY_DIR); \
$(RM) -rf include lib share
#Makefile to build and install ntl in palisade
TAR ?= tar
#note change the following directory to the correct one on your install
PALISADE_DIR := $(PWD)
NTL_VER:= 10.3.0
#check if the palisade directory is correct
ifneq ($(wildcard $(PALISADE_DIR)), )
$(info Makefile.ntl running PALISADE_DIR found)
else
$(info Makefile.ntl needs to be updated for correct PALISADE_DIR directory)
endif
THIRD_PARTY_DIR := $(PALISADE_DIR)/third-party
DISTROS_DIR := $(THIRD_PARTY_DIR)/distros
NTL_DISTRO_DIR := $(DISTROS_DIR)/ntl-$(NTL_VER)/src
#check if gmp has been installed into the correct directory
# and build gmp if not.
ifneq ($(wildcard $(THIRD_PARTY_DIR)/include), )
$(info Makefile.ntl running GMP install found)
GMP_NEEDED :=
else
$(info running Makefile.gmp for GMP install)
GMP_NEEDED :=gmp_make
endif
all: $(GMP_NEEDED) ntl_unpack ntl_config ntl_make ntl_check ntl_install
@echo $(all)
clean: clean_ntl
ntl_unpack:
@echo 'Unpacking ntl for Palisade into $(NTL_DISTRO_DIR)'
cd $(DISTROS_DIR);\
$(TAR) xf ./ntl-$(NTL_VER).tar.gz
ntl_config:
@echo 'Configuring ntl for Palisade in $(NTL_DISTRO_DIR)'
cd $(NTL_DISTRO_DIR); \
./configure PREFIX=$(THIRD_PARTY_DIR) GMP_PREFIX=$(THIRD_PARTY_DIR) NTL_THREADS=on NTL_THREAD_BOOST=on NTL_EXCEPTIONS=on SHARED=on
ntl_make:
@echo 'making ntl for Palisade in $(NTL_DISTRO_DIR)'
cd $(NTL_DISTRO_DIR); \
$(MAKE)
ntl_check:
@echo 'checking ntl for Palisade in $(NTL_DISTRO_DIR)'
cd $(NTL_DISTRO_DIR); \
$(MAKE) check
ntl_install:
@echo 'installing ntl for Palisade in $(THIRD_PARTY_DIR)'
cd $(NTL_DISTRO_DIR); \
$(MAKE) install
clean_ntl:
@echo 'Cleaning ntl installation'
cd $(DISTROS_DIR);\
$(RM) -rf ntl-$(NTL_VER)
cd $(THIRD_PARTY_DIR); \
$(RM) -rf include lib share includels
gmp_make:
$(MAKE) -f Makefile.gmp
...@@ -81,6 +81,8 @@ bool Matrix<BigInteger>::Deserialize(const Serialized& serObj) { ...@@ -81,6 +81,8 @@ bool Matrix<BigInteger>::Deserialize(const Serialized& serObj) {
return false; return false;
} }
#if MATHBACKEND != 7
template<> template<>
bool Matrix<native_int::BigInteger>::Serialize(Serialized* serObj) const { bool Matrix<native_int::BigInteger>::Serialize(Serialized* serObj) const {
return false; return false;
...@@ -91,6 +93,8 @@ bool Matrix<native_int::BigInteger>::Deserialize(const Serialized& serObj) { ...@@ -91,6 +93,8 @@ bool Matrix<native_int::BigInteger>::Deserialize(const Serialized& serObj) {
return false; return false;
} }
#endif
template<> template<>
bool Matrix<BigVector>::Serialize(Serialized* serObj) const { bool Matrix<BigVector>::Serialize(Serialized* serObj) const {
return false; return false;
......
/**
* @file serializable.cpp Serialization utilities.
* @author TPOC: palisade@njit.edu
*
* @copyright Copyright (c) 2017, New Jersey Institute of Technology (NJIT)
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "serializable.h"
namespace lbcrypto {
bool Serializable::includeKeysInSerializedCryptoContext = true;
void Serializable::EnableKeysInSerializedContext() {
includeKeysInSerializedCryptoContext = true;
}
void Serializable::DisableKeysInSerializedContext() {
includeKeysInSerializedCryptoContext = false;
}
}
...@@ -55,6 +55,8 @@ namespace lbcrypto { ...@@ -55,6 +55,8 @@ namespace lbcrypto {
class Serializable class Serializable
{ {
static bool includeKeysInSerializedCryptoContext;
/** /**
* Version number of the serialization; defaults to 1 * Version number of the serialization; defaults to 1
* @return version of the serialization * @return version of the serialization
...@@ -62,7 +64,11 @@ namespace lbcrypto { ...@@ -62,7 +64,11 @@ namespace lbcrypto {
virtual int getVersion() { return 1; } virtual int getVersion() { return 1; }
public: public:
virtual ~Serializable(){}; virtual ~Serializable() {}
static void EnableKeysInSerializedContext();
static void DisableKeysInSerializedContext();
static bool IncludeKeysInSerializedContext() { return includeKeysInSerializedCryptoContext; }
/** /**
* Serialize the object into a Serialized * Serialize the object into a Serialized
......
...@@ -69,11 +69,14 @@ int main(int argc, char* argv[]) { ...@@ -69,11 +69,14 @@ int main(int argc, char* argv[]) {
std::cout << " arg1 can be one of the following: keygen, encrypt, compute, or decrypt" << std::endl; std::cout << " arg1 can be one of the following: keygen, encrypt, compute, or decrypt" << std::endl;
} }
if (argc == 2) { if (argc == 2) {
if (std::string(argv[1]) == "keygen") if (std::string(argv[1]) == "keygen")
KeyGen(); KeyGen();
else if (std::string(argv[1]) == "encrypt") else {
Serializable::DisableKeysInSerializedContext();
if (std::string(argv[1]) == "encrypt")
Encrypt(); Encrypt();
else if (std::string(argv[1]) == "compute") else if (std::string(argv[1]) == "compute")
Compute(); Compute();
...@@ -83,10 +86,8 @@ int main(int argc, char* argv[]) { ...@@ -83,10 +86,8 @@ int main(int argc, char* argv[]) {
std::cerr << "the argument is invalid"; std::cerr << "the argument is invalid";
return 1; return 1;
} }
} }
}
//cin.get();
PackedIntPlaintextEncoding::Destroy(); PackedIntPlaintextEncoding::Destroy();
...@@ -223,31 +224,6 @@ void KeyGen() ...@@ -223,31 +224,6 @@ void KeyGen()
std::cout << "Completed" << std::endl; std::cout << "Completed" << std::endl;
const auto evalMultKey = cc->GetEvalMultKey();
std::cout << "Serializing multiplication evaluation key...";
if (evalMultKey) {
Serialized evalKey;
if (evalMultKey->Serialize(&evalKey)) {
if (!SerializableHelper::WriteSerializationToFile(evalKey, DATAFOLDER + "/" + "key-eval-mult" + std::to_string(k) + ".txt")) {
cerr << "Error writing serialization of multiplication evaluation key to key-eval-mult" + std::to_string(k) + ".txt" << endl;
return;
}
}
else {
cerr << "Error serializing multiplication evaluation key" << endl;
return;
}
}
else {
cerr << "Failure in generating multiplication evaluation key" << endl;
}
std::cout << "Completed" << std::endl;
// EvalSumKey // EvalSumKey
std::cout << "Generating summation evaluation keys..."; std::cout << "Generating summation evaluation keys...";
...@@ -256,34 +232,6 @@ void KeyGen() ...@@ -256,34 +232,6 @@ void KeyGen()
std::cout << "Completed" << std::endl; std::cout << "Completed" << std::endl;
auto evalSumKeys = cc->GetEvalSumKey();
std::cout << "Serializing summation evaluation keys...";
for (std::map<usint, shared_ptr<LPEvalKey<DCRTPoly>>>::iterator it = evalSumKeys.begin(); it != evalSumKeys.end(); ++it)
{
if (it->second) {
Serialized evalKey;
if (it->second->Serialize(&evalKey)) {
if (!SerializableHelper::WriteSerializationToFile(evalKey, DATAFOLDER + "/" + "key-eval-sum-" + std::to_string(k) + "-" + std::to_string(it->first) + ".txt")) {
cerr << "Error writing serialization of summation evaluation key to " << "key-eval-sum-" + std::to_string(k) + "-" + std::to_string(it->first) + ".txt" << endl;
return;
}
}
else {
cerr << "Error serializing summation evaluation key with index " + std::to_string(it->first) << endl;
return;
}
}
else {
cerr << "Failure in generating summation evaluation key with index " + std::to_string(it->first) << endl;
}
}
std::cout << "Completed" << std::endl;
// CryptoContext // CryptoContext
std::cout << "Serializing crypto context..."; std::cout << "Serializing crypto context...";
...@@ -330,8 +278,6 @@ void Encrypt() { ...@@ -330,8 +278,6 @@ void Encrypt() {
} }
} }
std::cout << "First loop completed" << std::endl;
auto product = x * y.Transpose(); auto product = x * y.Transpose();
uint64_t result = 0; uint64_t result = 0;
...@@ -386,9 +332,6 @@ void Encrypt() { ...@@ -386,9 +332,6 @@ void Encrypt() {
PackedIntPlaintextEncoding::SetParams(m, encodingParams); PackedIntPlaintextEncoding::SetParams(m, encodingParams);
cc->Enable(ENCRYPTION);
cc->Enable(SHE);
//std::cout << "plaintext modulus = " << cc->GetCryptoParameters()->GetPlaintextModulus() << std::endl; //std::cout << "plaintext modulus = " << cc->GetCryptoParameters()->GetPlaintextModulus() << std::endl;
// Deserialize the public key // Deserialize the public key
...@@ -486,70 +429,7 @@ void Compute() { ...@@ -486,70 +429,7 @@ void Compute() {
PackedIntPlaintextEncoding::SetParams(m, encodingParams); PackedIntPlaintextEncoding::SetParams(m, encodingParams);
cc->Enable(ENCRYPTION);
cc->Enable(SHE);
// Deserialize the eval mult key
std::cout << "Deserializing the multiplication evaluation key...";
Serialized emSer;
if (SerializableHelper::ReadSerializationFromFile(DATAFOLDER + "/" + emFileName, &emSer) == false) {
cerr << "Could not read mulplication evaluation key" << endl;
return;
}
shared_ptr<LPEvalKey<DCRTPoly>> em = cc->deserializeEvalKey(emSer);
if (!em) {
cerr << "Could not deserialize multiplication evaluation key" << endl;
return;
}
vector<shared_ptr<LPEvalKey<DCRTPoly>>> evalMultKeys;
evalMultKeys.push_back(em);
cc->SetEvalMultKeys(evalMultKeys);
std::cout << "Completed" << std::endl;
// Deserialize the eval sum keys
std::cout << "Deserializing the summation evaluation keys...";
std::map<usint, shared_ptr<LPEvalKey<DCRTPoly>>> evalSumKeys;
usint batchSize = encodingParams->GetBatchSize(); usint batchSize = encodingParams->GetBatchSize();
usint g = encodingParams->GetPlaintextGenerator();
std::map<usint, shared_ptr<LPEvalKey<DCRTPoly>>> evalKeys;
for (int i = 0; i < floor(log2(batchSize)); i++)
{
Serialized esSer;
string tempFileName = DATAFOLDER + "/" + esFileName + "-" + std::to_string(g) + ".txt";
if (SerializableHelper::ReadSerializationFromFile(tempFileName, &esSer) == false) {
cerr << "Could not read the evaluation key at index " << g << endl;
return;
}
shared_ptr<LPEvalKey<DCRTPoly>> es = cc->deserializeEvalKey(esSer);
if (!es) {
cerr << "Could not deserialize summation evaluation key at index " << g << endl;
return;
}
evalKeys[g] = es;
g = (g * g) % m;
}
cc->SetEvalSumKeys(evalKeys);
std::cout << "Completed" << std::endl;
// Deserialize X // Deserialize X
...@@ -660,9 +540,6 @@ void Decrypt() { ...@@ -660,9 +540,6 @@ void Decrypt() {
PackedIntPlaintextEncoding::SetParams(m, encodingParams); PackedIntPlaintextEncoding::SetParams(m, encodingParams);
cc->Enable(ENCRYPTION);
cc->Enable(SHE);
// Deserialize the private key // Deserialize the private key
std::cout << "Deserializing the private key..."; std::cout << "Deserializing the private key...";
...@@ -758,9 +635,13 @@ native_int::BigInteger CRTInterpolate(const std::vector<PackedIntPlaintextEncodi ...@@ -758,9 +635,13 @@ native_int::BigInteger CRTInterpolate(const std::vector<PackedIntPlaintextEncodi
native_int::BigInteger result(0); native_int::BigInteger result(0);
std::vector<native_int::BigInteger> q = { 3623,3779,3803 }; std::vector<native_int::BigInteger> q = { 3623,3779,3803,3863 };
native_int::BigInteger Q(1);
native_int::BigInteger Q(52068078551); for (size_t i = 0; i < crtVector.size(); i++) {
Q = Q*q[i];
}
std::vector<native_int::BigInteger> qInverse; std::vector<native_int::BigInteger> qInverse;
......
...@@ -163,6 +163,7 @@ CryptoContext<T>::Serialize(Serialized* serObj) const ...@@ -163,6 +163,7 @@ CryptoContext<T>::Serialize(Serialized* serObj) const
Serialized kser(rapidjson::kObjectType, &serObj->GetAllocator()); Serialized kser(rapidjson::kObjectType, &serObj->GetAllocator());
if( Serializable::IncludeKeysInSerializedContext() ) {
if( this->evalMultKeys.size() > 0 ) { if( this->evalMultKeys.size() > 0 ) {
SerializeVectorOfPointers<LPEvalKey<T>>("EvalMultKeys", "LPEvalKey", this->evalMultKeys, &ccser); SerializeVectorOfPointers<LPEvalKey<T>>("EvalMultKeys", "LPEvalKey", this->evalMultKeys, &ccser);
} }
...@@ -170,6 +171,7 @@ CryptoContext<T>::Serialize(Serialized* serObj) const ...@@ -170,6 +171,7 @@ CryptoContext<T>::Serialize(Serialized* serObj) const
if( this->evalSumKeys.size() > 0 ) { if( this->evalSumKeys.size() > 0 ) {
SerializeMapOfPointers("EvalSumKeys", T::GetElementName(), this->evalSumKeys, &ccser); SerializeMapOfPointers("EvalSumKeys", T::GetElementName(), this->evalSumKeys, &ccser);
} }
}
ccser.AddMember("Schemes", std::to_string(this->scheme->GetEnabled()), serObj->GetAllocator()); ccser.AddMember("Schemes", std::to_string(this->scheme->GetEnabled()), serObj->GetAllocator());
......
...@@ -1587,13 +1587,18 @@ namespace lbcrypto { ...@@ -1587,13 +1587,18 @@ namespace lbcrypto {
result = EvalInnerProduct(x0, y0, batchSize, evalSumKeys, evalMultKey); result = EvalInnerProduct(x0, y0, batchSize, evalSumKeys, evalMultKey);
#pragma omp parallel for #pragma omp parallel for ordered schedule(dynamic)
for (usint i = indexStart + 1; i < indexStart + length; i++) for (usint i = indexStart + 1; i < indexStart + length; i++)
{ {
shared_ptr<Ciphertext<Element>> xi = (*x)(i, 0).GetNumerator(); shared_ptr<Ciphertext<Element>> xi = (*x)(i, 0).GetNumerator();
shared_ptr<Ciphertext<Element>> yi = (*y)(i, 0).GetNumerator(); shared_ptr<Ciphertext<Element>> yi = (*y)(i, 0).GetNumerator();
result = EvalAdd(result,EvalInnerProduct(xi, yi, batchSize, evalSumKeys, evalMultKey)); auto product = EvalInnerProduct(xi, yi, batchSize, evalSumKeys, evalMultKey);
#pragma omp ordered
{
result = EvalAdd(result,product);
}
} }
return result; return result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment