Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use signum? #9

Open
zzzhhhlll opened this issue Jun 14, 2024 · 8 comments
Open

How to use signum? #9

zzzhhhlll opened this issue Jun 14, 2024 · 8 comments
Assignees

Comments

@zzzhhhlll
Copy link

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
Copy link
Contributor

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

@zzzhhhlll
Copy link
Author

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 alexandra-mara self-assigned this Jul 2, 2024
@alexandra-mara
Copy link
Contributor

alexandra-mara commented Jul 2, 2024

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
Copy link
Contributor

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

@zzzhhhlll
Copy link
Author

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
Copy link
Author

zzzhhhlll commented Jul 3, 2024

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?

@zzzhhhlll
Copy link
Author

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
Copy link
Contributor

Let me check and get back to you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants