|
2 | 2 |
|
3 | 3 | //! Library of anonymous ciphertext voting (ACV) solution.
|
4 | 4 |
|
5 |
| -use crate::config::{HASH_KECCAK256, SIGNATURE_SECP256K1}; |
6 | 5 | use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
|
7 |
| -use wedpr_l_crypto_zkp_utils::{bytes_to_point, point_to_bytes, BASEPOINT_G1}; |
| 6 | +use wedpr_l_crypto_zkp_utils::{BASEPOINT_G1, bytes_to_point, point_to_bytes}; |
8 | 7 | use wedpr_l_utils::{
|
9 | 8 | error::WedprError,
|
10 | 9 | traits::{Hash, Signature},
|
11 | 10 | };
|
12 |
| -use wedpr_s_protos::generated::acv::{ |
13 |
| - Ballot, CandidateList, CounterSystemParametersStorage, CountingPart, |
14 |
| - DecryptedResultPartStorage, RegistrationRequest, RegistrationResponse, |
15 |
| - StringToCountingPartPair, SystemParametersStorage, |
16 |
| -}; |
| 11 | + |
| 12 | +use wedpr_s_protos::generated::acv::{Ballot, CandidateBallot, CandidateList, CounterSystemParametersStorage, CountingPart, DecryptedResultPartStorage, RegistrationRequest, RegistrationResponse, StringToCountingPartPair, StringToInt64Pair, SystemParametersStorage, VoteResultStorage, VoteStorage}; |
| 13 | + |
| 14 | +use crate::config::{HASH_KECCAK256, SIGNATURE_SECP256K1}; |
17 | 15 |
|
18 | 16 | /// Makes system parameters containing public key and candidates list using counter storage messages.
|
19 | 17 | pub fn make_system_parameters(
|
@@ -125,3 +123,143 @@ pub fn aggregate_decrypted_part_sum(
|
125 | 123 | }
|
126 | 124 | Ok(true)
|
127 | 125 | }
|
| 126 | + |
| 127 | +/// ? |
| 128 | +pub fn aggregate_vote_sum_response( |
| 129 | + param: &SystemParametersStorage, |
| 130 | + vote_storage_part: &VoteStorage, |
| 131 | + vote_sum: &mut VoteStorage, |
| 132 | +) -> Result<bool, WedprError> { |
| 133 | + if !vote_sum.has_blank_ballot() { |
| 134 | + vote_sum |
| 135 | + .mut_blank_ballot() |
| 136 | + .set_ciphertext1(point_to_bytes(&RistrettoPoint::default())); |
| 137 | + vote_sum |
| 138 | + .mut_blank_ballot() |
| 139 | + .set_ciphertext2(point_to_bytes(&RistrettoPoint::default())); |
| 140 | + for candidate in param.get_candidates().get_candidate() { |
| 141 | + let mut ballot = Ballot::new(); |
| 142 | + ballot.set_ciphertext1(point_to_bytes(&RistrettoPoint::default())); |
| 143 | + ballot.set_ciphertext2(point_to_bytes(&RistrettoPoint::default())); |
| 144 | + let mut ballot_pair = CandidateBallot::new(); |
| 145 | + ballot_pair.set_candidate(candidate.to_string()); |
| 146 | + ballot_pair.set_ballot(ballot); |
| 147 | + vote_sum.mut_voted_ballot().push(ballot_pair); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + let mut tmp_vote_storage_sum = VoteStorage::new(); |
| 152 | + let mut blank_c1_sum = |
| 153 | + bytes_to_point(&vote_sum.get_blank_ballot().get_ciphertext1())?; |
| 154 | + let mut blank_c2_sum = |
| 155 | + bytes_to_point(&vote_sum.get_blank_ballot().get_ciphertext2())?; |
| 156 | + let c1_tmp_point = bytes_to_point( |
| 157 | + &vote_storage_part |
| 158 | + .get_blank_ballot() |
| 159 | + .get_ciphertext1() |
| 160 | + .clone(), |
| 161 | + )?; |
| 162 | + let c2_tmp_point = bytes_to_point( |
| 163 | + &vote_storage_part |
| 164 | + .get_blank_ballot() |
| 165 | + .get_ciphertext2() |
| 166 | + .clone(), |
| 167 | + )?; |
| 168 | + blank_c1_sum += c1_tmp_point; |
| 169 | + blank_c2_sum += c2_tmp_point; |
| 170 | + |
| 171 | + for candidate in param.get_candidates().get_candidate() { |
| 172 | + let mut candidate_ballot = Ballot::new(); |
| 173 | + for tmp_pair in vote_sum.get_voted_ballot() { |
| 174 | + if tmp_pair.get_candidate() == candidate { |
| 175 | + candidate_ballot = tmp_pair.get_ballot().clone(); |
| 176 | + } |
| 177 | + } |
| 178 | + let mut candidate_voted_c1_sum = |
| 179 | + bytes_to_point(&candidate_ballot.get_ciphertext1())?; |
| 180 | + let mut candidate_voted_c2_sum = |
| 181 | + bytes_to_point(&candidate_ballot.get_ciphertext2())?; |
| 182 | + let mut candidates_ballot = Ballot::new(); |
| 183 | + for ballot_pair in vote_storage_part.get_voted_ballot() { |
| 184 | + if candidate == ballot_pair.get_candidate() { |
| 185 | + candidates_ballot = ballot_pair.get_ballot().clone(); |
| 186 | + } |
| 187 | + } |
| 188 | + candidate_voted_c1_sum += |
| 189 | + bytes_to_point(&candidates_ballot.get_ciphertext1())?; |
| 190 | + candidate_voted_c2_sum += |
| 191 | + bytes_to_point(&candidates_ballot.get_ciphertext2())?; |
| 192 | + let mut vote_ballot = Ballot::new(); |
| 193 | + vote_ballot.set_ciphertext1(point_to_bytes(&candidate_voted_c1_sum)); |
| 194 | + vote_ballot.set_ciphertext2(point_to_bytes(&candidate_voted_c2_sum)); |
| 195 | + let mut tmp_pair = CandidateBallot::new(); |
| 196 | + tmp_pair.set_candidate(candidate.to_string()); |
| 197 | + tmp_pair.set_ballot(vote_ballot); |
| 198 | + tmp_vote_storage_sum.mut_voted_ballot().push(tmp_pair); |
| 199 | + } |
| 200 | + tmp_vote_storage_sum |
| 201 | + .mut_blank_ballot() |
| 202 | + .set_ciphertext1(point_to_bytes(&blank_c1_sum)); |
| 203 | + tmp_vote_storage_sum |
| 204 | + .mut_blank_ballot() |
| 205 | + .set_ciphertext2(point_to_bytes(&blank_c2_sum)); |
| 206 | + *vote_sum = tmp_vote_storage_sum.clone(); |
| 207 | + Ok(true) |
| 208 | +} |
| 209 | + |
| 210 | +/// Count the value of ballots received by each candidate. |
| 211 | +pub fn finalize_vote_result( |
| 212 | + param: &SystemParametersStorage, |
| 213 | + vote_sum: &VoteStorage, |
| 214 | + counting_result_sum: &DecryptedResultPartStorage, |
| 215 | + max_number: i64, |
| 216 | +) -> Result<VoteResultStorage, WedprError> { |
| 217 | + let mut request = VoteResultStorage::new(); |
| 218 | + let blank_c1_sum = |
| 219 | + bytes_to_point(vote_sum.get_blank_ballot().get_ciphertext1())?; |
| 220 | + let blank_c2_r_sum = |
| 221 | + bytes_to_point(counting_result_sum.get_blank_part().get_blinding_c2())?; |
| 222 | + let tmp = blank_c1_sum - (blank_c2_r_sum); |
| 223 | + for i in 1..=max_number { |
| 224 | + let try_num = Scalar::from(i as u64); |
| 225 | + if tmp.eq(&(*BASEPOINT_G1 * try_num)) { |
| 226 | + let mut tmp_pair = StringToInt64Pair::new(); |
| 227 | + tmp_pair.set_key("Wedpr_voting_total_ballots".to_string()); |
| 228 | + tmp_pair.set_value(i); |
| 229 | + request.mut_result().push(tmp_pair); |
| 230 | + break; |
| 231 | + } |
| 232 | + } |
| 233 | + for candidate in param.get_candidates().get_candidate() { |
| 234 | + let mut ballot = Ballot::new(); |
| 235 | + for tmp_pair in vote_sum.get_voted_ballot() { |
| 236 | + if candidate == tmp_pair.get_candidate() { |
| 237 | + ballot = tmp_pair.get_ballot().clone(); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + let mut candidate_counting_part = CountingPart::new(); |
| 242 | + for tmp_pair in counting_result_sum.get_candidate_part() { |
| 243 | + if candidate == tmp_pair.get_key() { |
| 244 | + candidate_counting_part = tmp_pair.get_value().clone(); |
| 245 | + } |
| 246 | + } |
| 247 | + let candidate_c2_r_sum = |
| 248 | + bytes_to_point(candidate_counting_part.get_blinding_c2())?; |
| 249 | + let tmp = |
| 250 | + bytes_to_point(ballot.get_ciphertext1())? - (candidate_c2_r_sum); |
| 251 | + |
| 252 | + for i in 0..=max_number { |
| 253 | + let try_num = Scalar::from(i as u64); |
| 254 | + |
| 255 | + if tmp.eq(&(*BASEPOINT_G1 * try_num)) { |
| 256 | + let mut tmp_pair = StringToInt64Pair::new(); |
| 257 | + tmp_pair.set_key(candidate.to_string()); |
| 258 | + tmp_pair.set_value(i); |
| 259 | + request.mut_result().push(tmp_pair); |
| 260 | + break; |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + Ok(request) |
| 265 | +} |
0 commit comments