TenSEAL学习笔记一

引言

发现网上没有什么TenSEAL的学习笔记,只能看官方文档,所以打算根据官方文档和自己的一些理解写一下TenSEAL的学习笔记。

TenSEAL简介

Tutorial 0: Getting Started
Welcome to TenSEAL’s first tutorial of a serie aiming at introducinghomomorphic encryption and the capabilities of the library.
TenSEAL is a library for doing homomorphic encryption operations on tensors. It’s built on top of MicrosoftSEAL, a C++ library implementing the BFV and CKKS homomorphic encryption schemes. TenSEAL provides ease of use through a Python API, while preserving efficiency by implementing most of its operations using C++, so TenSEAL is a C++ library with a Python interface.

Let’s now start the tutorial with a brief review of what homomorphic encryption is, but keep in mind that you don’t need to be a crypto expert to use the library.

Authors:

Homomorphic Encryption

在这里插入图片描述基本思想如下

x = 7
y = 3

x_encrypted = HE.encrypt(x)
y_encrypted = HE.encrypt(y)

z_encrypted = x_encrypted + y_encrypted

# z should now be x + y = 10
z = HE.decrypt(z_encrypted)


TenSEALContext

这个类是TenSEAL的最基础的类,封装了很多的encryption keys和parameters,在实际使用中,我们只需要将加密的需求传入这个类,就可以实现HE sheme的初始化。

import tenseal as ts

context = ts.context(ts.SCHEME_TYPE.BFV, poly_modulus_degree=4096, plain_modulus=1032193)
print(context)
#输出:<tenseal.enc_context.Context object at 0x7f380e2872b0>

在 TenSEALContext初始化完成后,会默认生成秘钥,但是也可以自己指定,这里为了方便学习,暂时先用他生成的秘钥。

import tenseal as ts

public_context = ts.context(ts.SCHEME_TYPE.BFV, poly_modulus_degree=4096, plain_modulus=1032193)
print("Is the context private?", ("Yes" if public_context.is_private() else "No"))
print("Is the context public?", ("Yes" if public_context.is_public() else "No"))

sk = public_context.secret_key()
print(sk)

# the context will drop the secret-key at this point
public_context.make_context_public()
print("Secret-key dropped")
print("Is the context private?", ("Yes" if public_context.is_private() else "No"))
print("Is the context public?", ("Yes" if public_context.is_public() else "No"))

在这里插入图片描述
可以看到,当创建TenSEALContext类时,默认产生了一个私有的秘钥,当秘钥被删除时,这个sheme就变成了共有的sheme。
当在print(“Secret-key dropped”)后执行
sk = public_context.secret_key()
print(sk)
时,会报错
在这里插入图片描述

Encryption and Evaluation

创建一个整数加密向量进行加密

import tenseal as ts

context = ts.context(ts.SCHEME_TYPE.BFV, poly_modulus_degree=4096, plain_modulus=1032193)

#创建加密vector
plain_vector = [60, 66, 73, 81, 90]
encrypted_vector = ts.bfv_vector(context, plain_vector)
print("We just encrypted our plaintext vector of size:", encrypted_vector.size())
print(encrypted_vector)

输出如下
在这里插入图片描述
创建两个BFVVector并进行加运算

import tenseal as ts

context = ts.context(ts.SCHEME_TYPE.BFV, poly_modulus_degree=4096, plain_modulus=1032193)

#创建加密vector
plain_vector = [60, 66, 73, 81, 90]
encrypted_vector = ts.bfv_vector(context, plain_vector)
print("We just encrypted our plaintext vector of size:", encrypted_vector.size())
print(encrypted_vector)

plain2_vector = [1, 2, 3, 4, 5]
encrypted2_vector = ts.bfv_vector(context, plain_vector)
#可以对两个vector做+,-,*运算
add_result = encrypted_vector + encrypted2_vector
print("[60, 66, 73, 81, 90] + [1, 2, 3, 4, 5]",add_result.decrypt())
sub_result = encrypted_vector - encrypted2_vector
print("[60, 66, 73, 81, 90] - [1, 2, 3, 4, 5]",sub_result.decrypt())
mul_result = encrypted_vector * encrypted2_vector
print("[60, 66, 73, 81, 90] * [1, 2, 3, 4, 5]",mul_result.decrypt())

运算结果如下
在这里插入图片描述
对ciphertext ⊙ plaintext(cp)和ciphertext ⊙ ciphertext(cc)运算做时间上的评估,发现时间差异巨大,cp的时间远远短与cc的时间。

from time import time

t_start = time()
_ = encrypted_add * encrypted_mul
t_end = time()
print("c2c multiply time: {} ms".format((t_end - t_start) * 1000))

t_start = time()
_ = encrypted_add * [1, 2, 3, 4, 5]
t_end = time()
print("c2p multiply time: {} ms".format((t_end - t_start) * 1000))

#c2c multiply time: 4.381418228149414 ms
#c2p multiply time: 0.5409717559814453 ms

在CKKS下,也可以使用global_scale对矩阵进行操作。

# this should throw an error as the global_scale isn't defined yet
try:
    print("global_scale:", context.global_scale)
except ValueError:
    print("The global_scale isn't defined yet")
    
# you can define it to 2 ** 20 for instance
context.global_scale = 2 ** 20
print("global_scale:", context.global_scale)

#输出
#The global_scale isn't defined yet
#global_scale: 1048576.0
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值