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 = 258664426012969094010652733694893533536393512754914660539884262666720468348340822774968888139573360124440321458177
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 = 32333053251621136751331591711861691692049189094364332567435817881934511297123972799646723302813083835942624121493
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::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<EdwardsParameters, FqVar>;

View File

@@ -1,6 +1,7 @@
use crate::fq::Fq;
use ark_r1cs_std::fields::fp::FpVar;
use crate::fq::Fq;
/// A variable that is the R1CS equivalent of `crate::Fq`.
pub type FqVar = FpVar<Fq>;

View File

@@ -2,9 +2,9 @@ use ark_ec::{
models::{ModelParameters, MontgomeryModelParameters, TEModelParameters},
twisted_edwards_extended::{GroupAffine, GroupProjective},
};
use ark_ff::field_new;
use ark_ff::MontFp;
use crate::{fq::Fq, fr::Fr};
use crate::{Fq, Fr};
#[cfg(test)]
mod tests;
@@ -24,18 +24,15 @@ impl ModelParameters for EdwardsParameters {
/// COFACTOR^(-1) mod r =
/// 12124894969357926281749346891948134384518445910386624712788431705725441736421489799867521238554906438478484045560
#[rustfmt::skip]
const COFACTOR_INV: Fr = field_new!(Fr, "12124894969357926281749346891948134384518445910386624712788431705725441736421489799867521238554906438478484045560");
const COFACTOR_INV: Fr = MontFp!(Fr, "12124894969357926281749346891948134384518445910386624712788431705725441736421489799867521238554906438478484045560");
}
impl TEModelParameters for EdwardsParameters {
/// COEFF_A = -1 =
#[rustfmt::skip]
const COEFF_A: Fq = field_new!(Fq, "-1");
const COEFF_A: Fq = MontFp!(Fq, "-1");
/// COEFF_D = 79743
#[rustfmt::skip]
const COEFF_D: Fq = field_new!(Fq, "79743");
const COEFF_D: Fq = MontFp!(Fq, "79743");
/// AFFINE_GENERATOR_COEFFS = (GENERATOR_X, GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) = (GENERATOR_X, GENERATOR_Y);
@@ -51,21 +48,18 @@ impl TEModelParameters for EdwardsParameters {
impl MontgomeryModelParameters for EdwardsParameters {
/// COEFF_A = 0x95D53EB3F6AC3F7A53C26020144439DC6073BCAE513E03FD06B6B3BAA390F25E51534B26719E33F4CD906D4DA9B535
#[rustfmt::skip]
const COEFF_A: Fq = field_new!(Fq, "90083623084271891037116870487743067984710080209539149685414147055329063590616489392386084989619674926965747987765");
const COEFF_A: Fq = MontFp!(Fq, "90083623084271891037116870487743067984710080209539149685414147055329063590616489392386084989619674926965747987765");
/// COEFF_B = 0x118650763CE64AB4BE743604C8D05013DC2663652A3D58B21ECAB7BFF65B70DB8BA09F9098E61CC903B2F92B2564ACA
#[rustfmt::skip]
const COEFF_B: Fq = field_new!(Fq, "168580802928697202973535863207150465551683432545375510854470115611391404757724333382582803149953685197474573470410");
const COEFF_B: Fq = MontFp!(Fq, "168580802928697202973535863207150465551683432545375510854470115611391404757724333382582803149953685197474573470410");
type TEModelParameters = EdwardsParameters;
}
/// GENERATOR_X =
/// 174701772324485506941690903512423551998294352968833659960042362742684869862495746426366187462669992073196420267127
#[rustfmt::skip]
const GENERATOR_X: Fq = field_new!(Fq, "174701772324485506941690903512423551998294352968833659960042362742684869862495746426366187462669992073196420267127");
const GENERATOR_X: Fq = MontFp!(Fq, "174701772324485506941690903512423551998294352968833659960042362742684869862495746426366187462669992073196420267127");
/// GENERATOR_Y =
/// 208487200052258845495340374451540775445408439654930191324011635560142523886549663106522691296420655144190624954833
#[rustfmt::skip]
const GENERATOR_Y: Fq = field_new!(Fq, "208487200052258845495340374451540775445408439654930191324011635560142523886549663106522691296420655144190624954833");
const GENERATOR_Y: Fq = MontFp!(Fq, "208487200052258845495340374451540775445408439654930191324011635560142523886549663106522691296420655144190624954833");

View File

@@ -1,11 +1,9 @@
use ark_algebra_test_templates::{curves::*, groups::*};
use ark_ec::{AffineCurve, ProjectiveCurve};
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_std::{rand::Rng, 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_377::{Fq, FqParameters};
pub use ark_bls12_377::{Fq, FqConfig};

View File

@@ -1,106 +1,7 @@
use ark_ff::{
biginteger::{BigInt, BigInteger384 as BigInteger},
fields::{FftParameters, Fp384, Fp384Parameters, FpParameters},
};
use ark_ff::fields::{Fp384, MontBackend, MontConfig};
pub type Fr = Fp384<FrParameters>;
pub struct FrParameters;
impl Fp384Parameters for FrParameters {}
impl FftParameters for FrParameters {
type BigInt = BigInteger;
const TWO_ADICITY: u32 = 2u32;
#[rustfmt::skip]
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInt::new([
12119792640622387781u64,
8318439284650634613u64,
6931324077796168275u64,
12851391603681523141u64,
6881015057611215092u64,
1893962574900431u64,
]);
}
impl FpParameters for FrParameters {
/// MODULUS = 32333053251621136751331591711861691692049189094364332567435817881934511297123972799646723302813083835942624121493
#[rustfmt::skip]
const MODULUS: BigInteger = BigInt::new([
4684667634276979349u64,
3748803659444032385u64,
16273581227874629698u64,
7152942431629910641u64,
6397188139321141543u64,
15137289088311837u64,
]);
const MODULUS_BITS: u32 = 374;
const CAPACITY: u32 = Self::MODULUS_BITS - 1;
const REPR_SHAVE_BITS: u32 = 10;
#[rustfmt::skip]
const R: BigInteger = BigInt::new([
12565484300600153878u64,
8749673077137355528u64,
9027943686469014788u64,
13026065139386752555u64,
11197589485989933721u64,
9525964145733727u64,
]);
#[rustfmt::skip]
const R2: BigInteger = BigInt::new([
17257035094703902127u64,
16096159112880350050u64,
3498553494623421763u64,
333405339929360058u64,
1125865524035793947u64,
1586246138566285u64,
]);
const INV: u64 = 16242011933465909059u64;
// 2
#[rustfmt::skip]
const GENERATOR: BigInteger = BigInt::new([
1999556893213776791u64,
13750542494830678672u64,
1782306145063399878u64,
452443773434042853u64,
15997990832658725900u64,
3914639203155617u64,
]);
#[rustfmt::skip]
const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInt::new([
11565705853993265482u64,
1874401829722016192u64,
17360162650792090657u64,
12799843252669731128u64,
12421966106515346579u64,
7568644544155918u64,
]);
/// 8083263312905284187832897927965422923012297273591083141858954470483627824280993199911680825703270958985656030373
const T: BigInteger = BigInt::new([
5782852926996632741,
10160572951715783904,
8680081325396045328,
15623293663189641372,
6210983053257673289,
3784322272077959,
]);
/// 4041631656452642093916448963982711461506148636795541570929477235241813912140496599955840412851635479492828015186
const T_MINUS_ONE_DIV_TWO: BigInteger = BigInt::new([
2891426463498316370,
5080286475857891952,
4340040662698022664,
17035018868449596494,
12328863563483612452,
1892161136038979,
]);
}
#[derive(MontConfig)]
#[modulus = "32333053251621136751331591711861691692049189094364332567435817881934511297123972799646723302813083835942624121493"]
#[generator = "13"]
pub struct FrConfig;
pub type Fr = Fp384<MontBackend<FrConfig, 6>>;

View File

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

View File

@@ -1,24 +1,12 @@
use ark_std::rand::Rng;
use ark_std::test_rng;
use ark_algebra_test_templates::{
fields::*, generate_field_serialization_test, generate_field_test,
};
use ark_ff::{Field, One, PrimeField, SquareRootField, UniformRand, Zero};
use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
use ark_std::{rand::Rng, test_rng};
use core::ops::{AddAssign, MulAssign, SubAssign};
use crate::{Fq, Fr};
use crate::{Fq, FqConfig, Fr, FrConfig};
use ark_algebra_test_templates::fields::*;
#[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_cp6_782; mont(6, 6); );
generate_field_serialization_test!(ed_on_cp6_782;);

View File

@@ -8,9 +8,9 @@
)]
#![forbid(unsafe_code)]
//! This library implements a twisted Edwards curve whose base field is the scalar field of the curve CP6.
//! This allows defining cryptographic primitives that use elliptic curves over the scalar field of the latter curve.
//! This curve was generated as part of the paper [\[BCGMMW20, “Zexe”\]](https://eprint.iacr.org/2018/962).
//! This library implements a twisted Edwards curve whose base field is the
//! scalar field of the curve CP6. This allows defining cryptographic primitives
//! that use elliptic curves over the scalar field of the latter curve. This curve was generated as part of the paper [\[BCGMMW20, “Zexe”\]](https://eprint.iacr.org/2018/962).
//!
//! Curve information:
//! * Base field: q = 258664426012969094010652733694893533536393512754914660539884262666720468348340822774968888139573360124440321458177