mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-11 21:49:26 +00:00
Use a clearer algorithm for the merkle
Should also be more efficient due to not shifting as often.
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std_shims::vec::Vec;
|
||||||
|
|
||||||
use crate::hash;
|
use crate::hash;
|
||||||
|
|
||||||
pub fn merkle_root(root: [u8; 32], leafs: &[[u8; 32]]) -> [u8; 32] {
|
pub fn merkle_root(root: [u8; 32], leafs: &[[u8; 32]]) -> [u8; 32] {
|
||||||
@@ -16,16 +18,24 @@ pub fn merkle_root(root: [u8; 32], leafs: &[[u8; 32]]) -> [u8; 32] {
|
|||||||
}
|
}
|
||||||
let low_pow_2 = high_pow_2 / 2;
|
let low_pow_2 = high_pow_2 / 2;
|
||||||
|
|
||||||
// Merge hashes until we're at the low_pow_2
|
// Merge right-most hashes until we're at the low_pow_2
|
||||||
let mut i = high_pow_2 - hashes.len();
|
{
|
||||||
while hashes.len() != low_pow_2 {
|
let overage = hashes.len() - low_pow_2;
|
||||||
let l = hashes.remove(i);
|
let mut rightmost = hashes.drain((low_pow_2 - overage) ..);
|
||||||
let r = hashes.remove(i);
|
// This is true since we took overage from beneath and above low_pow_2, taking twice as
|
||||||
hashes.insert(i, hash(&[l.as_ref(), &r].concat()));
|
// many elements as overage
|
||||||
i += 1;
|
debug_assert_eq!(rightmost.len() % 2, 0);
|
||||||
|
|
||||||
|
let mut paired_hashes = Vec::with_capacity(overage);
|
||||||
|
while let Some(left) = rightmost.next() {
|
||||||
|
let right = rightmost.next().unwrap();
|
||||||
|
paired_hashes.push(hash(&[left.as_ref(), &right].concat()));
|
||||||
|
}
|
||||||
|
drop(rightmost);
|
||||||
|
|
||||||
|
hashes.extend(paired_hashes);
|
||||||
|
assert_eq!(hashes.len(), low_pow_2);
|
||||||
}
|
}
|
||||||
assert_eq!(hashes.len(), i);
|
|
||||||
assert_eq!(hashes.len(), low_pow_2);
|
|
||||||
|
|
||||||
// Do a traditional pairing off
|
// Do a traditional pairing off
|
||||||
let mut new_hashes = Vec::with_capacity(hashes.len() / 2);
|
let mut new_hashes = Vec::with_capacity(hashes.len() / 2);
|
||||||
|
|||||||
Reference in New Issue
Block a user