github链接
官方文档
curl --tlsv1.2 -sSf https://sh.rustup.rs | sh
sudo apt install build-essential
cargo new greeting
cd ./greeting
cargo build --release
cargo run --release
[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"]}
//调库
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);
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));
}
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(())
}
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(())
}
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(())
}
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)
}