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

hot fix changes

parent 46f872d2
......@@ -30,6 +30,7 @@
#define _USE_MATH_DEFINES
#include "math/backend.h"
#include "lattice/backend.h"
#include "utils/inttypes.h"
#include "math/nbtheory.h"
#include "math/distrgen.h"
......
......@@ -42,6 +42,7 @@
#include <iostream>
#include "math/backend.h"
#include "lattice/backend.h"
#include "utils/inttypes.h"
#include "math/nbtheory.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() {
for (size_t i = 1; i < nativeParams.size(); i++ ) {
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);
}
......
......@@ -1075,7 +1075,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p,
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)
......@@ -1095,7 +1095,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p,
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
......@@ -1120,7 +1120,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p,
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
......@@ -1140,7 +1140,7 @@ DCRTPolyImpl<VecType>::ScaleAndRound(const NativeInteger &p,
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:
* @return result of the subtraction operation of type BigInteger.
*/
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,9 +275,6 @@ public:
* @return result of the subtraction operation of type BigInteger.
*/
const NativeInteger& MinusEq(const NativeInteger& b) {
if( m_value <= b.m_value )
m_value = 0;
else
m_value -= b.m_value;
return *this;
}
......@@ -535,15 +533,15 @@ public:
* @return result of the modulus addition operation.
*/
NativeInteger ModAddFastOptimized(const NativeInteger& b, const NativeInteger& modulus) const {
//#if NTL_BITS_PER_LONG==64
// return (uint_type)NTL::AddMod(this->m_value,b.m_value,modulus.m_value);
//#else
#if NTL_BITS_PER_LONG==64
return (uint_type)NTL::AddMod(this->m_value,b.m_value,modulus.m_value);
#else
Duint_type modsum = (Duint_type)m_value;
modsum += b.m_value;
if (modsum >= modulus.m_value)
modsum %= modulus.m_value;
return (uint_type)modsum;
//#endif
#endif
}
/**
......@@ -554,15 +552,15 @@ public:
* @return result of the modulus addition operation.
*/
const NativeInteger& ModAddFastOptimizedEq(const NativeInteger& b, const NativeInteger& modulus) {
//#if NTL_BITS_PER_LONG==64
// this->m_value = (uint_type)NTL::AddMod(this->m_value,b.m_value,modulus.m_value);
//#else
#if NTL_BITS_PER_LONG==64
this->m_value = (uint_type)NTL::AddMod(this->m_value,b.m_value,modulus.m_value);
#else
Duint_type modsum = (Duint_type)m_value;
modsum += b.m_value;
if (modsum >= modulus.m_value)
modsum %= modulus.m_value;
this->m_value = (uint_type)modsum;
//#endif
#endif
return *this;
}
......
......@@ -37,9 +37,7 @@ class ParallelControls {
public:
ParallelControls() {
machineThreads = omp_get_max_threads();
//omp_set_dynamic(1);
//omp_set_nested(1);
Disable();
Enable();
}
void Enable() {
......
......@@ -237,20 +237,20 @@ void basic_math_test(const string& msg) {
// support for negative numbers as of now ConvertToInt converts
// T calculatedResult to integer
{
// TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER
T a("20489");
T b("2034455");
calculatedResult = a.Minus(b);
expectedResult = 0;
//SINCE THERE IS NO CONCEPT OF NEGATIVE NUMEBR RESULT SHOULD BE
//ZERO
EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
<< msg << " Failure testing minus_a_less_than_b";
}
// {
// // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER
//
// T a("20489");
// T b("2034455");
//
// calculatedResult = a.Minus(b);
// expectedResult = 0;
//
// //SINCE THERE IS NO CONCEPT OF NEGATIVE NUMEBR RESULT SHOULD BE
// //ZERO
// EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
// << msg << " Failure testing minus_a_less_than_b";
// }
// TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO THE SECOND NUMBER
{
T a("2048956567");
......@@ -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,
// when a<b, since there is no concept of negative number as of now
// ConvertToInt converts T a to integer
{
// TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER
T a("20489");
T b("2034455");
a-=b;
expectedResult = 0;
//SINCE THERE IS NO CONCEPT OF NEGATIVE NUMBER RESULT SHOULD BE ZERO
EXPECT_EQ(expectedResult, a.ConvertToInt())
<< msg << " Failure testing minus_equals_a_less_than_b";
}
// {
// // TEST_F CASE WHEN FIRST NUMBER IS LESS THAN THE SECOND NUMBER
//
// T a("20489");
// T b("2034455");
//
// a-=b;
// expectedResult = 0;
//
// //SINCE THERE IS NO CONCEPT OF NEGATIVE NUMBER RESULT SHOULD BE ZERO
// EXPECT_EQ(expectedResult, a.ConvertToInt())
// << msg << " Failure testing minus_equals_a_less_than_b";
// }
// TEST_F CASE WHEN FIRST NUMBER IS EQUAL TO THE SECOND NUMBER
{
T a("2048956567");
......
This diff is collapsed.
This diff is collapsed.
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