-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix_docs_merkle_tree
- Loading branch information
Showing
10 changed files
with
1,048 additions
and
5 deletions.
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,79 @@ | ||
#![no_main] | ||
|
||
use lambdaworks_math::field::{ | ||
element::FieldElement, | ||
fields::u32_montgomery_backend_prime_field::U32MontgomeryBackendPrimeField, | ||
}; | ||
use libfuzzer_sys::fuzz_target; | ||
use p3_baby_bear::BabyBear; | ||
use p3_field::{Field, FieldAlgebra, PrimeField32}; | ||
|
||
pub type U32Babybear31PrimeField = U32MontgomeryBackendPrimeField<2013265921>; | ||
pub type F = FieldElement<U32Babybear31PrimeField>; | ||
|
||
fuzz_target!(|values: (u32, u32)| { | ||
// Note: we filter values outside of order as it triggers an assert within plonky3 disallowing values n >= Self::Order | ||
let (value_u32_a, value_u32_b) = values; | ||
|
||
if value_u32_a >= 2013265921 || value_u32_b >= 2013265921 { | ||
return; | ||
} | ||
let a = F::from(value_u32_a as u64); | ||
let b = F::from(value_u32_b as u64); | ||
|
||
// Note: if we parse using from_canonical_u32 fails due to check that n < Self::Order | ||
let a_expected = BabyBear::from_canonical_u32(value_u32_a); | ||
let b_expected = BabyBear::from_canonical_u32(value_u32_b); | ||
|
||
let add_u32 = &a + &b; | ||
let addition = a_expected + b_expected; | ||
assert_eq!(add_u32.representative(), addition.as_canonical_u32()); | ||
|
||
let sub_u32 = &a - &b; | ||
let substraction = a_expected - b_expected; | ||
assert_eq!(sub_u32.representative(), substraction.as_canonical_u32()); | ||
|
||
let mul_u32 = &a * &b; | ||
let multiplication = a_expected * b_expected; | ||
assert_eq!(mul_u32.representative(), multiplication.as_canonical_u32()); | ||
|
||
// Axioms soundness | ||
let one = F::one(); | ||
let zero = F::zero(); | ||
|
||
assert_eq!(&a + &zero, a, "Neutral add element a failed"); | ||
assert_eq!(&b + &zero, b, "Neutral mul element b failed"); | ||
assert_eq!(&a * &one, a, "Neutral add element a failed"); | ||
assert_eq!(&b * &one, b, "Neutral mul element b failed"); | ||
|
||
assert_eq!(&a + &b, &b + &a, "Commutative add property failed"); | ||
assert_eq!(&a * &b, &b * &a, "Commutative mul property failed"); | ||
|
||
let c = &a * &b; | ||
assert_eq!( | ||
(&a + &b) + &c, | ||
&a + (&b + &c), | ||
"Associative add property failed" | ||
); | ||
assert_eq!( | ||
(&a * &b) * &c, | ||
&a * (&b * &c), | ||
"Associative mul property failed" | ||
); | ||
|
||
assert_eq!( | ||
&a * (&b + &c), | ||
&a * &b + &a * &c, | ||
"Distributive property failed" | ||
); | ||
|
||
assert_eq!(&a - &a, zero, "Inverse add a failed"); | ||
assert_eq!(&b - &b, zero, "Inverse add b failed"); | ||
|
||
if a != zero { | ||
assert_eq!(&a * a.inv().unwrap(), one, "Inverse mul a failed"); | ||
} | ||
if b != zero { | ||
assert_eq!(&b * b.inv().unwrap(), one, "Inverse mul b failed"); | ||
} | ||
}); |
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
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,216 @@ | ||
use criterion::Criterion; | ||
use std::hint::black_box; | ||
|
||
use lambdaworks_math::field::{ | ||
element::FieldElement, | ||
fields::{ | ||
fft_friendly::babybear::Babybear31PrimeField, | ||
u32_montgomery_backend_prime_field::U32MontgomeryBackendPrimeField, | ||
}, | ||
}; | ||
|
||
use p3_baby_bear::BabyBear; | ||
use p3_field::{Field, FieldAlgebra}; | ||
|
||
use rand::random; | ||
use rand::Rng; | ||
|
||
pub type U32Babybear31PrimeField = U32MontgomeryBackendPrimeField<2013265921>; | ||
pub type F = FieldElement<U32Babybear31PrimeField>; | ||
pub type F64 = FieldElement<Babybear31PrimeField>; | ||
|
||
pub fn rand_field_elements(num: usize) -> Vec<(F, F)> { | ||
let mut result = Vec::with_capacity(num); | ||
for _ in 0..result.capacity() { | ||
result.push((F::from(random::<u64>()), F::from(random::<u64>()))); | ||
} | ||
result | ||
} | ||
|
||
fn rand_babybear_elements_p3(num: usize) -> Vec<(BabyBear, BabyBear)> { | ||
let mut rng = rand::thread_rng(); | ||
(0..num) | ||
.map(|_| (rng.gen::<BabyBear>(), rng.gen::<BabyBear>())) | ||
.collect() | ||
} | ||
|
||
pub fn babybear_ops_benchmarks(c: &mut Criterion) { | ||
let input: Vec<Vec<(F, F)>> = [1000000] | ||
.into_iter() | ||
.map(rand_field_elements) | ||
.collect::<Vec<_>>(); | ||
let mut group = c.benchmark_group("BabyBear operations using Lambdaworks u32"); | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Addition {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, y) in i { | ||
black_box(black_box(x) + black_box(y)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Multiplication {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, y) in i { | ||
black_box(black_box(x) * black_box(y)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Square {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, _) in i { | ||
black_box(black_box(x).square()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Inverse {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, _) in i { | ||
black_box(black_box(x).inv().unwrap()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Division {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, y) in i { | ||
black_box(black_box(x) / black_box(y)); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
pub fn rand_field_elements_u64(num: usize) -> Vec<(F64, F64)> { | ||
let mut result = Vec::with_capacity(num); | ||
for _ in 0..result.capacity() { | ||
result.push((F64::from(random::<u64>()), F64::from(random::<u64>()))); | ||
} | ||
result | ||
} | ||
pub fn babybear_ops_benchmarks_f64(c: &mut Criterion) { | ||
let input: Vec<Vec<(F64, F64)>> = [1000000] | ||
.into_iter() | ||
.map(rand_field_elements_u64) | ||
.collect::<Vec<_>>(); | ||
let mut group = c.benchmark_group("BabyBear operations using Lambdaworks u64"); | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Addition {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, y) in i { | ||
black_box(black_box(x) + black_box(y)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Multiplication {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, y) in i { | ||
black_box(black_box(x) * black_box(y)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Square {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, _) in i { | ||
black_box(black_box(x).square()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Inverse {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, _) in i { | ||
black_box(black_box(x).inv().unwrap()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Division {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, y) in i { | ||
black_box(black_box(x) / black_box(y)); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
pub fn babybear_p3_ops_benchmarks(c: &mut Criterion) { | ||
let input: Vec<Vec<(BabyBear, BabyBear)>> = [1000000] | ||
.into_iter() | ||
.map(rand_babybear_elements_p3) | ||
.collect::<Vec<_>>(); | ||
|
||
let mut group = c.benchmark_group("BabyBear operations using Plonky3"); | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Addition {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, y) in i { | ||
black_box(black_box(*x) + black_box(*y)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Multiplication {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, y) in i { | ||
black_box(black_box(*x) * black_box(*y)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Square {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, _) in i { | ||
black_box(black_box(x).square()); | ||
} | ||
}); | ||
}); | ||
} | ||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Inverse {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, _) in i { | ||
black_box(black_box(x).inverse()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
for i in input.clone().into_iter() { | ||
group.bench_with_input(format!("Division {:?}", &i.len()), &i, |bench, i| { | ||
bench.iter(|| { | ||
for (x, y) in i { | ||
black_box(black_box(*x) / black_box(*y)); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod baby_bear; | ||
pub mod mersenne31; | ||
pub mod mersenne31_montgomery; | ||
pub mod stark252; | ||
|
Oops, something went wrong.