From 1d8ba3dc0c6d2edb3ee1b58f671fbb904bcbbf46 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Mon, 16 Dec 2024 12:23:01 -0700 Subject: [PATCH] zcash_keys: Prepare for `no_std` usage. --- Cargo.lock | 46 +++++++++++++++++++++++++++++-------- Cargo.toml | 24 +++++++++---------- zcash_keys/Cargo.toml | 1 + zcash_keys/src/address.rs | 5 +++- zcash_keys/src/encoding.rs | 9 +++++--- zcash_keys/src/keys.rs | 27 +++++++++++----------- zcash_keys/src/lib.rs | 6 +++++ zcash_primitives/Cargo.toml | 2 +- 8 files changed, 80 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f61fb3faaa..48730f03f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1875,18 +1875,20 @@ dependencies = [ [[package]] name = "halo2_gadgets" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "126a150072b0c38c7b573fe3eaf0af944a7fed09e154071bf2436d3f016f7230" +checksum = "73a5e510d58a07d8ed238a5a8a436fe6c2c79e1bb2611f62688bc65007b4e6e7" dependencies = [ "arrayvec", "bitvec", "ff", "group", + "halo2_poseidon", "halo2_proofs", "lazy_static", "pasta_curves", "rand", + "sinsemilla", "subtle", "uint", ] @@ -1897,6 +1899,18 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47716fe1ae67969c5e0b2ef826f32db8c3be72be325e1aa3c1951d06b5575ec5" +[[package]] +name = "halo2_poseidon" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa3da60b81f02f9b33ebc6252d766f843291fb4d2247a07ae73d20b791fc56f" +dependencies = [ + "bitvec", + "ff", + "group", + "pasta_curves", +] + [[package]] name = "halo2_proofs" version = "0.3.0" @@ -2171,9 +2185,9 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] name = "incrementalmerkletree" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d45063fbc4b0a37837f6bfe0445f269d13d730ad0aa3b5a7f74aa7bf27a0f4df" +checksum = "216c71634ac6f6ed13c2102d64354c0a04dcbdc30e31692c5972d3974d8b6d97" dependencies = [ "either", "proptest", @@ -2504,12 +2518,9 @@ dependencies = [ [[package]] name = "memuse" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2145869435ace5ea6ea3d35f59be559317ec9a0d04e1812d5f185a87b6d36f1a" -dependencies = [ - "nonempty", -] +checksum = "3d97bbf43eb4f088f8ca469930cde17fa036207c9a5e02ccc5107c4e8b17c964" [[package]] name = "merlin" @@ -2778,16 +2789,18 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" [[package]] name = "orchard" version = "0.10.0" -source = "git+https://github.com/zcash/orchard.git?rev=7a44e3279b5747819022c4d8f4474fa79b2d9746#7a44e3279b5747819022c4d8f4474fa79b2d9746" +source = "git+https://github.com/zcash/orchard.git?rev=f99b6565a78763b58dac792d7492c55067bae680#f99b6565a78763b58dac792d7492c55067bae680" dependencies = [ "aes", "bitvec", "blake2b_simd", + "core2", "ff", "fpe", "getset", "group", "halo2_gadgets", + "halo2_poseidon", "halo2_proofs", "hex", "incrementalmerkletree", @@ -2799,6 +2812,7 @@ dependencies = [ "rand", "reddsa", "serde", + "sinsemilla", "subtle", "tracing", "visibility", @@ -4118,6 +4132,17 @@ dependencies = [ "time", ] +[[package]] +name = "sinsemilla" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d268ae0ea06faafe1662e9967cd4f9022014f5eeb798e0c302c876df8b7af9c" +dependencies = [ + "group", + "pasta_curves", + "subtle", +] + [[package]] name = "siphasher" version = "0.3.11" @@ -6319,6 +6344,7 @@ dependencies = [ "bls12_381", "bs58", "byteorder", + "core2", "document-features", "group", "hex", diff --git a/Cargo.toml b/Cargo.toml index 065b35f650..5732ef4dd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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"] } @@ -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 @@ -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"] } @@ -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] @@ -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" } diff --git a/zcash_keys/Cargo.toml b/zcash_keys/Cargo.toml index 52a8e3c62b..0ab84ba67e 100644 --- a/zcash_keys/Cargo.toml +++ b/zcash_keys/Cargo.toml @@ -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 } diff --git a/zcash_keys/src/address.rs b/zcash_keys/src/address.rs index 29eea7f073..01093c4d5b 100644 --- a/zcash_keys/src/address.rs +++ b/zcash_keys/src/address.rs @@ -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}, @@ -215,7 +218,7 @@ impl UnifiedAddress { /// Returns the set of receiver typecodes. pub fn receiver_types(&self) -> Vec { - 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")] diff --git a/zcash_keys/src/encoding.rs b/zcash_keys/src/encoding.rs index 09f1bd0fee..f4e97d0063 100644 --- a/zcash_keys/src/encoding.rs +++ b/zcash_keys/src/encoding.rs @@ -4,8 +4,10 @@ //! [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}; @@ -13,12 +15,13 @@ 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, }; @@ -95,7 +98,7 @@ where /// A trait for encoding and decoding Zcash addresses. pub trait AddressCodec

where - Self: std::marker::Sized, + Self: core::marker::Sized, { type Error; diff --git a/zcash_keys/src/keys.rs b/zcash_keys/src/keys.rs index 3b6ea90c00..c83ef32b38 100644 --- a/zcash_keys/src/keys.rs +++ b/zcash_keys/src/keys.rs @@ -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; @@ -15,7 +14,7 @@ use zcash_protocol::consensus::NetworkConstants; #[cfg(feature = "transparent-inputs")] use { - std::convert::TryInto, + core::convert::TryInto, transparent::keys::{IncomingViewingKey, NonHardenedChildIndex}, }; @@ -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, }; @@ -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), @@ -341,7 +340,7 @@ impl UnifiedSpendingKey { #[allow(clippy::unnecessary_unwrap)] #[cfg(feature = "unstable")] pub fn from_bytes(era: Era, encoded: &[u8]) -> Result { - let mut source = std::io::Cursor::new(encoded); + let mut source = core2::io::Cursor::new(encoded); let decoded_era = source .read_u32::() .map_err(|_| DecodingError::ReadError("era")) @@ -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)] @@ -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(), @@ -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(), @@ -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, diff --git a/zcash_keys/src/lib.rs b/zcash_keys/src/lib.rs index eb881a80f8..28c9c98b15 100644 --- a/zcash_keys/src/lib.rs +++ b/zcash_keys/src/lib.rs @@ -7,6 +7,7 @@ #![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. @@ -14,6 +15,11 @@ // Temporary until we have addressed all Result cases. #![allow(clippy::result_unit_err)] +#[macro_use] +extern crate alloc; + +extern crate std; + pub mod address; pub mod encoding; diff --git a/zcash_primitives/Cargo.toml b/zcash_primitives/Cargo.toml index c9b9b49e66..f325d6ac47 100644 --- a/zcash_primitives/Cargo.toml +++ b/zcash_primitives/Cargo.toml @@ -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