mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2024-12-25 21:17:17 +00:00
chore(merkle_root): split function to it's own file
Signed-off-by: Johns Beharry <johns@peakshift.com>
This commit is contained in:
parent
80ac60cc0e
commit
5f9e93d338
@ -8,22 +8,6 @@ pub fn hash(input: &[u8]) -> Vec<u8> {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn merkle_root(values: &[Vec<u8>]) -> Vec<u8> {
|
|
||||||
let values_len = values.len();
|
|
||||||
let mut o: Vec<Vec<u8>> = vec![vec![0]; values_len];
|
|
||||||
|
|
||||||
o.append(&mut values.to_vec());
|
|
||||||
|
|
||||||
for i in (0..values_len).rev() {
|
|
||||||
let mut current_value: Vec<u8> = o[i * 2].clone();
|
|
||||||
current_value.append(&mut o[i * 2 + 1].clone());
|
|
||||||
|
|
||||||
o[i] = hash(¤t_value[..]);
|
|
||||||
}
|
|
||||||
|
|
||||||
o[1].clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -41,23 +25,4 @@ mod tests {
|
|||||||
];
|
];
|
||||||
assert_eq!(expected, output.as_slice());
|
assert_eq!(expected, output.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_merkle_root() {
|
|
||||||
let input = vec![
|
|
||||||
"a".as_bytes().to_vec(),
|
|
||||||
"b".as_bytes().to_vec(),
|
|
||||||
"c".as_bytes().to_vec(),
|
|
||||||
"d".as_bytes().to_vec()
|
|
||||||
];
|
|
||||||
|
|
||||||
let output = merkle_root(&input[..]);
|
|
||||||
|
|
||||||
// merkle root of [[a],[b],[c],[d]]
|
|
||||||
let expected = &[
|
|
||||||
183, 91, 96, 122, 144, 174, 84, 92, 97, 156, 140, 192, 66, 221, 55, 229,
|
|
||||||
234, 48, 118, 7, 61, 207, 39, 125, 150, 32, 94, 90, 19, 88, 122, 163,
|
|
||||||
];
|
|
||||||
assert_eq!(expected, output.as_slice());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
41
eth2/utils/hashing/src/merkle_root.rs
Normal file
41
eth2/utils/hashing/src/merkle_root.rs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
use crate::hash;
|
||||||
|
|
||||||
|
pub fn merkle_root(values: &[Vec<u8>]) -> Vec<u8> {
|
||||||
|
let values_len = values.len();
|
||||||
|
let mut o: Vec<Vec<u8>> = vec![vec![0]; values_len];
|
||||||
|
|
||||||
|
o.append(&mut values.to_vec());
|
||||||
|
|
||||||
|
for i in (0..values_len).rev() {
|
||||||
|
let mut current_value: Vec<u8> = o[i * 2].clone();
|
||||||
|
current_value.append(&mut o[i * 2 + 1].clone());
|
||||||
|
|
||||||
|
o[i] = hash(¤t_value[..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
o[1].clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_merkle_root() {
|
||||||
|
let input = vec![
|
||||||
|
"a".as_bytes().to_vec(),
|
||||||
|
"b".as_bytes().to_vec(),
|
||||||
|
"c".as_bytes().to_vec(),
|
||||||
|
"d".as_bytes().to_vec()
|
||||||
|
];
|
||||||
|
|
||||||
|
let output = merkle_root(&input[..]);
|
||||||
|
|
||||||
|
// merkle root of [[a],[b],[c],[d]]
|
||||||
|
let expected = &[
|
||||||
|
183, 91, 96, 122, 144, 174, 84, 92, 97, 156, 140, 192, 66, 221, 55, 229,
|
||||||
|
234, 48, 118, 7, 61, 207, 39, 125, 150, 32, 94, 90, 19, 88, 122, 163,
|
||||||
|
];
|
||||||
|
assert_eq!(expected, output.as_slice());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user