-
Notifications
You must be signed in to change notification settings - Fork 1
feat(rust/catalyst-voting): Tally proof generation and verification #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
05af510
initialize a new crate
Mr-Leshiy 6935a1b
add intentionally failed test
Mr-Leshiy 78f601f
fix CI
Mr-Leshiy 9423567
fix
Mr-Leshiy 5d6b95b
fix
Mr-Leshiy b8d7ece
update vscode setting.recommended.json
Mr-Leshiy d644602
Merge branch 'main' into feat/voting-crate-setup
Mr-Leshiy e70f571
Merge branch 'main' into feat/el-gamal
Mr-Leshiy 0803b50
add a basic interfaces for the vote part
Mr-Leshiy 0ea84b3
add basic elgamal encryption based on the ristretto255 group
Mr-Leshiy 91106ff
add arithmetic tests for ristretto255
Mr-Leshiy e1b8251
fix tests
Mr-Leshiy 6c2d961
wip
Mr-Leshiy 594d114
add decryption algorithm, add tests
Mr-Leshiy c1b749d
fix CI
Mr-Leshiy 0a5bb99
remove unused std_ops_gen
Mr-Leshiy 0348b6c
add new voter module
Mr-Leshiy 9d9ddd6
add EncryptionRandomness random generation
Mr-Leshiy c342a1e
add a tally function
Mr-Leshiy bc658d6
Merge branch 'main' into feat/tally
Mr-Leshiy 7eacd5b
fix
Mr-Leshiy 15d97c6
wip
Mr-Leshiy dd1b1a3
add a babystep implementation
Mr-Leshiy a3c4d61
wip
Mr-Leshiy dcd1484
refactor, add decrypt_tally_result
Mr-Leshiy 4936687
wip
Mr-Leshiy f572ad3
wip
Mr-Leshiy 81a6323
add voting test
Mr-Leshiy f988417
remove rayon dependency for now
Mr-Leshiy 37cf886
fix spelling, remove rayon
Mr-Leshiy 24da2bc
fix
Mr-Leshiy 804c723
remove unused anyhow dep
Mr-Leshiy 921ad46
intentionally break the test
Mr-Leshiy fa63fe1
try
Mr-Leshiy c54f806
wip
Mr-Leshiy c444a02
update DecryptionTallySetup interface
Mr-Leshiy a63d11a
add doctest example
Mr-Leshiy 688f5b7
refactor, make voting_test as integration test
Mr-Leshiy 8466187
fix baby_step_giant_step_test
Mr-Leshiy eafd538
move tally module into the seprate dir
Mr-Leshiy ed25f6c
add new proof.rs
Mr-Leshiy 5918f0d
refactor
Mr-Leshiy 87af867
add to_bytes, from_bytes functions for Scalar and GroupElement
Mr-Leshiy 5c399b9
add zk_dl_equality.rs
Mr-Leshiy dde81b8
add hash module
Mr-Leshiy e7d5079
update hash implementation
Mr-Leshiy c275f5a
add dleq verify function, add tests
Mr-Leshiy ae6c00c
implement tally proof generation and tally proof verification
Mr-Leshiy 0a9ac9b
update voting_test with the tally proofs, fix verify_tally_proof
Mr-Leshiy ee09463
remove uneeded comment
Mr-Leshiy c75c0b0
Merge branch 'main' into feat/tally-proof
Mr-Leshiy 1ca194c
fix
Mr-Leshiy 5533456
fix
Mr-Leshiy 359cf53
fix spelling
Mr-Leshiy a0e25ea
fix comment
Mr-Leshiy 23b0bdc
update rust docs
Mr-Leshiy 9fd9e0c
fix rustdoc tests
Mr-Leshiy 8ddca46
Merge branch 'main' into feat/tally-proof
Mr-Leshiy 041eb66
Merge branch 'main' into feat/tally-proof
Mr-Leshiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ dbsync | |
| dcbor | ||
| decompressor | ||
| delegators | ||
| dleq | ||
| dockerhub | ||
| Dominik | ||
| dotenv | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,6 @@ | ||
| //! Group definitions used in voting protocol. | ||
| //! For more information, see: <https://input-output-hk.github.io/catalyst-voices/architecture/08_concepts/voting_transaction/crypto/#a-group-definition> | ||
|
|
||
| mod babystep_giantstep; | ||
| mod ristretto255; | ||
|
|
||
| pub(crate) use babystep_giantstep::BabyStepGiantStep; | ||
| pub(crate) use ristretto255::{GroupElement, Scalar}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //! Blake2b-256 hash implementation. | ||
|
|
||
| use curve25519_dalek::digest::{ | ||
| consts::U64, typenum::Unsigned, FixedOutput, HashMarker, Output, OutputSizeUser, Update, | ||
| }; | ||
|
|
||
| /// Blake2b-512 hasher instance. | ||
| pub struct Blake2b512Hasher(blake2b_simd::State); | ||
|
|
||
| impl Blake2b512Hasher { | ||
| /// Create a new `Blake2b256Hasher`. | ||
| pub fn new() -> Self { | ||
| Self( | ||
| blake2b_simd::Params::new() | ||
| .hash_length(Self::output_size()) | ||
| .to_state(), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // Implementation of the `digest::Digest` trait for `Blake2b256Hasher`. | ||
|
|
||
| impl Default for Blake2b512Hasher { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl Update for Blake2b512Hasher { | ||
| fn update(&mut self, data: &[u8]) { | ||
| self.0.update(data); | ||
| } | ||
| } | ||
|
|
||
| impl OutputSizeUser for Blake2b512Hasher { | ||
| type OutputSize = U64; | ||
|
|
||
| fn output_size() -> usize { | ||
| Self::OutputSize::USIZE | ||
| } | ||
| } | ||
|
|
||
| impl FixedOutput for Blake2b512Hasher { | ||
| fn finalize_into(self, out: &mut Output<Self>) { | ||
| let hash = self.0.finalize(); | ||
| out.copy_from_slice(hash.as_bytes()); | ||
| } | ||
| } | ||
|
|
||
| impl HashMarker for Blake2b512Hasher {} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| //! Crypto primitives which are used by voting protocol. | ||
|
|
||
| pub(crate) mod babystep_giantstep; | ||
| pub(crate) mod elgamal; | ||
| pub(crate) mod group; | ||
| pub(crate) mod hash; | ||
| pub(crate) mod zk_dl_equality; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| //! Non-interactive Zero Knowledge proof of Discrete Logarithm | ||
| //! Equality (DLEQ). | ||
| //! | ||
| //! The proof is the following: | ||
| //! | ||
| //! `NIZK{(base_1, base_2, point_1, point_2), (dlog): point_1 = base_1^dlog AND point_2 = | ||
| //! base_2^dlog}` | ||
| //! | ||
| //! which makes the statement, the two bases `base_1` and `base_2`, and the two | ||
| //! points `point_1` and `point_2`. The witness, on the other hand | ||
| //! is the discrete logarithm, `dlog`. | ||
|
|
||
| // cspell: words NIZK dlog | ||
|
|
||
| use curve25519_dalek::digest::Update; | ||
|
|
||
| use super::{ | ||
| group::{GroupElement, Scalar}, | ||
| hash::Blake2b512Hasher, | ||
| }; | ||
|
|
||
| /// DLEQ proof struct | ||
| pub struct DleqProof(Scalar, Scalar); | ||
|
|
||
| /// Generates a DLEQ proof. | ||
| pub fn generate_dleq_proof( | ||
| base_1: &GroupElement, base_2: &GroupElement, point_1: &GroupElement, point_2: &GroupElement, | ||
| dlog: &Scalar, randomness: &Scalar, | ||
| ) -> DleqProof { | ||
| let a_1 = base_1 * randomness; | ||
| let a_2 = base_2 * randomness; | ||
|
|
||
| let challenge = calculate_challenge(base_1, base_2, point_1, point_2, &a_1, &a_2); | ||
| let response = &(dlog * &challenge) + randomness; | ||
|
|
||
| DleqProof(challenge, response) | ||
| } | ||
|
|
||
| /// Verify a DLEQ proof. | ||
| pub fn verify_dleq_proof( | ||
| proof: &DleqProof, base_1: &GroupElement, base_2: &GroupElement, point_1: &GroupElement, | ||
| point_2: &GroupElement, | ||
| ) -> bool { | ||
| let a_1 = &(base_1 * &proof.1) - &(point_1 * &proof.0); | ||
| let a_2 = &(base_2 * &proof.1) - &(point_2 * &proof.0); | ||
|
|
||
| let challenge = calculate_challenge(base_1, base_2, point_1, point_2, &a_1, &a_2); | ||
| challenge == proof.0 | ||
| } | ||
|
|
||
| /// Calculates the challenge value. | ||
| /// Its a hash value represented as `Scalar` of all provided elements. | ||
| fn calculate_challenge( | ||
| base_1: &GroupElement, base_2: &GroupElement, point_1: &GroupElement, point_2: &GroupElement, | ||
| a_1: &GroupElement, a_2: &GroupElement, | ||
| ) -> Scalar { | ||
| let blake2b_hasher = Blake2b512Hasher::new() | ||
| .chain(base_1.to_bytes()) | ||
| .chain(base_2.to_bytes()) | ||
| .chain(point_1.to_bytes()) | ||
| .chain(point_2.to_bytes()) | ||
| .chain(a_1.to_bytes()) | ||
| .chain(a_2.to_bytes()); | ||
|
|
||
| Scalar::from_hash(blake2b_hasher) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::ops::Mul; | ||
|
|
||
| use test_strategy::proptest; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[proptest] | ||
| fn zk_dleq_test(e1: Scalar, e2: Scalar, dlog1: Scalar, dlog2: Scalar, randomness: Scalar) { | ||
| let base_1 = GroupElement::GENERATOR.mul(&e1); | ||
| let base_2 = GroupElement::GENERATOR.mul(&e2); | ||
|
|
||
| let point_1 = base_1.mul(&dlog1); | ||
| let point_2 = base_2.mul(&dlog1); | ||
|
|
||
| let proof = generate_dleq_proof(&base_1, &base_2, &point_1, &point_2, &dlog1, &randomness); | ||
| assert!(verify_dleq_proof( | ||
| &proof, &base_1, &base_2, &point_1, &point_2 | ||
| )); | ||
|
|
||
| // use different discrete logarithm for both points | ||
| let point_1 = base_1.mul(&dlog2); | ||
| let point_2 = base_2.mul(&dlog2); | ||
|
|
||
| let proof = generate_dleq_proof(&base_1, &base_2, &point_1, &point_2, &dlog1, &randomness); | ||
| assert!(!verify_dleq_proof( | ||
| &proof, &base_1, &base_2, &point_1, &point_2 | ||
| )); | ||
|
|
||
| // use different discrete logarithm across points | ||
| let point_1 = base_1.mul(&dlog1); | ||
| let point_2 = base_2.mul(&dlog2); | ||
|
|
||
| let proof = generate_dleq_proof(&base_1, &base_2, &point_1, &point_2, &dlog1, &randomness); | ||
| assert!(!verify_dleq_proof( | ||
| &proof, &base_1, &base_2, &point_1, &point_2 | ||
| )); | ||
| let proof = generate_dleq_proof(&base_1, &base_2, &point_1, &point_2, &dlog2, &randomness); | ||
| assert!(!verify_dleq_proof( | ||
| &proof, &base_1, &base_2, &point_1, &point_2 | ||
| )); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.