Skip to content

Commit

Permalink
ed25519: hexify Debug impl on Signature (#747)
Browse files Browse the repository at this point in the history
Formats the `R` and `s` signature components as hexidecimal to make the
representation more compact, as requested in #723:

ed25519::Signature {
    R: 0x3f3e3d3c3b3a393837363534333231302f2e2d2c2b2a29282726252423222120,
    s: 0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100,
}
  • Loading branch information
tarcieri committed Oct 15, 2023
1 parent 8f37b60 commit e8d4d23
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ecdsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#[cfg(feature = "alloc")]
extern crate alloc;

mod normalized;
mod recovery;

#[cfg(feature = "der")]
Expand All @@ -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};
Expand Down
11 changes: 11 additions & 0 deletions ecdsa/src/normalized.rs
Original file line number Diff line number Diff line change
@@ -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<C: PrimeCurve> {
inner: Signature<C>,
}
17 changes: 16 additions & 1 deletion ed25519/src/hex.rs
Original file line number Diff line number Diff line change
@@ -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, "0x")?;

for byte in self.0 {
write!(f, "{:02x}", byte)?;
}

Ok(())
}
}

impl fmt::LowerHex for Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for component in [&self.R, &self.s] {
Expand Down
6 changes: 3 additions & 3 deletions ed25519/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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()
}
}
Expand Down
1 change: 1 addition & 0 deletions ed25519/tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const EXAMPLE_SIGNATURE: SignatureBytes = hex!(
#[test]
fn test_serialize() {
let signature = Signature::try_from(&EXAMPLE_SIGNATURE[..]).unwrap();
dbg!(&signature);
let encoded_signature: Vec<u8> = bincode::serialize(&signature).unwrap();
assert_eq!(&EXAMPLE_SIGNATURE[..], &encoded_signature[..]);
}
Expand Down

0 comments on commit e8d4d23

Please sign in to comment.