primihub / hehub

HEhub is a library for homomorphic encryption and its applications, and is part of the PrimiHub project.
Apache License 2.0
88 stars 18 forks source link

密钥问题与乘法同态问题 #9

Closed zlzhao998 closed 11 months ago

zlzhao998 commented 1 year ago

问题1 密钥是用的对称密钥吗?我看加密和解密都是同一个密钥。 问题2 乘法同态计算错误,代码如下:

int precision_bits = 30;
auto params = ckks::create_params(4096, precision_bits);
std::cout << "(params:" << params << std::endl;
CkksSk sk(params);
auto relin_key = get_relin_key(sk, params.additional_mod);

auto text11 = ckks::encode(text1 , params);
auto textC1 = ckks::encrypt(text11, sk);

auto text22 = ckks::encode(text2 , params);
auto textC2 = ckks::encrypt(text22, sk);

auto text2_sub_c = ckks::sub(textC1, textC2);
double text2_sub = ckks::decode(ckks::decrypt(text2_sub_c, sk));
std::cout << "text1 - text2:" << text2_sub << std::endl;

CkksCt text_sum_c = ckks::add(textC1,textC2);
double text_sum = ckks::decode(ckks::decrypt(text_sum_c, sk));
std::cout << "text1 + text2:" << text_sum << std::endl;

CkksCt text_mult_c = ckks::mult_plain(textC1,text22);
double text_mult = ckks::decode(ckks::decrypt(text_mult_c, sk));
std::cout << "text1 * text2:" << text_mult << std::endl;

入参: text1 = 100,text2=300 结果: text1 - text2:-200 text1 + text2:400 text1 * text2:-200.531

cryptolrz commented 1 year ago

@zlzhao998 对于第一个问题,是的,目前仅实现了对称加密,暂时没有实现公钥加密的功能。 对于第二个问题,请使用如下的代码逻辑完成上述的功能。

std::vector<double> text1(1);​
std::vector<double> text2(1);​
text1[0] = 100.0;​
text2[0] = 300.0;​
​
auto text11 = ckks::simd_encode(text1 , params);​
auto textC1 = ckks::encrypt(text11, sk);​
​
auto text22 = ckks::simd_encode(text2 , params);​
auto textC2 = ckks::encrypt(text22, sk);​
​
CkksCt text_mult_c = ckks::mult_plain(textC1,text22);​
auto text_mult = ckks::simd_decode(ckks::decrypt(text_mult_c, sk));​
std::cout << "text1 * text2:" << text_mult[0] << std::endl;

使用encode和decode,对于一些稍大一些的数值,容易造成溢出。