关键词:
文章目录
引言
发现网上没有什么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:
- Ayoub Benaissa - Twitter: @y0uben11
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学习笔记二(代码片段)
文章目录TrainingandEvaluationofLogisticRegressiononEncryptedDataDownloadthecoronaryheartdiseasedatasetInitializedatasetTrainingaLogisticRegressionModelEncryptedEvaluationTraininganEncryptedLogisticRegress 查看详情
tenseal学习笔记二(代码片段)
文章目录TrainingandEvaluationofLogisticRegressiononEncryptedDataDownloadthecoronaryheartdiseasedatasetInitializedatasetTrainingaLogisticRegressionModelEncryptedEvaluationTraininganEncryptedLogisticRegressionModelonEncryptedDataTrainingandEvaluationofLogisticRegressiononEncryptedDataDownloadtheco... 查看详情
linux学习笔记一(代码片段)
linux学习笔记一文章目录linux学习笔记一Linuxpackageoperationoflookingfilesoperationhelpcommandsowncommandsechotunnelechoagainuserorrootprocessingaliasagainfinding这个是我在学习linux系统的时候的一点的小小的总结,希望对大家有一定的在帮助啦。Linux 查看详情
veriloghdl学习笔记一(代码片段)
VerilogHDL学习笔记一文章目录VerilogHDL学习笔记一一、简介二、第一个案例三、环境的配置四、其他知识一、简介VerilogHDL是一种硬件描述语言二、第一个案例这个只是一个展示了modulecounter10(//端口定义inputrstn,//复位端,低有效i... 查看详情
veriloghdl学习笔记一(代码片段)
VerilogHDL学习笔记一文章目录VerilogHDL学习笔记一一、简介二、第一个案例三、环境的配置四、其他知识一、简介VerilogHDL是一种硬件描述语言二、第一个案例这个只是一个展示了modulecounter10(//端口定义inputrstn,//复位端,低有效i... 查看详情
repuest转发学习笔记一(代码片段)
学习图:Java代码importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletR 查看详情
tensorflow学习笔记一(代码片段)
1、tensorflow中的数据类型importtensorflowastfimportnumpyasnp#张量可以是数字、列表、ndarray#使用列表创建张量print(tf.constant([2,3]))print(tf.constant([[2,3],[1,4]]))#tensorflow2默认使用Eager动态图机制print(type(tf.constant([[2,3],[1,4] 查看详情
jquery学习笔记详解--(一)(代码片段)
概念 jQuery是一个JavaScript函数库。jQuery是一个轻量级的"写的少,做的多"的JavaScript库。 就是一堆JavaScript代码 jQuery库包含以下功能: HTML元素选取 HTML元素操作 ... 查看详情
slurm学习笔记(代码片段)
slurm学习笔记(一)官网: https://slurm.schedmd.com/中文文档:https://docs.slurm.cn/users/shou-ce-ye学习笔记二:Slurm学习笔记(二)_种花家的奋斗兔的博客-CSDN博客一、slurm简介Slurm(SimpleL 查看详情
cmake学习笔记一(代码片段)
#设置cmake最低版本号CMAKE_MINIMUM_REQUIRED(VERSION3.4.0)#设置工程名称PROJECT(HelloWorld)#设置工程包含当前目录,非必须SET(CMAKE_INCLUDE_CURRENT_DIRON)#设置自动生成moc文件,AUTOMOC打开可以省去QT5_WRAP_CPP命令SET(CMAKE_AUTOMOCON)#设置自动生成ui.h文件,AU... 查看详情
java学习笔记(代码片段)
java笔记一、面向对象一、面向对象二、累和对象三、类的定义和使用四、构造方法五、this关键字六、方法重载七、static关键字二、继承和多态一、继承的含义以及用法二、方法的重写三、多态及其应用四、super关键字五、Object... 查看详情
es6学习笔记一(代码片段)
ECMAScript是JavaScript的标准,JavaScript是ECMAScript的实现。1.let: 为JS新增块级作用域。(1)let命令声明的变量是局部变量,仅在let所在代码块有效;(2)let声明的变量不存在变量提升;(3)块级作用域内let声明的变量,不受外部... 查看详情
flask学习笔记(一helloworld)(代码片段)
初始化fromflaskimportFlaskapp=Flask(__name__)当从此文件进入时__name__为__main__。Flask用这个参数确定程序的根目录,以便查找资源文件的位置。路由和视图@app.route('/')defindex():return'<h1>HelloWorld!</h1>' 查看详情
proteus学习笔记一:点亮led(代码片段)
最近想学习下C51单片机,懒得折腾硬件了,就用proteus软件学习下,把过程记录下,希望能够帮助到想学习C51的人吧。一、软件安装1)proteus8.13安装,请看这里:https://www.aliyundrive.com/s/9DRfHAQfo5G提取码:1d9... 查看详情
proteus学习笔记一:点亮led(代码片段)
最近想学习下C51单片机,懒得折腾硬件了,就用proteus软件学习下,把过程记录下,希望能够帮助到想学习C51的人吧。一、软件安装1)proteus8.13安装,请看这里:https://www.aliyundrive.com/s/9DRfHAQfo5G提取码:1d9... 查看详情
pandas学习笔记一:series(代码片段)
1、series基本概念importnumpyasnpimportpandasaspd#Series数据结构#Series是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象等),轴标签统称为索引ar=np.random.rand(10)print(ar,type(ar))ar 查看详情
人工智能学习笔记----03(代码片段)
人工智能学习笔记----03文章目录人工智能学习笔记----03一、数学知识1、函数以及映射2、python的numpy可以实现以上所述的数据结构3、向量空间4、统计与概率二、几种模型一、数学知识1、函数以及映射x->yfeature->label使用梯度... 查看详情
[oc学习笔记]对象消息运行期(代码片段)
学习目录一、理解“属性”这一概念属性特质:二、在对象内部尽量直接访问实例变量三、理解“对象等同性”这一概念(一)特定类所具有的等同性判定方法(二)等同性判定的执行深度(三)容器... 查看详情