Include signature check in BOLT 12 signature test
[rust-lightning] / lightning / src / offers / merkle.rs
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 bitcoin::secp256k1::{Message, PublicKey, Secp256k1, self};
14 use bitcoin::secp256k1::schnorr::Signature;
15 use crate::io;
16 use crate::util::ser::{BigSize, Readable, Writeable, Writer};
17
18 use crate::prelude::*;
19
20 /// Valid type range for signature TLV records.
21 const SIGNATURE_TYPES: core::ops::RangeInclusive<u64> = 240..=1000;
22
23 tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, {
24         (240, signature: Signature),
25 });
26
27 /// Error when signing messages.
28 #[derive(Debug, PartialEq)]
29 pub enum SignError<E> {
30         /// User-defined error when signing the message.
31         Signing(E),
32         /// Error when verifying the produced signature using the given pubkey.
33         Verification(secp256k1::Error),
34 }
35
36 /// Signs a message digest consisting of a tagged hash of the given bytes, checking if it can be
37 /// verified with the supplied pubkey.
38 ///
39 /// Panics if `bytes` is not a well-formed TLV stream containing at least one TLV record.
40 pub(super) fn sign_message<F, E>(
41         sign: F, tag: &str, bytes: &[u8], pubkey: PublicKey,
42 ) -> Result<Signature, SignError<E>>
43 where
44         F: FnOnce(&Message) -> Result<Signature, E>
45 {
46         let digest = message_digest(tag, bytes);
47         let signature = sign(&digest).map_err(|e| SignError::Signing(e))?;
48
49         let pubkey = pubkey.into();
50         let secp_ctx = Secp256k1::verification_only();
51         secp_ctx.verify_schnorr(&signature, &digest, &pubkey).map_err(|e| SignError::Verification(e))?;
52
53         Ok(signature)
54 }
55
56 /// Verifies the signature with a pubkey over the given bytes using a tagged hash as the message
57 /// digest.
58 ///
59 /// Panics if `bytes` is not a well-formed TLV stream containing at least one TLV record.
60 pub(super) fn verify_signature(
61         signature: &Signature, tag: &str, bytes: &[u8], pubkey: PublicKey,
62 ) -> Result<(), secp256k1::Error> {
63         let digest = message_digest(tag, bytes);
64         let pubkey = pubkey.into();
65         let secp_ctx = Secp256k1::verification_only();
66         secp_ctx.verify_schnorr(signature, &digest, &pubkey)
67 }
68
69 pub(super) fn message_digest(tag: &str, bytes: &[u8]) -> Message {
70         let tag = sha256::Hash::hash(tag.as_bytes());
71         let merkle_root = root_hash(bytes);
72         Message::from_slice(&tagged_hash(tag, merkle_root)).unwrap()
73 }
74
75 /// Computes a merkle root hash for the given data, which must be a well-formed TLV stream
76 /// containing at least one TLV record.
77 fn root_hash(data: &[u8]) -> sha256::Hash {
78         let nonce_tag = tagged_hash_engine(sha256::Hash::from_engine({
79                 let first_tlv_record = TlvStream::new(&data[..]).next().unwrap();
80                 let mut engine = sha256::Hash::engine();
81                 engine.input("LnNonce".as_bytes());
82                 engine.input(first_tlv_record.record_bytes);
83                 engine
84         }));
85         let leaf_tag = tagged_hash_engine(sha256::Hash::hash("LnLeaf".as_bytes()));
86         let branch_tag = tagged_hash_engine(sha256::Hash::hash("LnBranch".as_bytes()));
87
88         let mut leaves = Vec::new();
89         let tlv_stream = TlvStream::new(&data[..]);
90         for record in tlv_stream.skip_signatures() {
91                 leaves.push(tagged_hash_from_engine(leaf_tag.clone(), &record.record_bytes));
92                 leaves.push(tagged_hash_from_engine(nonce_tag.clone(), &record.type_bytes));
93         }
94
95         // Calculate the merkle root hash in place.
96         let num_leaves = leaves.len();
97         for level in 0.. {
98                 let step = 2 << level;
99                 let offset = step / 2;
100                 if offset >= num_leaves {
101                         break;
102                 }
103
104                 let left_branches = (0..num_leaves).step_by(step);
105                 let right_branches = (offset..num_leaves).step_by(step);
106                 for (i, j) in left_branches.zip(right_branches) {
107                         leaves[i] = tagged_branch_hash_from_engine(branch_tag.clone(), leaves[i], leaves[j]);
108                 }
109         }
110
111         *leaves.first().unwrap()
112 }
113
114 fn tagged_hash<T: AsRef<[u8]>>(tag: sha256::Hash, msg: T) -> sha256::Hash {
115         let engine = tagged_hash_engine(tag);
116         tagged_hash_from_engine(engine, msg)
117 }
118
119 fn tagged_hash_engine(tag: sha256::Hash) -> sha256::HashEngine {
120         let mut engine = sha256::Hash::engine();
121         engine.input(tag.as_ref());
122         engine.input(tag.as_ref());
123         engine
124 }
125
126 fn tagged_hash_from_engine<T: AsRef<[u8]>>(mut engine: sha256::HashEngine, msg: T) -> sha256::Hash {
127         engine.input(msg.as_ref());
128         sha256::Hash::from_engine(engine)
129 }
130
131 fn tagged_branch_hash_from_engine(
132         mut engine: sha256::HashEngine, leaf1: sha256::Hash, leaf2: sha256::Hash,
133 ) -> sha256::Hash {
134         if leaf1 < leaf2 {
135                 engine.input(leaf1.as_ref());
136                 engine.input(leaf2.as_ref());
137         } else {
138                 engine.input(leaf2.as_ref());
139                 engine.input(leaf1.as_ref());
140         };
141         sha256::Hash::from_engine(engine)
142 }
143
144 /// [`Iterator`] over a sequence of bytes yielding [`TlvRecord`]s. The input is assumed to be a
145 /// well-formed TLV stream.
146 #[derive(Clone)]
147 pub(super) struct TlvStream<'a> {
148         data: io::Cursor<&'a [u8]>,
149 }
150
151 impl<'a> TlvStream<'a> {
152         pub fn new(data: &'a [u8]) -> Self {
153                 Self {
154                         data: io::Cursor::new(data),
155                 }
156         }
157
158         pub fn range<T>(self, types: T) -> impl core::iter::Iterator<Item = TlvRecord<'a>>
159         where
160                 T: core::ops::RangeBounds<u64> + Clone,
161         {
162                 let take_range = types.clone();
163                 self.skip_while(move |record| !types.contains(&record.r#type))
164                         .take_while(move |record| take_range.contains(&record.r#type))
165         }
166
167         fn skip_signatures(self) -> core::iter::Filter<TlvStream<'a>, fn(&TlvRecord) -> bool> {
168                 self.filter(|record| !SIGNATURE_TYPES.contains(&record.r#type))
169         }
170 }
171
172 /// A slice into a [`TlvStream`] for a record.
173 pub(super) struct TlvRecord<'a> {
174         pub(super) r#type: u64,
175         type_bytes: &'a [u8],
176         // The entire TLV record.
177         pub(super) record_bytes: &'a [u8],
178 }
179
180 impl<'a> Iterator for TlvStream<'a> {
181         type Item = TlvRecord<'a>;
182
183         fn next(&mut self) -> Option<Self::Item> {
184                 if self.data.position() < self.data.get_ref().len() as u64 {
185                         let start = self.data.position();
186
187                         let r#type = <BigSize as Readable>::read(&mut self.data).unwrap().0;
188                         let offset = self.data.position();
189                         let type_bytes = &self.data.get_ref()[start as usize..offset as usize];
190
191                         let length = <BigSize as Readable>::read(&mut self.data).unwrap().0;
192                         let offset = self.data.position();
193                         let end = offset + length;
194
195                         let _value = &self.data.get_ref()[offset as usize..end as usize];
196                         let record_bytes = &self.data.get_ref()[start as usize..end as usize];
197
198                         self.data.set_position(end);
199
200                         Some(TlvRecord { r#type, type_bytes, record_bytes })
201                 } else {
202                         None
203                 }
204         }
205 }
206
207 /// Encoding for a pre-serialized TLV stream that excludes any signature TLV records.
208 ///
209 /// Panics if the wrapped bytes are not a well-formed TLV stream.
210 pub(super) struct WithoutSignatures<'a>(pub &'a Vec<u8>);
211
212 impl<'a> Writeable for WithoutSignatures<'a> {
213         #[inline]
214         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
215                 let tlv_stream = TlvStream::new(&self.0[..]);
216                 for record in tlv_stream.skip_signatures() {
217                         writer.write_all(record.record_bytes)?;
218                 }
219                 Ok(())
220         }
221 }
222
223 #[cfg(test)]
224 mod tests {
225         use super::{SIGNATURE_TYPES, TlvStream, WithoutSignatures};
226
227         use bitcoin::hashes::{Hash, sha256};
228         use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey};
229         use bitcoin::secp256k1::schnorr::Signature;
230         use core::convert::Infallible;
231         use crate::offers::offer::{Amount, OfferBuilder};
232         use crate::offers::invoice_request::InvoiceRequest;
233         use crate::offers::parse::Bech32Encode;
234         use crate::util::ser::Writeable;
235
236         #[test]
237         fn calculates_merkle_root_hash() {
238                 // BOLT 12 test vectors
239                 macro_rules! tlv1 { () => { "010203e8" } }
240                 macro_rules! tlv2 { () => { "02080000010000020003" } }
241                 macro_rules! tlv3 { () => { "03310266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c0351800000000000000010000000000000002" } }
242                 assert_eq!(
243                         super::root_hash(&hex::decode(tlv1!()).unwrap()),
244                         sha256::Hash::from_slice(&hex::decode("b013756c8fee86503a0b4abdab4cddeb1af5d344ca6fc2fa8b6c08938caa6f93").unwrap()).unwrap(),
245                 );
246                 assert_eq!(
247                         super::root_hash(&hex::decode(concat!(tlv1!(), tlv2!())).unwrap()),
248                         sha256::Hash::from_slice(&hex::decode("c3774abbf4815aa54ccaa026bff6581f01f3be5fe814c620a252534f434bc0d1").unwrap()).unwrap(),
249                 );
250                 assert_eq!(
251                         super::root_hash(&hex::decode(concat!(tlv1!(), tlv2!(), tlv3!())).unwrap()),
252                         sha256::Hash::from_slice(&hex::decode("ab2e79b1283b0b31e0b035258de23782df6b89a38cfa7237bde69aed1a658c5d").unwrap()).unwrap(),
253                 );
254         }
255
256         #[test]
257         fn calculates_merkle_root_hash_from_invoice_request() {
258                 let secp_ctx = Secp256k1::new();
259                 let recipient_pubkey = {
260                         let secret_key = SecretKey::from_slice(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()).unwrap();
261                         KeyPair::from_secret_key(&secp_ctx, &secret_key).public_key()
262                 };
263                 let payer_keys = {
264                         let secret_key = SecretKey::from_slice(&hex::decode("4242424242424242424242424242424242424242424242424242424242424242").unwrap()).unwrap();
265                         KeyPair::from_secret_key(&secp_ctx, &secret_key)
266                 };
267
268                 // BOLT 12 test vectors
269                 let invoice_request = OfferBuilder::new("A Mathematical Treatise".into(), recipient_pubkey)
270                         .amount(Amount::Currency { iso4217_code: *b"USD", amount: 100 })
271                         .build_unchecked()
272                         .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
273                         .build_unchecked()
274                         .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &payer_keys)))
275                         .unwrap();
276                 assert_eq!(
277                         invoice_request.to_string(),
278                         "lnr1qqyqqqqqqqqqqqqqqcp4256ypqqkgzshgysy6ct5dpjk6ct5d93kzmpq23ex2ct5d9ek293pqthvwfzadd7jejes8q9lhc4rvjxd022zv5l44g6qah82ru5rdpnpjkppqvjx204vgdzgsqpvcp4mldl3plscny0rt707gvpdh6ndydfacz43euzqhrurageg3n7kafgsek6gz3e9w52parv8gs2hlxzk95tzeswywffxlkeyhml0hh46kndmwf4m6xma3tkq2lu04qz3slje2rfthc89vss",
279                 );
280                 assert_eq!(
281                         super::root_hash(&invoice_request.bytes[..]),
282                         sha256::Hash::from_slice(&hex::decode("608407c18ad9a94d9ea2bcdbe170b6c20c462a7833a197621c916f78cf18e624").unwrap()).unwrap(),
283                 );
284                 assert_eq!(
285                         invoice_request.signature(),
286                         Signature::from_slice(&hex::decode("b8f83ea3288cfd6ea510cdb481472575141e8d8744157f98562d162cc1c472526fdb24befefbdebab4dbb726bbd1b7d8aec057f8fa805187e5950d2bbe0e5642").unwrap()).unwrap(),
287                 );
288         }
289
290         #[test]
291         fn skips_encoding_signature_tlv_records() {
292                 let secp_ctx = Secp256k1::new();
293                 let recipient_pubkey = {
294                         let secret_key = SecretKey::from_slice(&[41; 32]).unwrap();
295                         KeyPair::from_secret_key(&secp_ctx, &secret_key).public_key()
296                 };
297                 let payer_keys = {
298                         let secret_key = SecretKey::from_slice(&[42; 32]).unwrap();
299                         KeyPair::from_secret_key(&secp_ctx, &secret_key)
300                 };
301
302                 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey)
303                         .amount_msats(100)
304                         .build_unchecked()
305                         .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
306                         .build_unchecked()
307                         .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &payer_keys)))
308                         .unwrap();
309
310                 let mut bytes_without_signature = Vec::new();
311                 WithoutSignatures(&invoice_request.bytes).write(&mut bytes_without_signature).unwrap();
312
313                 assert_ne!(bytes_without_signature, invoice_request.bytes);
314                 assert_eq!(
315                         TlvStream::new(&bytes_without_signature).count(),
316                         TlvStream::new(&invoice_request.bytes).count() - 1,
317                 );
318         }
319
320         #[test]
321         fn iterates_over_tlv_stream_range() {
322                 let secp_ctx = Secp256k1::new();
323                 let recipient_pubkey = {
324                         let secret_key = SecretKey::from_slice(&[41; 32]).unwrap();
325                         KeyPair::from_secret_key(&secp_ctx, &secret_key).public_key()
326                 };
327                 let payer_keys = {
328                         let secret_key = SecretKey::from_slice(&[42; 32]).unwrap();
329                         KeyPair::from_secret_key(&secp_ctx, &secret_key)
330                 };
331
332                 let invoice_request = OfferBuilder::new("foo".into(), recipient_pubkey)
333                         .amount_msats(100)
334                         .build_unchecked()
335                         .request_invoice(vec![0; 8], payer_keys.public_key()).unwrap()
336                         .build_unchecked()
337                         .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &payer_keys)))
338                         .unwrap();
339
340                 let tlv_stream = TlvStream::new(&invoice_request.bytes).range(0..1)
341                         .chain(TlvStream::new(&invoice_request.bytes).range(1..80))
342                         .chain(TlvStream::new(&invoice_request.bytes).range(80..160))
343                         .chain(TlvStream::new(&invoice_request.bytes).range(160..240))
344                         .chain(TlvStream::new(&invoice_request.bytes).range(SIGNATURE_TYPES))
345                         .map(|r| r.record_bytes.to_vec())
346                         .flatten()
347                         .collect::<Vec<u8>>();
348
349                 assert_eq!(tlv_stream, invoice_request.bytes);
350         }
351
352         impl AsRef<[u8]> for InvoiceRequest {
353                 fn as_ref(&self) -> &[u8] {
354                         &self.bytes
355                 }
356         }
357
358         impl Bech32Encode for InvoiceRequest {
359                 const BECH32_HRP: &'static str = "lnr";
360         }
361
362         impl core::fmt::Display for InvoiceRequest {
363                 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
364                         self.fmt_bech32_str(f)
365                 }
366         }
367 }