Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed FHE arguments #378

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions examples/amm/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sunscreen::{
fhe_program,
types::{bfv::Rational, Cipher},
Ciphertext, CompiledFheProgram, Compiler, Error, FheRuntime, Params, PrivateKey, PublicKey,
CompiledFheProgram, Compiler, Error, FheRuntime, Params, PrivateKey, PublicKey,
};

#[fhe_program(scheme = "bfv")]
Expand Down Expand Up @@ -37,14 +37,10 @@ impl Miner {

pub fn run_contract(
&self,
nu_tokens_to_trade: Ciphertext,
nu_tokens_to_trade: Cipher<Rational>,
public_key: &PublicKey,
) -> Result<Ciphertext, Error> {
let results =
self.runtime
.run(&self.compiled_swap_nu, vec![nu_tokens_to_trade], public_key)?;

Ok(results[0].clone())
) -> Result<Cipher<Rational>, Error> {
swap_nu.run(&self.runtime, public_key, nu_tokens_to_trade)
}
}

Expand Down Expand Up @@ -73,13 +69,13 @@ impl Alice {
})
}

pub fn create_transaction(&self, amount: f64) -> Result<Ciphertext, Error> {
pub fn create_transaction(&self, amount: f64) -> Result<Cipher<Rational>, Error> {
Ok(self
.runtime
.encrypt(Rational::try_from(amount)?, &self.public_key)?)
}

pub fn check_received_eth(&self, received_eth: Ciphertext) -> Result<(), Error> {
pub fn check_received_eth(&self, received_eth: Cipher<Rational>) -> Result<(), Error> {
let received_eth: Rational = self.runtime.decrypt(&received_eth, &self.private_key)?;

let received_eth: f64 = received_eth.into();
Expand Down
77 changes: 25 additions & 52 deletions examples/calculator_fractional/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::thread::{self, JoinHandle};
use sunscreen::{
fhe_program,
types::{bfv::Fractional, Cipher},
Ciphertext, Compiler, FheApplication, FheRuntime, Params, PlainModulusConstraint, PublicKey,
RuntimeError,
Compiler, FheApplication, FheRuntime, Params, PlainModulusConstraint, PublicKey, RuntimeError,
};

fn help() {
Expand All @@ -26,7 +25,7 @@ fn help() {
enum Term {
Ans,
F64(f64),
Encrypted(Ciphertext),
Encrypted(Cipher<Fractional<64>>),
}

#[derive(PartialEq)]
Expand Down Expand Up @@ -134,7 +133,7 @@ fn alice(
send_pub: Sender<PublicKey>,
send_calc: Sender<Expression>,
recv_params: Receiver<Params>,
recv_res: Receiver<Ciphertext>,
recv_res: Receiver<Cipher<Fractional<64>>>,
) -> JoinHandle<()> {
thread::spawn(move || {
let stdin = io::stdin();
Expand Down Expand Up @@ -191,7 +190,7 @@ fn alice(
.unwrap();

// Get our result from Bob and print it.
let result: Ciphertext = recv_res.recv().unwrap();
let result: Cipher<Fractional<64>> = recv_res.recv().unwrap();
let result: Fractional<64> = match runtime.decrypt(&result, &private_key) {
Ok(v) => v,
Err(RuntimeError::TooMuchNoise) => {
Expand All @@ -207,22 +206,22 @@ fn alice(
})
}

fn compile_fhe_programs() -> FheApplication {
#[fhe_program(scheme = "bfv")]
fn add(a: Cipher<Fractional<64>>, b: Cipher<Fractional<64>>) -> Cipher<Fractional<64>> {
a + b
}
#[fhe_program(scheme = "bfv")]
fn add(a: Cipher<Fractional<64>>, b: Cipher<Fractional<64>>) -> Cipher<Fractional<64>> {
a + b
}

#[fhe_program(scheme = "bfv")]
fn sub(a: Cipher<Fractional<64>>, b: Cipher<Fractional<64>>) -> Cipher<Fractional<64>> {
a - b
}
#[fhe_program(scheme = "bfv")]
fn sub(a: Cipher<Fractional<64>>, b: Cipher<Fractional<64>>) -> Cipher<Fractional<64>> {
a - b
}

#[fhe_program(scheme = "bfv")]
fn mul(a: Cipher<Fractional<64>>, b: Cipher<Fractional<64>>) -> Cipher<Fractional<64>> {
a * b
}
#[fhe_program(scheme = "bfv")]
fn mul(a: Cipher<Fractional<64>>, b: Cipher<Fractional<64>>) -> Cipher<Fractional<64>> {
a * b
}

fn compile_fhe_programs() -> FheApplication {
Compiler::new()
.fhe_program(add)
.fhe_program(sub)
Expand All @@ -238,7 +237,7 @@ fn bob(
recv_pub: Receiver<PublicKey>,
recv_calc: Receiver<Expression>,
send_params: Sender<Params>,
send_res: Sender<Ciphertext>,
send_res: Sender<Cipher<Fractional<64>>>,
) -> JoinHandle<()> {
thread::spawn(move || {
let app = compile_fhe_programs();
Expand Down Expand Up @@ -268,41 +267,14 @@ fn bob(
_ => panic!("Alice sent us a plaintext!"),
};

let mut c = match op {
Operand::Add => runtime
.run(
app.get_fhe_program("add").unwrap(),
vec![left, right],
&public_key,
)
.unwrap(),
Operand::Sub => runtime
.run(
app.get_fhe_program("sub").unwrap(),
vec![left, right],
&public_key,
)
.unwrap(),
Operand::Mul => runtime
.run(
app.get_fhe_program("mul").unwrap(),
vec![left, right],
&public_key,
)
.unwrap(),
// To do division, Alice must send us 1 / b and we
// multiply.
Operand::Div => runtime
.run(
app.get_fhe_program("mul").unwrap(),
vec![left, right],
&public_key,
)
.unwrap(),
let c = match op {
Operand::Add => add.run(&runtime, &public_key, left, right).unwrap(),
Operand::Sub => sub.run(&runtime, &public_key, left, right).unwrap(),
Operand::Mul => mul.run(&runtime, &public_key, left, right).unwrap(),
Operand::Div => mul.run(&runtime, &public_key, left, right).unwrap(),
};

// Our FHE program produces a single value, so move the value out of the vector.
let c = c.drain(0..).next().unwrap();
ans = c.clone();

send_res.send(c).unwrap();
Expand All @@ -321,7 +293,8 @@ fn main() {
let (send_bob_params, receive_bob_params) = std::sync::mpsc::channel::<Params>();

// A channel for Bob to send calculation results to Alice.
let (send_bob_result, receive_bob_result) = std::sync::mpsc::channel::<Ciphertext>();
let (send_bob_result, receive_bob_result) =
std::sync::mpsc::channel::<Cipher<Fractional<64>>>();

// We intentionally break Alice and Bob's roles into different functions to clearly
// show the separation of their roles. In a real application, they're usually on
Expand Down
81 changes: 28 additions & 53 deletions examples/calculator_rational/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sunscreen::FheRuntime;
use sunscreen::{
fhe_program,
types::{bfv::Rational, Cipher},
Ciphertext, Compiler, FheApplication, Params, PlainModulusConstraint, PublicKey, RuntimeError,
Compiler, FheApplication, Params, PlainModulusConstraint, PublicKey, RuntimeError,
};

fn help() {
Expand All @@ -26,7 +26,7 @@ fn help() {
enum Term {
Ans,
F64(f64),
Encrypted(Ciphertext),
Encrypted(Cipher<Rational>),
}

enum Operand {
Expand Down Expand Up @@ -116,7 +116,7 @@ fn alice(
send_pub: Sender<PublicKey>,
send_calc: Sender<Expression>,
recv_params: Receiver<Params>,
recv_res: Receiver<Ciphertext>,
recv_res: Receiver<Cipher<Rational>>,
) -> JoinHandle<()> {
thread::spawn(move || {
let stdin = io::stdin();
Expand Down Expand Up @@ -173,7 +173,7 @@ fn alice(
.unwrap();

// Get our result from Bob and print it.
let result: Ciphertext = recv_res.recv().unwrap();
let result: Cipher<Rational> = recv_res.recv().unwrap();
let result: Rational = match runtime.decrypt(&result, &private_key) {
Ok(v) => v,
Err(RuntimeError::TooMuchNoise) => {
Expand All @@ -189,27 +189,27 @@ fn alice(
})
}

fn compile_fhe_programs() -> FheApplication {
#[fhe_program(scheme = "bfv")]
fn add(a: Cipher<Rational>, b: Cipher<Rational>) -> Cipher<Rational> {
a + b
}
#[fhe_program(scheme = "bfv")]
fn add(a: Cipher<Rational>, b: Cipher<Rational>) -> Cipher<Rational> {
a + b
}

#[fhe_program(scheme = "bfv")]
fn sub(a: Cipher<Rational>, b: Cipher<Rational>) -> Cipher<Rational> {
a - b
}
#[fhe_program(scheme = "bfv")]
fn sub(a: Cipher<Rational>, b: Cipher<Rational>) -> Cipher<Rational> {
a - b
}

#[fhe_program(scheme = "bfv")]
fn mul(a: Cipher<Rational>, b: Cipher<Rational>) -> Cipher<Rational> {
a * b
}
#[fhe_program(scheme = "bfv")]
fn mul(a: Cipher<Rational>, b: Cipher<Rational>) -> Cipher<Rational> {
a * b
}

#[fhe_program(scheme = "bfv")]
fn div(a: Cipher<Rational>, b: Cipher<Rational>) -> Cipher<Rational> {
a / b
}
#[fhe_program(scheme = "bfv")]
fn div(a: Cipher<Rational>, b: Cipher<Rational>) -> Cipher<Rational> {
a / b
}

fn compile_fhe_programs() -> FheApplication {
// We compile all the programs together so they have compatible
// scheme parameters
Compiler::new()
Expand All @@ -228,7 +228,7 @@ fn bob(
recv_pub: Receiver<PublicKey>,
recv_calc: Receiver<Expression>,
send_params: Sender<Params>,
send_res: Sender<Ciphertext>,
send_res: Sender<Cipher<Rational>>,
) -> JoinHandle<()> {
thread::spawn(move || {
let app = compile_fhe_programs();
Expand Down Expand Up @@ -258,39 +258,14 @@ fn bob(
_ => panic!("Alice sent us a plaintext!"),
};

let mut c = match op {
Operand::Add => runtime
.run(
app.get_fhe_program("add").unwrap(),
vec![left, right],
&public_key,
)
.unwrap(),
Operand::Sub => runtime
.run(
app.get_fhe_program("sub").unwrap(),
vec![left, right],
&public_key,
)
.unwrap(),
Operand::Mul => runtime
.run(
app.get_fhe_program("mul").unwrap(),
vec![left, right],
&public_key,
)
.unwrap(),
Operand::Div => runtime
.run(
app.get_fhe_program("div").unwrap(),
vec![left, right],
&public_key,
)
.unwrap(),
let c = match op {
Operand::Add => add.run(&runtime, &public_key, left, right).unwrap(),
Operand::Sub => sub.run(&runtime, &public_key, left, right).unwrap(),
Operand::Mul => mul.run(&runtime, &public_key, left, right).unwrap(),
Operand::Div => div.run(&runtime, &public_key, left, right).unwrap(),
};

// Our FHE program produces a single value, so move the value out of the vector.
let c = c.drain(0..).next().unwrap();
ans = c.clone();

send_res.send(c).unwrap();
Expand All @@ -309,7 +284,7 @@ fn main() {
let (send_bob_params, receive_bob_params) = std::sync::mpsc::channel::<Params>();

// A channel for Bob to send calculation results to Alice.
let (send_bob_result, receive_bob_result) = std::sync::mpsc::channel::<Ciphertext>();
let (send_bob_result, receive_bob_result) = std::sync::mpsc::channel::<Cipher<Rational>>();

// We intentionally break Alice and Bob's roles into different functions to clearly
// show the separation of their roles. In a real application, they're usually on
Expand Down
2 changes: 1 addition & 1 deletion examples/chi_sq/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn run_fhe<F, T, U>(
) -> Result<(), Error>
where
F: FheProgramFn + Clone + 'static + AsRef<str>,
U: From<T> + FheType + TypeName + std::fmt::Display,
U: From<T> + FheType + TypeName + std::fmt::Display + 'static,
{
let start = Instant::now();

Expand Down
Loading
Loading