|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -use crate::utils::ScalarFieldFor; |
18 | | -use ark_bls12_381::{G1Affine, G1Projective, G2Affine, G2Projective}; |
19 | | -use ark_ec::{pairing::Pairing, short_weierstrass::SWCurveConfig, AffineRepr}; |
| 17 | +use ark_ec::pairing::Pairing; |
| 18 | +use ark_std::{ops::Neg, Zero}; |
20 | 19 | use sp_ark_bls12_381::{ |
21 | | - Bls12_381 as Bls12_381Opt, G1Affine as G1AffineOpt, G1Projective as G1ProjectiveOpt, |
22 | | - G2Affine as G2AffineOpt, G2Projective as G2ProjectiveOpt, |
| 20 | + Bls12_381 as Bls12_381Opt, G1Affine as G1AffineOpt, G2Affine as G2AffineOpt, |
23 | 21 | }; |
24 | 22 |
|
25 | | -#[inline] |
26 | | -pub fn pairing(a: G1Affine, b: G2Affine) { |
27 | | - let _out = ark_bls12_381::Bls12_381::multi_pairing([a], [b]); |
28 | | -} |
29 | | - |
30 | | -#[inline] |
31 | | -pub fn pairing_opt(a: G1AffineOpt, b: G2AffineOpt) { |
32 | | - let _out = Bls12_381Opt::multi_pairing([a], [b]); |
33 | | -} |
34 | | - |
35 | | -#[inline] |
36 | | -pub fn msm_g1(bases: &[G1Affine], scalars: &[ScalarFieldFor<G1Affine>]) { |
37 | | - let _out = <ark_bls12_381::g1::Config as SWCurveConfig>::msm(bases, scalars); |
38 | | -} |
39 | | - |
40 | | -#[inline] |
41 | | -pub fn msm_g1_opt(bases: &[G1AffineOpt], scalars: &[<G1AffineOpt as AffineRepr>::ScalarField]) { |
42 | | - let _out = <sp_ark_bls12_381::g1::Config as SWCurveConfig>::msm(bases, scalars); |
43 | | -} |
44 | | - |
45 | | -#[inline] |
46 | | -pub fn msm_g2(bases: &[G2Affine], scalars: &[ScalarFieldFor<G2Affine>]) { |
47 | | - let _out = <ark_bls12_381::g2::Config as SWCurveConfig>::msm(bases, scalars); |
48 | | -} |
49 | | - |
50 | | -#[inline] |
51 | | -pub fn msm_g2_opt(bases: &[G2AffineOpt], scalars: &[ScalarFieldFor<G2AffineOpt>]) { |
52 | | - let _out = <sp_ark_bls12_381::g2::Config as SWCurveConfig>::msm(bases, scalars); |
53 | | -} |
54 | | - |
55 | | -#[inline] |
56 | | -pub fn mul_projective_g1(base: &G1Projective, scalar: &[u64]) { |
57 | | - let _out = <ark_bls12_381::g1::Config as SWCurveConfig>::mul_projective(base, scalar); |
58 | | -} |
59 | | - |
60 | | -#[inline] |
61 | | -pub fn mul_projective_g1_opt(base: &G1ProjectiveOpt, scalar: &[u64]) { |
62 | | - let _out = <sp_ark_bls12_381::g1::Config as SWCurveConfig>::mul_projective(base, scalar); |
63 | | -} |
64 | | - |
65 | | -#[inline] |
66 | | -pub fn mul_affine_g1(base: &G1Affine, scalar: &[u64]) { |
67 | | - let _out = <ark_bls12_381::g1::Config as SWCurveConfig>::mul_affine(base, scalar); |
68 | | -} |
69 | | - |
70 | | -#[inline] |
71 | | -pub fn mul_affine_g1_opt(base: &G1AffineOpt, scalar: &[u64]) { |
72 | | - let _out = <sp_ark_bls12_381::g1::Config as SWCurveConfig>::mul_affine(base, scalar); |
73 | | -} |
74 | | - |
75 | | -#[inline] |
76 | | -pub fn mul_projective_g2(base: &G2Projective, scalar: &[u64]) { |
77 | | - let _out = <ark_bls12_381::g2::Config as SWCurveConfig>::mul_projective(base, scalar); |
78 | | -} |
79 | | - |
80 | | -#[inline] |
81 | | -pub fn mul_projective_g2_opt(base: &G2ProjectiveOpt, scalar: &[u64]) { |
82 | | - let _out = <sp_ark_bls12_381::g2::Config as SWCurveConfig>::mul_projective(base, scalar); |
83 | | -} |
84 | | - |
85 | | -#[inline] |
86 | | -pub fn mul_affine_g2(base: &G2Affine, scalar: &[u64]) { |
87 | | - let _out = <ark_bls12_381::g2::Config as SWCurveConfig>::mul_affine(base, scalar); |
88 | | -} |
89 | | - |
90 | | -#[inline] |
91 | | -pub fn mul_affine_g2_opt(base: &G2AffineOpt, scalar: &[u64]) { |
92 | | - let _out = <sp_ark_bls12_381::g2::Config as SWCurveConfig>::mul_affine(base, scalar); |
| 23 | +/// An optimized way to verify Drand pulses from quicket |
| 24 | +/// Instead of computing two pairings and comparing them, we instead compute a multi miller loop, |
| 25 | +/// and then take the final exponentiation, saving a lot of computational cost. |
| 26 | +/// |
| 27 | +/// This function is also inlined as a way to optimize performance. |
| 28 | +/// |
| 29 | +/// * `signature`: |
| 30 | +/// * `q`: |
| 31 | +/// * `msg_on_curve`: The message signed by Drand, hashed to G1 |
| 32 | +/// * `p_pub`: The beacon public key |
| 33 | +#[inline] |
| 34 | +pub fn fast_pairing_opt( |
| 35 | + signature: G1AffineOpt, |
| 36 | + q: G2AffineOpt, |
| 37 | + r: G1AffineOpt, |
| 38 | + s: G2AffineOpt, |
| 39 | +) -> bool { |
| 40 | + let looped = Bls12_381Opt::multi_miller_loop([signature.neg(), r], [q, s]); |
| 41 | + let exp = Bls12_381Opt::final_exponentiation(looped); |
| 42 | + |
| 43 | + match exp { |
| 44 | + Some(e) => e.is_zero(), |
| 45 | + None => false, |
| 46 | + } |
93 | 47 | } |
0 commit comments