pairing: Pass affine references to Engine::pairing
This commit is contained in:
parent
57bb18ca6f
commit
da2e638c7d
|
@ -11,7 +11,7 @@ pub fn prepare_verifying_key<E: Engine>(vk: &VerifyingKey<E>) -> PreparedVerifyi
|
||||||
let delta = vk.delta_g2.neg();
|
let delta = vk.delta_g2.neg();
|
||||||
|
|
||||||
PreparedVerifyingKey {
|
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_gamma_g2: gamma.prepare(),
|
||||||
neg_delta_g2: delta.prepare(),
|
neg_delta_g2: delta.prepare(),
|
||||||
ic: vk.ic.clone(),
|
ic: vk.ic.clone(),
|
||||||
|
|
|
@ -115,14 +115,14 @@ fn bench_pairing_full(c: &mut Criterion) {
|
||||||
0xe5,
|
0xe5,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let v: Vec<(G1, G2)> = (0..SAMPLES)
|
let v: Vec<(G1Affine, G2Affine)> = (0..SAMPLES)
|
||||||
.map(|_| (G1::random(&mut rng), G2::random(&mut rng)))
|
.map(|_| (G1::random(&mut rng).into(), G2::random(&mut rng).into()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
c.bench_function("Full pairing", |b| {
|
c.bench_function("Full pairing", |b| {
|
||||||
b.iter(|| {
|
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;
|
count = (count + 1) % SAMPLES;
|
||||||
tmp
|
tmp
|
||||||
})
|
})
|
||||||
|
|
|
@ -1141,7 +1141,7 @@ pub mod g1 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn perform_pairing(&self, other: &G2Affine) -> Fq12 {
|
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 {
|
fn perform_pairing(&self, other: &G1Affine) -> Fq12 {
|
||||||
super::super::Bls12::pairing(*other, *self)
|
super::super::Bls12::pairing(other, self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use ff::PrimeField;
|
use ff::PrimeField;
|
||||||
use group::{CurveAffine, CurveProjective, Group};
|
use group::{CurveAffine, CurveProjective};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
@ -23,7 +23,7 @@ fn test_pairing_result_against_relic() {
|
||||||
0F41E58663BF08CF 068672CBD01A7EC7 3BACA4D72CA93544 DEFF686BFD6DF543 D48EAA24AFE47E1E FDE449383B676631
|
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: Fq6 {
|
||||||
c0: Fq2 {
|
c0: Fq2 {
|
||||||
c0: Fq::from_str("2819105605953691245277803056322684086884703000473961065716485506033588504203831029066448642358042597501014294104502").unwrap(),
|
c0: Fq::from_str("2819105605953691245277803056322684086884703000473961065716485506033588504203831029066448642358042597501014294104502").unwrap(),
|
||||||
|
|
|
@ -80,14 +80,10 @@ pub trait Engine: ScalarEngine {
|
||||||
),
|
),
|
||||||
>;
|
>;
|
||||||
|
|
||||||
/// Performs a complete pairing operation `(p, q)`.
|
/// Invoke the pairing function `G1 x G2 -> Gt` without the use of precomputation and
|
||||||
fn pairing<G1, G2>(p: G1, q: G2) -> Self::Gt
|
/// other optimizations.
|
||||||
where
|
fn pairing(p: &Self::G1Affine, q: &Self::G2Affine) -> Self::Gt {
|
||||||
G1: Into<Self::G1Affine>,
|
Self::miller_loop([(&(p.prepare()), &(q.prepare()))].iter()).final_exponentiation()
|
||||||
G2: Into<Self::G2Affine>,
|
|
||||||
{
|
|
||||||
Self::miller_loop([(&(p.into().prepare()), &(q.into().prepare()))].iter())
|
|
||||||
.final_exponentiation()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ pub fn engine_tests<E: Engine>() {
|
||||||
let b = E::G2::random(&mut rng).to_affine();
|
let b = E::G2::random(&mut rng).to_affine();
|
||||||
|
|
||||||
assert!(a.pairing_with(&b) == b.pairing_with(&a));
|
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 {
|
for _ in 0..1000 {
|
||||||
|
@ -62,13 +62,13 @@ fn random_miller_loop_tests<E: Engine>() {
|
||||||
|
|
||||||
// Exercise the miller loop for a reduced pairing
|
// Exercise the miller loop for a reduced pairing
|
||||||
for _ in 0..1000 {
|
for _ in 0..1000 {
|
||||||
let a = E::G1::random(&mut rng);
|
let a = E::G1::random(&mut rng).to_affine();
|
||||||
let b = E::G2::random(&mut rng);
|
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 a = a.prepare();
|
||||||
let b = b.to_affine().prepare();
|
let b = b.prepare();
|
||||||
|
|
||||||
let p1 = E::miller_loop(&[(&a, &b)]).final_exponentiation();
|
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
|
// Exercise a double miller loop
|
||||||
for _ in 0..1000 {
|
for _ in 0..1000 {
|
||||||
let a = E::G1::random(&mut rng);
|
let a = E::G1::random(&mut rng).to_affine();
|
||||||
let b = E::G2::random(&mut rng);
|
let b = E::G2::random(&mut rng).to_affine();
|
||||||
let c = E::G1::random(&mut rng);
|
let c = E::G1::random(&mut rng).to_affine();
|
||||||
let d = E::G2::random(&mut rng);
|
let d = E::G2::random(&mut rng).to_affine();
|
||||||
|
|
||||||
let ab = E::pairing(a, b);
|
let ab = E::pairing(&a, &b);
|
||||||
let cd = E::pairing(c, d);
|
let cd = E::pairing(&c, &d);
|
||||||
|
|
||||||
let mut abcd = ab;
|
let mut abcd = ab;
|
||||||
abcd.mul_assign(&cd);
|
abcd.mul_assign(&cd);
|
||||||
|
|
||||||
let a = a.to_affine().prepare();
|
let a = a.prepare();
|
||||||
let b = b.to_affine().prepare();
|
let b = b.prepare();
|
||||||
let c = c.to_affine().prepare();
|
let c = c.prepare();
|
||||||
let d = d.to_affine().prepare();
|
let d = d.prepare();
|
||||||
|
|
||||||
let abcd_with_double_loop = E::miller_loop(&[(&a, &b), (&c, &d)]).final_exponentiation();
|
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 {
|
for _ in 0..1000 {
|
||||||
let a = E::G1::random(&mut rng);
|
let a = E::G1::random(&mut rng).to_affine();
|
||||||
let b = E::G2::random(&mut rng);
|
let b = E::G2::random(&mut rng).to_affine();
|
||||||
|
|
||||||
let c = E::Fr::random(&mut rng);
|
let c = E::Fr::random(&mut rng);
|
||||||
let d = E::Fr::random(&mut rng);
|
let d = E::Fr::random(&mut rng);
|
||||||
|
|
||||||
let mut ac = a;
|
let ac = (a * &c).to_affine();
|
||||||
MulAssign::<&E::Fr>::mul_assign(&mut ac, &c);
|
let ad = (a * &d).to_affine();
|
||||||
|
let bc = (b * &c).to_affine();
|
||||||
|
let bd = (b * &d).to_affine();
|
||||||
|
|
||||||
let mut ad = a;
|
let acbd = E::pairing(&ac, &bd);
|
||||||
MulAssign::<&E::Fr>::mul_assign(&mut ad, &d);
|
let adbc = E::pairing(&ad, &bc);
|
||||||
|
|
||||||
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 mut cd = (c * &d).to_repr();
|
let mut cd = (c * &d).to_repr();
|
||||||
<E::Fr as PrimeField>::ReprEndianness::toggle_little_endian(&mut cd);
|
<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];
|
let mut cd_limbs = [0; 4];
|
||||||
byteorder::LittleEndian::read_u64_into(cd.as_ref(), &mut cd_limbs);
|
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, adbc);
|
||||||
assert_eq!(acbd, abcd);
|
assert_eq!(acbd, abcd);
|
||||||
|
|
Loading…
Reference in New Issue