-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust/catalyst-voting): Jormungandr tx building (#60)
* move vote protocol under the vote_protocol mod * add jormungandr tx struct * add CipherText serde * add EncryptedVote decoding functionality * add new deserializers * refactor * wip * wip * replace thiserror with anyhow * move decoding functionalities to separate module * wip * add test * fix * refactor * fix tests * wip * wip * fix spelling * add v1::Tx generation functions * add test * refactor, add ElectionPublicKey, ElectionSecretKey * fix * fix with must_use * fix docs * refactor digest crate imports * add ed25519 impl * add ed25519 decoding functionality * update v1::Tx * add Tx signing * wip * fix * wip * add Blake2b-256 hash impl, update v1::Tx sign * add txs::v1 doc test * update rust docs * make rng optional * wip * update v1::Tx decoding * add signature and proof verification * update verification * update decoding test * add decrypt_vote function * add private_choice, public_choice methods * fix spelling
- Loading branch information
Showing
23 changed files
with
1,040 additions
and
320 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -29,6 +29,7 @@ CBOR | |
cbork | ||
cdylib | ||
CEST | ||
chacha | ||
CHAINCODE | ||
chainsync | ||
childs | ||
|
This file contains 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 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,42 @@ | ||
//! `Ed25519` objects decoding implementation | ||
|
||
use ed25519_dalek::{ | ||
Signature as Ed25519Signature, VerifyingKey, PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH, | ||
}; | ||
|
||
use super::{PublicKey, Signature}; | ||
|
||
impl PublicKey { | ||
/// `PublicKey` bytes size | ||
pub const BYTES_SIZE: usize = PUBLIC_KEY_LENGTH; | ||
|
||
/// Convert this `PublicKey` to its underlying sequence of bytes. | ||
#[must_use] | ||
pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] { | ||
self.0.to_bytes() | ||
} | ||
|
||
/// Attempt to construct a `PublicKey` from a byte representation. | ||
/// | ||
/// # Errors | ||
/// - Cannot decode public key. | ||
pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> anyhow::Result<Self> { | ||
Ok(Self(VerifyingKey::from_bytes(bytes)?)) | ||
} | ||
} | ||
|
||
impl Signature { | ||
/// `Signature` bytes size | ||
pub const BYTES_SIZE: usize = SIGNATURE_LENGTH; | ||
|
||
/// Convert this `Signature` to its underlying sequence of bytes. | ||
#[must_use] | ||
pub fn to_bytes(&self) -> [u8; Self::BYTES_SIZE] { | ||
self.0.to_bytes() | ||
} | ||
|
||
/// Attempt to construct a `Signature` from a byte representation. | ||
pub fn from_bytes(bytes: &[u8; Self::BYTES_SIZE]) -> Self { | ||
Self(Ed25519Signature::from_bytes(bytes)) | ||
} | ||
} |
This file contains 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,72 @@ | ||
//! `EdDSA` digital signature scheme over Curve25519. | ||
|
||
mod decoding; | ||
|
||
use ed25519_dalek::{ | ||
ed25519::signature::Signer, Signature as Ed25519Signature, SigningKey, VerifyingKey, | ||
}; | ||
use rand_core::CryptoRngCore; | ||
|
||
/// `Ed25519` private key struct. | ||
#[must_use] | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct PrivateKey(SigningKey); | ||
|
||
impl PrivateKey { | ||
/// Randomly generate the `Ed25519` private key. | ||
pub fn random<R: CryptoRngCore>(rng: &mut R) -> Self { | ||
Self(SigningKey::generate(rng)) | ||
} | ||
|
||
/// Get associated `Ed25519` public key. | ||
pub fn public_key(&self) -> PublicKey { | ||
PublicKey(self.0.verifying_key()) | ||
} | ||
} | ||
|
||
/// `Ed25519` public key struct. | ||
#[must_use] | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct PublicKey(VerifyingKey); | ||
|
||
/// `Ed25519` signature struct. | ||
#[must_use] | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct Signature(Ed25519Signature); | ||
|
||
/// Sign a message using the `Ed25519` private key. | ||
pub fn sign(sk: &PrivateKey, msg: &[u8]) -> Signature { | ||
Signature(sk.0.sign(msg)) | ||
} | ||
|
||
/// Verify a `Ed25519` signature using the `Ed25519` public key. | ||
#[must_use] | ||
pub fn verify_signature(pk: &PublicKey, msg: &[u8], sig: &Signature) -> bool { | ||
pk.0.verify_strict(msg, &sig.0).is_ok() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use proptest::prelude::{any, Arbitrary, BoxedStrategy, Strategy}; | ||
use test_strategy::proptest; | ||
|
||
use super::*; | ||
|
||
impl Arbitrary for PrivateKey { | ||
type Parameters = (); | ||
type Strategy = BoxedStrategy<Self>; | ||
|
||
fn arbitrary_with((): Self::Parameters) -> Self::Strategy { | ||
any::<[u8; 32]>() | ||
.prop_map(|b| PrivateKey(SigningKey::from_bytes(&b))) | ||
.boxed() | ||
} | ||
} | ||
|
||
#[proptest] | ||
fn sign_test(private_key: PrivateKey, msg: Vec<u8>) { | ||
let public_key = private_key.public_key(); | ||
let signature = sign(&private_key, &msg); | ||
assert!(verify_signature(&public_key, &msg, &signature)); | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.