schnorr48 0.1.0

48-byte Schnorr signatures over Ed25519 (truncated challenge for bandwidth efficiency)
Documentation
//! Cryptographic key newtypes for type-safe key handling.

use core::fmt;
use zeroize::{Zeroize, ZeroizeOnDrop};

/// 32-byte seed for keypair derivation.
///
/// This is the root secret used to derive the keypair via [`crate::derive_keypair`].
/// Key material is zeroized on drop.
#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
pub struct Seed([u8; 32]);

impl Seed {
    /// Create a seed from raw bytes.
    #[inline]
    pub const fn new(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// Get the raw bytes.
    #[inline]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    /// Consume and return the inner bytes.
    #[inline]
    pub fn into_bytes(self) -> [u8; 32] {
        let bytes = self.0;
        core::mem::forget(self);
        bytes
    }
}

impl From<[u8; 32]> for Seed {
    #[inline]
    fn from(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }
}

impl AsRef<[u8; 32]> for Seed {
    #[inline]
    fn as_ref(&self) -> &[u8; 32] {
        &self.0
    }
}

impl fmt::Debug for Seed {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Seed([REDACTED])")
    }
}

/// 32-byte Ed25519 private key (clamped scalar).
///
/// Derived from a [`Seed`] via [`crate::derive_keypair`].
/// Key material is zeroized on drop.
#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
pub struct PrivateKey([u8; 32]);

impl PrivateKey {
    /// Create a private key from raw bytes.
    ///
    /// The caller must ensure the bytes are a properly clamped Ed25519 scalar.
    /// Prefer using [`crate::derive_keypair`] to obtain a valid private key.
    #[inline]
    pub const fn new(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// Get the raw bytes.
    #[inline]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    /// Consume and return the inner bytes.
    #[inline]
    pub fn into_bytes(self) -> [u8; 32] {
        let bytes = self.0;
        core::mem::forget(self);
        bytes
    }
}

impl AsRef<[u8; 32]> for PrivateKey {
    #[inline]
    fn as_ref(&self) -> &[u8; 32] {
        &self.0
    }
}

impl fmt::Debug for PrivateKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "PrivateKey([REDACTED])")
    }
}

/// 32-byte Ed25519 public key (compressed point).
///
/// Used for signature verification. Can be freely shared.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct PublicKey([u8; 32]);

impl PublicKey {
    /// Create a public key from raw bytes.
    #[inline]
    pub const fn new(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// Get the raw bytes.
    #[inline]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    /// Consume and return the inner bytes.
    #[inline]
    pub const fn into_bytes(self) -> [u8; 32] {
        self.0
    }
}

impl From<[u8; 32]> for PublicKey {
    #[inline]
    fn from(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }
}

impl AsRef<[u8; 32]> for PublicKey {
    #[inline]
    fn as_ref(&self) -> &[u8; 32] {
        &self.0
    }
}

impl fmt::Debug for PublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "PublicKey({:02x}{:02x}{:02x}{:02x}...)",
            self.0[0], self.0[1], self.0[2], self.0[3]
        )
    }
}

impl fmt::Display for PublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in &self.0 {
            write!(f, "{:02x}", byte)?;
        }
        Ok(())
    }
}