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/>.
|
|
|
|
//
|
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Bitcoin hash functions.
|
|
|
|
//!
|
|
|
|
//! This module provides utility functions related to hashing data, including
|
|
|
|
//! merkleization.
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
use prelude::*;
|
|
|
|
|
2021-06-09 10:40:41 +00:00
|
|
|
use io;
|
|
|
|
use core::cmp::min;
|
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();
|
2021-11-24 23:07:25 +00:00
|
|
|
data[idx1].consensus_encode(&mut encoder).expect("in-memory writers don't error");
|
|
|
|
data[idx2].consensus_encode(&mut encoder).expect("in-memory writers don't error");
|
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();
|
2021-11-24 23:07:25 +00:00
|
|
|
hash1.consensus_encode(&mut encoder).expect("in-memory writers don't error");
|
|
|
|
hash2.consensus_encode(&mut encoder).expect("in-memory writers don't error");
|
2019-12-18 11:40:46 +00:00
|
|
|
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
|
|
|
}
|
2021-11-19 00:18:51 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use consensus::encode::deserialize;
|
|
|
|
use hashes::hex::FromHex;
|
|
|
|
use hashes::sha256d;
|
|
|
|
|
|
|
|
use blockdata::block::Block;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn both_merkel_root_functions_return_the_same_result() {
|
|
|
|
// testnet block 000000000000045e0b1660b6445b5e5c5ab63c9a4f956be7e1e69be04fa4497b
|
|
|
|
let segwit_block = Vec::from_hex("000000202aa2f2ca794ccbd40c16e2f3333f6b8b683f9e7179b2c4d7490600000000000010bc26e70a2f672ad420a6153dd0c28b40a6002c55531bfc99bf8994a8e8f67e5503bd5750d4061a4ed90a700f010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3603da1b0e00045503bd5704c7dd8a0d0ced13bb5785010800000000000a636b706f6f6c122f4e696e6a61506f6f6c2f5345475749542fffffffff02b4e5a212000000001976a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac0000000000000000266a24aa21a9edf91c46b49eb8a29089980f02ee6b57e7d63d33b18b4fddac2bcd7db2a3983704012000000000000000000000000000000000000000000000000000000000000000000000000001000000017e4f81175332a733e26d4ba4e29f53f67b7a5d7c2adebb276e447ca71d130b55000000006b483045022100cac809cd1a3d9ad5d5e31a84e2e1d8ec5542841e4d14c6b52e8b38cbe1ff1728022064470b7fb0c2efeccb2e84bfa36ec5f9e434c84b1101c00f7ee32f726371b7410121020e62280798b6b8c37f068df0915b0865b63fabc401c2457cbc3ef96887dd3647ffffffff02ca2f780c000000001976a914c6b5545b3592cb477d709896fa705592c9b6113a88ac663b2a06000000001976a914e7c1345fc8f87c68170b3aa798a956c2fe6a9eff88ac0000000001000000011e99f5a785e677e017d36b50aa4fd10010ffd039f38f42f447ca8895250e121f01000000d90047304402200d3d296ad641a281dd5c0d68b9ab0d1ad5f7052bec148c1fb81fb1ba69181ec502201a372bb16fb8e054ee9bef41e300d292153830f841a4db0ab7f7407f6581b9bc01473044022002584f313ae990236b6bebb82fbbb006a2b02a448dd5c93434428991eae960d60220491d67d2660c4dde19025cf86e5164a559e2c79c3b98b40e146fab974acd24690147522102632178d046673c9729d828cfee388e121f497707f810c131e0d3fc0fe0bd66d62103a0951ec7d3a9da9de171617026442fcd30f34d66100fab539853b43f508787d452aeffffffff0240420f000000000017a9140ffdcf96700455074292a821c74922e8652993998788997bc60000000017a9148ce5408cfeaddb7ccb2545ded41ef478109454848700000000010000000113100b09e6a78d63ec4850654ab0f68806de29710b09172eddfef730652b155501000000da00473044022015389408e3446a3f36a05060e0e4a3c8b92ff3901ba2511aa944ec91a537a1cb022045a33b6ec47605b1718ed2e753263e54918edbf6126508ff039621fb928d28a001483045022100bb952fde81f216f7063575c0bb2bedc050ce08c96d9b437ea922f5eb98c882da02201b7cbf3a2f94ea4c5eb7f0df3af2ebcafa8705af7f410ab5d3d4bac13d6bc6120147522102632178d046673c9729d828cfee388e121f497707f810c131e0d3fc0fe0bd66d62103a0951ec7d3a9da9de171617026442fcd30f34d66100fab539853b43f508787d452aeffffffff0240420f000000000017a914d3db9a20312c3ab896a316eb108dbd01e47e17d687e0ba7ac60000000017a9148ce5408cfeaddb7ccb2545ded41ef47810945484870000000001000000016e3cca1599cde54878e2f27f434df69df0afd1f313cb6e38c08d3ffb57f97a6c01000000da0048304502210095623b70ec3194fa4037a1c1106c2580caedc390e25e5b330bbeb3111e8184bc02205ae973c4a4454be2a3a03beb66297143c1044a3c4743742c5cdd1d516a1ad3040147304402202f3d6d89996f5b42773dd6ebaf367f1af1f3a95c7c7b487ec040131c40f4a4a30220524ffbb0b563f37b3eb1341228f792e8f84111b7c4a9f49cdd998e052ee42efa0147522102632178d046673c9729d828cfee388e121f497707f810c131e0d3fc0fe0bd66d62103a0951ec7d3a9da9de171617026442fcd30f34d66100fab539853b43f508787d452aeffffffff0240420f000000000017a9141ade6b95896dde8ec4dee9e59af8849d3797348e8728af7ac60000000017a9148ce5408cfeaddb7ccb2545ded41ef47810945484870000000001000000011d9dc3a5df9b5b2eeb2bd11a2db243be9e8cc23e2f180bf317d32a499904c15501000000db00483045022100ebbd1c9a8ce626edbb1a7881df81e872ef8c6424feda36faa8a5745157400c6a02206eb463bc8acd5ea06a289e86115e1daae0c2cf10d9cbbd199e1311170d5543ef01483045022100809411a917dc8cf4f3a777f0388fdea6de06243ef7691e500c60abd1c7f19ae602205255d2b1191d8adedb77b814ccb66471eb8486cb4ff8727824254ee5589f176b0147522102632178d046673c9729d828cfee388e121f497707f810c131e0d3fc0fe0bd66d62103a0951ec7d3a9da9de171617026442fcd30f34d66100fab539853b43f508787d452aeffffffff0240420f000000000017a914759a49c772347be81c49517f9e1e6def6a88d4dd87800b85c60000000017a9148ce5408cfeaddb7ccb2545ded41ef47810945484870000000001000000018c51902affd8e5247dfcc2e5d0528a3815f53c8b6d2c200ff290b2b2b486d7704f0000006a47304402201be0d485f6a3ce871be80064c593c5327b3fd7e450f05ab7fae38385bc40cfbe02206e2a6c9970b5d1d10207892376733757486634fce4f352e772149c486857612101210350c33bc9a790c9495195761577b34912a949b73d5bc5ae5343f5ba08b33220ccffffffff0110270000000000001976a9142
|
|
|
|
let block: Block = deserialize(&segwit_block).expect("Failed to deserialize block");
|
|
|
|
assert!(block.check_merkle_root()); // Sanity check.
|
|
|
|
|
|
|
|
let hashes_iter = block.txdata.iter().map(|obj| obj.txid().as_hash());
|
|
|
|
|
|
|
|
let mut hashes_array: [sha256d::Hash; 15] = [Default::default(); 15];
|
|
|
|
for (i, hash) in hashes_iter.clone().enumerate() {
|
|
|
|
hashes_array[i] = hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
let from_iter = bitcoin_merkle_root(hashes_iter);
|
|
|
|
let from_array = bitcoin_merkle_root_inline(&mut hashes_array);
|
|
|
|
assert_eq!(from_iter, from_array);
|
|
|
|
}
|
|
|
|
}
|