使用Visual Studio在Windows下安装SEAL3.6,并使用cmake写一个小例子

内容已更新,我在虚拟机上运行成功

依赖项: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;
}

点击运行,大功告成。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值