Skip to content

Commit

Permalink
Implement no_std support via a default-enabled std feature flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Dec 18, 2024
1 parent 88b6441 commit c80ff2e
Show file tree
Hide file tree
Showing 26 changed files with 218 additions and 131 deletions.
38 changes: 15 additions & 23 deletions Cargo.lock

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

66 changes: 44 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,50 @@ features = ["test-dependencies"]
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
ff = "0.13"
group = { version = "0.13", features = ["wnaf-memuse"] }
ff = { version = "0.13", default-features = false }
group = "0.13"

bls12_381 = "0.8"
jubjub = "0.10"
redjubjub = "0.7"
bls12_381 = { version = "0.8", default-features = false, features = ["alloc"] }
jubjub = { version = "0.10", default-features = false, features = ["alloc"] }
redjubjub = { version = "0.7", default-features = false }
zcash_spec = "0.1"

# Boilerplate
getset = "0.1"

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

# Circuits
bellman = { version = "0.14", default-features = false, features = ["groth16"] }
bellman = { version = "0.14", features = ["groth16"], optional = true }

# CSPRNG
rand = "0.8"
rand_core = "0.6"
rand = { version = "0.8", default-features = false }
rand_core = { version = "0.6", default-features = false }

# Digests
blake2b_simd = "1"
blake2s_simd = "1"
blake2b_simd = { version = "1", default-features = false }
blake2s_simd = { version = "1", default-features = false }

# Documentation
document-features = "0.2"
document-features = { version = "0.2", optional = true }

# Encodings
byteorder = "1"
hex = "0.4"
hex = { version = "0.4", default-features = false, features = ["alloc"] }

# Logging and metrics
memuse = "0.2.1"
tracing = "0.1"
memuse = { version = "0.2.2", default-features = false }
tracing = { version = "0.1", default-features = false }

# Note Commitment Trees
bitvec = "1"
incrementalmerkletree = { version = "0.7", features = ["legacy-api"] }
bitvec = { version = "1", default-features = false }
incrementalmerkletree = { version = "0.7", default-features = false, features = ["legacy-api"] }

# Note encryption
zcash_note_encryption = { version = "0.4", features = ["pre-zip-212"] }

# Secret management
subtle = "2.2.3"
subtle = { version = "2.2.3", default-features = false }

# Static constants
lazy_static = "1"
Expand All @@ -69,8 +71,9 @@ proptest = { version = "1", optional = true }

# ZIP 32
aes = "0.8"
fpe = "0.6"
zip32 = "0.1"
fpe = { version = "0.6", default-features = false, features = ["alloc"] }
zip32 = { version = "0.1.1", default-features = false }


[dev-dependencies]
chacha20poly1305 = "0.10"
Expand All @@ -83,10 +86,26 @@ rand_xorshift = "0.3"
pprof = { version = "0.11", features = ["criterion", "flamegraph"] } # MSRV 1.56

[features]
default = ["multicore"]
default = ["multicore", "std"]
std = [
"core2/std",
"document-features",
"group/wnaf-memuse",
"redjubjub/std",
"circuit",
]

## Enables creation of Sapling proofs
circuit = [
"bellman",
"bls12_381/bits",
"bls12_381/groups",
"bls12_381/pairings",
"jubjub/bits",
]

## Enables multithreading support for creating proofs.
multicore = ["bellman/multicore"]
multicore = ["circuit", "bellman/multicore"]

### A temporary feature flag that exposes granular APIs needed by `zcashd`. These APIs
### should not be relied upon and will be removed in a future release.
Expand All @@ -105,3 +124,6 @@ harness = false
[[bench]]
name = "pedersen_hash"
harness = false

[patch.crates-io]
redjubjub = { git = "https://github.com/nuttycom/redjubjub", rev = "e413019904400f4caa3550df7c4040befadfbb14" }
43 changes: 31 additions & 12 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Types and functions for building Sapling transaction components.
use core::fmt;
use std::{collections::BTreeMap, iter, marker::PhantomData};
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use core::{fmt, iter, marker::PhantomData};

use group::ff::Field;
use incrementalmerkletree::Position;
Expand All @@ -11,24 +12,27 @@ use redjubjub::{Binding, SpendAuth};
use zcash_note_encryption::EphemeralKeyBytes;

use crate::{
bundle::{
Authorization, Authorized, Bundle, GrothProofBytes, OutputDescription, SpendDescription,
},
circuit,
bundle::{Authorization, Authorized, Bundle, GrothProofBytes},
keys::{
EphemeralSecretKey, ExpandedSpendingKey, FullViewingKey, OutgoingViewingKey,
SpendAuthorizingKey, SpendValidatingKey,
},
note::ExtractedNoteCommitment,
note_encryption::{sapling_note_encryption, Zip212Enforcement},
prover::{OutputProver, SpendProver},
util::generate_random_rseed_internal,
value::{
CommitmentSum, NoteValue, TrapdoorSum, ValueCommitTrapdoor, ValueCommitment, ValueSum,
},
value::{NoteValue, ValueCommitTrapdoor, ValueCommitment, ValueSum},
Anchor, Diversifier, MerklePath, Node, Note, Nullifier, PaymentAddress, SaplingIvk,
NOTE_COMMITMENT_TREE_DEPTH,
};

#[cfg(feature = "circuit")]
use crate::{
bundle::{OutputDescription, SpendDescription},
circuit,
prover::{OutputProver, SpendProver},
value::{CommitmentSum, TrapdoorSum},
zip32::ExtendedSpendingKey,
Anchor, Diversifier, MerklePath, Node, Note, Nullifier, PaymentAddress, ProofGenerationKey,
SaplingIvk, NOTE_COMMITMENT_TREE_DEPTH,
ProofGenerationKey,
};

/// If there are any shielded inputs, always have at least two shielded outputs, padding
Expand Down Expand Up @@ -268,6 +272,7 @@ impl PreparedSpendInfo {
(cv, nullifier, rk, alpha)
}

#[cfg(feature = "circuit")]
fn build<Pr: SpendProver, R: RngCore>(
self,
proof_generation_key: Option<ProofGenerationKey>,
Expand Down Expand Up @@ -464,6 +469,7 @@ impl PreparedOutputInfo {
)
}

#[cfg(feature = "circuit")]
fn build<Pr: OutputProver, R: RngCore>(
self,
rng: &mut R,
Expand Down Expand Up @@ -664,6 +670,7 @@ impl Builder {
}

/// Constructs the Sapling bundle from the builder's accumulated state.
#[cfg(feature = "circuit")]
pub fn build<SP: SpendProver, OP: OutputProver, R: RngCore, V: TryFrom<i64>>(
self,
extsks: &[ExtendedSpendingKey],
Expand Down Expand Up @@ -726,6 +733,7 @@ impl Builder {

/// Constructs a new Sapling transaction bundle of the given type from the specified set of spends
/// and outputs.
#[cfg(feature = "circuit")]
pub fn bundle<SP: SpendProver, OP: OutputProver, R: RngCore, V: TryFrom<i64>>(
rng: R,
bundle_type: BundleType,
Expand Down Expand Up @@ -917,6 +925,7 @@ fn build_bundle<B, SP, OP, R: RngCore>(
/// Type alias for an in-progress bundle that has no proofs or signatures.
///
/// This is returned by [`Builder::build`].
#[cfg(feature = "circuit")]
pub type UnauthorizedBundle<V> = Bundle<InProgress<Unproven, Unsigned>, V>;

/// Marker trait representing bundle proofs in the process of being created.
Expand Down Expand Up @@ -951,9 +960,11 @@ impl<P: InProgressProofs, S: InProgressSignatures> Authorization for InProgress<
///
/// The [`SpendDescription`]s and [`OutputDescription`]s within the bundle contain the
/// private data needed to create proofs.
#[cfg(feature = "circuit")]
#[derive(Clone, Copy, Debug)]
pub struct Unproven;

#[cfg(feature = "circuit")]
impl InProgressProofs for Unproven {
type SpendProof = circuit::Spend;
type OutputProof = circuit::Output;
Expand All @@ -969,29 +980,34 @@ impl InProgressProofs for Proven {
}

/// Reports on the progress made towards creating proofs for a bundle.
#[cfg(feature = "circuit")]
pub trait ProverProgress {
/// Updates the progress instance with the number of steps completed and the total
/// number of steps.
fn update(&mut self, cur: u32, end: u32);
}

#[cfg(feature = "circuit")]
impl ProverProgress for () {
fn update(&mut self, _: u32, _: u32) {}
}

#[cfg(feature = "circuit")]
impl<U: From<(u32, u32)>> ProverProgress for std::sync::mpsc::Sender<U> {
fn update(&mut self, cur: u32, end: u32) {
// If the send fails, we should ignore the error, not crash.
self.send(U::from((cur, end))).unwrap_or(());
}
}

#[cfg(feature = "circuit")]
impl<U: ProverProgress> ProverProgress for &mut U {
fn update(&mut self, cur: u32, end: u32) {
(*self).update(cur, end);
}
}

#[cfg(feature = "circuit")]
struct CreateProofs<'a, SP: SpendProver, OP: OutputProver, R: RngCore, U: ProverProgress> {
spend_prover: &'a SP,
output_prover: &'a OP,
Expand All @@ -1001,6 +1017,7 @@ struct CreateProofs<'a, SP: SpendProver, OP: OutputProver, R: RngCore, U: Prover
progress: u32,
}

#[cfg(feature = "circuit")]
impl<'a, SP: SpendProver, OP: OutputProver, R: RngCore, U: ProverProgress>
CreateProofs<'a, SP, OP, R, U>
{
Expand Down Expand Up @@ -1041,6 +1058,7 @@ impl<'a, SP: SpendProver, OP: OutputProver, R: RngCore, U: ProverProgress>
OP::encode_proof(proof)
}

#[cfg(feature = "circuit")]
fn map_authorization<S: InProgressSignatures>(
&mut self,
a: InProgress<Unproven, S>,
Expand All @@ -1052,6 +1070,7 @@ impl<'a, SP: SpendProver, OP: OutputProver, R: RngCore, U: ProverProgress>
}
}

#[cfg(feature = "circuit")]
impl<S: InProgressSignatures, V> Bundle<InProgress<Unproven, S>, V> {
/// Creates the proofs for this bundle.
pub fn create_proofs<SP: SpendProver, OP: OutputProver>(
Expand Down
Loading

0 comments on commit c80ff2e

Please sign in to comment.