Skip to content

Commit

Permalink
Attempt runtime resolution of engine
Browse files Browse the repository at this point in the history
  • Loading branch information
quackzar committed Aug 20, 2024
1 parent 04feeee commit 358c76d
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 38 deletions.
14 changes: 10 additions & 4 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
pub mod parsing;

use ff::Field;
use rand::RngCore;

use crate::{
algebra::{field::Field, math::Vector},
algebra::math::Vector,
net::{network::Network, Id, SplitChannel},
protocols::beaver::{beaver_multiply, BeaverTriple},
schemes::interactive::{InteractiveShared, InteractiveSharedMany},
schemes::{
interactive::{InteractiveShared, InteractiveSharedMany},
shamir,

Check failure on line 12 in src/vm/mod.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused import: `shamir`

Check failure on line 12 in src/vm/mod.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused import: `shamir`
},
};

pub enum Value<F> {
Expand Down Expand Up @@ -85,7 +89,7 @@ impl<S: InteractiveSharedMany> Stack<S> {
impl<C, S, R, F> Engine<C, S, R>
where
C: SplitChannel,
S: InteractiveSharedMany<Value = F>,
S: InteractiveSharedMany<Value = F> + 'static,
R: RngCore + Send,
F: Field,
{
Expand Down Expand Up @@ -204,7 +208,9 @@ where
(SharedValue::Vector(x), SharedValue::Vector(y)) => {
todo!()
}
(SharedValue::Vector(_), SharedValue::Single(_)) => todo!(),
(SharedValue::Vector(_), SharedValue::Single(y)) => {
todo!()
}
(SharedValue::Single(_), SharedValue::Vector(_)) => todo!(),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/vm/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
vm::{Instruction, Script, Value},
};

struct Exp<F> {
pub struct Exp<F> {
exp: Vec<Instruction<F>>,
}

Expand Down
1 change: 1 addition & 0 deletions wecare/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wecare/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ curve25519-dalek = { version = "4.1.3", features = ["group", "serde"] }
rand = "0.8.5"
fixed = "2.0.0-alpha.11"
enum_dispatch = "0.3.13"
ff = "0.13.0"

[dev-dependencies]
tempfile = "3.10.1"
53 changes: 20 additions & 33 deletions wecare/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
pub mod vm;

use caring::{
algebra::math::Vector,
net::network::TcpNetwork,
schemes::{
feldman, interactive::InteractiveSharedMany, shamir, spdz::{self, preprocessing}
feldman,
interactive::InteractiveSharedMany,
shamir,
spdz::{self, preprocessing},
},
};
use curve25519_dalek::RistrettoPoint;
Expand Down Expand Up @@ -44,18 +49,17 @@ impl Mapping for S25519 {

impl Mapping for S32 {
fn from_f64(val: f64) -> Self {
let num : i32 = FixedI32::<16>::from_num(val).to_bits();
let num: i32 = FixedI32::<16>::from_num(val).to_bits();
S32::from(num as u32)
}

fn into_f64(val: Self) -> f64 {
let val : u32 = val.into();
let val = FixedI32::<16>::from_bits(val as i32);
let val: u32 = val.into();
let val = FixedI32::<16>::from_bits(val as i32);
val.to_num()
}
}


pub struct AdderEngine<S: InteractiveSharedMany> {
network: TcpNetwork,
runtime: tokio::runtime::Runtime,
Expand All @@ -64,7 +68,8 @@ pub struct AdderEngine<S: InteractiveSharedMany> {

impl<S, F> AdderEngine<S>
where
S: InteractiveSharedMany<Value = F>, F: Mapping
S: InteractiveSharedMany<Value = F>,
F: Mapping,
{
//pub fn setup_engine(my_addr: &str, others: &[impl AsRef<str>], file_name: String) -> Result<AdderEngine<F>, MpcError> {
fn new(network: TcpNetwork, runtime: Runtime, context: S::Context) -> Self {
Expand Down Expand Up @@ -93,12 +98,7 @@ where
context,
} = self;

let nums: Vec<_> = nums
.iter()
.map(|&num| {
F::from_f64(num)
})
.collect();
let nums: Vec<_> = nums.iter().map(|&num| F::from_f64(num)).collect();
let rng = rand::rngs::StdRng::from_rng(thread_rng()).unwrap();
let res: Option<_> = runtime.block_on(async move {
let ctx = context;
Expand All @@ -111,10 +111,7 @@ where
let sum: S::VectorShare = shares.into_iter().sum();
let res: Vector<F> = S::recombine_many(ctx, sum, network).await.unwrap();

let res = res
.into_iter()
.map(|x| F::into_f64(x))
.collect();
let res = res.into_iter().map(|x| F::into_f64(x)).collect();
Some(res)
});
res
Expand Down Expand Up @@ -145,22 +142,12 @@ pub fn do_preproc(files: &mut [File], number_of_shares: Vec<usize>, use_32: bool
let number_of_triplets = 0;
if use_32 {
let num = S32::from_f64(0.0);
preprocessing::write_preproc_to_file(
files,
known_to_each,
number_of_triplets,
num,
)
.unwrap();
preprocessing::write_preproc_to_file(files, known_to_each, number_of_triplets, num)
.unwrap();
} else {
let num = S25519::from_f64(0.0);
preprocessing::write_preproc_to_file(
files,
known_to_each,
number_of_triplets,
num,
)
.unwrap();
preprocessing::write_preproc_to_file(files, known_to_each, number_of_triplets, num)
.unwrap();
}
}

Expand Down Expand Up @@ -193,12 +180,12 @@ mod generic {
.ok_or(MpcError("No proccesing file found"))?;
if self.use_32bit_field {
let mut context = preprocessing::read_preproc_from_file(file);
context.params.who_am_i = network.index;
context.params.who_am_i = network.id();
let engine = SpdzEngine32::new(network, runtime, context);
Ok(AdderEngine::Spdz32(engine))
} else {
let mut context = preprocessing::read_preproc_from_file(file);
context.params.who_am_i = network.index;
context.params.who_am_i = network.id();
let engine = SpdzEngine::new(network, runtime, context);
Ok(AdderEngine::Spdz(engine))
}
Expand Down Expand Up @@ -300,7 +287,7 @@ mod generic {
AdderEngine::Spdz32(e) => e.mpc_sum(nums),
AdderEngine::Shamir(e) => e.mpc_sum(nums),
AdderEngine::Shamir32(e) => e.mpc_sum(nums),
AdderEngine::Feldman(e) => e.mpc_sum(nums)
AdderEngine::Feldman(e) => e.mpc_sum(nums),
}
}

Expand Down
81 changes: 81 additions & 0 deletions wecare/src/vm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use enum_dispatch::enum_dispatch;
use ff::Field;
use rand::rngs::StdRng;

use std::{
any::Any,
error::Error,
ops::{Add, Sub},
pin::Pin,
sync::Arc,
};

use caring::{
algebra::element::Element32,
net::connection::{Connection, TcpConnection},
schemes::{feldman, shamir, spdz},
vm::{self, parsing::Exp},
};

type ShamirEngine<F> = vm::Engine<TcpConnection, shamir::Share<F>, StdRng>;
type SpdzEngine<F> = vm::Engine<TcpConnection, spdz::Share<F>, StdRng>;
type FeldmanEngine<F, G> = vm::Engine<TcpConnection, feldman::VerifiableShare<F, G>, StdRng>;

type ShamirCurve25519Engine = ShamirEngine<curve25519_dalek::Scalar>;
type ShamirElement32Engine = ShamirEngine<caring::algebra::element::Element32>;
type SpdzCurve25519Engine = SpdzEngine<curve25519_dalek::Scalar>;
type SpdzElement32Engine = SpdzEngine<caring::algebra::element::Element32>;

impl Expr {}

enum Expr {
Curve25519(vm::parsing::Exp<curve25519_dalek::Scalar>),
Element32(vm::parsing::Exp<Element32>),
}

impl Add for Expr {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Expr::Curve25519(a), Expr::Curve25519(b)) => Expr::Curve25519(a + b),
(Expr::Element32(a), Expr::Element32(b)) => Expr::Element32(a + b),
_ => {
panic!("Incompatible")
}
}
}
}

impl Sub for Expr {
type Output = Self;

fn sub(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Expr::Curve25519(a), Expr::Curve25519(b)) => Self::Curve25519(a - b),
(Expr::Element32(a), Expr::Element32(b)) => Self::Element32(a - b),
_ => {
panic!("Incompatible")
}
}
}
}

pub enum Engine {
Spdz(SpdzCurve25519Engine),
Shamir(ShamirElement32Engine),
}

impl Engine {
pub async fn execute(&mut self, expr: Expr) {
match (self, expr) {
(Engine::Spdz(engine), Expr::Curve25519(exp)) => {
engine.execute(&exp.finalize());
}
(Engine::Shamir(engine), Expr::Element32(exp)) => {
engine.execute(&exp.finalize());
}
_ => panic!("unsupported!"),
}
}
}

0 comments on commit 358c76d

Please sign in to comment.