diff --git a/src/share/additive.rs b/src/share/additive.rs index 3092f606..0f30d244 100644 --- a/src/share/additive.rs +++ b/src/share/additive.rs @@ -15,7 +15,7 @@ use derivative::Derivative; use crate::reveal::Reveal; -use super::pairing::ExtendedPairingEngine; +// use super::pairing::ExtendedPairingEngine; // use super::group::GroupAffineShare; use super::{ field::{ExtFieldShare, FieldShare}, @@ -193,7 +193,9 @@ macro_rules! impl_group_basics { impl_group_basics!(AdditiveGroupShare, Group); -impl GroupShare for AdditiveGroupShare {} +impl GroupShare for AdditiveGroupShare { + type FieldShare = AdditiveFieldShare; +} #[derive(Clone, Copy, Debug, Derivative)] #[derivative( @@ -202,14 +204,14 @@ impl GroupShare for AdditiveGroupShare {} )] pub struct AdditivePairingShare(pub PhantomData); -impl PairingShare for AdditivePairingShare { +impl PairingShare for AdditivePairingShare { type FrShare = AdditiveFieldShare; type FqShare = AdditiveFieldShare; type FqeShare = AdditiveExtFieldShare; // Not a typo. We want a multiplicative subgroup. type FqkShare = MulExtFieldShare; - type G1AffineShare = AdditiveGroupShare; - type G2AffineShare = AdditiveGroupShare; - type G1ProjectiveShare = AdditiveGroupShare; - type G2ProjectiveShare = AdditiveGroupShare; + type G1AffineShare = AdditiveGroupShare; + type G2AffineShare = AdditiveGroupShare; + type G1ProjectiveShare = AdditiveGroupShare; + type G2ProjectiveShare = AdditiveGroupShare; } diff --git a/src/share/group.rs b/src/share/group.rs index 030c079f..bda0f0e3 100644 --- a/src/share/group.rs +++ b/src/share/group.rs @@ -8,6 +8,8 @@ use ark_serialize::{ use std::fmt::Debug; use std::hash::Hash; +use super::field::FieldShare; + pub trait GroupShare: Clone + Copy @@ -24,6 +26,7 @@ pub trait GroupShare: + ToBytes + 'static { + type FieldShare: FieldShare; } // pub trait GroupAffineShare: diff --git a/src/share/pairing.rs b/src/share/pairing.rs index 2c4d58b2..939a01f6 100644 --- a/src/share/pairing.rs +++ b/src/share/pairing.rs @@ -1,4 +1,4 @@ -use ark_ec::{group::Group, AffineCurve, PairingEngine, ProjectiveCurve}; +use ark_ec::{bls12::Bls12Parameters, group::Group, AffineCurve, PairingEngine, ProjectiveCurve}; use super::{ field::{ExtFieldShare, FieldShare}, @@ -7,47 +7,53 @@ use super::{ use std::{fmt::Debug, ops::MulAssign}; -pub trait ExtendedPairingEngine: PairingEngine { - type GroupedG1Projective: ProjectiveCurve< - BaseField = Self::Fq, - ScalarField = Self::Fr, - Affine = Self::GroupedG1Affine, - > + From - + Into - + MulAssign - // needed due to https://github.com/rust-lang/rust/issues/69640 - + Group; +// pub trait ExtendedPairingEngine: PairingEngine { +// type GroupedG1Projective: ProjectiveCurve< +// BaseField = Self::Fq, +// ScalarField = Self::Fr, +// Affine = Self::GroupedG1Affine, +// > + From +// + Into +// + MulAssign +// // needed due to https://github.com/rust-lang/rust/issues/69640 +// + Group; - type GroupedG1Affine: AffineCurve< - BaseField = Self::Fq, - ScalarField = Self::Fr, - Projective = Self::GroupedG1Projective, - > + From - + Into - + Into - + Group; +// type GroupedG1Affine: AffineCurve< +// BaseField = Self::Fq, +// ScalarField = Self::Fr, +// Projective = Self::GroupedG1Projective, +// > + From +// + Into +// + Into +// + Group; - type GroupedG2Projective: ProjectiveCurve< - BaseField = Self::Fqe, - ScalarField = Self::Fr, - Affine = Self::GroupedG2Affine, - > + From - + Into - + MulAssign - // needed due to https://github.com/rust-lang/rust/issues/69640 - + Group; +// type GroupedG2Projective: ProjectiveCurve< +// BaseField = Self::Fqe, +// ScalarField = Self::Fr, +// Affine = Self::GroupedG2Affine, +// > + From +// + Into +// + MulAssign +// // needed due to https://github.com/rust-lang/rust/issues/69640 +// + Group; - type GroupedG2Affine: AffineCurve< - BaseField = Self::Fqe, - ScalarField = Self::Fr, - Projective = Self::GroupedG2Projective, - > + From - + Into - + Into - + Group; -} +// type GroupedG2Affine: AffineCurve< +// BaseField = Self::Fqe, +// ScalarField = Self::Fr, +// Projective = Self::GroupedG2Projective, +// > + From +// + Into +// + Into +// + Group; +// } + +pub trait GroupedAffine {} + +impl GroupedAffine for C {} + +pub trait GroupedProjective {} -pub trait PairingShare: +pub trait PairingShare: Clone + Copy + Debug + 'static + Send + Sync + PartialEq + Eq { type FrShare: FieldShare; @@ -59,8 +65,8 @@ pub trait PairingShare: // type hoge: E::G1Affine; - type G1AffineShare: GroupShare; - type G2AffineShare: GroupShare; - type G1ProjectiveShare: GroupShare; - type G2ProjectiveShare: GroupShare; + type G1AffineShare: GroupShare; + type G2AffineShare: GroupShare; + type G1ProjectiveShare: GroupShare; + type G2ProjectiveShare: GroupShare; } diff --git a/src/wire/group.rs b/src/wire/group.rs index 6305f3b3..3f1af8dd 100644 --- a/src/wire/group.rs +++ b/src/wire/group.rs @@ -1,7 +1,18 @@ +use std::fmt::{self, Display}; +use std::io::{self, Read, Write}; +use std::ops::*; + +use std::iter::Sum; + use ark_ec::{group::Group, AffineCurve}; +use ark_ff::prelude::*; +use ark_ff::{FromBytes, ToBytes}; +use rand::Rng; use crate::share::group::GroupShare; +use super::field::MpcField; + #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum MpcGroup> { Public(G), @@ -13,3 +24,189 @@ pub enum MpcGroup> { // Public(G), // Shared(S), // } + +impl> Display for MpcGroup { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result { + todo!() + } +} + +impl> ToBytes for MpcGroup { + fn write(&self, writer: W) -> io::Result<()> { + todo!() + } +} + +impl> FromBytes for MpcGroup { + fn read(reader: R) -> io::Result { + todo!() + } +} + +impl> UniformRand for MpcGroup { + fn rand(rng: &mut R) -> Self { + todo!() + } +} + +impl> Sum for MpcGroup { + fn sum>(iter: I) -> Self { + todo!() + } +} + +impl<'a, G: Group, S: GroupShare> Sum<&'a MpcGroup> for MpcGroup { + fn sum>>(iter: I) -> Self { + todo!() + } +} + +impl> Neg for MpcGroup { + type Output = Self; + + fn neg(self) -> Self::Output { + todo!() + } +} + +// impl_ref_ops!(Add, AddAssign, add, add_assign, Group, GroupShare, MpcGroup); +// impl_ref_ops!(Sub, SubAssign, sub, sub_assign, Group, GroupShare, MpcGroup); + +impl> AddAssign for MpcGroup { + fn add_assign(&mut self, rhs: Self) { + todo!() + } +} + +impl<'a, G: Group, S: GroupShare> AddAssign<&'a MpcGroup> for MpcGroup { + fn add_assign(&mut self, rhs: &'a MpcGroup) { + todo!() + } +} + +impl> Add for MpcGroup { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + todo!() + } +} + +impl<'a, G: Group, S: GroupShare> Add<&'a MpcGroup> for MpcGroup { + type Output = Self; + + fn add(self, rhs: &'a MpcGroup) -> Self::Output { + todo!() + } +} + +impl> SubAssign for MpcGroup { + fn sub_assign(&mut self, rhs: Self) { + todo!() + } +} + +impl<'a, G: Group, S: GroupShare> SubAssign<&'a MpcGroup> for MpcGroup { + fn sub_assign(&mut self, rhs: &'a MpcGroup) { + todo!() + } +} + +impl> Sub for MpcGroup { + type Output = Self; + + fn sub(self, rhs: Self) -> Self::Output { + todo!() + } +} + +impl<'a, G: Group, S: GroupShare> Sub<&'a MpcGroup> for MpcGroup { + type Output = Self; + + fn sub(self, rhs: &'a MpcGroup) -> Self::Output { + todo!() + } +} + +impl> Zero for MpcGroup { + fn zero() -> Self { + todo!() + } + + fn is_zero(&self) -> bool { + todo!() + } +} + +impl> Default for MpcGroup { + fn default() -> Self { + todo!() + } +} + +// impl> Mul> for MpcGroup { +// type Output = Self; +// #[inline] +// fn mul(mut self, other: MpcField) -> Self::Output { +// self *= &other; +// self +// } +// } + +// impl<'a, T: Group, S: GroupShare> Mul<&'a MpcField> +// for MpcGroup +// { +// type Output = Self; +// #[inline] +// fn mul(mut self, other: &MpcField) -> Self::Output { +// self *= other; +// self +// } +// } +impl> MulAssign> + for MpcGroup +{ + #[inline] + fn mul_assign(&mut self, other: MpcField) { + *self *= &other; + } +} +impl<'a, T: Group, S: GroupShare> MulAssign<&'a MpcField> + for MpcGroup +{ + #[inline] + fn mul_assign(&mut self, other: &MpcField) { + match self { + // for some reason, a two-stage match (rather than a tuple match) avoids moving + // self + MpcGroup::Public(x) => match other { + MpcField::Public(y) => { + *x *= *y; + } + MpcField::Shared(y) => { + todo!() + } + }, + MpcGroup::Shared(x) => match other { + MpcField::Public(y) => { + todo!() + } + MpcField::Shared(y) => { + todo!() + } + }, + } + } +} + +impl> Group for MpcGroup { + type ScalarField = MpcField; + + fn double(&self) -> Self { + todo!() + } + + fn double_in_place(&mut self) -> &mut Self { + todo!() + } +} diff --git a/src/wire/pairing.rs b/src/wire/pairing.rs index 057d85dc..2f2f786e 100644 --- a/src/wire/pairing.rs +++ b/src/wire/pairing.rs @@ -8,6 +8,7 @@ use std::ops::*; use zeroize::Zeroize; +use ark_ec::group::Group; use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve}; use ark_ff::bytes::{FromBytes, ToBytes}; use ark_ff::prelude::*; @@ -17,8 +18,6 @@ use ark_serialize::{ CanonicalSerializeWithFlags, Flags, SerializationError, }; -use crate::share::pairing::ExtendedPairingEngine; - use super::super::share::field::ExtFieldShare; use super::super::share::pairing::PairingShare; use super::field::MpcField; @@ -34,8 +33,8 @@ use derivative::Derivative; Eq(bound = "E::G1Affine: Eq"), Hash(bound = "E::G1Affine: Hash") )] -pub struct MpcG1Affine> { - pub val: MpcGroup, +pub struct MpcG1Affine> { + pub val: MpcGroup, } #[derive(Debug, Derivative, Clone, Copy, Eq)] @@ -43,13 +42,13 @@ pub struct MpcG1Affine> { PartialEq(bound = "E::G1Affine: PartialEq"), Hash(bound = "E::G1Affine: Hash") )] -pub struct MpcG1Projective> { - pub val: MpcGroup, +pub struct MpcG1Projective> { + pub val: MpcGroup, } #[derive(Debug, Clone, Derivative)] #[derivative(Default(bound = "E::G1Prepared: Default"))] -pub struct MpcG1Prep> { +pub struct MpcG1Prep> { pub val: E::G1Prepared, _phantom: PhantomData<(E, PS)>, } @@ -62,8 +61,8 @@ pub struct MpcG1Prep> { Eq(bound = "E::G1Affine: Eq"), Hash(bound = "E::G1Affine: Hash") )] -pub struct MpcG2Affine> { - pub val: MpcGroup, +pub struct MpcG2Affine> { + pub val: MpcGroup, } #[derive(Debug, Derivative, Clone, Copy, Eq)] @@ -71,13 +70,13 @@ pub struct MpcG2Affine> { PartialEq(bound = "E::G1Affine: PartialEq"), Hash(bound = "E::G1Affine: Hash") )] -pub struct MpcG2Projective> { - pub val: MpcGroup, +pub struct MpcG2Projective> { + pub val: MpcGroup, } #[derive(Debug, Clone, Derivative)] #[derivative(Default(bound = "E::G1Prepared: Default"))] -pub struct MpcG2Prep> { +pub struct MpcG2Prep> { pub val: E::G2Prepared, _phantom: PhantomData<(E, PS)>, } @@ -96,11 +95,11 @@ pub struct MpcExtField> { #[derive(Derivative)] #[derivative(Clone, Copy, Debug, PartialEq, Eq)] -pub struct MpcPairingEngine> { +pub struct MpcPairingEngine> { _phantom: PhantomData<(E, PS)>, } -impl> PairingEngine for MpcPairingEngine { +impl> PairingEngine for MpcPairingEngine { type Fr = MpcField; type G1Projective = MpcG1Projective; type G1Affine = MpcG1Affine; @@ -485,7 +484,7 @@ macro_rules! impl_pairing_curve_wrapper { impl_pairing_curve_wrapper!( MpcGroup, - ExtendedPairingEngine, + PairingEngine, PairingShare, G1Affine, G1AffineShare, @@ -493,7 +492,7 @@ impl_pairing_curve_wrapper!( ); impl_pairing_curve_wrapper!( MpcGroup, - ExtendedPairingEngine, + PairingEngine, PairingShare, G1Projective, G1ProjectiveShare, @@ -501,7 +500,7 @@ impl_pairing_curve_wrapper!( ); impl_pairing_curve_wrapper!( MpcGroup, - ExtendedPairingEngine, + PairingEngine, PairingShare, G2Affine, G2AffineShare, @@ -509,7 +508,7 @@ impl_pairing_curve_wrapper!( ); impl_pairing_curve_wrapper!( MpcGroup, - ExtendedPairingEngine, + PairingEngine, PairingShare, G2Projective, G2ProjectiveShare, @@ -520,31 +519,42 @@ impl_ext_field_wrapper!(MpcField, MpcExtField); macro_rules! impl_aff_proj { ($w_prep:ident, $prep:ident, $w_aff:ident, $w_pro:ident, $aff:ident, $pro:ident, $g_name:ident, $w_base:ident, $base:ident, $base_share:ident, $share_aff:ident, $share_proj:ident) => { - impl> From<$w_pro> for $w_aff { + impl> Group for $w_aff { + type ScalarField = MpcField; + + fn double(&self) -> Self { + todo!() + } + + fn double_in_place(&mut self) -> &mut Self { + todo!() + } + } + impl> From<$w_pro> for $w_aff { fn from(p: $w_pro) -> Self { todo!() } } - impl> From<$w_aff> for $w_pro { + impl> From<$w_aff> for $w_pro { fn from(p: $w_aff) -> Self { todo!() } } - impl> From<$w_aff> for $w_prep { + impl> From<$w_aff> for $w_prep { fn from(p: $w_aff) -> Self { todo!() } } - impl> ToBytes for $w_prep { + impl> ToBytes for $w_prep { fn write(&self, writer: W) -> io::Result<()> { todo!() } } - impl> AffineCurve for $w_aff { + impl> AffineCurve for $w_aff { type ScalarField = MpcField; const COFACTOR: &'static [u64] = E::$aff::COFACTOR; type BaseField = $w_base; @@ -574,7 +584,7 @@ macro_rules! impl_aff_proj { } } - impl> ProjectiveCurve for $w_pro { + impl> ProjectiveCurve for $w_pro { type ScalarField = MpcField; // aff?pro?