TenSEAL学习笔记一

Posted 一只特立独行的猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于TenSEAL学习笔记一的主要内容,如果未能解决你的问题,请参考以下文章

TenSEAL学习笔记二

TenSEAL学习笔记二

同态加密:CKKS方案简介及一个python实现:TENSEAL(不定期更新)

一大波ThreeJS学习笔记来啦

NumPy学习笔记 一

linux学习笔记一