arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/ed_on_bls12_381/src/curves/mod.rs
ed_on_bls12_381/src/curves/mod.rsRaw
 1use crate::{Fq, Fr};
 2use ark_ec::{
 3    models::{ModelParameters, MontgomeryModelParameters, TEModelParameters},
 4    twisted_edwards_extended::{GroupAffine, GroupProjective},
 5};
 6use ark_ff::field_new;
 7
 8#[cfg(test)]
 9mod tests;
10
11pub type EdwardsAffine = GroupAffine<EdwardsParameters>;
12pub type EdwardsProjective = GroupProjective<EdwardsParameters>;
13
14/// `JubJub` is a twisted Edwards curve. These curves have equations of the
15/// form: ax² + y² = 1 - dx²y².
16/// over some base finite field Fq.
17///
18/// JubJub's curve equation: -x² + y² = 1 - (10240/10241)x²y²
19///
20/// q = 52435875175126190479447740508185965837690552500527637822603658699938581184513.
21///
22/// a = -1.
23/// d = (10240/10241) mod q
24///   = 19257038036680949359750312669786877991949435402254120286184196891950884077233.
25///
26/// Sage script to calculate these:
27///
28/// ```text
29/// q = 52435875175126190479447740508185965837690552500527637822603658699938581184513
30/// Fq = GF(q)
31/// d = -(Fq(10240)/Fq(10241))
32/// ```
33/// These parameters and the sage script obtained from:
34/// <https://github.com/zcash/zcash/issues/2230#issuecomment-317182190>
35#[derive(Clone, Default, PartialEq, Eq)]
36pub struct EdwardsParameters;
37
38impl ModelParameters for EdwardsParameters {
39    type BaseField = Fq;
40    type ScalarField = Fr;
41}
42
43impl TEModelParameters for EdwardsParameters {
44    /// COEFF_A = -1
45    #[rustfmt::skip]
46    const COEFF_A: Fq = field_new!(Fq, "-1");
47
48    /// COEFF_D = (10240/10241) mod q
49    #[rustfmt::skip]
50    const COEFF_D: Fq = field_new!(Fq, "19257038036680949359750312669786877991949435402254120286184196891950884077233");
51
52    /// COFACTOR = 8
53    const COFACTOR: &'static [u64] = &[8];
54
55    /// COFACTOR^(-1) mod r =
56    /// 819310549611346726241370945440405716213240158234039660170669895299022906775
57    #[rustfmt::skip]
58    const COFACTOR_INV: Fr = field_new!(Fr, "819310549611346726241370945440405716213240158234039660170669895299022906775");
59
60    /// AFFINE_GENERATOR_COEFFS = (GENERATOR_X, GENERATOR_Y)
61    const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) = (GENERATOR_X, GENERATOR_Y);
62
63    type MontgomeryModelParameters = EdwardsParameters;
64
65    /// Multiplication by `a` is simply negation here.
66    #[inline(always)]
67    fn mul_by_a(elem: &Self::BaseField) -> Self::BaseField {
68        -(*elem)
69    }
70}
71
72impl MontgomeryModelParameters for EdwardsParameters {
73    /// COEFF_A = 40962
74    #[rustfmt::skip]
75    const COEFF_A: Fq = field_new!(Fq, "40962");
76    /// COEFF_B = -40964
77    #[rustfmt::skip]
78    const COEFF_B: Fq = field_new!(Fq, "-40964");
79
80    type TEModelParameters = EdwardsParameters;
81}
82
83#[rustfmt::skip]
84const GENERATOR_X: Fq = field_new!(Fq, "8076246640662884909881801758704306714034609987455869804520522091855516602923");
85#[rustfmt::skip]
86const GENERATOR_Y: Fq = field_new!(Fq, "13262374693698910701929044844600465831413122818447359594527400194675274060458");