zama-concrete库 rust语言的同态加密库

zama-concrete库 rust语言的同态加密库

  • Zama-concrete
  • 1. 部署过程
    • 在windows上的wsl2的ubuntu部署rust
    • 链接vscode到wsl上,
    • 部署rust
    • 在cargo.toml加入包
  • 2. 初始化
    • 新建密钥
    • 加密
  • 3. 基于Boolean的TFHE
    • 代码(测试与/或/非/异或/不等/MUX门)
    • 运行结果
  • 4. 基于interger的TFHE
    • 整数操作
    • 代码(整数密文运算 模8运算+/-/*/%)
    • 代码(整数密文明文运算)
    • 代码(整数二进制运算,按位与/或/非/移位)
  • 5. 序列化传递TFHE
    • 代码(将公钥和密文序列化,发送到服务端运算)
    • 运行结果(两个TRUE的加密序列化结果不一样)
    • 密文和密钥大小

Zama-concrete

github链接
官方文档

1. 部署过程

在windows上的wsl2的ubuntu部署rust

curl  --tlsv1.2 -sSf https://sh.rustup.rs | sh
sudo apt install build-essential

链接vscode到wsl上,

zama-concrete库 rust语言的同态加密库_第1张图片

部署rust

cargo new greeting 
cd ./greeting 
cargo build --release
cargo run --release

在cargo.toml加入包

[package]
name = "greeting"
version = "0.1.0"
edition = "2021"
#See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
concrete = { version = "0.2.0-beta", features = ["booleans"]}

2. 初始化

新建密钥

//调库
use concrete::{FheBool, ConfigBuilder, generate_keys, set_server_key, if_then_else};
use concrete::prelude::*;

//建立密文环境
let config = ConfigBuilder::all_disabled()
        .enable_default_bool()
        .build();       
        
//生成密钥
let (client_key, server_key) = generate_keys(config);
//设置公钥
set_server_key(server_key);

加密

let pt_1 = true;
let pt_2 = true;

let ct_1 = FheBool::encrypt(pt_1, &client_key);
let ct_2 = FheBool::encrypt(pt_1, &client_key);

3. 基于Boolean的TFHE

代码(测试与/或/非/异或/不等/MUX门)

  • rust有一个缺点,变量不能重复用,所以用clone代替
use concrete::{FheBool, ConfigBuilder, generate_keys, set_server_key, if_then_else};
use concrete::prelude::*;

fn main() {
    let config = ConfigBuilder::all_disabled()
        .enable_default_bool()
        .build();
    let (client_key, server_key) = generate_keys(config);

    set_server_key(server_key);

    let pt_1 = true;
    let pt_2 = false;

    let ct_1 = FheBool::encrypt(pt_1, &client_key);
    let ct_2 = FheBool::encrypt(pt_2, &client_key);

    let ct_3 = !ct_2.clone();
    let ct_4 = ct_1.clone() & ct_3.clone();
    let ct_5 = ct_1.clone().nand(&ct_4);
    let ct_6 = ct_2.neq(&ct_1.clone());
    let ct_7 = ct_1.clone() | ct_2.clone();
    let ct_8 = ct_1.clone() ^ ct_2.clone();
    let ct_9 = if_then_else(ct_1.clone(),ct_2.clone(),ct_3.clone()); 
    // mux门在concrete2.0 改叫if_then_else

    println!("ct_1 = {}",ct_1.decrypt(&client_key));
    println!("ct_2 = {}",ct_2.decrypt(&client_key));
    println!("ct_3 = {}",ct_3.decrypt(&client_key));
    println!("ct_4 = {}",ct_4.decrypt(&client_key));
    println!("ct_5 = {}",ct_5.decrypt(&client_key));
    println!("ct_6 = {}",ct_6.decrypt(&client_key));
    println!("ct_7 = {}",ct_7.decrypt(&client_key));
    println!("ct_8 = {}",ct_8.decrypt(&client_key));
    println!("ct_9 = {}",ct_9.decrypt(&client_key));

}

运行结果

zama-concrete库 rust语言的同态加密库_第2张图片

4. 基于interger的TFHE

整数操作

zama-concrete库 rust语言的同态加密库_第3张图片
zama-concrete库 rust语言的同态加密库_第4张图片zama-concrete库 rust语言的同态加密库_第5张图片

代码(整数密文运算 模8运算+/-/*/%)

use concrete::prelude::*;
use concrete::{generate_keys, set_server_key, ConfigBuilder, FheUint3};

fn main() -> Result<(), Box> {
    let config = ConfigBuilder::all_disabled().enable_default_uint3().build();
    let (keys, server_keys) = generate_keys(config);
    set_server_key(server_keys);
    
    let clear_1 = 6u8;
    let clear_2 = 3u8;
    let clear_3 = 2u8;
    let clear_4 = 0u8;
    let clear_5 = 5u8;

    let ct1 = FheUint3::try_encrypt(clear_1, &keys)?; //加密
    let ct2 = FheUint3::try_encrypt(clear_2, &keys)?;
    let ct3 = FheUint3::try_encrypt(clear_3, &keys)?;
    let ct4 = FheUint3::try_encrypt(clear_4, &keys)?;
    let ct5 = FheUint3::try_encrypt(clear_5, &keys)?;

    let ct6 = &ct1 + &ct2; //运算
    let ct7 = &ct1 * &ct3;
    let ct8 = &ct3 - &ct5;
    let ct9 = &ct1 / &ct3;

    println!("ct6 = {}",ct6.decrypt(&keys)); //解密
    println!("ct7 = {}",ct7.decrypt(&keys));
    println!("ct8 = {}",ct8.decrypt(&keys));
    println!("ct9 = {}",ct9.decrypt(&keys));

    // We homomorphically swapped values using bitwise operations

    Ok(())
}

zama-concrete库 rust语言的同态加密库_第6张图片

代码(整数密文明文运算)

fn main() -> Result<(), Box> {
    let config = ConfigBuilder::all_disabled().enable_default_uint3().build();
    let (keys, server_keys) = generate_keys(config);
    set_server_key(server_keys);
    
    let clear_1 = 6u8;
    let clear_2 = 3u8;
    let clear_3 = 2u8;
    let clear_4 = 0u8;
    let clear_5 = 5u8;

    let ct1 = FheUint3::try_encrypt(clear_1, &keys)?;
    let ct2 = FheUint3::try_encrypt(clear_2, &keys)?;
    let ct3 = FheUint3::try_encrypt(clear_3, &keys)?;
    let ct4 = FheUint3::try_encrypt(clear_4, &keys)?;
    let ct5 = FheUint3::try_encrypt(clear_5, &keys)?;

    let ct6 = &ct1 + 2u8;
    let ct7 = &ct2 * 4u8;
    let ct8 = &ct3 - 6u8;
    let ct9 = &ct4 % 2u8;

    println!("ct6 = {}",ct6.decrypt(&keys));
    println!("ct7 = {}",ct7.decrypt(&keys));
    println!("ct8 = {}",ct8.decrypt(&keys));
    println!("ct9 = {}",ct9.decrypt(&keys));

    // We homomorphically swapped values using bitwise operations

    Ok(())

}

zama-concrete库 rust语言的同态加密库_第7张图片

代码(整数二进制运算,按位与/或/非/移位)

fn main() -> Result<(), Box> {
    let config = ConfigBuilder::all_disabled().enable_default_uint3().build();
    let (keys, server_keys) = generate_keys(config);
    set_server_key(server_keys);
    
    let clear_1 = 7u8;
    let clear_2 = 0u8;
    let clear_3 = 3u8;
    let clear_4 = 4u8;
    let clear_5 = 5u8;

    let ct1 = FheUint3::try_encrypt(clear_1, &keys)?;
    let ct2 = FheUint3::try_encrypt(clear_2, &keys)?;
    let ct3 = FheUint3::try_encrypt(clear_3, &keys)?;
    let ct4 = FheUint3::try_encrypt(clear_4, &keys)?;
    let ct5 = FheUint3::try_encrypt(clear_5, &keys)?;

    let ct6 = &ct1 & &ct2;
    let ct7 = &ct2 | &ct3;
    let ct8 = &ct4 ^ &ct5;
    let ct9 = &ct3 >> 1;

    println!("ct6 = {}",ct6.decrypt(&keys));
    println!("ct7 = {}",ct7.decrypt(&keys));
    println!("ct8 = {}",ct8.decrypt(&keys));
    println!("ct9 = {}",ct9.decrypt(&keys));

    // We homomorphically swapped values using bitwise operations

    Ok(())
}

zama-concrete库 rust语言的同态加密库_第8张图片

5. 序列化传递TFHE

代码(将公钥和密文序列化,发送到服务端运算)

use bincode;

use std::io::Cursor;

use concrete::{ConfigBuilder, ServerKey, generate_keys, set_server_key, FheUint8};
use concrete::prelude::*;

fn main() -> Result<(), Box>{
    let config = ConfigBuilder::all_disabled()
        .enable_default_uint8()
        .build();

    let ( client_key, server_key) = generate_keys(config);

    let msg1 = 1;
    let msg2 = 0;
    
    let value_1 = FheUint8::encrypt(msg1, &client_key);
    let value_2 = FheUint8::encrypt(msg2, &client_key);

    // Prepare to send data to the server
    // The ClientKey is _not_ sent
    let mut serialized_data = Vec::new();
    bincode::serialize_into(&mut serialized_data, &server_key)?;
    bincode::serialize_into(&mut serialized_data, &value_1)?;
    bincode::serialize_into(&mut serialized_data, &value_2)?;

    // Simulate sending serialized data to a server and getting
    // back the serialized result
    let serialized_result = server_function(&serialized_data)?;
    let result: FheUint8 = bincode::deserialize(&serialized_result)?;

    let output: u8 = result.decrypt(&client_key);
    assert_eq!(output, msg1 + msg2);
    Ok(())
}


fn server_function(serialized_data: &[u8]) -> Result, Box> { //服务端操作
    let mut serialized_data = Cursor::new(serialized_data);
    let server_key: ServerKey = bincode::deserialize_from(&mut serialized_data)?;
    let ct_1: FheUint8 = bincode::deserialize_from(&mut serialized_data)?;
    let ct_2: FheUint8 = bincode::deserialize_from(&mut serialized_data)?;
    
    set_server_key(server_key);

    let result = ct_1 + ct_2;

    let serialized_result = bincode::serialize(&result)?;

    Ok(serialized_result)
}

运行结果(两个TRUE的加密序列化结果不一样)

  • 即使加密同一个明文,密文也是不一样的
    zama-concrete库 rust语言的同态加密库_第9张图片

zama-concrete库 rust语言的同态加密库_第10张图片

密文和密钥大小

zama-concrete库 rust语言的同态加密库_第11张图片

你可能感兴趣的:(rust,同态加密)