Skip to content

Commit 5c16fae

Browse files
committed
update voting_test
1 parent 76c78c1 commit 5c16fae

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

rust/catalyst-voting/src/crypto/elgamal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::crypto::group::{GroupElement, Scalar};
1212
pub struct SecretKey(Scalar);
1313

1414
/// ``ElGamal`` public key.
15-
#[derive(Debug, Clone, Eq, PartialEq)]
15+
#[derive(Clone, Debug, PartialEq, Eq)]
1616
pub struct PublicKey(GroupElement);
1717

1818
/// ``ElGamal`` ciphertext, encrypted message with the public key.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::crypto::{
1313
/// Represented as a Unit vector which size is `voting_options`
1414
/// and the `choice` value is the index of the unit vector component equals to `1`,
1515
/// and other components equal to `0`.
16+
#[derive(Clone, Debug, PartialEq, Eq)]
1617
pub struct Vote {
1718
/// Voter's voting choice.
1819
choice: usize,
@@ -21,9 +22,11 @@ pub struct Vote {
2122
}
2223

2324
/// A representation of the encrypted vote.
25+
#[derive(Clone, Debug, PartialEq, Eq)]
2426
pub struct EncryptedVote(Vec<Ciphertext>);
2527

2628
/// A representation of the encryption randomness, used to encrypt the vote.
29+
#[derive(Clone, Debug, PartialEq, Eq)]
2730
pub struct EncryptionRandomness(Vec<Scalar>);
2831

2932
impl EncryptionRandomness {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ pub fn generate_voter_proof<R: CryptoRngCore>(
7070
#[must_use]
7171
#[allow(clippy::module_name_repetitions)]
7272
pub fn verify_voter_proof(
73-
encrypted_vote: EncryptedVote, public_key: &PublicKey, commitment_key: &PublicKey,
73+
encrypted_vote: EncryptedVote, public_key: &PublicKey, commitment: &VoterProofCommitment,
7474
proof: &VoterProof,
7575
) -> bool {
76-
verify_unit_vector_proof(&proof.0, encrypted_vote.0, public_key, commitment_key)
76+
verify_unit_vector_proof(&proof.0, encrypted_vote.0, public_key, &commitment.0)
7777
}

rust/catalyst-voting/tests/voting_test.rs

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ use catalyst_voting::{
66
proof::{generate_tally_proof, verify_tally_proof},
77
tally, DecryptionTallySetup,
88
},
9-
voter::{encrypt_vote, Vote},
9+
voter::{
10+
encrypt_vote,
11+
proof::{generate_voter_proof, verify_voter_proof, VoterProofCommitment},
12+
Vote,
13+
},
1014
SecretKey,
1115
};
1216
use proptest::prelude::ProptestConfig;
@@ -28,17 +32,51 @@ fn voting_test(voters: [Voter; 100]) {
2832

2933
let election_secret_key = SecretKey::random(&mut rng);
3034
let election_public_key = election_secret_key.public_key();
35+
let voter_proof_commitment = VoterProofCommitment::random(&mut rng);
3136

3237
let votes: Vec<_> = voters
3338
.iter()
3439
.map(|voter| Vote::new(voter.choice, VOTING_OPTIONS).unwrap())
3540
.collect();
3641

37-
let (encrypted_votes, _randomness): (Vec<_>, Vec<_>) = votes
42+
let (encrypted_votes, randomness): (Vec<_>, Vec<_>) = votes
3843
.iter()
3944
.map(|vote| encrypt_vote(vote, &election_public_key, &mut rng))
4045
.unzip();
4146

47+
// Verify encrypted votes
48+
{
49+
let voter_proofs: Vec<_> = votes
50+
.iter()
51+
.zip(encrypted_votes.iter())
52+
.zip(randomness.iter())
53+
.map(|((v, enc_v), r)| {
54+
generate_voter_proof(
55+
v,
56+
enc_v.clone(),
57+
r.clone(),
58+
&election_public_key,
59+
&voter_proof_commitment,
60+
&mut rng,
61+
)
62+
.unwrap()
63+
})
64+
.collect();
65+
66+
let is_ok = voter_proofs
67+
.iter()
68+
.zip(encrypted_votes.iter())
69+
.all(|(p, enc_v)| {
70+
verify_voter_proof(
71+
enc_v.clone(),
72+
&election_public_key,
73+
&voter_proof_commitment,
74+
p,
75+
)
76+
});
77+
assert!(is_ok);
78+
}
79+
4280
let voting_powers: Vec<_> = voters
4381
.iter()
4482
.map(|voter| u64::from(voter.voting_power))
@@ -51,22 +89,25 @@ fn voting_test(voters: [Voter; 100]) {
5189
let total_voting_power = voting_powers.iter().sum();
5290
let decryption_tally_setup = DecryptionTallySetup::new(total_voting_power).unwrap();
5391

54-
let tally_proofs: Vec<_> = encrypted_tallies
55-
.iter()
56-
.map(|t| generate_tally_proof(t, &election_secret_key, &mut rng))
57-
.collect();
58-
5992
let decrypted_tallies: Vec<_> = encrypted_tallies
6093
.iter()
6194
.map(|t| decrypt_tally(t, &election_secret_key, &decryption_tally_setup).unwrap())
6295
.collect();
6396

64-
let is_ok = tally_proofs
65-
.iter()
66-
.zip(encrypted_tallies.iter())
67-
.zip(decrypted_tallies.iter())
68-
.all(|((p, enc_t), t)| verify_tally_proof(enc_t, *t, &election_public_key, p));
69-
assert!(is_ok);
97+
// Verify tallies
98+
{
99+
let tally_proofs: Vec<_> = encrypted_tallies
100+
.iter()
101+
.map(|t| generate_tally_proof(t, &election_secret_key, &mut rng))
102+
.collect();
103+
104+
let is_ok = tally_proofs
105+
.iter()
106+
.zip(encrypted_tallies.iter())
107+
.zip(decrypted_tallies.iter())
108+
.all(|((p, enc_t), t)| verify_tally_proof(enc_t, *t, &election_public_key, p));
109+
assert!(is_ok);
110+
}
70111

71112
let expected_tallies: Vec<_> = (0..VOTING_OPTIONS)
72113
.map(|i| {

0 commit comments

Comments
 (0)