Skip to content

Commit

Permalink
zcash_keys: Prepare for no_std usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Dec 16, 2024
1 parent c04a71e commit 1d8ba3d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 40 deletions.
46 changes: 36 additions & 10 deletions Cargo.lock

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

24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ pczt = { version = "0.0", path = "pczt" }

# Shielded protocols
bellman = { version = "0.14", default-features = false, features = ["groth16"] }
ff = "0.13"
ff = { version = "0.13", default-features = false }
group = "0.13"
incrementalmerkletree = "0.7"
incrementalmerkletree = { version = "0.7.1", default-features = false }
shardtree = "0.5"
zcash_spec = "0.1"

# Payment protocols
# - Sapling
bitvec = "1"
blake2s_simd = "1"
bitvec = { version = "1", default-features = false, features = ["alloc"] }
blake2s_simd = { version = "1", default-features = false }
bls12_381 = "0.8"
jubjub = "0.10"
redjubjub = "0.7"
Expand All @@ -75,12 +75,12 @@ ripemd = { version = "0.1", default-features = false }
secp256k1 = { version = "0.27", default-features = false, features = ["alloc"] }
transparent = { package = "zcash_transparent", version = "0.0", path = "zcash_transparent", default-features = false }

# Boilerplate
# Boilerplate & missing stdlib
getset = "0.1"

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

# Currency conversions
rust_decimal = { version = "1.35", default-features = false, features = ["serde"] }
Expand All @@ -100,7 +100,7 @@ byteorder = "1"
hex = { version = "0.4", default-features = false, features = ["alloc"] }
percent-encoding = "2.1.0"
postcard = { version = "1", features = ["alloc"] }
serde = { version = "1", features = ["derive"] }
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1"

# HTTP
Expand All @@ -111,8 +111,8 @@ tokio-rustls = "0.24"
webpki-roots = "0.25"

# 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 }

# No-std support
core2 = { version = "0.3", default-features = false, features = ["alloc"] }
Expand Down Expand Up @@ -166,7 +166,7 @@ trait-variant = "0.1"

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

[profile.release]
Expand Down Expand Up @@ -195,5 +195,5 @@ debug = true
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(zcash_unstable, values("zfuture"))'] }

[patch.crates-io]
orchard = { git = "https://github.com/zcash/orchard.git", rev = "7a44e3279b5747819022c4d8f4474fa79b2d9746" }
orchard = { git = "https://github.com/zcash/orchard.git", rev = "f99b6565a78763b58dac792d7492c55067bae680" }
sapling-crypto = { git = "https://github.com/zcash/sapling-crypto.git", rev = "e47d57f5c9c46f05740328f8ef9601f6d697cf34" }
1 change: 1 addition & 0 deletions zcash_keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rand_core.workspace = true
# - Encodings
bech32.workspace = true
bs58.workspace = true
core2.workspace = true

# - Transparent protocols
bip32 = { workspace = true, optional = true }
Expand Down
5 changes: 4 additions & 1 deletion zcash_keys/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Structs for handling supported address types.
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use transparent::address::TransparentAddress;
use zcash_address::{
unified::{self, Container, Encoding, Typecode},
Expand Down Expand Up @@ -215,7 +218,7 @@ impl UnifiedAddress {

/// Returns the set of receiver typecodes.
pub fn receiver_types(&self) -> Vec<Typecode> {
let result = std::iter::empty();
let result = core::iter::empty();
#[cfg(feature = "orchard")]
let result = result.chain(self.orchard.map(|_| Typecode::Orchard));
#[cfg(feature = "sapling")]
Expand Down
9 changes: 6 additions & 3 deletions zcash_keys/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
//! [zcash_protocol::constants] module.
use crate::address::UnifiedAddress;
use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use bs58::{self, decode::Error as Bs58Error};
use std::fmt;
use core::fmt;

use transparent::address::TransparentAddress;
use zcash_address::unified::{self, Encoding};
use zcash_protocol::consensus::{self, NetworkConstants};

#[cfg(feature = "sapling")]
use {
alloc::vec::Vec,
bech32::{
primitives::decode::{CheckedHrpstring, CheckedHrpstringError},
Bech32, Hrp,
},
core2::io::{self, Write},
sapling::zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
std::io::{self, Write},
zcash_protocol::consensus::NetworkType,
};

Expand Down Expand Up @@ -95,7 +98,7 @@ where
/// A trait for encoding and decoding Zcash addresses.
pub trait AddressCodec<P>
where
Self: std::marker::Sized,
Self: core::marker::Sized,
{
type Error;

Expand Down
27 changes: 14 additions & 13 deletions zcash_keys/src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Helper functions for managing light client key material.
use std::{
error,
fmt::{self, Display},
};
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt::{self, Display};

use zcash_address::unified::{self, Container, Encoding, Typecode, Ufvk, Uivk};
use zcash_protocol::consensus;
Expand All @@ -15,7 +14,7 @@ use zcash_protocol::consensus::NetworkConstants;

#[cfg(feature = "transparent-inputs")]
use {
std::convert::TryInto,
core::convert::TryInto,
transparent::keys::{IncomingViewingKey, NonHardenedChildIndex},
};

Expand All @@ -28,8 +27,8 @@ use transparent::address::TransparentAddress;
#[cfg(feature = "unstable")]
use {
byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt},
std::convert::TryFrom,
std::io::{Read, Write},
core::convert::TryFrom,
core2::io::{Read, Write},
zcash_encoding::CompactSize,
zcash_protocol::consensus::BranchId,
};
Expand Down Expand Up @@ -151,8 +150,8 @@ pub enum DecodingError {
KeyDataInvalid(Typecode),
}

impl std::fmt::Display for DecodingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for DecodingError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
#[cfg(feature = "unstable")]
DecodingError::ReadError(s) => write!(f, "Read error: {}", s),
Expand Down Expand Up @@ -341,7 +340,7 @@ impl UnifiedSpendingKey {
#[allow(clippy::unnecessary_unwrap)]
#[cfg(feature = "unstable")]
pub fn from_bytes(era: Era, encoded: &[u8]) -> Result<Self, DecodingError> {
let mut source = std::io::Cursor::new(encoded);
let mut source = core2::io::Cursor::new(encoded);
let decoded_era = source
.read_u32::<LittleEndian>()
.map_err(|_| DecodingError::ReadError("era"))
Expand Down Expand Up @@ -548,7 +547,7 @@ impl fmt::Display for AddressGenerationError {
}
}

impl error::Error for AddressGenerationError {}
impl std::error::Error for AddressGenerationError {}

/// Specification for how a unified address should be generated from a unified viewing key.
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -827,7 +826,7 @@ impl UnifiedFullViewingKey {

/// Returns the string encoding of this `UnifiedFullViewingKey` for the given network.
fn to_ufvk(&self) -> Ufvk {
let items = std::iter::empty().chain(self.unknown.iter().map(|(typecode, data)| {
let items = core::iter::empty().chain(self.unknown.iter().map(|(typecode, data)| {
unified::Fvk::Unknown {
typecode: *typecode,
data: data.clone(),
Expand Down Expand Up @@ -1067,7 +1066,7 @@ impl UnifiedIncomingViewingKey {

/// Converts this unified incoming viewing key to a unified encoding.
fn render(&self) -> Uivk {
let items = std::iter::empty().chain(self.unknown.iter().map(|(typecode, data)| {
let items = core::iter::empty().chain(self.unknown.iter().map(|(typecode, data)| {
unified::Ivk::Unknown {
typecode: *typecode,
data: data.clone(),
Expand Down Expand Up @@ -1329,6 +1328,8 @@ mod tests {
#[cfg(feature = "transparent-inputs")]
use {
crate::{address::Address, encoding::AddressCodec},
alloc::string::ToString,
alloc::vec::Vec,
transparent::keys::{AccountPrivKey, IncomingViewingKey},
zcash_address::test_vectors,
zip32::DiversifierIndex,
Expand Down
6 changes: 6 additions & 0 deletions zcash_keys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
#![doc = document_features::document_features!()]
//!
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
// Catch documentation errors caused by code changes.
#![deny(rustdoc::broken_intra_doc_links)]
// Temporary until we have addressed all Result<T, ()> cases.
#![allow(clippy::result_unit_err)]

#[macro_use]
extern crate alloc;

extern crate std;

pub mod address;
pub mod encoding;

Expand Down
2 changes: 1 addition & 1 deletion zcash_primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ff.workspace = true
group = { workspace = true, features = ["wnaf-memuse"] }
jubjub.workspace = true
nonempty.workspace = true
orchard.workspace = true
orchard = { workspace = true, features = ["circuit"] }
sapling.workspace = true
zcash_spec.workspace = true

Expand Down

0 comments on commit 1d8ba3d

Please sign in to comment.