mirror of
https://github.com/saymrwulf/risc0-curve25519-dalek-source.git
synced 2026-09-09 20:50:40 +00:00
Impl signature::Digest* traits for Ed25519ph (#270)
* Impl `signature::Digest*` traits for Ed25519ph Adds the following trait impls: - impl DigestSigner for SigningKey - impl DigestVerifier for VerifyingKey These traits can be used to create and verify Ed25519 signatures, thunking to `SigningKey::sign_prehashed` and `VerifyingKey::verify_prehashed` respectively. * Add rustdoc comments for trait impls
This commit is contained in:
parent
f61e9dcf9b
commit
ba765a5988
4 changed files with 41 additions and 2 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -301,6 +301,7 @@ dependencies = [
|
||||||
"serde_bytes",
|
"serde_bytes",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sha2",
|
"sha2",
|
||||||
|
"signature",
|
||||||
"toml",
|
"toml",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
@ -749,6 +750,9 @@ name = "signature"
|
||||||
version = "2.0.0"
|
version = "2.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8fe458c98333f9c8152221191a77e2a44e8325d0193484af2e9421a53019e57d"
|
checksum = "8fe458c98333f9c8152221191a77e2a44e8325d0193484af2e9421a53019e57d"
|
||||||
|
dependencies = [
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spki"
|
name = "spki"
|
||||||
|
|
|
||||||
|
|
@ -26,11 +26,14 @@ features = ["nightly", "batch", "pkcs8"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
curve25519-dalek = { version = "=4.0.0-pre.5", default-features = false, features = ["digest"] }
|
curve25519-dalek = { version = "=4.0.0-pre.5", default-features = false, features = ["digest"] }
|
||||||
ed25519 = { version = "2", default-features = false }
|
ed25519 = { version = "2", default-features = false }
|
||||||
|
signature = { version = ">=2.0, <2.1", optional = true, default-features = false }
|
||||||
|
sha2 = { version = "0.10", default-features = false }
|
||||||
|
|
||||||
|
# optional features
|
||||||
merlin = { version = "3", default-features = false, optional = true }
|
merlin = { version = "3", default-features = false, optional = true }
|
||||||
rand_core = { version = "0.6.4", default-features = false, optional = true }
|
rand_core = { version = "0.6.4", default-features = false, optional = true }
|
||||||
serde = { version = "1.0", default-features = false, optional = true }
|
serde = { version = "1.0", default-features = false, optional = true }
|
||||||
serde_bytes = { version = "0.11", optional = true }
|
serde_bytes = { version = "0.11", optional = true }
|
||||||
sha2 = { version = "0.10", default-features = false }
|
|
||||||
zeroize = { version = "1.5", default-features = false, optional = true }
|
zeroize = { version = "1.5", default-features = false, optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
@ -58,7 +61,7 @@ std = ["alloc", "ed25519/std", "serde?/std", "sha2/std"]
|
||||||
asm = ["sha2/asm"]
|
asm = ["sha2/asm"]
|
||||||
batch = ["alloc", "merlin", "rand_core"]
|
batch = ["alloc", "merlin", "rand_core"]
|
||||||
fast = ["curve25519-dalek/precomputed-tables"]
|
fast = ["curve25519-dalek/precomputed-tables"]
|
||||||
digest = []
|
digest = ["signature/digest"]
|
||||||
# This features turns off stricter checking for scalar malleability in signatures
|
# This features turns off stricter checking for scalar malleability in signatures
|
||||||
legacy_compatibility = []
|
legacy_compatibility = []
|
||||||
pkcs8 = ["ed25519/pkcs8"]
|
pkcs8 = ["ed25519/pkcs8"]
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,9 @@ use curve25519_dalek::scalar::Scalar;
|
||||||
|
|
||||||
use ed25519::signature::{KeypairRef, Signer, Verifier};
|
use ed25519::signature::{KeypairRef, Signer, Verifier};
|
||||||
|
|
||||||
|
#[cfg(feature = "digest")]
|
||||||
|
use signature::DigestSigner;
|
||||||
|
|
||||||
#[cfg(feature = "zeroize")]
|
#[cfg(feature = "zeroize")]
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop};
|
use zeroize::{Zeroize, ZeroizeOnDrop};
|
||||||
|
|
||||||
|
|
@ -484,6 +487,17 @@ impl Signer<ed25519::Signature> for SigningKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Equivalent to [`SigningKey::sign_prehashed`] with `context` set to [`None`].
|
||||||
|
#[cfg(feature = "digest")]
|
||||||
|
impl<D> DigestSigner<D, ed25519::Signature> for SigningKey
|
||||||
|
where
|
||||||
|
D: Digest<OutputSize = U64>,
|
||||||
|
{
|
||||||
|
fn try_sign_digest(&self, msg_digest: D) -> Result<ed25519::Signature, SignatureError> {
|
||||||
|
self.sign_prehashed(msg_digest, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Verifier<ed25519::Signature> for SigningKey {
|
impl Verifier<ed25519::Signature> for SigningKey {
|
||||||
/// Verify a signature on a message with this signing key's public key.
|
/// Verify a signature on a message with this signing key's public key.
|
||||||
fn verify(&self, message: &[u8], signature: &ed25519::Signature) -> Result<(), SignatureError> {
|
fn verify(&self, message: &[u8], signature: &ed25519::Signature) -> Result<(), SignatureError> {
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,9 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde_bytes::{ByteBuf as SerdeByteBuf, Bytes as SerdeBytes};
|
use serde_bytes::{ByteBuf as SerdeByteBuf, Bytes as SerdeBytes};
|
||||||
|
|
||||||
|
#[cfg(feature = "digest")]
|
||||||
|
use signature::DigestVerifier;
|
||||||
|
|
||||||
use crate::constants::*;
|
use crate::constants::*;
|
||||||
use crate::errors::*;
|
use crate::errors::*;
|
||||||
use crate::signature::*;
|
use crate::signature::*;
|
||||||
|
|
@ -417,6 +420,21 @@ impl Verifier<ed25519::Signature> for VerifyingKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Equivalent to [`VerifyingKey::verify_prehashed`] with `context` set to [`None`].
|
||||||
|
#[cfg(feature = "digest")]
|
||||||
|
impl<D> DigestVerifier<D, ed25519::Signature> for VerifyingKey
|
||||||
|
where
|
||||||
|
D: Digest<OutputSize = U64>,
|
||||||
|
{
|
||||||
|
fn verify_digest(
|
||||||
|
&self,
|
||||||
|
msg_digest: D,
|
||||||
|
signature: &ed25519::Signature,
|
||||||
|
) -> Result<(), SignatureError> {
|
||||||
|
self.verify_prehashed(msg_digest, None, signature)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl TryFrom<&[u8]> for VerifyingKey {
|
impl TryFrom<&[u8]> for VerifyingKey {
|
||||||
type Error = SignatureError;
|
type Error = SignatureError;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue