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_bls12_381`.
//!
//! 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,10 +1,7 @@
use ark_ec::{
models::{ModelParameters, MontgomeryModelParameters, TEModelParameters},
short_weierstrass_jacobian::{
GroupAffine as SWGroupAffine, GroupProjective as SWGroupProjective,
},
twisted_edwards_extended::{GroupAffine, GroupProjective},
SWModelParameters,
models::CurveConfig,
short_weierstrass::{self, SWCurveConfig},
twisted_edwards::{Affine, MontCurveConfig, Projective, TECurveConfig},
};
use ark_ff::MontFp;
@@ -13,10 +10,10 @@ use crate::{Fq, Fr};
#[cfg(test)]
mod tests;
pub type EdwardsAffine = GroupAffine<JubjubParameters>;
pub type EdwardsProjective = GroupProjective<JubjubParameters>;
pub type SWAffine = SWGroupAffine<JubjubParameters>;
pub type SWProjective = SWGroupProjective<JubjubParameters>;
pub type EdwardsAffine = Affine<JubjubParameters>;
pub type EdwardsProjective = Projective<JubjubParameters>;
pub type SWAffine = short_weierstrass::Affine<JubjubParameters>;
pub type SWProjective = short_weierstrass::Projective<JubjubParameters>;
/// `JubJub` is a twisted Edwards curve. These curves have equations of the
/// form: ax² + y² = 1 - dx²y².
@@ -56,7 +53,7 @@ pub struct JubjubParameters;
pub type EdwardsParameters = JubjubParameters;
pub type SWParameters = JubjubParameters;
impl ModelParameters for JubjubParameters {
impl CurveConfig for JubjubParameters {
type BaseField = Fq;
type ScalarField = Fr;
@@ -65,26 +62,22 @@ impl ModelParameters for JubjubParameters {
/// COFACTOR^(-1) mod r =
/// 819310549611346726241370945440405716213240158234039660170669895299022906775
const COFACTOR_INV: Fr = MontFp!(
Fr,
"819310549611346726241370945440405716213240158234039660170669895299022906775"
);
const COFACTOR_INV: Fr =
MontFp!("819310549611346726241370945440405716213240158234039660170669895299022906775");
}
impl TEModelParameters for JubjubParameters {
impl TECurveConfig for JubjubParameters {
/// COEFF_A = -1
const COEFF_A: Fq = MontFp!(Fq, "-1");
const COEFF_A: Fq = MontFp!("-1");
/// COEFF_D = (10240/10241) mod q
const COEFF_D: Fq = MontFp!(
Fq,
"19257038036680949359750312669786877991949435402254120286184196891950884077233"
);
const COEFF_D: Fq =
MontFp!("19257038036680949359750312669786877991949435402254120286184196891950884077233");
/// 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 = JubjubParameters;
type MontCurveConfig = JubjubParameters;
/// Multiplication by `a` is simply negation here.
#[inline(always)]
@@ -93,52 +86,39 @@ impl TEModelParameters for JubjubParameters {
}
}
impl MontgomeryModelParameters for JubjubParameters {
impl MontCurveConfig for JubjubParameters {
/// COEFF_A = 40962
const COEFF_A: Fq = MontFp!(Fq, "40962");
const COEFF_A: Fq = MontFp!("40962");
/// COEFF_B = -40964
const COEFF_B: Fq = MontFp!(Fq, "-40964");
const COEFF_B: Fq = MontFp!("-40964");
type TEModelParameters = JubjubParameters;
type TECurveConfig = JubjubParameters;
}
const GENERATOR_X: Fq = MontFp!(
Fq,
"8076246640662884909881801758704306714034609987455869804520522091855516602923"
);
const GENERATOR_X: Fq =
MontFp!("8076246640662884909881801758704306714034609987455869804520522091855516602923");
const GENERATOR_Y: Fq = MontFp!(
Fq,
"13262374693698910701929044844600465831413122818447359594527400194675274060458"
);
const GENERATOR_Y: Fq =
MontFp!("13262374693698910701929044844600465831413122818447359594527400194675274060458");
impl SWModelParameters for JubjubParameters {
impl SWCurveConfig for JubjubParameters {
/// COEFF_A = 52296097456646850916096512823759002727550416093741407922227928430486925478210
const COEFF_A: Self::BaseField = MontFp!(
Fq,
"52296097456646850916096512823759002727550416093741407922227928430486925478210"
);
const COEFF_A: Self::BaseField =
MontFp!("52296097456646850916096512823759002727550416093741407922227928430486925478210");
/// COEFF_B = 48351165704696163914533707656614864561753505123260775585269522553028192119009
const COEFF_B: Self::BaseField = MontFp!(
Fq,
"48351165704696163914533707656614864561753505123260775585269522553028192119009"
);
const COEFF_B: Self::BaseField =
MontFp!("48351165704696163914533707656614864561753505123260775585269522553028192119009");
/// generators
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
(SW_GENERATOR_X, SW_GENERATOR_Y);
const GENERATOR: SWAffine = SWAffine::new_unchecked(SW_GENERATOR_X, SW_GENERATOR_Y);
}
/// x coordinate for SW curve generator
const SW_GENERATOR_X: Fq = MontFp!(
Fq,
"33835869156188682335217394949746694649676633840125476177319971163079011318731"
);
const SW_GENERATOR_X: Fq =
MontFp!("33835869156188682335217394949746694649676633840125476177319971163079011318731");
/// y coordinate for SW curve generator
const SW_GENERATOR_Y: Fq = MontFp!(
Fq,
"43777270878440091394432848052353307184915192688165709016756678962558652055320"
);
const SW_GENERATOR_Y: Fq =
MontFp!("43777270878440091394432848052353307184915192688165709016756678962558652055320");

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::*;
@@ -14,31 +14,6 @@ fn test_projective_curve() {
sw_tests::<JubjubParameters>();
}
#[test]
fn test_projective_group() {
let mut rng = test_rng();
let a = rng.gen();
let b = rng.gen();
let c = rng.gen();
let d = rng.gen();
for _i in 0..100 {
group_test::<EdwardsProjective>(a, b);
group_test::<SWProjective>(c, d);
}
}
#[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() {
// edward curve
@@ -52,22 +27,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(
@@ -98,19 +57,6 @@ fn test_scalar_multiplication() {
assert_eq!(f1g.mul(f2).into_affine(), f1f2g);
}
#[test]
fn test_bytes() {
let g_from_repr = EdwardsAffine::from_str(
"(1158870117176967269192899343636553522971009777237254192973081388797299308391, \
36933624999642413792569726058244472742169727126562409632889593958355839948294)",
)
.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::<JubjubParameters>();

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};
@@ -298,22 +297,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([
@@ -360,22 +343,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() {
let f100_from_repr = Fr::from(BigInteger::new([0x64, 0, 0, 0]));