Skip to content

Commit

Permalink
add bench for U64 mont and mersenne31
Browse files Browse the repository at this point in the history
  • Loading branch information
PatStiles committed Nov 6, 2023
1 parent b4bed36 commit ffeeb3f
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
2 changes: 2 additions & 0 deletions math/benches/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#![allow(dead_code)]
pub mod fft_functions;
pub mod stark252_utils;
pub mod u32_mont_utils;
pub mod u32_utils;
pub mod u64_goldilocks_utils;
pub mod u64_utils;

Expand Down
61 changes: 61 additions & 0 deletions math/benches/utils/u32_mont_utils.rs
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))
}
57 changes: 57 additions & 0 deletions math/benches/utils/u32_utils.rs
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))
}
2 changes: 2 additions & 0 deletions math/src/field/fields/fft_friendly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
pub mod babybear;
/// Implementation of two-adic Prime Field over 256 bit unsigned integers.
pub mod stark_252_prime_field;
/// Implemenation of the Mersenne Prime field p = 2^31 - 1
pub mod u64_mersenne_montgomery_field;
/// Implemenation of the Goldilocks Prime Field p = 2^64 - 2^32 + 1
pub mod u64_goldilocks;
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())
}
}
2 changes: 1 addition & 1 deletion math/src/field/fields/u32_mersenne_prime_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
/// Invariants:
/// 31st bit is clear
/// n < MODULUS
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct Mersenne31Field;

impl Mersenne31Field {
Expand Down

0 comments on commit ffeeb3f

Please sign in to comment.