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

add Canonical(De-)Serialize to miller loop args #571

Closed
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
- `MNT6Parameters` → `MNT6Config`
- `GLVParameters` → `GLVConfig`
- [\#557](https://github.com/arkworks-rs/algebra/pull/557) (`ark-ff`) Change `frobenius_map` to return the result, instead of mutating the input. Add `frobenius_map_in_place` for the old behavior.
- [\#537](https://github.com/arkworks-rs/algebra/pull/571) (`ark-ec`) Add `CanonicalSerialize` and `CanonicalDeserialize` traits to the arguments of `multi_miller_loop`.

### Features

Expand Down
17 changes: 13 additions & 4 deletions ec/src/models/bls12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ark_ff::{
},
BitIteratorBE, CyclotomicMultSubgroup, Field, PrimeField,
};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{marker::PhantomData, vec::Vec};
use num_traits::{One, Zero};

Expand Down Expand Up @@ -44,8 +45,12 @@ pub trait Bls12Config: 'static + Sized {
>;

fn multi_miller_loop(
a: impl IntoIterator<Item = impl Into<G1Prepared<Self>>>,
b: impl IntoIterator<Item = impl Into<G2Prepared<Self>>>,
a: impl IntoIterator<
Item = impl Into<G1Prepared<Self>> + CanonicalSerialize + CanonicalDeserialize,
>,
b: impl IntoIterator<
Item = impl Into<G2Prepared<Self>> + CanonicalSerialize + CanonicalDeserialize,
>,
) -> MillerLoopOutput<Bls12<Self>> {
use itertools::Itertools;

Expand Down Expand Up @@ -211,8 +216,12 @@ impl<P: Bls12Config> Pairing for Bls12<P> {
type TargetField = Fp12<P::Fp12Config>;

fn multi_miller_loop(
a: impl IntoIterator<Item = impl Into<Self::G1Prepared>>,
b: impl IntoIterator<Item = impl Into<Self::G2Prepared>>,
a: impl IntoIterator<
Item = impl Into<Self::G1Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
b: impl IntoIterator<
Item = impl Into<Self::G2Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
) -> MillerLoopOutput<Self> {
P::multi_miller_loop(a, b)
}
Expand Down
9 changes: 7 additions & 2 deletions ec/src/models/bn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ark_ff::{
},
CyclotomicMultSubgroup,
};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{marker::PhantomData, vec::Vec};
use itertools::Itertools;
use num_traits::One;
Expand Down Expand Up @@ -102,8 +103,12 @@ impl<P: BnConfig> Pairing for Bn<P> {
type TargetField = Fp12<P::Fp12Config>;

fn multi_miller_loop(
a: impl IntoIterator<Item = impl Into<Self::G1Prepared>>,
b: impl IntoIterator<Item = impl Into<Self::G2Prepared>>,
a: impl IntoIterator<
Item = impl Into<Self::G1Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
b: impl IntoIterator<
Item = impl Into<Self::G2Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
) -> MillerLoopOutput<Self> {
let mut pairs = a
.into_iter()
Expand Down
17 changes: 13 additions & 4 deletions ec/src/models/bw6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ark_ff::{
},
BitIteratorBE, CyclotomicMultSubgroup,
};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use itertools::Itertools;
use num_traits::One;

Expand Down Expand Up @@ -52,8 +53,12 @@ pub trait BW6Config: 'static + Eq + Sized {
}

fn multi_miller_loop(
a: impl IntoIterator<Item = impl Into<G1Prepared<Self>>>,
b: impl IntoIterator<Item = impl Into<G2Prepared<Self>>>,
a: impl IntoIterator<
Item = impl Into<G1Prepared<Self>> + CanonicalSerialize + CanonicalDeserialize,
>,
b: impl IntoIterator<
Item = impl Into<G2Prepared<Self>> + CanonicalSerialize + CanonicalDeserialize,
>,
) -> MillerLoopOutput<BW6<Self>> {
// Alg.5 in https://eprint.iacr.org/2020/351.pdf

Expand Down Expand Up @@ -311,8 +316,12 @@ impl<P: BW6Config> Pairing for BW6<P> {
}

fn multi_miller_loop(
a: impl IntoIterator<Item = impl Into<Self::G1Prepared>>,
b: impl IntoIterator<Item = impl Into<Self::G2Prepared>>,
a: impl IntoIterator<
Item = impl Into<Self::G1Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
b: impl IntoIterator<
Item = impl Into<Self::G2Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
) -> MillerLoopOutput<Self> {
P::multi_miller_loop(a, b)
}
Expand Down
9 changes: 7 additions & 2 deletions ec/src/models/mnt4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ark_ff::{
use itertools::Itertools;
use num_traits::{One, Zero};

use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{marker::PhantomData, vec::Vec};

#[cfg(feature = "parallel")]
Expand Down Expand Up @@ -208,8 +209,12 @@ impl<P: MNT4Config> Pairing for MNT4<P> {
type TargetField = Fp4<P::Fp4Config>;

fn multi_miller_loop(
a: impl IntoIterator<Item = impl Into<Self::G1Prepared>>,
b: impl IntoIterator<Item = impl Into<Self::G2Prepared>>,
a: impl IntoIterator<
Item = impl Into<Self::G1Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
b: impl IntoIterator<
Item = impl Into<Self::G2Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
) -> MillerLoopOutput<Self> {
let pairs = a
.into_iter()
Expand Down
9 changes: 7 additions & 2 deletions ec/src/models/mnt6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ark_ff::{
use itertools::Itertools;
use num_traits::{One, Zero};

use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{marker::PhantomData, vec::Vec};

#[cfg(feature = "parallel")]
Expand Down Expand Up @@ -214,8 +215,12 @@ impl<P: MNT6Config> Pairing for MNT6<P> {
type TargetField = Fp6<P::Fp6Config>;

fn multi_miller_loop(
a: impl IntoIterator<Item = impl Into<Self::G1Prepared>>,
b: impl IntoIterator<Item = impl Into<Self::G2Prepared>>,
a: impl IntoIterator<
Item = impl Into<Self::G1Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
b: impl IntoIterator<
Item = impl Into<Self::G2Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
) -> MillerLoopOutput<Self> {
let pairs = a
.into_iter()
Expand Down
32 changes: 22 additions & 10 deletions ec/src/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub trait Pairing: Sized + 'static + Copy + Debug + Sync + Send + Eq {
type G1Affine: AffineRepr<Group = Self::G1, ScalarField = Self::ScalarField>
+ From<Self::G1>
+ Into<Self::G1>
+ Into<Self::G1Prepared>;
+ Into<Self::G1Prepared>
+ CanonicalSerialize
+ CanonicalDeserialize;

/// A G1 element that has been preprocessed for use in a pairing.
type G1Prepared: Default
Expand All @@ -63,7 +65,9 @@ pub trait Pairing: Sized + 'static + Copy + Debug + Sync + Send + Eq {
type G2Affine: AffineRepr<Group = Self::G2, ScalarField = Self::ScalarField>
+ From<Self::G2>
+ Into<Self::G2>
+ Into<Self::G2Prepared>;
+ Into<Self::G2Prepared>
+ CanonicalSerialize
+ CanonicalDeserialize;

/// A G2 element that has been preprocessed for use in a pairing.
type G2Prepared: Default
Expand All @@ -83,14 +87,18 @@ pub trait Pairing: Sized + 'static + Copy + Debug + Sync + Send + Eq {

/// Computes the product of Miller loops for some number of (G1, G2) pairs.
fn multi_miller_loop(
a: impl IntoIterator<Item = impl Into<Self::G1Prepared>>,
b: impl IntoIterator<Item = impl Into<Self::G2Prepared>>,
a: impl IntoIterator<
Item = impl Into<Self::G1Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
b: impl IntoIterator<
Item = impl Into<Self::G2Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
) -> MillerLoopOutput<Self>;

/// Computes the Miller loop over `a` and `b`.
fn miller_loop(
a: impl Into<Self::G1Prepared>,
b: impl Into<Self::G2Prepared>,
a: impl Into<Self::G1Prepared> + CanonicalSerialize + CanonicalDeserialize,
b: impl Into<Self::G2Prepared> + CanonicalSerialize + CanonicalDeserialize,
) -> MillerLoopOutput<Self> {
Self::multi_miller_loop([a], [b])
}
Expand All @@ -101,16 +109,20 @@ pub trait Pairing: Sized + 'static + Copy + Debug + Sync + Send + Eq {

/// Computes a "product" of pairings.
fn multi_pairing(
a: impl IntoIterator<Item = impl Into<Self::G1Prepared>>,
b: impl IntoIterator<Item = impl Into<Self::G2Prepared>>,
a: impl IntoIterator<
Item = impl Into<Self::G1Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
b: impl IntoIterator<
Item = impl Into<Self::G2Prepared> + CanonicalSerialize + CanonicalDeserialize,
>,
) -> PairingOutput<Self> {
Self::final_exponentiation(Self::multi_miller_loop(a, b)).unwrap()
}

/// Performs multiple pairing operations
fn pairing(
p: impl Into<Self::G1Prepared>,
q: impl Into<Self::G2Prepared>,
p: impl Into<Self::G1Prepared> + CanonicalSerialize + CanonicalDeserialize,
q: impl Into<Self::G2Prepared> + CanonicalSerialize + CanonicalDeserialize,
) -> PairingOutput<Self> {
Self::multi_pairing([p], [q])
}
Expand Down
3 changes: 1 addition & 2 deletions ff/src/fields/models/fp/montgomery_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,7 @@ impl<T: MontConfig<N>, const N: usize> Fp<MontBackend<T, N>, N> {

#[cfg(test)]
mod test {
use ark_std::str::FromStr;
use ark_std::vec::Vec;
use ark_std::{str::FromStr, vec::Vec};
use ark_test_curves::secp256k1::Fr;
use num_bigint::{BigInt, BigUint, Sign};

Expand Down
1 change: 0 additions & 1 deletion test-curves/src/bls12_381/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use ark_ec::{
AffineRepr, CurveGroup, Group,
};
use ark_ff::{BigInt, Field, MontFp, Zero};

pub type G2Affine = bls12::G2Affine<crate::bls12_381::Config>;
pub type G2Projective = bls12::G2Projective<crate::bls12_381::Config>;

Expand Down
3 changes: 1 addition & 2 deletions test-templates/src/h2c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ macro_rules! test_h2c {
fs::{read_dir, File},
io::BufReader,
};
use $crate::decode;
use $crate::Sha256;
use $crate::{decode, Sha256};

use $crate::json::SuiteVector;
#[test]
Expand Down
2 changes: 1 addition & 1 deletion test-templates/src/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ macro_rules! test_pairing {
let c = <$Pairing as Pairing>::G1::rand(rng).into_affine();
let d = <$Pairing as Pairing>::G2::rand(rng).into_affine();
let ans1 = <$Pairing>::pairing(a, b) + &<$Pairing>::pairing(c, d);
let ans2 = <$Pairing>::multi_pairing(&[a, c], &[b, d]);
let ans2 = <$Pairing>::multi_pairing([a, c], [b, d]);
assert_eq!(ans1, ans2);
}
}
Expand Down