Skip to content

Commit

Permalink
Make signature no-std
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Feb 12, 2024
1 parent 9a13e15 commit def7e8a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 45 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
27 changes: 21 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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 {}
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
}
78 changes: 44 additions & 34 deletions src/signature.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -45,22 +44,6 @@ impl<D: Domain> Signature<D> {
}
}

impl std::fmt::Debug for Signature<Binding> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Signature<Binding>")
.field(&hex::encode(&<[u8; 64]>::from(*self)))
.finish()
}
}

impl std::fmt::Debug for Signature<SpendAuth> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Signature<SpendAuth>")
.field(&hex::encode(&<[u8; 64]>::from(*self)))
.finish()
}
}

impl<D: Domain> From<[u8; 64]> for Signature<D> {
fn from(bytes: [u8; 64]) -> Signature<D> {
Signature {
Expand All @@ -76,12 +59,6 @@ impl<D: Domain> From<Signature<D>> for [u8; 64] {
}
}

impl<D: Domain> From<Signature<D>> for Vec<u8> {
fn from(sig: Signature<D>) -> Vec<u8> {
sig.to_bytes().into()
}
}

impl<D: Domain> TryFrom<&[u8]> for Signature<D> {
type Error = Error;

Expand All @@ -99,18 +76,51 @@ impl<D: Domain> TryFrom<&[u8]> for Signature<D> {
}
}

impl<D: Domain> TryFrom<Vec<u8>> for Signature<D> {
type Error = Error;

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
value.as_slice().try_into()
impl<D: Domain> cmp::PartialEq for Signature<D> {
fn eq(&self, other: &Self) -> bool {
self.bytes == other.bytes
}
}

impl<D: Domain> std::cmp::PartialEq for Signature<D> {
fn eq(&self, other: &Self) -> bool {
self.bytes == other.bytes
impl<D: Domain> cmp::Eq for Signature<D> {}

#[cfg(feature = "std")]
mod std_only {
use super::*;
use std::fmt;

use crate::{Binding, Signature, SpendAuth};

impl<D: Domain> TryFrom<Vec<u8>> for Signature<D> {
type Error = Error;

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
value.as_slice().try_into()
}
}

impl<D: Domain> From<Signature<D>> for Vec<u8> {
fn from(sig: Signature<D>) -> Vec<u8> {
sig.to_bytes().into()
}
}

impl fmt::Debug for Signature<Binding> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Signature<Binding>")
.field(&hex::encode(&<[u8; 64]>::from(*self)))
.finish()
}
}

impl fmt::Debug for Signature<SpendAuth> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Signature<SpendAuth>")
.field(&hex::encode(&<[u8; 64]>::from(*self)))
.finish()
}
}
}

impl<D: Domain> std::cmp::Eq for Signature<D> {}
#[cfg(feature = "std")]
pub use std_only::*;

0 comments on commit def7e8a

Please sign in to comment.