Skip to content

Commit

Permalink
🚧 Update share and wire
Browse files Browse the repository at this point in the history
  • Loading branch information
sheagrief committed Sep 26, 2023
1 parent f4c0bfc commit a0eacb7
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 73 deletions.
16 changes: 9 additions & 7 deletions src/share/additive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -193,7 +193,9 @@ macro_rules! impl_group_basics {

impl_group_basics!(AdditiveGroupShare, Group);

impl<G: Group> GroupShare<G> for AdditiveGroupShare<G> {}
impl<G: Group> GroupShare<G> for AdditiveGroupShare<G> {
type FieldShare = AdditiveFieldShare<G::ScalarField>;
}

#[derive(Clone, Copy, Debug, Derivative)]
#[derivative(
Expand All @@ -202,14 +204,14 @@ impl<G: Group> GroupShare<G> for AdditiveGroupShare<G> {}
)]
pub struct AdditivePairingShare<E: PairingEngine>(pub PhantomData<E>);

impl<E: ExtendedPairingEngine> PairingShare<E> for AdditivePairingShare<E> {
impl<E: PairingEngine> PairingShare<E> for AdditivePairingShare<E> {
type FrShare = AdditiveFieldShare<E::Fr>;
type FqShare = AdditiveFieldShare<E::Fq>;
type FqeShare = AdditiveExtFieldShare<E::Fqe>;
// Not a typo. We want a multiplicative subgroup.
type FqkShare = MulExtFieldShare<E::Fqk>;
type G1AffineShare = AdditiveGroupShare<E::GroupedG1Affine>;
type G2AffineShare = AdditiveGroupShare<E::GroupedG2Affine>;
type G1ProjectiveShare = AdditiveGroupShare<E::GroupedG1Projective>;
type G2ProjectiveShare = AdditiveGroupShare<E::GroupedG2Projective>;
type G1AffineShare = AdditiveGroupShare<E::G1Affine>;
type G2AffineShare = AdditiveGroupShare<E::G2Affine>;
type G1ProjectiveShare = AdditiveGroupShare<E::G1Projective>;
type G2ProjectiveShare = AdditiveGroupShare<E::G2Projective>;
}
3 changes: 3 additions & 0 deletions src/share/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use ark_serialize::{
use std::fmt::Debug;
use std::hash::Hash;

use super::field::FieldShare;

pub trait GroupShare<G: Group>:
Clone
+ Copy
Expand All @@ -24,6 +26,7 @@ pub trait GroupShare<G: Group>:
+ ToBytes
+ 'static
{
type FieldShare: FieldShare<G::ScalarField>;
}

// pub trait GroupAffineShare<G: AffineCurve>:
Expand Down
90 changes: 48 additions & 42 deletions src/share/pairing.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<Self::GroupedG1Affine>
+ Into<Self::GroupedG1Affine>
+ MulAssign<Self::Fr>
// needed due to https://github.com/rust-lang/rust/issues/69640
+ Group<ScalarField = Self::Fr>;
// pub trait ExtendedPairingEngine: PairingEngine {
// type GroupedG1Projective: ProjectiveCurve<
// BaseField = Self::Fq,
// ScalarField = Self::Fr,
// Affine = Self::GroupedG1Affine,
// > + From<Self::GroupedG1Affine>
// + Into<Self::GroupedG1Affine>
// + MulAssign<Self::Fr>
// // needed due to https://github.com/rust-lang/rust/issues/69640
// + Group<ScalarField = Self::Fr>;

type GroupedG1Affine: AffineCurve<
BaseField = Self::Fq,
ScalarField = Self::Fr,
Projective = Self::GroupedG1Projective,
> + From<Self::GroupedG1Projective>
+ Into<Self::GroupedG1Projective>
+ Into<Self::G1Prepared>
+ Group<ScalarField = Self::Fr>;
// type GroupedG1Affine: AffineCurve<
// BaseField = Self::Fq,
// ScalarField = Self::Fr,
// Projective = Self::GroupedG1Projective,
// > + From<Self::GroupedG1Projective>
// + Into<Self::GroupedG1Projective>
// + Into<Self::G1Prepared>
// + Group<ScalarField = Self::Fr>;

type GroupedG2Projective: ProjectiveCurve<
BaseField = Self::Fqe,
ScalarField = Self::Fr,
Affine = Self::GroupedG2Affine,
> + From<Self::GroupedG2Affine>
+ Into<Self::GroupedG2Affine>
+ MulAssign<Self::Fr>
// needed due to https://github.com/rust-lang/rust/issues/69640
+ Group<ScalarField = Self::Fr>;
// type GroupedG2Projective: ProjectiveCurve<
// BaseField = Self::Fqe,
// ScalarField = Self::Fr,
// Affine = Self::GroupedG2Affine,
// > + From<Self::GroupedG2Affine>
// + Into<Self::GroupedG2Affine>
// + MulAssign<Self::Fr>
// // needed due to https://github.com/rust-lang/rust/issues/69640
// + Group<ScalarField = Self::Fr>;

type GroupedG2Affine: AffineCurve<
BaseField = Self::Fqe,
ScalarField = Self::Fr,
Projective = Self::GroupedG2Projective,
> + From<Self::GroupedG2Projective>
+ Into<Self::GroupedG2Projective>
+ Into<Self::G2Prepared>
+ Group<ScalarField = Self::Fr>;
}
// type GroupedG2Affine: AffineCurve<
// BaseField = Self::Fqe,
// ScalarField = Self::Fr,
// Projective = Self::GroupedG2Projective,
// > + From<Self::GroupedG2Projective>
// + Into<Self::GroupedG2Projective>
// + Into<Self::G2Prepared>
// + Group<ScalarField = Self::Fr>;
// }

pub trait GroupedAffine<C: AffineCurve> {}

impl<C: AffineCurve> GroupedAffine<C> for C {}

pub trait GroupedProjective<C: ProjectiveCurve> {}

pub trait PairingShare<E: ExtendedPairingEngine>:
pub trait PairingShare<E: PairingEngine>:
Clone + Copy + Debug + 'static + Send + Sync + PartialEq + Eq
{
type FrShare: FieldShare<E::Fr>;
Expand All @@ -59,8 +65,8 @@ pub trait PairingShare<E: ExtendedPairingEngine>:

// type hoge: E::G1Affine;

type G1AffineShare: GroupShare<E::GroupedG1Affine>;
type G2AffineShare: GroupShare<E::GroupedG2Affine>;
type G1ProjectiveShare: GroupShare<E::GroupedG1Projective>;
type G2ProjectiveShare: GroupShare<E::GroupedG2Projective>;
type G1AffineShare: GroupShare<E::G1Affine>;
type G2AffineShare: GroupShare<E::G2Affine>;
type G1ProjectiveShare: GroupShare<E::G1Projective>;
type G2ProjectiveShare: GroupShare<E::G2Projective>;
}
197 changes: 197 additions & 0 deletions src/wire/group.rs
Original file line number Diff line number Diff line change
@@ -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<G: Group, S: GroupShare<G>> {
Public(G),
Expand All @@ -13,3 +24,189 @@ pub enum MpcGroup<G: Group, S: GroupShare<G>> {
// Public(G),
// Shared(S),
// }

impl<G: Group, S: GroupShare<G>> Display for MpcGroup<G, S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> ToBytes for MpcGroup<G, S> {
fn write<W: ark_serialize::Write>(&self, writer: W) -> io::Result<()> {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> FromBytes for MpcGroup<G, S> {
fn read<R: Read>(reader: R) -> io::Result<Self> {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> UniformRand for MpcGroup<G, S> {
fn rand<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> Sum for MpcGroup<G, S> {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
todo!()
}
}

impl<'a, G: Group, S: GroupShare<G>> Sum<&'a MpcGroup<G, S>> for MpcGroup<G, S> {
fn sum<I: Iterator<Item = &'a MpcGroup<G, S>>>(iter: I) -> Self {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> Neg for MpcGroup<G, S> {
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<G: Group, S: GroupShare<G>> AddAssign for MpcGroup<G, S> {
fn add_assign(&mut self, rhs: Self) {
todo!()
}
}

impl<'a, G: Group, S: GroupShare<G>> AddAssign<&'a MpcGroup<G, S>> for MpcGroup<G, S> {
fn add_assign(&mut self, rhs: &'a MpcGroup<G, S>) {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> Add for MpcGroup<G, S> {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
todo!()
}
}

impl<'a, G: Group, S: GroupShare<G>> Add<&'a MpcGroup<G, S>> for MpcGroup<G, S> {
type Output = Self;

fn add(self, rhs: &'a MpcGroup<G, S>) -> Self::Output {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> SubAssign for MpcGroup<G, S> {
fn sub_assign(&mut self, rhs: Self) {
todo!()
}
}

impl<'a, G: Group, S: GroupShare<G>> SubAssign<&'a MpcGroup<G, S>> for MpcGroup<G, S> {
fn sub_assign(&mut self, rhs: &'a MpcGroup<G, S>) {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> Sub for MpcGroup<G, S> {
type Output = Self;

fn sub(self, rhs: Self) -> Self::Output {
todo!()
}
}

impl<'a, G: Group, S: GroupShare<G>> Sub<&'a MpcGroup<G, S>> for MpcGroup<G, S> {
type Output = Self;

fn sub(self, rhs: &'a MpcGroup<G, S>) -> Self::Output {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> Zero for MpcGroup<G, S> {
fn zero() -> Self {
todo!()
}

fn is_zero(&self) -> bool {
todo!()
}
}

impl<G: Group, S: GroupShare<G>> Default for MpcGroup<G, S> {
fn default() -> Self {
todo!()
}
}

// impl<T: Group, S: GroupShare<T>> Mul<MpcField<T::ScalarField, S::FieldShare>> for MpcGroup<T, S> {
// type Output = Self;
// #[inline]
// fn mul(mut self, other: MpcField<T::ScalarField, S::FieldShare>) -> Self::Output {
// self *= &other;
// self
// }
// }

// impl<'a, T: Group, S: GroupShare<T>> Mul<&'a MpcField<T::ScalarField, S::FieldShare>>
// for MpcGroup<T, S>
// {
// type Output = Self;
// #[inline]
// fn mul(mut self, other: &MpcField<T::ScalarField, S::FieldShare>) -> Self::Output {
// self *= other;
// self
// }
// }
impl<T: Group, S: GroupShare<T>> MulAssign<MpcField<T::ScalarField, S::FieldShare>>
for MpcGroup<T, S>
{
#[inline]
fn mul_assign(&mut self, other: MpcField<T::ScalarField, S::FieldShare>) {
*self *= &other;
}
}
impl<'a, T: Group, S: GroupShare<T>> MulAssign<&'a MpcField<T::ScalarField, S::FieldShare>>
for MpcGroup<T, S>
{
#[inline]
fn mul_assign(&mut self, other: &MpcField<T::ScalarField, S::FieldShare>) {
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<T: Group, S: GroupShare<T>> Group for MpcGroup<T, S> {
type ScalarField = MpcField<T::ScalarField, S::FieldShare>;

fn double(&self) -> Self {
todo!()
}

fn double_in_place(&mut self) -> &mut Self {
todo!()
}
}
Loading

0 comments on commit a0eacb7

Please sign in to comment.