#merkle #merkle-tree #no-std

no-std merkle_light

Light merkle tree implementation with SPV support and dependency agnostic

5 releases (3 breaking)

0.4.0 Jun 21, 2022
0.3.1 Dec 13, 2017
0.3.0 Nov 30, 2017
0.2.0 Nov 25, 2017
0.1.0 Nov 22, 2017

#1199 in Cryptography

Download history 207/week @ 2023-10-23 239/week @ 2023-10-30 231/week @ 2023-11-06 211/week @ 2023-11-13 110/week @ 2023-11-20 736/week @ 2023-11-27 621/week @ 2023-12-04 125/week @ 2023-12-11 173/week @ 2023-12-18 58/week @ 2023-12-25 110/week @ 2024-01-01 130/week @ 2024-01-08 227/week @ 2024-01-15 262/week @ 2024-01-22 141/week @ 2024-01-29 128/week @ 2024-02-05

763 downloads per month
Used in merkle_light_derive

BSD-3-Clause

42KB
815 lines

merkle

Build Status Issues License Crates.io

merkle is a lightweight Rust implementation of a Merkle tree.

Features

  • external dependency agnostic
  • core::hash::Hasher compatibility
  • standard types hasher implementations
  • #[derive(Hashable)] support for simple struct
  • customizable merkle leaf/node hashing algorithm
  • support for custom hash types (e.g. [u8; 16], [u64; 4], [u128; 2], struct)
  • customizable hashing algorithm
  • linear memory layout, no nodes on heap
  • buildable from iterator, objects or hashes
  • certificate transparency style merkle hashing support
  • SPV included

Documentation

Documentation is available.

Examples

  • test_sip.rs: algorithm implementation example for std sip hasher, u64 hash items
  • test_xor128.rs: custom hash example xor128
  • test_cmh.rs: custom merkle hasher implementation example
  • crypto_bitcoin_mt.rs: bitcoin merkle tree using crypto lib
  • crypto_chaincore_mt.rs: chain core merkle tree using crypto lib
  • ring_bitcoin_mt.rs: bitcoin merkle tree using ring lib

Quick start

extern crate crypto;
extern crate merkle_light;

use std::fmt;
use std::hash::Hasher;
use std::iter::FromIterator;
use crypto::sha3::{Sha3, Sha3Mode};
use crypto::digest::Digest;
use merkle_light::hash::{Algorithm, Hashable};
use merkle_light::merkle::MerkleTree;

pub struct ExampleAlgorithm(Sha3);

impl ExampleAlgorithm {
    pub fn new() -> ExampleAlgorithm {
        ExampleAlgorithm(Sha3::new(Sha3Mode::Sha3_256))
    }
}

impl Default for ExampleAlgorithm {
    fn default() -> ExampleAlgorithm {
        ExampleAlgorithm::new()
    }
}

impl Hasher for ExampleAlgorithm {
    #[inline]
    fn write(&mut self, msg: &[u8]) {
        self.0.input(msg)
    }

    #[inline]
    fn finish(&self) -> u64 {
        unimplemented!()
    }
}

impl Algorithm<[u8; 32]> for ExampleAlgorithm {
    #[inline]
    fn hash(&mut self) -> [u8; 32] {
        let mut h = [0u8; 32];
        self.0.result(&mut h);
        h
    }

    #[inline]
    fn reset(&mut self) {
        self.0.reset();
    }
}

fn main() {
    let mut h1 = [0u8; 32];
    let mut h2 = [0u8; 32];
    let mut h3 = [0u8; 32];
    h1[0] = 0x11;
    h2[0] = 0x22;
    h3[0] = 0x33;

    let t: MerkleTree<[u8; 32], ExampleAlgorithm> = MerkleTree::from_iter(vec![h1, h2, h3]);
    println!("{:?}", t.root());
}

Bug Reporting

Please report bugs either as pull requests or as issues in the issue tracker. merkle has a full disclosure vulnerability policy. Please do NOT attempt to report any security vulnerability in this code privately to anybody.

License

See LICENSE.

Dependencies

~0–3.5MB
~84K SLoC