Skip to content

Commit

Permalink
add decoding impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Oct 17, 2024
1 parent 4c5511f commit bbd21ba
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
29 changes: 26 additions & 3 deletions rust/catalyst-voting/src/crypto/ed25519/decoding.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,7 +42,9 @@ impl PublicKey {
/// # Errors
/// - Cannot decode public key.
pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> anyhow::Result<Self> {
Ok(Self(VerifyingKey::from_bytes(bytes)?))
Ok(Self(
VerifyingKey::from_bytes(bytes).map_err(|_| anyhow!("Cannot decode public key."))?,
))
}
}

Expand Down
66 changes: 66 additions & 0 deletions rust/catalyst-voting/src/vote_protocol/committee/decoding.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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<Self> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Module containing all primitives related to the committee.
mod decoding;

use rand_core::CryptoRngCore;

use crate::crypto::{
Expand Down

0 comments on commit bbd21ba

Please sign in to comment.