Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-std support via a default-enabled std feature flag #446

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to Rust's notion of

## [Unreleased]

### Changed
- Migrated to `nonempty 0.11`.

## [0.10.1] - 2024-12-16

### Added
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pasta_curves = "0.5"
proptest = { version = "1.0.0", optional = true }
rand = { version = "0.8", default-features = false }
reddsa = { version = "0.5", default-features = false }
nonempty = "0.7"
nonempty = { version = "0.11", default-features = false }
poseidon = { package = "halo2_poseidon", version = "0.1" }
serde = { version = "1.0", default-features = false, features = ["derive"] }
sinsemilla = "0.1"
Expand Down Expand Up @@ -81,8 +81,9 @@ pprof = { version = "0.11", features = ["criterion", "flamegraph"] }
bench = false

[features]
default = ["circuit", "multicore"]
circuit = ["dep:halo2_gadgets", "dep:halo2_proofs", "core2/std", "group/wnaf-memuse", "reddsa/std"]
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 Down
3 changes: 3 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl fmt::Display for BuildError {
}
}

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

#[cfg(feature = "circuit")]
Expand Down Expand Up @@ -200,6 +201,7 @@ impl fmt::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.
Expand All @@ -212,6 +214,7 @@ impl fmt::Display for OutputError {
}
}

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

/// Information about a specific note to be spent in an [`Action`].
Expand Down
5 changes: 4 additions & 1 deletion src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,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 @@ -474,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.tail.dynamic_usage()
Expand Down
2 changes: 1 addition & 1 deletion src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ impl IncomingViewingKey {
#[derive(Clone, Debug)]
pub struct PreparedIncomingViewingKey(PreparedNonZeroScalar);

#[cfg(feature = "circuit")]
#[cfg(feature = "std")]
impl memuse::DynamicUsage for PreparedIncomingViewingKey {
fn dynamic_usage(&self) -> usize {
self.0.dynamic_usage()
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,80 @@
#![deny(missing_debug_implementations)]
#![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;
pub mod bundle;
#[cfg(feature = "circuit")]
pub mod circuit;
mod constants;
pub mod keys;
pub mod note;
pub mod note_encryption;
pub mod pczt;
pub mod primitives;
mod spec;
pub mod tree;
pub mod value;
pub mod zip32;

#[cfg(test)]
mod test_vectors;

pub use action::Action;
pub use address::Address;
pub use bundle::Bundle;
pub use constants::MERKLE_DEPTH_ORCHARD as NOTE_COMMITMENT_TREE_DEPTH;
pub use note::Note;
pub use tree::Anchor;

/// A proof of the validity of an Orchard [`Bundle`].
///
/// [`Bundle`]: crate::bundle::Bundle
#[derive(Clone)]
pub struct Proof(Vec<u8>);

impl core::fmt::Debug for Proof {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if f.alternate() {
f.debug_tuple("Proof").field(&self.0).finish()
} else {
// By default, only show the proof length, not its contents.
f.debug_tuple("Proof")
.field(&format_args!("{} bytes", self.0.len()))
.finish()
}
}
}

impl AsRef<[u8]> for Proof {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl memuse::DynamicUsage for Proof {
fn dynamic_usage(&self) -> usize {
self.0.dynamic_usage()
}

fn dynamic_usage_bounds(&self) -> (usize, Option<usize>) {
self.0.dynamic_usage_bounds()
}
}

impl Proof {
/// Constructs a new Proof value.
pub fn new(bytes: Vec<u8>) -> Self {
Proof(bytes)
}
}
6 changes: 3 additions & 3 deletions src/primitives/redpallas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::cmp::{Ord, Ordering, PartialOrd};
use pasta_curves::pallas;
use rand::{CryptoRng, RngCore};

#[cfg(feature = "circuit")]
#[cfg(feature = "std")]
pub use reddsa::batch;

#[cfg(test)]
Expand Down Expand Up @@ -127,7 +127,7 @@ impl VerificationKey<SpendAuth> {
}

/// Creates a batch validation item from a `SpendAuth` signature.
#[cfg(feature = "circuit")]
#[cfg(feature = "std")]
pub fn create_batch_item<M: AsRef<[u8]>>(
&self,
sig: Signature<SpendAuth>,
Expand All @@ -137,7 +137,7 @@ impl VerificationKey<SpendAuth> {
}
}

#[cfg(feature = "circuit")]
#[cfg(feature = "std")]
impl VerificationKey<Binding> {
/// Creates a batch validation item from a `Binding` signature.
pub fn create_batch_item<M: AsRef<[u8]>>(
Expand Down
4 changes: 2 additions & 2 deletions src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ff::{Field, FromUniformBytes, PrimeField, PrimeFieldBits};
use group::{Curve, Group, GroupEncoding, WnafBase, WnafScalar};
#[cfg(feature = "circuit")]
use halo2_gadgets::{poseidon::primitives as poseidon, sinsemilla::primitives as sinsemilla};
#[cfg(feature = "circuit")]
#[cfg(feature = "std")]
use memuse::DynamicUsage;
use pasta_curves::{
arithmetic::{CurveAffine, CurveExt},
Expand Down Expand Up @@ -157,7 +157,7 @@ impl PreparedNonIdentityBase {
#[derive(Clone, Debug)]
pub(crate) struct PreparedNonZeroScalar(WnafScalar<pallas::Scalar, PREPARED_WINDOW_SIZE>);

#[cfg(feature = "circuit")]
#[cfg(feature = "std")]
impl DynamicUsage for PreparedNonZeroScalar {
fn dynamic_usage(&self) -> usize {
self.0.dynamic_usage()
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
Loading