fairmath / polycircuit

Polycircuit is an FHE components library built via FHERMA competitions
https://fherma.io/challenges
Apache License 2.0
15 stars 1 forks source link

How to use signum? #9

Open zzzhhhlll opened 3 months ago

zzzhhhlll commented 3 months ago

m_cc->Enable(ADVANCEDSHE);

m_OutputC = m_cc->EvalChebyshevSeries(m_InputC, coeff_val, -1, 1);

std::vector<Ciphertext<DCRTPoly>> t(1024);
int l = 512;
t[1] = m_InputC;

//--CHEBYSHEV series computation <--- this is very naively implemented---
for (int i = 2; i < l + 1; i++) {
    int j = int((i - 1) / 2) + 1;
    auto prod = m_cc->EvalMult(t[j], t[i - j]);
    t[i] = m_cc->EvalAdd(prod, prod);
    if (2 * j == i)
        m_cc->EvalSubInPlace(t[i], 1);
    else
        m_cc->EvalSubInPlace(t[i], t[2 * j - i]);
}

//----------------------------  T1009,T1011,T1013,T1015 -----------------------------

std::vector<double> coeff_val2(
    {5.3627954846304366e-05, -4.766676484102891e-05, 4.170646728565051e-05, -3.574695081520454e-05});
int len = 4;

for (int i = 0; i < len; i++) {
    double coeff = coeff_val2[i];
    auto temp1 = m_cc->EvalMult(m_cc->EvalMult(t[1 + 2 * i], coeff * 64), t[16]);
    auto temp2 = m_cc->EvalMult(t[15 - 2 * i], coeff * 32);
    temp1 = m_cc->EvalSub(temp1, temp2);
    temp1 = m_cc->EvalMult(temp1, t[32]);
    temp2 = m_cc->EvalMult(t[15 - 2 * i], coeff * 16);
    temp1 = m_cc->EvalSub(temp1, temp2);
    temp1 = m_cc->EvalMult(temp1, t[64]);
    temp2 = m_cc->EvalMult(t[15 - 2 * i], coeff * 8);
    temp1 = m_cc->EvalSub(temp1, temp2);
    temp1 = m_cc->EvalMult(temp1, t[128]);
    temp2 = m_cc->EvalMult(t[15 - 2 * i], coeff * 4);
    temp1 = m_cc->EvalSub(temp1, temp2);
    temp1 = m_cc->EvalMult(temp1, t[256]);
    temp2 = m_cc->EvalMult(t[15 - 2 * i], coeff * 2);
    temp1 = m_cc->EvalSub(temp1, temp2);
    temp1 = m_cc->EvalMult(temp1, t[512]);
    temp2 = m_cc->EvalMult(t[15 - 2 * i], coeff);
    auto t59 = m_cc->EvalSub(temp1, temp2);
    m_OutputC = m_cc->EvalAdd(m_OutputC, t59);
}

Could you explain these codes, please? By the way, how should I use it to compare two ciphertexts, what is the required multiplication depth, and how is the precision controlled?"Thank you very much.

g-arakelov commented 3 months ago

please see examples here: https://github.com/fairmath/polycircuit/tree/main/examples

zzzhhhlll commented 3 months ago

please see examples here: https://github.com/fairmath/polycircuit/tree/main/examples

Thank you, but I'm a bit slow. I want to use this to compare encrypted texts. How can I achieve this? Is there any interface or function I can call to use it quickly?

alexandra-mara commented 3 months ago

Hello! Have you tried running signum.cc from examples? https://github.com/fairmath/polycircuit/tree/main/examples/signum_usage In a nutshell, this example sets up command-line options to specify locations for the cryptographic context, input ciphertext, and output ciphertext. It deserializes the cryptographic context and input ciphertext from files, applies the Signum function to the ciphertext, and then serializes the result back to a file.

g-arakelov commented 3 months ago

@zzzhhhlll you can implement comparison function using SIGN component. Just use sign(a - b).

zzzhhhlll commented 3 months ago

Hello! Have you tried running signum.cc from examples? https://github.com/fairmath/polycircuit/tree/main/examples/signum_usage In a nutshell, this example sets up command-line options to specify locations for the cryptographic context, input ciphertext, and output ciphertext. It deserializes the cryptographic context and input ciphertext from files, applies the Signum function to the ciphertext, and then serializes the result back to a file.

ok,I would like to ask what the purpose of the move function inside is, and why it is used

terminate called after throwing an instance of 'lbcrypto::OpenFHEException' what(): /home/zhl/download/openfhe-development-main/openfhe-development-main/src/core/include/lattice/hal/default/dcrtpoly-impl.h:l.698:DropLastElement(): DropLastElement: Removing last element of DCRTPoly renders it inlid.

zzzhhhlll commented 3 months ago

Hello! Have you tried running signum.cc from examples? https://github.com/fairmath/polycircuit/tree/main/examples/signum_usage In a nutshell, this example sets up command-line options to specify locations for the cryptographic context, input ciphertext, and output ciphertext. It deserializes the cryptographic context and input ciphertext from files, applies the Signum function to the ciphertext, and then serializes the result back to a file.

ok,I would like to ask what the purpose of the move function inside is, and why it is used The following error occurs when executing a function with the move function:

terminate called after throwing an instance of 'lbcrypto::OpenFHEException' what(): /home/zhl/download/openfhe-development-main/openfhe-development-main/src/core/include/lattice/hal/default/dcrtpoly-impl.h:l.698:DropLastElement(): DropLastElement: Removing last element of DCRTPoly renders it inlid.

I don't want to use any other parameters, I just want to use this P to get the output result P:polycircuit::Signum(std::move(cc), std::move(c1)).evaluate() Is the function of p a sign function approximation?

zzzhhhlll commented 3 months ago

Hello! Have you tried running signum.cc from examples? https://github.com/fairmath/polycircuit/tree/main/examples/signum_usage In a nutshell, this example sets up command-line options to specify locations for the cryptographic context, input ciphertext, and output ciphertext. It deserializes the cryptographic context and input ciphertext from files, applies the Signum function to the ciphertext, and then serializes the result back to a file.

ok,I would like to ask what the purpose of the move function inside is, and why it is used The following error occurs when executing a function with the move function:

terminate called after throwing an instance of 'lbcrypto::OpenFHEException' what(): /home/zhl/download/openfhe-development-main/openfhe-development-main/src/core/include/lattice/hal/default/dcrtpoly-impl.h:l.698:DropLastElement(): DropLastElement: Removing last element of DCRTPoly renders it inlid.

I don't want to use any other parameters, I just want to use this P to get the output result P:polycircuit::Signumlbcrypto::DCRTPoly(std::move(cc), std::move(c1)).evaluate() Is the function of p a sign function approximation?

I succeeded, but I have some other issues. How can I control this precision, and does the content of the vector I input have to be between -1 and 1? If the content of my vector is something like [25, -100, 1000], how should I adjust other parameters, such as multiplication depth, modulus, etc.?

alexandra-mara commented 3 months ago

Let me check and get back to you