Skip to content

Commit

Permalink
Transition remaining items to no-std
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby authored and redshiftzero committed Feb 13, 2024
1 parent 606d02d commit c849ea1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 76 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ repository = "https://github.com/penumbra-zone/decaf377-rdsa"
[dependencies]
# No Alloc, No Std
blake2b_simd = { version = "0.5", default-features = false }
cfg-if = "1.0"
decaf377 = { git = "https://github.com/penumbra-zone/decaf377", rev = "f8c185eb40d2ece19783ce02b99861e85d304e92", default-features = false }
decaf377 = { git = "https://github.com/penumbra-zone/decaf377", rev = "e26c88c896d9e3da4677bfe9ba127ce45979e0b2", default-features = false }
digest = { version = "0.9", default-features = false }
rand_core = { version = "0.6", default-features = false }
hex = { version = "0.4", default-features = false }
Expand All @@ -39,8 +38,7 @@ name = "bench"
harness = false

[features]
#default = ["serde", "std"]
default = []
default = ["serde", "std"]
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
23 changes: 9 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![doc = include_str!("../README.md")]
use cfg_if::cfg_if;

mod domain;
mod error;
mod hash;
use hash::HStar;
mod signature;

mod signing_key;
mod verification_key;

pub use domain::{Binding, Domain, SpendAuth};
pub use error::Error;
pub use signature::Signature;
pub use signing_key::SigningKey;
pub use verification_key::{VerificationKey, VerificationKeyBytes};

cfg_if! {
if #[cfg(feature = "std")] {
pub mod batch;

mod signing_key;
mod verification_key;

pub use signature::Signature;
pub use signing_key::SigningKey;
pub use verification_key::{VerificationKey, VerificationKeyBytes};
pub use decaf377::Fr;

pub use decaf377::Fr;
}
}
#[cfg(feature = "std")]
pub mod batch;
50 changes: 29 additions & 21 deletions src/signing_key.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::convert::{TryFrom, TryInto};

use decaf377::Fr;
use rand_core::{CryptoRng, RngCore};

use crate::{Binding, Domain, Error, Signature, SpendAuth, VerificationKey};
use crate::{Domain, Error, Signature, SpendAuth, VerificationKey};

/// A `decaf377-rdsa` signing key.
#[derive(Copy, Clone)]
Expand All @@ -16,22 +14,6 @@ pub struct SigningKey<D: Domain> {
pk: VerificationKey<D>,
}

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

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

impl<'a, D: Domain> From<&'a SigningKey<D>> for VerificationKey<D> {
fn from(sk: &'a SigningKey<D>) -> VerificationKey<D> {
sk.pk.clone()
Expand Down Expand Up @@ -63,8 +45,7 @@ impl<D: Domain> TryFrom<[u8; 32]> for SigningKey<D> {
type Error = Error;

fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
use ark_serialize::CanonicalDeserialize;
let sk = Fr::deserialize_compressed(&bytes[..]).map_err(|_| Error::MalformedSigningKey)?;
let sk = Fr::from_bytes_checked(&bytes).map_err(|_| Error::MalformedSigningKey)?;
Ok(Self::new_from_field(sk))
}
}
Expand Down Expand Up @@ -186,3 +167,30 @@ impl<D: Domain> SigningKey<D> {
Signature::from_parts(r_bytes, s_bytes)
}
}

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

use crate::Binding;

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

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

#[cfg(feature = "std")]
pub use std_only::*;
83 changes: 46 additions & 37 deletions src/verification_key.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::{
cmp::Ord,
convert::TryFrom,
use core::{
cmp::{self, Ord},
hash::{Hash, Hasher},
marker::PhantomData,
};

use decaf377::Fr;

use crate::{domain::Sealed, Binding, Domain, Error, Signature, SpendAuth};
use crate::{domain::Sealed, Domain, Error, Signature, SpendAuth};

/// A refinement type for `[u8; 32]` indicating that the bytes represent
/// an encoding of a `decaf377-rdsa` verification key.
Expand Down Expand Up @@ -51,13 +50,13 @@ impl<D: Domain> Hash for VerificationKeyBytes<D> {
}

impl<D: Domain> PartialOrd for VerificationKeyBytes<D> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.bytes.partial_cmp(&other.bytes)
}
}

impl<D: Domain> Ord for VerificationKeyBytes<D> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.bytes.cmp(&other.bytes)
}
}
Expand All @@ -84,13 +83,13 @@ impl<D: Domain> Hash for VerificationKey<D> {
}

impl<D: Domain> PartialOrd for VerificationKey<D> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.bytes.partial_cmp(&other.bytes)
}
}

impl<D: Domain> Ord for VerificationKey<D> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.bytes.cmp(&other.bytes)
}
}
Expand Down Expand Up @@ -212,7 +211,7 @@ impl<D: Domain> VerificationKey<D> {

/// Convenience method for identity checks.
pub fn is_identity(&self) -> bool {
self.point.is_identity()
self.point == decaf377::Element::IDENTITY
}

/// Verify a purported `signature` with a prehashed challenge.
Expand All @@ -232,57 +231,67 @@ impl<D: Domain> VerificationKey<D> {
let cA = self.point * c;
let check = sB - cA - R;

if check.is_identity() {
if check == decaf377::Element::IDENTITY {
Ok(())
} else {
Err(Error::InvalidSignature)
}
}
}

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

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

impl<D: Domain> std::cmp::Eq for VerificationKey<D> {}
impl<D: Domain> std::cmp::Eq for VerificationKeyBytes<D> {}
impl<D: Domain> Eq for VerificationKey<D> {}
impl<D: Domain> Eq for VerificationKeyBytes<D> {}

impl std::fmt::Debug for VerificationKey<Binding> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("VerificationKey<Binding>")
.field(&hex::encode(&<[u8; 32]>::from(*self)))
.finish()
#[cfg(feature = "std")]
mod std_only {
use super::*;

use crate::Binding;

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

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

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

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

#[cfg(feature = "std")]
pub use std_only::*;

0 comments on commit c849ea1

Please sign in to comment.