Skip to content

Commit

Permalink
Merge branch 'feat/algebra' into fix/algebra-fork-ark-algebra
Browse files Browse the repository at this point in the history
  • Loading branch information
sheagrief committed Sep 22, 2023
2 parents 9b3fa68 + 0cb011e commit 098e5ed
Show file tree
Hide file tree
Showing 7 changed files with 813 additions and 49 deletions.
181 changes: 162 additions & 19 deletions src/share/additive.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
use std::fmt::{self, Debug};
use std::hash::Hash;
use std::io::{self, Read, Write};
use std::marker::PhantomData;

use ark_ec::AffineCurve;
use ark_ec::{group::Group, PairingEngine};
use ark_ff::{Field, FromBytes, ToBytes};
use ark_serialize::{
CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
CanonicalSerializeWithFlags,
CanonicalSerializeWithFlags, Flags, SerializationError,
};
use ark_std::UniformRand;
use derivative::Derivative;

use crate::reveal::Reveal;

use super::{field::FieldShare, pairing::PairingShare};
use super::pairing::ExtendedPairingEngine;
// use super::group::GroupAffineShare;
use super::{
field::{ExtFieldShare, FieldShare},
group::GroupShare,
pairing::PairingShare,
};

#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct AdditiveFieldShare<T> {
Expand All @@ -27,24 +38,88 @@ impl<F: Field> Reveal for AdditiveFieldShare<F> {

impl<F: Field> FieldShare<F> for AdditiveFieldShare<F> {}

impl<F: Field> ToBytes for AdditiveFieldShare<F> {}

impl<F: Field> FromBytes for AdditiveFieldShare<F> {}

impl<F: Field> CanonicalSerialize for AdditiveFieldShare<F> {}

impl<F: Field> CanonicalSerializeWithFlags for AdditiveFieldShare<F> {}

impl<F: Field> CanonicalDeserialize for AdditiveFieldShare<F> {}

impl<F: Field> CanonicalDeserializeWithFlags for AdditiveFieldShare<F> {}
macro_rules! impl_field_basics {
($share:ident, $bound:ident) => {
impl<T: $bound> ToBytes for $share<T> {
fn write<W: Write>(&self, writer: W) -> io::Result<()> {
todo!()
}
}
impl<T: $bound> FromBytes for $share<T> {
fn read<R: Read>(reader: R) -> io::Result<Self> {
todo!()
}
}
impl<T: $bound> CanonicalSerialize for $share<T> {
fn serialize<W: Write>(&self, writer: W) -> Result<(), SerializationError> {
todo!()
}

fn serialized_size(&self) -> usize {
todo!()
}
}
impl<T: $bound> CanonicalSerializeWithFlags for $share<T> {
fn serialize_with_flags<W: Write, Fl: Flags>(
&self,
writer: W,
flags: Fl,
) -> Result<(), SerializationError> {
todo!()
}

fn serialized_size_with_flags<Fl: Flags>(&self) -> usize {
todo!()
}
}
impl<T: $bound> CanonicalDeserialize for $share<T> {
fn deserialize<R: Read>(reader: R) -> Result<Self, SerializationError> {
todo!()
}
}
impl<T: $bound> CanonicalDeserializeWithFlags for $share<T> {
fn deserialize_with_flags<R: Read, Fl: Flags>(
reader: R,
) -> Result<(Self, Fl), SerializationError> {
todo!()
}
}
impl<T: $bound> UniformRand for $share<T> {
fn rand<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
todo!()
}
}
};
}

impl<F: Field> UniformRand for AdditiveFieldShare<F> {}
impl_field_basics!(AdditiveFieldShare, Field);

#[derive(Clone, Copy, 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>;
}

#[derive(Clone, Copy, Debug)]
pub struct MulExtFieldShare<F: Field>(pub PhantomData<F>);

impl<F: Field> ExtFieldShare<F> for MulExtFieldShare<F> {
type Base = AdditiveFieldShare<F::BasePrimeField>;
type Ext = AdditiveFieldShare<F>;
}

#[derive(Derivative)]
#[derivative(
Clone(bound = "T:Clone"),
Copy(bound = "T:Copy"),
PartialEq(bound = "T: PartialEq"),
Eq(bound = "T: Eq"),
PartialOrd(bound = "T: PartialOrd"),
Ord(bound = "T: Ord"),
Hash(bound = "T: Hash")
)]
pub struct AdditiveGroupShare<T> {
pub val: T,
}
Expand All @@ -57,16 +132,84 @@ impl<G: Group> Reveal for AdditiveGroupShare<G> {
}
}

macro_rules! impl_group_basics {
($share:ident, $bound:ident) => {
impl<T: $bound> Debug for $share<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
todo!()
}
}
impl<T: $bound> ToBytes for $share<T> {
fn write<W: Write>(&self, writer: W) -> io::Result<()> {
todo!()
}
}
impl<T: $bound> FromBytes for $share<T> {
fn read<R: Read>(reader: R) -> io::Result<Self> {
todo!()
}
}
impl<T: $bound> CanonicalSerialize for $share<T> {
fn serialize<W: Write>(&self, writer: W) -> Result<(), SerializationError> {
todo!()
}

fn serialized_size(&self) -> usize {
todo!()
}
}
impl<T: $bound> CanonicalSerializeWithFlags for $share<T> {
fn serialize_with_flags<W: Write, Fl: Flags>(
&self,
writer: W,
flags: Fl,
) -> Result<(), SerializationError> {
todo!()
}

fn serialized_size_with_flags<Fl: Flags>(&self) -> usize {
todo!()
}
}
impl<T: $bound> CanonicalDeserialize for $share<T> {
fn deserialize<R: Read>(reader: R) -> Result<Self, SerializationError> {
todo!()
}
}
impl<T: $bound> CanonicalDeserializeWithFlags for $share<T> {
fn deserialize_with_flags<R: Read, Fl: Flags>(
reader: R,
) -> Result<(Self, Fl), SerializationError> {
todo!()
}
}
impl<T: $bound> UniformRand for $share<T> {
fn rand<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
todo!()
}
}
};
}

impl_group_basics!(AdditiveGroupShare, Group);

impl<G: Group> GroupShare<G> for AdditiveGroupShare<G> {}

#[derive(Clone, Copy, Debug, Derivative)]
#[derivative(
PartialEq(bound = "E::G1Affine: PartialEq"),
Eq(bound = "E::G1Affine: Eq")
)]
pub struct AdditivePairingShare<E: PairingEngine>(pub PhantomData<E>);

impl<E: PairingEngine> PairingShare<E> for AdditivePairingShare<E> {
impl<E: ExtendedPairingEngine> 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::G1Affine>;
type G2AffineShare = AdditiveGroupShare<E::G2Affine>;
type G1ProjectiveShare = AdditiveGroupShare<E::G1Projective>;
type G2ProjectiveShare = AdditiveGroupShare<E::G2Projective>;
type G1AffineShare = AdditiveGroupShare<E::GroupedG1Affine>;
type G2AffineShare = AdditiveGroupShare<E::GroupedG2Affine>;
type G1ProjectiveShare = AdditiveGroupShare<E::GroupedG1Projective>;
type G2ProjectiveShare = AdditiveGroupShare<E::GroupedG2Projective>;
}
3 changes: 2 additions & 1 deletion src/share/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub trait FieldShare<F: Field>:
{
}

pub trait ExtFieldShare<F: Field> {
pub trait ExtFieldShare<F: Field>: Clone + Copy + Debug + 'static {
type Base: FieldShare<F::BasePrimeField>;
type Ext: FieldShare<F>;
}
44 changes: 42 additions & 2 deletions src/share/group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
use ark_ec::{group::Group, AffineCurve};
use ark_ff::prelude::*;
use ark_ff::ToBytes;
use ark_serialize::{
CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
CanonicalSerializeWithFlags,
};
use std::fmt::Debug;
use std::hash::Hash;

pub trait GroupShare<G: Group> {}
pub trait GroupShare<G: Group>:
Clone
+ Copy
+ Debug
+ Send
+ Sync
+ Eq
+ Hash
+ CanonicalSerialize
+ CanonicalDeserialize
+ CanonicalSerializeWithFlags
+ CanonicalDeserializeWithFlags
+ UniformRand
+ ToBytes
+ 'static
{
}

pub trait GroupAffineShare<G: AffineCurve> {}
// pub trait GroupAffineShare<G: AffineCurve>:
// Clone
// + Copy
// + Debug
// + Send
// + Sync
// + Hash
// + Ord
// + CanonicalSerialize
// + CanonicalDeserialize
// + CanonicalSerializeWithFlags
// + CanonicalDeserializeWithFlags
// + UniformRand
// + ToBytes
// + 'static
// {
// }
60 changes: 53 additions & 7 deletions src/share/pairing.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
use ark_ec::PairingEngine;
use ark_ec::{group::Group, AffineCurve, PairingEngine, ProjectiveCurve};

use super::{
field::{ExtFieldShare, FieldShare},
group::{GroupAffineShare, GroupShare},
group::GroupShare,
};

pub trait PairingShare<E: PairingEngine>: 'static + Send + Sync {
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>;

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 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 PairingShare<E: ExtendedPairingEngine>:
Clone + Copy + Debug + 'static + Send + Sync + PartialEq + Eq
{
type FrShare: FieldShare<E::Fr>;
type FqShare: FieldShare<E::Fq>;
type FqeShare: ExtFieldShare<E::Fqe>;

// warning: maybe wrong
type FqkShare: ExtFieldShare<E::Fqk>;

type G1AffineShare: GroupAffineShare<E::G1Affine>;
type G2AffineShare: GroupAffineShare<E::G2Affine>;
type G1ProjectiveShare: GroupShare<E::G1Projective>;
type G2ProjectiveShare: GroupShare<E::G2Projective>;
// type hoge: E::G1Affine;

type G1AffineShare: GroupShare<E::GroupedG1Affine>;
type G2AffineShare: GroupShare<E::GroupedG2Affine>;
type G1ProjectiveShare: GroupShare<E::GroupedG1Projective>;
type G2ProjectiveShare: GroupShare<E::GroupedG2Projective>;
}
17 changes: 16 additions & 1 deletion src/wire/field.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use derivative::Derivative;
use num_bigint::BigUint;
use std::fmt::{self, Debug, Display};
use std::io::{self, Read, Write};
Expand All @@ -6,7 +7,7 @@ use std::ops::*;
use std::str::FromStr;
use zeroize::Zeroize;

use ark_ff::{prelude::*, FftField};
use ark_ff::{biginteger::*, prelude::*, FftField};
use ark_ff::{FromBytes, ToBytes};
use ark_serialize::{
CanonicalDeserialize, CanonicalDeserializeWithFlags, CanonicalSerialize,
Expand Down Expand Up @@ -388,3 +389,17 @@ where
todo!()
}
}

impl<F: PrimeField, S: FieldShare<F>> SquareRootField for MpcField<F, S> {
fn legendre(&self) -> ark_ff::LegendreSymbol {
todo!()
}

fn sqrt(&self) -> Option<Self> {
todo!()
}

fn sqrt_in_place(&mut self) -> Option<&mut Self> {
todo!()
}
}
Loading

0 comments on commit 098e5ed

Please sign in to comment.