Merge pull request #126 from zcash/incrementalmerkletree_no_std
incrementalmerkletree: Enable use in `no_std` environments.
This commit is contained in:
commit
b822170f2b
|
@ -103,7 +103,7 @@ checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
|
|||
|
||||
[[package]]
|
||||
name = "incrementalmerkletree"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
dependencies = [
|
||||
"either",
|
||||
"proptest",
|
||||
|
|
|
@ -7,6 +7,11 @@ and this project adheres to Rust's notion of
|
|||
|
||||
## Unreleased
|
||||
|
||||
## [0.8.1] - 2024-12-11
|
||||
|
||||
### Changed
|
||||
- Enables use in `no_std` environments.
|
||||
|
||||
## [0.8.0] - 2024-12-11
|
||||
|
||||
### Changed
|
||||
|
@ -14,6 +19,11 @@ and this project adheres to Rust's notion of
|
|||
- `IncrementalWitness::{from_tree, from_parts}` now return `Option<Self>`
|
||||
(returning `None` if a witness cannot be constructed).
|
||||
|
||||
## [0.7.1] - 2024-12-16
|
||||
|
||||
### Added
|
||||
- `no-std` support, via a default-enabled `std` feature flag.
|
||||
|
||||
## [0.7.0] - 2024-09-25
|
||||
|
||||
### Changed
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "incrementalmerkletree"
|
||||
description = "Common types, interfaces, and utilities for Merkle tree data structures"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
authors = [
|
||||
"Sean Bowe <ewillbefull@gmail.com>",
|
||||
"Kris Nuttycombe <kris@nutty.land>",
|
||||
|
@ -18,7 +18,7 @@ all-features = true
|
|||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[dependencies]
|
||||
either = "1.8"
|
||||
either = { version = "1.8", default-features = false }
|
||||
proptest = { workspace = true, optional = true }
|
||||
rand = { version = "0.8", optional = true }
|
||||
rand_core = { version = "0.6", optional = true }
|
||||
|
@ -30,10 +30,14 @@ rand_core = "0.6"
|
|||
rand_chacha = "0.3"
|
||||
|
||||
[features]
|
||||
# The `std` feature is no longer enabled by default because it is not needed
|
||||
# for backwards compatibility; it is only enabled by the `test-dependencies`
|
||||
# feature.
|
||||
std = []
|
||||
# The legacy-api feature guards types and functions that were previously
|
||||
# part of the `zcash_primitives` crate. Those types were removed in the
|
||||
# `zcash_primitives` 0.12 release and are now maintained here.
|
||||
legacy-api = []
|
||||
# The test-dependencies feature guards types and functions that are
|
||||
# useful for testing incremental Merkle trees and Merkle tree frontiers.
|
||||
test-dependencies = ["dep:proptest", "dep:rand", "dep:rand_core"]
|
||||
test-dependencies = ["dep:proptest", "dep:rand", "dep:rand_core", "std"]
|
||||
|
|
|
@ -1,17 +1,18 @@
|
|||
use std::mem::size_of;
|
||||
use alloc::vec::Vec;
|
||||
use core::mem::size_of;
|
||||
|
||||
use crate::{Address, Hashable, Level, MerklePath, Position, Source};
|
||||
|
||||
#[cfg(feature = "legacy-api")]
|
||||
use {std::collections::VecDeque, std::iter::repeat};
|
||||
use {alloc::boxed::Box, alloc::collections::VecDeque, core::iter::repeat};
|
||||
|
||||
#[cfg(any(test, feature = "test-dependencies"))]
|
||||
use {
|
||||
core::num::{NonZeroU64, NonZeroU8},
|
||||
rand::{
|
||||
distributions::{Distribution, Standard},
|
||||
Rng, RngCore,
|
||||
},
|
||||
std::num::{NonZeroU64, NonZeroU8},
|
||||
};
|
||||
|
||||
/// Validation errors that can occur during reconstruction of a Merkle frontier from
|
||||
|
@ -195,7 +196,7 @@ where
|
|||
NonEmptyFrontier::from_parts(
|
||||
position,
|
||||
rng.gen(),
|
||||
std::iter::repeat_with(|| rng.gen())
|
||||
core::iter::repeat_with(|| rng.gen())
|
||||
.take(position.past_ommer_count().into())
|
||||
.collect(),
|
||||
)
|
||||
|
@ -209,7 +210,7 @@ where
|
|||
) -> (Vec<H>, Self) {
|
||||
let prior_subtree_count: u64 = u64::from(tree_size) >> u8::from(subtree_depth);
|
||||
if prior_subtree_count > 0 {
|
||||
let prior_roots: Vec<H> = std::iter::repeat_with(|| rng.gen())
|
||||
let prior_roots: Vec<H> = core::iter::repeat_with(|| rng.gen())
|
||||
.take(prior_subtree_count as usize)
|
||||
.collect();
|
||||
|
||||
|
@ -673,14 +674,15 @@ impl<H: Hashable + Clone, const DEPTH: u8> CommitmentTree<H, DEPTH> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "test-dependencies"))]
|
||||
#[cfg(any(all(test, feature = "std"), feature = "test-dependencies"))]
|
||||
pub mod testing {
|
||||
use core::fmt::Debug;
|
||||
use proptest::collection::vec;
|
||||
use proptest::prelude::*;
|
||||
use rand::{distributions::Standard, prelude::Distribution};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::Hasher;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use {core::hash::Hasher, std::collections::hash_map::DefaultHasher};
|
||||
|
||||
use crate::{frontier::Frontier, Hashable, Level};
|
||||
|
||||
|
@ -699,6 +701,7 @@ pub mod testing {
|
|||
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct TestNode(pub u64);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Hashable for TestNode {
|
||||
fn empty_leaf() -> Self {
|
||||
Self(0)
|
||||
|
@ -762,9 +765,9 @@ pub mod testing {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(all(test, feature = "std"))]
|
||||
mod tests {
|
||||
|
||||
use alloc::string::{String, ToString};
|
||||
use rand::SeedableRng;
|
||||
use rand_chacha::ChaChaRng;
|
||||
|
||||
|
|
|
@ -44,13 +44,21 @@
|
|||
//! context `ommers` refers to the node's ommer, plus each ancestor's ommer.
|
||||
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![no_std]
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
extern crate std;
|
||||
|
||||
#[macro_use]
|
||||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use core::cmp::Ordering;
|
||||
use core::convert::{TryFrom, TryInto};
|
||||
use core::fmt;
|
||||
use core::num::TryFromIntError;
|
||||
use core::ops::{Add, AddAssign, Range, Sub};
|
||||
use either::Either;
|
||||
use std::cmp::Ordering;
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::fmt;
|
||||
use std::num::TryFromIntError;
|
||||
use std::ops::{Add, AddAssign, Range, Sub};
|
||||
|
||||
pub mod frontier;
|
||||
|
||||
|
@ -445,7 +453,7 @@ impl Address {
|
|||
let level_delta = (u64::BITS - index_delta.leading_zeros()) as u8;
|
||||
Address {
|
||||
level: higher.level + level_delta,
|
||||
index: std::cmp::max(higher.index, lower_ancestor_idx) >> level_delta,
|
||||
index: core::cmp::max(higher.index, lower_ancestor_idx) >> level_delta,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -672,12 +680,14 @@ pub trait Hashable: fmt::Debug {
|
|||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use crate::MerklePath;
|
||||
|
||||
use super::{Address, Level, Position, Source};
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::vec::Vec;
|
||||
use core::ops::Range;
|
||||
use either::Either;
|
||||
|
||||
use super::{Address, Level, Position, Source};
|
||||
use crate::MerklePath;
|
||||
|
||||
#[test]
|
||||
fn position_is_complete_subtree() {
|
||||
assert!(Position(0).is_complete_subtree(Level(0)));
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::{Hashable, Level};
|
||||
use alloc::string::{String, ToString};
|
||||
|
||||
/// A possibly-empty incremental Merkle frontier.
|
||||
pub trait Frontier<H> {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use std::convert::TryInto;
|
||||
use std::iter::repeat;
|
||||
use alloc::vec::Vec;
|
||||
use core::convert::TryInto;
|
||||
use core::iter::repeat;
|
||||
|
||||
use crate::{
|
||||
frontier::{CommitmentTree, PathFiller},
|
||||
|
@ -249,7 +250,10 @@ impl<H: Hashable + Clone, const DEPTH: u8> IncrementalWitness<H, DEPTH> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use alloc::string::{String, ToString};
|
||||
|
||||
use crate::{frontier::CommitmentTree, witness::IncrementalWitness, Position};
|
||||
|
||||
#[test]
|
||||
fn witness_tip_position() {
|
||||
let mut base_tree = CommitmentTree::<String, 6>::empty();
|
||||
|
|
Loading…
Reference in New Issue