Commit 7e63af9b authored by Gerard Ryan's avatar Gerard Ryan

hot fix changes

parent 46f872d2
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include "math/backend.h" #include "math/backend.h"
#include "lattice/backend.h"
#include "utils/inttypes.h" #include "utils/inttypes.h"
#include "math/nbtheory.h" #include "math/nbtheory.h"
#include "math/distrgen.h" #include "math/distrgen.h"
......
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
#include <iostream> #include <iostream>
#include "math/backend.h" #include "math/backend.h"
#include "lattice/backend.h"
#include "utils/inttypes.h" #include "utils/inttypes.h"
#include "math/nbtheory.h" #include "math/nbtheory.h"
#include "lattice/elemparams.h" #include "lattice/elemparams.h"
......
/*
* @file lib-benchmark : library benchmark routines for comparison by build
* @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.
*
*/
/*
* This file benchmarks a small number of operations in order to exercise large pieces of the library
*/
#define PROFILE
#define _USE_MATH_DEFINES
#include "benchmark/benchmark_api.h"
#include <iostream>
#include <fstream>
#include <limits>
#include <iterator>
#include <random>
#include "palisade.h"
#include "cryptocontexthelper.h"
#include "utils/debug.h"
using namespace std;
using namespace lbcrypto;
CryptoContext<DCRTPoly>
GenerateContext() {
usint ptm = 2;
double sigma = 3.19;
double rootHermiteFactor = 1.0048;
size_t count = 100;
//Set Crypto Parameters
CryptoContext<DCRTPoly> cryptoContext = CryptoContextFactory<DCRTPoly>::genCryptoContextBFVrns(
ptm, rootHermiteFactor, sigma, 0, 5, 0, OPTIMIZED,3,30,55);
// enable features that you wish to use
cryptoContext->Enable(ENCRYPTION);
cryptoContext->Enable(SHE);
// std::cout << "\np = " << cryptoContext->GetCryptoParameters()->GetPlaintextModulus() << std::endl;
// std::cout << "n = " << cryptoContext->GetCryptoParameters()->GetElementParams()->GetCyclotomicOrder() / 2 << std::endl;
// std::cout << "log2 q = " << log2(cryptoContext->GetCryptoParameters()->GetElementParams()->GetModulus().ConvertToDouble()) << std::endl;
return cryptoContext;
}
void KeyGen(benchmark::State& state) {
CryptoContext<DCRTPoly> cryptoContext = GenerateContext();
LPKeyPair<DCRTPoly> keyPair;
while (state.KeepRunning()) {
keyPair = cryptoContext->KeyGen();
}
}
BENCHMARK(KeyGen)->Unit(benchmark::kMicrosecond);
void Encryption(benchmark::State& state) {
CryptoContext<DCRTPoly> cryptoContext = GenerateContext();
LPKeyPair<DCRTPoly> keyPair = cryptoContext->KeyGen();
std::vector<int64_t> vectorOfInts1 = {1,0,1,0,1,1,1,0,1,1,1,0};
Plaintext plaintext1 = cryptoContext->MakeCoefPackedPlaintext(vectorOfInts1);
while (state.KeepRunning()) {
auto ciphertext1 = cryptoContext->Encrypt(keyPair.publicKey, plaintext1);
}
}
BENCHMARK(Encryption)->Unit(benchmark::kMicrosecond);
void MultNoRelin(benchmark::State& state) {
CryptoContext<DCRTPoly> cryptoContext = GenerateContext();
LPKeyPair<DCRTPoly> keyPair = cryptoContext->KeyGen();
std::vector<int64_t> vectorOfInts1 = {1,0,1,0,1,1,1,0,1,1,1,0};
Plaintext plaintext1 = cryptoContext->MakeCoefPackedPlaintext(vectorOfInts1);
std::vector<int64_t> vectorOfInts2 = {1,1,1,1,1,1,1,0,1,1,1,0};
Plaintext plaintext2 = cryptoContext->MakeCoefPackedPlaintext(vectorOfInts2);
auto ciphertext1 = cryptoContext->Encrypt(keyPair.publicKey, plaintext1);
auto ciphertext2 = cryptoContext->Encrypt(keyPair.publicKey, plaintext2);
while (state.KeepRunning()) {
auto ciphertextMul = cryptoContext->EvalMultNoRelin(ciphertext1,ciphertext2);
}
}
BENCHMARK(MultNoRelin)->Unit(benchmark::kMicrosecond);
void MultRelin(benchmark::State& state) {
CryptoContext<DCRTPoly> cryptoContext = GenerateContext();
LPKeyPair<DCRTPoly> keyPair = cryptoContext->KeyGen();
cryptoContext->EvalMultKeyGen(keyPair.secretKey);
std::vector<int64_t> vectorOfInts1 = {1,0,1,0,1,1,1,0,1,1,1,0};
Plaintext plaintext1 = cryptoContext->MakeCoefPackedPlaintext(vectorOfInts1);
std::vector<int64_t> vectorOfInts2 = {1,1,1,1,1,1,1,0,1,1,1,0};
Plaintext plaintext2 = cryptoContext->MakeCoefPackedPlaintext(vectorOfInts2);
auto ciphertext1 = cryptoContext->Encrypt(keyPair.publicKey, plaintext1);
auto ciphertext2 = cryptoContext->Encrypt(keyPair.publicKey, plaintext2);
while (state.KeepRunning()) {
auto ciphertextMul = cryptoContext->EvalMult(ciphertext1,ciphertext2);
}
}
BENCHMARK(MultRelin)->Unit(benchmark::kMicrosecond);
void Decryption(benchmark::State& state) {
CryptoContext<DCRTPoly> cryptoContext = GenerateContext();
LPKeyPair<DCRTPoly> keyPair = cryptoContext->KeyGen();
std::vector<int64_t> vectorOfInts1 = {1,0,1,0,1,1,1,0,1,1,1,0};
Plaintext plaintext1 = cryptoContext->MakeCoefPackedPlaintext(vectorOfInts1);
auto ciphertext1 = cryptoContext->Encrypt(keyPair.publicKey, plaintext1);
Plaintext plaintextDec1;
while (state.KeepRunning()) {
cryptoContext->Decrypt(keyPair.secretKey, ciphertext1, &plaintextDec1);
}
}
BENCHMARK(Decryption)->Unit(benchmark::kMicrosecond);
void NTTTransform(benchmark::State& state) {
usint m = 2048;
usint phim = 1024;
NativeInteger modulusQ("288230376151748609");
NativeInteger rootOfUnity("64073710037604316");
uint64_t nRep;
DiscreteUniformGeneratorImpl<NativeVector> dug;
dug.SetModulus(modulusQ);
NativeVector x = dug.GenerateVector(phim);
NativeVector rootOfUnityTable(phim, modulusQ);
NativeInteger t(1);
for (usint i = 0; i<phim; i++) {
rootOfUnityTable.at(i)= t;
t = t.ModMul(rootOfUnity, modulusQ);
}
// test runs to force all precomputations
NativeVector X(m/2), xx(m/2);
ChineseRemainderTransformFTT<NativeVector>::ForwardTransform(x, rootOfUnity, m, &X);
ChineseRemainderTransformFTT<NativeVector>::InverseTransform(X, rootOfUnity, m, &xx);
while (state.KeepRunning()) {
ChineseRemainderTransformFTT<NativeVector>::ForwardTransform(x, rootOfUnity, m, &X);
}
}
BENCHMARK(NTTTransform)->Unit(benchmark::kMicrosecond);
BENCHMARK_MAIN()
...@@ -84,7 +84,8 @@ bool PackedEncoding::Encode() { ...@@ -84,7 +84,8 @@ bool PackedEncoding::Encode() {
for (size_t i = 1; i < nativeParams.size(); i++ ) { for (size_t i = 1; i < nativeParams.size(); i++ ) {
NativePoly temp(firstElement); NativePoly temp(firstElement);
temp.SwitchModulus(nativeParams[i]->GetModulus(),nativeParams[i]->GetRootOfUnity()); temp.SwitchModulus(nativeParams[i]->GetModulus(),nativeParams[i]->GetRootOfUnity(),
nativeParams[i]->GetBigModulus(),nativeParams[i]->GetBigRootOfUnity());
this->encodedVectorDCRT.SetElementAtIndex(i,temp); this->encodedVectorDCRT.SetElementAtIndex(i,temp);
} }
......
...@@ -1075,7 +1075,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p, ...@@ -1075,7 +1075,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p,
curFloatSum += (double)(xi.ConvertToInt())*beta[vi]; curFloatSum += (double)(xi.ConvertToInt())*beta[vi];
} }
coefficients[ri] = ((curIntSum + Integer(std::llround(curFloatSum))).Mod(p)).ConvertToInt(); coefficients[ri] = (curIntSum + NativeInteger(std::llround(curFloatSum))).Mod(p);
} }
} }
else if (m_vectors[0].GetModulus().GetMSB() < 58) else if (m_vectors[0].GetModulus().GetMSB() < 58)
...@@ -1095,7 +1095,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p, ...@@ -1095,7 +1095,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p,
curFloatSum += (long double)(xi.ConvertToInt())*extBeta[vi]; curFloatSum += (long double)(xi.ConvertToInt())*extBeta[vi];
} }
coefficients[ri] = ((curIntSum + Integer(std::llround(curFloatSum))).Mod(p)).ConvertToInt(); coefficients[ri] = (curIntSum + NativeInteger(std::llround(curFloatSum))).Mod(p);
} }
} }
else else
...@@ -1120,7 +1120,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p, ...@@ -1120,7 +1120,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p,
curFloatSum += quadFloatFromInt64(xi.ConvertToInt())*quadBeta[vi]; curFloatSum += quadFloatFromInt64(xi.ConvertToInt())*quadBeta[vi];
} }
coefficients[ri] = ((curIntSum + Integer(quadFloatRound(curFloatSum - pFloat*floor(curFloatSum/pFloat)))).Mod(p)).ConvertToInt(); coefficients[ri] = (curIntSum + NativeInteger(quadFloatRound(curFloatSum - pFloat*floor(curFloatSum/pFloat)))).Mod(p);
} }
} }
else else
...@@ -1140,7 +1140,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p, ...@@ -1140,7 +1140,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p,
curFloatSum += quadFloatFromInt64(xi.ConvertToInt())*quadBeta[vi]; curFloatSum += quadFloatFromInt64(xi.ConvertToInt())*quadBeta[vi];
} }
coefficients[ri] = ((curIntSum + Integer(quadFloatRound(curFloatSum))).Mod(p)).ConvertToInt(); coefficients[ri] = (curIntSum + NativeInteger(quadFloatRound(curFloatSum))).Mod(p);
} }
} }
} }
......
...@@ -264,7 +264,8 @@ public: ...@@ -264,7 +264,8 @@ public:
* @return result of the subtraction operation of type BigInteger. * @return result of the subtraction operation of type BigInteger.
*/ */
NativeInteger Minus(const NativeInteger& b) const { NativeInteger Minus(const NativeInteger& b) const {
return m_value <= b.m_value ? 0 : m_value - b.m_value; return m_value - b.m_value;
// return m_value <= b.m_value ? 0 : m_value - b.m_value;
} }
/** /**
...@@ -274,10 +275,7 @@ public: ...@@ -274,10 +275,7 @@ public:
* @return result of the subtraction operation of type BigInteger. * @return result of the subtraction operation of type BigInteger.
*/ */
const NativeInteger& MinusEq(const NativeInteger& b) { const NativeInteger& MinusEq(const NativeInteger& b) {
if( m_value <= b.m_value ) m_value -= b.m_value;
m_value = 0;
else
m_value -= b.m_value;
return *this; return *this;
} }
...@@ -535,15 +533,15 @@ public: ...@@ -535,15 +533,15 @@ public:
* @return result of the modulus addition operation. * @return result of the modulus addition operation.
*/ */
NativeInteger ModAddFastOptimized(const NativeInteger& b, const NativeInteger& modulus) const { NativeInteger ModAddFastOptimized(const NativeInteger& b, const NativeInteger& modulus) const {
//#if NTL_BITS_PER_LONG==64 #if NTL_BITS_PER_LONG==64
// return (uint_type)NTL::AddMod(this->m_value,b.m_value,modulus.m_value); return (uint_type)NTL::AddMod(this->m_value,b.m_value,modulus.m_value);
//#else #else
Duint_type modsum = (Duint_type)m_value; Duint_type modsum = (Duint_type)m_value;
modsum += b.m_value; modsum += b.m_value;
if (modsum >= modulus.m_value) if (modsum >= modulus.m_value)
modsum %= modulus.m_value; modsum %= modulus.m_value;
return (uint_type)modsum; return (uint_type)modsum;
//#endif #endif
} }
/** /**
...@@ -554,15 +552,15 @@ public: ...@@ -554,15 +552,15 @@ public:
* @return result of the modulus addition operation. * @return result of the modulus addition operation.
*/ */
const NativeInteger& ModAddFastOptimizedEq(const NativeInteger& b, const NativeInteger& modulus) { const NativeInteger& ModAddFastOptimizedEq(const NativeInteger& b, const NativeInteger& modulus) {
//#if NTL_BITS_PER_LONG==64 #if NTL_BITS_PER_LONG==64
// this->m_value = (uint_type)NTL::AddMod(this->m_value,b.m_value,modulus.m_value); this->m_value = (uint_type)NTL::AddMod(this->m_value,b.m_value,modulus.m_value);
//#else #else
Duint_type modsum = (Duint_type)m_value; Duint_type modsum = (Duint_type)m_value;
modsum += b.m_value; modsum += b.m_value;
if (modsum >= modulus.m_value) if (modsum >= modulus.m_value)
modsum %= modulus.m_value; modsum %= modulus.m_value;
this->m_value = (uint_type)modsum; this->m_value = (uint_type)modsum;
//#endif #endif
return *this; return *this;
} }
......
...@@ -37,9 +37,7 @@ class ParallelControls { ...@@ -37,9 +37,7 @@ class ParallelControls {
public: public:
ParallelControls() { ParallelControls() {
machineThreads = omp_get_max_threads(); machineThreads = omp_get_max_threads();
//omp_set_dynamic(1); Enable();
//omp_set_nested(1);
Disable();
} }
void Enable() { void Enable() {
......
...@@ -237,20 +237,20 @@ void basic_math_test(const string& msg) { ...@@ -237,20 +237,20 @@ void basic_math_test(const string& msg) {
// support for negative numbers as of now ConvertToInt converts // support for negative numbers as of now ConvertToInt converts
// T calculatedResult to integer // T calculatedResult to integer
{ // {
// TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER // // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER
//
T a("20489"); // T a("20489");
T b("2034455"); // T b("2034455");
//
calculatedResult = a.Minus(b); // calculatedResult = a.Minus(b);
expectedResult = 0; // expectedResult = 0;
//
//SINCE THERE IS NO CONCEPT OF NEGATIVE NUMEBR RESULT SHOULD BE // //SINCE THERE IS NO CONCEPT OF NEGATIVE NUMEBR RESULT SHOULD BE
//ZERO // //ZERO
EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt()) // EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
<< msg << " Failure testing minus_a_less_than_b"; // << msg << " Failure testing minus_a_less_than_b";
} // }
// TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO THE SECOND NUMBER // TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO THE SECOND NUMBER
{ {
T a("2048956567"); T a("2048956567");
...@@ -293,19 +293,19 @@ void basic_math_test(const string& msg) { ...@@ -293,19 +293,19 @@ void basic_math_test(const string& msg) {
// Integers a,b Calculates a-b, and stores result in a Results to 0, // Integers a,b Calculates a-b, and stores result in a Results to 0,
// when a<b, since there is no concept of negative number as of now // when a<b, since there is no concept of negative number as of now
// ConvertToInt converts T a to integer // ConvertToInt converts T a to integer
{ // {
// TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER // // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER
//
T a("20489"); // T a("20489");
T b("2034455"); // T b("2034455");
//
a-=b; // a-=b;
expectedResult = 0; // expectedResult = 0;
//
//SINCE THERE IS NO CONCEPT OF NEGATIVE NUMBER RESULT SHOULD BE ZERO // //SINCE THERE IS NO CONCEPT OF NEGATIVE NUMBER RESULT SHOULD BE ZERO
EXPECT_EQ(expectedResult, a.ConvertToInt()) // EXPECT_EQ(expectedResult, a.ConvertToInt())
<< msg << " Failure testing minus_equals_a_less_than_b"; // << msg << " Failure testing minus_equals_a_less_than_b";
} // }
// TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO THE SECOND NUMBER // TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO THE SECOND NUMBER
{ {
T a("2048956567"); T a("2048956567");
......
/*
* @file
* @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.
*
*/
/*
BFV RNS testing programs
*/
#include <iostream>
#include <fstream>
#include <limits>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "palisade.h"
#include "cryptocontexthelper.h"
#include "encoding/encodings.h"
#include "utils/debug.h"
using namespace std;
using namespace lbcrypto;
#include <iterator>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void Sharpen();
void KeyGen(size_t size);
void Encrypt(size_t size);
void Evaluate(size_t size);
void Decrypt(size_t size);
int main(int argc, char **argv) {
static int operation_flag;
int opt;
static struct option long_options[] =
{
/* These options set a flag. */
//{"verbose", no_argument, &verbose_flag, 1},
//{"brief", no_argument, &verbose_flag, 0},
{"run", no_argument, &operation_flag, 0},
{"keygen", no_argument, &operation_flag, 1},
{"encrypt", no_argument, &operation_flag, 2},
{"evaluate", no_argument, &operation_flag, 3},
{"decrypt", no_argument, &operation_flag, 4},
/* These options don�t set a flag.
We distinguish them by their indices. */
{"size", required_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
size_t size = 0;
while ((opt = getopt_long(argc, argv, "s:h", long_options, &option_index)) != -1) {
switch (opt)
{
case 0:
if (long_options[option_index].flag != 0)
break;
break;
case 's':
size = stoi(optarg);
break;
case 'h':
default: /* '?' */
std::cerr<< "Usage: "<<argv[0]<<" <arguments> " <<std::endl
<< "arguments:" <<std::endl
<< " --run simple run w/o serialization" <<std::endl
<< " --keygen --encrypt --evaluate --decrypt operation to run" <<std::endl
<< " -s --size size of the image" <<std::endl
<< " -h --help prints this message" <<std::endl;
exit(EXIT_FAILURE);
}
}
switch(operation_flag)
{
case 0:
Sharpen();
break;
case 1:
KeyGen(size);
break;
case 2:
Encrypt(size);
break;
case 3:
Evaluate(size);
break;
case 4:
Decrypt(size);
break;
default:
exit(EXIT_FAILURE);
}
//Sharpen();
//cin.get();
return 0;
}
#define PROFILE
CryptoContext<DCRTPoly> DeserializeContext(const string& ccFileName)
{
std::cout << "Deserializing the crypto context...";
Serialized ccSer;
if (SerializableHelper::ReadSerializationFromFile(ccFileName, &ccSer) == false) {
cerr << "Could not read the cryptocontext file" << endl;
return 0;
}
CryptoContext<DCRTPoly> cc = CryptoContextFactory<DCRTPoly>::DeserializeAndCreateContext(ccSer);
std::cout << "Completed" << std::endl;
return cc;
}
CryptoContext<DCRTPoly> DeserializeContextWithEvalKeys(const string& ccFileName, const string& eaFileName)
{
std::cout << "Deserializing the crypto context...";
Serialized ccSer, eaSer;
if (SerializableHelper::ReadSerializationFromFile(ccFileName, &ccSer) == false) {
cerr << "Could not read the cryptocontext file" << endl;
return 0;
}
if (SerializableHelper::ReadSerializationFromFile(eaFileName, &eaSer) == false) {
cerr << "Could not read the eval automorphism key file" << endl;
return 0;
}
CryptoContext<DCRTPoly> cc = CryptoContextFactory<DCRTPoly>::DeserializeAndCreateContext(ccSer);
if( cc->DeserializeEvalAutomorphismKey(eaSer) == false ) {
cerr << "Could not deserialize the eval automorphism key file" << endl;
return 0;
}
std::cout << "Completed" << std::endl;
return cc;
}
void KeyGen(size_t size) {
TimeVar t1, t_total; //for TIC TOC
TIC(t_total);
double timeKeyGen(0.0), timeSer(0.0), timeTotal(0.0);
usint ptm = 65537;
double sigma = 3.19;
double rootHermiteFactor = 1.004;
std::cout << "Generating parameters...";
EncodingParams encodingParams(new EncodingParamsImpl(ptm));
CryptoContext<DCRTPoly> cryptoContext = CryptoContextFactory<DCRTPoly>::genCryptoContextBFVrns(
encodingParams, rootHermiteFactor, sigma, 0, 1, 0, OPTIMIZED,2,30,60);
uint32_t m = cryptoContext->GetCyclotomicOrder();
PackedEncoding::SetParams(m, encodingParams);
uint32_t batchSize = m/4;
encodingParams->SetBatchSize(batchSize);
std::cout << "p = " << cryptoContext->GetCryptoParameters()->GetPlaintextModulus() << std::endl;
std::cout << "n = " << cryptoContext->GetCryptoParameters()->GetElementParams()->GetCyclotomicOrder() / 2 << std::endl;
std::cout << "log2 q = " << cryptoContext->GetCryptoParameters()->GetElementParams()->GetModulus().GetMSB() << std::endl;
// enable features that you wish to use
cryptoContext->Enable(ENCRYPTION);
cryptoContext->Enable(SHE);
std::cout << "Completed" << std::endl;
std::cout << "Generating keys...";
// Key generation
LPKeyPair<DCRTPoly> kp;
TIC(t1);
kp = cryptoContext->KeyGen();
timeKeyGen = TOC(t1);
// Read the image file
int width, height, bpp;
string path = "demoData/Baboon" + to_string(size) + ".png";
const char *pathc = path.c_str();
/*unsigned char* data =*/ stbi_load( pathc, &width, &height, &bpp, 1 );
// Rotations we are supporting
vector<int32_t> indexList = {-1-width,-width,-width+1,-1,1,width-1,width,width+1};
TIC(t1);
cryptoContext->EvalAtIndexKeyGen(kp.secretKey,indexList);
timeKeyGen += TOC(t1);
std::cout << "Completed" << std::endl;
TIC(t1);
std::cout << "Serializing crypto context...";
Serialized ctxt;
if (cryptoContext->Serialize(&ctxt)) {
if (!SerializableHelper::WriteSerializationToFile(ctxt, "demoData/cryptocontext.txt")) {