From a47b9565cbb31ad6de833f9f923da27354074a2f Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Fri, 10 Jan 2025 11:40:54 +0100 Subject: [PATCH 1/5] add conversion between SecretKey and blst_scalar --- bindings/rust/src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index a91c4b74..53637efc 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -750,6 +750,13 @@ macro_rules! sig_variant_impl { pub fn from_bytes(sk_in: &[u8]) -> Result { SecretKey::deserialize(sk_in) } + + /// # Safety + /// + /// passed [`blst_scalar`] must be ok as per [`blst_sk_check`] + pub unsafe fn from_scalar_unchecked(value: blst_scalar) -> Self { + SecretKey { value } + } } #[cfg(feature = "serde-secret")] @@ -775,6 +782,25 @@ macro_rules! sig_variant_impl { } } + impl From<&SecretKey> for blst_scalar { + fn from(sk: &SecretKey) -> Self { + sk.value.clone() + } + } + + impl std::convert::TryFrom for SecretKey { + type Error = BLST_ERROR; + + fn try_from(value: blst_scalar) -> Result { + unsafe { + if !blst_sk_check(&value) { + return Err(BLST_ERROR::BLST_BAD_ENCODING); + } + } + Ok(SecretKey { value }) + } + } + #[repr(transparent)] #[derive(Default, Debug, Clone, Copy)] pub struct PublicKey { From e2b35fec0efc2f568ee9045742faa444963019de Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Fri, 10 Jan 2025 12:59:42 +0100 Subject: [PATCH 2/5] add conversion between Signature and $sig_aff --- bindings/rust/src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index 53637efc..eeff7ecf 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -1494,6 +1494,18 @@ macro_rules! sig_variant_impl { } } + impl From for $sig_aff { + fn from(sig: Signature) -> Self { + sig.point + } + } + + impl From<$sig_aff> for Signature { + fn from(point: $sig_aff) -> Self { + Self { point } + } + } + #[repr(transparent)] #[derive(Debug, Clone, Copy)] pub struct AggregateSignature { From 273616b79bb27476b5f0af93e7aa6bccad01136b Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Fri, 10 Jan 2025 13:55:42 +0100 Subject: [PATCH 3/5] add reference versions of conversion --- bindings/rust/src/lib.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index eeff7ecf..cabd0fff 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -782,9 +782,9 @@ macro_rules! sig_variant_impl { } } - impl From<&SecretKey> for blst_scalar { - fn from(sk: &SecretKey) -> Self { - sk.value.clone() + impl<'a> From<&'a SecretKey> for &'a blst_scalar { + fn from(sk: &'a SecretKey) -> Self { + &sk.value } } @@ -1500,6 +1500,12 @@ macro_rules! sig_variant_impl { } } + impl<'a> From<&'a Signature> for &'a $sig_aff { + fn from(sig: &'a Signature) -> Self { + &sig.point + } + } + impl From<$sig_aff> for Signature { fn from(point: $sig_aff) -> Self { Self { point } From 19e5f3494fd8c7efe92a66b968423641ff93b2eb Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Tue, 21 Jan 2025 16:20:48 +0100 Subject: [PATCH 4/5] add remaining conversions --- bindings/rust/src/lib.rs | 54 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index cabd0fff..fd915bf5 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -926,6 +926,24 @@ macro_rules! sig_variant_impl { } } + impl From for $pk_aff { + fn from(pk: PublicKey) -> Self { + pk.point + } + } + + impl<'a> From<&'a PublicKey> for &'a $pk_aff { + fn from(pk: &'a PublicKey) -> Self { + &pk.point + } + } + + impl From<$pk_aff> for PublicKey { + fn from(point: $pk_aff) -> Self { + Self { point } + } + } + #[repr(transparent)] #[derive(Debug, Clone, Copy)] pub struct AggregatePublicKey { @@ -1043,6 +1061,24 @@ macro_rules! sig_variant_impl { } } + impl From for $pk { + fn from(pk: AggregatePublicKey) -> Self { + pk.point + } + } + + impl<'a> From<&'a AggregatePublicKey> for &'a $pk { + fn from(pk: &'a AggregatePublicKey) -> Self { + &pk.point + } + } + + impl From<$pk> for AggregatePublicKey { + fn from(point: $pk) -> Self { + Self { point } + } + } + #[repr(transparent)] #[derive(Debug, Clone, Copy)] pub struct Signature { @@ -1653,6 +1689,24 @@ macro_rules! sig_variant_impl { } } + impl From for $sig { + fn from(sig: AggregateSignature) -> Self { + sig.point + } + } + + impl<'a> From<&'a AggregateSignature> for &'a $sig { + fn from(sig: &'a AggregateSignature) -> Self { + &sig.point + } + } + + impl From<$sig> for AggregateSignature { + fn from(point: $sig) -> Self { + Self { point } + } + } + impl MultiPoint for [PublicKey] { type Output = AggregatePublicKey; From f9dbf4a269c0c479deb257f20a9031aa2f7a3023 Mon Sep 17 00:00:00 2001 From: Daniel Knopik Date: Tue, 28 Jan 2025 17:45:47 +0100 Subject: [PATCH 5/5] use `TryFrom` from `core` instead of `std` --- bindings/rust/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/rust/src/lib.rs b/bindings/rust/src/lib.rs index fd915bf5..6043bd98 100644 --- a/bindings/rust/src/lib.rs +++ b/bindings/rust/src/lib.rs @@ -788,7 +788,7 @@ macro_rules! sig_variant_impl { } } - impl std::convert::TryFrom for SecretKey { + impl core::convert::TryFrom for SecretKey { type Error = BLST_ERROR; fn try_from(value: blst_scalar) -> Result {