Skip to content

Commit

Permalink
feat: thread-safe elements
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Feb 10, 2024
1 parent 881e6d7 commit 7a232e3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 39 deletions.
2 changes: 1 addition & 1 deletion rust-bindings/bls-signatures/src/bip32/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl ExtendedPrivateKey {
Ok(G1Element {
c_element: c_err_to_result(|did_err| unsafe {
BIP32ExtendedPrivateKeyGetPublicKey(self.c_extended_private_key, did_err)
})?,
})?.into(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion rust-bindings/bls-signatures/src/bip32/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl ExtendedPublicKey {

pub fn public_key(&self) -> G1Element {
G1Element {
c_element: unsafe { BIP32ExtendedPublicKeyGetPublicKey(self.c_extended_public_key) },
c_element: unsafe { BIP32ExtendedPublicKeyGetPublicKey(self.c_extended_public_key) }.into(),
}
}
}
Expand Down
55 changes: 40 additions & 15 deletions rust-bindings/bls-signatures/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ use bls_dash_sys::{
G2ElementFromBytes, G2ElementIsEqual, G2ElementSerialize,
};

use std::sync::Arc;

use crate::{schemes::Scheme, utils::c_err_to_result, BlsError};

// TODO Split into modules

pub const G1_ELEMENT_SIZE: usize = 48; // TODO somehow extract it from bls library
pub const G2_ELEMENT_SIZE: usize = 96; // TODO somehow extract it from bls library

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct G1Element {
pub(crate) c_element: *mut c_void,
pub(crate) c_element: Arc<*mut c_void>,
}

unsafe impl Send for G1Element {}

impl PartialEq for G1Element {
fn eq(&self, other: &Self) -> bool {
unsafe { G1ElementIsEqual(self.c_element, other.c_element) }
unsafe { G1ElementIsEqual(*self.c_element, *other.c_element) }
}
}

Expand All @@ -30,7 +34,7 @@ impl G1Element {
pub fn generate() -> Self {
let c_element = unsafe { G1ElementGenerator() };

G1Element { c_element }
G1Element { c_element: c_element.into() }
}

pub(crate) fn from_bytes_with_legacy_flag(
Expand All @@ -49,7 +53,7 @@ impl G1Element {
Ok(G1Element {
c_element: c_err_to_result(|did_err| unsafe {
G1ElementFromBytes(bytes.as_ptr() as *const _, legacy, did_err)
})?,
})?.into(),
})
}

Expand All @@ -59,7 +63,7 @@ impl G1Element {

pub(crate) fn serialize_with_legacy_flag(&self, legacy: bool) -> Box<[u8; G1_ELEMENT_SIZE]> {
unsafe {
let malloc_ptr = G1ElementSerialize(self.c_element, legacy);
let malloc_ptr = G1ElementSerialize(*self.c_element, legacy);
Box::from_raw(malloc_ptr as *mut _)
}
}
Expand All @@ -75,13 +79,13 @@ impl G1Element {
) -> G1Element {
G1Element {
c_element: unsafe {
CoreMPLDeriveChildPkUnhardened(scheme.as_mut_ptr(), self.c_element, index)
},
CoreMPLDeriveChildPkUnhardened(scheme.as_mut_ptr(), *self.c_element, index)
}.into(),
}
}

pub(crate) fn fingerprint_with_legacy_flag(&self, legacy: bool) -> u32 {
unsafe { G1ElementGetFingerprint(self.c_element, legacy) }
unsafe { G1ElementGetFingerprint(*self.c_element, legacy) }
}

pub fn fingerprint(&self) -> u32 {
Expand All @@ -91,18 +95,20 @@ impl G1Element {

impl Drop for G1Element {
fn drop(&mut self) {
unsafe { G1ElementFree(self.c_element) }
unsafe { G1ElementFree(*self.c_element) }
}
}

#[derive(Debug)]
pub struct G2Element {
pub(crate) c_element: *mut c_void,
pub(crate) c_element: Arc<*mut c_void>,
}

unsafe impl Send for G2Element {}

impl PartialEq for G2Element {
fn eq(&self, other: &Self) -> bool {
unsafe { G2ElementIsEqual(self.c_element, other.c_element) }
unsafe { G2ElementIsEqual(*self.c_element, *other.c_element) }
}
}

Expand All @@ -125,7 +131,7 @@ impl G2Element {
Ok(G2Element {
c_element: c_err_to_result(|did_err| unsafe {
G2ElementFromBytes(bytes.as_ptr() as *const _, legacy, did_err)
})?,
})?.into(),
})
}

Expand All @@ -135,7 +141,7 @@ impl G2Element {

pub(crate) fn serialize_with_legacy_flag(&self, legacy: bool) -> Box<[u8; G2_ELEMENT_SIZE]> {
unsafe {
let malloc_ptr = G2ElementSerialize(self.c_element, legacy);
let malloc_ptr = G2ElementSerialize(*self.c_element, legacy);
Box::from_raw(malloc_ptr as *mut _)
}
}
Expand All @@ -147,12 +153,13 @@ impl G2Element {

impl Drop for G2Element {
fn drop(&mut self) {
unsafe { G2ElementFree(self.c_element) }
unsafe { G2ElementFree(*self.c_element) }
}
}

#[cfg(test)]
mod tests {
use std::thread;
use super::*;
use crate::{
schemes::{AugSchemeMPL, Scheme},
Expand Down Expand Up @@ -207,4 +214,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();
}
}
2 changes: 1 addition & 1 deletion rust-bindings/bls-signatures/src/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl PrivateKey {
Ok(G1Element {
c_element: c_err_to_result(|did_err| unsafe {
PrivateKeyGetG1Element(self.c_private_key, did_err)
})?,
})?.into(),
})
}

Expand Down
42 changes: 21 additions & 21 deletions rust-bindings/bls-signatures/src/schemes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ pub trait Scheme {
) -> bool {
let mut g1_pointers = public_keys
.into_iter()
.map(|g1| g1.c_element)
.map(|g1| *g1.c_element)
.collect::<Vec<_>>();

unsafe {
CoreMPLVerifySecure(
self.as_mut_ptr(),
g1_pointers.as_mut_ptr(),
g1_pointers.len(),
signature.c_element,
*signature.c_element,
message.as_ptr() as *const _,
message.len(),
)
Expand All @@ -47,7 +47,7 @@ pub trait Scheme {
) -> G1Element {
let mut g1_pointers = public_keys
.into_iter()
.map(|g1| g1.c_element)
.map(|g1| *g1.c_element)
.collect::<Vec<_>>();
G1Element {
c_element: unsafe {
Expand All @@ -56,20 +56,20 @@ pub trait Scheme {
g1_pointers.as_mut_ptr(),
g1_pointers.len(),
)
},
}.into(),
}
}

fn aggregate_sigs<'a>(&self, sigs: impl IntoIterator<Item = &'a G2Element>) -> G2Element {
let mut g2_pointers = sigs.into_iter().map(|g2| g2.c_element).collect::<Vec<_>>();
let mut g2_pointers = sigs.into_iter().map(|g2| *g2.c_element).collect::<Vec<_>>();
G2Element {
c_element: unsafe {
CoreMPLAggregateSigs(
self.as_mut_ptr(),
g2_pointers.as_mut_ptr(),
g2_pointers.len(),
)
},
}.into(),
}
}

Expand All @@ -94,7 +94,7 @@ fn prepare_aggregate_verify_args<'a>(
) -> AggregateVerifyArgs {
let g1_pointers = public_keys
.into_iter()
.map(|g1| g1.c_element)
.map(|g1| *g1.c_element)
.collect::<Vec<_>>();

let mut messages_pointers = Vec::new();
Expand Down Expand Up @@ -138,18 +138,18 @@ impl Scheme for BasicSchemeMPL {
message.as_ptr() as *const _,
message.len(),
)
},
}.into(),
}
}

fn verify(&self, public_key: &G1Element, message: &[u8], signature: &G2Element) -> bool {
unsafe {
CoreMPLVerify(
self.scheme,
public_key.c_element,
*public_key.c_element,
message.as_ptr() as *const _,
message.len(),
signature.c_element,
*signature.c_element,
)
}
}
Expand All @@ -174,7 +174,7 @@ impl Scheme for BasicSchemeMPL {
messages_pointers.as_mut_ptr() as *mut _,
messages_lengthes.as_mut_ptr() as *mut _,
messages_pointers.len(),
signature.c_element,
*signature.c_element,
)
}
}
Expand Down Expand Up @@ -206,18 +206,18 @@ impl Scheme for LegacySchemeMPL {
message.as_ptr() as *const _,
message.len(),
)
},
}.into(),
}
}

fn verify(&self, public_key: &G1Element, message: &[u8], signature: &G2Element) -> bool {
unsafe {
LegacySchemeMPLVerify(
self.scheme,
public_key.c_element,
*public_key.c_element,
message.as_ptr() as *const _,
message.len(),
signature.c_element,
*signature.c_element,
)
}
}
Expand All @@ -230,15 +230,15 @@ impl Scheme for LegacySchemeMPL {
) -> bool {
let mut g1_pointers = public_keys
.into_iter()
.map(|g1| g1.c_element)
.map(|g1| *g1.c_element)
.collect::<Vec<_>>();

unsafe {
LegacySchemeMPLVerifySecure(
self.as_mut_ptr(),
g1_pointers.as_mut_ptr(),
g1_pointers.len(),
signature.c_element,
*signature.c_element,
message.as_ptr() as *const _,
message.len(),
)
Expand All @@ -265,7 +265,7 @@ impl Scheme for LegacySchemeMPL {
messages_pointers.as_mut_ptr() as *mut _,
messages_lengthes.as_mut_ptr() as *mut _,
messages_pointers.len(),
signature.c_element,
*signature.c_element,
)
}
}
Expand Down Expand Up @@ -303,18 +303,18 @@ impl Scheme for AugSchemeMPL {
message.as_ptr() as *const _,
message.len(),
)
},
}.into(),
}
}

fn verify(&self, public_key: &G1Element, message: &[u8], signature: &G2Element) -> bool {
unsafe {
AugSchemeMPLVerify(
self.scheme,
public_key.c_element,
*public_key.c_element,
message.as_ptr() as *const _,
message.len(),
signature.c_element,
*signature.c_element,
)
}
}
Expand All @@ -339,7 +339,7 @@ impl Scheme for AugSchemeMPL {
messages_pointers.as_mut_ptr() as *mut _,
messages_lengths.as_mut_ptr() as *mut _,
messages_pointers.len(),
signature.c_element,
*signature.c_element,
)
}
}
Expand Down

0 comments on commit 7a232e3

Please sign in to comment.