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.15k stars 763 forks source link

Bug or bad params? divison by zero NTL #223

Open AlexDanDuna opened 6 years ago

AlexDanDuna commented 6 years ago

I'm really not sure why, but this code gives me an NTL division by zero error upon attempting to encrypt a 1 :(. It appears to be because for some reason in the public key, the 'ptxtSpace' is zero and it reaches a point where a division by this value is done.. NOTE: I generated m, gens, ords, mvec using parmams.cpp.

`#include

include

include <NTL/BasicThreadPool.h>

NTL_CLIENT

include "FHE.h"

include "EncryptedArray.h"

include "intraSlot.h"

include "binaryArith.h"

include "binaryCompare.h"

typedef unsigned long uLong;

using namespace std; using namespace NTL;

int main() { SetNumThreads(12);

uLong m, p, r;

m = 1001;
p = 2;
r = 1;

uLong L, c;
L = 30; // 30 levels without bootstrapping
c = 3;  // numCols of KeySwitch matrix

vector<long> gens {430, 274};
vector<long> ords {6, 2};
Vec<long> mvec;
mvec.append(7);
mvec.append(143);

FHEcontext context (m, p, r, gens, ords);
buildModChain(context, L, c, 7);
context.makeBootstrappable(mvec, 0, false, true); // BOOTSTRAPPING!

EncryptedArray ea(context);
std::vector<zzX> unpackSlotEncoding;
buildUnpackSlotEncoding(unpackSlotEncoding, ea);

FHESecKey secretKey (context);
FHEPubKey publicKey = secretKey;

uLong w = 64; // Hamming weight
secretKey.GenSecKey(w);
addSome1DMatrices(secretKey);
addFrbMatrices(secretKey);
secretKey.genRecryptData();

Ctxt encBit(publicKey);
publicKey.Encrypt(encBit, to_ZZX(1));

return 0;

}`

fionser commented 6 years ago
AlexDanDuna commented 6 years ago

I.. I .. I would've never guessed that was the problem. Thank you SO much! using GenSecKey actually solved the problem. I'll also use the reference advice!

fionser commented 6 years ago

@PhantomR You can think in this way

  1. you create an empty secret key
  2. then you create a new public key based on the empty secret key
  3. then you fill in the secrete key. But the public key is still empty.
AlexDanDuna commented 6 years ago

It makes a lot of sense now! Thak you! :)