Upgrade to work with latest ark-ff (#95)

Co-authored-by: Sun <huachuang20@gmail.com>
This commit is contained in:
Pratyush Mishra
2022-03-07 13:12:03 -08:00
committed by GitHub
parent d0dc200f22
commit 1551d6d76c
231 changed files with 2830 additions and 4343 deletions

View File

@@ -25,10 +25,10 @@ ark-curve-constraint-tests = { path = "../curve-constraint-tests", default-featu
[features]
default = []
std = [
"ark-std/std",
"ark-ff/std",
"ark-ec/std",
"ark-bls12-381/std"
std = [
"ark-std/std",
"ark-ff/std",
"ark-ec/std",
"ark-bls12-381/std"
]
r1cs = ["ark-r1cs-std"]

View File

@@ -0,0 +1,28 @@
modulus = 52435875175126190479447740508185965837690552500527637822603658699938581184513
assert(modulus.is_prime())
Fp = GF(modulus)
generator = Fp(0);
for i in range(0, 20):
i = Fp(i);
neg_i = Fp(-i)
if not(i.is_primitive_root() or neg_i.is_primitive_root()):
continue
elif i.is_primitive_root():
assert(i.is_primitive_root());
print("Generator: %d" % i)
generator = i
break
else:
assert(neg_i.is_primitive_root());
print("Generator: %d" % neg_i)
generator = neg_i
break
two_adicity = valuation(modulus - 1, 2);
trace = (modulus - 1) / 2**two_adicity;
two_adic_root_of_unity = generator^trace
print("2-adic Root of Unity: %d " % two_adic_root_of_unity)

View File

@@ -0,0 +1,28 @@
modulus = 13108968793781547619861935127046491459309155893440570251786403306729687672801
assert(modulus.is_prime())
Fp = GF(modulus)
generator = Fp(0);
for i in range(0, 20):
i = Fp(i);
neg_i = Fp(-i)
if not(i.is_primitive_root() or neg_i.is_primitive_root()):
continue
elif i.is_primitive_root():
assert(i.is_primitive_root());
print("Generator: %d" % i)
generator = i
break
else:
assert(neg_i.is_primitive_root());
print("Generator: %d" % neg_i)
generator = neg_i
break
two_adicity = valuation(modulus - 1, 2);
trace = (modulus - 1) / 2**two_adicity;
two_adic_root_of_unity = generator^trace
print("2-adic Root of Unity: %d " % two_adic_root_of_unity)

View File

@@ -1,6 +1,7 @@
use crate::{constraints::FqVar, *};
use ark_r1cs_std::groups::curves::{short_weierstrass::ProjectiveVar, twisted_edwards::AffineVar};
use crate::{constraints::FqVar, *};
/// A variable that is the R1CS equivalent of `crate::BandersnatchParameters`.
pub type EdwardsVar = AffineVar<BandersnatchParameters, FqVar>;

View File

@@ -1,4 +1,3 @@
use crate::{Fq, Fr};
use ark_ec::{
models::{ModelParameters, MontgomeryModelParameters, TEModelParameters},
short_weierstrass_jacobian::{
@@ -7,7 +6,9 @@ use ark_ec::{
twisted_edwards_extended::{GroupAffine, GroupProjective},
SWModelParameters,
};
use ark_ff::{field_new, Field};
use ark_ff::{Field, MontFp};
use crate::{Fq, Fr};
#[cfg(test)]
mod tests;
@@ -18,8 +19,8 @@ pub type EdwardsProjective = GroupProjective<BandersnatchParameters>;
pub type SWAffine = SWGroupAffine<BandersnatchParameters>;
pub type SWProjective = SWGroupProjective<BandersnatchParameters>;
/// `bandersnatch` is a twisted Edwards curve. These curves have equations of the
/// form: ax² + y² = 1 - dx²y².
/// `bandersnatch` is a twisted Edwards curve. These curves have equations of
/// the form: ax² + y² = 1 - dx²y².
/// over some base finite field Fq.
///
/// bandersnatch's curve equation: -5x² + y² = 1 - dx²y²
@@ -50,7 +51,6 @@ pub type SWProjective = SWGroupProjective<BandersnatchParameters>;
///
/// Script to transfer between different curves are available
/// <https://github.com/zhenfeizhang/bandersnatch/blob/main/bandersnatch/script/bandersnatch.sage>
///
#[derive(Clone, Default, PartialEq, Eq)]
pub struct BandersnatchParameters;
@@ -66,18 +66,22 @@ impl ModelParameters for BandersnatchParameters {
/// COFACTOR^(-1) mod r =
/// 9831726595336160714896451345284868594481866920080427688839802480047265754601
#[rustfmt::skip]
const COFACTOR_INV: Fr = field_new!(Fr, "9831726595336160714896451345284868594481866920080427688839802480047265754601");
const COFACTOR_INV: Fr = MontFp!(
Fr,
"9831726595336160714896451345284868594481866920080427688839802480047265754601"
);
}
impl TEModelParameters for BandersnatchParameters {
/// COEFF_A = -5
const COEFF_A: Fq = field_new!(Fq, "-5");
const COEFF_A: Fq = MontFp!(Fq, "-5");
/// COEFF_D = (138827208126141220649022263972958607803/
/// 171449701953573178309673572579671231137) mod q
#[rustfmt::skip]
const COEFF_D: Fq = field_new!(Fq, "45022363124591815672509500913686876175488063829319466900776701791074614335719");
const COEFF_D: Fq = MontFp!(
Fq,
"45022363124591815672509500913686876175488063829319466900776701791074614335719"
);
/// AFFINE_GENERATOR_COEFFS = (GENERATOR_X, GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
@@ -95,44 +99,65 @@ impl TEModelParameters for BandersnatchParameters {
impl MontgomeryModelParameters for BandersnatchParameters {
/// COEFF_A = 29978822694968839326280996386011761570173833766074948509196803838190355340952
#[rustfmt::skip]
const COEFF_A: Fq = field_new!(Fq, "29978822694968839326280996386011761570173833766074948509196803838190355340952");
const COEFF_A: Fq = MontFp!(
Fq,
"29978822694968839326280996386011761570173833766074948509196803838190355340952"
);
/// COEFF_B = 25465760566081946422412445027709227188579564747101592991722834452325077642517
#[rustfmt::skip]
const COEFF_B: Fq = field_new!(Fq, "25465760566081946422412445027709227188579564747101592991722834452325077642517");
const COEFF_B: Fq = MontFp!(
Fq,
"25465760566081946422412445027709227188579564747101592991722834452325077642517"
);
type TEModelParameters = BandersnatchParameters;
}
// The TE form generator is generated following Zcash's fashion:
// "The generators of G1 and G2 are computed by finding the lexicographically smallest
// valid x-coordinate, and its lexicographically smallest y-coordinate and scaling it
// by the cofactor such that the result is not the point at infinity."
// The SW form generator is the same TE generator converted into SW form, obtained from the scripts:
// "The generators of G1 and G2 are computed by finding the lexicographically
// smallest valid x-coordinate, and its lexicographically smallest
// y-coordinate and scaling it by the cofactor such that the result is not
// the point at infinity."
// The SW form generator is the same TE generator converted into SW form,
// obtained from the scripts:
// <https://github.com/zhenfeizhang/bandersnatch/blob/main/bandersnatch/script/bandersnatch.sage>
/// x coordinate for TE curve generator
#[rustfmt::skip]
const TE_GENERATOR_X: Fq = field_new!(Fq, "18886178867200960497001835917649091219057080094937609519140440539760939937304");
const TE_GENERATOR_X: Fq = MontFp!(
Fq,
"18886178867200960497001835917649091219057080094937609519140440539760939937304"
);
/// y coordinate for TE curve generator
#[rustfmt::skip]
const TE_GENERATOR_Y: Fq = field_new!(Fq, "19188667384257783945677642223292697773471335439753913231509108946878080696678");
const TE_GENERATOR_Y: Fq = MontFp!(
Fq,
"19188667384257783945677642223292697773471335439753913231509108946878080696678"
);
/// x coordinate for SW curve generator
#[rustfmt::skip]
const SW_GENERATOR_X: Fq = field_new!(Fq, "30900340493481298850216505686589334086208278925799850409469406976849338430199");
const SW_GENERATOR_X: Fq = MontFp!(
Fq,
"30900340493481298850216505686589334086208278925799850409469406976849338430199"
);
/// y coordinate for SW curve generator
#[rustfmt::skip]
const SW_GENERATOR_Y: Fq = field_new!(Fq, "12663882780877899054958035777720958383845500985908634476792678820121468453298");
const SW_GENERATOR_Y: Fq = MontFp!(
Fq,
"12663882780877899054958035777720958383845500985908634476792678820121468453298"
);
impl SWModelParameters for BandersnatchParameters {
/// COEFF_A = 10773120815616481058602537765553212789256758185246796157495669123169359657269
#[rustfmt::skip]
const COEFF_A: Self::BaseField = field_new!(Fq, "10773120815616481058602537765553212789256758185246796157495669123169359657269");
const COEFF_A: Self::BaseField = MontFp!(
Fq,
"10773120815616481058602537765553212789256758185246796157495669123169359657269"
);
/// COEFF_B = 29569587568322301171008055308580903175558631321415017492731745847794083609535
#[rustfmt::skip]
const COEFF_B: Self::BaseField = field_new!(Fq, "29569587568322301171008055308580903175558631321415017492731745847794083609535");
const COEFF_B: Self::BaseField = MontFp!(
Fq,
"29569587568322301171008055308580903175558631321415017492731745847794083609535"
);
/// generators
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =

View File

@@ -1,9 +1,10 @@
use crate::*;
use ark_algebra_test_templates::{curves::*, groups::*};
use ark_ec::{AffineCurve, ProjectiveCurve};
use ark_ff::{bytes::FromBytes, Zero};
use ark_std::{rand::Rng, str::FromStr, test_rng};
use crate::*;
#[test]
fn test_projective_curve() {
curve_tests::<EdwardsProjective>();

View File

@@ -1 +1 @@
pub use ark_bls12_381::{Fr as Fq, FrParameters as FqParameters};
pub use ark_bls12_381::{Fr as Fq, FrConfig as FqConfig};

View File

@@ -1,115 +1,7 @@
use ark_ff::{
biginteger::{BigInt, BigInteger256 as BigInteger},
fields::{FftParameters, Fp256, Fp256Parameters, FpParameters},
};
use ark_ff::fields::{Fp256, MontBackend, MontConfig};
pub type Fr = Fp256<FrParameters>;
pub struct FrParameters;
impl Fp256Parameters for FrParameters {}
impl FftParameters for FrParameters {
type BigInt = BigInteger;
/// Let `N` be the size of the multiplicative group defined by the field.
/// Then `TWO_ADICITY` is the two-adicity of `N`, i.e. the integer `s`
/// such that `N = 2^s * t` for some odd integer `t`.
const TWO_ADICITY: u32 = 5;
/// 2^s root of unity computed by GENERATOR^t
/// 4740934665446857387895054948191089665295030226009829406950782728666658007874
#[rustfmt::skip]
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInt::new([
0xa4dcdba087826b42,
0x6e4ab162f57f862a,
0xabc5492749348d6a,
0xa7b462035f8c169,
]);
}
impl FpParameters for FrParameters {
/// The modulus of the field.
/// MODULUS = 13108968793781547619861935127046491459309155893440570251786403306729687672801.
#[rustfmt::skip]
const MODULUS: BigInteger = BigInt::new([
0x74fd06b52876e7e1,
0xff8f870074190471,
0x0cce760202687600,
0x1cfb69d4ca675f52,
]);
/// The number of bits needed to represent the `Self::MODULUS`.
const MODULUS_BITS: u32 = 253;
/// The number of bits that can be reliably stored.
/// (Should equal `SELF::MODULUS_BITS - 1`)
const CAPACITY: u32 = Self::MODULUS_BITS - 1;
/// The number of bits that must be shaved from the beginning of
/// the representation when randomly sampling.
const REPR_SHAVE_BITS: u32 = 4;
/// Let `M` be the power of 2^64 nearest to `Self::MODULUS_BITS`. Then
/// `R = M % Self::MODULUS`.
/// R = 10920338887063814464675503992315976178796737518116002025166357554075628257528
#[rustfmt::skip]
const R: BigInteger = BigInt::new([
0x5817ca56bc48c0f8,
0x0383c7fc5f37dc74,
0x998c4fefecbc4ff8,
0x1824b159acc5056f,
]);
/// R2 = R^2 % Self::MODULUS
/// R2 = 4932290691328759802879919559207542894238895193980447506221046538067943049163
#[rustfmt::skip]
const R2: BigInteger = BigInt::new([
0xdbb4f5d658db47cb,
0x40fa7ca27fecb938,
0xaa9e6daec0055cea,
0xae793ddb14aec7d
]);
/// INV = -MODULUS^{-1} mod 2^64
/// INV = 17410672245482742751
const INV: u64 = 0xf19f22295cc063df;
/// A multiplicative generator of the field.
/// `Self::GENERATOR` is an element having multiplicative order
/// `Self::MODULUS - 1`.
/// n = 9962557815892774795293348142308860067333132192265356416788884706064406244838
#[rustfmt::skip]
const GENERATOR: BigInteger = BigInt::new([
0x56b6f3ab7b616de6,
0x114f419d6c9083e5,
0xbf518d217780c4b9,
0x16069b9f45dbce7f,
]);
/// (Self::MODULUS - 1) / 2
/// 6554484396890773809930967563523245729654577946720285125893201653364843836400
const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInt::new([
0xba7e835a943b73f0,
0x7fc7c3803a0c8238,
0x06673b0101343b00,
0xe7db4ea6533afa9,
]);
/// t for 2^s * t = MODULUS - 1, and t coprime to 2.
/// t = 409655274805673363120685472720202858103411121670017820368325103335302739775
/// = (modulus-1)/2^5
const T: BigInteger = BigInt::new([
0x8ba7e835a943b73f,
0x07fc7c3803a0c823,
0x906673b0101343b0,
0xe7db4ea6533afa,
]);
/// (t - 1) / 2
/// = 204827637402836681560342736360101429051705560835008910184162551667651369887
const T_MINUS_ONE_DIV_TWO: BigInteger = BigInt::new([
0xc5d3f41ad4a1db9f,
0x03fe3e1c01d06411,
0x483339d80809a1d8,
0x73eda753299d7d,
]);
}
#[derive(MontConfig)]
#[modulus = "13108968793781547619861935127046491459309155893440570251786403306729687672801"]
#[generator = "7"]
pub struct FrConfig;
pub type Fr = Fp256<MontBackend<FrConfig, 4>>;

View File

@@ -4,5 +4,5 @@ pub mod fr;
pub use fq::*;
pub use fr::*;
#[cfg(all(feature = "ed_on_bls12_381_bandersnatch", test))]
#[cfg(test)]
mod tests;

View File

@@ -1,30 +1,20 @@
use crate::{Fq, Fr};
use ark_algebra_test_templates::fields::*;
use ark_algebra_test_templates::{
fields::*, generate_field_serialization_test, generate_field_test,
};
use ark_ff::{
biginteger::BigInteger256 as BigInteger,
bytes::{FromBytes, ToBytes},
fields::{Field, LegendreSymbol::*, SquareRootField},
One, Zero,
fields::{Field, LegendreSymbol::*, PrimeField, SquareRootField},
One, UniformRand, Zero,
};
use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
use ark_std::{rand::Rng, str::FromStr, test_rng};
use core::ops::{AddAssign, MulAssign, SubAssign};
#[test]
fn test_fr() {
let mut rng = test_rng();
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
primefield_test::<Fr>();
}
use crate::{Fq, FqConfig, Fr, FrConfig};
#[test]
fn test_fq() {
let mut rng = test_rng();
let a: Fq = rng.gen();
let b: Fq = rng.gen();
field_test(a, b);
primefield_test::<Fq>();
}
generate_field_test!(ed_on_bls12_381_bandersnatch; mont(4, 4); );
generate_field_serialization_test!(ed_on_bls12_381_bandersnatch;);
#[test]
fn test_fq_add() {
@@ -145,22 +135,6 @@ fn test_fq_sub() {
assert_eq!(f1 - &f2, f3);
}
#[test]
fn test_fq_double_in_place() {
let mut f1 = Fq::from_str(
"29729289787452206300641229002276778748586801323231253291984198106063944136114",
)
.unwrap();
let f3 = Fq::from_str(
"7022704399778222121834717496367591659483050145934868761364737512189307087715",
)
.unwrap();
assert!(!f1.is_zero());
assert!(!f3.is_zero());
f1.double_in_place();
assert_eq!(f1, f3);
}
#[test]
fn test_fq_double_in_place_thrice() {
let mut f1 = Fq::from_str(
@@ -277,22 +251,9 @@ fn test_fq_square_in_place() {
assert_eq!(f1, f3);
}
#[test]
fn test_fq_sqrt() {
let f1 = Fq::from_str(
"10875927553327821418567659853801220899541454800710193788767706167237535308235",
)
.unwrap();
let f3 = Fq::from_str(
"10816221372957505053219354782681292880545918527618367765651802809826238616708",
)
.unwrap();
assert_eq!(f1.sqrt().unwrap(), f3);
}
#[test]
fn test_fq_from_str() {
let f1_from_repr = Fq::from(BigInteger([
let f1_from_repr = Fq::from(BigInteger::new([
0xab8a2535947d1a77,
0x9ba74cbfda0bbcda,
0xe928b59724d60baf,
@@ -302,7 +263,7 @@ fn test_fq_from_str() {
"13026376210409056429264774981357153555336288129100724591327877625017068755575",
)
.unwrap();
let f2_from_repr = Fq::from(BigInteger([
let f2_from_repr = Fq::from(BigInteger::new([
0x97e9103775d2f35c,
0xbe6756b6c587544b,
0x6ee38c3afd88ef4b,
@@ -321,14 +282,14 @@ fn test_fq_legendre() {
assert_eq!(QuadraticResidue, Fq::one().legendre());
assert_eq!(Zero, Fq::zero().legendre());
let e = BigInteger([
let e = BigInteger::new([
0x0dbc5349cd5664da,
0x8ac5b6296e3ae29d,
0x127cb819feceaa3b,
0x3a6b21fb03867191,
]);
assert_eq!(QuadraticResidue, Fq::from(e).legendre());
let e = BigInteger([
let e = BigInteger::new([
0x96341aefd047c045,
0x9b5f4254500a4d65,
0x1ee08223b68ac240,
@@ -339,7 +300,7 @@ fn test_fq_legendre() {
#[test]
fn test_fq_bytes() {
let f1_from_repr = Fq::from(BigInteger([
let f1_from_repr = Fq::from(BigInteger::new([
0xab8a2535947d1a77,
0x9ba74cbfda0bbcda,
0xe928b59724d60baf,
@@ -355,19 +316,19 @@ fn test_fq_bytes() {
#[test]
fn test_fr_add() {
let f1 = Fr::from(BigInteger([
let f1 = Fr::from(BigInteger::new([
0xc81265fb4130fe0c,
0xb308836c14e22279,
0x699e887f96bff372,
0x84ecc7e76c11ad,
]));
let f2 = Fr::from(BigInteger([
let f2 = Fr::from(BigInteger::new([
0x71875719b422efb8,
0x0043658e68a93612,
0x9fa756be2011e833,
0xaa2b2cb08dac497,
]));
let f3 = Fr::from(BigInteger([
let f3 = Fr::from(BigInteger::new([
0x3999bd14f553edc4,
0xb34be8fa7d8b588c,
0x0945df3db6d1dba5,
@@ -378,19 +339,19 @@ fn test_fr_add() {
#[test]
fn test_fr_mul() {
let f1 = Fr::from(BigInteger([
let f1 = Fr::from(BigInteger::new([
0xc81265fb4130fe0c,
0xb308836c14e22279,
0x699e887f96bff372,
0x84ecc7e76c11ad,
]));
let f2 = Fr::from(BigInteger([
let f2 = Fr::from(BigInteger::new([
0x71875719b422efb8,
0x43658e68a93612,
0x9fa756be2011e833,
0xaa2b2cb08dac497,
]));
let f3 = Fr::from(BigInteger([
let f3 = Fr::from(BigInteger::new([
0xbe3e50c164fe3381,
0x5ac45bc180974585,
0x1c234ad6dcdc70c9,
@@ -401,7 +362,7 @@ fn test_fr_mul() {
#[test]
fn test_fr_bytes() {
let f1_from_repr = Fr::from(BigInteger([
let f1_from_repr = Fr::from(BigInteger::new([
0xc81265fb4130fe0c,
0xb308836c14e22279,
0x699e887f96bff372,
@@ -417,7 +378,7 @@ fn test_fr_bytes() {
#[test]
fn test_fr_from_str() {
let f100_from_repr = Fr::from(BigInteger([0x64, 0, 0, 0]));
let f100_from_repr = Fr::from(BigInteger::new([0x64, 0, 0, 0]));
let f100 = Fr::from_str("100").unwrap();
assert_eq!(f100_from_repr, f100);
}