diff --git a/Cargo.toml b/Cargo.toml index 2f37e12..f88d422 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,8 @@ name = "bench" harness = false [features] -default = ["serde", "std"] +#default = ["serde", "std"] +default = [] alloc = ["ark-ff", "ark-serialize"] std = ["alloc", "ark-ff/std", "blake2b_simd/std", "decaf377/arkworks", "digest/std", "hex/std", "rand_core/std", "thiserror"] parallel = ["ark-ff/parallel", "decaf377/parallel"] diff --git a/src/error.rs b/src/error.rs index df03a48..8b81f3d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,18 +1,33 @@ -use thiserror::Error; +use core::fmt; /// An error related to `decaf377-rdsa` signatures. -#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum Error { /// The encoding of a signing key was malformed. - #[error("Malformed signing key encoding.")] MalformedSigningKey, /// The encoding of a verification key was malformed. - #[error("Malformed verification key encoding.")] MalformedVerificationKey, /// Signature verification failed. - #[error("Invalid signature.")] InvalidSignature, /// Occurs when reading from a slice of the wrong length. - #[error("Wrong slice length, expected {expected}, found {found}")] WrongSliceLength { expected: usize, found: usize }, } + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::MalformedSigningKey => f.write_str("Malformed signing key encoding."), + Self::MalformedVerificationKey => f.write_str("Malformed verification key encoding."), + Self::InvalidSignature => f.write_str("Invalid signature."), + Self::WrongSliceLength { expected, found } => { + f.write_str("Wrong slice length, expected ")?; + expected.fmt(f)?; + f.write_str(", found ")?; + found.fmt(f) + } + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for Error {} diff --git a/src/lib.rs b/src/lib.rs index 9742144..bdee263 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,26 +3,25 @@ use cfg_if::cfg_if; mod domain; +mod error; mod hash; use hash::HStar; +mod signature; pub use domain::{Binding, Domain, SpendAuth}; +pub use error::Error; cfg_if! { if #[cfg(feature = "std")] { pub mod batch; - mod error; - mod signature; mod signing_key; mod verification_key; - pub use error::Error; pub use signature::Signature; pub use signing_key::SigningKey; pub use verification_key::{VerificationKey, VerificationKeyBytes}; pub use decaf377::Fr; - } else { } } diff --git a/src/signature.rs b/src/signature.rs index ce27f8a..1cae3fc 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -1,7 +1,6 @@ -use std::convert::TryFrom; -use std::marker::PhantomData; +use core::{cmp, convert::TryFrom, marker::PhantomData}; -use crate::{Binding, Domain, Error, SpendAuth}; +use crate::{Domain, Error}; /// A `decaf377-rdsa` signature. #[derive(Copy, Clone)] @@ -45,22 +44,6 @@ impl Signature { } } -impl std::fmt::Debug for Signature { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("Signature") - .field(&hex::encode(&<[u8; 64]>::from(*self))) - .finish() - } -} - -impl std::fmt::Debug for Signature { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("Signature") - .field(&hex::encode(&<[u8; 64]>::from(*self))) - .finish() - } -} - impl From<[u8; 64]> for Signature { fn from(bytes: [u8; 64]) -> Signature { Signature { @@ -76,12 +59,6 @@ impl From> for [u8; 64] { } } -impl From> for Vec { - fn from(sig: Signature) -> Vec { - sig.to_bytes().into() - } -} - impl TryFrom<&[u8]> for Signature { type Error = Error; @@ -99,18 +76,51 @@ impl TryFrom<&[u8]> for Signature { } } -impl TryFrom> for Signature { - type Error = Error; - - fn try_from(value: Vec) -> Result { - value.as_slice().try_into() +impl cmp::PartialEq for Signature { + fn eq(&self, other: &Self) -> bool { + self.bytes == other.bytes } } -impl std::cmp::PartialEq for Signature { - fn eq(&self, other: &Self) -> bool { - self.bytes == other.bytes +impl cmp::Eq for Signature {} + +#[cfg(feature = "std")] +mod std_only { + use super::*; + use std::fmt; + + use crate::{Binding, Signature, SpendAuth}; + + impl TryFrom> for Signature { + type Error = Error; + + fn try_from(value: Vec) -> Result { + value.as_slice().try_into() + } + } + + impl From> for Vec { + fn from(sig: Signature) -> Vec { + sig.to_bytes().into() + } + } + + impl fmt::Debug for Signature { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Signature") + .field(&hex::encode(&<[u8; 64]>::from(*self))) + .finish() + } + } + + impl fmt::Debug for Signature { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Signature") + .field(&hex::encode(&<[u8; 64]>::from(*self))) + .finish() + } } } -impl std::cmp::Eq for Signature {} +#[cfg(feature = "std")] +pub use std_only::*;