diff --git a/rust-bindings/bls-signatures/src/elements.rs b/rust-bindings/bls-signatures/src/elements.rs index 38d191675..b671bdf6b 100644 --- a/rust-bindings/bls-signatures/src/elements.rs +++ b/rust-bindings/bls-signatures/src/elements.rs @@ -23,6 +23,10 @@ pub struct G1Element { pub(crate) c_element: *mut c_void, } +// G1Element is immutable and thread safe +unsafe impl Send for G1Element {} +unsafe impl Sync for G1Element {} + impl PartialEq for G1Element { fn eq(&self, other: &Self) -> bool { unsafe { G1ElementIsEqual(self.c_element, other.c_element) } @@ -194,6 +198,10 @@ pub struct G2Element { pub(crate) c_element: *mut c_void, } +// G2Element is immutable and thread safe +unsafe impl Send for G2Element {} +unsafe impl Sync for G2Element {} + impl PartialEq for G2Element { fn eq(&self, other: &Self) -> bool { unsafe { G2ElementIsEqual(self.c_element, other.c_element) } @@ -327,6 +335,7 @@ impl Drop for G2Element { #[cfg(test)] mod tests { + use std::thread; use super::*; use crate::{ schemes::{AugSchemeMPL, Scheme}, @@ -381,4 +390,22 @@ mod tests { assert_eq!(g1_element.fingerprint(), 2093959050); } + + #[test] + fn should_be_thread_safe() { + let bytes = [ + 151, 241, 211, 167, 49, 151, 215, 148, 38, 149, 99, 140, 79, 169, 172, 15, 195, 104, + 140, 79, 151, 116, 185, 5, 161, 78, 58, 63, 23, 27, 172, 88, 108, 85, 232, 63, 249, + 122, 26, 239, 251, 58, 240, 10, 219, 34, 198, 187, + ]; + + let g1_element = + G1Element::from_bytes(&bytes).expect("should create g1 element from bytes"); + + let test_thread = thread::spawn(move|| { + assert_eq!(g1_element.fingerprint(), 2093959050); + }); + + test_thread.join().unwrap(); + } }