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 764 forks source link

Do I have to also send the secret key when using Binary IO? #226

Open AlexDanDuna opened 6 years ago

AlexDanDuna commented 6 years ago

I'm trying to do a client-server application, where the client generates the context, PK and SK and then sends the context and PK to the server like this:

Creation:

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

    ea = context.ea;
    buildUnpackSlotEncoding(unpackSlotEncoding, *ea);

    FHESecKey secretKey (context);
    uLong w = 2; // Hamming weight // TODO: VERY SMALL!
    secretKey.GenSecKey(w);
    FHEPubKey& publicKey {secretKey};

    addSome1DMatrices(secretKey);
    addFrbMatrices(secretKey);
    secretKey.genRecryptData();

Sending:

    writeContextBaseBinary(connect, context);
    writeContextBinary(connect, context);
    writePubKeyBinary(connect, publicKey);

The server tries to rebuild the context and public key like this:

            readContextBaseBinary(client, m, p, r, gens, ords);
            FHEcontext context(m, p, r, gens, ords);
            readContextBinary(client, context);
            ea = context.ea;
            buildUnpackSlotEncoding(unpackSlotEncoding, *ea);
            FHEPubKey publicKey(context);
            readPubKeyBinary(client, publicKey);

The problem is that the results I get from the computation on the server are wrong when I decrypt them on the client :(. Is boostrapping supported by Binary IO? Did I miss any step in sending/receiving the context and public key taking into account the fact that boostrapping is used?

shaih commented 6 years ago

The code looks right to me, maybe the problem is with the choice of parameters? Does bootstrapping work when you do it on the client side? Does the problem persist when you use the ascii I/O instead of the binary one?

AlexDanDuna commented 6 years ago

Thank you so much! I tried using ASCII (I used the "<<" and ">>" operators, hopefully it's those) and it worked. I am actually unsure whether the binary not working wasn't due to me not doing a clean/rebuild, but I'm a bit short on time to try and see it now.. I'll note it down and give it a try in a few days.

EDIT: To answer the other question, yes, the computation worked when ran entirely on the client, but I sometimes ran into segmentation faults (since it's probably important, I was using more than one thread). If I may ask, could that be because of incorrect parameters for bootstrapping?

Once again, many thanks for your help.