-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It is desirable to have no-std support so the consumers will have more flexibility of use with this library, provided an appropriate storage backend. This functionality will support the usage of contracts in fuel-tx, since its id is calculated with a binary merkle tree, according to fuel specs.
- Loading branch information
Showing
18 changed files
with
178 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,35 @@ | ||
use digest::Digest; | ||
use crate::common::{self, Bytes32, LEAF, NODE}; | ||
|
||
use crate::common::{Bytes32, LEAF, NODE}; | ||
use lazy_static::lazy_static; | ||
use digest::Digest; | ||
use sha2::Sha256; | ||
use std::convert::TryInto; | ||
|
||
type Hash = Sha256; | ||
|
||
lazy_static! { | ||
static ref EMPTY_SUM: Bytes32 = Hash::new().finalize().try_into().unwrap(); | ||
} | ||
|
||
// Merkle Tree hash of an empty list | ||
// MTH({}) = Hash() | ||
pub fn empty_sum() -> &'static Bytes32 { | ||
&*EMPTY_SUM | ||
pub const fn empty_sum() -> &'static Bytes32 { | ||
common::empty_sum_sha256() | ||
} | ||
|
||
// Merkle tree hash of an n-element list D[n] | ||
// MTH(D[n]) = Hash(0x01 || MTH(D[0:k]) || MTH(D[k:n]) | ||
pub fn node_sum(lhs_data: &[u8], rhs_data: &[u8]) -> Bytes32 { | ||
let mut hash = Hash::new(); | ||
|
||
hash.update(&[NODE]); | ||
hash.update(&lhs_data); | ||
hash.update(&rhs_data); | ||
hash.finalize().try_into().unwrap() | ||
|
||
hash.finalize().into() | ||
} | ||
|
||
// Merkle tree hash of a list with one entry | ||
// MTH({d(0)}) = Hash(0x00 || d(0)) | ||
pub fn leaf_sum(data: &[u8]) -> Bytes32 { | ||
let mut hash = Hash::new(); | ||
|
||
hash.update(&[LEAF]); | ||
hash.update(&data); | ||
hash.finalize().try_into().unwrap() | ||
|
||
hash.finalize().into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
use alloc::boxed::Box; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Subtree<T> { | ||
node: T, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
extern crate alloc; | ||
|
||
pub mod binary; | ||
pub mod common; | ||
pub mod sparse; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.