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

Feat/#32 implement mpc groth16 #64

Merged
merged 3 commits into from
Oct 17, 2024
Merged
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
2 changes: 2 additions & 0 deletions arkworks/groth16/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub mod prover;
/// Verify proofs for the Groth16 zkSNARK construction.
pub mod verifier;

pub mod reveal;

/// Constraints for the Groth16 verifier.
#[cfg(feature = "r1cs")]
pub mod constraints;
Expand Down
5 changes: 4 additions & 1 deletion examples/bin_test_groth16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ struct Opt {
fn main() {
let opt = Opt::from_args();
Net::init_from_file(opt.input.to_str().unwrap(), opt.id);
// groth16::mpc_test_prove_and_verify(1);
zk_mpc::groth16::mpc_test_prove_and_verify::<
ark_bls12_377::Bls12_377,
mpc_algebra::AdditivePairingShare<ark_bls12_377::Bls12_377>,
>(1);
}
5 changes: 5 additions & 0 deletions mpc-algebra/src/share/additive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ impl<G: Group, M> Reveal for AdditiveGroupShare<G, M> {

macro_rules! impl_group_basics {
($share:ident, $bound:ident) => {
impl<T: $bound, M> Display for $share<T, M> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.val)
}
}
impl<T: $bound, M> Debug for $share<T, M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.val)
Expand Down
46 changes: 46 additions & 0 deletions mpc-algebra/src/share/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ use ark_serialize::{
CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
CanonicalSerializeWithFlags,
};
use ark_std::end_timer;
use ark_std::start_timer;
use std::fmt::Debug;
use std::fmt::Display;
use std::hash::Hash;

use crate::Reveal;

use super::field::FieldShare;
use super::BeaverSource;

pub trait GroupShare<G: Group>:
Clone
+ Copy
+ Debug
+ Display
+ Send
+ Sync
+ Eq
Expand Down Expand Up @@ -63,6 +68,47 @@ pub trait GroupShare<G: Group>:

fn shift(&mut self, other: &G) -> &mut Self;

fn scale<S: BeaverSource<Self, Self::FieldShare, Self>>(
self,
other: Self::FieldShare,
source: &mut S,
) -> Self {
let timer = start_timer!(|| "SS scalar multiplication");
let (mut x, y, z) = source.triple();
let s = self;
let o = other;
// output: z - open(s + x)y - x*open(o + y) + open(s + x)open(o + y)
// xy - sy - xy - ox - yx + so + sy + xo + xy
// so
let mut sx = {
let mut t = s;
t.add(&x).open()
};
let oy = {
let mut t = o;
t.add(&y).open()
};
let mut out = z.clone();
out.sub(&Self::scale_pub_group(sx.clone(), &y));
out.sub(x.scale_pub_scalar(&oy));
sx *= oy;
out.shift(&sx);
#[cfg(debug_assertions)]
{
let a = s.reveal();
let b = o.reveal();
let mut acp = a.clone();
acp *= b;
let r = out.reveal();
if acp != r {
println!("Bad multiplication!.\n{}\n*\n{}\n=\n{}", a, b, r);
panic!("Bad multiplication");
}
}
end_timer!(timer);
out
}

/// Compute \sum_i (s_i * g_i)
/// where the s_i are shared and the g_i are public.
fn multi_scale_pub_group(bases: &[G], scalars: &[Self::FieldShare]) -> Self {
Expand Down
67 changes: 57 additions & 10 deletions mpc-algebra/src/wire/group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::{self, Display};
use std::io::{self, Read, Write};
use std::marker::PhantomData;
use std::ops::*;

use std::iter::Sum;
Expand All @@ -12,10 +13,12 @@ use ark_serialize::{
CanonicalSerializeWithFlags,
};
use ark_serialize::{Flags, SerializationError};
use derivative::Derivative;
use mpc_net::{MpcMultiNet as Net, MpcNet};
use mpc_trait::MpcWire;

use crate::share::group::GroupShare;
use crate::Reveal;
use crate::{BeaverSource, Reveal};

use super::field::MpcField;

Expand All @@ -25,6 +28,45 @@ pub enum MpcGroup<G: Group, S: GroupShare<G>> {
Shared(S),
}

#[derive(Derivative)]
#[derivative(Default(bound = ""), Clone(bound = ""), Copy(bound = ""))]
pub struct DummyGroupTripleSource<T, S> {
_scalar: PhantomData<T>,
_share: PhantomData<S>,
}

impl<T: Group, S: GroupShare<T>> BeaverSource<S, S::FieldShare, S>
for DummyGroupTripleSource<T, S>
{
#[inline]
fn triple(&mut self) -> (S, S::FieldShare, S) {
(
S::from_add_shared(T::zero()),
<S::FieldShare as Reveal>::from_add_shared(if Net::am_king() {
T::ScalarField::one()
} else {
T::ScalarField::zero()
}),
S::from_add_shared(T::zero()),
)
}
#[inline]
fn inv_pair(&mut self) -> (S::FieldShare, S::FieldShare) {
(
<S::FieldShare as Reveal>::from_add_shared(if Net::am_king() {
T::ScalarField::one()
} else {
T::ScalarField::zero()
}),
<S::FieldShare as Reveal>::from_add_shared(if Net::am_king() {
T::ScalarField::one()
} else {
T::ScalarField::zero()
}),
)
}
}

impl<T: Group, S: GroupShare<T>> MpcGroup<T, S> {
pub fn map<TT: Group, SS: GroupShare<TT>, FT: Fn(T) -> TT, FS: Fn(S) -> SS>(
self,
Expand Down Expand Up @@ -78,8 +120,11 @@ impl<G: Group, S: GroupShare<G>> Mul<MpcField<G::ScalarField, S::FieldShare>> fo
}

impl<G: Group, S: GroupShare<G>> Display for MpcGroup<G, S> {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
todo!()
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
match self {
MpcGroup::Public(x) => write!(f, "{x} (public)"),
MpcGroup::Shared(x) => write!(f, "{x} (shared)"),
}
}
}

Expand Down Expand Up @@ -308,16 +353,18 @@ impl<'a, T: Group, S: GroupShare<T>> MulAssign<&'a MpcField<T::ScalarField, S::F
MpcField::Public(y) => {
*x *= *y;
}
MpcField::Shared(_y) => {
todo!()
MpcField::Shared(y) => {
let t = MpcGroup::Shared(S::scale_pub_group(*x, y));
*self = t;
}
},
MpcGroup::Shared(_x) => match other {
MpcField::Public(_y) => {
todo!()
MpcGroup::Shared(x) => match other {
MpcField::Public(y) => {
x.scale_pub_scalar(y);
}
MpcField::Shared(_y) => {
todo!()
MpcField::Shared(y) => {
let t = x.scale(*y, &mut DummyGroupTripleSource::default());
*x = t;
}
},
}
Expand Down
13 changes: 7 additions & 6 deletions mpc-algebra/src/wire/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,14 @@ macro_rules! impl_pairing_curve_wrapper {
}
}
#[inline]
fn from_add_shared(_t: Self::Base) -> Self {
todo!()
fn from_add_shared(t: Self::Base) -> Self {
Self {
val: $wrapped::from_add_shared(t),
}
}
#[inline]
fn unwrap_as_public(self) -> Self::Base {
// self.val.unwrap_as_public()
todo!()
self.val.unwrap_as_public()
}
#[inline]
fn king_share<R: Rng>(_f: Self::Base, _rng: &mut R) -> Self {
Expand All @@ -578,8 +579,8 @@ macro_rules! impl_pairing_curve_wrapper {
}

impl<E: $bound1, PS: $bound2<E>> MulAssign<MpcField<E::Fr, PS::FrShare>> for $wrap<E, PS> {
fn mul_assign(&mut self, _rhs: MpcField<E::Fr, PS::FrShare>) {
todo!()
fn mul_assign(&mut self, other: MpcField<E::Fr, PS::FrShare>) {
self.val.mul_assign(other);
}
}
};
Expand Down
6 changes: 2 additions & 4 deletions src/circuits/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ impl<ConstraintF: PrimeField + LocalOrMPC<ConstraintF>> ConstraintSynthesizer<Co
}

#[derive(Clone)]
pub struct MySimpleCircuit<F: PrimeField + LocalOrMPC<F>> {
pub struct MySimpleCircuit<F: PrimeField> {
pub a: Option<F>,
pub b: Option<F>,
}

impl<ConstraintF: PrimeField + LocalOrMPC<ConstraintF>> ConstraintSynthesizer<ConstraintF>
for MySimpleCircuit<ConstraintF>
{
impl<ConstraintF: PrimeField> ConstraintSynthesizer<ConstraintF> for MySimpleCircuit<ConstraintF> {
fn generate_constraints(
self,
cs: ConstraintSystemRef<ConstraintF>,
Expand Down
Loading
Loading