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

Rename current methods #58

Draft
wants to merge 5 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
452 changes: 429 additions & 23 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ mpc-net = { path = "mpc-net", version = "0.1.0" }
mpc-trait = { path = "mpc-trait", version = "0.1.0" }
itertools = "0.13.0"
nalgebra = "0.33.0"
tokio = { version = "1.40.0", features = ["sync", "net"] }

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
Expand Down
4 changes: 2 additions & 2 deletions mpc-algebra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ mpc-net = { path = "../mpc-net" }
mpc-trait = { path = "../mpc-trait" }

structopt = "0.3"
env_logger = "0.8"
sha2 = "0.9"
env_logger = "0.11.5"

[features]
default = ["ark-r1cs-std"]

[[example]]
name = "algebra"
path = "examples/algebra.rs"
path = "examples/algebra.rs"
7 changes: 3 additions & 4 deletions mpc-algebra/examples/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ use ark_poly::reveal;
use ark_std::PubUniformRand;
use ark_std::{end_timer, start_timer};
use log::debug;
use mpc_algebra::pedersen::Randomness;
use mpc_algebra::boolean_field::MpcBooleanField;
use mpc_algebra::pedersen::Randomness;
use mpc_algebra::{
edwards2, share, AdditiveFieldShare, BitAdd, BitDecomposition, BitwiseLessThan, BooleanWire,
CommitmentScheme as MpcCommitmentScheme, EqualityZero, LessThan,
LogicalOperations,
CommitmentScheme as MpcCommitmentScheme, EqualityZero, LessThan, LogicalOperations,
MpcEdwardsProjective, MpcField, Reveal, UniformBitRand,
};
use mpc_net::{MpcMultiNet as Net, MpcNet};
Expand Down Expand Up @@ -412,7 +411,7 @@ fn test_pedersen_commitment() {
// mpc calculation
let mpc_parameters = MpcPed::setup(rng).unwrap();

let scalar_x_bytes = if Net::am_king() {
let scalar_x_bytes = if Net::is_leader() {
x_bits
.iter()
.map(|b| {
Expand Down
4 changes: 2 additions & 2 deletions mpc-algebra/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub trait MpcSerNet: MpcNet {
fn send_to_king<T: CanonicalDeserialize + CanonicalSerialize>(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::worker_send_or_leader_receive(&bytes_out).map(|bytes_in| {
bytes_in
.into_iter()
.map(|b| T::deserialize(&b[..]).unwrap())
Expand All @@ -29,7 +29,7 @@ pub trait MpcSerNet: MpcNet {
}

fn receive_from_king<T: CanonicalSerialize + CanonicalDeserialize>(out: Option<Vec<T>>) -> T {
let bytes_in = Self::recv_bytes_from_king(out.map(|outs| {
let bytes_in = Self::worker_receive_or_leader_send(out.map(|outs| {
outs.iter()
.map(|out| {
let mut bytes_out = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ where
let revealed_xr = xytzrsuv.reveal();

// step5: allocate share
let share = if Net::am_king() {
let share = if Net::is_leader() {
MpcTEProjective::from_public(revealed_xr) - rsuv
} else {
-rsuv
Expand Down
22 changes: 11 additions & 11 deletions mpc-algebra/src/share/additive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<F: Field> Reveal for AdditiveFieldShare<F> {

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

Expand All @@ -99,7 +99,7 @@ impl<F: Field> Reveal for AdditiveFieldShare<F> {
let mut r: Vec<F> = (0..(Net::n_parties() - 1)).map(|_| F::rand(rng)).collect();
let sum_r: F = r.iter().sum();
r.push(f - sum_r);
Self::from_add_shared(Net::receive_from_king(if Net::am_king() {
Self::from_add_shared(Net::receive_from_king(if Net::is_leader() {
Some(r)
} else {
None
Expand All @@ -113,7 +113,7 @@ impl<F: Field> Reveal for AdditiveFieldShare<F> {
.map(|i| f[i] - &rs.iter().map(|r| &r[i]).sum())
.collect();
rs.push(final_shares);
Net::receive_from_king(if Net::am_king() { Some(rs) } else { None })
Net::receive_from_king(if Net::is_leader() { Some(rs) } else { None })
.into_iter()
.map(Self::from_add_shared)
.collect()
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<F: Field> FieldShare<F> for AdditiveFieldShare<F> {
}

fn shift(&mut self, other: &F) -> &mut Self {
if Net::am_king() {
if Net::is_leader() {
self.val += other;
}
self
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<F: Field> Reveal for MulFieldShare<F> {
}
fn from_public(f: F) -> Self {
Self {
val: if Net::am_king() { f } else { F::one() },
val: if Net::is_leader() { f } else { F::one() },
}
}
fn from_add_shared(f: F) -> Self {
Expand Down Expand Up @@ -290,7 +290,7 @@ impl<F: Field> FieldShare<F> for MulFieldShare<F> {
}

fn scale(&mut self, other: &F) -> &mut Self {
if Net::am_king() {
if Net::is_leader() {
self.val *= other;
}
self
Expand Down Expand Up @@ -367,7 +367,7 @@ impl<G: Group, M> Reveal for AdditiveGroupShare<G, M> {

fn from_public(b: G) -> Self {
Self {
val: if Net::am_king() { b } else { G::zero() },
val: if Net::is_leader() { b } else { G::zero() },
_phants: PhantomData,
}
}
Expand All @@ -379,7 +379,7 @@ impl<G: Group, M> Reveal for AdditiveGroupShare<G, M> {
let mut r: Vec<G> = (0..(Net::n_parties() - 1)).map(|_| G::rand(rng)).collect();
let sum_r: G = r.iter().sum();
r.push(f - sum_r);
Self::from_add_shared(Net::receive_from_king(if Net::am_king() {
Self::from_add_shared(Net::receive_from_king(if Net::is_leader() {
Some(r)
} else {
None
Expand All @@ -393,7 +393,7 @@ impl<G: Group, M> Reveal for AdditiveGroupShare<G, M> {
.map(|i| f[i] - &rs.iter().map(|r| &r[i]).sum())
.collect();
rs.push(final_shares);
Net::receive_from_king(if Net::am_king() { Some(rs) } else { None })
Net::receive_from_king(if Net::is_leader() { Some(rs) } else { None })
.into_iter()
.map(Self::from_add_shared)
.collect()
Expand Down Expand Up @@ -483,7 +483,7 @@ impl<G: Group, M: Msm<G, G::ScalarField>> GroupShare<G> for AdditiveGroupShare<G
}

fn shift(&mut self, other: &G) -> &mut Self {
if Net::am_king() {
if Net::is_leader() {
self.val += other;
}
self
Expand Down Expand Up @@ -524,7 +524,7 @@ macro_rules! groups_share {
mut a: Self::ProjectiveShare,
o: &E::$affine,
) -> Self::ProjectiveShare {
if Net::am_king() {
if Net::is_leader() {
a.val.add_assign_mixed(&o);
}
a
Expand Down
16 changes: 8 additions & 8 deletions mpc-algebra/src/share/spdz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::Reveal;

#[inline]
pub fn mac_share<F: Field>() -> F {
if Net::am_king() {
if Net::is_leader() {
F::one()
} else {
F::zero()
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<F: Field> Reveal for SpdzFieldShare<F> {
let mut r: Vec<F> = (0..(Net::n_parties() - 1)).map(|_| F::rand(rng)).collect();
let sum_r: F = r.iter().sum();
r.push(f - sum_r);
Self::from_add_shared(Net::receive_from_king(if Net::am_king() {
Self::from_add_shared(Net::receive_from_king(if Net::is_leader() {
Some(r)
} else {
None
Expand All @@ -159,7 +159,7 @@ impl<F: Field> Reveal for SpdzFieldShare<F> {
.map(|i| f[i] - &rs.iter().map(|r| &r[i]).sum())
.collect();
rs.push(final_shares);
Net::receive_from_king(if Net::am_king() { Some(rs) } else { None })
Net::receive_from_king(if Net::is_leader() { Some(rs) } else { None })
.into_iter()
.map(Self::from_add_shared)
.collect()
Expand Down Expand Up @@ -320,7 +320,7 @@ impl<G: Group, M> Reveal for SpdzGroupShare<G, M> {
let mut r: Vec<G> = (0..(Net::n_parties() - 1)).map(|_| G::rand(rng)).collect();
let sum_r: G = r.iter().sum();
r.push(f - sum_r);
Self::from_add_shared(Net::receive_from_king(if Net::am_king() {
Self::from_add_shared(Net::receive_from_king(if Net::is_leader() {
Some(r)
} else {
None
Expand All @@ -334,7 +334,7 @@ impl<G: Group, M> Reveal for SpdzGroupShare<G, M> {
.map(|i| f[i] - &rs.iter().map(|r| &r[i]).sum())
.collect();
rs.push(final_shares);
Net::receive_from_king(if Net::am_king() { Some(rs) } else { None })
Net::receive_from_king(if Net::is_leader() { Some(rs) } else { None })
.into_iter()
.map(Self::from_add_shared)
.collect()
Expand Down Expand Up @@ -455,7 +455,7 @@ impl<G: Group, M: Msm<G, G::ScalarField>> GroupShare<G> for SpdzGroupShare<G, M>
}

fn shift(&mut self, other: &G) -> &mut Self {
if Net::am_king() {
if Net::is_leader() {
self.sh.shift(other);
}
let mut other = other.clone();
Expand Down Expand Up @@ -531,7 +531,7 @@ impl<F: Field, S: PrimeField> FieldShare<F> for SpdzMulFieldShare<F, S> {
}

fn scale(&mut self, other: &F) -> &mut Self {
if Net::am_king() {
if Net::is_leader() {
self.sh.scale(other);
}
self.mac.scale(&other.pow(&mac_share::<S>().into_repr()));
Expand Down Expand Up @@ -642,7 +642,7 @@ macro_rules! groups_share {
mut a: Self::ProjectiveShare,
o: &E::$affine,
) -> Self::ProjectiveShare {
if Net::am_king() {
if Net::is_leader() {
a.sh.val.add_assign_mixed(&o);
}
a.mac.val += &o.scalar_mul(mac_share::<E::Fr>());
Expand Down
4 changes: 2 additions & 2 deletions mpc-algebra/src/wire/edwards2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ impl<P: Parameters> AffProjShare<P::ScalarField, GroupAffine<P>, GroupProjective
mut a: Self::ProjectiveShare,
o: &GroupAffine<P>,
) -> Self::ProjectiveShare {
if Net::am_king() {
if Net::is_leader() {
a.val.add_assign_mixed(&o);
}
a
Expand Down Expand Up @@ -928,7 +928,7 @@ impl<P: Parameters> AffProjShare<P::ScalarField, GroupAffine<P>, GroupProjective
mut a: Self::ProjectiveShare,
o: &GroupAffine<P>,
) -> Self::ProjectiveShare {
if Net::am_king() {
if Net::is_leader() {
a.sh.val.add_assign_mixed(&o);
}
a
Expand Down
30 changes: 25 additions & 5 deletions mpc-algebra/src/wire/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,35 @@ pub struct DummyFieldTripleSource<F, S> {
impl<T: Field, S: FieldShare<T>> BeaverSource<S, S, S> for DummyFieldTripleSource<T, S> {
fn triple(&mut self) -> (S, S, S) {
(
S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }),
S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }),
S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }),
S::from_add_shared(if Net::is_leader() {
T::one()
} else {
T::zero()
}),
S::from_add_shared(if Net::is_leader() {
T::one()
} else {
T::zero()
}),
S::from_add_shared(if Net::is_leader() {
T::one()
} else {
T::zero()
}),
)
}
fn inv_pair(&mut self) -> (S, S) {
(
S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }),
S::from_add_shared(if Net::am_king() { T::one() } else { T::zero() }),
S::from_add_shared(if Net::is_leader() {
T::one()
} else {
T::zero()
}),
S::from_add_shared(if Net::is_leader() {
T::one()
} else {
T::zero()
}),
)
}
}
Expand Down
Loading
Loading