bc/
sigcache.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Bitcoin protocol consensus library.
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2019-2024 by
//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// Copyright (C) 2019-2024 LNP/BP Standards Association. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Borrow;

use amplify::{Bytes32, IoError};
use commit_verify::{Digest, DigestExt, Sha256};

use crate::{
    Annex, ConsensusEncode, Sats, ScriptCode, ScriptPubkey, SeqNo, SigScript, Sighash, SighashFlag,
    SighashType, TapLeafHash, TapSighash, Tx as Transaction, TxIn, TxOut, Txid, VarIntArray,
};

/// Used for signature hash for invalid use of SIGHASH_SINGLE.
const UINT256_ONE: [u8; 32] = [
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
#[display(
    "number of inputs ({inputs}) doesn't match to the number of provided prevouts ({prevouts}) \
     for signature hasher on tx {txid}."
)]
pub struct PrevoutMismatch {
    txid: Txid,
    inputs: usize,
    prevouts: usize,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
#[display(doc_comments)]
pub enum SighashError {
    /// invalid input index {index} in {txid} which has only {inputs} inputs.
    InvalidInputIndex {
        txid: Txid,
        index: usize,
        inputs: usize,
    },

    /// transaction {txid} input {index} uses SIGHASH_SINGLE, but the total
    /// number of outputs is {outputs} and thus no signature can be produced.
    NoSingleOutputMatch {
        txid: Txid,
        index: usize,
        outputs: usize,
    },
}

impl From<IoError> for SighashError {
    fn from(_: IoError) -> Self { unreachable!("in-memory I/O doesn't error in Rust") }
}

/// Efficiently calculates signature hash message for legacy, segwit and taproot
/// inputs.
#[derive(Debug)]
pub struct SighashCache<Prevout: Borrow<TxOut> = TxOut, Tx: Borrow<Transaction> = Transaction> {
    /// Access to transaction required for transaction introspection.
    tx: Tx,

    prevouts: Vec<Prevout>,

    /// Common cache for taproot and segwit inputs, `None` for legacy inputs.
    common_cache: Option<CommonCache>,

    /// Cache for segwit v0 inputs (the result of another round of sha256 on
    /// `common_cache`).
    segwit_cache: Option<SegwitCache>,

    /// Cache for taproot v1 inputs.
    taproot_cache: Option<TaprootCache>,
}

/// Common values cached between segwit and taproot inputs.
#[derive(Copy, Clone, Debug)]
struct CommonCache {
    prevouts: Bytes32,
    sequences: Bytes32,

    /// In theory `outputs` could be an `Option` since `SIGHASH_NONE` and
    /// `SIGHASH_SINGLE` do not need it, but since `SIGHASH_ALL` is by far
    /// the most used variant we don't bother.
    outputs: Bytes32,
}

/// Values cached for segwit inputs, equivalent to [`CommonCache`] plus another
/// round of `sha256`.
#[derive(Copy, Clone, Debug)]
struct SegwitCache {
    prevouts: Bytes32,
    sequences: Bytes32,
    outputs: Bytes32,
}

/// Values cached for taproot inputs.
#[derive(Copy, Clone, Debug)]
struct TaprootCache {
    amounts: Bytes32,
    script_pubkeys: Bytes32,
}

impl<Prevout: Borrow<TxOut>, Tx: Borrow<Transaction>> SighashCache<Prevout, Tx> {
    /// Constructs a new `SighashCache` from an unsigned transaction.
    ///
    /// The sighash components are computed in a lazy manner when required. For
    /// the generated sighashes to be valid, no fields in the transaction
    /// may change except for script_sig and witness.
    pub fn new(tx: Tx, prevouts: Vec<Prevout>) -> Result<Self, PrevoutMismatch> {
        if tx.borrow().inputs.len() != prevouts.len() {
            return Err(PrevoutMismatch {
                txid: tx.borrow().txid(),
                inputs: tx.borrow().inputs.len(),
                prevouts: prevouts.len(),
            });
        }

        Ok(SighashCache {
            tx,
            prevouts,
            common_cache: None,
            taproot_cache: None,
            segwit_cache: None,
        })
    }

    /// Computes the BIP341 sighash for any type with a fine-grained control
    /// over annex and code separator.
    pub fn tap_sighash_custom(
        &mut self,
        input_index: usize,
        annex: Option<Annex>,
        leaf_hash_code_separator: Option<(TapLeafHash, u32)>,
        sighash_type: Option<SighashType>,
    ) -> Result<TapSighash, SighashError> {
        let mut hasher = TapSighash::engine();

        let SighashType {
            flag: sighash_flag,
            anyone_can_pay,
        } = sighash_type.unwrap_or_default();

        // epoch
        0u8.consensus_encode(&mut hasher)?;

        // * Control:
        // hash_type (1).
        match sighash_type {
            None => 0u8.consensus_encode(&mut hasher)?,
            Some(sighash_type) => sighash_type.to_consensus_u8().consensus_encode(&mut hasher)?,
        };

        {
            let tx = self.tx.borrow();
            // * Transaction Data:
            // nVersion (4): the nVersion of the transaction.
            tx.version.consensus_encode(&mut hasher)?;

            // nLockTime (4): the nLockTime of the transaction.
            tx.lock_time.consensus_encode(&mut hasher)?;
        }

        // If the hash_type & 0x80 does not equal SIGHASH_ANYONECANPAY:
        //     sha_prevouts (32): the SHA256 of the serialization of all input
        // outpoints.     sha_amounts (32): the SHA256 of the serialization of
        // all spent output amounts.     sha_scriptpubkeys (32): the SHA256 of
        // the serialization of all spent output scriptPubKeys.
        // sha_sequences (32): the SHA256 of the serialization of all
        // input nSequence.
        if !anyone_can_pay {
            self.common_cache().prevouts.consensus_encode(&mut hasher)?;
            self.taproot_cache().amounts.consensus_encode(&mut hasher)?;
            self.taproot_cache().script_pubkeys.consensus_encode(&mut hasher)?;
            self.common_cache().sequences.consensus_encode(&mut hasher)?;
        }

        // If hash_type & 3 does not equal SIGHASH_NONE or SIGHASH_SINGLE:
        //     sha_outputs (32): the SHA256 of the serialization of all outputs in
        // CTxOut format.
        if sighash_flag != SighashFlag::None && sighash_flag != SighashFlag::Single {
            self.common_cache().outputs.consensus_encode(&mut hasher)?;
        }

        // * Data about this input:
        // spend_type (1): equal to (ext_flag * 2) + annex_present, where annex_present
        // is 0 if no annex is present, or 1 otherwise
        let mut spend_type = 0u8;
        if annex.is_some() {
            spend_type |= 1u8;
        }
        if leaf_hash_code_separator.is_some() {
            spend_type |= 2u8;
        }
        spend_type.consensus_encode(&mut hasher)?;

        let tx = self.tx.borrow();

        // If hash_type & 0x80 equals SIGHASH_ANYONECANPAY:
        //      outpoint (36): the COutPoint of this input (32-byte hash + 4-byte
        // little-endian).      amount (8): value of the previous output spent
        // by this input.      scriptPubKey (35): scriptPubKey of the previous
        // output spent by this input, serialized as script inside CTxOut. Its
        // size is always 35 bytes.      nSequence (4): nSequence of this input.
        if anyone_can_pay {
            let txin = tx.inputs.get(input_index).ok_or(SighashError::InvalidInputIndex {
                txid: tx.txid(),
                index: input_index,
                inputs: tx.inputs.len(),
            })?;
            let previous_output = self.prevouts[input_index].borrow();
            txin.prev_output.consensus_encode(&mut hasher)?;
            previous_output.value.consensus_encode(&mut hasher)?;
            previous_output.script_pubkey.consensus_encode(&mut hasher)?;
            txin.sequence.consensus_encode(&mut hasher)?;
        } else {
            (input_index as u32).consensus_encode(&mut hasher)?;
        }

        // If an annex is present (the lowest bit of spend_type is set):
        //      sha_annex (32): the SHA256 of (compact_size(size of annex) || annex),
        // where annex      includes the mandatory 0x50 prefix.
        if let Some(annex) = annex {
            let mut enc = Sha256::default();
            annex.consensus_encode(&mut enc)?;
            let hash = enc.finish();
            hash.consensus_encode(&mut hasher)?;
        }

        // * Data about this output:
        // If hash_type & 3 equals SIGHASH_SINGLE:
        //      sha_single_output (32): the SHA256 of the corresponding output in CTxOut
        // format.
        if sighash_flag == SighashFlag::Single {
            let mut enc = Sha256::default();
            tx.outputs
                .get(input_index)
                .ok_or(SighashError::NoSingleOutputMatch {
                    txid: tx.txid(),
                    index: input_index,
                    outputs: tx.outputs.len(),
                })?
                .consensus_encode(&mut enc)?;
            let hash = enc.finish();
            hash.consensus_encode(&mut hasher)?;
        }

        //     if (scriptpath):
        //         ss += TaggedHash("TapLeaf", bytes([leaf_ver]) + ser_string(script))
        //         ss += bytes([0])
        //         ss += struct.pack("<i", codeseparator_pos)
        if let Some((hash, code_separator_pos)) = leaf_hash_code_separator {
            hash.consensus_encode(&mut hasher)?;
            0u8.consensus_encode(&mut hasher)?;
            code_separator_pos.consensus_encode(&mut hasher)?;
        }

        Ok(TapSighash::from_engine(hasher))
    }

    /// Computes the BIP341 sighash for a key spend.
    pub fn tap_sighash_key(
        &mut self,
        input_index: usize,
        sighash_type: Option<SighashType>,
    ) -> Result<TapSighash, SighashError> {
        self.tap_sighash_custom(input_index, None, None, sighash_type)
    }

    /// Computes the BIP341 sighash for a script spend.
    ///
    /// Assumes the default `OP_CODESEPARATOR` position of `0xFFFFFFFF`.
    pub fn tap_sighash_script(
        &mut self,
        input_index: usize,
        leaf_hash: impl Into<TapLeafHash>,
        sighash_type: Option<SighashType>,
    ) -> Result<TapSighash, SighashError> {
        self.tap_sighash_custom(
            input_index,
            None,
            Some((leaf_hash.into(), 0xFFFFFFFF)),
            sighash_type,
        )
    }

    /// Computes the BIP143 sighash for any flag type.
    pub fn segwit_sighash(
        &mut self,
        input_index: usize,
        script_code: &ScriptCode,
        value: Sats,
        sighash_type: SighashType,
    ) -> Result<Sighash, SighashError> {
        let mut hasher = Sighash::engine();

        let zero_hash = [0u8; 32];

        let SighashType {
            flag: sighash_flag,
            anyone_can_pay,
        } = sighash_type;

        self.tx.borrow().version.consensus_encode(&mut hasher)?;

        if !anyone_can_pay {
            self.segwit_cache().prevouts.consensus_encode(&mut hasher)?;
        } else {
            zero_hash.consensus_encode(&mut hasher)?;
        }

        if !anyone_can_pay
            && sighash_flag != SighashFlag::Single
            && sighash_flag != SighashFlag::None
        {
            self.segwit_cache().sequences.consensus_encode(&mut hasher)?;
        } else {
            zero_hash.consensus_encode(&mut hasher)?;
        }

        {
            let tx = self.tx.borrow();
            let txin = tx.inputs.get(input_index).ok_or(SighashError::InvalidInputIndex {
                txid: tx.txid(),
                index: input_index,
                inputs: tx.inputs.len(),
            })?;

            txin.prev_output.consensus_encode(&mut hasher)?;
            script_code.consensus_encode(&mut hasher)?;
            value.consensus_encode(&mut hasher)?;
            txin.sequence.consensus_encode(&mut hasher)?;
        }

        if sighash_flag != SighashFlag::Single && sighash_flag != SighashFlag::None {
            self.segwit_cache().outputs.consensus_encode(&mut hasher)?;
        } else if sighash_flag == SighashFlag::Single
            && input_index < self.tx.borrow().outputs.len()
        {
            let mut single_enc = Sighash::engine();
            self.tx.borrow().outputs[input_index].consensus_encode(&mut single_enc)?;
            Sighash::from_engine(single_enc).consensus_encode(&mut hasher)?;
        } else {
            zero_hash.consensus_encode(&mut hasher)?;
        }

        self.tx.borrow().lock_time.consensus_encode(&mut hasher)?;
        sighash_type.to_consensus_u32().consensus_encode(&mut hasher)?;

        Ok(Sighash::from_engine(hasher))
    }

    /// Computes the legacy sighash for any `sighash_type`.
    pub fn legacy_sighash(
        &self,
        input_index: usize,
        script_pubkey: &ScriptPubkey,
        sighash_type: u32,
    ) -> Result<Sighash, SighashError> {
        let tx_src = self.tx.borrow();
        let mut hasher = Sighash::engine();

        if input_index >= tx_src.inputs.len() {
            return Err(SighashError::InvalidInputIndex {
                txid: tx_src.txid(),
                index: input_index,
                inputs: tx_src.inputs.len(),
            });
        }

        let SighashType {
            flag: sighash_flag,
            anyone_can_pay,
        } = SighashType::from_consensus_u32(sighash_type);

        if sighash_flag == SighashFlag::Single && input_index >= tx_src.outputs.len() {
            return Ok(Sighash::from(UINT256_ONE));
        }

        // Build tx to sign
        let mut tx = Transaction {
            version: tx_src.version,
            lock_time: tx_src.lock_time,
            inputs: none!(),
            outputs: none!(),
        };

        // Add all necessary inputs...
        let sig_script = script_pubkey.as_script_bytes().clone().into();
        if anyone_can_pay {
            tx.inputs = VarIntArray::from_checked(vec![TxIn {
                prev_output: tx_src.inputs[input_index].prev_output,
                sig_script,
                sequence: tx_src.inputs[input_index].sequence,
                witness: none!(),
            }]);
        } else {
            let inputs = tx_src.inputs.iter().enumerate().map(|(n, input)| TxIn {
                prev_output: input.prev_output,
                sig_script: if n == input_index { sig_script.clone() } else { SigScript::new() },
                sequence: if n != input_index
                    && (sighash_flag == SighashFlag::Single || sighash_flag == SighashFlag::None)
                {
                    SeqNo::ZERO
                } else {
                    input.sequence
                },
                witness: none!(),
            });
            tx.inputs = VarIntArray::from_iter_checked(inputs);
        }
        // ...then all outputs
        tx.outputs = match sighash_flag {
            SighashFlag::All => tx_src.outputs.clone(),
            SighashFlag::Single => {
                let outputs = tx_src.outputs.iter()
                    .take(input_index + 1)  // sign all outputs up to and including this one, but erase
                    .enumerate()            // all of them except for this one
                    .map(|(n, out)| if n == input_index { out.clone() } else { TxOut::default() });
                VarIntArray::from_iter_checked(outputs)
            }
            SighashFlag::None => none!(),
        };
        // hash the result
        tx.consensus_encode(&mut hasher)?;
        sighash_type.consensus_encode(&mut hasher)?;

        Ok(Sighash::from_engine(hasher))
    }

    fn common_cache(&mut self) -> &CommonCache {
        let tx = self.tx.borrow();
        self.common_cache.get_or_insert_with(|| {
            let mut enc_prevouts = Sha256::default();
            let mut enc_sequences = Sha256::default();
            for txin in &tx.inputs {
                let _ = txin.prev_output.consensus_encode(&mut enc_prevouts);
                let _ = txin.sequence.consensus_encode(&mut enc_sequences);
            }
            let mut enc_outputs = Sha256::default();
            for txout in &tx.outputs {
                let _ = txout.consensus_encode(&mut enc_outputs);
            }
            CommonCache {
                prevouts: enc_prevouts.finish().into(),
                sequences: enc_sequences.finish().into(),
                outputs: enc_outputs.finish().into(),
            }
        })
    }

    fn segwit_cache(&mut self) -> &SegwitCache {
        let common_cache = *self.common_cache();
        self.segwit_cache.get_or_insert_with(|| SegwitCache {
            prevouts: <[u8; 32]>::from(Sha256::digest(common_cache.prevouts)).into(),
            sequences: <[u8; 32]>::from(Sha256::digest(common_cache.sequences)).into(),
            outputs: <[u8; 32]>::from(Sha256::digest(common_cache.outputs)).into(),
        })
    }

    fn taproot_cache(&mut self) -> &TaprootCache {
        self.taproot_cache.get_or_insert_with(|| {
            let mut enc_amounts = Sha256::default();
            let mut enc_script_pubkeys = Sha256::default();
            for prevout in &self.prevouts {
                let _ = prevout.borrow().value.consensus_encode(&mut enc_amounts);
                let _ = prevout.borrow().script_pubkey.consensus_encode(&mut enc_script_pubkeys);
            }
            TaprootCache {
                amounts: enc_amounts.finish().into(),
                script_pubkeys: enc_script_pubkeys.finish().into(),
            }
        })
    }
}