-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
287 additions
and
131 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 |
---|---|---|
|
@@ -5,3 +5,4 @@ edition = "2021" | |
publish = false | ||
|
||
[dependencies] | ||
signature = "2.1.0" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use core::fmt::Debug; | ||
|
||
use signature::{Signer, Verifier}; | ||
|
||
/// Defines the requirements for a private key type. | ||
pub trait PrivateKey | ||
where | ||
Self: Clone + Debug + Signer<Self::Signature>, | ||
{ | ||
type Signature: Clone + Debug + PartialEq + Eq; | ||
type PublicKey: PublicKey<Signature = Self::Signature>; | ||
|
||
fn public_key(&self) -> Self::PublicKey; | ||
} | ||
|
||
/// Defines the requirements for a public key type. | ||
pub trait PublicKey | ||
where | ||
Self: Clone + Debug + PartialEq + Eq + Verifier<Self::Signature>, | ||
{ | ||
type Signature: Clone + Debug + PartialEq + Eq; | ||
} |
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
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
[package] | ||
name = "malachite-test" | ||
name = "malachite-test" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
malachite-common = { version = "0.1.0", path = "../common" } | ||
malachite-common = { version = "0.1.0", path = "../common" } | ||
malachite-consensus = { version = "0.1.0", path = "../consensus" } | ||
malachite-round = { version = "0.1.0", path = "../round" } | ||
malachite-vote = { version = "0.1.0", path = "../vote" } | ||
malachite-round = { version = "0.1.0", path = "../round" } | ||
malachite-vote = { version = "0.1.0", path = "../vote" } | ||
|
||
ed25519-consensus = "2.1.0" | ||
signature = "2.1.0" | ||
rand = { version = "0.8.5", features = ["std_rng"] } | ||
sha2 = "0.10.8" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use ed25519_consensus::{Signature, SigningKey, VerificationKey}; | ||
|
||
use malachite_common::{PrivateKey, PublicKey}; | ||
use rand::{CryptoRng, RngCore}; | ||
use signature::{Signer, Verifier}; | ||
|
||
pub type Ed25519Signature = Signature; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Ed25519PrivateKey(SigningKey); | ||
|
||
impl Ed25519PrivateKey { | ||
pub fn generate<R>(rng: R) -> Self | ||
where | ||
R: RngCore + CryptoRng, | ||
{ | ||
let signing_key = SigningKey::new(rng); | ||
|
||
Self(signing_key) | ||
} | ||
|
||
pub fn public_key(&self) -> Ed25519PublicKey { | ||
Ed25519PublicKey::new(self.0.verification_key()) | ||
} | ||
} | ||
|
||
impl PrivateKey for Ed25519PrivateKey { | ||
type Signature = Signature; | ||
type PublicKey = Ed25519PublicKey; | ||
|
||
fn public_key(&self) -> Self::PublicKey { | ||
self.public_key() | ||
} | ||
} | ||
|
||
impl Signer<Signature> for Ed25519PrivateKey { | ||
fn try_sign(&self, msg: &[u8]) -> Result<Signature, signature::Error> { | ||
Ok(self.0.sign(msg)) | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
pub struct Ed25519PublicKey(VerificationKey); | ||
|
||
impl Ed25519PublicKey { | ||
pub fn new(key: impl Into<VerificationKey>) -> Self { | ||
Self(key.into()) | ||
} | ||
|
||
pub fn hash(&self) -> [u8; 32] { | ||
use sha2::{Digest, Sha256}; | ||
let mut hasher = Sha256::new(); | ||
hasher.update(self.0.as_bytes()); | ||
hasher.finalize().into() | ||
} | ||
} | ||
|
||
impl PublicKey for Ed25519PublicKey { | ||
type Signature = Signature; | ||
} | ||
|
||
impl Verifier<Signature> for Ed25519PublicKey { | ||
fn verify(&self, msg: &[u8], signature: &Signature) -> Result<(), signature::Error> { | ||
self.0 | ||
.verify(signature, msg) | ||
.map_err(|_| signature::Error::new()) | ||
} | ||
} |
Oops, something went wrong.