Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize acv output message #76

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 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 @@ -353,27 +354,8 @@ pub fn finalize_vote_result(
max_vote_limit: i64,
) -> Result<VoteResultStorage, WedprError> {
let mut result = VoteResultStorage::new();
let blank_c1_sum =
bytes_to_point(vote_sum.get_blank_ballot().get_ciphertext1())?;
let blank_c2_r_sum = bytes_to_point(
aggregated_decrypted_result
.get_blank_part()
.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 +368,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 +378,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 +418,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 +443,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 @@ -472,8 +462,14 @@ pub fn decrypt_unlisted_candidate_ballot(
decrypted_unlisted_candidate_ballot_result
.insert(candidate, i as u64);
}
break;
}
}
wedpr_println!(
"** decrypt_unlisted_candidate_ballot iter: {:?}, result: {:?}",
iter,
decrypted_unlisted_candidate_ballot_result
);
Ok(true)
}

Expand All @@ -484,12 +480,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 @@ -513,5 +513,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)
}
7 changes: 1 addition & 6 deletions solution/anonymous_ciphertext_voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,13 +922,8 @@ mod tests {
.unwrap();
assert!(result);
// check the candidate result(with Wedpr_voting_total_ballots)
assert!(final_result_request_unlisted.get_result().len() == 4);
assert!(final_result_request_unlisted.get_result().len() == 3);
for candidate_result in final_result_request_unlisted.get_result() {
if candidate_result.get_key()
== POLL_RESULT_KEY_TOTAL_BALLOTS.to_string()
{
assert!(candidate_result.get_value() == 60);
}
if candidate_result.get_key() == "Alice" {
assert!(candidate_result.get_value() == 40);
}
Expand Down
9 changes: 0 additions & 9 deletions solution/anonymous_ciphertext_voting/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,6 @@ pub fn verify_vote_result(
.get_blinding_c2(),
)?;

let blank_result =
get_int64_by_candidate(vote_result, POLL_RESULT_KEY_TOTAL_BALLOTS)?;
let expected_blank_ballot_result = blank_c1_sum - (blank_c2_r_sum);
if expected_blank_ballot_result
.ne(&(*BASEPOINT_G1 * (Scalar::from(blank_result as u64))))
{
return Ok(false);
}

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 Down
Loading