Skip to content

Commit

Permalink
feat(rust): allow to move G1 and G2 elements between threads (#87)
Browse files Browse the repository at this point in the history
* feat: thread-safe elements

* chore: update recent code from develop

* chore: remove arc

* chore: implement Sync for elements

* fix: conflicting implementation
  • Loading branch information
shumkov authored Feb 15, 2024
1 parent 3540b8b commit 1c2fc79
Showing 1 changed file with 27 additions and 0 deletions.
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();
}
}

0 comments on commit 1c2fc79

Please sign in to comment.