From 159966b80e2d956907f853bb24513ff5bef4087d Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 16 Dec 2024 21:42:18 +0000 Subject: [PATCH] Add no-std support via a default-enabled `std` feature flag --- Cargo.lock | 5 ++--- Cargo.toml | 10 +++++++--- src/builder.rs | 3 +++ src/bundle.rs | 5 ++++- src/keys.rs | 2 +- src/lib.rs | 1 + src/primitives/redpallas.rs | 6 +++--- src/spec.rs | 4 ++-- src/value.rs | 1 + 9 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 90b75e1f6..1a2fb5b8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1351,9 +1351,8 @@ dependencies = [ [[package]] name = "nonempty" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9e591e719385e6ebaeb5ce5d3887f7d5676fceca6411d1925ccc95745f3d6f7" +version = "0.10.0" +source = "git+https://github.com/nuttycom/nonempty.git?rev=38d37189faecb2a0e3d6adc05aa24e1b93c2483b#38d37189faecb2a0e3d6adc05aa24e1b93c2483b" [[package]] name = "num-bigint" diff --git a/Cargo.toml b/Cargo.toml index c9be0848d..8ef4bfcce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.10", default-features = false } poseidon = { package = "halo2_poseidon", version = "0.1" } serde = { version = "1.0", default-features = false, features = ["derive"] } sinsemilla = "0.1" @@ -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"] @@ -105,3 +106,6 @@ debug = true [profile.bench] debug = true + +[patch.crates-io] +nonempty = { git = "https://github.com/nuttycom/nonempty.git", rev = "38d37189faecb2a0e3d6adc05aa24e1b93c2483b" } diff --git a/src/builder.rs b/src/builder.rs index b370fb3c2..f15269235 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -163,6 +163,7 @@ impl fmt::Display for BuildError { } } +#[cfg(feature = "std")] impl std::error::Error for BuildError {} #[cfg(feature = "circuit")] @@ -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. @@ -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`]. diff --git a/src/bundle.rs b/src/bundle.rs index ed81045ba..4e0c20640 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -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, @@ -474,6 +476,7 @@ impl Bundle { } } +#[cfg(feature = "std")] impl DynamicUsage for Bundle { fn dynamic_usage(&self) -> usize { self.actions.tail.dynamic_usage() diff --git a/src/keys.rs b/src/keys.rs index c032a15ad..ab96e9f84 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -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() diff --git a/src/lib.rs b/src/lib.rs index 11db7790b..3a52af5c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ #[macro_use] extern crate alloc; +#[cfg(feature = "std")] extern crate std; use alloc::vec::Vec; diff --git a/src/primitives/redpallas.rs b/src/primitives/redpallas.rs index 0dc7c08d2..cbef23436 100644 --- a/src/primitives/redpallas.rs +++ b/src/primitives/redpallas.rs @@ -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)] @@ -127,7 +127,7 @@ impl VerificationKey { } /// Creates a batch validation item from a `SpendAuth` signature. - #[cfg(feature = "circuit")] + #[cfg(feature = "std")] pub fn create_batch_item>( &self, sig: Signature, @@ -137,7 +137,7 @@ impl VerificationKey { } } -#[cfg(feature = "circuit")] +#[cfg(feature = "std")] impl VerificationKey { /// Creates a batch validation item from a `Binding` signature. pub fn create_batch_item>( diff --git a/src/spec.rs b/src/spec.rs index b9702eead..f1c3e4459 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -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}, @@ -157,7 +157,7 @@ impl PreparedNonIdentityBase { #[derive(Clone, Debug)] pub(crate) struct PreparedNonZeroScalar(WnafScalar); -#[cfg(feature = "circuit")] +#[cfg(feature = "std")] impl DynamicUsage for PreparedNonZeroScalar { fn dynamic_usage(&self) -> usize { self.0.dynamic_usage() diff --git a/src/value.rs b/src/value.rs index 058feb103..dac380ece 100644 --- a/src/value.rs +++ b/src/value.rs @@ -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.