diff --git a/ecdsa/src/lib.rs b/ecdsa/src/lib.rs index 7825a962..a449829b 100644 --- a/ecdsa/src/lib.rs +++ b/ecdsa/src/lib.rs @@ -57,6 +57,7 @@ #[cfg(feature = "alloc")] extern crate alloc; +mod normalized; mod recovery; #[cfg(feature = "der")] @@ -70,7 +71,7 @@ mod signing; #[cfg(feature = "verifying")] mod verifying; -pub use crate::recovery::RecoveryId; +pub use crate::{normalized::NormalizedSignature, recovery::RecoveryId}; // Re-export the `elliptic-curve` crate (and select types) pub use elliptic_curve::{self, sec1::EncodedPoint, PrimeCurve}; diff --git a/ecdsa/src/normalized.rs b/ecdsa/src/normalized.rs new file mode 100644 index 00000000..6a66a4b7 --- /dev/null +++ b/ecdsa/src/normalized.rs @@ -0,0 +1,11 @@ +//! Support for ECDSA signatures with low-S normalization. + +use crate::Signature; +use elliptic_curve::PrimeCurve; + +/// ECDSA signature with low-S normalization applied. +#[derive(Clone, Eq, PartialEq)] +#[repr(transparent)] +pub struct NormalizedSignature { + inner: Signature, +} diff --git a/ed25519/src/hex.rs b/ed25519/src/hex.rs index 41bc197a..3848afa3 100644 --- a/ed25519/src/hex.rs +++ b/ed25519/src/hex.rs @@ -1,9 +1,24 @@ //! Hexadecimal encoding support // TODO(tarcieri): use `base16ct`? -use crate::{Error, Signature}; +use crate::{ComponentBytes, Error, Signature}; use core::{fmt, str}; +/// Format a signature component as hex. +pub(crate) struct ComponentFormatter<'a>(pub(crate) &'a ComponentBytes); + +impl fmt::Debug for ComponentFormatter<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "\"")?; + + for byte in self.0 { + write!(f, "{:02x}", byte)?; + } + + write!(f, "\"") + } +} + impl fmt::LowerHex for Signature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for component in [&self.R, &self.s] { diff --git a/ed25519/src/lib.rs b/ed25519/src/lib.rs index dfb61f0a..2e8cb45b 100644 --- a/ed25519/src/lib.rs +++ b/ed25519/src/lib.rs @@ -208,7 +208,7 @@ //! let mut ed25519_seed = [0u8; 32]; //! OsRng.fill_bytes(&mut ed25519_seed); //! -//! let signing_key = SigningKey::from_seed(&ed25519_seed).unwrap(); +//! let signing_key = SigningKey::from_bytes(&ed25519_seed); //! let verifying_key = signing_key.verifying_key(); //! //! let signer = RingHelloSigner { signing_key }; @@ -406,8 +406,8 @@ impl TryFrom<&[u8]> for Signature { impl fmt::Debug for Signature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("ed25519::Signature") - .field("R", self.r_bytes()) - .field("s", self.s_bytes()) + .field("R", &hex::ComponentFormatter(self.r_bytes())) + .field("s", &hex::ComponentFormatter(self.s_bytes())) .finish() } }