Skip to content

Commit

Permalink
Add no-std support via a default-enabled std feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Dec 16, 2024
1 parent a5f27e0 commit 1d60917
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 40 deletions.
26 changes: 16 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 24 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "katex-header.html"]

[dependencies]
aes = "0.8"
bitvec = "1"
blake2b_simd = "1"
ff = "0.13"
fpe = "0.6"
group = { version = "0.13", features = ["wnaf-memuse"] }
hex = "0.4"
bitvec = { version = "1", default-features = false }
blake2b_simd = { version = "1", default-features = false }
ff = { version = "0.13", default-features = false }
fpe = { version = "0.6", default-features = false, features = ["alloc"] }
group = "0.13"
hex = { version = "0.4", default-features = false, features = ["alloc"] }
lazy_static = "1"
memuse = { version = "0.2.1", features = ["nonempty"] }
memuse = { version = "0.2.2", default-features = false }
pasta_curves = "0.5"
proptest = { version = "1.0.0", optional = true }
rand = "0.8"
reddsa = "0.5"
nonempty = "0.7"
rand = { version = "0.8", default-features = false }
reddsa = { version = "0.5", default-features = false }
nonempty = { version = "0.10", default-features = false }
poseidon = { package = "halo2_poseidon", version = "0.1" }
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
sinsemilla = "0.1"
subtle = "2.3"
subtle = { version = "2.3", default-features = false }
zcash_note_encryption = "0.4"
incrementalmerkletree = "0.7"
incrementalmerkletree = { version = "0.7", default-features = false }
zcash_spec = "0.1"
zip32 = "0.1"
zip32 = { version = "0.1", default-features = false }
visibility = "0.1.1"

# Circuit
Expand All @@ -55,7 +55,10 @@ halo2_proofs = { version = "0.3", optional = true, default-features = false, fea
getset = "0.1"

# Logging
tracing = "0.1"
tracing = { version = "0.1", default-features = false }

# No-std support
core2 = { version = "0.3", default-features = false, features = ["alloc"] }

# Developer tooling dependencies
image = { version = "0.24", optional = true }
Expand All @@ -78,8 +81,9 @@ pprof = { version = "0.11", features = ["criterion", "flamegraph"] }
bench = false

[features]
default = ["circuit", "multicore"]
circuit = ["dep:halo2_gadgets", "dep:halo2_proofs"]
default = ["circuit", "multicore", "std"]
std = ["core2/std", "group/wnaf-memuse", "reddsa/std"]
circuit = ["dep:halo2_gadgets", "dep:halo2_proofs", "std"]
unstable-frost = []
multicore = ["halo2_proofs?/multicore"]
dev-graph = ["halo2_proofs?/dev-graph", "image", "plotters"]
Expand All @@ -102,3 +106,6 @@ debug = true

[profile.bench]
debug = true

[patch.crates-io]
nonempty = { git = "https://github.com/nuttycom/nonempty.git", rev = "090815ca02ff969d23c6331f9f5b1e4c7720160a" }
15 changes: 10 additions & 5 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Logic for building Orchard components of transactions.
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use core::fmt;
use core::iter;
use std::collections::BTreeMap;
use std::fmt::Display;

use ff::Field;
use pasta_curves::pallas;
Expand Down Expand Up @@ -141,7 +141,7 @@ pub enum BuildError {
BundleTypeNotSatisfiable,
}

impl Display for BuildError {
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use BuildError::*;
match self {
Expand All @@ -163,6 +163,7 @@ impl Display for BuildError {
}
}

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

#[cfg(feature = "circuit")]
Expand All @@ -189,7 +190,7 @@ pub enum SpendError {
FvkMismatch,
}

impl Display for SpendError {
impl fmt::Display for SpendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use SpendError::*;
f.write_str(match self {
Expand All @@ -200,18 +201,20 @@ impl Display for SpendError {
}
}

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

/// The only error that can occur here is if outputs are disabled for this builder.
#[derive(Debug, PartialEq, Eq)]
pub struct OutputError;

impl Display for OutputError {
impl fmt::Display for OutputError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Outputs are not enabled for this builder")
}
}

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

/// Information about a specific note to be spent in an [`Action`].
Expand Down Expand Up @@ -1137,7 +1140,9 @@ impl OutputView for OutputInfo {
#[cfg(any(test, feature = "test-dependencies"))]
#[cfg_attr(docsrs, doc(cfg(feature = "test-dependencies")))]
pub mod testing {
use alloc::vec::Vec;
use core::fmt::Debug;

use incrementalmerkletree::{frontier::Frontier, Hashable};
use rand::{rngs::StdRng, CryptoRng, SeedableRng};

Expand Down
13 changes: 10 additions & 3 deletions src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Structs related to bundles of Orchard actions.
use alloc::vec::Vec;

pub mod commitments;

#[cfg(feature = "circuit")]
Expand All @@ -10,10 +12,12 @@ pub use batch::BatchValidator;
use core::fmt;

use blake2b_simd::Hash as Blake2bHash;
use memuse::DynamicUsage;
use nonempty::NonEmpty;
use zcash_note_encryption::{try_note_decryption, try_output_recovery_with_ovk};

#[cfg(feature = "std")]
use memuse::DynamicUsage;

use crate::{
action::Action,
address::Address,
Expand Down Expand Up @@ -472,16 +476,17 @@ impl<V> Bundle<Authorized, V> {
}
}

#[cfg(feature = "std")]
impl<V: DynamicUsage> DynamicUsage for Bundle<Authorized, V> {
fn dynamic_usage(&self) -> usize {
self.actions.dynamic_usage()
self.actions.tail.dynamic_usage()
+ self.value_balance.dynamic_usage()
+ self.authorization.proof.dynamic_usage()
}

fn dynamic_usage_bounds(&self) -> (usize, Option<usize>) {
let bounds = (
self.actions.dynamic_usage_bounds(),
self.actions.tail.dynamic_usage_bounds(),
self.value_balance.dynamic_usage_bounds(),
self.authorization.proof.dynamic_usage_bounds(),
);
Expand Down Expand Up @@ -519,6 +524,8 @@ pub struct BundleAuthorizingCommitment(pub Blake2bHash);
#[cfg(any(test, feature = "test-dependencies"))]
#[cfg_attr(docsrs, doc(cfg(feature = "test-dependencies")))]
pub mod testing {
use alloc::vec::Vec;

use group::ff::FromUniformBytes;
use nonempty::NonEmpty;
use pasta_curves::pallas;
Expand Down
2 changes: 2 additions & 0 deletions src/bundle/batch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

use halo2_proofs::plonk;
use pasta_curves::vesta;
use rand::{CryptoRng, RngCore};
Expand Down
3 changes: 3 additions & 0 deletions src/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! The Orchard Action circuit implementation.
use alloc::vec::Vec;

use group::{Curve, GroupEncoding};
use halo2_proofs::{
circuit::{floor_planner, Layouter, Value},
Expand Down Expand Up @@ -919,6 +921,7 @@ impl Proof {

#[cfg(test)]
mod tests {
use alloc::vec::Vec;
use core::iter;

use ff::Field;
Expand Down
4 changes: 4 additions & 0 deletions src/constants/fixed_bases.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//! Orchard fixed bases.
#[cfg(feature = "circuit")]
use alloc::vec::Vec;

use super::{L_ORCHARD_SCALAR, L_VALUE};

#[cfg(feature = "circuit")]
Expand Down
6 changes: 4 additions & 2 deletions src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Key structures for Orchard.
use std::io::{self, Read, Write};
use alloc::vec::Vec;
use core2::io::{self, Read, Write};

use ::zip32::{AccountId, ChildIndex};
use aes::Aes256;
Expand Down Expand Up @@ -405,7 +406,7 @@ impl FullViewingKey {
Self::from_bytes(&data).ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"Unable to deserialize a valid Orchard FullViewingKey from bytes".to_owned(),
"Unable to deserialize a valid Orchard FullViewingKey from bytes",
)
})
}
Expand Down Expand Up @@ -681,6 +682,7 @@ impl IncomingViewingKey {
#[derive(Clone, Debug)]
pub struct PreparedIncomingViewingKey(PreparedNonZeroScalar);

#[cfg(feature = "std")]
impl memuse::DynamicUsage for PreparedIncomingViewingKey {
fn dynamic_usage(&self) -> usize {
self.0.dynamic_usage()
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! implicitly mean it is an Orchard payment address (as opposed to e.g. a Sapling payment
//! address, which is also shielded).
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
// Temporary until we have more of the crate implemented.
#![allow(dead_code)]
Expand All @@ -16,6 +17,14 @@
#![deny(missing_docs)]
#![deny(unsafe_code)]

Check warning on line 19 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

doc list item without indentation

warning: doc list item without indentation --> src/bundle/commitments.rs:19:5 | 19 | /// with ZCASH_ORCHARD_ACTIONS_COMPACT_HASH_PERSONALIZATION | ^^ | = help: if this is supposed to be its own paragraph, add a blank line = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_lazy_continuation = note: `#[warn(clippy::doc_lazy_continuation)]` on by default help: indent this line | 19 | /// with ZCASH_ORCHARD_ACTIONS_COMPACT_HASH_PERSONALIZATION | +
#[macro_use]
extern crate alloc;

Check warning on line 21 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

doc list item without indentation

warning: doc list item without indentation --> src/bundle/commitments.rs:21:5 | 21 | /// with ZCASH_ORCHARD_ACTIONS_MEMOS_HASH_PERSONALIZATION | ^^ | = help: if this is supposed to be its own paragraph, add a blank line = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_lazy_continuation help: indent this line | 21 | /// with ZCASH_ORCHARD_ACTIONS_MEMOS_HASH_PERSONALIZATION | +

#[cfg(feature = "std")]

Check warning on line 23 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

doc list item without indentation

warning: doc list item without indentation --> src/bundle/commitments.rs:23:5 | 23 | /// with ZCASH_ORCHARD_ACTIONS_NONCOMPACT_HASH_PERSONALIZATION | ^^ | = help: if this is supposed to be its own paragraph, add a blank line = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_lazy_continuation help: indent this line | 23 | /// with ZCASH_ORCHARD_ACTIONS_NONCOMPACT_HASH_PERSONALIZATION | +
extern crate std;

Check warning on line 24 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

doc list item without indentation

warning: doc list item without indentation --> src/bundle/commitments.rs:24:5 | 24 | /// as defined in [ZIP-244: Transaction Identifier Non-Malleability][zip244] | ^ | = help: if this is supposed to be its own paragraph, add a blank line = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_lazy_continuation help: indent this line | 24 | /// as defined in [ZIP-244: Transaction Identifier Non-Malleability][zip244] | +++

use alloc::vec::Vec;

mod action;
mod address;
pub mod builder;
Expand Down
1 change: 1 addition & 0 deletions src/note_encryption.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! In-band secret distribution for Orchard bundles.
use alloc::vec::Vec;
use core::fmt;

use blake2b_simd::{Hash, Params};
Expand Down
6 changes: 4 additions & 2 deletions src/pczt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! PCZT support for Orchard.
use std::collections::BTreeMap;
use std::fmt;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;

use getset::Getters;
use pasta_curves::pallas;
Expand Down
2 changes: 2 additions & 0 deletions src/pczt/io_finalizer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

use rand::{CryptoRng, RngCore};

use crate::{
Expand Down
4 changes: 3 additions & 1 deletion src/pczt/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::collections::BTreeMap;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;

use ff::PrimeField;
use incrementalmerkletree::Hashable;
Expand Down
2 changes: 2 additions & 0 deletions src/pczt/prover.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

use halo2_proofs::plonk;
use rand::{CryptoRng, RngCore};

Expand Down
3 changes: 3 additions & 0 deletions src/pczt/updater.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use alloc::string::String;
use alloc::vec::Vec;

use super::{Action, Bundle, Zip32Derivation};

impl Bundle {
Expand Down
Loading

0 comments on commit 1d60917

Please sign in to comment.