2021SC@SDUSC-SEAL全同态加密库(十一)

SEAL全同态加密库(十一)

rotation

    print_example_banner("Example: Rotation / Rotation in BFV")
    EncryptionParameters parms(scheme_type::BFV);
    size_t poly_modulus_degree = 8192;
    parms.set_poly_modulus_degree(poly_modulus_degree);
    parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
    parms.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 20));

    auto context = SEALContext::Create(parms);
    print_parameters(context);
    cout << endl;

    KeyGenerator keygen(context);
    PublicKey public_key = keygen.public_key();
    SecretKey secret_key = keygen.secret_key();
    RelinKeys relin_keys = keygen.relin_keys();
    Encryptor encryptor(context, public_key);
    Evaluator evaluator(context);
    Decryptor decryptor(context, secret_key);

BatchEncoder batch_encoder(context);
size_t slot_count = batch_encoder.slot_count();
size_t row_size = slot_count / 2;
cout << "Plaintext matrix row size: " << row_size << endl;

vector<uint64_t> pod_matrix(slot_count, 0ULL);
pod_matrix[0] = 0ULL;
pod_matrix[1] = 1ULL;
pod_matrix[2] = 2ULL;
pod_matrix[3] = 3ULL;
pod_matrix[row_size] = 4ULL;
pod_matrix[row_size + 1] = 5ULL;
pod_matrix[row_size + 2] = 6ULL;
pod_matrix[row_size + 3] = 7ULL;

cout << "Input plaintext matrix:" << endl;
print_matrix(pod_matrix, row_size);

首先,我们使用BatchEncoder将矩阵编码成明文。我们像往常一样加密明文。
现在将两个矩阵行向左旋转3步,解密、解码和打印。

print_line(__LINE__);
cout << "Rotate rows 3 steps left." << endl;
evaluator.rotate_rows_inplace(encrypted_matrix, 3, gal_keys);  //利用gal_keys 旋转矩阵:左移三个
Plaintext plain_result;
cout << "    + Noise budget after rotation: "     //旋转之后看看噪声预算
    << decryptor.invariant_noise_budget(encrypted_matrix) << " bits" << endl;
cout << "    + Decrypt and decode ...... Correct." << endl;
decryptor.decrypt(encrypted_matrix, plain_result);     //解密
batch_encoder.decode(plain_result, pod_matrix);   //加密 pod_matrix
print_matrix(pod_matrix, row_size);

print_line(__LINE__);
cout << "Rotate rows 4 steps right." << endl;
evaluator.rotate_rows_inplace(encrypted_matrix, -4, gal_keys);
cout << "    + Noise budget after rotation: "
    << decryptor.invariant_noise_budget(encrypted_matrix) << " bits" << endl;
cout << "    + Decrypt and decode ...... Correct." << endl;
decryptor.decrypt(encrypted_matrix, plain_result);
batch_encoder.decode(plain_result, pod_matrix);
print_matrix(pod_matrix, row_size);

注意,旋转不消耗任何噪音预算。然而,只有当特殊素数至少与其他素数一样大时,才会出现这种情况。relinearization也是如此。Microsoft SEAL不要求特殊的prime具有任何特定的大小,因此确保这种情况是由用户来做的。

    EncryptionParameters parms(scheme_type::CKKS);

    size_t poly_modulus_degree = 8192;
    parms.set_poly_modulus_degree(poly_modulus_degree);
    parms.set_coeff_modulus(CoeffModulus::Create(
        poly_modulus_degree, {
    
     40, 40, 40, 40, 40 }));

    auto context = SEALContext::Create(parms);
    print_parameters(context);
    cout << endl;

    KeyGenerator keygen(context);
    PublicKey public_key = keygen.public_key();
    SecretKey secret_key = keygen.secret_key();
    RelinKeys relin_keys = keygen.relin_keys();
    GaloisKeys gal_keys = keygen.galois_keys();
    Encryptor encryptor(context, public_key);
    Evaluator evaluator(context);
    Decryptor decryptor(context, secret_key);
    
    CKKSEncoder ckks_encoder(context);

size_t slot_count = ckks_encoder.slot_count();
cout << "Number of slots: " << slot_count << endl;
vector<double> input;
input.reserve(slot_count); //input reserve
double curr_point = 0;
double step_size = 1.0 / (static_cast<double>(slot_count) - 1);
for (size_t i = 0; i < slot_count; i++, curr_point += step_size)
{
    
    
    input.push_back(curr_point);
}
cout << "Input vector:" << endl;
print_vector(input, 3, 7);

auto scale = pow(2.0, 50);

print_line(__LINE__);
cout << "Encode and encrypt." << endl;
Plaintext plain;
ckks_encoder.encode(input, scale, plain);  //编码成plain
Ciphertext encrypted;
encryptor.encrypt(plain, encrypted);      //加密成encrypted

Ciphertext rotated;
print_line(__LINE__);
cout << "Rotate 2 steps left." << endl;
evaluator.rotate_vector(encrypted, 2, gal_keys, rotated);  //做旋转,左移两位
cout << "    + Decrypt and decode ...... Correct." << endl;
decryptor.decrypt(rotated, plain);
vector<double> result;
ckks_encoder.decode(plain, result);
print_vector(result, 3, 7);

使用CKKS方案,还可以使用Evaluator:: complex_共轭来计算加密复数向量上的复共轭。这其实是一种旋转,也需要伽罗瓦键。

猜你喜欢

转载自blog.csdn.net/weixin_46021936/article/details/122520869