pairing: Pass affine references to Engine::pairing

This commit is contained in:
Jack Grigg 2020-05-30 16:56:58 +12:00
parent 57bb18ca6f
commit da2e638c7d
6 changed files with 37 additions and 48 deletions

View File

@ -11,7 +11,7 @@ pub fn prepare_verifying_key<E: Engine>(vk: &VerifyingKey<E>) -> PreparedVerifyi
let delta = vk.delta_g2.neg();
PreparedVerifyingKey {
alpha_g1_beta_g2: E::pairing(vk.alpha_g1, vk.beta_g2),
alpha_g1_beta_g2: E::pairing(&vk.alpha_g1, &vk.beta_g2),
neg_gamma_g2: gamma.prepare(),
neg_delta_g2: delta.prepare(),
ic: vk.ic.clone(),

View File

@ -115,14 +115,14 @@ fn bench_pairing_full(c: &mut Criterion) {
0xe5,
]);
let v: Vec<(G1, G2)> = (0..SAMPLES)
.map(|_| (G1::random(&mut rng), G2::random(&mut rng)))
let v: Vec<(G1Affine, G2Affine)> = (0..SAMPLES)
.map(|_| (G1::random(&mut rng).into(), G2::random(&mut rng).into()))
.collect();
let mut count = 0;
c.bench_function("Full pairing", |b| {
b.iter(|| {
let tmp = Bls12::pairing(v[count].0, v[count].1);
let tmp = Bls12::pairing(&v[count].0, &v[count].1);
count = (count + 1) % SAMPLES;
tmp
})

View File

@ -1141,7 +1141,7 @@ pub mod g1 {
}
fn perform_pairing(&self, other: &G2Affine) -> Fq12 {
super::super::Bls12::pairing(*self, *other)
super::super::Bls12::pairing(self, other)
}
}
@ -1780,7 +1780,7 @@ pub mod g2 {
}
fn perform_pairing(&self, other: &G1Affine) -> Fq12 {
super::super::Bls12::pairing(*other, *self)
super::super::Bls12::pairing(other, self)
}
}

View File

@ -1,5 +1,5 @@
use ff::PrimeField;
use group::{CurveAffine, CurveProjective, Group};
use group::{CurveAffine, CurveProjective};
use super::*;
use crate::*;
@ -23,7 +23,7 @@ fn test_pairing_result_against_relic() {
0F41E58663BF08CF 068672CBD01A7EC7 3BACA4D72CA93544 DEFF686BFD6DF543 D48EAA24AFE47E1E FDE449383B676631
*/
assert_eq!(Bls12::pairing(G1::generator(), G2::generator()), Fq12 {
assert_eq!(Bls12::pairing(&G1Affine::generator(), &G2Affine::generator()), Fq12 {
c0: Fq6 {
c0: Fq2 {
c0: Fq::from_str("2819105605953691245277803056322684086884703000473961065716485506033588504203831029066448642358042597501014294104502").unwrap(),

View File

@ -80,14 +80,10 @@ pub trait Engine: ScalarEngine {
),
>;
/// Performs a complete pairing operation `(p, q)`.
fn pairing<G1, G2>(p: G1, q: G2) -> Self::Gt
where
G1: Into<Self::G1Affine>,
G2: Into<Self::G2Affine>,
{
Self::miller_loop([(&(p.into().prepare()), &(q.into().prepare()))].iter())
.final_exponentiation()
/// 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 {
Self::miller_loop([(&(p.prepare()), &(q.prepare()))].iter()).final_exponentiation()
}
}

View File

@ -17,7 +17,7 @@ pub fn engine_tests<E: Engine>() {
let b = E::G2::random(&mut rng).to_affine();
assert!(a.pairing_with(&b) == b.pairing_with(&a));
assert!(a.pairing_with(&b) == E::pairing(a, b));
assert!(a.pairing_with(&b) == E::pairing(&a, &b));
}
for _ in 0..1000 {
@ -62,13 +62,13 @@ fn random_miller_loop_tests<E: Engine>() {
// Exercise the miller loop for a reduced pairing
for _ in 0..1000 {
let a = E::G1::random(&mut rng);
let b = E::G2::random(&mut rng);
let a = E::G1::random(&mut rng).to_affine();
let b = E::G2::random(&mut rng).to_affine();
let p2 = E::pairing(a, b);
let p2 = E::pairing(&a, &b);
let a = a.to_affine().prepare();
let b = b.to_affine().prepare();
let a = a.prepare();
let b = b.prepare();
let p1 = E::miller_loop(&[(&a, &b)]).final_exponentiation();
@ -77,21 +77,21 @@ fn random_miller_loop_tests<E: Engine>() {
// Exercise a double miller loop
for _ in 0..1000 {
let a = E::G1::random(&mut rng);
let b = E::G2::random(&mut rng);
let c = E::G1::random(&mut rng);
let d = E::G2::random(&mut rng);
let a = E::G1::random(&mut rng).to_affine();
let b = E::G2::random(&mut rng).to_affine();
let c = E::G1::random(&mut rng).to_affine();
let d = E::G2::random(&mut rng).to_affine();
let ab = E::pairing(a, b);
let cd = E::pairing(c, d);
let ab = E::pairing(&a, &b);
let cd = E::pairing(&c, &d);
let mut abcd = ab;
abcd.mul_assign(&cd);
let a = a.to_affine().prepare();
let b = b.to_affine().prepare();
let c = c.to_affine().prepare();
let d = d.to_affine().prepare();
let a = a.prepare();
let b = b.prepare();
let c = c.prepare();
let d = d.prepare();
let abcd_with_double_loop = E::miller_loop(&[(&a, &b), (&c, &d)]).final_exponentiation();
@ -106,26 +106,19 @@ fn random_bilinearity_tests<E: Engine>() {
]);
for _ in 0..1000 {
let a = E::G1::random(&mut rng);
let b = E::G2::random(&mut rng);
let a = E::G1::random(&mut rng).to_affine();
let b = E::G2::random(&mut rng).to_affine();
let c = E::Fr::random(&mut rng);
let d = E::Fr::random(&mut rng);
let mut ac = a;
MulAssign::<&E::Fr>::mul_assign(&mut ac, &c);
let ac = (a * &c).to_affine();
let ad = (a * &d).to_affine();
let bc = (b * &c).to_affine();
let bd = (b * &d).to_affine();
let mut ad = a;
MulAssign::<&E::Fr>::mul_assign(&mut ad, &d);
let mut bc = b;
MulAssign::<&E::Fr>::mul_assign(&mut bc, &c);
let mut bd = b;
MulAssign::<&E::Fr>::mul_assign(&mut bd, &d);
let acbd = E::pairing(ac, bd);
let adbc = E::pairing(ad, bc);
let acbd = E::pairing(&ac, &bd);
let adbc = E::pairing(&ad, &bc);
let mut cd = (c * &d).to_repr();
<E::Fr as PrimeField>::ReprEndianness::toggle_little_endian(&mut cd);
@ -134,7 +127,7 @@ fn random_bilinearity_tests<E: Engine>() {
let mut cd_limbs = [0; 4];
byteorder::LittleEndian::read_u64_into(cd.as_ref(), &mut cd_limbs);
let abcd = E::pairing(a, b).pow_vartime(cd_limbs);
let abcd = E::pairing(&a, &b).pow_vartime(cd_limbs);
assert_eq!(acbd, adbc);
assert_eq!(acbd, abcd);