Skip to content

Commit cdd6add

Browse files
authored
add verifyBlankBallot java ffi (#61)
* add verifyBlankBallot java ffi * store proof with binary data
1 parent bbda7a8 commit cdd6add

File tree

7 files changed

+124
-192
lines changed

7 files changed

+124
-192
lines changed

bounty/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ rand = "0.3.17"
1414
sha3 = "0.8"
1515
wedpr_l_crypto_zkp_discrete_logarithm_proof = {version = "1.3.0", git = "https://github.com/WeBankBlockchain/WeDPR-Lab-Crypto", branch = "dev-1.3.0"}
1616
wedpr_l_crypto_zkp_range_proof = "1.2.0"
17-
wedpr_l_crypto_zkp_utils = "1.0.0"
17+
wedpr_l_crypto_zkp_utils ={version = "1.3.0", git = "https://github.com/WeBankBlockchain/WeDPR-Lab-Crypto", branch = "dev-1.3.0"}
1818
wedpr_l_macros = "1.0.0"
1919
wedpr_l_utils = "1.0.0"
2020
wedpr_s_protos = { path = "../protos" }

ffi/ffi_java/ffi_java_acv/src/verifier.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use wedpr_l_crypto_zkp_utils::bytes_to_point;
1919
use wedpr_s_anonymous_ciphertext_voting;
2020

2121
use wedpr_s_protos::generated::acv::{
22-
DecryptedResultPartStorage, PollParametersStorage, VoteRequest,
23-
VoteResultStorage, VoteStorage,
22+
DecryptedResultPartStorage, PollParametersStorage, RegistrationRequest,
23+
RegistrationResponse, VoteRequest, VoteResultStorage, VoteStorage,
2424
};
2525

2626
// Java FFI: Java interfaces will be generated under
@@ -366,3 +366,46 @@ pub extern "system" fn Java_com_webank_wedpr_acv_NativeInterface_verifyVoteResul
366366
);
367367
result_jobject.into_inner()
368368
}
369+
370+
// Java interface section.
371+
// All functions are under class name 'com.webank.wedpr.acv.NativeInterface'.
372+
/// Java interface for
373+
/// 'com.webank.wedpr.acv.NativeInterface->verifyBlankBallot'.
374+
#[no_mangle]
375+
pub extern "system" fn Java_com_webank_wedpr_acv_NativeInterface_verifyBlankBallot(
376+
_env: JNIEnv,
377+
_class: JClass,
378+
registration_request: JString,
379+
registration_response: JString,
380+
) -> jobject {
381+
let result_jobject = get_result_jobject(&_env);
382+
let pb_registration_request = java_safe_jstring_to_pb!(
383+
_env,
384+
result_jobject,
385+
registration_request,
386+
RegistrationRequest
387+
);
388+
let pb_registration_response = java_safe_jstring_to_pb!(
389+
_env,
390+
result_jobject,
391+
registration_response,
392+
RegistrationResponse
393+
);
394+
let result =
395+
match wedpr_s_anonymous_ciphertext_voting::voter::verify_blank_ballot(
396+
&pb_registration_request,
397+
&pb_registration_response,
398+
) {
399+
Ok(v) => v,
400+
Err(e) => {
401+
return java_set_error_field_and_extract_jobject(
402+
&_env,
403+
&result_jobject,
404+
&format!("verifyBlankBallot failed, err = {:?}", e),
405+
)
406+
},
407+
};
408+
// write verify_result
409+
java_safe_set_boolean_field!(_env, result_jobject, result, "verifyResult");
410+
result_jobject.into_inner()
411+
}

protos/src/lib.rs

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
extern crate lazy_static;
66
use crate::{
77
config::{HASH, SIGNATURE},
8-
generated::{
9-
acv::Ballot,
10-
zkp::{PBBalanceProof, PBEqualityProof},
11-
},
8+
generated::{acv::Ballot, zkp::PBBalanceProof},
129
};
1310
use wedpr_l_utils::{
1411
error::WedprError,
@@ -20,7 +17,7 @@ pub mod generated;
2017
use protobuf::Message;
2118
use wedpr_l_crypto_zkp_utils::{
2219
bytes_to_point, bytes_to_scalar, point_to_bytes, scalar_to_bytes,
23-
ArithmeticProof, BalanceProof, EqualityProof, FormatProof, KnowledgeProof,
20+
ArithmeticProof,
2421
};
2522

2623
pub fn proto_to_bytes<T: Message>(proto: &T) -> Result<Vec<u8>, WedprError> {
@@ -37,80 +34,6 @@ pub fn bytes_to_proto<T: Message>(proto_bytes: &[u8]) -> Result<T, WedprError> {
3734
};
3835
}
3936

40-
/// from struct BalanceProof to PBBalanceProof
41-
pub fn balance_proof_to_pb(balance_proof: &BalanceProof) -> PBBalanceProof {
42-
let mut pb_proof = PBBalanceProof::new();
43-
pb_proof.set_check1(scalar_to_bytes(&balance_proof.check1));
44-
pb_proof.set_check2(scalar_to_bytes(&balance_proof.check2));
45-
pb_proof.set_m1(scalar_to_bytes(&balance_proof.m1));
46-
pb_proof.set_m2(scalar_to_bytes(&balance_proof.m2));
47-
pb_proof.set_m3(scalar_to_bytes(&balance_proof.m3));
48-
pb_proof.set_m4(scalar_to_bytes(&balance_proof.m4));
49-
pb_proof.set_m5(scalar_to_bytes(&balance_proof.m5));
50-
pb_proof.set_m6(scalar_to_bytes(&balance_proof.m6));
51-
pb_proof
52-
}
53-
54-
// from PBBalanceProof to struct BalanceProof
55-
pub fn pb_to_balance_proof(
56-
balance_proof: &PBBalanceProof,
57-
) -> Result<BalanceProof, WedprError> {
58-
Ok(BalanceProof {
59-
check1: bytes_to_scalar(balance_proof.get_check1())?,
60-
check2: bytes_to_scalar(balance_proof.get_check2())?,
61-
m1: bytes_to_scalar(balance_proof.get_m1())?,
62-
m2: bytes_to_scalar(balance_proof.get_m2())?,
63-
m3: bytes_to_scalar(balance_proof.get_m3())?,
64-
m4: bytes_to_scalar(balance_proof.get_m4())?,
65-
m5: bytes_to_scalar(balance_proof.get_m5())?,
66-
m6: bytes_to_scalar(balance_proof.get_m6())?,
67-
})
68-
}
69-
70-
// from struct KnowledgeProof to PBBalanceProof
71-
pub fn knowledge_proof_to_pb(
72-
knowledge_proof: &KnowledgeProof,
73-
) -> PBBalanceProof {
74-
let mut pb_proof = PBBalanceProof::new();
75-
pb_proof.set_t1(point_to_bytes(&knowledge_proof.t1));
76-
pb_proof.set_m1(scalar_to_bytes(&knowledge_proof.m1));
77-
pb_proof.set_m2(scalar_to_bytes(&knowledge_proof.m2));
78-
pb_proof
79-
}
80-
81-
// from PBBalanceProof to struct KnowledgeProof
82-
pub fn pb_to_knowledge_proof(
83-
knowledge_proof: &PBBalanceProof,
84-
) -> Result<KnowledgeProof, WedprError> {
85-
Ok(KnowledgeProof {
86-
t1: bytes_to_point(&knowledge_proof.get_t1())?,
87-
m1: bytes_to_scalar(&knowledge_proof.get_m1())?,
88-
m2: bytes_to_scalar(&knowledge_proof.get_m2())?,
89-
})
90-
}
91-
92-
// from struct FormatProof to PBBalanceProof
93-
pub fn format_proof_to_pb(format_proof: &FormatProof) -> PBBalanceProof {
94-
let mut pb_proof = PBBalanceProof::new();
95-
pb_proof.set_t1(point_to_bytes(&format_proof.t1));
96-
pb_proof.set_t2(point_to_bytes(&format_proof.t2));
97-
pb_proof.set_m1(scalar_to_bytes(&format_proof.m1));
98-
pb_proof.set_m2(scalar_to_bytes(&format_proof.m2));
99-
pb_proof
100-
}
101-
102-
// from PBBalanceProof to struct FormatProof
103-
pub fn pb_to_format_proof(
104-
format_proof: &PBBalanceProof,
105-
) -> Result<FormatProof, WedprError> {
106-
Ok(FormatProof {
107-
t1: bytes_to_point(&format_proof.get_t1())?,
108-
t2: bytes_to_point(&format_proof.get_t2())?,
109-
m1: bytes_to_scalar(&format_proof.get_m1())?,
110-
m2: bytes_to_scalar(&format_proof.get_m2())?,
111-
})
112-
}
113-
11437
// from struct ArithmeticProof to PBBalanceProof
11538
pub fn arithmetric_proof_to_pb(
11639
arithmetric_proof: &ArithmeticProof,
@@ -143,26 +66,6 @@ pub fn pb_to_arithmetric_proof(
14366
})
14467
}
14568

146-
// from struct EqualityProof to PBEqualityProof
147-
pub fn equality_proof_to_pb(equality_proof: &EqualityProof) -> PBEqualityProof {
148-
let mut pb_proof = PBEqualityProof::new();
149-
pb_proof.set_t1(point_to_bytes(&equality_proof.t1));
150-
pb_proof.set_t2(point_to_bytes(&equality_proof.t2));
151-
pb_proof.set_m1(scalar_to_bytes(&equality_proof.m1));
152-
pb_proof
153-
}
154-
155-
// from PBEqualityProof to struct EqualityProof
156-
pub fn pb_to_equality_proof(
157-
equality_proof: &PBEqualityProof,
158-
) -> Result<EqualityProof, WedprError> {
159-
Ok(EqualityProof {
160-
t1: bytes_to_point(&equality_proof.get_t1())?,
161-
t2: bytes_to_point(&equality_proof.get_t2())?,
162-
m1: bytes_to_scalar(&equality_proof.get_m1())?,
163-
})
164-
}
165-
16669
// generate signature for the ballot
16770
pub fn generate_ballot_signature(
16871
secret_key: &[u8],

solution/anonymous_ciphertext_voting/src/counter.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,16 @@
55
use wedpr_l_crypto_zkp_discrete_logarithm_proof::prove_equality_relationship_proof;
66
use wedpr_l_crypto_zkp_utils::{
77
bytes_to_point, bytes_to_scalar, get_random_scalar, point_to_bytes,
8-
scalar_to_bytes, BASEPOINT_G2,
8+
scalar_to_bytes, Serialize, BASEPOINT_G2,
99
};
1010
use wedpr_l_utils::error::WedprError;
1111

12-
use wedpr_s_protos::{
13-
generated::acv::{
14-
CounterParametersShareRequest, CounterSecret, CountingPart,
15-
DecryptedResultPartStorage, StringToCountingPartPair,
16-
UnlistedBallotDecryptedResult, VoteStorage,
17-
},
18-
proto_to_bytes,
12+
use wedpr_s_protos::generated::acv::{
13+
CounterParametersShareRequest, CounterSecret, CountingPart,
14+
DecryptedResultPartStorage, StringToCountingPartPair,
15+
UnlistedBallotDecryptedResult, VoteStorage,
1916
};
2017

21-
use wedpr_s_protos::equality_proof_to_pb;
22-
2318
/// Makes secrets used by a counter.
2419
pub fn make_counter_secret() -> CounterSecret {
2520
let secret_share = get_random_scalar();
@@ -71,9 +66,7 @@ pub fn count(
7166
&BASEPOINT_G2,
7267
&candidate_part_share,
7368
);
74-
counting_part.set_equality_proof(proto_to_bytes(
75-
&(equality_proof_to_pb(&equality_proof)),
76-
)?);
69+
counting_part.set_equality_proof(equality_proof.serialize());
7770
// Write back.
7871
let candidate = candidate_ballot_pair.get_candidate();
7972
let mut candidate_counting_part_pair = StringToCountingPartPair::new();
@@ -97,9 +90,7 @@ pub fn count(
9790

9891
// Write back.
9992
let blank_part = partially_decrypted_result.mut_blank_part();
100-
blank_part.set_equality_proof(proto_to_bytes(&equality_proof_to_pb(
101-
&equality_proof,
102-
))?);
93+
blank_part.set_equality_proof(equality_proof.serialize());
10394
blank_part.set_blinding_c2(point_to_bytes(&blinding_c2));
10495
blank_part.set_counter_id(counter_id.to_string());
10596
Ok(partially_decrypted_result)
@@ -130,9 +121,8 @@ pub fn count_unlisted(
130121
decrypted_unlisted_candidate.set_blinding_c2(point_to_bytes(
131122
&(unlisted_candidate_part_share * secret_share),
132123
));
133-
decrypted_unlisted_candidate.set_equality_proof(proto_to_bytes(
134-
&equality_proof_to_pb(&equality_proof),
135-
)?);
124+
decrypted_unlisted_candidate
125+
.set_equality_proof(equality_proof.serialize());
136126

137127
// decrypt and generaate the equality proof for unlisted candidate
138128
// ballot
@@ -148,9 +138,8 @@ pub fn count_unlisted(
148138
decrypted_ulisted_candidate_ballot.set_blinding_c2(point_to_bytes(
149139
&(unlisted_candidate_ballot_part_share * secret_share),
150140
));
151-
decrypted_ulisted_candidate_ballot.set_equality_proof(proto_to_bytes(
152-
&equality_proof_to_pb(&equality_proof),
153-
)?);
141+
decrypted_ulisted_candidate_ballot
142+
.set_equality_proof(equality_proof.serialize());
154143

155144
let mut unlisted_candidate_part_item =
156145
UnlistedBallotDecryptedResult::new();

solution/anonymous_ciphertext_voting/src/lib.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ mod tests {
4646
.push(counter_parameters_share);
4747
counter_secret_list.push(counter_secret);
4848
}
49-
5049
// Initialize the coordinator.
5150
let (public_key, secret_key) = SIGNATURE.generate_keypair();
5251

@@ -60,7 +59,6 @@ mod tests {
6059
&counter_parameters,
6160
)
6261
.unwrap();
63-
6462
// Initialize all voters.
6563
let mut voter_secret_list: Vec<VoterSecret> = vec![];
6664
let mut voter_registration_list = vec![];
@@ -121,7 +119,6 @@ mod tests {
121119
&public_key
122120
)
123121
.unwrap());
124-
125122
// Coordinator aggregates individual ciphertext ballots.
126123
assert!(coordinator::aggregate_vote_sum_response(
127124
&poll_parameters,
@@ -131,7 +128,6 @@ mod tests {
131128
.unwrap());
132129
vote_request_list.push(vote_request);
133130
}
134-
135131
// All counters decrypt the poll result in a distributed manner.
136132
let mut aggregated_decrypted_result = DecryptedResultPartStorage::new();
137133
for index in 0..counter_secret_list.len() {
@@ -153,7 +149,6 @@ mod tests {
153149
&partially_decrypted_result
154150
)
155151
.unwrap());
156-
157152
// Coordinator aggregates parts of decrypted poll result.
158153
assert!(coordinator::aggregate_decrypted_part_sum(
159154
&poll_parameters,
@@ -162,7 +157,6 @@ mod tests {
162157
)
163158
.unwrap());
164159
}
165-
166160
// Coordinator decrypts the final poll result by enumerating all
167161
// possible value and checking ZKP data.
168162
// TODO: Design a better way to do the decryption.
@@ -298,6 +292,20 @@ mod tests {
298292
)
299293
.unwrap();
300294

295+
// verify blank ballot
296+
let result =
297+
voter::verify_blank_ballot(&registration_request1, &response1)
298+
.unwrap();
299+
assert_eq!(result, true);
300+
let result =
301+
voter::verify_blank_ballot(&registration_request2, &response2)
302+
.unwrap();
303+
assert_eq!(result, true);
304+
let result =
305+
voter::verify_blank_ballot(&registration_request3, &response3)
306+
.unwrap();
307+
assert_eq!(result, true);
308+
301309
// begin vote
302310
let mut encrypted_vote_sum = VoteStorage::new();
303311
let make_choice = |x: &Vec<i32>| {
@@ -614,7 +622,19 @@ mod tests {
614622
blank_ballot_count[2],
615623
)
616624
.unwrap();
617-
625+
// verify blank ballot
626+
let result =
627+
voter::verify_blank_ballot(&registration_request1, &response1)
628+
.unwrap();
629+
assert_eq!(result, true);
630+
let result =
631+
voter::verify_blank_ballot(&registration_request2, &response2)
632+
.unwrap();
633+
assert_eq!(result, true);
634+
let result =
635+
voter::verify_blank_ballot(&registration_request3, &response3)
636+
.unwrap();
637+
assert_eq!(result, true);
618638
let mut encrypted_vote_sum = VoteStorage::new();
619639

620640
let make_choice = |x: &Vec<i32>, y: &Vec<i64>| {

0 commit comments

Comments
 (0)