Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FieldElement benchmarks #511

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions benches/benches/add.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
use std::ops::Add;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lambdaworks_math::traits::ByteConversion;
use rand::RngCore;
use utils::generate_random_elements;

use crate::utils::to_lambdaworks_vec;

pub mod utils;

const BENCHMARK_NAME: &str = "add";

pub fn criterion_benchmark(c: &mut Criterion) {
let arkworks_vec = generate_random_elements();

// arkworks-ff
{
use ark_std::{test_rng, UniformRand};
use ark_test_curves::starknet_fp::Fq as F;

let mut rng = test_rng();

let mut v = Vec::new();
for _i in 0..10000 {
let a = F::rand(&mut rng);
v.push(a);
}

c.bench_function(
&format!(
"{} | ark-ff - branch: faster-benchmarks-and-starknet-field",
BENCHMARK_NAME
),
|b| {
b.iter(|| {
let mut iter = v.iter();
let mut iter = arkworks_vec.iter();

for _i in 0..5000 {
let a = iter.next().unwrap();
Expand All @@ -41,22 +35,11 @@ pub fn criterion_benchmark(c: &mut Criterion) {

// lambdaworks-math
{
use lambdaworks_math::field::{
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
};
let mut v = Vec::new();
let mut buf = [0u8; 32];
for _i in 0..10000 {
rand::thread_rng().fill_bytes(&mut buf[..]);

let a = FieldElement::<Stark252PrimeField>::from_bytes_be(&buf).unwrap();

v.push(a);
}
let lambdaworks_vec = to_lambdaworks_vec(&arkworks_vec);

c.bench_function(&format!("{} | lambdaworks", BENCHMARK_NAME,), |b| {
b.iter(|| {
let mut iter = v.iter();
let mut iter = lambdaworks_vec.iter();

for _i in 0..5000 {
let a = iter.next().unwrap();
Expand Down
37 changes: 10 additions & 27 deletions benches/benches/invert.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
use ark_ff::Field;
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use lambdaworks_math::traits::ByteConversion;
use rand::RngCore;
use utils::generate_random_elements;

use crate::utils::to_lambdaworks_vec;

pub mod utils;

const BENCHMARK_NAME: &str = "invert";

pub fn criterion_benchmark(c: &mut Criterion) {
let arkworks_vec = generate_random_elements();

// arkworks-ff
{
use ark_std::{test_rng, UniformRand};
use ark_test_curves::starknet_fp::Fq as F;

let mut rng = test_rng();

let mut v = Vec::new();
for _i in 0..10000 {
let a = F::rand(&mut rng);
v.push(a);
}

c.bench_function(
&format!(
"{} | ark-ff - branch: faster-benchmarks-and-starknet-field",
BENCHMARK_NAME
),
|b| {
b.iter_batched(
|| v.clone(),
|| arkworks_vec.clone(),
|mut v| {
for mut elem in v.iter_mut() {
black_box(black_box(&mut elem).inverse_in_place());
Expand All @@ -40,22 +34,11 @@ pub fn criterion_benchmark(c: &mut Criterion) {

// lambdaworks-math
{
use lambdaworks_math::field::{
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
};
let mut v = Vec::new();
let mut buf = [0u8; 32];
for _i in 0..10000 {
rand::thread_rng().fill_bytes(&mut buf[..]);

let a = FieldElement::<Stark252PrimeField>::from_bytes_be(&buf).unwrap();

v.push(a);
}
let lambdaworks_vec = to_lambdaworks_vec(&arkworks_vec);

c.bench_function(&format!("{} | lambdaworks", BENCHMARK_NAME,), |b| {
b.iter(|| {
for elem in v.iter() {
for elem in lambdaworks_vec.iter() {
black_box(black_box(&elem).inv());
}
});
Expand Down
40 changes: 11 additions & 29 deletions benches/benches/mul.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::ops::Mul;
use utils::generate_random_elements;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lambdaworks_math::traits::ByteConversion;
use rand::RngCore;
use crate::utils::to_lambdaworks_vec;

pub mod utils;

const BENCHMARK_NAME: &str = "mul";

pub fn criterion_benchmark(c: &mut Criterion) {
let arkworks_vec = generate_random_elements();

// arkworks-ff
{
use ark_std::{test_rng, UniformRand};
use ark_test_curves::starknet_fp::Fq as F;

let mut rng = test_rng();

let mut v = Vec::new();
for _i in 0..10000 {
let a = F::rand(&mut rng);
v.push(a);
}

c.bench_function(
&format!(
"{} | ark-ff - branch: faster-benchmarks-and-starknet-field",
BENCHMARK_NAME
),
|b| {
b.iter(|| {
let mut iter = v.iter();
let mut iter = arkworks_vec.iter();

for _i in 0..5000 {
let a = iter.next().unwrap();
let b = iter.next().unwrap();
black_box(black_box(&a).mul(black_box(b)));
black_box(black_box(a).mul(black_box(b)));
}
});
},
Expand All @@ -41,22 +34,11 @@ pub fn criterion_benchmark(c: &mut Criterion) {

// lambdaworks-math
{
use lambdaworks_math::field::{
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
};
let mut v = Vec::new();
let mut buf = [0u8; 32];
for _i in 0..10000 {
rand::thread_rng().fill_bytes(&mut buf[..]);

let a = FieldElement::<Stark252PrimeField>::from_bytes_be(&buf).unwrap();

v.push(a);
}
let lambdaworks_vec = to_lambdaworks_vec(&arkworks_vec);

c.bench_function(&format!("{} | lambdaworks", BENCHMARK_NAME,), |b| {
b.iter(|| {
let mut iter = v.iter();
let mut iter = lambdaworks_vec.iter();

for _i in 0..5000 {
let a = iter.next().unwrap();
Expand Down
57 changes: 16 additions & 41 deletions benches/benches/pow.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
use ark_ff::Field;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lambdaworks_math::traits::ByteConversion;
use rand::Rng;
use rand::RngCore;
use utils::generate_random_elements;

const BENCHMARK_NAME: &str = "pow";
use crate::utils::to_lambdaworks_vec;

pub fn criterion_benchmark(c: &mut Criterion) {
// arkworks-ff
{
use ark_std::{test_rng, UniformRand};
use ark_test_curves::starknet_fp::Fq as F;
pub mod utils;

let mut rng = test_rng();

let mut v = Vec::new();
for _i in 0..10000 {
let a = F::rand(&mut rng);
v.push(a);
}
const BENCHMARK_NAME: &str = "pow";

let mut rng = rand::thread_rng();
pub fn criterion_benchmark(c: &mut Criterion) {
let arkworks_vec = generate_random_elements();

let mut v_ints = Vec::new();
for _i in 0..10000 {
v_ints.push(rng.gen::<u64>());
}
let mut rng = rand::thread_rng();
let mut v_ints = Vec::new();
for _i in 0..10000 {
v_ints.push(rng.gen::<u64>());
}

// arkworks-ff
{
c.bench_function(
&format!(
"{} | ark-ff - branch: faster-benchmarks-and-starknet-field",
BENCHMARK_NAME
),
|b| {
b.iter(|| {
let mut iter = v.iter();
let mut iter = arkworks_vec.iter();
let mut iter_ints = v_ints.iter();

for _i in 0..10000 {
Expand All @@ -49,29 +42,11 @@ pub fn criterion_benchmark(c: &mut Criterion) {

// lambdaworks-math
{
use lambdaworks_math::field::{
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
};
let mut v = Vec::new();
let mut buf = [0u8; 32];
for _i in 0..10000 {
rand::thread_rng().fill_bytes(&mut buf[..]);

let a = FieldElement::<Stark252PrimeField>::from_bytes_be(&buf).unwrap();

v.push(a);
}

let mut rng = rand::thread_rng();

let mut v_ints = Vec::new();
for _i in 0..10000 {
v_ints.push(rng.gen::<u64>());
}
let lambdaworks_vec = to_lambdaworks_vec(&arkworks_vec);

c.bench_function(&format!("{} | lambdaworks", BENCHMARK_NAME,), |b| {
b.iter(|| {
let mut iter = v.iter();
let mut iter = lambdaworks_vec.iter();
let mut iter_ints = v_ints.iter();

for _i in 0..10000 {
Expand Down
45 changes: 18 additions & 27 deletions benches/benches/sqrt.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
use ark_ff::Field;
use ark_std::{test_rng, UniformRand};
use ark_test_curves::starknet_fp::Fq as F;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lambdaworks_math::traits::ByteConversion;
use rand::RngCore;

use crate::utils::to_lambdaworks_vec;

pub mod utils;

const BENCHMARK_NAME: &str = "sqrt";

pub fn criterion_benchmark(c: &mut Criterion) {
// arkworks-ff
{
use ark_std::{test_rng, UniformRand};
use ark_test_curves::starknet_fp::Fq as F;
let mut rng = test_rng();

let mut rng = test_rng();

let mut v = Vec::new();
for _i in 0..1000 {
let a = F::rand(&mut rng);
let square = a * a;
v.push(square);
}
let mut arkworks_vec = Vec::new();
for _i in 0..1000 {
let a = F::rand(&mut rng);
let square = a * a;
arkworks_vec.push(square);
}

// arkworks-ff
{
c.bench_function(
&format!(
"{} | ark-ff - branch: faster-benchmarks-and-starknet-field",
BENCHMARK_NAME
),
|b| {
b.iter(|| {
let mut iter = v.iter();
let mut iter = arkworks_vec.iter();

for _i in 0..1000 {
let a = iter.next().unwrap();
Expand All @@ -40,21 +41,11 @@ pub fn criterion_benchmark(c: &mut Criterion) {

// lambdaworks-math
{
use lambdaworks_math::field::{
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
};
let mut v = Vec::new();
let mut buf = [0u8; 32];
for _i in 0..1000 {
rand::thread_rng().fill_bytes(&mut buf[..]);
let a = FieldElement::<Stark252PrimeField>::from_bytes_be(&buf).unwrap();
let square = a * a;
v.push(square);
}
let lambdaworks_vec = to_lambdaworks_vec(&arkworks_vec);

c.bench_function(&format!("{} | lambdaworks", BENCHMARK_NAME,), |b| {
b.iter(|| {
let mut iter = v.iter();
let mut iter = lambdaworks_vec.iter();

for _i in 0..1000 {
let a = iter.next().unwrap();
Expand Down
Loading