@@ -26,7 +26,7 @@ pub struct EncryptionRandomness(Vec<Scalar>);
2626
2727impl 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) ]
118103mod 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}
0 commit comments