我对2个双精度值进行了加密,对乘法进行了循环,发现第三次乘法的结果错误,并且第四次乘法被中止。我的代码是否存在问题,或者印章不支持多次乘法?
void testMul()
{
EncryptionParameters parms(scheme_type::CKKS);
parms.set_poly_modulus_degree(8192);
parms.set_coeff_modulus({DefaultParams::small_mods_40bit(0), DefaultParams::small_mods_40bit(1),DefaultParams::small_mods_40bit(2), DefaultParams::small_mods_40bit(3) });
auto context = SEALContext::Create(parms);
KeyGenerator keygen(context);
auto public_key = keygen.public_key();
auto secret_key = keygen.secret_key();
auto relin_keys = keygen.relin_keys(DefaultParams::dbc_max());
Encryptor encryptor(context, public_key);
Evaluator evaluator(context);
Decryptor decryptor(context, secret_key);
CKKSEncoder encoder(context);
auto scale = static_cast<double>(parms.coeff_modulus().back().value());
double d1 = 3, d2 = 2, dResult, dTemp;
dResult = d2;
Plaintext plain_coeff1, plain_coeff2,plain_result;
encoder.encode(d1, scale, plain_coeff1);
encoder.encode(d2, scale, plain_coeff2);
Ciphertext encrypted_x1, encrypted_x2;
encryptor.encrypt(plain_coeff1, encrypted_x1);
encryptor.encrypt(plain_coeff2, encrypted_x2);
for (int i = 0; i < 10; i++)
{
dTemp = dResult;
encrypted_x2.scale() = encrypted_x1.scale();
evaluator.mod_switch_to_inplace(encrypted_x1, encrypted_x2.parms_id());
evaluator.multiply_inplace(encrypted_x2, encrypted_x1);
evaluator.relinearize_inplace(encrypted_x2, relin_keys);
evaluator.rescale_to_next_inplace(encrypted_x2);
decryptor.decrypt(encrypted_x2, plain_result);
vector<double> result;
encoder.decode(plain_result, result);
dResult = result[0];
cout << dTemp << " * " << d1 << " =" << dResult << endl;
}
}
output
{
2 * 3 =6
6 * 3 =18
18 * 3 =-0.000702552
terminate called after throwing an instance of 'std::invalid_argument'
what(): scale out of bounds
Aborted (core dumped)
}