Skip to content

Commit

Permalink
Add no-std support (#11)
Browse files Browse the repository at this point in the history
* Add no_std support

* Update CHANGELOG

* Remove unused alloc feature flag

* Remove a forgotten comment

Co-authored-by: str4d <[email protected]>

* Make zeroize dependency optional

* Add alloc feature flag

* Clean the code by outer attributes

* use 2021 edition

Co-authored-by: str4d <[email protected]>
Co-authored-by: Conrado Gouvea <[email protected]>
  • Loading branch information
3 people authored May 6, 2022
1 parent 0e912de commit ed11f44
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 36 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Entries are listed in reverse chronological order.
## Unreleased

* Migrate to `group` 0.12, `jubjub` 0.9, `pasta_curves` 0.4
* Added support for `no-std` builds, via new (default-enabled) `std` and `alloc`
feature flags. Module `batch` is supported on `alloc` feature only. Module
`frost` is supported on `std` feature only.

## 0.2.0

Expand Down
27 changes: 17 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reddsa"
edition = "2018"
edition = "2021"
# When releasing to crates.io:
# - Update html_root_url
# - Update CHANGELOG.md
Expand All @@ -23,15 +23,19 @@ description = "A standalone implementation of the RedDSA signature scheme."
features = ["nightly"]

[dependencies]
blake2b_simd = "1"
byteorder = "1.4"
group = "0.12"
jubjub = "0.9"
pasta_curves = "0.4"
rand_core = "0.6"
blake2b_simd = { version = "1", default-features = false }
byteorder = { version = "1.4", default-features = false }
group = { version = "0.12", default-features = false }
jubjub = { version = "0.9", default-features = false }
pasta_curves = { version = "0.4", default-features = false, features = ["alloc"] }
rand_core = { version = "0.6", default-features = false }
serde = { version = "1", optional = true, features = ["derive"] }
thiserror = "1.0"
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"] }
thiserror = { version = "1.0", optional = true }

[dependencies.zeroize]
version = "1"
features = ["zeroize_derive"]
optional = true

[dev-dependencies]
bincode = "1"
Expand All @@ -44,8 +48,11 @@ rand_chacha = "0.3"
serde_json = "1.0"

[features]
std = ["blake2b_simd/std", "thiserror", "zeroize", "alloc",
"serde"] # conditional compilation for serde not complete (issue #9)
alloc = []
nightly = []
default = ["serde"]
default = ["std"]

[[bench]]
name = "bench"
Expand Down
5 changes: 3 additions & 2 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
//! and loss of the ability to easily pinpoint failing signatures.
//!
use std::convert::TryFrom;
use alloc::vec::Vec;
use core::convert::TryFrom;

use group::{
cofactor::CofactorGroup,
Expand Down Expand Up @@ -246,7 +247,7 @@ impl<S: SpendAuth, B: Binding<Scalar = S::Scalar, Point = S::Point>> Verifier<S,
VKs.push(VK);
}

use std::iter::once;
use core::iter::once;

let scalars = once(&P_spendauth_coeff)
.chain(once(&P_binding_coeff))
Expand Down
20 changes: 15 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@
// - Deirdre Connolly <[email protected]>
// - Henry de Valence <[email protected]>

use thiserror::Error;
use core::fmt;

/// An error related to RedDSA 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,
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MalformedSigningKey => write!(f, "Malformed signing key encoding."),
Self::MalformedVerificationKey => write!(f, "Malformed verification key encoding."),
Self::InvalidSignature => write!(f, "Invalid signature."),
}
}
}
1 change: 1 addition & 0 deletions src/frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//! Internally, keygen_with_dealer generates keys using Verifiable Secret
//! Sharing, where shares are generated using Shamir Secret Sharing.
use alloc::vec::Vec;
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
Expand Down
2 changes: 1 addition & 1 deletion src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// - Deirdre Connolly <[email protected]>
// - Henry de Valence <[email protected]>

use std::marker::PhantomData;
use core::marker::PhantomData;

use blake2b_simd::{Params, State};

Expand Down
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,30 @@
// - Deirdre Connolly <[email protected]>
// - Henry de Valence <[email protected]>

#![no_std]
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]

//! Docs require the `nightly` feature until RFC 1990 lands.
#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "alloc")]
pub mod batch;
mod constants;
mod error;
#[cfg(feature = "std")]
pub mod frost;
mod hash;
#[cfg(feature = "std")]
mod messages;
pub mod orchard;
pub mod sapling;
#[cfg(feature = "alloc")]
mod scalar_mul;
pub(crate) mod signature;
mod signing_key;
Expand Down Expand Up @@ -74,12 +85,18 @@ pub(crate) mod private {
}

pub trait Sealed<T: SigType>:
Copy + Clone + Default + Eq + PartialEq + std::fmt::Debug
Copy + Clone + Default + Eq + PartialEq + core::fmt::Debug
{
const H_STAR_PERSONALIZATION: &'static [u8; 16];
type Scalar: group::ff::PrimeField + SealedScalar;

// `Point: VartimeMultiscalarMul` is conditioned by `alloc` feature flag
// This is fine because `Sealed` is an internal trait.
#[cfg(feature = "alloc")]
type Point: group::cofactor::CofactorCurve<Scalar = Self::Scalar>
+ scalar_mul::VartimeMultiscalarMul<Scalar = Self::Scalar, Point = Self::Point>;
#[cfg(not(feature = "alloc"))]
type Point: group::cofactor::CofactorCurve<Scalar = Self::Scalar>;

fn basepoint() -> T::Point;
}
Expand Down
1 change: 1 addition & 0 deletions src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{frost, signature, verification_key, SpendAuth};
use group::GroupEncoding;
use serde::{Deserialize, Serialize};

use alloc::vec::Vec;
use std::{collections::BTreeMap, convert::TryInto};

#[cfg(test)]
Expand Down
23 changes: 15 additions & 8 deletions src/orchard.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
//! Signature types for the Orchard protocol.
use std::borrow::Borrow;

use group::{ff::PrimeField, Group, GroupEncoding};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "alloc")]
use core::borrow::Borrow;

use group::GroupEncoding;
#[cfg(feature = "alloc")]
use group::{ff::PrimeField, Group};
use pasta_curves::pallas;

use crate::{
private,
scalar_mul::{LookupTable5, NonAdjacentForm, VartimeMultiscalarMul},
SigType,
};
use crate::{private, SigType};

#[cfg(feature = "alloc")]
use crate::scalar_mul::{LookupTable5, NonAdjacentForm, VartimeMultiscalarMul};

/// The byte-encoding of the basepoint for `OrchardSpendAuthSig`.
const ORCHARD_SPENDAUTHSIG_BASEPOINT_BYTES: [u8; 32] = [
Expand Down Expand Up @@ -74,6 +78,7 @@ impl private::Sealed<Binding> for Binding {
}
}

#[cfg(feature = "alloc")]
impl NonAdjacentForm for pallas::Scalar {
/// Compute a width-\\(w\\) "Non-Adjacent Form" of this scalar.
///
Expand Down Expand Up @@ -136,6 +141,7 @@ impl NonAdjacentForm for pallas::Scalar {
}
}

#[cfg(feature = "alloc")]
impl<'a> From<&'a pallas::Point> for LookupTable5<pallas::Point> {
#[allow(non_snake_case)]
fn from(A: &'a pallas::Point) -> Self {
Expand All @@ -149,6 +155,7 @@ impl<'a> From<&'a pallas::Point> for LookupTable5<pallas::Point> {
}
}

#[cfg(feature = "alloc")]
impl VartimeMultiscalarMul for pallas::Point {
type Scalar = pallas::Scalar;
type Point = pallas::Point;
Expand Down
3 changes: 2 additions & 1 deletion src/scalar_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
// - Henry de Valence <[email protected]>
// - Deirdre Connolly <[email protected]>

use std::{borrow::Borrow, fmt::Debug};
use alloc::vec::Vec;
use core::{borrow::Borrow, fmt::Debug};

use jubjub::{ExtendedNielsPoint, ExtendedPoint};

Expand Down
2 changes: 1 addition & 1 deletion src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// - Henry de Valence <[email protected]>

//! RedDSA Signatures
use std::marker::PhantomData;
use core::marker::PhantomData;

use crate::SigType;

Expand Down
2 changes: 1 addition & 1 deletion src/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// - Deirdre Connolly <[email protected]>
// - Henry de Valence <[email protected]>

use std::{
use core::{
convert::{TryFrom, TryInto},
marker::PhantomData,
};
Expand Down
2 changes: 1 addition & 1 deletion src/verification_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// - Deirdre Connolly <[email protected]>
// - Henry de Valence <[email protected]>

use std::{
use core::{
convert::{TryFrom, TryInto},
hash::{Hash, Hasher},
marker::PhantomData,
Expand Down
2 changes: 2 additions & 0 deletions tests/batch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "alloc")]

use rand::thread_rng;

use reddsa::*;
Expand Down
4 changes: 2 additions & 2 deletions tests/bincode.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::convert::TryFrom;
#![cfg(feature = "std")]

use proptest::prelude::*;

use reddsa::*;
use std::convert::TryFrom;

proptest! {
#[test]
Expand Down
2 changes: 2 additions & 0 deletions tests/frost.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(all(feature = "std", feature = "serde"))]

use rand::thread_rng;
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion tests/librustzcash_vectors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::convert::TryFrom;
use core::convert::TryFrom;

#[macro_use]
extern crate lazy_static;
Expand Down
2 changes: 1 addition & 1 deletion tests/proptests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::convert::TryFrom;
use core::convert::TryFrom;

use proptest::prelude::*;
use rand_core::{CryptoRng, RngCore};
Expand Down
2 changes: 1 addition & 1 deletion tests/smallorder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::convert::TryFrom;
use core::convert::TryFrom;

use jubjub::{AffinePoint, Fq};

Expand Down

0 comments on commit ed11f44

Please sign in to comment.