Catch up with algebra (#106)

Co-authored-by: Pratyush Mishra <pratyushmishra@berkeley.edu>
This commit is contained in:
Marcin
2022-07-29 21:16:16 +02:00
committed by GitHub
parent 76579d0fbb
commit 93e64df895
120 changed files with 934 additions and 1653 deletions

View File

@@ -1,7 +1,7 @@
//! This module implements the R1CS equivalent of `ark_ed_on_bn254`.
//!
//! It implements field variables for `crate::Fq`,
//! and group variables for `crate::GroupProjective`.
//! and group variables for `crate::Projective`.
//!
//! The field underlying these constraints is `crate::Fq`.
//!

View File

@@ -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");

View File

@@ -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>();

View File

@@ -3,8 +3,7 @@ use ark_algebra_test_templates::{
};
use ark_ff::{
biginteger::BigInteger256 as BigInteger,
bytes::{FromBytes, ToBytes},
fields::{Field, LegendreSymbol::*, PrimeField, SquareRootField},
fields::{Field, LegendreSymbol::*, PrimeField},
One, UniformRand, Zero,
};
use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
@@ -293,22 +292,6 @@ fn test_fq_legendre() {
assert_eq!(QuadraticNonResidue, Fq::from(e).legendre());
}
#[test]
fn test_fq_bytes() {
let f1_from_repr = Fq::from(BigInteger::new([
0xab8a2535947d1a77,
0x9ba74cbfda0bbcda,
0xe928b59724d60baf,
0x1cccaaeb9bb1680a,
]));
let mut f1_bytes = [0u8; 32];
f1_from_repr.write(f1_bytes.as_mut()).unwrap();
let f1 = Fq::read(f1_bytes.as_ref()).unwrap();
assert_eq!(f1_from_repr, f1);
}
#[test]
fn test_fr_add() {
let f1 = Fr::from(BigInteger::new([
@@ -354,21 +337,6 @@ fn test_fr_mul() {
]));
assert_eq!(f1 * &f2, f3);
}
#[test]
fn test_fr_bytes() {
let f1_from_repr = Fr::from(BigInteger::new([
0xc81265fb4130fe0c,
0xb308836c14e22279,
0x699e887f96bff372,
0x84ecc7e76c11ad,
]));
let mut f1_bytes = [0u8; 32];
f1_from_repr.write(f1_bytes.as_mut()).unwrap();
let f1 = Fr::read(f1_bytes.as_ref()).unwrap();
assert_eq!(f1_from_repr, f1);
}
#[test]
fn test_fr_from_str() {