mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-11 18:04:18 +00:00
Implement tree hash caching (#584)
* Implement basic tree hash caching * Use spaces to indent top-level Cargo.toml * Optimize BLS tree hash by hashing bytes directly * Implement tree hash caching for validator registry * Persist BeaconState tree hash cache to disk * Address Paul's review comments
This commit is contained in:
@@ -10,6 +10,9 @@ use ring::digest::{digest, SHA256};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
#[cfg(feature = "zero_hash_cache")]
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
/// Returns the digest of `input`.
|
||||
///
|
||||
/// Uses `ring::digest::SHA256`.
|
||||
@@ -23,6 +26,31 @@ pub fn hash(input: &[u8]) -> Vec<u8> {
|
||||
h
|
||||
}
|
||||
|
||||
/// Compute the hash of two slices concatenated.
|
||||
pub fn hash_concat(h1: &[u8], h2: &[u8]) -> Vec<u8> {
|
||||
let mut vec1 = h1.to_vec();
|
||||
vec1.extend_from_slice(h2);
|
||||
hash(&vec1)
|
||||
}
|
||||
|
||||
/// The max index that can be used with `ZERO_HASHES`.
|
||||
#[cfg(feature = "zero_hash_cache")]
|
||||
pub const ZERO_HASHES_MAX_INDEX: usize = 48;
|
||||
|
||||
#[cfg(feature = "zero_hash_cache")]
|
||||
lazy_static! {
|
||||
/// Cached zero hashes where `ZERO_HASHES[i]` is the hash of a Merkle tree with 2^i zero leaves.
|
||||
pub static ref ZERO_HASHES: Vec<Vec<u8>> = {
|
||||
let mut hashes = vec![vec![0; 32]; ZERO_HASHES_MAX_INDEX + 1];
|
||||
|
||||
for i in 0..ZERO_HASHES_MAX_INDEX {
|
||||
hashes[i + 1] = hash_concat(&hashes[i], &hashes[i]);
|
||||
}
|
||||
|
||||
hashes
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -41,4 +69,14 @@ mod tests {
|
||||
let expected: Vec<u8> = expected_hex.from_hex().unwrap();
|
||||
assert_eq!(expected, output);
|
||||
}
|
||||
|
||||
#[cfg(feature = "zero_hash_cache")]
|
||||
mod zero_hash {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn zero_hash_zero() {
|
||||
assert_eq!(ZERO_HASHES[0], vec![0; 32]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user