From 1ab41bbecd2e34d3b3e93dfa825f1092c6403316 Mon Sep 17 00:00:00 2001 From: kilic Date: Thu, 13 Jul 2023 03:05:51 +0300 Subject: [PATCH] Replace `pairing` with `zkcrypto/pairing` (#69) replace local `pairing` with `zkcrypto/pairing` --- Cargo.toml | 1 + src/bn256/engine.rs | 4 +- src/lib.rs | 1 - src/pairing.rs | 94 --------------------------------------------- 4 files changed, 3 insertions(+), 97 deletions(-) delete mode 100644 src/pairing.rs diff --git a/Cargo.toml b/Cargo.toml index 2570d294..91fd9c05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ bincode = "1.3.3" subtle = "2.4" ff = { version = "0.13.0", default-features = false, features = ["std"] } group = "0.13.0" +pairing = "0.23.0" pasta_curves = "0.5.0" static_assertions = "1.1.0" rand = "0.8" diff --git a/src/bn256/engine.rs b/src/bn256/engine.rs index 8ed8725b..028b09fe 100644 --- a/src/bn256/engine.rs +++ b/src/bn256/engine.rs @@ -8,10 +8,10 @@ use crate::bn256::fr::*; use crate::ff::{Field, PrimeField}; use crate::group::cofactor::CofactorCurveAffine; use crate::group::Group; -use crate::pairing::{Engine, MillerLoopResult, MultiMillerLoop, PairingCurveAffine}; use core::borrow::Borrow; use core::iter::Sum; use core::ops::{Add, Mul, MulAssign, Neg, Sub}; +use pairing::{Engine, MillerLoopResult, MultiMillerLoop, PairingCurveAffine}; use rand_core::RngCore; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq}; @@ -633,7 +633,7 @@ pub fn pairing(g1: &G1Affine, g2: &G2Affine) -> Gt { pub struct Bn256; impl Engine for Bn256 { - type Scalar = Fr; + type Fr = Fr; type G1 = G1; type G1Affine = G1Affine; type G2 = G2; diff --git a/src/lib.rs b/src/lib.rs index 63d76a7f..1c950a71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ mod arithmetic; pub mod hash_to_curve; -pub mod pairing; pub mod serde; pub mod bn256; diff --git a/src/pairing.rs b/src/pairing.rs deleted file mode 100644 index b90be998..00000000 --- a/src/pairing.rs +++ /dev/null @@ -1,94 +0,0 @@ -use crate::CurveAffine; -use core::ops::Mul; -use ff::PrimeField; -use group::{ - prime::PrimeCurve, Group, GroupOps, GroupOpsOwned, ScalarMul, ScalarMulOwned, - UncompressedEncoding, -}; - -pub trait Engine: Sized + 'static + Clone { - /// This is the scalar field of the engine's groups. - type Scalar: PrimeField; - - /// The projective representation of an element in G1. - type G1: PrimeCurve - + From - + GroupOps - + GroupOpsOwned - + ScalarMul - + ScalarMulOwned - + Group; - - /// The affine representation of an element in G1. - type G1Affine: PairingCurveAffine< - ScalarExt = Self::Scalar, - CurveExt = Self::G1, - Pair = Self::G2Affine, - PairingResult = Self::Gt, - > + From - + Mul - + for<'a> Mul<&'a Self::Scalar, Output = Self::G1>; - - /// The projective representation of an element in G2. - type G2: PrimeCurve - + From - + GroupOps - + GroupOpsOwned - + ScalarMul - + ScalarMulOwned; - - /// The affine representation of an element in G2. - type G2Affine: PairingCurveAffine< - ScalarExt = Self::Scalar, - CurveExt = Self::G2, - Pair = Self::G1Affine, - PairingResult = Self::Gt, - > + From - + Mul - + for<'a> Mul<&'a Self::Scalar, Output = Self::G2>; - - /// The extension field that hosts the target group of the pairing. - type Gt: Group + ScalarMul + ScalarMulOwned; - - /// Invoke the pairing function `G1 x G2 -> Gt` without the use of precomputation and - /// other optimizations. - fn pairing(p: &Self::G1Affine, q: &Self::G2Affine) -> Self::Gt; -} - -/// Affine representation of an elliptic curve point that can be used -/// to perform pairings. -pub trait PairingCurveAffine: CurveAffine + UncompressedEncoding { - type Pair: PairingCurveAffine; - type PairingResult: Group; - - /// Perform a pairing - fn pairing_with(&self, other: &Self::Pair) -> Self::PairingResult; -} - -/// An engine that can compute sums of pairings in an efficient way. -pub trait MultiMillerLoop: Engine { - /// The prepared form of `Self::G2Affine`. - type G2Prepared: Clone + Send + Sync + From; - - /// The type returned by `Engine::miller_loop`. - type Result: MillerLoopResult; - - /// Computes $$\sum_{i=1}^n \textbf{ML}(a_i, b_i)$$ given a series of terms - /// $$(a_1, b_1), (a_2, b_2), ..., (a_n, b_n).$$ - fn multi_miller_loop(terms: &[(&Self::G1Affine, &Self::G2Prepared)]) -> Self::Result; -} - -/// Represents results of a Miller loop, one of the most expensive portions of the pairing -/// function. -/// -/// `MillerLoopResult`s cannot be compared with each other until -/// [`MillerLoopResult::final_exponentiation`] is called, which is also expensive. -pub trait MillerLoopResult { - /// The extension field that hosts the target group of the pairing. - type Gt: Group; - - /// This performs a "final exponentiation" routine to convert the result of a Miller - /// loop into an element of [`MillerLoopResult::Gt`], so that it can be compared with - /// other elements of `Gt`. - fn final_exponentiation(&self) -> Self::Gt; -}