Skip to content

Commit bbd21ba

Browse files
committed
add decoding impl
1 parent 4c5511f commit bbd21ba

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

rust/catalyst-voting/src/crypto/ed25519/decoding.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
//! `Ed25519` objects decoding implementation
22
3+
use anyhow::anyhow;
34
use ed25519_dalek::{
4-
Signature as Ed25519Signature, VerifyingKey, PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH,
5+
Signature as Ed25519Signature, SigningKey, VerifyingKey, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH,
6+
SIGNATURE_LENGTH,
57
};
68

7-
use super::{PublicKey, Signature};
9+
use super::{PrivateKey, PublicKey, Signature};
10+
11+
impl PrivateKey {
12+
/// `PrivateKey` bytes size
13+
pub const BYTES_SIZE: usize = SECRET_KEY_LENGTH;
14+
15+
/// Convert this `PrivateKey` to its underlying sequence of bytes.
16+
#[must_use]
17+
pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] {
18+
self.0.to_bytes()
19+
}
20+
21+
/// Attempt to construct a `PrivateKey` from a byte representation.
22+
///
23+
/// # Errors
24+
/// - Cannot decode public key.
25+
pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> Self {
26+
Self(SigningKey::from_bytes(bytes))
27+
}
28+
}
829

930
impl PublicKey {
1031
/// `PublicKey` bytes size
@@ -21,7 +42,9 @@ impl PublicKey {
2142
/// # Errors
2243
/// - Cannot decode public key.
2344
pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> anyhow::Result<Self> {
24-
Ok(Self(VerifyingKey::from_bytes(bytes)?))
45+
Ok(Self(
46+
VerifyingKey::from_bytes(bytes).map_err(|_| anyhow!("Cannot decode public key."))?,
47+
))
2548
}
2649
}
2750

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! committee objects decoding implementation
2+
3+
use anyhow::anyhow;
4+
5+
use super::{ElectionPublicKey, ElectionSecretKey, GroupElement, Scalar};
6+
7+
impl ElectionSecretKey {
8+
/// `ElectionSecretKey` bytes size
9+
pub const BYTES_SIZE: usize = Scalar::BYTES_SIZE;
10+
11+
/// Convert this `ElectionSecretKey` to its underlying sequence of bytes.
12+
#[must_use]
13+
pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] {
14+
self.0.to_bytes()
15+
}
16+
17+
/// Attempt to construct a `ElectionSecretKey` from a byte representation.
18+
///
19+
/// # Errors
20+
/// - Cannot decode election secret key.
21+
pub fn from_bytes(bytes: [u8; Self::BYTES_SIZE]) -> anyhow::Result<Self> {
22+
Ok(Self(Scalar::from_bytes(bytes).map_err(|_| {
23+
anyhow!("Cannot decode election secret key.")
24+
})?))
25+
}
26+
}
27+
28+
impl ElectionPublicKey {
29+
/// `ElectionPublicKey` bytes size
30+
pub const BYTES_SIZE: usize = GroupElement::BYTES_SIZE;
31+
32+
/// Convert this `ElectionPublicKey` to its underlying sequence of bytes.
33+
#[must_use]
34+
pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] {
35+
self.0.to_bytes()
36+
}
37+
38+
/// Attempt to construct a `ElectionPublicKey` from a byte representation.
39+
///
40+
/// # Errors
41+
/// - Cannot decode election public key.
42+
pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> anyhow::Result<Self> {
43+
Ok(Self(
44+
GroupElement::from_bytes(bytes).map_err(|_| anyhow!("Cannot decode public key."))?,
45+
))
46+
}
47+
}
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use test_strategy::proptest;
52+
53+
use super::*;
54+
55+
#[proptest]
56+
fn election_keys_to_bytes_from_bytes_test(sk1: ElectionSecretKey) {
57+
let bytes = sk1.to_bytes();
58+
let sk2 = ElectionSecretKey::from_bytes(bytes).unwrap();
59+
assert_eq!(sk1, sk2);
60+
61+
let pk1 = sk1.public_key();
62+
let bytes = pk1.to_bytes();
63+
let pk2 = ElectionPublicKey::from_bytes(&bytes).unwrap();
64+
assert_eq!(pk1, pk2);
65+
}
66+
}

rust/catalyst-voting/src/vote_protocol/committee.rs renamed to rust/catalyst-voting/src/vote_protocol/committee/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Module containing all primitives related to the committee.
22
3+
mod decoding;
4+
35
use rand_core::CryptoRngCore;
46

57
use crate::crypto::{

0 commit comments

Comments
 (0)