2014-07-18 13:56:17 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
2015-04-07 22:51:57 +00:00
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
2014-07-18 13:56:17 +00:00
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2018-08-08 21:38:50 +00:00
|
|
|
//! Hash functions
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
//! Utility functions related to hashing data, including merkleization
|
|
|
|
|
2015-04-05 17:58:49 +00:00
|
|
|
use std::cmp::min;
|
2019-12-18 11:40:46 +00:00
|
|
|
use std::io;
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2019-12-06 09:01:15 +00:00
|
|
|
use hashes::Hash;
|
2019-12-18 11:40:46 +00:00
|
|
|
use consensus::encode::Encodable;
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2019-12-18 11:40:46 +00:00
|
|
|
/// Calculates the merkle root of a list of hashes inline
|
|
|
|
/// into the allocated slice.
|
|
|
|
///
|
|
|
|
/// In most cases, you'll want to use [bitcoin_merkle_root] instead.
|
|
|
|
pub fn bitcoin_merkle_root_inline<T>(data: &mut [T]) -> T
|
|
|
|
where T: Hash + Encodable,
|
|
|
|
<T as Hash>::Engine: io::Write,
|
|
|
|
{
|
2018-06-01 22:46:10 +00:00
|
|
|
// Base case
|
2019-08-05 19:41:07 +00:00
|
|
|
if data.is_empty() {
|
2018-06-01 22:46:10 +00:00
|
|
|
return Default::default();
|
|
|
|
}
|
|
|
|
if data.len() < 2 {
|
2019-12-18 11:40:46 +00:00
|
|
|
return T::from_inner(data[0].into_inner());
|
2018-06-01 22:46:10 +00:00
|
|
|
}
|
|
|
|
// Recursion
|
|
|
|
for idx in 0..((data.len() + 1) / 2) {
|
|
|
|
let idx1 = 2 * idx;
|
|
|
|
let idx2 = min(idx1 + 1, data.len() - 1);
|
2019-12-18 11:40:46 +00:00
|
|
|
let mut encoder = T::engine();
|
2018-06-01 22:46:10 +00:00
|
|
|
data[idx1].consensus_encode(&mut encoder).unwrap();
|
|
|
|
data[idx2].consensus_encode(&mut encoder).unwrap();
|
2019-12-18 11:40:46 +00:00
|
|
|
data[idx] = T::from_engine(encoder);
|
|
|
|
}
|
|
|
|
let half_len = data.len() / 2 + data.len() % 2;
|
|
|
|
bitcoin_merkle_root_inline(&mut data[0..half_len])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculates the merkle root of an iterator of hashes.
|
|
|
|
pub fn bitcoin_merkle_root<T, I>(mut iter: I) -> T
|
|
|
|
where T: Hash + Encodable,
|
|
|
|
<T as Hash>::Engine: io::Write,
|
|
|
|
I: ExactSizeIterator<Item = T>,
|
|
|
|
{
|
|
|
|
// Base case
|
|
|
|
if iter.len() == 0 {
|
|
|
|
return Default::default();
|
|
|
|
}
|
|
|
|
if iter.len() == 1 {
|
2020-09-14 14:57:42 +00:00
|
|
|
return T::from_inner(iter.next().unwrap().into_inner());
|
2019-12-18 11:40:46 +00:00
|
|
|
}
|
|
|
|
// Recursion
|
|
|
|
let half_len = iter.len() / 2 + iter.len() % 2;
|
|
|
|
let mut alloc = Vec::with_capacity(half_len);
|
|
|
|
while let Some(hash1) = iter.next() {
|
|
|
|
// If the size is odd, use the last element twice.
|
|
|
|
let hash2 = iter.next().unwrap_or(hash1);
|
|
|
|
let mut encoder = T::engine();
|
|
|
|
hash1.consensus_encode(&mut encoder).unwrap();
|
|
|
|
hash2.consensus_encode(&mut encoder).unwrap();
|
|
|
|
alloc.push(T::from_engine(encoder));
|
2018-06-01 22:46:10 +00:00
|
|
|
}
|
2019-12-18 11:40:46 +00:00
|
|
|
bitcoin_merkle_root_inline(&mut alloc)
|
2018-06-01 22:46:10 +00:00
|
|
|
}
|