arnaucube
arnaucube/arkeddsaPublic Self-hosted repository
arkeddsa/src/ed_on_bn254_twist.rs
src/ed_on_bn254_twist.rsRaw
  1use ark_ec::{
  2    models::CurveConfig,
  3    twisted_edwards::{Affine, MontCurveConfig, Projective, TECurveConfig},
  4};
  5use ark_ff::MontFp;
  6pub type EdwardsAffine = Affine<EdwardsConfig>;
  7pub type EdwardsProjective = Projective<EdwardsConfig>;
  8
  9/// Twist of `Baby-JubJub` is a twist of twisted Edwards curve. These curves have equations of the
 10/// form: ax² + y² = 1 + dx²y².
 11/// over some base finite field BaseField.
 12///
 13/// q = 21888242871839275222246405745257275088548364400416034343698204186575808495617
 14#[derive(Clone, Default, PartialEq, Eq)]
 15pub struct EdwardsConfig;
 16
 17#[cfg(test)]
 18ark_algebra_test_templates::test_group!(te; EdwardsProjective; te);
 19
 20impl CurveConfig for EdwardsConfig {
 21    type BaseField = ark_ed_on_bn254::Fq;
 22    type ScalarField = ark_ed_on_bn254::Fr;
 23
 24    /// COFACTOR = 8
 25    const COFACTOR: &'static [u64] = &[8];
 26
 27    /// COFACTOR^(-1) mod r =
 28    /// 2394026564107420727433200628387514462817212225638746351800188703329891451411
 29    const COFACTOR_INV: ark_ed_on_bn254::Fr =
 30        MontFp!("2394026564107420727433200628387514462817212225638746351800188703329891451411");
 31}
 32
 33impl TECurveConfig for EdwardsConfig {
 34    const COEFF_A: ark_ed_on_bn254::Fq = MontFp!("168700");
 35
 36    #[inline(always)]
 37    fn mul_by_a(elem: Self::BaseField) -> Self::BaseField {
 38        elem * <Self as TECurveConfig>::COEFF_A
 39    }
 40
 41    const COEFF_D: ark_ed_on_bn254::Fq = MontFp!("168696");
 42
 43    const GENERATOR: EdwardsAffine = EdwardsAffine::new_unchecked(GENERATOR_X, GENERATOR_Y);
 44
 45    type MontCurveConfig = EdwardsConfig;
 46}
 47
 48impl MontCurveConfig for EdwardsConfig {
 49    /// COEFF_A = 168698
 50    const COEFF_A: ark_ed_on_bn254::Fq = MontFp!("168698");
 51    /// COEFF_B = 168700
 52    const COEFF_B: ark_ed_on_bn254::Fq = MontFp!("1");
 53
 54    type TECurveConfig = EdwardsConfig;
 55}
 56
 57/// GENERATOR_X =
 58/// 19698561148652590122159747500897617769866003486955115824547446575314762165298
 59pub const GENERATOR_X: ark_ed_on_bn254::Fq =
 60    MontFp!("5299619240641551281634865583518297030282874472190772894086521144482721001553");
 61
 62/// GENERATOR_Y =
 63/// 19298250018296453272277890825869354524455968081175474282777126169995084727839
 64pub const GENERATOR_Y: ark_ed_on_bn254::Fq =
 65    MontFp!("16950150798460657717958625567821834550301663161624707787222815936182638968203");
 66
 67#[test]
 68fn test_twist() {
 69    fn twist(twist: ark_ed_on_bn254::EdwardsAffine) -> EdwardsAffine {
 70        use ark_ff::Field;
 71        let inv_sqrt_a = MontFp!("168700").sqrt().unwrap().inverse().unwrap();
 72        EdwardsAffine {
 73            x: twist.x * inv_sqrt_a,
 74            y: twist.y,
 75        }
 76    }
 77
 78    fn untwist(curve: EdwardsAffine) -> ark_ed_on_bn254::EdwardsAffine {
 79        use ark_ff::Field;
 80        const A: ark_ed_on_bn254::Fq = MontFp!("168700");
 81        let sqrt_a = A.sqrt().unwrap();
 82        ark_ed_on_bn254::EdwardsAffine {
 83            x: curve.x * sqrt_a,
 84            y: curve.y,
 85        }
 86    }
 87
 88    use ark_ec::{AffineRepr, CurveGroup};
 89    use ark_ed_on_bn254::Fr;
 90    use ark_std::UniformRand;
 91    use rand_core::OsRng;
 92
 93    let v0: ark_ed_on_bn254::EdwardsAffine = ark_ed_on_bn254::EdwardsAffine::generator();
 94    let u0: crate::ed_on_bn254_twist::EdwardsAffine = twist(v0);
 95    assert!(u0.is_on_curve());
 96    assert!(u0.is_in_correct_subgroup_assuming_on_curve());
 97    let x = Fr::rand(&mut OsRng);
 98    let u1 = (u0 * x).into_affine();
 99    let v1 = (v0 * x).into_affine();
100    let v2 = untwist(u1);
101    assert_eq!(v1, v2);
102}