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 = 41898490967918953402344214791240637128170709919953949071783502921025352812571106773058893763790338921418070971888458477323173057491593855069696241854796396165721416325350064441470418137846398469611935719059908164220784476160001
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 = 5237311370989869175293026848905079641021338739994243633972937865128169101571388346632361720473792365177258871486054600656048925740061347509722287043067341250552640264308621296888446513816907173362124418513727200975392177480577
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::fields::FqVar;
use crate::{constraints::fields::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,7 +2,7 @@ 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};
@@ -24,18 +24,15 @@ impl ModelParameters for EdwardsParameters {
/// COFACTOR_INV (mod r) =
/// 4582647449616135528381398492791944685893671397494963179726320631987147963874964803303316505414568319530101512550297775574042810022553679071007001162683923594233560231270043634777390699589793776691858866199511300853468155295505
#[rustfmt::skip]
const COFACTOR_INV: Fr = field_new!(Fr, "4582647449616135528381398492791944685893671397494963179726320631987147963874964803303316505414568319530101512550297775574042810022553679071007001162683923594233560231270043634777390699589793776691858866199511300853468155295505");
const COFACTOR_INV: Fr = MontFp!(Fr, "4582647449616135528381398492791944685893671397494963179726320631987147963874964803303316505414568319530101512550297775574042810022553679071007001162683923594233560231270043634777390699589793776691858866199511300853468155295505");
}
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 = 317690
#[rustfmt::skip]
const COEFF_D: Fq = field_new!(Fq, "317690");
const COEFF_D: Fq = MontFp!(Fq, "317690");
/// Generated randomly
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) = (GENERATOR_X, GENERATOR_Y);
@@ -51,22 +48,18 @@ impl TEModelParameters for EdwardsParameters {
impl MontgomeryModelParameters for EdwardsParameters {
/// COEFF_A = 40212480635445336270302172549278415015971955924352275480357619589919378421241453024646804979794897776496091377551124233752850182852486874251193367187677349266115879541798515219680194853352256809837126277708211496794264654247419
#[rustfmt::skip]
const COEFF_A: Fq = field_new!(Fq, "40212480635445336270302172549278415015971955924352275480357619589919378421241453024646804979794897776496091377551124233752850182852486874251193367187677349266115879541798515219680194853352256809837126277708211496794264654247419");
const COEFF_A: Fq = MontFp!(Fq, "40212480635445336270302172549278415015971955924352275480357619589919378421241453024646804979794897776496091377551124233752850182852486874251193367187677349266115879541798515219680194853352256809837126277708211496794264654247419");
/// COEFF_B = 1686010332473617132042042241962222112198753995601673591425883331105974391329653748412088783995441144921979594337334243570322874639106980818502874667119046899605536783551549221790223284494141659774809441351696667426519821912580
#[rustfmt::skip]
const COEFF_B: Fq = field_new!(Fq, "1686010332473617132042042241962222112198753995601673591425883331105974391329653748412088783995441144921979594337334243570322874639106980818502874667119046899605536783551549221790223284494141659774809441351696667426519821912580");
const COEFF_B: Fq = MontFp!(Fq, "1686010332473617132042042241962222112198753995601673591425883331105974391329653748412088783995441144921979594337334243570322874639106980818502874667119046899605536783551549221790223284494141659774809441351696667426519821912580");
type TEModelParameters = EdwardsParameters;
}
/// GENERATOR_X =
/// 41126137307536311801428235632419266329480236393691483739251051053325519918069469184425962602019877935619960143044210127218431046103600632347238890180171944971817510488009355627861577881883236134824745174469522277738875418206826
#[rustfmt::skip]
const GENERATOR_X: Fq = field_new!(Fq, "41126137307536311801428235632419266329480236393691483739251051053325519918069469184425962602019877935619960143044210127218431046103600632347238890180171944971817510488009355627861577881883236134824745174469522277738875418206826");
const GENERATOR_X: Fq = MontFp!(Fq, "41126137307536311801428235632419266329480236393691483739251051053325519918069469184425962602019877935619960143044210127218431046103600632347238890180171944971817510488009355627861577881883236134824745174469522277738875418206826");
/// GENERATOR_Y =
/// 18249602579663240810999977712212098844157230095713722119136881953011435881503578209163288529034825612841855863913294174196656077002578342108932925693640046298989762289691399012056048139253937882385653600831389370198228562812681
#[rustfmt::skip]
const GENERATOR_Y: Fq = field_new!(Fq, "18249602579663240810999977712212098844157230095713722119136881953011435881503578209163288529034825612841855863913294174196656077002578342108932925693640046298989762289691399012056048139253937882385653600831389370198228562812681");
const GENERATOR_Y: Fq = MontFp!(Fq, "18249602579663240810999977712212098844157230095713722119136881953011435881503578209163288529034825612841855863913294174196656077002578342108932925693640046298989762289691399012056048139253937882385653600831389370198228562812681");

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_mnt4_753::{Fr as Fq, FrParameters as FqParameters};
pub use ark_mnt4_753::{Fr as Fq, FrConfig as FqConfig};

View File

@@ -1,182 +1,7 @@
use ark_ff::{
biginteger::{BigInt, BigInteger768 as BigInteger},
fields::{FftParameters, Fp768, Fp768Parameters, FpParameters},
};
use ark_ff::fields::{Fp768, MontBackend, MontConfig};
pub type Fr = Fp768<FrParameters>;
pub struct FrParameters;
impl Fp768Parameters for FrParameters {}
impl FftParameters for FrParameters {
type BigInt = BigInteger;
const TWO_ADICITY: u32 = 7u32;
// ROOT_OF_UNITY = GENERATOR ^ t =
// 5051348772165646558710889803432238607797392809516000790038615454406641638798360636639094562941749878118669420392611632754442159525900729019616828636182878045303562497793780656635901271279409699078868658041674335385318499053954
// t is defined below
// This number needs to be in the Montgomery residue form.
// I.e., write
// 5051348772165646558710889803432238607797392809516000790038615454406641638798360636639094562941749878118669420392611632754442159525900729019616828636182878045303562497793780656635901271279409699078868658041674335385318499053954
// * R
// = 3163945077843586747114473523156080008349200300253316071422414259389979351386670787753361998953450578171951209600907861296956453653582402723399808696724060539858637307706671971132333536614595846054039300191656599533885935499352
#[rustfmt::skip]
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInt::new([
17630237153019476056u64,
13843632041501582123u64,
8277579215948731020u64,
16543319700733887487u64,
17904443422768964556u64,
4398189354108552378u64,
15178824470536352826u64,
5393472405610595666u64,
9815530206026813666u64,
9111703519527971535u64,
9880873531994141485u64,
37593433148184u64,
]);
}
impl FpParameters for FrParameters {
// MODULUS = 5237311370989869175293026848905079641021338739994243633972937865128169101571388346632361720473792365177258871486054600656048925740061347509722287043067341250552640264308621296888446513816907173362124418513727200975392177480577
// Factors of MODULUS - 1:
// 2^7
// 3
// 67
// 193189
// 5324381
// 20502324317011
// 12991385268608969143
// 743005941432538001939136029613828619428586060274612824031793373798492678674419102414979927623550862639644071557313558044209469997283394306590808303316688123808776073253386140931
#[rustfmt::skip]
const MODULUS: BigInteger = BigInt::new([
1918157353135465345u64,
963476667289301255u64,
6719983938249379016u64,
3655897403342429413u64,
14998997414201165002u64,
13135040821375901270u64,
12914675130452106995u64,
6989506515121216945u64,
12382362535852178190u64,
13186314214759855613u64,
2451174275904461237u64,
62228802984066u64,
]);
const MODULUS_BITS: u32 = 750;
const CAPACITY: u32 = Self::MODULUS_BITS - 1;
const REPR_SHAVE_BITS: u32 = 18;
// see ark_ff/src/fields/mod.rs for more information
// R = pow(2,768) % MODULUS
// R = 933352698056040166367534174176950366489065242993745918174914647273231163953185260894581718311971532174387033963715296372791285468903747270837716556902938133611910788060028435531754797383796835009316018259656953442114538695438
#[rustfmt::skip]
const R: BigInteger = BigInt::new([
13829252738394483470u64,
3696102008259415033u64,
13727024804350215797u64,
13923468026436718769u64,
5924604905079742862u64,
10708002647109138222u64,
14670460945619011269u64,
10920055614013427454u64,
16773322069409968132u64,
11648025004657998992u64,
5853759956175613481u64,
11089930891582u64,
]);
// R2 = R * R % MODULUS
// R2 = 2468731867191023344597744941938926307216338526282824416880609839804154918771848044056240157551420210981962520047623686977567450338290776997282473798413876535168711321018336215486289519735826959884564283681071791441993286279295
#[rustfmt::skip]
const R2: BigInteger = BigInt::new([
10440129917231554687u64,
8797934528693354276u64,
14378434434829994158u64,
7755707164286885667u64,
16206546501540671680u64,
8674228973811871262u64,
12794601382709871071u64,
17194287857269754157u64,
2120600029739364160u64,
15454005187782655500u64,
18107041519543174727u64,
29333033326005u64,
]);
// INV = -(MODULUS)^(-1) % 2^64
const INV: u64 = 3079018560679650175u64;
// GENERATOR = 5
// This number needs to be in the Montgomery residue form.
// I.e., write 5 * R =
// 4666763490280200831837670870884751832445326214968729590874573236366155819765926304472908591559857660871935169818576481863956427344518736354188582784514690668059553940300142177658773986918984175046580091298284767210572693477190
#[rustfmt::skip]
const GENERATOR: BigInteger = BigInt::new([
13806031470843762502u64,
33765967587523552u64,
13294891800622424138u64,
14277107911054939000u64,
11176280451689162697u64,
16646525088126587879u64,
18012072506966401499u64,
17706789922648034041u64,
10079634052211634198u64,
2899892802161340116u64,
10822055707168515792u64,
55449654457911u64,
]);
// (n-1)/2 = 2618655685494934587646513424452539820510669369997121816986468932564084550785694173316180860236896182588629435743027300328024462870030673754861143521533670625276320132154310648444223256908453586681062209256863600487696088740288
#[rustfmt::skip]
const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInt::new([
10182450713422508480u64,
481738333644650627u64,
12583364005979465316u64,
1827948701671214706u64,
7499498707100582501u64,
15790892447542726443u64,
15680709602080829305u64,
3494753257560608472u64,
15414553304780864903u64,
15816529144234703614u64,
1225587137952230618u64,
31114401492033u64,
]);
// t = (n - 1) / 2^{TWO_ADICITY} =
// 40916495085858352931976772257070934695479208906205028390413577071313821106026471458065325941201502852947334933484801567625382232344229277419705367523963603519942502064911103881940988389194587291891597019638493757620251386567
const T: BigInteger = BigInt::new([
1023791920852361927u64,
10383820702924820450u64,
14608133870179016345u64,
10693085616076947257u64,
12511085841822051593u64,
16675864135140424508u64,
7162540115173594813u64,
2072218152711366715u64,
18111135716793329142u64,
7741123047823172587u64,
307380175182215347u64,
486162523313u64,
]);
// (t-1)/2 = 20458247542929176465988386128535467347739604453102514195206788535656910553013235729032662970600751426473667466742400783812691116172114638709852683761981801759971251032455551940970494194597293645945798509819246878810125693283
const T_MINUS_ONE_DIV_TWO: BigInteger = BigInt::new([
511895960426180963u64,
14415282388317186033u64,
16527438971944283980u64,
14569914844893249436u64,
6255542920911025796u64,
17561304104424988062u64,
12804642094441573214u64,
1036109076355683357u64,
18278939895251440379u64,
13093933560766362101u64,
9377062124445883481u64,
243081261656u64,
]);
}
#[derive(MontConfig)]
#[modulus = "5237311370989869175293026848905079641021338739994243633972937865128169101571388346632361720473792365177258871486054600656048925740061347509722287043067341250552640264308621296888446513816907173362124418513727200975392177480577"]
#[generator = "5"]
pub struct FrConfig;
pub type Fr = Fp768<MontBackend<FrConfig, 12>>;

View File

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

View File

@@ -1,23 +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 ark_algebra_test_templates::fields::*;
use crate::{Fq, FqConfig, Fr, FrConfig};
#[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_mnt4_753; mont(12, 12); );
generate_field_serialization_test!(ed_on_mnt4_753;);

View File

@@ -8,9 +8,10 @@
)]
#![forbid(unsafe_code)]
//! This library implements a twisted Edwards curve whose base field is the scalar field of the
//! curve MNT4-753. This allows defining cryptographic primitives that use elliptic curves over
//! the scalar field of the latter curve.
//! This library implements a twisted Edwards curve whose base field is the
//! scalar field of the curve MNT4-753. This allows defining cryptographic
//! primitives that use elliptic curves over the scalar field of the latter
//! curve.
//!
//! Curve information:
//! * Base field: q = 41898490967918953402344214791240637128170709919953949071783502921025352812571106773058893763790338921418070971888458477323173057491593855069696241854796396165721416325350064441470418137846398469611935719059908164220784476160001