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

chore: Make batch_inverse single threaded #241

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion cryptography/bls12_381/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ repository = { workspace = true }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rayon = { workspace = true }

blst = { version = "0.3", default-features = false }

Expand Down
29 changes: 1 addition & 28 deletions cryptography/bls12_381/src/batch_inversion.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
use rayon::prelude::*;

/// Given a vector of field elements {v_i}, compute the vector {v_i^(-1)}
//
/// Panics: If any of the elements are zero
pub fn batch_inverse<F: ff::Field>(elements: &mut [F]) {
batch_inversion(elements)
}

/// Given a vector of field elements {v_i}, compute the vector {v_i^(-1)}
///
// Taken and modified from arkworks codebase
fn batch_inversion<F: ff::Field>(v: &mut [F]) {
// Divide the vector v evenly between all available cores
let min_elements_per_thread = 1;
let num_cpus_available = rayon::current_num_threads();
let num_elems = v.len();
let num_elem_per_thread =
std::cmp::max(num_elems / num_cpus_available, min_elements_per_thread);

// Batch invert in parallel, without copying the vector
v.par_chunks_mut(num_elem_per_thread).for_each(|chunk| {
serial_batch_inversion(chunk);
});
}

/// Given a vector of field elements {v_i}, compute the vector {coeff * v_i^(-1)}
/// This method is explicitly single core.
fn serial_batch_inversion<F: ff::Field>(v: &mut [F]) {
pub fn batch_inverse<F: ff::Field>(v: &mut [F]) {
// Montgomery’s Trick and Fast Implementation of Masked AES
// Genelle, Prouff and Quisquater
// Section 3.2
Expand Down
Loading