Skip to content

Commit a739e30

Browse files
committed
remove func to coordinato
1 parent f187bbd commit a739e30

File tree

4 files changed

+170
-167
lines changed

4 files changed

+170
-167
lines changed

solution/anonymous_ciphertext_voting/src/coordinator.rs

Lines changed: 145 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
//! Library of anonymous ciphertext voting (ACV) solution.
44
5-
use crate::config::{HASH_KECCAK256, SIGNATURE_SECP256K1};
65
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};
87
use wedpr_l_utils::{
98
error::WedprError,
109
traits::{Hash, Signature},
1110
};
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};
1715

1816
/// Makes system parameters containing public key and candidates list using counter storage messages.
1917
pub fn make_system_parameters(
@@ -125,3 +123,143 @@ pub fn aggregate_decrypted_part_sum(
125123
}
126124
Ok(true)
127125
}
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+
}

solution/anonymous_ciphertext_voting/src/counter.rs

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
//! Library of anonymous ciphertext voting (ACV) solution.
44
5-
use curve25519_dalek::scalar::Scalar;
65
use wedpr_l_crypto_zkp_discrete_logarithm_proof::prove_equality_relationship_proof;
76
use wedpr_l_crypto_zkp_utils::{
8-
bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes,
9-
scalar_to_bytes, BASEPOINT_G1, BASEPOINT_G2,
7+
BASEPOINT_G2, bytes_to_point, bytes_to_scalar,
8+
get_random_scalar, point_to_bytes, scalar_to_bytes,
109
};
1110
use wedpr_l_protos::proto_to_bytes;
1211
use wedpr_l_utils::error::WedprError;
12+
1313
use wedpr_s_protos::generated::acv::{
14-
Ballot, CounterSecret, CounterSystemParametersShareRequest, CountingPart,
15-
DecryptedResultPartStorage, StringToCountingPartPair, StringToInt64Pair,
16-
SystemParametersStorage, VoteResultStorage, VoteStorage,
14+
CounterSecret, CounterSystemParametersShareRequest, CountingPart,
15+
DecryptedResultPartStorage, StringToCountingPartPair,
16+
VoteStorage,
1717
};
1818

1919
/// Generates a random number as secret key used for making system parameter and counting.
@@ -97,60 +97,3 @@ pub fn count(
9797
.set_counter_id(counter_id.to_string());
9898
Ok(request)
9999
}
100-
101-
/// Count the value of ballots received by each candidate.
102-
pub fn finalize_vote_result(
103-
param: &SystemParametersStorage,
104-
vote_sum: &VoteStorage,
105-
counting_result_sum: &DecryptedResultPartStorage,
106-
max_number: i64,
107-
) -> Result<VoteResultStorage, WedprError> {
108-
let mut request = VoteResultStorage::new();
109-
let blank_c1_sum =
110-
bytes_to_point(vote_sum.get_blank_ballot().get_ciphertext1())?;
111-
let blank_c2_r_sum =
112-
bytes_to_point(counting_result_sum.get_blank_part().get_blinding_c2())?;
113-
let tmp = blank_c1_sum - (blank_c2_r_sum);
114-
for i in 1..=max_number {
115-
let try_num = Scalar::from(i as u64);
116-
if tmp.eq(&(*BASEPOINT_G1 * try_num)) {
117-
let mut tmp_pair = StringToInt64Pair::new();
118-
tmp_pair.set_key("Wedpr_voting_total_ballots".to_string());
119-
tmp_pair.set_value(i);
120-
request.mut_result().push(tmp_pair);
121-
break;
122-
}
123-
}
124-
for candidate in param.get_candidates().get_candidate() {
125-
let mut ballot = Ballot::new();
126-
for tmp_pair in vote_sum.get_voted_ballot() {
127-
if candidate == tmp_pair.get_candidate() {
128-
ballot = tmp_pair.get_ballot().clone();
129-
}
130-
}
131-
132-
let mut candidate_counting_part = CountingPart::new();
133-
for tmp_pair in counting_result_sum.get_candidate_part() {
134-
if candidate == tmp_pair.get_key() {
135-
candidate_counting_part = tmp_pair.get_value().clone();
136-
}
137-
}
138-
let candidate_c2_r_sum =
139-
bytes_to_point(candidate_counting_part.get_blinding_c2())?;
140-
let tmp =
141-
bytes_to_point(ballot.get_ciphertext1())? - (candidate_c2_r_sum);
142-
143-
for i in 0..=max_number {
144-
let try_num = Scalar::from(i as u64);
145-
146-
if tmp.eq(&(*BASEPOINT_G1 * try_num)) {
147-
let mut tmp_pair = StringToInt64Pair::new();
148-
tmp_pair.set_key(candidate.to_string());
149-
tmp_pair.set_value(i);
150-
request.mut_result().push(tmp_pair);
151-
break;
152-
}
153-
}
154-
}
155-
Ok(request)
156-
}

solution/anonymous_ciphertext_voting/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ pub mod voter;
1616

1717
#[cfg(test)]
1818
mod tests {
19-
use super::*;
20-
use crate::config::SIGNATURE_SECP256K1;
2119
use wedpr_l_crypto_zkp_utils::bytes_to_point;
2220
use wedpr_l_utils::traits::Signature;
21+
2322
use wedpr_s_protos::generated::acv::{
2423
CandidateList, CounterSecret, CounterSystemParametersStorage,
25-
DecryptedResultPartStorage, VoteChoice, VoteChoices, VoteStorage,
26-
VoterSecret,
24+
DecryptedResultPartStorage, VoteChoice, VoteChoices, VoterSecret,
25+
VoteStorage,
2726
};
2827

28+
use crate::config::SIGNATURE_SECP256K1;
29+
use crate::coordinator;
30+
31+
use super::*;
32+
2933
#[test]
3034
fn test_bounded_voting() {
3135
// Generate coordinator's key pair
@@ -125,7 +129,7 @@ mod tests {
125129
);
126130
assert_eq!(
127131
true,
128-
verifier::aggregate_vote_sum_response(
132+
coordinator::aggregate_vote_sum_response(
129133
&system_parameters,
130134
&vote_request.get_vote(),
131135
&mut encrypted_vote_sum
@@ -169,7 +173,7 @@ mod tests {
169173
.unwrap()
170174
);
171175
}
172-
let final_result_request = counter::finalize_vote_result(
176+
let final_result_request = coordinator::finalize_vote_result(
173177
&system_parameters,
174178
&encrypted_vote_sum,
175179
&vote_sum_total,

0 commit comments

Comments
 (0)