Skip to content

Commit

Permalink
Add encrypted ERF
Browse files Browse the repository at this point in the history
  • Loading branch information
cgouert committed May 16, 2024
1 parent a0d3949 commit a005f05
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "MIT"
clap = "3.0"
image = "0.23"
num-integer = "0.1.46"
statrs = "0.16.0"
csv = "1.3"
debug_print = "1.0.0"
dwt = "0.5.2"
Expand Down Expand Up @@ -91,4 +92,4 @@ path = "src/edge_detection_ptxt.rs"

[[bin]]
name = "edge_detection_lut"
path = "src/edge_detection_lut.rs"
path = "src/edge_detection_lut.rs"
42 changes: 38 additions & 4 deletions src/primitive_ops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{f64::consts::E, time::Instant};

use ripple::common::*;
use statrs::function::erf::erf;
use tfhe::{
integer::{
gen_keys_radix, wopbs::*, IntegerCiphertext, IntegerRadixCiphertext, RadixCiphertext,
Expand Down Expand Up @@ -80,7 +81,7 @@ fn ct_lut_eval_haar(

fn main() {
// ------- Client side ------- //
let bit_width = 16;
let bit_width = 24;

// Number of blocks per ciphertext
let nb_blocks = bit_width >> 1;
Expand All @@ -103,7 +104,7 @@ fn main() {
start.elapsed().as_secs_f64()
);

let precision = 8u8;
let precision = 16u8;
let x = quantize(64.0, precision, bit_width as u8);
let x_ct = client_key.encrypt(x);
let y = quantize(2.0, precision, bit_width as u8);
Expand Down Expand Up @@ -453,7 +454,7 @@ fn main() {
unquantize(dwt_gt, precision, bit_width as u8),
);

// 9.1 x > y using LUT
// 10.1 x > y using LUT
fn gt_pt2(value: f64) -> f64 {
((value > 0_f64) as u8) as f64
}
Expand All @@ -463,7 +464,7 @@ fn main() {
let lut_gt: bool = client_key.decrypt_bool(&gt_ct);
println!("GT (LUT) time: {:?}", lut_time);

// 9.2. x > y using Haar DWT LUT
// 10.2. x > y using Haar DWT LUT
let (gt_ct_haar, dwt_time) = ct_lut_eval_haar(
x_ct.clone(),
precision,
Expand All @@ -483,4 +484,37 @@ fn main() {
lut_gt,
unquantize(dwt_gt, precision, bit_width as u8),
);

// 11.1 ERF using LUT
let (erf_ct, lut_time) = ct_lut_eval(
x_ct.clone(),
precision,
bit_width,
&erf,
&wopbs_key,
&server_key,
);
let lut_erf: u64 = client_key.decrypt(&erf_ct);
println!("ERF (LUT) time: {:?}", lut_time);

// 11.2 ERF using Haar DWT LUT
let (erf_ct_haar, dwt_time) = ct_lut_eval_haar(
x_ct.clone(),
precision,
bit_width,
nb_blocks,
&erf,
&wopbs_key,
&server_key,
);
let dwt_erf: u64 = client_key.decrypt(&erf_ct_haar);
println!("ERF (Haar) time: {:?}", dwt_time);

println!(
"--- LUT: {:?}, DWT LUT: {:?}\n--- unq: LUT: {:?}, DWT LUT: {:?}",
lut_erf,
dwt_erf,
unquantize(lut_erf, precision, bit_width as u8),
unquantize(dwt_erf, precision, bit_width as u8),
);
}

0 comments on commit a005f05

Please sign in to comment.