-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
rust/catalyst-voting/src/vote_protocol/committee/decoding.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...yst-voting/src/vote_protocol/committee.rs → ...voting/src/vote_protocol/committee/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters