From bbd21ba236a064df7e149d49e979df135ea59fe8 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Thu, 17 Oct 2024 11:06:17 +0300 Subject: [PATCH] add decoding impl --- .../src/crypto/ed25519/decoding.rs | 29 +++++++- .../src/vote_protocol/committee/decoding.rs | 66 +++++++++++++++++++ .../{committee.rs => committee/mod.rs} | 2 + 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 rust/catalyst-voting/src/vote_protocol/committee/decoding.rs rename rust/catalyst-voting/src/vote_protocol/{committee.rs => committee/mod.rs} (98%) diff --git a/rust/catalyst-voting/src/crypto/ed25519/decoding.rs b/rust/catalyst-voting/src/crypto/ed25519/decoding.rs index ab4b5daa7..f40bf5416 100644 --- a/rust/catalyst-voting/src/crypto/ed25519/decoding.rs +++ b/rust/catalyst-voting/src/crypto/ed25519/decoding.rs @@ -1,10 +1,31 @@ //! `Ed25519` objects decoding implementation +use anyhow::anyhow; use ed25519_dalek::{ - Signature as Ed25519Signature, VerifyingKey, PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH, + Signature as Ed25519Signature, SigningKey, VerifyingKey, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, + SIGNATURE_LENGTH, }; -use super::{PublicKey, Signature}; +use super::{PrivateKey, PublicKey, Signature}; + +impl PrivateKey { + /// `PrivateKey` bytes size + pub const BYTES_SIZE: usize = SECRET_KEY_LENGTH; + + /// Convert this `PrivateKey` to its underlying sequence of bytes. + #[must_use] + pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] { + self.0.to_bytes() + } + + /// Attempt to construct a `PrivateKey` from a byte representation. + /// + /// # Errors + /// - Cannot decode public key. + pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> Self { + Self(SigningKey::from_bytes(bytes)) + } +} impl PublicKey { /// `PublicKey` bytes size @@ -21,7 +42,9 @@ impl PublicKey { /// # Errors /// - Cannot decode public key. pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> anyhow::Result { - Ok(Self(VerifyingKey::from_bytes(bytes)?)) + Ok(Self( + VerifyingKey::from_bytes(bytes).map_err(|_| anyhow!("Cannot decode public key."))?, + )) } } diff --git a/rust/catalyst-voting/src/vote_protocol/committee/decoding.rs b/rust/catalyst-voting/src/vote_protocol/committee/decoding.rs new file mode 100644 index 000000000..3d2630c0b --- /dev/null +++ b/rust/catalyst-voting/src/vote_protocol/committee/decoding.rs @@ -0,0 +1,66 @@ +//! committee objects decoding implementation + +use anyhow::anyhow; + +use super::{ElectionPublicKey, ElectionSecretKey, GroupElement, Scalar}; + +impl ElectionSecretKey { + /// `ElectionSecretKey` bytes size + pub const BYTES_SIZE: usize = Scalar::BYTES_SIZE; + + /// Convert this `ElectionSecretKey` to its underlying sequence of bytes. + #[must_use] + pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] { + self.0.to_bytes() + } + + /// Attempt to construct a `ElectionSecretKey` from a byte representation. + /// + /// # Errors + /// - Cannot decode election secret key. + pub fn from_bytes(bytes: [u8; Self::BYTES_SIZE]) -> anyhow::Result { + Ok(Self(Scalar::from_bytes(bytes).map_err(|_| { + anyhow!("Cannot decode election secret key.") + })?)) + } +} + +impl ElectionPublicKey { + /// `ElectionPublicKey` bytes size + pub const BYTES_SIZE: usize = GroupElement::BYTES_SIZE; + + /// Convert this `ElectionPublicKey` to its underlying sequence of bytes. + #[must_use] + pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] { + self.0.to_bytes() + } + + /// Attempt to construct a `ElectionPublicKey` from a byte representation. + /// + /// # Errors + /// - Cannot decode election public key. + pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> anyhow::Result { + Ok(Self( + GroupElement::from_bytes(bytes).map_err(|_| anyhow!("Cannot decode public key."))?, + )) + } +} + +#[cfg(test)] +mod tests { + use test_strategy::proptest; + + use super::*; + + #[proptest] + fn election_keys_to_bytes_from_bytes_test(sk1: ElectionSecretKey) { + let bytes = sk1.to_bytes(); + let sk2 = ElectionSecretKey::from_bytes(bytes).unwrap(); + assert_eq!(sk1, sk2); + + let pk1 = sk1.public_key(); + let bytes = pk1.to_bytes(); + let pk2 = ElectionPublicKey::from_bytes(&bytes).unwrap(); + assert_eq!(pk1, pk2); + } +} diff --git a/rust/catalyst-voting/src/vote_protocol/committee.rs b/rust/catalyst-voting/src/vote_protocol/committee/mod.rs similarity index 98% rename from rust/catalyst-voting/src/vote_protocol/committee.rs rename to rust/catalyst-voting/src/vote_protocol/committee/mod.rs index 551dfe594..0f11ea0bf 100644 --- a/rust/catalyst-voting/src/vote_protocol/committee.rs +++ b/rust/catalyst-voting/src/vote_protocol/committee/mod.rs @@ -1,5 +1,7 @@ //! Module containing all primitives related to the committee. +mod decoding; + use rand_core::CryptoRngCore; use crate::crypto::{