Skip to content

Commit d0b5784

Browse files
authored
fix(rust/catalyst-voting): Added missing feature flag, updated conversion from CtOption to Option (#61)
* fix rand_core dep, update Scalar from_bytes * fix spelling * add decoding impl * fix
1 parent 143174b commit d0b5784

File tree

6 files changed

+100
-9
lines changed

6 files changed

+100
-9
lines changed

rust/catalyst-voting/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# cspell: words getrandom
2+
13
[package]
24
name = "catalyst-voting"
35
version = "0.0.1"
@@ -12,7 +14,7 @@ workspace = true
1214

1315
[dependencies]
1416
anyhow = "1.0.89"
15-
rand_core = "0.6.4"
17+
rand_core = { version = "0.6.4", features = ["getrandom"] }
1618
rand_chacha = "0.3.1"
1719
curve25519-dalek = { version = "4.1.3", features = ["digest", "rand_core"] }
1820
ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }

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

rust/catalyst-voting/src/crypto/group/ristretto255/decoding.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ impl Scalar {
1414
/// # Errors
1515
/// - Cannot decode scalar.
1616
pub fn from_bytes(bytes: [u8; Self::BYTES_SIZE]) -> anyhow::Result<Scalar> {
17-
IScalar::from_canonical_bytes(bytes)
17+
Into::<Option<_>>::into(IScalar::from_canonical_bytes(bytes))
1818
.map(Scalar)
19-
.into_option()
2019
.ok_or(anyhow!("Cannot decode scalar."))
2120
}
2221

rust/catalyst-voting/src/txs/v1/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::{
5353
vote_protocol::{
5454
committee::{ElectionPublicKey, ElectionSecretKey},
5555
voter::{
56-
decrypt_vote, encrypt_vote_with_default_rng,
56+
decrypt_vote, encrypt_vote,
5757
proof::{generate_voter_proof, verify_voter_proof, VoterProof, VoterProofCommitment},
5858
EncryptedVote, Vote,
5959
},
@@ -272,8 +272,7 @@ impl VotePayload {
272272
) -> anyhow::Result<Self> {
273273
let vote = Vote::new(choice.into(), proposal_voting_options.into())?;
274274

275-
let (encrypted_vote, randomness) =
276-
encrypt_vote_with_default_rng(&vote, election_public_key);
275+
let (encrypted_vote, randomness) = encrypt_vote(&vote, election_public_key, rng);
277276

278277
let vote_plan_id_hash = Blake2b512Hasher::new().chain_update(vote_plan_id);
279278
let commitment = VoterProofCommitment::from_hash(vote_plan_id_hash);
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)