homenc / HElib

HElib is an open-source software library that implements homomorphic encryption. It supports the BGV scheme with bootstrapping and the Approximate Number CKKS scheme. HElib also includes optimizations for efficient homomorphic evaluation, focusing on effective use of ciphertext packing techniques and on the Gentry-Halevi-Smart optimizations.
https://homenc.github.io/HElib
Other
3.14k stars 765 forks source link

Why is decryption time much slower than encryption time when m is big? #370

Open siaaron045 opened 4 years ago

siaaron045 commented 4 years ago

When I was running my own program which m=52609,the enc time is 0.33s while the dec time is 16.47s.why? Thank you.

fionser commented 4 years ago
siaaron045 commented 4 years ago

I just do one encryption and one decryption.And I know this case usually does not happen, I want to know why and how to fix it.Thank you. `#include

include

include "FHE.h"

include "EncryptedArray.h"

include "hypercube.h"

include <NTL/ZZX.h>

include <NTL/ZZ.h>

include

include

include "permutations.h"

include "NumbTh.h"

//#include "ArgMap.h"

include "replicate.h"

include

//#include "sf-moremat.h"

define CLOCKS_PER_MSE ((clock_t)1000)

using namespace std;

define VEC_SIZE 4

int main() { / BEGIN INITIALIZATION / long m = 0; // Specific modulus long p = 257; // Plaintext base [default=2], should be a prime number long r = 1;//1 // Lifting [default=1] long L = 6;//10 // Number of levels in the modulus chain [default=heuristic] long c = 5; // Number of columns in key-switching matrix [default=2] long w = 64; // Hamming weight of secret key long d = 1; // Degree of the field extension [default=1] long k = 80; // Security parameter [default=80] long s = 0; // Minimum number of slots [default=0]

    cout<<"L=  "<<L<<endl;

clock_t start,finish;
start = clock();
std::cout << "Finding m... " << std::flush;
//m = FindM(k, L, c, p, d, s, 0);           // Find a value for m given the specified values
m = 42181;
std::cout << "m = " << m << std::endl;

std::cout << "Initializing context... " << std::flush;
FHEcontext context(m, p, r);              // Initialize context
buildModChain(context, L, c);             // Modify the context, adding primes to the modulus chain
std::cout << "OK!" << std::endl;

std::cout << "Generating keys... " << std::flush;
FHESecKey sk(context);                    // Construct a secret key structure
const FHEPubKey& pk = sk;                 // An "upcast": FHESecKey is a subclass of FHEPubKey
sk.GenSecKey(w);                          // Actually generate a secret key with Hamming weight
addSome1DMatrices(sk);                    // Extra information for relinearization
std::cout << "OK!" << std::endl;
finish = clock();
cout<<"Time taken for the initialization:"<<(finish-start)/CLOCKS_PER_MSE<<"ms"<<endl;

EncryptedArray ea(context, context.alMod.getFactorsOverZZ()[0]);
Ctxt temp(pk);
NewPlaintextArray p0(ea);
random(ea,p0);
//clock_t start,finish;
start = clock();
ea.encrypt(temp,pk,p0);
finish = clock();
cout<<"enc run time:"<<(finish-start)/CLOCKS_PER_MSE<<"ms"<<endl;

start = clock();
vector<long> result;

start = clock();
ea.decrypt(temp,sk,result);
finish = clock();
cout<<"dec run time:"<<(finish-start)/CLOCKS_PER_MSE<<"ms"<<endl;
return 0;

} `