forked from lambdaclass/lambdaworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bench for U64 mont and mersenne31
- Loading branch information
Showing
6 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use const_random::const_random; | ||
use lambdaworks_math::{ | ||
field::element::FieldElement, | ||
field::fields::fft_friendly::u64_mersenne_montgomery_field::Mersenne31MontgomeryPrimeField, | ||
field::fields::u64_prime_field::U64FieldElement, polynomial::Polynomial, | ||
}; | ||
use rand::random; | ||
|
||
// Mersenne prime numbers | ||
// https://www.math.utah.edu/~pa/math/mersenne.html | ||
const PRIMES: [u64; 39] = [ | ||
13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, | ||
11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787, | ||
1398269, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583, 25964951, 30402457, | ||
]; | ||
|
||
const MODULUS: u64 = PRIMES[const_random!(usize) % PRIMES.len()]; | ||
pub type FE = U64FieldElement<MODULUS>; | ||
|
||
#[inline(never)] | ||
#[export_name = "u32_utils::fp_get_mersenne_mont_primes"] | ||
pub fn get_field_elements() -> ( | ||
FieldElement<Mersenne31MontgomeryPrimeField>, | ||
FieldElement<Mersenne31MontgomeryPrimeField>, | ||
) { | ||
let x = FieldElement::<Mersenne31MontgomeryPrimeField>::from(521); | ||
let y = FieldElement::<Mersenne31MontgomeryPrimeField>::from(132049); | ||
(x, y) | ||
} | ||
|
||
#[inline(never)] | ||
#[export_name = "u32_utils::fp_squared_mersenne_mont_prime"] | ||
pub fn get_squared_field_element() -> FieldElement<Mersenne31MontgomeryPrimeField> { | ||
let (x, _) = get_field_elements(); | ||
x * x | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[inline(never)] | ||
#[export_name = "u32_utils::rand_field_mersenne_mont_elements"] | ||
pub fn rand_field_elements(order: u64) -> Vec<FE> { | ||
let mut result = Vec::with_capacity(1 << order); | ||
for _ in 0..result.capacity() { | ||
result.push(FE::new(random())); | ||
} | ||
result | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[inline(never)] | ||
#[export_name = "u32_utils::rand_field_mersenne_mont_elements_pair"] | ||
pub fn rand_field_elements_pair() -> (FE, FE) { | ||
(FE::new(random()), FE::new(random())) | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[inline(never)] | ||
#[export_name = "u32_utils::rand_mersenne_mont_poly"] | ||
pub fn rand_poly(order: u64) -> Polynomial<FE> { | ||
Polynomial::new(&rand_field_elements(order)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use const_random::const_random; | ||
use lambdaworks_math::{ | ||
field::element::FieldElement, field::fields::u32_mersenne_prime_field::Mersenne31Field, | ||
field::fields::u64_prime_field::U64FieldElement, polynomial::Polynomial, | ||
}; | ||
use rand::random; | ||
|
||
// Mersenne prime numbers | ||
// https://www.math.utah.edu/~pa/math/mersenne.html | ||
const PRIMES: [u64; 39] = [ | ||
13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, | ||
11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787, | ||
1398269, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583, 25964951, 30402457, | ||
]; | ||
|
||
const MODULUS: u64 = PRIMES[const_random!(usize) % PRIMES.len()]; | ||
pub type FE = U64FieldElement<MODULUS>; | ||
|
||
#[inline(never)] | ||
#[export_name = "u32_utils::fp_get_mersenne_primes"] | ||
pub fn get_field_elements() -> (FieldElement<Mersenne31Field>, FieldElement<Mersenne31Field>) { | ||
let x = FieldElement::<Mersenne31Field>::from(521); | ||
let y = FieldElement::<Mersenne31Field>::from(132049); | ||
(x, y) | ||
} | ||
|
||
#[inline(never)] | ||
#[export_name = "u32_utils::fp_squared_mersenne_prime"] | ||
pub fn get_squared_field_element() -> FieldElement<Mersenne31Field> { | ||
let (x, _) = get_field_elements(); | ||
x * x | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[inline(never)] | ||
#[export_name = "u32_utils::rand_field_mersenne_elements"] | ||
pub fn rand_field_elements(order: u64) -> Vec<FE> { | ||
let mut result = Vec::with_capacity(1 << order); | ||
for _ in 0..result.capacity() { | ||
result.push(FE::new(random())); | ||
} | ||
result | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[inline(never)] | ||
#[export_name = "u32_utils::rand_field_mersenne_elements_pair"] | ||
pub fn rand_field_elements_pair() -> (FE, FE) { | ||
(FE::new(random()), FE::new(random())) | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[inline(never)] | ||
#[export_name = "u32_utils::rand_mersenne_poly"] | ||
pub fn rand_poly(order: u64) -> Polynomial<FE> { | ||
Polynomial::new(&rand_field_elements(order)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
math/src/field/fields/fft_friendly/u64_mersenne_montgomery_field.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::{ | ||
field::{ | ||
element::FieldElement, | ||
fields::montgomery_backed_prime_fields::{IsModulus, MontgomeryBackendPrimeField}, | ||
}, | ||
unsigned_integer::element::U64, | ||
}; | ||
|
||
pub type U64MontgomeryBackendPrimeField<T> = MontgomeryBackendPrimeField<T, 1>; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct MontgomeryConfigMersenne31PrimeField; | ||
impl IsModulus<U64> for MontgomeryConfigMersenne31PrimeField { | ||
//Mersenne Prime p = 2^31 - 1 | ||
const MODULUS: U64 = U64::from_u64(2147483647); | ||
} | ||
|
||
pub type Mersenne31MontgomeryPrimeField = | ||
U64MontgomeryBackendPrimeField<MontgomeryConfigMersenne31PrimeField>; | ||
|
||
impl FieldElement<Mersenne31MontgomeryPrimeField> { | ||
pub fn to_bytes_le(&self) -> [u8; 8] { | ||
let limbs = self.representative().limbs; | ||
limbs[0].to_le_bytes() | ||
} | ||
|
||
pub fn to_bytes_be(&self) -> [u8; 8] { | ||
let limbs = self.representative().limbs; | ||
limbs[0].to_be_bytes() | ||
} | ||
} | ||
|
||
impl PartialOrd for FieldElement<Mersenne31MontgomeryPrimeField> { | ||
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { | ||
self.representative().partial_cmp(&other.representative()) | ||
} | ||
} | ||
|
||
impl Ord for FieldElement<Mersenne31MontgomeryPrimeField> { | ||
fn cmp(&self, other: &Self) -> core::cmp::Ordering { | ||
self.representative().cmp(&other.representative()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters