ed448: make ComponentBytes an array (#983)

This commit is contained in:
Arthur Gautier
2025-06-03 18:57:22 +00:00
committed by GitHub
parent 2798f7999b
commit ffc1ffe3c3
2 changed files with 19 additions and 17 deletions
+2 -2
View File
@@ -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)?;
}
+17 -15
View File
@@ -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<ComponentBytes>, s: impl Into<ComponentBytes>) -> Self {
Self {
R: r.into(),
s: s.into(),
}
}
}
impl From<Signature> for SignatureBytes {