From 7fe89c488bfc8d2e4987ba17fd7e13dcd3bb79ba Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Tue, 30 Jan 2024 13:36:54 +0100 Subject: [PATCH] Add byte accessors to `ElligatorSwiftSharedSecret` The inner bytes of `ElligatorSwiftSharedSecret` were almost inaccessible making the type almost useless, so this commit adds methods to access inner bytes. Closes #675 --- src/ellswift.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ellswift.rs b/src/ellswift.rs index 2821be6f1..0bb7e86ef 100644 --- a/src/ellswift.rs +++ b/src/ellswift.rs @@ -266,6 +266,20 @@ impl ElligatorSwift { #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ElligatorSwiftSharedSecret([u8; 32]); +impl ElligatorSwiftSharedSecret { + /// Creates shared secret from bytes. + /// + /// This is generally not needed except for unusual cases like restoring the secret from a + /// database. + pub const fn from_secret_bytes(bytes: [u8; 32]) -> Self { Self(bytes) } + + /// Returns the secret bytes as an array. + pub const fn to_secret_bytes(self) -> [u8; 32] { self.0 } + + /// Returns the secret bytes as a reference to an array. + pub const fn as_secret_bytes(&self) -> &[u8; 32] { &self.0 } +} + /// Represents which party we are in the ECDH, A is the initiator, B is the responder. /// This is important because the hash of the shared secret is different depending on which party /// we are. In this context, "we" means the party that is using this library, and possesses the