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

Bug: multiply_plain[_inplace] - incorrect product if the plaintext holds negative encoded values #166

Closed
s0l0ist opened this issue Apr 30, 2020 · 1 comment

Comments

@s0l0ist
Copy link
Contributor

s0l0ist commented Apr 30, 2020

Hello all,

When using evaluator::multiply_plain[_inplace], the decrypted output is not correct when passing in a Plaintext created by encoding a signed vector (vector<int64>) holding negative values.

Note: I have not tested this for gsl::span, but it should be affected.

Example C++ code:

int main()
{
    EncryptionParameters parms(scheme_type::BFV);
    size_t poly_modulus_degree = 4096;
    parms.set_poly_modulus_degree(poly_modulus_degree);
    parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
    parms.set_plain_modulus(786433);
    auto context = SEALContext::Create(parms);
    KeyGenerator keygen(context);
    PublicKey public_key = keygen.public_key();
    SecretKey secret_key = keygen.secret_key();
    BatchEncoder batch_encoder(context);
    Encryptor encryptor(context, public_key);
    Evaluator evaluator(context);
    Decryptor decryptor(context, secret_key);

    size_t slot_count = batch_encoder.slot_count();
    vector<int64_t> vect(slot_count, -3); // Filling with *negative* values

    Plaintext plain;
    batch_encoder.encode(vect, plain);

    Ciphertext cipher;
    encryptor.encrypt(plain, cipher);

    // Successful: multiplying ciphers together results in the proper output
    // evaluator.multiply_inplace(cipher, cipher); // Output [9,9,9,...]

    // Silent fail: produces an incorrect decryption (same with multiply_plain).
    evaluator.multiply_plain_inplace(cipher, plain); // Output [-258029, -233196, 291981,...]

    Plaintext plain_result;
    decryptor.decrypt(cipher, plain_result);

    vector<int64_t> vect_result;
    batch_encoder.decode(plain_result, vect_result);

    for (auto const& i: vect_result) {
        std::cout << i << ", " << std::endl;
    }
    return 0;
}

Build settings:

cmake \
  -DSEAL_USE_CXX17=ON \
  -DSEAL_USE_INTRIN=OFF \
  -DSEAL_USE_ZLIB=ON \
  -DSEAL_USE_MSGSL=ON \
  -DSEAL_BUILD_EXAMPLES=OFF \
  -DSEAL_BUILD_TESTS=OFF \
  -DBUILD_SHARED_LIBS=OFF \
  -DCMAKE_BUILD_TYPE=Release \
  .
s0l0ist pushed a commit to s0l0ist/node-seal that referenced this issue Apr 30, 2020
Discovered issue:
microsoft/SEAL#166

Waiting for fix. Will update tests accordingly.

TODO: Update tests for symmetric encryption
@WeiDaiWD
Copy link
Contributor

It's fixed. v3.5.1 is coming really soon. Thanks a lot for capturing this. Our unit tests should certainly be improved.

s0l0ist added a commit to s0l0ist/node-seal that referenced this issue Jul 1, 2022
Discovered issue:
microsoft/SEAL#166

Waiting for fix. Will update tests accordingly.

TODO: Update tests for symmetric encryption
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

2 participants