Skip to content

Commit

Permalink
🚧 Wip
Browse files Browse the repository at this point in the history
git log
  • Loading branch information
taskooh committed May 5, 2024
1 parent 0bcaaf0 commit 4a7d086
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 422 deletions.
2 changes: 1 addition & 1 deletion mpc-algebra/examples/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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
40 changes: 20 additions & 20 deletions mpc-algebra/src/channel.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
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())
.collect()
})
}

fn recieve_from_king<T: CanonicalSerialize + CanonicalDeserialize>(out: Option<Vec<T>>) -> T {
let bytes_in = Self::recv_bytes_from_king(out.map(|outs| {
fn recieve_from_king<T: CanonicalSerialize + CanonicalDeserialize>(&self,out: Option<Vec<T>>) -> T {
let bytes_in = self.recieve_from_king(out.map(|outs| {
outs.iter()
.map(|out| {
let mut bytes_out = Vec::new();
Expand All @@ -41,20 +41,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 +69,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::recieve_from_king(king_response)
let king_response = self.send_to_king(x).map(f);
self.recieve_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::{Field, FromBytes, ToBytes};
Expand All @@ -17,8 +18,8 @@ use derivative::Derivative;
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 @@ -28,9 +29,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 @@ -76,21 +79,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 {
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 {
Self { val: b, net }
}

fn from_public(f: Self::Base) -> Self {
fn from_public(f: Self::Base, net: Net) -> Self {
Self {
val: if Net::am_king() { f } else { F::zero() },
val: if net. { f } else { F::zero() },
net: self.net.clone(),
}
}

fn unwrap_as_public(self) -> Self::Base {
fn unwrap_as_public(&self) -> Self::Base {
self.val
}
}
Expand Down Expand Up @@ -202,14 +206,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> {
type Base = AdditiveFieldShare<F::BasePrimeField>;
type Ext = AdditiveFieldShare<F>;
}

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

#[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 @@ -25,7 +25,7 @@ use ark_serialize::{
use crate::share::field::FieldShare;
use crate::{BeaverSource, BitAdd, BitDecomposition, BitwiseLessThan, LessThan, 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 4a7d086

Please sign in to comment.