From ffc1ffe3c3b9633d071fc82d507567cc8297d1ce Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Tue, 3 Jun 2025 18:57:22 +0000 Subject: [PATCH] ed448: make ComponentBytes an array (#983) --- ed448/src/hex.rs | 4 ++-- ed448/src/lib.rs | 32 +++++++++++++++++--------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ed448/src/hex.rs b/ed448/src/hex.rs index fba9777..840ff4f 100644 --- a/ed448/src/hex.rs +++ b/ed448/src/hex.rs @@ -4,7 +4,7 @@ use core::{fmt, str}; impl fmt::LowerHex for Signature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - for component in [&self.R.0, &self.s.0] { + for component in [&self.R, &self.s] { for byte in component { write!(f, "{:02x}", byte)?; } @@ -15,7 +15,7 @@ impl fmt::LowerHex for Signature { impl fmt::UpperHex for Signature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - for component in [&self.R.0, &self.s.0] { + for component in [&self.R, &self.s] { for byte in component { write!(f, "{:02X}", byte)?; } diff --git a/ed448/src/lib.rs b/ed448/src/lib.rs index eda6931..b456fab 100644 --- a/ed448/src/lib.rs +++ b/ed448/src/lib.rs @@ -82,18 +82,11 @@ pub use signature::{self, Error, SignatureEncoding}; use core::fmt; /// Size of a single component of an Ed448 signature. -const COMPONENT_SIZE: usize = 57; +pub const COMPONENT_SIZE: usize = 57; /// Size of an `R` or `s` component of an Ed448 signature when serialized /// as bytes. -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct ComponentBytes([u8; COMPONENT_SIZE]); - -impl Default for ComponentBytes { - fn default() -> Self { - ComponentBytes([0; COMPONENT_SIZE]) - } -} +pub type ComponentBytes = [u8; COMPONENT_SIZE]; /// Ed448 signature serialized as a byte array. pub type SignatureBytes = [u8; Signature::BYTE_SIZE]; @@ -119,12 +112,12 @@ impl Signature { /// Parse an Ed448 signature from a byte slice. pub fn from_bytes(bytes: &SignatureBytes) -> Self { - let mut R = ComponentBytes::default(); - let mut s = ComponentBytes::default(); + let mut R = [0; COMPONENT_SIZE]; + let mut s = [0; COMPONENT_SIZE]; let components = bytes.split_at(COMPONENT_SIZE); - R.0.copy_from_slice(components.0); - s.0.copy_from_slice(components.1); + R.copy_from_slice(components.0); + s.copy_from_slice(components.1); Self { R, s } } @@ -154,10 +147,19 @@ impl Signature { pub fn to_bytes(&self) -> SignatureBytes { let mut ret = [0u8; Self::BYTE_SIZE]; let (R, s) = ret.split_at_mut(COMPONENT_SIZE); - R.copy_from_slice(&self.R.0); - s.copy_from_slice(&self.s.0); + R.copy_from_slice(&self.R); + s.copy_from_slice(&self.s); ret } + + /// Create a [`Signature`] from the serialized `r` and `s` component values + /// which comprise the signature. + pub fn from_components(r: impl Into, s: impl Into) -> Self { + Self { + R: r.into(), + s: s.into(), + } + } } impl From for SignatureBytes {