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_bandersnatch`.
//!
//! 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::{Field, MontFp};
@@ -13,11 +10,11 @@ use crate::{Fq, Fr};
#[cfg(test)]
mod tests;
pub type EdwardsAffine = GroupAffine<BandersnatchParameters>;
pub type EdwardsProjective = GroupProjective<BandersnatchParameters>;
pub type EdwardsAffine = Affine<BandersnatchParameters>;
pub type EdwardsProjective = Projective<BandersnatchParameters>;
pub type SWAffine = SWGroupAffine<BandersnatchParameters>;
pub type SWProjective = SWGroupProjective<BandersnatchParameters>;
pub type SWAffine = short_weierstrass::Affine<BandersnatchParameters>;
pub type SWProjective = short_weierstrass::Projective<BandersnatchParameters>;
/// `bandersnatch` is a twisted Edwards curve. These curves have equations of
/// the form: ax² + y² = 1 - dx²y².
@@ -57,7 +54,7 @@ pub struct BandersnatchParameters;
pub type EdwardsParameters = BandersnatchParameters;
pub type SWParameters = BandersnatchParameters;
impl ModelParameters for BandersnatchParameters {
impl CurveConfig for BandersnatchParameters {
type BaseField = Fq;
type ScalarField = Fr;
@@ -66,28 +63,23 @@ impl ModelParameters for BandersnatchParameters {
/// COFACTOR^(-1) mod r =
/// 9831726595336160714896451345284868594481866920080427688839802480047265754601
const COFACTOR_INV: Fr = MontFp!(
Fr,
"9831726595336160714896451345284868594481866920080427688839802480047265754601"
);
const COFACTOR_INV: Fr =
MontFp!("9831726595336160714896451345284868594481866920080427688839802480047265754601");
}
impl TEModelParameters for BandersnatchParameters {
impl TECurveConfig for BandersnatchParameters {
/// COEFF_A = -5
const COEFF_A: Fq = MontFp!(Fq, "-5");
const COEFF_A: Fq = MontFp!("-5");
/// COEFF_D = (138827208126141220649022263972958607803/
/// 171449701953573178309673572579671231137) mod q
const COEFF_D: Fq = MontFp!(
Fq,
"45022363124591815672509500913686876175488063829319466900776701791074614335719"
);
const COEFF_D: Fq =
MontFp!("45022363124591815672509500913686876175488063829319466900776701791074614335719");
/// AFFINE_GENERATOR_COEFFS = (GENERATOR_X, GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
(TE_GENERATOR_X, TE_GENERATOR_Y);
const GENERATOR: EdwardsAffine = EdwardsAffine::new_unchecked(TE_GENERATOR_X, TE_GENERATOR_Y);
type MontgomeryModelParameters = BandersnatchParameters;
type MontCurveConfig = BandersnatchParameters;
/// Multiplication by `a` is multiply by `-5`.
#[inline(always)]
@@ -97,20 +89,16 @@ impl TEModelParameters for BandersnatchParameters {
}
}
impl MontgomeryModelParameters for BandersnatchParameters {
impl MontCurveConfig for BandersnatchParameters {
/// COEFF_A = 29978822694968839326280996386011761570173833766074948509196803838190355340952
const COEFF_A: Fq = MontFp!(
Fq,
"29978822694968839326280996386011761570173833766074948509196803838190355340952"
);
const COEFF_A: Fq =
MontFp!("29978822694968839326280996386011761570173833766074948509196803838190355340952");
/// COEFF_B = 25465760566081946422412445027709227188579564747101592991722834452325077642517
const COEFF_B: Fq = MontFp!(
Fq,
"25465760566081946422412445027709227188579564747101592991722834452325077642517"
);
const COEFF_B: Fq =
MontFp!("25465760566081946422412445027709227188579564747101592991722834452325077642517");
type TEModelParameters = BandersnatchParameters;
type TECurveConfig = BandersnatchParameters;
}
// The TE form generator is generated following Zcash's fashion:
@@ -123,43 +111,30 @@ impl MontgomeryModelParameters for BandersnatchParameters {
// <https://github.com/zhenfeizhang/bandersnatch/blob/main/bandersnatch/script/bandersnatch.sage>
/// x coordinate for TE curve generator
const TE_GENERATOR_X: Fq = MontFp!(
Fq,
"18886178867200960497001835917649091219057080094937609519140440539760939937304"
);
const TE_GENERATOR_X: Fq =
MontFp!("18886178867200960497001835917649091219057080094937609519140440539760939937304");
/// y coordinate for TE curve generator
const TE_GENERATOR_Y: Fq = MontFp!(
Fq,
"19188667384257783945677642223292697773471335439753913231509108946878080696678"
);
const TE_GENERATOR_Y: Fq =
MontFp!("19188667384257783945677642223292697773471335439753913231509108946878080696678");
/// x coordinate for SW curve generator
const SW_GENERATOR_X: Fq = MontFp!(
Fq,
"30900340493481298850216505686589334086208278925799850409469406976849338430199"
);
const SW_GENERATOR_X: Fq =
MontFp!("30900340493481298850216505686589334086208278925799850409469406976849338430199");
/// y coordinate for SW curve generator
const SW_GENERATOR_Y: Fq = MontFp!(
Fq,
"12663882780877899054958035777720958383845500985908634476792678820121468453298"
);
const SW_GENERATOR_Y: Fq =
MontFp!("12663882780877899054958035777720958383845500985908634476792678820121468453298");
impl SWModelParameters for BandersnatchParameters {
impl SWCurveConfig for BandersnatchParameters {
/// COEFF_A = 10773120815616481058602537765553212789256758185246796157495669123169359657269
const COEFF_A: Self::BaseField = MontFp!(
Fq,
"10773120815616481058602537765553212789256758185246796157495669123169359657269"
);
const COEFF_A: Self::BaseField =
MontFp!("10773120815616481058602537765553212789256758185246796157495669123169359657269");
/// COEFF_B = 29569587568322301171008055308580903175558631321415017492731745847794083609535
const COEFF_B: Self::BaseField = MontFp!(
Fq,
"29569587568322301171008055308580903175558631321415017492731745847794083609535"
);
const COEFF_B: Self::BaseField =
MontFp!("29569587568322301171008055308580903175558631321415017492731745847794083609535");
/// 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);
}

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::<BandersnatchParameters>();
}
#[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,31 +27,6 @@ fn test_generator() {
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}
#[test]
fn test_conversion() {
// edward curve
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);
// weierstrass curve
let mut rng = test_rng();
let a: SWProjective = rng.gen();
let b: SWProjective = rng.gen();
let a_b = { (a + &b).double().double() };
let a_b2 = (a + &b).double().double();
assert_eq!(a_b.into_affine(), a_b2.into_affine());
}
#[test]
fn test_scalar_multiplication() {
let f1 = Fr::from_str(
@@ -107,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(
"(29627151942733444043031429156003786749302466371339015363120350521834195802525, \
27488387519748396681411951718153463804682561779047093991696427532072116857978)",
)
.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::<BandersnatchParameters>();

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]));