Skip to content

Commit

Permalink
🚧 Wip
Browse files Browse the repository at this point in the history
git log
  • Loading branch information
taskooh committed Aug 29, 2024
1 parent a7fbf17 commit 6a93135
Show file tree
Hide file tree
Showing 17 changed files with 966 additions and 66 deletions.
381 changes: 378 additions & 3 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/bin_test_groth16.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mpc_net::{MpcMultiNet as Net, MpcNet};
use mpc_net::{LocalTestNet as Net, MPCNet};
use std::path::PathBuf;
use structopt::StructOpt;

Expand Down
2 changes: 1 addition & 1 deletion examples/bin_test_marlin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mpc_net::{MpcMultiNet as Net, MpcNet};
use mpc_net::{LocalTestNet as Net, MPCNet};
use std::path::PathBuf;
use structopt::StructOpt;

Expand Down
3 changes: 2 additions & 1 deletion examples/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use ark_marlin::IndexProverKey;
use ark_serialize::{CanonicalDeserialize, Read};
use ark_std::test_rng;
use mpc_algebra::Reveal;
use mpc_net::{MpcMultiNet as Net, MpcNet};
use mpc_net::{LocalTestNet as Net, MPCNet};

use serde::Deserialize;
use std::{fs::File, path::PathBuf, vec};
use structopt::StructOpt;
Expand Down
2 changes: 1 addition & 1 deletion mpc-algebra/examples/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn test_div() {
}

fn test_sum() {
let a = [
let a = vec![
MF::from_public(F::from(1u64)),
MF::from_public(F::from(2u64)),
MF::from_public(F::from(3u64)),
Expand Down
35 changes: 19 additions & 16 deletions mpc-algebra/src/channel.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use mpc_net::MpcNet;
use mpc_net::MPCNet;
use rand::RngCore;
use sha2::{Digest, Sha256};
use std::cell::Cell;

/// A trait for MPC networks that can serialize and deserialize.
pub trait MpcSerNet: MpcNet {
pub trait MPCSerNet: MPCNet {
/// Broadcast a value to each other.
fn broadcast<T: CanonicalSerialize + CanonicalDeserialize>(out: &T) -> Vec<T> {
fn broadcast<T: CanonicalSerialize + CanonicalDeserialize>(&self, out: &T) -> Vec<T> {
let mut bytes_out = Vec::new();
out.serialize(&mut bytes_out).unwrap();
let bytes_in = Self::broadcast_bytes(&bytes_out);
let bytes_in = self.broadcast(&bytes_out);
bytes_in
.into_iter()
.map(|b| T::deserialize(&b[..]).unwrap())
.collect()
}

fn send_to_king<T: CanonicalDeserialize + CanonicalSerialize>(out: &T) -> Option<Vec<T>> {
fn send_to_king<T: CanonicalDeserialize + CanonicalSerialize>(
&self,
out: &T,
) -> Option<Vec<T>> {
let mut bytes_out = Vec::new();
out.serialize(&mut bytes_out).unwrap();
Self::send_bytes_to_king(&bytes_out).map(|bytes_in| {
self.send_to_king(&bytes_out).map(|bytes_in| {
bytes_in
.into_iter()
.map(|b| T::deserialize(&b[..]).unwrap())
Expand All @@ -41,20 +44,20 @@ pub trait MpcSerNet: MpcNet {
T::deserialize(&bytes_in[..]).unwrap()
}

fn atomic_broadcast<T: CanonicalDeserialize + CanonicalSerialize>(out: &T) -> Vec<T> {
fn atomic_broadcast<T: CanonicalDeserialize + CanonicalSerialize>(&self, out: &T) -> Vec<T> {
let mut bytes_out = Vec::new();
out.serialize(&mut bytes_out).unwrap();
let ser_len = bytes_out.len();
bytes_out.resize(ser_len + COMMIT_RAND_BYTES, 0);
rand::thread_rng().fill_bytes(&mut bytes_out[ser_len..]);
let commitment = CommitHash::new().chain(&bytes_out).finalize();
// exchange commitments
let all_commits = Self::broadcast_bytes(&commitment[..]);
// exchange (data || randomness)
let all_data = Self::broadcast_bytes(&bytes_out);
let self_id = Self::party_id();
let commitment = CommitHash::new().chain(&bytes_out).finalize().to_vec();
// コミットメントを交換
let all_commits = self.broadcast(&commitment);
// データとランダムネスを交換
let all_data = self.broadcast(&bytes_out);
let self_id = self.party_id();
for i in 0..all_commits.len() {
if i != self_id {
if i as u32 != self_id {
// check other commitment
assert_eq!(
&all_commits[i][..],
Expand All @@ -69,15 +72,15 @@ pub trait MpcSerNet: MpcNet {
}

fn king_compute<T: CanonicalDeserialize + CanonicalSerialize>(
&self,
x: &T,
f: impl Fn(Vec<T>) -> Vec<T>,
) -> T {
let king_response = Self::send_to_king(x).map(f);
Self::receive_from_king(king_response)
}
}

impl<N: MpcNet> MpcSerNet for N {}
impl<N: MPCNet> MPCSerNet for N {}

const ALLOW_CHEATING: Cell<bool> = Cell::new(true);

Expand Down
28 changes: 17 additions & 11 deletions mpc-algebra/src/share/additive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::{self, Debug, Display, Formatter};
use std::hash::Hash;
use std::io::{self, Read, Write};
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};

use ark_ec::{group::Group, PairingEngine, ProjectiveCurve};
use ark_ff::BigInteger;
Expand All @@ -19,8 +20,8 @@ use rand::Rng;
use crate::reveal::Reveal;
use crate::{BeaverSource, DenseOrSparsePolynomial, DensePolynomial, Msm, SparsePolynomial};

use crate::channel::MpcSerNet;
use mpc_net::{MpcMultiNet as Net, MpcNet};
use crate::channel::MPCSerNet;
use mpc_net::LocalTestNet as Net;

// use super::pairing::ExtendedPairingEngine;
// use super::group::GroupAffineShare;
Expand All @@ -30,9 +31,11 @@ use super::{
pairing::{AffProjShare, PairingShare},
};

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct AdditiveFieldShare<T> {
pub val: T,
// reference to Net
net: Arc<Mutex<Net>>,
}

impl<F: Field> AdditiveFieldShare<F> {
Expand Down Expand Up @@ -78,21 +81,22 @@ impl<F: Field> AdditiveFieldShare<F> {
impl<F: Field> Reveal for AdditiveFieldShare<F> {
type Base = F;

fn reveal(self) -> Self::Base {
Net::broadcast(&self.val).into_iter().sum()
fn reveal(&self) -> Self::Base {

Check failure on line 84 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Fix Check

method `reveal` has an incompatible type for trait

Check failure on line 84 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Online Binary

method `reveal` has an incompatible type for trait

Check failure on line 84 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Werewolf Binary

method `reveal` has an incompatible type for trait

Check failure on line 84 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Groth16 Binary

method `reveal` has an incompatible type for trait

Check failure on line 84 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Marlin Binary

method `reveal` has an incompatible type for trait

Check failure on line 84 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Tests

method `reveal` has an incompatible type for trait
self.net.broadcast(&self.val).into_iter().sum()
}

fn from_add_shared(b: Self::Base) -> Self {
Self { val: b }
fn from_add_shared(b: Self::Base, net: Net) -> Self {

Check failure on line 88 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Fix Check

method `from_add_shared` has 2 parameters but the declaration in trait `reveal::Reveal::from_add_shared` has 1

Check failure on line 88 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Online Binary

method `from_add_shared` has 2 parameters but the declaration in trait `reveal::Reveal::from_add_shared` has 1

Check failure on line 88 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Werewolf Binary

method `from_add_shared` has 2 parameters but the declaration in trait `reveal::Reveal::from_add_shared` has 1

Check failure on line 88 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Groth16 Binary

method `from_add_shared` has 2 parameters but the declaration in trait `reveal::Reveal::from_add_shared` has 1

Check failure on line 88 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Marlin Binary

method `from_add_shared` has 2 parameters but the declaration in trait `reveal::Reveal::from_add_shared` has 1

Check failure on line 88 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Tests

method `from_add_shared` has 2 parameters but the declaration in trait `reveal::Reveal::from_add_shared` has 1
Self { val: b, net }
}

fn from_public(f: Self::Base) -> Self {
fn from_public(f: Self::Base, net: Net) -> Self {

Check failure on line 92 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Fix Check

method `from_public` has 2 parameters but the declaration in trait `reveal::Reveal::from_public` has 1

Check failure on line 92 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Online Binary

method `from_public` has 2 parameters but the declaration in trait `reveal::Reveal::from_public` has 1

Check failure on line 92 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Werewolf Binary

method `from_public` has 2 parameters but the declaration in trait `reveal::Reveal::from_public` has 1

Check failure on line 92 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Groth16 Binary

method `from_public` has 2 parameters but the declaration in trait `reveal::Reveal::from_public` has 1

Check failure on line 92 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Marlin Binary

method `from_public` has 2 parameters but the declaration in trait `reveal::Reveal::from_public` has 1

Check failure on line 92 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Tests

method `from_public` has 2 parameters but the declaration in trait `reveal::Reveal::from_public` has 1
Self {
val: if Net::am_king() { f } else { F::zero() },
val: if net. { f } else { F::zero() },

Check failure on line 94 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Fix Check

unexpected token: `{`

Check failure on line 94 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Online Binary

unexpected token: `{`

Check failure on line 94 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Werewolf Binary

unexpected token: `{`

Check failure on line 94 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Groth16 Binary

unexpected token: `{`

Check failure on line 94 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Marlin Binary

unexpected token: `{`

Check failure on line 94 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Tests

unexpected token: `{`
net: self.net.clone(),

Check failure on line 95 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Fix Check

expected value, found module `self`

Check failure on line 95 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Online Binary

expected value, found module `self`

Check failure on line 95 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Werewolf Binary

expected value, found module `self`

Check failure on line 95 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Groth16 Binary

expected value, found module `self`

Check failure on line 95 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Marlin Binary

expected value, found module `self`

Check failure on line 95 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Tests

expected value, found module `self`
}
}

fn unwrap_as_public(self) -> Self::Base {
fn unwrap_as_public(&self) -> Self::Base {

Check failure on line 99 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Fix Check

method `unwrap_as_public` has an incompatible type for trait

Check failure on line 99 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Online Binary

method `unwrap_as_public` has an incompatible type for trait

Check failure on line 99 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Werewolf Binary

method `unwrap_as_public` has an incompatible type for trait

Check failure on line 99 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Groth16 Binary

method `unwrap_as_public` has an incompatible type for trait

Check failure on line 99 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Marlin Binary

method `unwrap_as_public` has an incompatible type for trait

Check failure on line 99 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Tests

method `unwrap_as_public` has an incompatible type for trait
self.val
}
fn king_share<R: Rng>(f: Self::Base, rng: &mut R) -> Self {
Expand Down Expand Up @@ -239,14 +243,16 @@ macro_rules! impl_field_basics {

impl_field_basics!(AdditiveFieldShare, Field);

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
pub struct AdditiveExtFieldShare<F: Field>(pub PhantomData<F>);

impl<F: Field> ExtFieldShare<F> for AdditiveExtFieldShare<F> {

Check failure on line 249 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Fix Check

the trait bound `AdditiveExtFieldShare<F>: std::marker::Copy` is not satisfied

Check failure on line 249 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Online Binary

the trait bound `AdditiveExtFieldShare<F>: std::marker::Copy` is not satisfied

Check failure on line 249 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Werewolf Binary

the trait bound `AdditiveExtFieldShare<F>: std::marker::Copy` is not satisfied

Check failure on line 249 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Groth16 Binary

the trait bound `AdditiveExtFieldShare<F>: std::marker::Copy` is not satisfied

Check failure on line 249 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Marlin Binary

the trait bound `AdditiveExtFieldShare<F>: std::marker::Copy` is not satisfied

Check failure on line 249 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Tests

the trait bound `AdditiveExtFieldShare<F>: std::marker::Copy` is not satisfied
type Base = AdditiveFieldShare<F::BasePrimeField>;
type Ext = AdditiveFieldShare<F>;
}

impl Copy for AdditiveExtFieldShare<crate::field::Fp> {}

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Fix Check

cannot find type `Fp` in module `crate::field`

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Fix Check

`field` is ambiguous

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Online Binary

cannot find type `Fp` in module `crate::field`

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Online Binary

`field` is ambiguous

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Werewolf Binary

cannot find type `Fp` in module `crate::field`

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Werewolf Binary

`field` is ambiguous

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Groth16 Binary

cannot find type `Fp` in module `crate::field`

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Groth16 Binary

`field` is ambiguous

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Marlin Binary

cannot find type `Fp` in module `crate::field`

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Run Marlin Binary

`field` is ambiguous

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Tests

cannot find type `Fp` in module `crate::field`

Check failure on line 254 in mpc-algebra/src/share/additive.rs

View workflow job for this annotation

GitHub Actions / Tests

`field` is ambiguous

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct MulFieldShare<T> {
pub val: T,
Expand Down
6 changes: 3 additions & 3 deletions mpc-algebra/src/share/spdz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::hash::Hash;
use std::io::{self, Read, Write};
use std::marker::PhantomData;

use crate::channel::{can_cheat, MpcSerNet};
use mpc_net::{MpcMultiNet as Net, MpcNet};
use crate::channel::{can_cheat, MPCSerNet};
use mpc_net::{LocalTestNet as Net, MPCNet};

use super::additive::{AdditiveFieldShare, AdditiveGroupShare, MulFieldShare};
use super::field::{DenseOrSparsePolynomial, DensePolynomial, ExtFieldShare, FieldShare};
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn mac<F: Field>() -> F {
}
}

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct SpdzFieldShare<T> {
sh: AdditiveFieldShare<T>,
mac: AdditiveFieldShare<T>,
Expand Down
4 changes: 2 additions & 2 deletions mpc-algebra/src/wire/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use ark_crypto_primitives::commitment::pedersen::{Parameters, Randomness};
use ark_crypto_primitives::encryption::elgamal::Parameters as ElGamalParameters;
use ark_crypto_primitives::encryption::elgamal::Randomness as ElGamalRandomness;

use mpc_net::MpcMultiNet as Net;
use mpc_net::LocalTestNet as Net;
use mpc_trait::MpcWire;

use crate::{channel::MpcSerNet, SpdzFieldShare};
use crate::{channel::MPCSerNet, SpdzFieldShare};
use crate::{AdditiveFieldShare, MpcField, Reveal};

type AdditiveFq = MpcField<ark_bls12_377::Fr, AdditiveFieldShare<ark_bls12_377::Fr>>;
Expand Down
2 changes: 1 addition & 1 deletion mpc-algebra/src/wire/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
LogicalOperations, Reveal,
};
use crate::{EqualityZero, UniformBitRand};
use mpc_net::{MpcMultiNet as Net, MpcNet};
use mpc_net::{LocalTestNet as Net, MPCNet};

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum MpcField<F: Field, S: FieldShare<F>> {
Expand Down
8 changes: 4 additions & 4 deletions mpc-algebra/src/wire/macros.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};

use crate::channel::MpcSerNet;
use mpc_net::MpcNet;
use crate::channel::MPCSerNet;
use mpc_net::MPCNet;

use std::fmt::Display;

pub fn check_eq<T: CanonicalSerialize + CanonicalDeserialize + Clone + Eq + Display>(t: T) {
debug_assert!({
use log::debug;
debug!("Consistency check");
let others = mpc_net::MpcMultiNet::broadcast(&t);
let others = mpc_net::LocalTestNet::broadcast(&t);
let mut result = true;
for (i, other_t) in others.iter().enumerate() {
if &t != other_t {
println!(
"\nConsistency check failed\nI (party {}) have {}\nvs\n (party {}) has {}",
mpc_net::MpcMultiNet::party_id(),
mpc_net::LocalTestNet::party_id(),
t,
i,
other_t
Expand Down
Loading

0 comments on commit 6a93135

Please sign in to comment.