小结

1.EncryptionParameters类

  • set_poly_modulus_degree():设置多项式模数
  • set_coeff_modulus():设置密文模数
  • set_plain_modulus():设置明文模数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//BFV
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(1024);
//parms.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 20));

//CKKS
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,{60,40,40,60}));

double scale = pow(2.0,40);

创建context

1
2
3
4
5
6
SEALContext context(parms);

/*
判断Encoder标志是否开启
*/
auto qualifiers = context.first_context_data()->qualifiers();

2.KeyGenerator类

  • secret_key():生成私钥
  • create_public_key():生成公钥
  • create_relin_keys():生成重新线性化密钥
  • create_galois_keys():生成galois密钥
1
2
3
4
5
6
7
8
9
KeyGenerator keygen(context);

auto secret_key = keygen.secret_key();
PublicKey public_key;
keygen.create_public_key(public_key);
RelinKeys relin_keys;
keygen.create_relin_keys(relin_keys);
GaloisKeys gal_keys;
keygen.create_galois_keys(gal_keys);

3.BatchEncoder/CKKSEncoder类

(1)BatchEncoder

  • slot_count()
  • encode():编码
  • decode():解码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
BatchEncoder batch_encoder(context);

size_t slot_count = batch_encoder.slot_count();
size_t row_size = slot_count / 2;

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;

Plaintext plain_matrix;
batch_encoder.encode(pod_matrix, plain_matrix);

vector<uint64_t> pod_result;
batch_encoder.decode(plain_matrix, pod_result);

(2)CKKSEncoder

1
2
3
4
5
6
7
8
9
10
11
CKKSEncoder encoder(context);

size_t slot_count = encoder.slot_count();
vector<double> input{ 0.0, 1.1, 2.2, 3.3 };

Plaintext plain;
double scale = pow(2.0, 30);
encoder.encode(input, scale, plain);

vector<double> output;
encoder.decode(plain, output);

4.Encryptor/Evaluator/Decryptor类

1
2
3
Encryptor encryptor(context,public_key);
Evaluator evaluator(context);
Decryptor decryptor(context,secret_key);

(1)Encryptor

  • encrypt()
1
encryptor.encrypt(plain, encrypted);

(2)Evaluator

  • square_inplace():平方
  • square()
  • relinearize_inplace():重新线性化
  • rescale_to_next_inplace():rescale
  • mod_switch_to_next_inplace():切换到下一个level
1
2
3
4
5
6
7
evaluator.square_inplace(encrypted);
evaluator.relinearize_inplace(encrypted, relin_keys);

evaluator.square(x1_encrypted, x3_encrypted);
evaluator.relinearize_inplace(x3_encrypted, relin_keys);

evaluator.mod_switch_to_next_inplace(encrypted);

(3)Decryptor

  • decrypt()
  • invariant_noise_budget():可用噪声预算
1
2
3
decryptor.decrypt(encrypted, plain);

decryptor.invariant_noise_budget(encrypted)

5.save()/load()

使用save()函数将密文存入指定文件;
使用load()函数从指定文件载入密文。

(密钥同理)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void saveCiphertext(Ciphertext encrypted, string filename){
ofstream ct;
ct.open(filename, ios::binary);
encrypted.save(ct);
};

/*
If you can't / don't want / don't need to verify the encryption parameters
*/
Ciphertext unsafe_loadCiphertext(string filename){

ifstream ct;
ct.open(filename, ios::binary);
Ciphertext result;
result.unsafe_load(context);

return result;
};

/*
Verifying encryption parameters
*/
Ciphertext loadCiphertext(string filename, EncryptionParameters parms){

auto context = SEALContext::Create(parms);

ifstream ct;
ct.open(filename, ios::binary);
Ciphertext result;
result.load(context, ct);

return result;
};

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!