Skip to content

Commit

Permalink
bindings/rust/src/lib.rs: add From traits to facilitate raw blst_sk_*…
Browse files Browse the repository at this point in the history
… calls.

Fixes #248.
  • Loading branch information
dot-asm committed Feb 11, 2025
1 parent 3a98b4b commit ba2abbd
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ macro_rules! sig_variant_impl {
$sig_aggr_in_group:ident,
) => {
/// Secret Key
#[repr(transparent)]
#[derive(Default, Debug, Clone, Zeroize)]
#[zeroize(drop)]
pub struct SecretKey {
Expand Down Expand Up @@ -775,6 +776,29 @@ macro_rules! sig_variant_impl {
}
}

// From<by-value> traits are not provided to discourage duplication
// of the secret key material.
impl<'a> From<&'a SecretKey> for &'a blst_scalar {
fn from(sk: &'a SecretKey) -> Self {
unsafe {
transmute::<&SecretKey, Self>(sk)
}
}
}

impl<'a> core::convert::TryFrom<&'a blst_scalar> for &'a SecretKey {
type Error = BLST_ERROR;

fn try_from(sk: &'a blst_scalar) -> Result<Self, Self::Error> {
unsafe {
if !blst_sk_check(sk) {
return Err(BLST_ERROR::BLST_BAD_ENCODING);
}
Ok(transmute::<&blst_scalar, Self>(sk))
}
}
}

#[repr(transparent)]
#[derive(Default, Debug, Clone, Copy)]
pub struct PublicKey {
Expand Down

0 comments on commit ba2abbd

Please sign in to comment.