Skip to content

Commit 57a27db

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 2f9fe6d commit 57a27db

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

lightning/src/offers/merkle.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
const SIGNATURE_TYPES: core::ops::RangeInclusive<u64> = 240..=1000;
16+
17+
pub(super) fn root_hash(data: &[u8]) -> sha256::Hash {
18+
let mut engine = sha256::Hash::engine();
19+
engine.input("LnAll".as_bytes());
20+
for record in TlvStream::new(&data[..]) {
21+
if !SIGNATURE_TYPES.contains(&record.r#type.0) {
22+
engine.input(record.as_ref());
23+
}
24+
}
25+
let nonce_tag = sha256::Hash::from_engine(engine);
26+
let leaf_tag = sha256::Hash::hash("LnLeaf".as_bytes());
27+
let branch_tag = sha256::Hash::hash("LnBranch".as_bytes());
28+
29+
let mut leaves = Vec::new();
30+
for record in TlvStream::new(&data[..]) {
31+
if !SIGNATURE_TYPES.contains(&record.r#type.0) {
32+
leaves.push(tagged_hash(leaf_tag, &record));
33+
leaves.push(tagged_hash(nonce_tag, &record));
34+
}
35+
}
36+
37+
let num_leaves = leaves.len();
38+
for level in 0.. {
39+
let step = 2 << level;
40+
let offset = step / 2;
41+
if offset >= num_leaves {
42+
break;
43+
}
44+
45+
for (i, j) in (0..num_leaves).step_by(step).zip((offset..num_leaves).step_by(step)) {
46+
leaves[i] = tagged_branch_hash(branch_tag, leaves[i], leaves[j]);
47+
}
48+
}
49+
50+
// TODO: Can we ever have zero leaves?
51+
*leaves.first().unwrap()
52+
}
53+
54+
pub(super) fn tagged_hash<T: AsRef<[u8]>>(tag: sha256::Hash, msg: T) -> sha256::Hash {
55+
let mut engine = sha256::Hash::engine();
56+
engine.input(tag.as_ref());
57+
engine.input(tag.as_ref());
58+
engine.input(msg.as_ref());
59+
sha256::Hash::from_engine(engine)
60+
}
61+
62+
fn tagged_branch_hash(tag: sha256::Hash, leaf1: sha256::Hash, leaf2: sha256::Hash) -> sha256::Hash {
63+
let mut engine = sha256::Hash::engine();
64+
engine.input(tag.as_ref());
65+
engine.input(tag.as_ref());
66+
if leaf1 < leaf2 {
67+
engine.input(leaf1.as_ref());
68+
engine.input(leaf2.as_ref());
69+
} else {
70+
engine.input(leaf2.as_ref());
71+
engine.input(leaf1.as_ref());
72+
};
73+
sha256::Hash::from_engine(engine)
74+
}
75+
76+
struct TlvStream<'a> {
77+
data: ::io::Cursor<&'a [u8]>,
78+
}
79+
80+
impl<'a> TlvStream<'a> {
81+
fn new(data: &'a [u8]) -> Self {
82+
Self {
83+
data: ::io::Cursor::new(data),
84+
}
85+
}
86+
}
87+
88+
struct TlvRecord<'a> {
89+
r#type: BigSize,
90+
_length: BigSize,
91+
_value: &'a [u8],
92+
data: &'a [u8],
93+
}
94+
95+
impl AsRef<[u8]> for TlvRecord<'_> {
96+
fn as_ref(&self) -> &[u8] { &self.data }
97+
}
98+
99+
impl<'a> Iterator for TlvStream<'a> {
100+
type Item = TlvRecord<'a>;
101+
102+
fn next(&mut self) -> Option<Self::Item> {
103+
if self.data.position() < self.data.get_ref().len() as u64 {
104+
let start = self.data.position();
105+
106+
let r#type: BigSize = Readable::read(&mut self.data).unwrap();
107+
let length: BigSize = Readable::read(&mut self.data).unwrap();
108+
109+
let offset = self.data.position();
110+
let end = offset + length.0;
111+
112+
let value = &self.data.get_ref()[offset as usize..end as usize];
113+
let data = &self.data.get_ref()[start as usize..end as usize];
114+
115+
self.data.set_position(end);
116+
117+
Some(TlvRecord { r#type, _length: length, _value: value, data })
118+
} else {
119+
None
120+
}
121+
}
122+
}

lightning/src/offers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! Implementation of Lightning Offers
1111
//! ([BOLT 12](https://github.com/lightning/bolts/blob/master/12-offer-encoding.md)).
1212
13+
mod merkle;
1314
mod offer;
1415

1516
pub use self::offer::{Amount, BlindedPath, CurrencyCode, Offer};

0 commit comments

Comments
 (0)