mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-25 12:06:02 +00:00
Monero: expose merkel root function
This commit is contained in:
parent
dc1b8dfccd
commit
980c5f04ea
3 changed files with 15 additions and 10 deletions
|
@ -121,8 +121,13 @@ impl Block {
|
||||||
/// use the [`Block::hash`] function.
|
/// use the [`Block::hash`] function.
|
||||||
pub fn serialize_pow_hash(&self) -> Vec<u8> {
|
pub fn serialize_pow_hash(&self) -> Vec<u8> {
|
||||||
let mut blob = self.header.serialize();
|
let mut blob = self.header.serialize();
|
||||||
blob.extend_from_slice(&merkle_root(self.miner_transaction.hash(), &self.transactions));
|
|
||||||
write_varint(&(1 + u64::try_from(self.transactions.len()).unwrap()), &mut blob).unwrap();
|
let mut transactions = Vec::with_capacity(self.transactions.len() + 1);
|
||||||
|
transactions.push(self.miner_transaction.hash());
|
||||||
|
transactions.extend_from_slice(&self.transactions);
|
||||||
|
|
||||||
|
blob.extend_from_slice(&merkle_root(&transactions));
|
||||||
|
write_varint(&(1 + self.transactions.len()), &mut blob).unwrap();
|
||||||
blob
|
blob
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +137,7 @@ impl Block {
|
||||||
// Monero pre-appends a VarInt of the block-to-hash'ss length before getting the block hash,
|
// Monero pre-appends a VarInt of the block-to-hash'ss length before getting the block hash,
|
||||||
// but doesn't do this when getting the proof of work hash :)
|
// but doesn't do this when getting the proof of work hash :)
|
||||||
let mut hashing_blob = Vec::with_capacity(9 + hashable.len());
|
let mut hashing_blob = Vec::with_capacity(9 + hashable.len());
|
||||||
write_varint(&u64::try_from(hashable.len()).unwrap(), &mut hashing_blob).unwrap();
|
write_varint(&hashable.len(), &mut hashing_blob).unwrap();
|
||||||
hashing_blob.append(&mut hashable);
|
hashing_blob.append(&mut hashable);
|
||||||
|
|
||||||
let hash = keccak256(hashing_blob);
|
let hash = keccak256(hashing_blob);
|
||||||
|
|
|
@ -7,7 +7,8 @@ pub use monero_io as io;
|
||||||
pub use monero_generators as generators;
|
pub use monero_generators as generators;
|
||||||
pub use monero_primitives as primitives;
|
pub use monero_primitives as primitives;
|
||||||
|
|
||||||
mod merkle;
|
/// Merkel tree functionality.
|
||||||
|
pub mod merkle;
|
||||||
|
|
||||||
/// Ring Signature structs and functionality.
|
/// Ring Signature structs and functionality.
|
||||||
pub mod ring_signatures;
|
pub mod ring_signatures;
|
||||||
|
|
|
@ -2,14 +2,13 @@ use std_shims::vec::Vec;
|
||||||
|
|
||||||
use crate::primitives::keccak256;
|
use crate::primitives::keccak256;
|
||||||
|
|
||||||
pub(crate) fn merkle_root(root: [u8; 32], leafs: &[[u8; 32]]) -> [u8; 32] {
|
/// Calculates the merkel root of the given tree. Equivalent to `tree_hash` in monero-core.
|
||||||
|
pub fn merkle_root(leafs: &[[u8; 32]]) -> [u8; 32] {
|
||||||
match leafs.len() {
|
match leafs.len() {
|
||||||
0 => root,
|
1 => leafs[0],
|
||||||
1 => keccak256([root, leafs[0]].concat()),
|
2 => keccak256([leafs[0], leafs[1]].concat()),
|
||||||
_ => {
|
_ => {
|
||||||
let mut hashes = Vec::with_capacity(1 + leafs.len());
|
let mut hashes = leafs.to_vec();
|
||||||
hashes.push(root);
|
|
||||||
hashes.extend(leafs);
|
|
||||||
|
|
||||||
// Monero preprocess this so the length is a power of 2
|
// Monero preprocess this so the length is a power of 2
|
||||||
let mut high_pow_2 = 4; // 4 is the lowest value this can be
|
let mut high_pow_2 = 4; // 4 is the lowest value this can be
|
||||||
|
|
Loading…
Reference in a new issue