SEAL库 例5之rotation解析

SEAL/native/examples/5_rotation.cpp

BFV方案(使用BatchEncoder)和CKKS方案都支持对加密数字进行native计算。除了slot-wise计算外,还可以循环地旋转加密向量。

一、 void example_rotation_bfv()

{ //设置基本参数

    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将矩阵编码成明文。我们像往常一样加密明文。

Plaintext plain_matrix;
print_line(__LINE__);
cout << "Encode and encrypt." << endl;
batch_encoder.encode(pod_matrix, plain_matrix);
Ciphertext encrypted_matrix;
encryptor.encrypt(plain_matrix, encrypted_matrix);//加密结果放在encrypted_matrix
cout << "    + Noise budget in fresh encryption: "
    << decryptor.invariant_noise_budget(encrypted_matrix) << " bits" << endl;
cout << endl;

旋转需要另一种特殊的键,叫做“伽罗瓦键 Galois keys”。这些很容易从密钥生成器获得。

GaloisKeys gal_keys = keygen.galois_keys();

现在将两个矩阵行向左旋转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);

// We can also rotate the columns, i.e., swap the rows.

print_line(__LINE__);
cout << "Rotate columns." << endl;
evaluator.rotate_columns_inplace(encrypted_matrix, 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);
Finally, we rotate the rows 4 steps to the right, decrypt, decode, and print.
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具有任何特定的大小,因此确保这种情况是由用户来做的。

二 、 void example_rotation_ckks()

{

 print_example_banner("Example: Rotation / Rotation in CKKS");

Rotations in the CKKS scheme work very similarly to rotations in BFV.

    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_共轭来计算加密复数向量上的复共轭。这其实是一种旋转,也需要伽罗瓦键。

void example_rotation()
{
    print_example_banner("Example: Rotation");

    /*
    Run all rotation examples.
    */
    example_rotation_bfv();
    example_rotation_ckks();
}

三、运行结果

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值