Skip to content

Commit 963e46c

Browse files
authored
fix decrypt vote for the unlisted-candidates error (#64)
1 parent 0cbd571 commit 963e46c

File tree

4 files changed

+112
-17
lines changed

4 files changed

+112
-17
lines changed

ffi/ffi_java/ffi_java_acv/src/verifier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub extern "system" fn Java_com_webank_wedpr_acv_NativeInterface_verifyUnbounded
155155
);
156156
let public_key =
157157
java_safe_jbytes_to_bytes!(_env, result_jobject, public_key_bytes);
158-
let verify_result = match wedpr_s_anonymous_ciphertext_voting::verifier::verify_unbounded_vote_request(&pb_poll_parameters,
158+
let verify_result = match wedpr_s_anonymous_ciphertext_voting::verifier::verify_unbounded_vote_request_unlisted(&pb_poll_parameters,
159159
&pb_vote_request, &public_key)
160160
{
161161
Ok(v) => v,

solution/anonymous_ciphertext_voting/src/coordinator.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub fn aggregate_vote_sum_response(
153153
bytes_to_point(&vote_sum.get_blank_ballot().get_ciphertext2())?
154154
+ c2_point;
155155

156-
let mut output_vote_sum = VoteStorage::new();
156+
let mut updated_vote_sum_list = Vec::new();
157157
for candidate in poll_parameters.get_candidates().get_candidate() {
158158
let sum_ballot = get_ballot_by_candidate(&vote_sum, candidate)?;
159159
let new_ballot = get_ballot_by_candidate(&vote_part, candidate)?;
@@ -171,12 +171,19 @@ pub fn aggregate_vote_sum_response(
171171
let mut new_pair = CandidateBallot::new();
172172
new_pair.set_candidate(candidate.to_string());
173173
new_pair.set_ballot(new_sum_ballot);
174-
output_vote_sum.mut_voted_ballot().push(new_pair);
174+
updated_vote_sum_list.push(new_pair);
175175
}
176-
let blank_ballot = output_vote_sum.mut_blank_ballot();
177-
blank_ballot.set_ciphertext1(point_to_bytes(&blank_c1_sum));
178-
blank_ballot.set_ciphertext2(point_to_bytes(&blank_c2_sum));
179-
*vote_sum = output_vote_sum;
176+
// update the voted_ballot for vote sum
177+
vote_sum.clear_voted_ballot();
178+
for voted_ballot in updated_vote_sum_list {
179+
vote_sum.mut_voted_ballot().push(voted_ballot);
180+
}
181+
vote_sum
182+
.mut_blank_ballot()
183+
.set_ciphertext1(point_to_bytes(&blank_c1_sum));
184+
vote_sum
185+
.mut_blank_ballot()
186+
.set_ciphertext2(point_to_bytes(&blank_c2_sum));
180187
Ok(true)
181188
}
182189

@@ -233,8 +240,7 @@ pub fn aggregate_decrypted_part_sum(
233240
.mut_blank_part()
234241
.set_blinding_c2(point_to_bytes(&blank_c2_r_sum));
235242

236-
let mut output_decrypted_result = aggregated_decrypted_result.clone();
237-
output_decrypted_result.clear_candidate_part();
243+
let mut updated_candidate_part_list = Vec::new();
238244
for candidate in poll_parameters.get_candidates().get_candidate() {
239245
let aggregated_part = get_counting_part_by_candidate(
240246
&aggregated_decrypted_result,
@@ -255,9 +261,14 @@ pub fn aggregate_decrypted_part_sum(
255261
let mut new_pair = StringToCountingPartPair::new();
256262
new_pair.set_key(candidate.to_string());
257263
new_pair.set_value(candidate_part);
258-
output_decrypted_result.mut_candidate_part().push(new_pair);
264+
updated_candidate_part_list.push(new_pair);
265+
}
266+
aggregated_decrypted_result.clear_candidate_part();
267+
for updated_candidate_part in updated_candidate_part_list {
268+
aggregated_decrypted_result
269+
.mut_candidate_part()
270+
.push(updated_candidate_part);
259271
}
260-
*aggregated_decrypted_result = output_decrypted_result;
261272
Ok(true)
262273
}
263274

@@ -268,8 +279,10 @@ pub fn aggregate_decrypted_part_for_specify_unlisted_candidate(
268279
// counting for the unlisted candidate
269280
let current_aggregated_unlisted_candidate =
270281
aggregated_decrypted_part.get_decrypted_unlisted_candidate();
282+
271283
let decrypted_part_unlisted_candidate =
272284
decrypted_part.get_decrypted_unlisted_candidate();
285+
273286
let aggregated_unlisted_candidate = bytes_to_point(
274287
current_aggregated_unlisted_candidate.get_blinding_c2(),
275288
)? + bytes_to_point(
@@ -279,7 +292,6 @@ pub fn aggregate_decrypted_part_for_specify_unlisted_candidate(
279292
aggregated_decrypted_part
280293
.mut_decrypted_unlisted_candidate()
281294
.set_blinding_c2(point_to_bytes(&aggregated_unlisted_candidate));
282-
283295
// push the cipher unlisted candidate ballot into aggregated_decrypted_part
284296
let unlisted_candidate_ballot =
285297
decrypted_part.get_decrypted_unlisted_candidate_ballot();

solution/anonymous_ciphertext_voting/src/lib.rs

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ pub mod voter;
1818
#[cfg(test)]
1919
mod tests {
2020
use super::*;
21-
use crate::{config::SIGNATURE, coordinator};
21+
use crate::{
22+
config::{POLL_RESULT_KEY_TOTAL_BALLOTS, SIGNATURE},
23+
coordinator,
24+
};
2225
use wedpr_l_crypto_zkp_utils::{
2326
bytes_to_point, get_random_scalar, scalar_to_bytes,
2427
};
@@ -681,6 +684,7 @@ mod tests {
681684
.unwrap()
682685
);
683686
// aggregate
687+
wedpr_println!("##### aggregate_vote_sum_response_unlisted request1");
684688
assert_eq!(
685689
true,
686690
coordinator::aggregate_vote_sum_response_unlisted(
@@ -690,6 +694,11 @@ mod tests {
690694
)
691695
.unwrap()
692696
);
697+
wedpr_println!(
698+
"##### aggregate_vote_sum_response_unlisted request1 success, \
699+
size: {:?}",
700+
encrypted_vote_sum.get_voted_ballot_unlisted().len()
701+
);
693702
// vote2
694703
let choice_candidate_unlisted2 =
695704
&candidate_list_unlisted[1..3].to_vec();
@@ -713,6 +722,7 @@ mod tests {
713722
)
714723
.unwrap()
715724
);
725+
wedpr_println!("##### aggregate_vote_sum_response_unlisted request2");
716726
assert_eq!(
717727
true,
718728
coordinator::aggregate_vote_sum_response_unlisted(
@@ -722,6 +732,11 @@ mod tests {
722732
)
723733
.unwrap()
724734
);
735+
wedpr_println!(
736+
"##### aggregate_vote_sum_response_unlisted request2 success, \
737+
size: {:?}",
738+
encrypted_vote_sum.get_voted_ballot_unlisted().len()
739+
);
725740
// vote3
726741
let choice_candidate_unlisted3 =
727742
&candidate_list_unlisted[2..3].to_vec();
@@ -736,6 +751,7 @@ mod tests {
736751
&poll_parameters,
737752
)
738753
.unwrap();
754+
wedpr_println!("##### aggregate_vote_sum_response_unlisted request3");
739755
assert_eq!(
740756
true,
741757
verifier::verify_unbounded_vote_request_unlisted(
@@ -754,7 +770,13 @@ mod tests {
754770
)
755771
.unwrap()
756772
);
757-
wedpr_println!("encrypted_vote_sum: {:?}", encrypted_vote_sum);
773+
wedpr_println!(
774+
"##### aggregate_vote_sum_response_unlisted request3 success"
775+
);
776+
wedpr_println!(
777+
"encrypted_vote_sum, unlisted size: {:?}",
778+
encrypted_vote_sum.get_voted_ballot_unlisted().len()
779+
);
758780

759781
let mut vote_sum_total = DecryptedResultPartStorage::new();
760782

@@ -778,6 +800,11 @@ mod tests {
778800
.unwrap()
779801
);
780802

803+
wedpr_println!(
804+
"##### aggregate_decrypted_part_sum_unlisted request1, unlisted \
805+
size: {:?}",
806+
vote_sum_total.get_unlisted_candidate_part().len()
807+
);
781808
assert_eq!(
782809
true,
783810
coordinator::aggregate_decrypted_part_sum_unlisted(
@@ -787,6 +814,11 @@ mod tests {
787814
)
788815
.unwrap()
789816
);
817+
wedpr_println!(
818+
"##### aggregate_decrypted_part_sum_unlisted request1, success \
819+
unlisted size: {:?}",
820+
vote_sum_total.get_unlisted_candidate_part().len()
821+
);
790822
let decrypt_request2 = counter::count_unlisted(
791823
counter_id_list[1],
792824
&counter2,
@@ -806,7 +838,11 @@ mod tests {
806838
)
807839
.unwrap()
808840
);
809-
841+
wedpr_println!(
842+
"##### aggregate_decrypted_part_sum_unlisted request2, unlisted \
843+
size: {:?}",
844+
vote_sum_total.get_unlisted_candidate_part().len()
845+
);
810846
assert_eq!(
811847
true,
812848
coordinator::aggregate_decrypted_part_sum_unlisted(
@@ -816,6 +852,11 @@ mod tests {
816852
)
817853
.unwrap()
818854
);
855+
wedpr_println!(
856+
"##### aggregate_decrypted_part_sum_unlisted request2, unlisted \
857+
size: {:?} succ",
858+
vote_sum_total.get_unlisted_candidate_part().len()
859+
);
819860

820861
let decrypt_request3 = counter::count_unlisted(
821862
counter_id_list[2],
@@ -836,7 +877,11 @@ mod tests {
836877
)
837878
.unwrap()
838879
);
839-
880+
wedpr_println!(
881+
"##### aggregate_decrypted_part_sum_unlisted request3, unlisted \
882+
size: {:?}",
883+
vote_sum_total.get_unlisted_candidate_part().len()
884+
);
840885
assert_eq!(
841886
true,
842887
coordinator::aggregate_decrypted_part_sum_unlisted(
@@ -846,6 +891,11 @@ mod tests {
846891
)
847892
.unwrap()
848893
);
894+
wedpr_println!(
895+
"##### aggregate_decrypted_part_sum_unlisted request3, unlisted \
896+
size: {:?} success",
897+
vote_sum_total.get_unlisted_candidate_part().len()
898+
);
849899
// wedpr_println!("vote_sum_total:{:?}", vote_sum_total);
850900

851901
let final_result_request_unlisted =
@@ -871,5 +921,38 @@ mod tests {
871921
)
872922
.unwrap();
873923
assert!(result);
924+
// check the candidate result(with Wedpr_voting_total_ballots)
925+
assert!(final_result_request_unlisted.get_result().len() == 4);
926+
for candidate_result in final_result_request_unlisted.get_result() {
927+
if candidate_result.get_key()
928+
== POLL_RESULT_KEY_TOTAL_BALLOTS.to_string()
929+
{
930+
assert!(candidate_result.get_value() == 60);
931+
}
932+
if candidate_result.get_key() == "Alice" {
933+
assert!(candidate_result.get_value() == 40);
934+
}
935+
if candidate_result.get_key() == "Bob" {
936+
assert!(candidate_result.get_value() == 50);
937+
}
938+
if candidate_result.get_key() == "charlie" {
939+
assert!(candidate_result.get_value() == 60);
940+
}
941+
}
942+
// check the unlisted candidate result
943+
assert!(final_result_request_unlisted.get_unlisted_result().len() == 3);
944+
for candidate_result in
945+
final_result_request_unlisted.get_unlisted_result()
946+
{
947+
if candidate_result.get_candidate_id() == 1 {
948+
assert!(candidate_result.get_value() == 10);
949+
}
950+
if candidate_result.get_candidate_id() == 2 {
951+
assert!(candidate_result.get_value() == 0);
952+
}
953+
if candidate_result.get_candidate_id() == 3 {
954+
assert!(candidate_result.get_value() == 60);
955+
}
956+
}
874957
}
875958
}

solution/anonymous_ciphertext_voting/src/voter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ pub fn generate_candidate_cipher(
318318
let blinding = get_random_scalar();
319319
let ciphertext1 = RistrettoPoint::multiscalar_mul(
320320
&[Scalar::from(value as u64), blinding],
321-
&[*BASEPOINT_G2, *poll_point],
321+
&[*BASEPOINT_G1, *poll_point],
322322
);
323323
let ciphertext2 = *BASEPOINT_G2 * blinding;
324324
cipher_point.set_ciphertext1(point_to_bytes(&ciphertext1));

0 commit comments

Comments
 (0)