Initial commit

This commit is contained in:
Pratyush Mishra
2020-10-11 19:50:41 -07:00
commit 43ca2132fd
209 changed files with 18825 additions and 0 deletions

115
mnt4_298/src/fields/fq.rs Normal file
View File

@@ -0,0 +1,115 @@
use ark_ff::{
biginteger::BigInteger320 as BigInteger,
fields::{FftParameters, Fp320, Fp320Parameters, FpParameters},
};
pub type Fq = Fp320<FqParameters>;
pub struct FqParameters;
impl Fp320Parameters for FqParameters {}
impl FftParameters for FqParameters {
type BigInt = BigInteger;
const TWO_ADICITY: u32 = 17;
#[rustfmt::skip]
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInteger([
9821480371597472441u64,
9468346035609379175u64,
9963748368231707135u64,
14865337659602750405u64,
3984815592673u64,
]);
const SMALL_SUBGROUP_BASE: Option<u32> = Some(7);
const SMALL_SUBGROUP_BASE_ADICITY: Option<u32> = Some(2);
/// LARGE_SUBGROUP_ROOT_OF_UNITY = x * g
/// where x = (n - 1) / 2^17 / 7^2
/// and represent this value in the Montgomery residue form.
/// I.e., write
/// 381811485921190977554243339163030148371175054922689353173385941180422489253833691237722982
/// * R
/// = 260534023778902228073198316993669317435810479439368306496187170459125001342456918103569322
const LARGE_SUBGROUP_ROOT_OF_UNITY: Option<BigInteger> = Some(BigInteger([
7711798843682337706u64,
16456007754393011187u64,
7470854640069402569u64,
10767969225751706229u64,
2250015743691u64,
]));
}
impl FpParameters for FqParameters {
/// MODULUS = 475922286169261325753349249653048451545124879242694725395555128576210262817955800483758081
#[rustfmt::skip]
const MODULUS: BigInteger = BigInteger([
14487189785281953793u64,
4731562877756902930u64,
14622846468719063274u64,
11702080941310629006u64,
4110145082483u64,
]);
const MODULUS_BITS: u32 = 298;
const CAPACITY: u32 = Self::MODULUS_BITS - 1;
const REPR_SHAVE_BITS: u32 = 22;
#[rustfmt::skip]
const R: BigInteger = BigInteger([
1784298994435064924u64,
16852041090100268533u64,
14258261760832875328u64,
2961187778261111191u64,
1929014752195u64,
]);
#[rustfmt::skip]
const R2: BigInteger = BigInteger([
28619103704175136u64,
11702218449377544339u64,
7403203599591297249u64,
2248105543421449339u64,
2357678148148u64,
]);
const INV: u64 = 12714121028002250751u64;
#[rustfmt::skip]
const GENERATOR: BigInteger = BigInteger([
2709730703260633621u64,
13556085429182073539u64,
10903316137158576359u64,
5319113788683590444u64,
4022235209932u64,
]);
#[rustfmt::skip]
const T: BigInteger = BigInteger([
0x70964866b2d38b3,
0x987520d4f1af2890,
0x2a47657764b1ae89,
0x6a39d133124ed3d8,
0x1de7bde,
]);
#[rustfmt::skip]
const T_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
0x384b24335969c59,
0xcc3a906a78d79448,
0x1523b2bbb258d744,
0x351ce899892769ec,
0xef3def,
]);
#[rustfmt::skip]
const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
0x64866b2d38b30000,
0x20d4f1af28900709,
0x657764b1ae899875,
0xd133124ed3d82a47,
0x1de7bde6a39,
]);
}

View File

@@ -0,0 +1,58 @@
use crate::{Fq, FQ_ONE};
use ark_ff::{
biginteger::BigInteger320 as BigInteger,
field_new,
fields::fp2::{Fp2, Fp2Parameters},
};
pub type Fq2 = Fp2<Fq2Parameters>;
pub struct Fq2Parameters;
impl Fp2Parameters for Fq2Parameters {
type Fp = Fq;
/// The quadratic non-residue (17) used to construct the extension is
/// the same as that used in [`libff`](https://github.com/scipr-lab/libff/blob/c927821ebe02e0a24b5e0f9170cec5e211a35f08/libff/algebra/curves/mnt/mnt4/mnt4_init.cpp#L102).
#[rustfmt::skip]
const NONRESIDUE: Fq = field_new!(Fq, BigInteger([
2709730703260633621,
13556085429182073539,
10903316137158576359,
5319113788683590444,
4022235209932,
]));
/// The quadratic non-residue in F<sub>p</sub><sup>2</sup> that is used
/// in the computation of square roots is (8, 1), the same as that in
/// [`libff`](https://github.com/scipr-lab/libff/blob/c927821ebe02e0a24b5e0f9170cec5e211a35f08/libff/algebra/curves/mnt/mnt4/mnt4_init.cpp#L103)
const QUADRATIC_NONRESIDUE: (Self::Fp, Self::Fp) = (
field_new!(
Fq,
BigInteger([
7706310747053761245,
9941175645274129776,
14857322459377157960,
7030003475866554129,
3101682770110
])
),
FQ_ONE,
);
/// Precomputed coefficients:
/// `[1, 475922286169261325753349249653048451545124879242694725395555128576210262817955800483758080]`
const FROBENIUS_COEFF_FP2_C1: &'static [Self::Fp] = &[
FQ_ONE,
field_new!(
Fq,
BigInteger([
12702890790846888869,
6326265861366186013,
364584707886187945,
8740893163049517815,
2181130330288
])
),
];
}

View File

@@ -0,0 +1,56 @@
use crate::{Fq, Fq2, Fq2Parameters, FQ_ONE, FQ_ZERO};
use ark_ff::{
biginteger::BigInteger320 as BigInteger,
field_new,
fields::fp4::{Fp4, Fp4Parameters},
};
pub type Fq4 = Fp4<Fq4Parameters>;
pub struct Fq4Parameters;
impl Fp4Parameters for Fq4Parameters {
type Fp2Params = Fq2Parameters;
const NONRESIDUE: Fq2 = field_new!(Fq2, FQ_ZERO, FQ_ONE);
// Coefficients for the Frobenius automorphism.
// c1[0] = 1,
// c1[1] = 7684163245453501615621351552473337069301082060976805004625011694147890954040864167002308
// c1[2] = 475922286169261325753349249653048451545124879242694725395555128576210262817955800483758080
// c1[3] = 468238122923807824137727898100575114475823797181717920390930116882062371863914936316755773
#[rustfmt::skip]
const FROBENIUS_COEFF_FP4_C1: &'static [Fq] = &[
FQ_ONE,
field_new!(
Fq,
BigInteger([
16439849825752526567,
14772594681319164557,
16175669228740845684,
4590896976404796446,
3810243174413
])
),
field_new!(
Fq,
BigInteger([
12702890790846888869,
6326265861366186013,
364584707886187945,
8740893163049517815,
2181130330288
])
),
field_new!(
Fq,
BigInteger([
16494084033238978842,
8405712270147289988,
16893921313687769205,
7111183964905832559,
299901908070
])
),
];
}

99
mnt4_298/src/fields/fr.rs Normal file
View File

@@ -0,0 +1,99 @@
use ark_ff::{
biginteger::BigInteger320 as BigInteger,
fields::{FftParameters, Fp320, Fp320Parameters, FpParameters},
};
pub type Fr = Fp320<FrParameters>;
pub struct FrParameters;
impl Fp320Parameters for FrParameters {}
impl FftParameters for FrParameters {
type BigInt = BigInteger;
const TWO_ADICITY: u32 = 34;
#[rustfmt::skip]
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInteger([
0x818b361df1af7be4,
0x2ae2750d46a53957,
0x5784a8fe792c5f8a,
0xf9bd39c0cdcf1bb6,
0x6a24a0f8a8,
]);
}
impl FpParameters for FrParameters {
/// MODULUS = 475922286169261325753349249653048451545124878552823515553267735739164647307408490559963137
#[rustfmt::skip]
const MODULUS: BigInteger = BigInteger([
0xbb4334a400000001,
0xfb494c07925d6ad3,
0xcaeec9635cf44194,
0xa266249da7b0548e,
0x3bcf7bcd473,
]);
const MODULUS_BITS: u32 = 298;
const CAPACITY: u32 = Self::MODULUS_BITS - 1;
const REPR_SHAVE_BITS: u32 = 22;
#[rustfmt::skip]
const R: BigInteger = BigInteger([
0xc3177aefffbb845c,
0x9b80c702f9961788,
0xc5df8dcdac70a85a,
0x29184098647b5197,
0x1c1223d33c3,
]);
#[rustfmt::skip]
const R2: BigInteger = BigInteger([
0x465a743c68e0596b,
0x34f9102adb68371,
0x4bbd6dcf1e3a8386,
0x2ff00dced8e4b6d,
0x149bb44a342,
]);
const INV: u64 = 0xbb4334a3ffffffff;
#[rustfmt::skip]
const GENERATOR: BigInteger = BigInteger([
0xb1ddfacffd532b94,
0x25e295ff76674008,
0x8f00647b48958d36,
0x1159f37d4e0fddb2,
0x2977770b3d1,
]);
#[rustfmt::skip]
const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
0xdda19a5200000000,
0x7da4a603c92eb569,
0x657764b1ae7a20ca,
0xd133124ed3d82a47,
0x1de7bde6a39,
]);
// T and T_MINUS_ONE_DIV_TWO, where MODULUS - 1 = 2^S * T
#[rustfmt::skip]
const T: BigInteger = BigInteger([
0xe4975ab4eed0cd29,
0xd73d10653ed25301,
0x69ec1523b2bbb258,
0x3def351ce8998927,
0xef,
]);
#[rustfmt::skip]
const T_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
0xf24bad5a77686694,
0x6b9e88329f692980,
0xb4f60a91d95dd92c,
0x9ef79a8e744cc493,
0x77,
]);
}

View File

@@ -0,0 +1,22 @@
#[cfg(feature = "scalar_field")]
pub mod fr;
#[cfg(feature = "scalar_field")]
pub use self::fr::*;
#[cfg(feature = "base_field")]
pub mod fq;
#[cfg(feature = "base_field")]
pub use self::fq::*;
#[cfg(feature = "curve")]
pub mod fq2;
#[cfg(feature = "curve")]
pub use self::fq2::*;
#[cfg(feature = "curve")]
pub mod fq4;
#[cfg(feature = "curve")]
pub use self::fq4::*;
#[cfg(all(feature = "curve", test))]
mod tests;

View File

@@ -0,0 +1,45 @@
use ark_ff::{test_rng, Field};
use rand::Rng;
use crate::*;
use ark_curve_tests::fields::*;
#[test]
fn test_fr() {
let mut rng = test_rng();
let a: Fr = rng.gen();
let b: Fr = rng.gen();
field_test(a, b);
sqrt_field_test(a);
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);
sqrt_field_test(a);
primefield_test::<Fq>();
}
#[test]
fn test_fq2() {
let mut rng = test_rng();
let a: Fq2 = rng.gen();
let b: Fq2 = rng.gen();
field_test(a, b);
sqrt_field_test(a);
frobenius_test::<Fq2, _>(Fq::characteristic(), 13);
}
#[test]
fn test_fq4() {
let mut rng = test_rng();
let a: Fq4 = rng.gen();
let b: Fq4 = rng.gen();
field_test(a, b);
frobenius_test::<Fq4, _>(Fq::characteristic(), 13);
}