Skip to content

Commit 4de6818

Browse files
committed
Merkle root hash computation
Offers uses a merkle root hash construction for an id and for signature verification. Add a submodule implementing this so that it can be used when parsing offers.
1 parent a412fcb commit 4de6818

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

lightning/src/offers/merkle.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Tagged hashes for use in signature calculation and verification.
11+
12+
use bitcoin::hashes::{Hash, HashEngine, sha256};
13+
use util::ser::{BigSize, Readable};
14+
15+
use prelude::*;
16+
17+
const SIGNATURE_TYPES: core::ops::RangeInclusive<u64> = 240..=1000;
18+
19+
pub(super) fn root_hash(data: &[u8]) -> sha256::Hash {
20+
let mut tlv_stream = TlvStream::new(&data[..]).peekable();
21+
let nonce_tag = tagged_hash_engine(sha256::Hash::from_engine({
22+
let mut engine = sha256::Hash::engine();
23+
engine.input("LnNonce".as_bytes());
24+
engine.input(tlv_stream.peek().unwrap().type_bytes);
25+
engine
26+
}));
27+
let leaf_tag = tagged_hash_engine(sha256::Hash::hash("LnLeaf".as_bytes()));
28+
let branch_tag = tagged_hash_engine(sha256::Hash::hash("LnBranch".as_bytes()));
29+
30+
let mut leaves = Vec::new();
31+
for record in tlv_stream {
32+
if !SIGNATURE_TYPES.contains(&record.r#type.0) {
33+
leaves.push(tagged_hash_from_engine(leaf_tag.clone(), &record));
34+
leaves.push(tagged_hash_from_engine(nonce_tag.clone(), &record.type_bytes));
35+
}
36+
}
37+
38+
let num_leaves = leaves.len();
39+
for level in 0.. {
40+
let step = 2 << level;
41+
let offset = step / 2;
42+
if offset >= num_leaves {
43+
break;
44+
}
45+
46+
for (i, j) in (0..num_leaves).step_by(step).zip((offset..num_leaves).step_by(step)) {
47+
leaves[i] = tagged_branch_hash_from_engine(branch_tag.clone(), leaves[i], leaves[j]);
48+
}
49+
}
50+
51+
// TODO: Can we ever have zero leaves?
52+
*leaves.first().unwrap()
53+
}
54+
55+
pub(super) fn tagged_hash<T: AsRef<[u8]>>(tag: sha256::Hash, msg: T) -> sha256::Hash {
56+
let engine = tagged_hash_engine(tag);
57+
tagged_hash_from_engine(engine, msg)
58+
}
59+
60+
fn tagged_hash_engine(tag: sha256::Hash) -> sha256::HashEngine {
61+
let mut engine = sha256::Hash::engine();
62+
engine.input(tag.as_ref());
63+
engine.input(tag.as_ref());
64+
engine
65+
}
66+
67+
fn tagged_hash_from_engine<T: AsRef<[u8]>>(mut engine: sha256::HashEngine, msg: T) -> sha256::Hash {
68+
engine.input(msg.as_ref());
69+
sha256::Hash::from_engine(engine)
70+
}
71+
72+
fn tagged_branch_hash_from_engine(
73+
mut engine: sha256::HashEngine, leaf1: sha256::Hash, leaf2: sha256::Hash,
74+
) -> sha256::Hash {
75+
if leaf1 < leaf2 {
76+
engine.input(leaf1.as_ref());
77+
engine.input(leaf2.as_ref());
78+
} else {
79+
engine.input(leaf2.as_ref());
80+
engine.input(leaf1.as_ref());
81+
};
82+
sha256::Hash::from_engine(engine)
83+
}
84+
85+
struct TlvStream<'a> {
86+
data: ::io::Cursor<&'a [u8]>,
87+
}
88+
89+
impl<'a> TlvStream<'a> {
90+
fn new(data: &'a [u8]) -> Self {
91+
Self {
92+
data: ::io::Cursor::new(data),
93+
}
94+
}
95+
}
96+
97+
struct TlvRecord<'a> {
98+
r#type: BigSize,
99+
type_bytes: &'a [u8],
100+
data: &'a [u8],
101+
}
102+
103+
impl AsRef<[u8]> for TlvRecord<'_> {
104+
fn as_ref(&self) -> &[u8] { &self.data }
105+
}
106+
107+
impl<'a> Iterator for TlvStream<'a> {
108+
type Item = TlvRecord<'a>;
109+
110+
fn next(&mut self) -> Option<Self::Item> {
111+
if self.data.position() < self.data.get_ref().len() as u64 {
112+
let start = self.data.position();
113+
114+
let r#type: BigSize = Readable::read(&mut self.data).unwrap();
115+
let offset = self.data.position();
116+
let type_bytes = &self.data.get_ref()[start as usize..offset as usize];
117+
118+
let length: BigSize = Readable::read(&mut self.data).unwrap();
119+
let offset = self.data.position();
120+
let end = offset + length.0;
121+
122+
let _value = &self.data.get_ref()[offset as usize..end as usize];
123+
let data = &self.data.get_ref()[start as usize..end as usize];
124+
125+
self.data.set_position(end);
126+
127+
Some(TlvRecord { r#type, type_bytes, data })
128+
} else {
129+
None
130+
}
131+
}
132+
}

lightning/src/offers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
//!
1313
//! Offers are a flexible protocol for Lightning payments.
1414
15+
mod merkle;
1516
pub mod offer;
1617
pub mod parse;

0 commit comments

Comments
 (0)