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

feat(rust): allow to move G1 and G2 elements between threads #87

Merged
merged 6 commits into from
Feb 15, 2024
Merged
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
27 changes: 27 additions & 0 deletions rust-bindings/bls-signatures/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -327,6 +335,7 @@ impl Drop for G2Element {

#[cfg(test)]
mod tests {
use std::thread;
use super::*;
use crate::{
schemes::{AugSchemeMPL, Scheme},
Expand Down Expand Up @@ -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();
}
}
Loading