arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/ed_on_bn254/src/curves/mod.rs
ed_on_bn254/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::{biginteger::BigInteger256, field_new};
  7
  8#[cfg(test)]
  9mod tests;
 10
 11pub type EdwardsAffine = GroupAffine<EdwardsParameters>;
 12pub type EdwardsProjective = GroupProjective<EdwardsParameters>;
 13
 14#[rustfmt::skip]
 15const GENERATOR_X: Fq = field_new!(Fq, BigInteger256([
 16    0x3db6612c2863cc99,
 17    0x8a9e4521b36347dc,
 18    0x310a1a625c16a534,
 19    0x23ceae2710df4a14,
 20]));
 21#[rustfmt::skip]
 22const GENERATOR_Y: Fq = field_new!(Fq, BigInteger256([
 23    0xb83342d20d0201aa,
 24    0x2ffef2f7cdcfeac7,
 25    0xbfa79a9425a6e625,
 26    0xdfb859dc3a44b70,
 27]));
 28
 29/// `Baby-JubJub` is a twisted Edwards curve. These curves have equations of the
 30/// form: ax² + y² = 1 + dx²y².
 31/// over some base finite field Fq.
 32///
 33/// Baby-JubJub's curve equation: x² + y² = 1 + (168696/168700)x²y²
 34///
 35/// q = 21888242871839275222246405745257275088548364400416034343698204186575808495617
 36///
 37#[derive(Clone, Default, PartialEq, Eq)]
 38pub struct EdwardsParameters;
 39
 40impl ModelParameters for EdwardsParameters {
 41    type BaseField = Fq;
 42    type ScalarField = Fr;
 43}
 44
 45impl TEModelParameters for EdwardsParameters {
 46    /// COEFF_A = 1
 47    #[rustfmt::skip]
 48    const COEFF_A: Fq = field_new!(Fq, BigInteger256([
 49        0xac96341c4ffffffb,
 50        0x36fc76959f60cd29,
 51        0x666ea36f7879462e,
 52        0xe0a77c19a07df2f,
 53    ]));
 54
 55    #[inline(always)]
 56    fn mul_by_a(elem: &Self::BaseField) -> Self::BaseField {
 57        *elem
 58    }
 59
 60    /// COEFF_D = 168696/168700 mod q
 61    ///         = 9706598848417545097372247223557719406784115219466060233080913168975159366771
 62    #[rustfmt::skip]
 63    const COEFF_D: Fq = field_new!(Fq, BigInteger256([
 64        0xe7a66d1d9fb08e74,
 65        0xd775bbd5e17629dc,
 66        0x70ccd097286ef1e7,
 67        0x45809398fdf98,
 68    ]));
 69
 70    /// COFACTOR = 8
 71    const COFACTOR: &'static [u64] = &[8];
 72
 73    /// COFACTOR^(-1) mod r =
 74    /// 2394026564107420727433200628387514462817212225638746351800188703329891451411
 75    #[rustfmt::skip]
 76    const COFACTOR_INV: Fr = field_new!(Fr, BigInteger256([
 77        0xfac308b2e25a3d4b,
 78        0xa7c55b66e25b59cb,
 79        0xeccdd46def0f28c5,
 80        0x1c14ef83340fbe5,
 81    ]));
 82
 83    /// AFFINE_GENERATOR_COEFFS = (GENERATOR_X, GENERATOR_Y)
 84    const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) = (GENERATOR_X, GENERATOR_Y);
 85
 86    type MontgomeryModelParameters = EdwardsParameters;
 87}
 88
 89impl MontgomeryModelParameters for EdwardsParameters {
 90    /// COEFF_A = 168698
 91    #[rustfmt::skip]
 92    const COEFF_A: Fq = field_new!(Fq, BigInteger256([
 93        9251058552732279275u64,
 94        16047179255329565110u64,
 95        14708493084570629864u64,
 96        2559515811206512830u64,
 97    ]));
 98    /// COEFF_B = 168700
 99    #[rustfmt::skip]
100    const COEFF_B: Fq = field_new!(Fq, BigInteger256([
101        10785223227458347488u64,
102        2627865112663806840u64,
103        16189334210225400552u64,
104        1096023023792938739u64,
105    ]));
106
107    type TEModelParameters = EdwardsParameters;
108}