mirror of
https://github.com/arnaucube/ark-curves-cherry-picked.git
synced 2026-01-11 08:21:33 +01:00
Catch up with algebra (#106)
Co-authored-by: Pratyush Mishra <pratyushmishra@berkeley.edu>
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
use ark_ec::{
|
||||
models::{ModelParameters, MontgomeryModelParameters, TEModelParameters},
|
||||
twisted_edwards_extended::{GroupAffine, GroupProjective},
|
||||
models::CurveConfig,
|
||||
twisted_edwards::{Affine, MontCurveConfig, Projective, TECurveConfig},
|
||||
};
|
||||
use ark_ff::MontFp;
|
||||
use ark_ff::{Field, MontFp};
|
||||
|
||||
use crate::{Fq, Fr};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub type EdwardsAffine = GroupAffine<EdwardsParameters>;
|
||||
pub type EdwardsProjective = GroupProjective<EdwardsParameters>;
|
||||
pub type EdwardsAffine = Affine<EdwardsParameters>;
|
||||
pub type EdwardsProjective = Projective<EdwardsParameters>;
|
||||
|
||||
/// `Baby-JubJub` is a twisted Edwards curve. These curves have equations of the
|
||||
/// form: ax² + y² = 1 + dx²y².
|
||||
@@ -22,7 +22,7 @@ pub type EdwardsProjective = GroupProjective<EdwardsParameters>;
|
||||
#[derive(Clone, Default, PartialEq, Eq)]
|
||||
pub struct EdwardsParameters;
|
||||
|
||||
impl ModelParameters for EdwardsParameters {
|
||||
impl CurveConfig for EdwardsParameters {
|
||||
type BaseField = Fq;
|
||||
type ScalarField = Fr;
|
||||
|
||||
@@ -31,15 +31,13 @@ impl ModelParameters for EdwardsParameters {
|
||||
|
||||
/// COFACTOR^(-1) mod r =
|
||||
/// 2394026564107420727433200628387514462817212225638746351800188703329891451411
|
||||
const COFACTOR_INV: Fr = MontFp!(
|
||||
Fr,
|
||||
"2394026564107420727433200628387514462817212225638746351800188703329891451411"
|
||||
);
|
||||
const COFACTOR_INV: Fr =
|
||||
MontFp!("2394026564107420727433200628387514462817212225638746351800188703329891451411");
|
||||
}
|
||||
|
||||
impl TEModelParameters for EdwardsParameters {
|
||||
impl TECurveConfig for EdwardsParameters {
|
||||
/// COEFF_A = 1
|
||||
const COEFF_A: Fq = MontFp!(Fq, "1");
|
||||
const COEFF_A: Fq = Fq::ONE;
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_by_a(elem: &Self::BaseField) -> Self::BaseField {
|
||||
@@ -48,32 +46,26 @@ impl TEModelParameters for EdwardsParameters {
|
||||
|
||||
/// COEFF_D = 168696/168700 mod q
|
||||
/// = 9706598848417545097372247223557719406784115219466060233080913168975159366771
|
||||
const COEFF_D: Fq = MontFp!(
|
||||
Fq,
|
||||
"9706598848417545097372247223557719406784115219466060233080913168975159366771"
|
||||
);
|
||||
const COEFF_D: Fq =
|
||||
MontFp!("9706598848417545097372247223557719406784115219466060233080913168975159366771");
|
||||
|
||||
/// AFFINE_GENERATOR_COEFFS = (GENERATOR_X, GENERATOR_Y)
|
||||
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) = (GENERATOR_X, GENERATOR_Y);
|
||||
const GENERATOR: EdwardsAffine = EdwardsAffine::new_unchecked(GENERATOR_X, GENERATOR_Y);
|
||||
|
||||
type MontgomeryModelParameters = EdwardsParameters;
|
||||
type MontCurveConfig = EdwardsParameters;
|
||||
}
|
||||
|
||||
impl MontgomeryModelParameters for EdwardsParameters {
|
||||
impl MontCurveConfig for EdwardsParameters {
|
||||
/// COEFF_A = 168698
|
||||
const COEFF_A: Fq = MontFp!(Fq, "168698");
|
||||
const COEFF_A: Fq = MontFp!("168698");
|
||||
/// COEFF_B = 168700
|
||||
const COEFF_B: Fq = MontFp!(Fq, "168700");
|
||||
const COEFF_B: Fq = MontFp!("168700");
|
||||
|
||||
type TEModelParameters = EdwardsParameters;
|
||||
type TECurveConfig = EdwardsParameters;
|
||||
}
|
||||
|
||||
const GENERATOR_X: Fq = MontFp!(
|
||||
Fq,
|
||||
"19698561148652590122159747500897617769866003486955115824547446575314762165298"
|
||||
);
|
||||
const GENERATOR_X: Fq =
|
||||
MontFp!("19698561148652590122159747500897617769866003486955115824547446575314762165298");
|
||||
|
||||
const GENERATOR_Y: Fq = MontFp!(
|
||||
Fq,
|
||||
"19298250018296453272277890825869354524455968081175474282777126169995084727839"
|
||||
);
|
||||
const GENERATOR_Y: Fq =
|
||||
MontFp!("19298250018296453272277890825869354524455968081175474282777126169995084727839");
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use ark_algebra_test_templates::{curves::*, groups::*};
|
||||
use ark_algebra_test_templates::curves::*;
|
||||
use ark_ec::{AffineCurve, ProjectiveCurve};
|
||||
use ark_ff::{bytes::FromBytes, Zero};
|
||||
use ark_std::{rand::Rng, str::FromStr, test_rng};
|
||||
use ark_ff::Zero;
|
||||
use ark_std::str::FromStr;
|
||||
|
||||
use crate::*;
|
||||
|
||||
@@ -12,26 +12,6 @@ fn test_projective_curve() {
|
||||
edwards_tests::<EdwardsParameters>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_projective_group() {
|
||||
let mut rng = test_rng();
|
||||
let a = rng.gen();
|
||||
let b = rng.gen();
|
||||
for _i in 0..100 {
|
||||
group_test::<EdwardsProjective>(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_affine_group() {
|
||||
let mut rng = test_rng();
|
||||
let a: EdwardsAffine = rng.gen();
|
||||
let b: EdwardsAffine = rng.gen();
|
||||
for _i in 0..100 {
|
||||
group_test::<EdwardsAffine>(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generator() {
|
||||
let generator = EdwardsAffine::prime_subgroup_generator();
|
||||
@@ -39,22 +19,6 @@ fn test_generator() {
|
||||
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_conversion() {
|
||||
let mut rng = test_rng();
|
||||
let a: EdwardsAffine = rng.gen();
|
||||
let b: EdwardsAffine = rng.gen();
|
||||
let a_b = {
|
||||
use ark_ec::group::Group;
|
||||
(a + &b).double().double()
|
||||
};
|
||||
let a_b2 = (a.into_projective() + &b.into_projective())
|
||||
.double()
|
||||
.double();
|
||||
assert_eq!(a_b, a_b2.into_affine());
|
||||
assert_eq!(a_b.into_projective(), a_b2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scalar_multiplication() {
|
||||
let f1 = Fr::from_str(
|
||||
@@ -85,19 +49,6 @@ fn test_scalar_multiplication() {
|
||||
assert_eq!(f1g.mul(f2).into_affine(), f1f2g);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytes() {
|
||||
let g_from_repr = EdwardsAffine::from_str(
|
||||
"(15863623088992515880085393097393553694825975317405843389771115419751650972659, \
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203)",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let g_bytes = ark_ff::to_bytes![g_from_repr].unwrap();
|
||||
let g = EdwardsAffine::read(g_bytes.as_slice()).unwrap();
|
||||
assert_eq!(g_from_repr, g);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_montgomery_conversion() {
|
||||
montgomery_conversion_test::<EdwardsParameters>();
|
||||
|
||||
Reference in New Issue
Block a user