@@ -51,9 +51,9 @@ use crate::{
5151 hash:: { digest:: Digest , Blake2b256Hasher , Blake2b512Hasher } ,
5252 } ,
5353 vote_protocol:: {
54- committee:: ElectionPublicKey ,
54+ committee:: { ElectionPublicKey , ElectionSecretKey } ,
5555 voter:: {
56- encrypt_vote_with_default_rng,
56+ decrypt_vote , encrypt_vote_with_default_rng,
5757 proof:: { generate_voter_proof, verify_voter_proof, VoterProof , VoterProofCommitment } ,
5858 EncryptedVote , Vote ,
5959 } ,
@@ -163,6 +163,33 @@ impl Tx {
163163 matches ! ( self . vote, VotePayload :: Private ( _, _) )
164164 }
165165
166+ /// Returns public voting choice.
167+ ///
168+ /// # Errors
169+ /// - Not a public vote
170+ pub fn public_choice ( & self ) -> anyhow:: Result < u8 > {
171+ if let VotePayload :: Public ( choice) = & self . vote {
172+ Ok ( * choice)
173+ } else {
174+ Err ( anyhow:: anyhow!( "Not a public vote" ) )
175+ }
176+ }
177+
178+ /// Returns private voting choice.
179+ ///
180+ /// # Errors
181+ /// - Not a private vote
182+ #[ allow( clippy:: cast_possible_truncation) ]
183+ pub fn private_choice ( & self , secret_key : & ElectionSecretKey ) -> anyhow:: Result < u8 > {
184+ if let VotePayload :: Private ( vote, _) = & self . vote {
185+ let vote = decrypt_vote ( vote, secret_key) ?;
186+ let choice = vote. choice ( ) as u8 ;
187+ Ok ( choice)
188+ } else {
189+ Err ( anyhow:: anyhow!( "Not a private vote" ) )
190+ }
191+ }
192+
166193 /// Verify transaction signature
167194 ///
168195 /// # Errors
@@ -262,27 +289,6 @@ impl VotePayload {
262289
263290 Ok ( Self :: Private ( encrypted_vote, voter_proof) )
264291 }
265-
266- // #[allow(clippy::cast_possible_truncation, dead_code)]
267- // fn choice(&self, secret_key: &ElectionSecretKey) -> anyhow::Result<u8> {
268- // match self {
269- // Self::Public(choice) => Ok(*choice),
270- // Self::Private(vote, _) => {
271- // // Making a tally and decryption tally procedure on one vote to retrieve
272- // the // original voting choice.
273- // // Assuming that the voting power argument must be equals to 1.
274- // let setup = DecryptionTallySetup::new(1)?;
275- // for voting_option in 0..vote.voting_options() {
276- // let tally = tally(voting_option, &[vote.clone()], &[1])?;
277- // let choice_for_voting_option = decrypt_tally(&tally, secret_key,
278- // &setup)?; if choice_for_voting_option == 1 {
279- // return Ok(voting_option as u8);
280- // }
281- // }
282- // bail!("Invalid encrypted vote, not a unit vector");
283- // },
284- // }
285- // }
286292}
287293
288294#[ cfg( test) ]
@@ -312,6 +318,8 @@ mod tests {
312318 assert ! ( !tx. is_private( ) ) ;
313319 tx. verify_signature ( ) . unwrap ( ) ;
314320 tx. verify_proof ( & election_public_key) . unwrap ( ) ;
321+ assert_eq ! ( tx. public_choice( ) . unwrap( ) , choice) ;
322+ assert ! ( tx. private_choice( & election_secret_key) . is_err( ) ) ;
315323
316324 let tx = Tx :: new_private_with_default_rng (
317325 vote_plan_id,
@@ -326,5 +334,7 @@ mod tests {
326334 assert ! ( tx. is_private( ) ) ;
327335 tx. verify_signature ( ) . unwrap ( ) ;
328336 tx. verify_proof ( & election_public_key) . unwrap ( ) ;
337+ assert_eq ! ( tx. private_choice( & election_secret_key) . unwrap( ) , choice) ;
338+ assert ! ( tx. public_choice( ) . is_err( ) ) ;
329339 }
330340}
0 commit comments