Skip to content

Commit

Permalink
Add no_std support (#13)
Browse files Browse the repository at this point in the history
* add no_std support
* fix log calculations
  • Loading branch information
Wizdave97 authored Mar 28, 2022
1 parent 0f11c66 commit 9261516
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 17 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ exclude = ["/ci/*", "/scripts/*", "/.github/*", "/bors.toml"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sha2 = "0.9.8"
sha2 = { version = "0.10.2", default-features = false }
micromath = "2.0.0"

# standard crate data is left out
[dev-dependencies]
rayon = "1.5.1"

[features]
default = ['std']
std = ["sha2/std"]
2 changes: 1 addition & 1 deletion src/algorithms/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Hasher;
use crate::{prelude::*, Hasher};
use sha2::{digest::FixedOutput, Digest, Sha256};

/// Sha256 implementation of the [`Hasher`] trait.
Expand Down
6 changes: 4 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::{Debug, Display, Formatter};
use crate::prelude::*;
use core::fmt::{Debug, Display, Formatter};

/// A list specifying general categories of tree traversals/parsing errors.
///
Expand Down Expand Up @@ -89,10 +90,11 @@ impl Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.message)
}
}
5 changes: 3 additions & 2 deletions src/hasher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::convert::TryFrom;
use std::mem;
use crate::prelude::*;
use core::convert::TryFrom;
use core::mem;

/// Hasher is a trait used to provide a hashing algorithm for the library.
///
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
//! # Ok(())
//! # }
//! ```
#![no_std]

extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

pub use error::Error;
pub use error::ErrorKind;
Expand All @@ -140,6 +146,7 @@ mod hasher;
mod merkle_proof;
mod merkle_tree;
mod partial_tree;
mod prelude;
#[doc(hidden)]
pub mod utils;

Expand Down
3 changes: 2 additions & 1 deletion src/merkle_proof.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::prelude::*;
use crate::{
error::Error,
partial_tree::PartialTree,
proof_serializers::{DirectHashesOrder, MerkleProofSerializer},
utils, Hasher,
};
use std::convert::TryFrom;
use core::convert::TryFrom;

/// [`MerkleProof`] is used to parse, verify, calculate a root for Merkle proofs.
///
Expand Down
1 change: 1 addition & 0 deletions src/merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::prelude::*;
use crate::{partial_tree::PartialTree, utils, utils::indices, Hasher, MerkleProof};

/// [`MerkleTree`] is a Merkle Tree that is well suited for both basic and advanced usage.
Expand Down
1 change: 1 addition & 0 deletions src/partial_tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::prelude::*;
use crate::{error::Error, utils, Hasher};

type PartialTreeLayer<H> = Vec<(usize, H)>;
Expand Down
13 changes: 13 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub use core::prelude::v1::*;

pub use alloc::borrow::ToOwned;
pub use alloc::boxed::Box;
pub use alloc::string::{String, ToString};
pub use alloc::vec::Vec;

pub use alloc::format;
pub use alloc::vec;

// Those are exported by default in the std prelude in Rust 2021
pub use core::convert::{TryFrom, TryInto};
pub use core::iter::FromIterator;
4 changes: 2 additions & 2 deletions src/proof_serializers/direct_hashes_order.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Error, Hasher, MerkleProof, MerkleProofSerializer};
use std::convert::TryFrom;
use crate::{prelude::*, Error, Hasher, MerkleProof, MerkleProofSerializer};
use core::convert::TryFrom;

/// Serializes proof data to bytes with a direct hash order - hashes are concatenated from
/// left to right, bottom to top.
Expand Down
2 changes: 1 addition & 1 deletion src/proof_serializers/merkle_proof_serializer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Error, Hasher, MerkleProof};
use crate::{prelude::*, Error, Hasher, MerkleProof};

/// Trait representing a Merkle proof serializer. Used in [`MerkleProof::serialize`] and
/// [`MerkleProof::deserialize`].
Expand Down
4 changes: 2 additions & 2 deletions src/proof_serializers/reverse_hashes_order.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Error, Hasher, MerkleProof, MerkleProofSerializer};
use std::convert::TryFrom;
use crate::{prelude::*, Error, Hasher, MerkleProof, MerkleProofSerializer};
use core::convert::TryFrom;

/// Serializes proof data to bytes with a reverse hash order - hashes are concatenated from
/// top to bottom, right to left.
Expand Down
2 changes: 2 additions & 0 deletions src/utils/collections.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::prelude::*;

fn byte_to_hex(byte: &u8) -> String {
format!("{:02x}", byte)
}
Expand Down
11 changes: 6 additions & 5 deletions src/utils/indices.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::utils;
use std::collections::HashMap;
use crate::{prelude::*, utils};
use alloc::collections::BTreeMap;

pub fn is_left_index(index: usize) -> bool {
index % 2 == 0
Expand Down Expand Up @@ -35,15 +35,16 @@ pub fn tree_depth(leaves_count: usize) -> usize {
if leaves_count == 1 {
1
} else {
(leaves_count as f64).log2().ceil() as usize
let val = micromath::F32(leaves_count as f32);
val.log2().ceil().0 as usize
}
}

pub fn uneven_layers(tree_leaves_count: usize) -> HashMap<usize, usize> {
pub fn uneven_layers(tree_leaves_count: usize) -> BTreeMap<usize, usize> {
let mut leaves_count = tree_leaves_count;
let depth = tree_depth(tree_leaves_count);

let mut uneven_layers = HashMap::new();
let mut uneven_layers = BTreeMap::new();

for index in 0..depth {
let uneven_layer = leaves_count % 2 != 0;
Expand Down

0 comments on commit 9261516

Please sign in to comment.