Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bgv方案乘法同态问题 #11

Open
ch0lesk1 opened this issue Jan 22, 2024 · 1 comment
Open

bgv方案乘法同态问题 #11

ch0lesk1 opened this issue Jan 22, 2024 · 1 comment

Comments

@ch0lesk1
Copy link

ch0lesk1 commented Jan 22, 2024

bgv方案计算密文乘法出现错误,计算密文状态下的0*1,结果为28733。代码如下:

#include "fhe/bgv/bgv.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <bitset>

using namespace hehub;


int main() {
    // 设置BGV加密环境
    std::vector<u64> ct_moduli{131530753, 130809857}; // 示例模数
    size_t dimension = 1;
    RnsPolyParams ct_params{dimension, ct_moduli.size(), ct_moduli};
    RlweSk sk(ct_params);

    u64 pt_modulus = 65537; // 示例明文模数
    RnsPolyParams pt_params{dimension, 1, std::vector{pt_modulus}};
    
    // 生成重线性化密钥
    u64 additional_mod = 131923969; // 额外模数
    auto relin_key = get_relin_key(sk, additional_mod);

    auto data_count = dimension;
    std::vector<u64> plain_data1(data_count);
    std::vector<u64> plain_data2(data_count);
    u64 seed = 1;
    for (auto &d : plain_data1) {
        d = (seed++) % 2;
    }
    for (auto &d : plain_data2) {
        d = (seed++) % 2;
    }
    for (int i = 0; i < dimension; i++) {
        std::cout<<"plain_data1:"<<plain_data1[i]<<" ";
    }
    std::cout<<std::endl;
     for (int i = 0; i < dimension; i++) {
        std::cout<<"plain_data2:"<<plain_data2[i]<<" ";
    }
    std::cout<<std::endl;
    
    // encode
    auto pt1 = bgv::simd_encode(plain_data1, pt_modulus);
    auto pt2 = bgv::simd_encode(plain_data2, pt_modulus);

    // encrypt & arith
    auto ct1 = bgv::encrypt(pt1, sk);
    auto ct2 = bgv::encrypt(pt2, sk);
    auto ct_prod_quadratic = bgv::mult_low_level(ct1, ct2);
    auto ct_prod = bgv::relinearize(ct_prod_quadratic, relin_key);

    // decrypt
    auto prod_decrypted = bgv::decrypt(ct_prod, sk);
    auto prod_data = bgv::simd_decode(prod_decrypted);

    // check
    for (size_t i = 0; i < data_count; i++) {
        std::cout<<"prod_data"<<i+1<<":"<<prod_data[i]<<std::endl;
    }

    return 0;
}

下面是运行结果:

plain_data1:1
plain_data2:0
prod_data1:28733

因为我需要在同态加密的情况下计算乘法和加法,所以我选择了bgv加密方案。我不清楚是不是我的代码有问题。感谢!

@ppppbamzy
Copy link
Contributor

我检查一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants