Windows10、VS2019平台下安装Microsoft SEAL 3.6.2
(SEAL 3.6之后的版本需要使用CMake安装,注意安装VS2019时选择CMake tool依赖项)
具体CMake、VS2019安装可参考(125条消息) 使用Visual Studio在Windows下安装SEAL3.6,并使用cmake写一个小例子_AdijeShen的博客-CSDN博客
从GitHub上下载SEAL或使用git clone
git clone https://github.com/microsoft/SEAL.git
安装SEAL
- cmd 安装
以管理员身份打开cmd,转到SEAL安装目录
输入
cmake -S . -B build
然后
cmake --build build
cmake --install build
- 从VS2019命令行工具安装
选择x64 Native Tools Command Prompt for VS 2019,转到SEAL安装目录
依次输入
cmake -S . -B build -G "Visual Studio 16 2019" -A x64
cmake --build build --config Release
cmake --install build
无报错,安装结束
此时在\SEAL\build\cmake中会生成SEALConfig.cmake等文件。
新建项目并配置(两种方式)
- CMake项目
从VS2019创建一个CMake项目,命名为helloSEAL
此时helloSEAL文件夹下会自动生成CMakeList.txt、helloSEAL.cpp等文件
注意切换配置为X64 Release,与SEAL构建时一致
在CMakeList.txt中写
cmake_minimum_required (VERSION 3.8)
project ("helloSEAL")
#告诉CMake那里去找你的SEAL库
set(E:\\SEAL-3.6.2\\build\\cmake)
#去找SEAL库
find_package(SEAL 3.6.2 REQUIRED)
# 将源代码添加到此项目的可执行文件。
add_executable (helloSEAL "helloSEAL.cpp" "helloSEAL.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;
}
保存CMakeList.txt后会自动生成,点击上方运行当前文档helloSEAL.cpp
大功告成!
- C++空项目
从VS2019中新建一个C++空项目
打开项目——属性,同样选择Release-X64配置
1、在C/C++—常规—附加包含目录中添加
.\native\src 和 .\build\native\src
2、在链接器—常规—附加库目录中添加
.\build\lib\Release
3、在链接器—输入—附加依赖项中添加
seal-3.6.lib
以上配置完成后,新添加cpp文件
同上,输入测试代码,上方运行后显示
大功告成!