内容已更新,我在虚拟机上运行成功
依赖项:cmake
先安装cmake https://cmake.org/download/
注意添加到PATH
下载SEAL库
git clone https://github.com/microsoft/SEAL.git
安装Visual Studio 2019
下载在Installer中勾选C++以及CMake依赖项
安装SEAL库
打开cmd,转到SEAL目录
cmake -S . -B build
中间遇到报错很有可能是因为github连接不上,多试几次,直到看到:
然后执行
cmake --build build
cmake --install build
此时在\SEAL\build\cmake中会生成SEALConfig.cmake等文件。
现在新建一个自己的CMake项目
比如命名为helloSEAL
在CMakeList.txt中写
cmake_minimum_required (VERSION 3.8)
project ("helloSEAL")
#告诉CMake那里去找你的SEAL库
set(SEAL_DIR <your path>\\SEAL\\build\\cmake)
#去找SEAL库
find_package(SEAL 3.6.2 REQUIRED)
# 将源代码添加到此项目的可执行文件。
add_executable (helloSEAL "SEALtry.cpp" "SEALtry.h")
# 将SEAL库链接到该项目
target_link_libraries(helloSEAL SEAL::seal)
# TODO: 如有需要,请添加测试并安装目标。
在helloSEAL.cpp中写
#include "helloSEAL.h"
#include"seal/seal.h"
using namespace std;
using namespace seal;
void print_line(int line_number)
{
std::cout << "Line " << line_number << " --> ";
}
int main()
{
print_line(__LINE__);
cout << "Set encryption parameters" << endl;
EncryptionParameters params(scheme_type::bfv);
size_t poly_modulus_degree = 4096;
params.set_poly_modulus_degree(poly_modulus_degree);
params.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
params.set_plain_modulus(1024);
SEALContext context(params);
KeyGenerator keygen(context);
SecretKey secret_key = keygen.secret_key();
PublicKey public_key;
keygen.create_public_key(public_key);
Encryptor encryptor(context, public_key);
Evaluator evaluator(context);
Decryptor decryptor(context, secret_key);
print_line(__LINE__);
int x = 6;
Plaintext x_plain(to_string(x));
cout << "Express x = " + to_string(x) + " as a plaintext polynomial 0x" + x_plain.to_string() + "." << endl;
print_line(__LINE__);
Ciphertext x_encrypted;
cout << "Encrypt x_plain to x_encrypted." << endl;
encryptor.encrypt(x_plain, x_encrypted);
cout << " + size of freshly encrypted x: " << x_encrypted.size() << endl;
cout << " + noise budget in freshly encrypted x: " << decryptor.invariant_noise_budget(x_encrypted) << " bits"
<< endl;
Plaintext x_decrypted;
cout << " + decryption of x_encrypted: ";
decryptor.decrypt(x_encrypted, x_decrypted);
cout << "0x" << x_decrypted.to_string() << " ...... Correct." << endl;
print_line(__LINE__);
cout << "Compute x_sq_plus_one (x^2+1)." << endl;
Ciphertext x_sq_plus_one;
evaluator.square(x_encrypted, x_sq_plus_one);
Plaintext plain_one("1");
evaluator.add_plain_inplace(x_sq_plus_one, plain_one);
cout << " + size of x_sq_plus_one: " << x_sq_plus_one.size() << endl;
cout << " + noise budget in x_sq_plus_one: " << decryptor.invariant_noise_budget(x_sq_plus_one) << " bits"
<< endl;
Plaintext decrypted_result;
cout << " + decryption of x_sq_plus_one: ";
decryptor.decrypt(x_sq_plus_one, decrypted_result);
cout << "0x" << decrypted_result.to_string() << " ...... Correct." << endl;
return 0;
}
点击运行,大功告成。