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 9ac6376 commit 67f5974
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 19 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

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

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

[dependencies]
aes = "0.8"
bitvec = "1"
blake2b_simd = "1"
bitvec = { version = "1", default-features = false }
blake2b_simd = { version = "1", default-features = false }
ff = "0.13"
fpe = "0.6"
group = { version = "0.13", features = ["wnaf-memuse"] }
hex = "0.4"
hex = { version = "0.4", default-features = false }
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"
Expand All @@ -57,6 +57,9 @@ getset = "0.1"
# Logging
tracing = "0.1"

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

# Developer tooling dependencies
image = { version = "0.24", optional = true }
plotters = { version = "0.3.0", 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", "memuse/nonempty"]
circuit = ["dep:halo2_gadgets", "dep:halo2_proofs", "std"]
unstable-frost = []
multicore = ["halo2_proofs?/multicore"]
dev-graph = ["halo2_proofs?/dev-graph", "image", "plotters"]
Expand Down
13 changes: 8 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
7 changes: 6 additions & 1 deletion 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,6 +476,7 @@ 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()
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
2 changes: 2 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
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
5 changes: 3 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
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
1 change: 1 addition & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Types related to Orchard note commitment trees and anchors.
use alloc::vec::Vec;
use core::iter;

use crate::{
Expand Down
1 change: 1 addition & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl fmt::Display for OverflowError {
}
}

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

/// The non-negative value of an individual Orchard note.
Expand Down

0 comments on commit 67f5974

Please sign in to comment.