Skip to content

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

@s0l0ist

Description

@s0l0ist

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 \
  .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions