mean variance

This commit is contained in:
rickwebiii
2023-03-09 14:27:13 -08:00
parent 1e42462973
commit 32cdba8cf1
2 changed files with 30 additions and 4 deletions

View File

@@ -1,3 +1,5 @@
use std::time::Instant;
use sunscreen::{
fhe_program,
types::{bfv::Rational, Cipher},
@@ -26,8 +28,12 @@ struct Miner {
impl Miner {
pub fn setup() -> Result<Miner, Error> {
let now = Instant::now();
let app = Compiler::new().fhe_program(swap_nu).compile()?;
println!("compile time {}s", now.elapsed().as_secs_f64());
let runtime = Runtime::new_fhe(app.params())?;
Ok(Miner {
@@ -41,10 +47,14 @@ impl Miner {
nu_tokens_to_trade: Ciphertext,
public_key: &PublicKey,
) -> Result<Ciphertext, Error> {
let now = Instant::now();
let results =
self.runtime
.run(&self.compiled_swap_nu, vec![nu_tokens_to_trade], public_key)?;
println!("Run circuit in {}s", now.elapsed().as_secs_f64());
Ok(results[0].clone())
}
}
@@ -65,8 +75,12 @@ impl Alice {
pub fn setup(params: &Params) -> Result<Alice, Error> {
let runtime = Runtime::new_fhe(params)?;
let now = Instant::now();
let (public_key, private_key) = runtime.generate_keys()?;
println!("Keygen {}s", now.elapsed().as_secs_f64());
Ok(Alice {
public_key,
private_key,
@@ -75,9 +89,15 @@ impl Alice {
}
pub fn create_transaction(&self, amount: f64) -> Result<Ciphertext, Error> {
Ok(self
let now = Instant::now();
let res = Ok(self
.runtime
.encrypt(Rational::try_from(amount)?, &self.public_key)?)
.encrypt(Rational::try_from(amount)?, &self.public_key)?);
println!("Encrypt time {}s", now.elapsed().as_secs_f64());
res
}
pub fn check_received_eth(&self, received_eth: Ciphertext) -> Result<(), Error> {

View File

@@ -1,5 +1,5 @@
use bincode::Error as BincodeError;
use std::ops::{Add, Div, Mul, Sub};
use std::{ops::{Add, Div, Mul, Sub}, time::Instant};
use sunscreen::{
fhe_program,
types::{bfv::Fractional, Cipher},
@@ -81,11 +81,17 @@ impl Bob {
variance(&data)
}
let now = Instant::now();
let app = Compiler::new()
.fhe_program(mean_fhe)
.fhe_program(variance_fhe)
.fhe_program(variance_fhe).additional_noise_budget(4)
.plain_modulus_constraint(sunscreen::PlainModulusConstraint::Raw(2048* 400))
.compile()?;
println!("{:#?}", app.params());
println!("Compile time {}", now.elapsed().as_secs_f64());
let mean_program = app.get_fhe_program(mean_fhe).unwrap();
let variance_program = app.get_fhe_program(variance_fhe).unwrap();