Skip to content

Commit 8e2263a

Browse files
committed
refactor
1 parent 36141d1 commit 8e2263a

File tree

3 files changed

+13
-50
lines changed

3 files changed

+13
-50
lines changed

rust/catalyst-voting/src/crypto/zk_unit_vector/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct UnitVectorProof(
4343
/// the proof will be invalid.
4444
pub fn generate_unit_vector_proof<R: CryptoRngCore>(
4545
unit_vector: &[Scalar], mut encryption_randomness: Vec<Scalar>,
46-
mut ciphertexts: Vec<Ciphertext>, public_key: &PublicKey, commitment_key: &PublicKey,
46+
mut ciphertexts: Vec<Ciphertext>, public_key: &PublicKey, commitment_key: &GroupElement,
4747
rng: &mut R,
4848
) -> UnitVectorProof {
4949
let i = unit_vector
@@ -249,7 +249,7 @@ mod tests {
249249
#[proptest]
250250
fn zk_unit_vector_test(
251251
secret_key: SecretKey, secret_commitment_key: SecretKey,
252-
#[strategy(2..10_usize)] unit_vector_size: usize,
252+
#[strategy(1..10_usize)] unit_vector_size: usize,
253253
#[strategy(0..#unit_vector_size)] unit_vector_index: usize,
254254
) {
255255
let mut rng = OsRng;
@@ -300,7 +300,7 @@ mod tests {
300300
#[proptest]
301301
fn not_a_unit_vector_test(
302302
secret_key: SecretKey, secret_commitment_key: SecretKey,
303-
#[any(size_range(2..10_usize).lift())] random_vector: Vec<Scalar>,
303+
#[any(size_range(1..10_usize).lift())] random_vector: Vec<Scalar>,
304304
) {
305305
let mut rng = OsRng;
306306

rust/catalyst-voting/src/voter/mod.rs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct EncryptionRandomness(Vec<Scalar>);
2626

2727
impl EncryptionRandomness {
2828
/// Randomly generate the `EncryptionRandomness`.
29-
pub fn generate<R: CryptoRngCore>(rng: &mut R, voting_options: usize) -> Self {
29+
fn generate<R: CryptoRngCore>(rng: &mut R, voting_options: usize) -> Self {
3030
Self((0..voting_options).map(|_| Scalar::random(rng)).collect())
3131
}
3232
}
@@ -79,30 +79,15 @@ impl Vote {
7979
}
8080
}
8181

82-
/// Encrypted vote error
83-
#[derive(thiserror::Error, Debug)]
84-
pub enum EncryptedVoteError {
85-
/// Incorrect randomness length
86-
#[error(
87-
"Invalid randomness length, the length of randomness: {0}, should be equal to the number of voting options: {1}."
88-
)]
89-
IncorrectRandomnessLength(usize, usize),
90-
}
91-
9282
/// Create a new encrypted vote from the given vote and public key.
9383
/// More detailed described [here](https://input-output-hk.github.io/catalyst-voices/architecture/08_concepts/voting_transaction/crypto/#vote-encryption)
9484
///
9585
/// # Errors
9686
/// - `EncryptedVoteError`
97-
pub fn encrypt_vote(
98-
vote: &Vote, public_key: &PublicKey, randomness: &EncryptionRandomness,
99-
) -> Result<EncryptedVote, EncryptedVoteError> {
100-
if vote.voting_options != randomness.0.len() {
101-
return Err(EncryptedVoteError::IncorrectRandomnessLength(
102-
randomness.0.len(),
103-
vote.voting_options,
104-
));
105-
}
87+
pub fn encrypt_vote<R: CryptoRngCore>(
88+
vote: &Vote, public_key: &PublicKey, rng: &mut R,
89+
) -> (EncryptedVote, EncryptionRandomness) {
90+
let randomness = EncryptionRandomness::generate(rng, vote.voting_options);
10691

10792
let unit_vector = vote.to_unit_vector();
10893
let ciphers = unit_vector
@@ -111,16 +96,12 @@ pub fn encrypt_vote(
11196
.map(|(m, r)| encrypt(m, public_key, r))
11297
.collect();
11398

114-
Ok(EncryptedVote(ciphers))
99+
(EncryptedVote(ciphers), randomness)
115100
}
116101

117102
#[cfg(test)]
118103
mod tests {
119-
use proptest::sample::size_range;
120-
use test_strategy::proptest;
121-
122104
use super::*;
123-
use crate::crypto::elgamal::SecretKey;
124105

125106
#[test]
126107
fn vote_test() {
@@ -150,17 +131,4 @@ mod tests {
150131
assert!(Vote::new(3, voting_options).is_err());
151132
assert!(Vote::new(4, voting_options).is_err());
152133
}
153-
154-
#[proptest]
155-
fn encrypt_test(
156-
secret_key: SecretKey, #[strategy(1..10usize)] voting_options: usize,
157-
#[any(size_range(#voting_options).lift())] randomness: Vec<Scalar>,
158-
) {
159-
let public_key = secret_key.public_key();
160-
let vote = Vote::new(0, voting_options).unwrap();
161-
162-
let encrypted =
163-
encrypt_vote(&vote, &public_key, &EncryptionRandomness(randomness)).unwrap();
164-
assert_eq!(encrypted.0.len(), vote.voting_options);
165-
}
166134
}

rust/catalyst-voting/tests/voting_test.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use catalyst_voting::{
44
decrypt_tally, encrypt_vote, generate_tally_proof, tally, verify_tally_proof,
5-
DecryptionTallySetup, EncryptionRandomness, SecretKey, Vote,
5+
DecryptionTallySetup, SecretKey, Vote,
66
};
77
use proptest::prelude::ProptestConfig;
88
use test_strategy::{proptest, Arbitrary};
@@ -29,15 +29,10 @@ fn voting_test(voters: [Voter; 100]) {
2929
.map(|voter| Vote::new(voter.choice, VOTING_OPTIONS).unwrap())
3030
.collect();
3131

32-
let voters_randomness: Vec<_> = (0..voters.len())
33-
.map(|_| EncryptionRandomness::generate(&mut rng, VOTING_OPTIONS))
34-
.collect();
35-
36-
let encrypted_votes: Vec<_> = votes
32+
let (encrypted_votes, _randomness): (Vec<_>, Vec<_>) = votes
3733
.iter()
38-
.zip(voters_randomness.iter())
39-
.map(|(vote, r)| encrypt_vote(vote, &election_public_key, r).unwrap())
40-
.collect();
34+
.map(|vote| encrypt_vote(vote, &election_public_key, &mut rng))
35+
.unzip();
4136

4237
let voting_powers: Vec<_> = voters
4338
.iter()

0 commit comments

Comments
 (0)