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.11k stars 759 forks source link

How to perform polyEval() ? #469

Open pandasamanvaya opened 2 years ago

pandasamanvaya commented 2 years ago

Hi, everyone. I am new to HElib and recently tried to use the polyEval() function provided in the helib/polyEval.h header file. I tried to evaluate a simple polynomial x+x^2. But I am getting random values as output. Is there any documentation on how to use the polyEval()? Here is the code snippet -

#include<helib/helib.h>
#include<helib/polyEval.h>

using namespace std;
using namespace helib;

int main()
{
    //Defining CKKS scheme parameters
    Context context = ContextBuilder<CKKS>().m(16384).bits(119).precision(20).c(2).build();
    long n = context.getNSlots();

    //Generating keys
    SecKey secretKey(context);
    secretKey.GenSecKey();
    const PubKey& publicKey = secretKey;

    vector<int> x = {1, 2}, coeff={0, 1, 1};
    NTL::ZZX poly;
    for(int i=1; i < coeff.size(); i++){
        SetCoeff(poly, i, coeff[i]);
    }
    PtxtArray x0(context, x);
    Ctxt ct(publicKey);
    x0.encrypt(ct);

    Ctxt ans(publicKey);
    polyEval(ans, poly, ct);
    PtxtArray res(context);
    res.decrypt(ans, secretKey);

    vector<double> y;
    res.store(y);
    cout << y[0] << " " << y[1] << "\n";
}