Skip to content

Commit ba2abbd

Browse files
committed
bindings/rust/src/lib.rs: add From traits to facilitate raw blst_sk_* calls.
Fixes #248.
1 parent 3a98b4b commit ba2abbd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

bindings/rust/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ macro_rules! sig_variant_impl {
547547
$sig_aggr_in_group:ident,
548548
) => {
549549
/// Secret Key
550+
#[repr(transparent)]
550551
#[derive(Default, Debug, Clone, Zeroize)]
551552
#[zeroize(drop)]
552553
pub struct SecretKey {
@@ -775,6 +776,29 @@ macro_rules! sig_variant_impl {
775776
}
776777
}
777778

779+
// From<by-value> traits are not provided to discourage duplication
780+
// of the secret key material.
781+
impl<'a> From<&'a SecretKey> for &'a blst_scalar {
782+
fn from(sk: &'a SecretKey) -> Self {
783+
unsafe {
784+
transmute::<&SecretKey, Self>(sk)
785+
}
786+
}
787+
}
788+
789+
impl<'a> core::convert::TryFrom<&'a blst_scalar> for &'a SecretKey {
790+
type Error = BLST_ERROR;
791+
792+
fn try_from(sk: &'a blst_scalar) -> Result<Self, Self::Error> {
793+
unsafe {
794+
if !blst_sk_check(sk) {
795+
return Err(BLST_ERROR::BLST_BAD_ENCODING);
796+
}
797+
Ok(transmute::<&blst_scalar, Self>(sk))
798+
}
799+
}
800+
}
801+
778802
#[repr(transparent)]
779803
#[derive(Default, Debug, Clone, Copy)]
780804
pub struct PublicKey {

0 commit comments

Comments
 (0)