Commit 1f9fe2bb authored by Gerard Ryan's avatar Gerard Ryan
Browse files

fix benchmarks, finishing up all polys in one

parent 564c3883
......@@ -113,7 +113,7 @@ PALISADEPYLIB := pycrypto$(LIBSUFFIX)
all:
$(MAKE) gmp_all
$(MAKE) ntl_all
$(MAKE) allcore allpke alltrapdoor allcircuit utall
$(MAKE) allcore allpke alltrapdoor allcircuit utall benchmark
docs: apidocs
......
......@@ -52,8 +52,8 @@ using namespace lbcrypto;
// add
static void add_BigVec(benchmark::State& state) {
state.PauseTiming();
BigVector a = makeVector(parmArray[state.range(0)]);
BigVector b = makeVector(parmArray[state.range(0)]);
BigVector a = makeVector<Poly>(parmArray[state.range(0)]);
BigVector b = makeVector<Poly>(parmArray[state.range(0)]);
state.ResumeTiming();
a = a+b;
......@@ -71,8 +71,8 @@ DO_PARM_BENCHMARK(BM_BigVec_Addition)
// +=
static void addeq_BigVec(benchmark::State& state) {
state.PauseTiming();
BigVector a = makeVector(parmArray[state.range(0)]);
BigVector b = makeVector(parmArray[state.range(0)]);
BigVector a = makeVector<Poly>(parmArray[state.range(0)]);
BigVector b = makeVector<Poly>(parmArray[state.range(0)]);
state.ResumeTiming();
a += b;
......@@ -90,8 +90,8 @@ DO_PARM_BENCHMARK(BM_BigVec_Addeq)
// mult
static void mult_BigVec(benchmark::State& state) { // function
state.PauseTiming();
BigVector a = makeVector(parmArray[state.range(0)]);
BigVector b = makeVector(parmArray[state.range(0)]);
BigVector a = makeVector<Poly>(parmArray[state.range(0)]);
BigVector b = makeVector<Poly>(parmArray[state.range(0)]);
state.ResumeTiming();
a = a*b;
......@@ -109,8 +109,8 @@ DO_PARM_BENCHMARK(BM_BigVec_Multiplication)
// mult
static void multeq_BigVec(benchmark::State& state) { // function
state.PauseTiming();
BigVector a = makeVector(parmArray[state.range(0)]);
BigVector b = makeVector(parmArray[state.range(0)]);
BigVector a = makeVector<Poly>(parmArray[state.range(0)]);
BigVector b = makeVector<Poly>(parmArray[state.range(0)]);
state.ResumeTiming();
a *= b;
......
......@@ -61,51 +61,55 @@ void BM_keygen(benchmark::State& state) { // benchmark
if( state.thread_index == 0 ) {
state.PauseTiming();
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
try {
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
} catch( ... ) {
state.SkipWithError("Unable to make context");
return;
}
try {
ChineseRemainderTransformFTT<BigInteger,BigVector>::PreCompute(cc->GetRootOfUnity(),
ChineseRemainderTransformFTT<BigInteger,BigVector>::PreCompute(cc->GetRootOfUnity(),
cc->GetCyclotomicOrder(),
cc->GetModulus());
} catch( ... ) {}
try {
typename Poly::DggType dgg = Poly::DggType(4); // Create the noise generator
} catch( ... ) {}
state.ResumeTiming();
}
while (state.KeepRunning()) {
LPKeyPair<Poly> kp = cc->KeyGen();
//try {
LPKeyPair<Poly> kp = cc->KeyGen();
//} catch( ... ) {}
}
}
BENCHMARK_PARMS(BM_keygen)
static void fillrandint(vector<int64_t>& vec, PlaintextModulus mod) {
mod /= 2;
for( int ii=0; ii < vec.size(); ii++ )
vec[ii] = rand() % mod;
};
void BM_encrypt(benchmark::State& state) { // benchmark
CryptoContext<Poly> cc;
LPKeyPair<Poly> kp;
Ciphertext<Poly> ciphertext;
Plaintext plaintext;
auto randchar = []() -> char {
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[ rand() % max_index ];
};
if( state.thread_index == 0 ) {
state.PauseTiming();
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
try {
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
} catch( ... ) {
state.ResumeTiming();
return;
}
try {
ChineseRemainderTransformFTT<BigInteger,BigVector>::PreCompute(cc->GetRootOfUnity(),
......@@ -113,19 +117,15 @@ void BM_encrypt(benchmark::State& state) { // benchmark
cc->GetModulus());
} catch( ... ) {}
try {
typename Poly::DggType dgg = Poly::DggType(4); // Create the noise generator
} catch( ... ) {}
size_t strSize = cc->GetRingDimension();
if( strSize == 0 ) {
state.SkipWithError( "Chunk size is 0" );
}
string shortStr(strSize,0);
std::generate_n(shortStr.begin(), strSize, randchar);
plaintext = cc->MakeStringPlaintext(shortStr);
vector<int64_t> input(strSize,0);
fillrandint(input, cc->GetEncodingParams()->GetPlaintextModulus());
plaintext = cc->MakeCoefPackedPlaintext(input);
state.ResumeTiming();
}
......@@ -148,21 +148,16 @@ void BM_decrypt(benchmark::State& state) { // benchmark
Plaintext plaintext;
Plaintext plaintextNew;
auto randchar = []() -> char {
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[ rand() % max_index ];
};
if( state.thread_index == 0 ) {
state.PauseTiming();
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
try {
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
} catch( ... ) {
state.ResumeTiming();
return;
}
try {
ChineseRemainderTransformFTT<BigInteger,BigVector>::PreCompute(cc->GetRootOfUnity(),
......@@ -170,19 +165,15 @@ void BM_decrypt(benchmark::State& state) { // benchmark
cc->GetModulus());
} catch( ... ) {}
try {
typename Poly::DggType dgg = Poly::DggType(4); // Create the noise generator
} catch( ... ) {}
size_t strSize = cc->GetRingDimension();
if( strSize == 0 ) {
state.SkipWithError( "Chunk size is 0" );
}
string shortStr(strSize,0);
std::generate_n(shortStr.begin(), strSize, randchar);
plaintext = cc->MakeStringPlaintext(shortStr);
vector<int64_t> input(strSize,0);
fillrandint(input, cc->GetEncodingParams()->GetPlaintextModulus());
plaintext = cc->MakeCoefPackedPlaintext(input);
state.ResumeTiming();
}
......@@ -205,20 +196,20 @@ void BM_rekeygen(benchmark::State& state) { // benchmark
if( state.thread_index == 0 ) {
state.PauseTiming();
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
cc->Enable(SHE);
try {
ChineseRemainderTransformFTT<BigInteger,BigVector>::PreCompute(cc->GetRootOfUnity(),
cc->GetCyclotomicOrder(),
cc->GetModulus());
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
cc->Enable(SHE);
} catch( ... ) {
state.ResumeTiming();
return;
}
try {
typename Poly::DggType dgg = Poly::DggType(4); // Create the noise generator
ChineseRemainderTransformFTT<BigInteger,BigVector>::PreCompute(cc->GetRootOfUnity(),
cc->GetCyclotomicOrder(),
cc->GetModulus());
} catch( ... ) {
}
......@@ -251,21 +242,16 @@ void BM_reencrypt(benchmark::State& state) { // benchmark
Plaintext plaintext;
Plaintext plaintextNew;
auto randchar = []() -> char {
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[ rand() % max_index ];
};
if( state.thread_index == 0 ) {
state.PauseTiming();
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
try {
cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
cc->Enable(ENCRYPTION);
cc->Enable(PRE);
} catch( ... ) {
state.ResumeTiming();
return;
}
try {
ChineseRemainderTransformFTT<BigInteger,BigVector>::PreCompute(cc->GetRootOfUnity(),
......@@ -273,19 +259,15 @@ void BM_reencrypt(benchmark::State& state) { // benchmark
cc->GetModulus());
} catch( ... ) {}
try {
typename Poly::DggType dgg = Poly::DggType(4); // Create the noise generator
} catch( ... ) {}
size_t strSize = cc->GetRingDimension();
if( strSize == 0 ) {
state.SkipWithError( "Chunk size is 0" );
}
string shortStr(strSize,0);
std::generate_n(shortStr.begin(), strSize, randchar);
plaintext = cc->MakeStringPlaintext(shortStr);
vector<int64_t> input(strSize,0);
fillrandint(input, cc->GetEncodingParams()->GetPlaintextModulus());
plaintext = cc->MakeCoefPackedPlaintext(input);
state.ResumeTiming();
}
......
......@@ -73,7 +73,7 @@ void BM_encoding_Integer(benchmark::State& state) { // benchmark
usint m = 1024;
PlaintextModulus ptm = 128;
int64_t mv = ((int64_t)1<<33) + (int64_t)1;
int64_t mv = 58;
shared_ptr<ILParams> lp = ElemParamFactory::GenElemParams<ILParams,BigInteger>(m);
EncodingParams ep( new EncodingParamsImpl(ptm) );
......@@ -96,14 +96,14 @@ void BM_encoding_CoefPacked(benchmark::State& state) {
usint m = 1024;
PlaintextModulus ptm = 128;
int64_t mv = ((int64_t)1<<33) + (int64_t)1;
PlaintextModulus half = ptm / 2;
shared_ptr<ILParams> lp = ElemParamFactory::GenElemParams<ILParams,BigInteger>(m);
EncodingParams ep( new EncodingParamsImpl(ptm) );
vector<int64_t> intvec;
for( usint ii=0; ii<m/2; ii++)
intvec.push_back( rand() % ptm );
intvec.push_back( rand() % half );
plaintext.reset( new CoefPackedEncoding(lp,ep,intvec) );
state.ResumeTiming();
......
This diff is collapsed.
......@@ -32,9 +32,9 @@
#include "math/backend.h"
using namespace lbcrypto;
template<typename T>
inline BigVector makeVector(shared_ptr<T> p) {
Poly::DugType dug;
template<typename P>
inline typename P::Vector makeVector(shared_ptr<typename P::Params> p) {
typename P::DugType dug;
dug.SetModulus(p->GetModulus());
return dug.GenerateVector(p->GetCyclotomicOrder()/2);
......
......@@ -67,7 +67,7 @@ DCRTPolyImpl<ModType,IntType,VecType,ParmType>::DCRTPolyImpl(const DCRTPolyImpl
template<typename ModType, typename IntType, typename VecType, typename ParmType>
const DCRTPolyImpl<ModType,IntType,VecType,ParmType>&
DCRTPolyImpl<ModType,IntType,VecType,ParmType>::operator=(const Poly &element)
DCRTPolyImpl<ModType,IntType,VecType,ParmType>::operator=(const PolyLargeType &element)
{
if( element.GetModulus() > m_params->GetModulus() ) {
......@@ -104,7 +104,7 @@ DCRTPolyImpl<ModType,IntType,VecType,ParmType>::operator=(const Poly &element)
/* Construct from a single Poly. The format is derived from the passed in Poly.*/
template<typename ModType, typename IntType, typename VecType, typename ParmType>
DCRTPolyImpl<ModType,IntType,VecType,ParmType>::DCRTPolyImpl(const Poly &element, const shared_ptr<ParmType> params)
DCRTPolyImpl<ModType,IntType,VecType,ParmType>::DCRTPolyImpl(const PolyLargeType &element, const shared_ptr<ParmType> params)
{
Format format;
try {
......@@ -278,8 +278,8 @@ DCRTPolyImpl<ModType,IntType,VecType,ParmType> DCRTPolyImpl<ModType,IntType,VecT
// create an Element to pull from
// create a dummy parm to use in the Poly world
shared_ptr<ILParams> parm( new ILParams(m_params->GetCyclotomicOrder(), m_params->GetModulus(), 1) );
Poly element( parm );
shared_ptr<ILParamsImpl<IntType>> parm( new ILParamsImpl<IntType>(m_params->GetCyclotomicOrder(), m_params->GetModulus(), 1) );
PolyLargeType element( parm );
element.SetValues( randVec, m_format );
res = element;
......@@ -328,11 +328,11 @@ std::vector<DCRTPolyImpl<ModType,IntType,VecType,ParmType>> DCRTPolyImpl<ModType
DEBUG("...::BaseDecompose" );
DEBUG("baseBits=" << baseBits );
Poly v( CRTInterpolate() );
PolyLargeType v( CRTInterpolate() );
DEBUG("<v>" << std::endl << v << "</v>" );
std::vector<Poly> bdV = v.BaseDecompose(baseBits, false);
std::vector<PolyLargeType> bdV = v.BaseDecompose(baseBits, false);
DEBUG("<bdV>" );
for( auto i : bdV )
......@@ -914,7 +914,7 @@ void DCRTPolyImpl<ModType,IntType,VecType,ParmType>::ModReduce(const IntType &pl
* for parameter selection of the Poly.
*/
template<typename ModType, typename IntType, typename VecType, typename ParmType>
Poly DCRTPolyImpl<ModType,IntType,VecType,ParmType>::CRTInterpolate() const
typename DCRTPolyImpl<ModType,IntType,VecType,ParmType>::PolyLargeType DCRTPolyImpl<ModType,IntType,VecType,ParmType>::CRTInterpolate() const
{
bool dbg_flag = false;
......@@ -926,21 +926,21 @@ Poly DCRTPolyImpl<ModType,IntType,VecType,ParmType>::CRTInterpolate() const
for( usint vi = 0; vi < nTowers; vi++ )
DEBUG("tower " << vi << " is " << m_vectors[vi]);
BigInteger bigModulus(GetModulus()); // qT
ModType bigModulus(GetModulus()); // qT
DEBUG("bigModulus " << bigModulus);
// this is the resulting vector of coefficients
BigVector coefficients(ringDimension, bigModulus);
VecType coefficients(ringDimension, bigModulus);
// this will finally be V[j]= {Sigma(i = 0 --> t-1) ValueOf M(r,i) * qt/qj *[ (qt/qj)^(-1) mod qj ]}modqt
// first, precompute qt/qj factors
vector<BigInteger> multiplier(nTowers);
vector<IntType> multiplier(nTowers);
for( usint vi = 0 ; vi < nTowers; vi++ ) {
BigInteger qj(m_vectors[vi].GetModulus().ConvertToInt());
BigInteger divBy = bigModulus / qj;
BigInteger modInv = divBy.ModInverse(qj).Mod(qj);
IntType qj(m_vectors[vi].GetModulus().ConvertToInt());
IntType divBy = bigModulus / qj;
IntType modInv = divBy.ModInverse(qj).Mod(qj);
multiplier[vi] = divBy * modInv;
DEBUG("multiplier " << vi << " " << qj << " " << multiplier[vi]);
......@@ -963,14 +963,14 @@ Poly DCRTPolyImpl<ModType,IntType,VecType,ParmType>::CRTInterpolate() const
DEBUG("tower " << vi << " is " << (*vecs)[vi]);
//Precompute the Barrett mu parameter
BigInteger mu = ComputeMu<BigInteger>(bigModulus);
IntType mu = ComputeMu<IntType>(bigModulus);
// now, compute the values for the vector
#pragma omp parallel for
for( usint ri = 0; ri < ringDimension; ri++ ) {
coefficients[ri] = 0;
for( usint vi = 0; vi < nTowers; vi++ ) {
coefficients[ri] += (BigInteger((*vecs)[vi].GetValues()[ri].ConvertToInt()) * multiplier[vi]);
coefficients[ri] += (IntType((*vecs)[vi].GetValues()[ri].ConvertToInt()) * multiplier[vi]);
}
DEBUG( (*vecs)[0].GetValues()[ri] << " * " << multiplier[0] << " == " << coefficients[ri] );
coefficients[ri].ModBarrettInPlace(bigModulus,mu);
......@@ -986,7 +986,7 @@ Poly DCRTPolyImpl<ModType,IntType,VecType,ParmType>::CRTInterpolate() const
DEBUG("modulus "<< bigModulus);
// Setting the root of unity to ONE as the calculation is expensive and not required.
Poly polynomialReconstructed( shared_ptr<ILParams>( new ILParams(GetCyclotomicOrder(), bigModulus, 1) ) );
typename DCRTPolyImpl<ModType,IntType,VecType,ParmType>::PolyLargeType polynomialReconstructed( shared_ptr<ILParamsImpl<IntType>>( new ILParamsImpl<IntType>(GetCyclotomicOrder(), bigModulus, 1) ) );
polynomialReconstructed.SetValues(coefficients,COEFFICIENT);
DEBUG("answer: " << polynomialReconstructed);
......@@ -1301,7 +1301,7 @@ bool DCRTPolyImpl<ModType,IntType,VecType,ParmType>::InverseExists() const
template<typename ModType, typename IntType, typename VecType, typename ParmType>
double DCRTPolyImpl<ModType, IntType, VecType, ParmType>::Norm() const
{
Poly poly(CRTInterpolate());
PolyLargeType poly(CRTInterpolate());
return poly.Norm();
}
......
......@@ -69,7 +69,9 @@ public:
// this class contains an array of these:
typedef PolyImpl<NativeInteger,NativeInteger,NativeVector,ILNativeParams> PolyType;
typedef PolyImpl<ModType,IntType,VecType,ILParams> PolyLargeType;
// the composed polynomial type
typedef PolyImpl<ModType,IntType,VecType,ILParamsImpl<IntType>> PolyLargeType;
static const std::string GetElementName() {
return "DCRTPolyImpl";
......@@ -91,7 +93,7 @@ public:
*/
DCRTPolyImpl(const shared_ptr<ParmType> params, Format format = EVALUATION, bool initializeElementToZero = false);
const DCRTPolyType& operator=(const Poly& element);
const DCRTPolyType& operator=(const PolyLargeType& element);
/**
* @brief Constructor based on discrete Gaussian generator.
......@@ -137,7 +139,7 @@ public:
* @param &element Poly to build other towers from.
* @param params parameter set required for DCRTPoly.
*/
DCRTPolyImpl(const Poly &element, const shared_ptr<ParmType> params);
DCRTPolyImpl(const PolyLargeType &element, const shared_ptr<ParmType> params);
/**
* @brief Construct using an tower of ILVectro2ns. The params and format for the DCRTPoly will be derived from the towers.
......@@ -684,11 +686,7 @@ public:
*
* @return the interpolated ring element as a Poly object.
*/
Poly CRTInterpolate() const;
// NativePoly CRTInterpolate() {
// PALISADE_THROW(config_error, "DCRT interpolate to NativePoly is not supported");
// }
PolyLargeType CRTInterpolate() const;
NativePoly DecryptionCRTInterpolate(PlaintextModulus ptm) const;
......
......@@ -41,7 +41,7 @@ ILDCRTParams<IntType>::ILDCRTParams(usint order, usint depth, usint bits) : Elem
throw std::logic_error("Invalid bits for ILDCRTParams");
m_parms.resize(depth);
this->ciphertextModulus = BigInteger(0);
this->ciphertextModulus = IntType(0);
NativeInteger q = FirstPrime<NativeInteger>(bits, order);
......
......@@ -70,7 +70,7 @@ public:
* @param &modulus is the modulus for the primary ciphertext.
* @param rootsOfUnity is unused
*/
ILDCRTParams(const usint cyclotomic_order, const BigInteger &modulus, const BigInteger& rootOfUnity)
ILDCRTParams(const usint cyclotomic_order, const IntType &modulus, const IntType& rootOfUnity)
: ElemParams<IntType>(cyclotomic_order, modulus, 0, 0, 0) {
// note this does not create a tower of native params
}
......
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