2024-05-31 00:52:12 +00:00
|
|
|
use std::{cell::RefCell, ops::DerefMut};
|
2023-11-18 14:00:33 +00:00
|
|
|
|
|
|
|
use multiexp::BatchVerifier as InternalBatchVerifier;
|
|
|
|
use rayon::prelude::*;
|
|
|
|
use thread_local::ThreadLocal;
|
|
|
|
|
2024-05-31 00:52:12 +00:00
|
|
|
/// A multithreaded batch verifier.
|
2023-11-18 14:00:33 +00:00
|
|
|
pub struct MultiThreadedBatchVerifier {
|
2024-05-31 00:52:12 +00:00
|
|
|
internal: ThreadLocal<RefCell<InternalBatchVerifier<(), dalek_ff_group::EdwardsPoint>>>,
|
2023-11-18 14:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MultiThreadedBatchVerifier {
|
|
|
|
/// Create a new multithreaded batch verifier,
|
|
|
|
pub fn new(numb_threads: usize) -> MultiThreadedBatchVerifier {
|
|
|
|
MultiThreadedBatchVerifier {
|
|
|
|
internal: ThreadLocal::with_capacity(numb_threads),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verify(self) -> bool {
|
|
|
|
self.internal
|
|
|
|
.into_iter()
|
2024-05-31 00:52:12 +00:00
|
|
|
.map(RefCell::into_inner)
|
2023-11-18 14:00:33 +00:00
|
|
|
.par_bridge()
|
2024-02-15 16:03:04 +00:00
|
|
|
.find_any(|batch_verifier| !batch_verifier.verify_vartime())
|
2023-11-18 14:00:33 +00:00
|
|
|
.is_none()
|
|
|
|
}
|
|
|
|
}
|
2024-06-07 12:34:53 +00:00
|
|
|
|
|
|
|
impl cuprate_consensus_rules::batch_verifier::BatchVerifier for &'_ MultiThreadedBatchVerifier {
|
|
|
|
fn queue_statement<R>(
|
|
|
|
&mut self,
|
|
|
|
stmt: impl FnOnce(&mut InternalBatchVerifier<(), dalek_ff_group::EdwardsPoint>) -> R,
|
|
|
|
) -> R {
|
|
|
|
let mut verifier = self
|
|
|
|
.internal
|
|
|
|
.get_or(|| RefCell::new(InternalBatchVerifier::new(32)))
|
|
|
|
.borrow_mut();
|
|
|
|
|
|
|
|
stmt(verifier.deref_mut())
|
|
|
|
}
|
|
|
|
}
|