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

@@ -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 = 6554484396890773809930967563523245729705921265872317281365359162392183254199
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,7 +1,6 @@
use crate::*;
use ark_r1cs_std::groups::curves::{short_weierstrass::ProjectiveVar, twisted_edwards::AffineVar};
use crate::constraints::FqVar;
use crate::{constraints::FqVar, *};
/// A variable that is the R1CS equivalent of `crate::EdwardsAffine`.
pub type EdwardsVar = AffineVar<JubjubParameters, 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;
use ark_ff::MontFp;
use crate::{Fq, Fr};
#[cfg(test)]
mod tests;
@@ -64,18 +65,21 @@ impl ModelParameters for JubjubParameters {
/// COFACTOR^(-1) mod r =
/// 819310549611346726241370945440405716213240158234039660170669895299022906775
#[rustfmt::skip]
const COFACTOR_INV: Fr = field_new!(Fr, "819310549611346726241370945440405716213240158234039660170669895299022906775");
const COFACTOR_INV: Fr = MontFp!(
Fr,
"819310549611346726241370945440405716213240158234039660170669895299022906775"
);
}
impl TEModelParameters for JubjubParameters {
/// COEFF_A = -1
#[rustfmt::skip]
const COEFF_A: Fq = field_new!(Fq, "-1");
const COEFF_A: Fq = MontFp!(Fq, "-1");
/// COEFF_D = (10240/10241) mod q
#[rustfmt::skip]
const COEFF_D: Fq = field_new!(Fq, "19257038036680949359750312669786877991949435402254120286184196891950884077233");
const COEFF_D: Fq = MontFp!(
Fq,
"19257038036680949359750312669786877991949435402254120286184196891950884077233"
);
/// AFFINE_GENERATOR_COEFFS = (GENERATOR_X, GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) = (GENERATOR_X, GENERATOR_Y);
@@ -91,28 +95,36 @@ impl TEModelParameters for JubjubParameters {
impl MontgomeryModelParameters for JubjubParameters {
/// COEFF_A = 40962
#[rustfmt::skip]
const COEFF_A: Fq = field_new!(Fq, "40962");
const COEFF_A: Fq = MontFp!(Fq, "40962");
/// COEFF_B = -40964
#[rustfmt::skip]
const COEFF_B: Fq = field_new!(Fq, "-40964");
const COEFF_B: Fq = MontFp!(Fq, "-40964");
type TEModelParameters = JubjubParameters;
}
#[rustfmt::skip]
const GENERATOR_X: Fq = field_new!(Fq, "8076246640662884909881801758704306714034609987455869804520522091855516602923");
#[rustfmt::skip]
const GENERATOR_Y: Fq = field_new!(Fq, "13262374693698910701929044844600465831413122818447359594527400194675274060458");
const GENERATOR_X: Fq = MontFp!(
Fq,
"8076246640662884909881801758704306714034609987455869804520522091855516602923"
);
const GENERATOR_Y: Fq = MontFp!(
Fq,
"13262374693698910701929044844600465831413122818447359594527400194675274060458"
);
impl SWModelParameters for JubjubParameters {
/// COEFF_A = 52296097456646850916096512823759002727550416093741407922227928430486925478210
#[rustfmt::skip]
const COEFF_A: Self::BaseField = field_new!(Fq, "52296097456646850916096512823759002727550416093741407922227928430486925478210");
const COEFF_A: Self::BaseField = MontFp!(
Fq,
"52296097456646850916096512823759002727550416093741407922227928430486925478210"
);
/// COEFF_B = 48351165704696163914533707656614864561753505123260775585269522553028192119009
#[rustfmt::skip]
const COEFF_B: Self::BaseField = field_new!(Fq, "48351165704696163914533707656614864561753505123260775585269522553028192119009");
const COEFF_B: Self::BaseField = MontFp!(
Fq,
"48351165704696163914533707656614864561753505123260775585269522553028192119009"
);
/// generators
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
@@ -120,8 +132,13 @@ impl SWModelParameters for JubjubParameters {
}
/// x coordinate for SW curve generator
#[rustfmt::skip]
const SW_GENERATOR_X: Fq = field_new!(Fq, "33835869156188682335217394949746694649676633840125476177319971163079011318731");
const SW_GENERATOR_X: Fq = MontFp!(
Fq,
"33835869156188682335217394949746694649676633840125476177319971163079011318731"
);
/// y coordinate for SW curve generator
#[rustfmt::skip]
const SW_GENERATOR_Y: Fq = field_new!(Fq, "43777270878440091394432848052353307184915192688165709016756678962558652055320");
const SW_GENERATOR_Y: Fq = MontFp!(
Fq,
"43777270878440091394432848052353307184915192688165709016756678962558652055320"
);

View File

@@ -1,11 +1,10 @@
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::*;
use ark_algebra_test_templates::{curves::*, groups::*};
#[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,81 +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;
const TWO_ADICITY: u32 = 1;
#[rustfmt::skip]
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInt::new([
0xaa9f02ab1d6124de,
0xb3524a6466112932,
0x7342261215ac260b,
0x4d6b87b1da259e2,
]);
}
impl FpParameters for FrParameters {
/// MODULUS = 6554484396890773809930967563523245729705921265872317281365359162392183254199.
#[rustfmt::skip]
const MODULUS: BigInteger = BigInt::new([
0xd0970e5ed6f72cb7,
0xa6682093ccc81082,
0x6673b0101343b00,
0xe7db4ea6533afa9,
]);
const MODULUS_BITS: u32 = 252;
const CAPACITY: u32 = Self::MODULUS_BITS - 1;
const REPR_SHAVE_BITS: u32 = 4;
#[rustfmt::skip]
const R: BigInteger = BigInt::new([
0x25f80bb3b99607d9,
0xf315d62f66b6e750,
0x932514eeeb8814f4,
0x9a6fc6f479155c6,
]);
#[rustfmt::skip]
const R2: BigInteger = BigInt::new([
0x67719aa495e57731,
0x51b0cef09ce3fc26,
0x69dab7fac026e9a5,
0x4f6547b8d127688,
]);
const INV: u64 = 0x1ba3a358ef788ef9;
#[rustfmt::skip]
const GENERATOR: BigInteger = BigInt::new([
0x720b1b19d49ea8f1,
0xbf4aa36101f13a58,
0x5fa8cc968193ccbb,
0xe70cbdc7dccf3ac,
]);
const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInt::new([
7515249040934278747,
5995434913520945217,
9454073218019761536,
522094803716528084,
]);
const T: BigInteger = Self::MODULUS_MINUS_ONE_DIV_TWO;
const T_MINUS_ONE_DIV_TWO: BigInteger = BigInt::new([
12980996557321915181,
2997717456760472608,
4727036609009880768,
261047401858264042,
]);
}
#[derive(MontConfig)]
#[modulus = "6554484396890773809930967563523245729705921265872317281365359162392183254199"]
#[generator = "6"]
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", test))]
#[cfg(test)]
mod tests;

View File

@@ -1,33 +1,20 @@
use crate::{Fq, Fr};
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_std::test_rng;
use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
use ark_std::{rand::Rng, str::FromStr, test_rng};
use core::ops::{AddAssign, MulAssign, SubAssign};
use ark_algebra_test_templates::fields::*;
use crate::{Fq, FqConfig, Fr, FrConfig};
use ark_std::{rand::Rng, str::FromStr};
#[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>();
}
#[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; mont(4, 4); );
generate_field_serialization_test!(ed_on_bls12_381;);
#[test]
fn test_fq_add() {
@@ -148,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(
@@ -280,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,
@@ -305,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,
@@ -324,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,
@@ -342,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,
@@ -358,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,
0x43658e68a93612,
0x9fa756be2011e833,
0xaa2b2cb08dac497,
]));
let f3 = Fr::from(BigInteger([
let f3 = Fr::from(BigInteger::new([
0x3999bd14f553edc4,
0xb34be8fa7d8b588c,
0x945df3db6d1dba5,
@@ -381,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([
0x6d6618ac6b4a8381,
0x5b9eb35d711ee1da,
0xce83310e6ac4105d,
@@ -404,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,
@@ -420,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);
}