Skip to main content

ark_models_ext/models/bn/
mod.rs

1pub use ark_ec::models::bn::TwistType;
2use ark_ec::{
3    models::CurveConfig,
4    pairing::{MillerLoopOutput, Pairing, PairingOutput},
5};
6use ark_ff::{
7    fields::{
8        fp12_2over3over2::{Fp12, Fp12Config},
9        fp2::Fp2Config,
10        fp6_3over2::Fp6Config,
11        Fp2,
12    },
13    PrimeField,
14};
15use ark_std::marker::PhantomData;
16use derivative::Derivative;
17
18use crate::models::short_weierstrass::SWCurveConfig;
19
20pub trait BnConfig: 'static + Sized {
21    /// The absolute value of the BN curve parameter `X`
22    /// (as a multiple of six the `X` used in the paper).
23    const X: &'static [u64];
24    /// Whether or not `X` is negative.
25    const X_IS_NEGATIVE: bool;
26    /// What kind of twist is this?
27    const TWIST_TYPE: TwistType;
28
29    type Fp: PrimeField + Into<<Self::Fp as PrimeField>::BigInt>;
30    type Fp2Config: Fp2Config<Fp = Self::Fp>;
31    type Fp6Config: Fp6Config<Fp2Config = Self::Fp2Config>;
32    type Fp12Config: Fp12Config<Fp6Config = Self::Fp6Config>;
33    type G1Config: SWCurveConfig<BaseField = Self::Fp>;
34    type G2Config: SWCurveConfig<
35        BaseField = Fp2<Self::Fp2Config>,
36        ScalarField = <Self::G1Config as CurveConfig>::ScalarField,
37    >;
38
39    fn multi_miller_loop(
40        a_vec: impl IntoIterator<Item = impl Into<G1Prepared<Self>>>,
41        b_vec: impl IntoIterator<Item = impl Into<G2Prepared<Self>>>,
42    ) -> MillerLoopOutput<Bn<Self>>;
43
44    fn final_exponentiation(f: MillerLoopOutput<Bn<Self>>) -> Option<PairingOutput<Bn<Self>>>;
45}
46
47pub mod g1;
48pub mod g2;
49
50pub use self::{
51    g1::{G1Affine, G1Prepared, G1Projective},
52    g2::{G2Affine, G2Prepared, G2Projective},
53};
54
55#[derive(Derivative)]
56#[derivative(Copy, Clone, PartialEq, Eq, Debug, Hash)]
57pub struct Bn<P: BnConfig>(PhantomData<fn() -> P>);
58
59impl<P: BnConfig> Pairing for Bn<P> {
60    type BaseField = <P::G1Config as CurveConfig>::BaseField;
61    type ScalarField = <P::G1Config as CurveConfig>::ScalarField;
62    type G1 = G1Projective<P>;
63    type G1Affine = G1Affine<P>;
64    type G1Prepared = G1Prepared<P>;
65    type G2 = G2Projective<P>;
66    type G2Affine = G2Affine<P>;
67    type G2Prepared = G2Prepared<P>;
68    type TargetField = Fp12<P::Fp12Config>;
69
70    fn multi_miller_loop(
71        a: impl IntoIterator<Item = impl Into<Self::G1Prepared>>,
72        b: impl IntoIterator<Item = impl Into<Self::G2Prepared>>,
73    ) -> MillerLoopOutput<Self> {
74        P::multi_miller_loop(a, b)
75    }
76
77    fn final_exponentiation(f: MillerLoopOutput<Self>) -> Option<PairingOutput<Self>> {
78        P::final_exponentiation(f)
79    }
80}