3
3
//! Library for a poll coordinator.
4
4
5
5
use curve25519_dalek:: { ristretto:: RistrettoPoint , scalar:: Scalar } ;
6
+ use std:: time:: Instant ;
6
7
use wedpr_l_crypto_zkp_utils:: { bytes_to_point, point_to_bytes, BASEPOINT_G1 } ;
7
8
use wedpr_l_utils:: error:: WedprError ;
8
9
@@ -353,27 +354,8 @@ pub fn finalize_vote_result(
353
354
max_vote_limit : i64 ,
354
355
) -> Result < VoteResultStorage , WedprError > {
355
356
let mut result = VoteResultStorage :: new ( ) ;
356
- let blank_c1_sum =
357
- bytes_to_point ( vote_sum. get_blank_ballot ( ) . get_ciphertext1 ( ) ) ?;
358
- let blank_c2_r_sum = bytes_to_point (
359
- aggregated_decrypted_result
360
- . get_blank_part ( )
361
- . get_blinding_c2 ( ) ,
362
- ) ?;
363
-
364
- // Compute the total votes.
365
- let target_total = blank_c1_sum - blank_c2_r_sum;
366
- for i in 1 ..=max_vote_limit {
367
- if target_total. eq ( & ( * BASEPOINT_G1 * Scalar :: from ( i as u64 ) ) ) {
368
- let mut new_pair = StringToInt64Pair :: new ( ) ;
369
- new_pair. set_key ( POLL_RESULT_KEY_TOTAL_BALLOTS . to_string ( ) ) ;
370
- new_pair. set_value ( i) ;
371
- result. mut_result ( ) . push ( new_pair) ;
372
- break ;
373
- }
374
- }
375
-
376
357
// Compute the votes for each candidate.
358
+ let mut iter = 0 ;
377
359
for candidate in poll_parameters. get_candidates ( ) . get_candidate ( ) {
378
360
let ballot = get_ballot_by_candidate ( vote_sum, candidate) ?;
379
361
let candidate_counting_part = get_counting_part_by_candidate (
@@ -386,6 +368,7 @@ pub fn finalize_vote_result(
386
368
bytes_to_point ( ballot. get_ciphertext1 ( ) ) ? - candidate_c2_r_sum;
387
369
388
370
for i in 0 ..=max_vote_limit {
371
+ iter = & iter + 1 ;
389
372
if target_candidate. eq ( & ( * BASEPOINT_G1 * Scalar :: from ( i as u64 ) ) ) {
390
373
let mut new_pair = StringToInt64Pair :: new ( ) ;
391
374
new_pair. set_key ( candidate. to_string ( ) ) ;
@@ -395,6 +378,11 @@ pub fn finalize_vote_result(
395
378
}
396
379
}
397
380
}
381
+ wedpr_println ! (
382
+ "** finalize_vote_result decrypt candidate iter: {:?}, result: {:?}" ,
383
+ iter,
384
+ result. get_result( )
385
+ ) ;
398
386
Ok ( result)
399
387
}
400
388
@@ -430,6 +418,7 @@ pub fn decrypt_unlisted_candidate_ballot(
430
418
}
431
419
// decrypt the unlisted candidate ballot value when decrypt candidate
432
420
// success
421
+ let mut iter = 0 ;
433
422
for unlisted_vote_ballot in vote_sum. get_voted_ballot_unlisted ( ) {
434
423
if unlisted_candidate_part. get_candidate_cipher ( )
435
424
!= unlisted_vote_ballot. get_key ( )
@@ -454,6 +443,7 @@ pub fn decrypt_unlisted_candidate_ballot(
454
443
// decrypt the ballot value
455
444
for i in 0 ..=max_vote_limit {
456
445
let try_num = Scalar :: from ( i as u64 ) ;
446
+ iter = & iter + 1 ;
457
447
if !target_total. eq ( & ( * BASEPOINT_G1 * try_num) ) {
458
448
continue ;
459
449
}
@@ -475,6 +465,11 @@ pub fn decrypt_unlisted_candidate_ballot(
475
465
break ;
476
466
}
477
467
}
468
+ wedpr_println ! (
469
+ "** decrypt_unlisted_candidate_ballot iter: {:?}, result: {:?}" ,
470
+ iter,
471
+ decrypted_unlisted_candidate_ballot_result
472
+ ) ;
478
473
Ok ( true )
479
474
}
480
475
@@ -485,12 +480,16 @@ pub fn finalize_vote_result_unlisted(
485
480
max_vote_limit : i64 ,
486
481
max_candidate_number : i64 ,
487
482
) -> Result < VoteResultStorage , WedprError > {
483
+ let start_t = Instant :: now ( ) ;
484
+ let mut start = Instant :: now ( ) ;
488
485
let mut vote_result = finalize_vote_result (
489
486
poll_parameters,
490
487
vote_sum,
491
488
aggregated_decrypted_result,
492
489
max_vote_limit,
493
490
) ?;
491
+ let finalize_vote_result_duration = start. elapsed ( ) ;
492
+ start = Instant :: now ( ) ;
494
493
// finalize the vote result for the unlisted-candidates
495
494
let mut aggregated_unlisted_candidate_ballot_result = BTreeMap :: new ( ) ;
496
495
for mut unlisted_candidate in
@@ -514,5 +513,16 @@ pub fn finalize_vote_result_unlisted(
514
513
. mut_unlisted_result ( )
515
514
. push ( unlisted_candidate_ballot_result) ;
516
515
}
516
+ let decrypt_unlisted_candidate_ballot_duration = start. elapsed ( ) ;
517
+ let total_time_cost = start_t. elapsed ( ) ;
518
+ wedpr_println ! (
519
+ "** Time elapsed: finalize_vote_result_duration: {:?}, \
520
+ decrypt_unlisted_candidate_ballot_duration: {:?}, total time cost: \
521
+ {:?}, ### {:?}",
522
+ finalize_vote_result_duration,
523
+ decrypt_unlisted_candidate_ballot_duration,
524
+ total_time_cost,
525
+ vote_result
526
+ ) ;
517
527
Ok ( vote_result)
518
528
}
0 commit comments