Skip to content

Commit

Permalink
optimize acv output message
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull committed May 7, 2024
1 parent 44a9a7c commit 8663fc4
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions solution/anonymous_ciphertext_voting/src/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Library for a poll coordinator.

use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
use std::time::Instant;
use wedpr_l_crypto_zkp_utils::{bytes_to_point, point_to_bytes, BASEPOINT_G1};
use wedpr_l_utils::error::WedprError;

Expand Down Expand Up @@ -361,19 +362,8 @@ pub fn finalize_vote_result(
.get_blinding_c2(),
)?;

// Compute the total votes.
let target_total = blank_c1_sum - blank_c2_r_sum;
for i in 1..=max_vote_limit {
if target_total.eq(&(*BASEPOINT_G1 * Scalar::from(i as u64))) {
let mut new_pair = StringToInt64Pair::new();
new_pair.set_key(POLL_RESULT_KEY_TOTAL_BALLOTS.to_string());
new_pair.set_value(i);
result.mut_result().push(new_pair);
break;
}
}

// Compute the votes for each candidate.
let mut iter = 0;
for candidate in poll_parameters.get_candidates().get_candidate() {
let ballot = get_ballot_by_candidate(vote_sum, candidate)?;
let candidate_counting_part = get_counting_part_by_candidate(
Expand All @@ -386,6 +376,7 @@ pub fn finalize_vote_result(
bytes_to_point(ballot.get_ciphertext1())? - candidate_c2_r_sum;

for i in 0..=max_vote_limit {
iter = &iter + 1;
if target_candidate.eq(&(*BASEPOINT_G1 * Scalar::from(i as u64))) {
let mut new_pair = StringToInt64Pair::new();
new_pair.set_key(candidate.to_string());
Expand All @@ -395,6 +386,11 @@ pub fn finalize_vote_result(
}
}
}
wedpr_println!(
"** finalize_vote_result decrypt candidate iter: {:?}, result: {:?}",
iter,
result.get_result()
);
Ok(result)
}

Expand Down Expand Up @@ -430,6 +426,7 @@ pub fn decrypt_unlisted_candidate_ballot(
}
// decrypt the unlisted candidate ballot value when decrypt candidate
// success
let mut iter = 0;
for unlisted_vote_ballot in vote_sum.get_voted_ballot_unlisted() {
if unlisted_candidate_part.get_candidate_cipher()
!= unlisted_vote_ballot.get_key()
Expand All @@ -454,6 +451,7 @@ pub fn decrypt_unlisted_candidate_ballot(
// decrypt the ballot value
for i in 0..=max_vote_limit {
let try_num = Scalar::from(i as u64);
iter = &iter + 1;
if !target_total.eq(&(*BASEPOINT_G1 * try_num)) {
continue;
}
Expand All @@ -475,6 +473,11 @@ pub fn decrypt_unlisted_candidate_ballot(
break;
}
}
wedpr_println!(
"** decrypt_unlisted_candidate_ballot iter: {:?}, result: {:?}",
iter,
decrypted_unlisted_candidate_ballot_result
);
Ok(true)
}

Expand All @@ -485,12 +488,16 @@ pub fn finalize_vote_result_unlisted(
max_vote_limit: i64,
max_candidate_number: i64,
) -> Result<VoteResultStorage, WedprError> {
let start_t = Instant::now();
let mut start = Instant::now();
let mut vote_result = finalize_vote_result(
poll_parameters,
vote_sum,
aggregated_decrypted_result,
max_vote_limit,
)?;
let finalize_vote_result_duration = start.elapsed();
start = Instant::now();
// finalize the vote result for the unlisted-candidates
let mut aggregated_unlisted_candidate_ballot_result = BTreeMap::new();
for mut unlisted_candidate in
Expand All @@ -514,5 +521,16 @@ pub fn finalize_vote_result_unlisted(
.mut_unlisted_result()
.push(unlisted_candidate_ballot_result);
}
let decrypt_unlisted_candidate_ballot_duration = start.elapsed();
let total_time_cost = start_t.elapsed();
wedpr_println!(
"** Time elapsed: finalize_vote_result_duration: {:?}, \
decrypt_unlisted_candidate_ballot_duration: {:?}, total time cost: \
{:?}, ### {:?}",
finalize_vote_result_duration,
decrypt_unlisted_candidate_ballot_duration,
total_time_cost,
vote_result
);
Ok(vote_result)
}

0 comments on commit 8663fc4

Please sign in to comment.