Move low-R comments into blocks
[rust-lightning] / lightning / src / sign / mod.rs
index 4e418f049bbc9867a7a2a7920706e17c135ed23e..fd992a594c489fe2e6ada0491f1cdc3c5fe8d0e9 100644 (file)
@@ -38,7 +38,7 @@ use bitcoin::secp256k1::schnorr;
 use bitcoin::{secp256k1, Sequence, Witness, Txid};
 
 use crate::util::transaction_utils;
-use crate::util::crypto::{hkdf_extract_expand_twice, sign, sign_with_aux_rand};
+use crate::crypto::utils::{hkdf_extract_expand_twice, sign, sign_with_aux_rand};
 use crate::util::ser::{Writeable, Writer, Readable, ReadableArgs};
 use crate::chain::transaction::OutPoint;
 use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
@@ -53,7 +53,6 @@ use crate::offers::invoice::UnsignedBolt12Invoice;
 use crate::offers::invoice_request::UnsignedInvoiceRequest;
 
 use crate::prelude::*;
-use core::convert::TryInto;
 use core::ops::Deref;
 use core::sync::atomic::{AtomicUsize, Ordering};
 #[cfg(taproot)]
@@ -65,7 +64,7 @@ use crate::sign::ecdsa::{EcdsaChannelSigner, WriteableEcdsaChannelSigner};
 #[cfg(taproot)]
 use crate::sign::taproot::TaprootChannelSigner;
 use crate::util::atomic_counter::AtomicCounter;
-use crate::util::chacha20::ChaCha20;
+use crate::crypto::chacha20::ChaCha20;
 use crate::util::invoice::construct_invoice_preimage;
 
 pub(crate) mod type_resolver;
@@ -157,7 +156,7 @@ impl StaticPaymentOutputDescriptor {
        pub fn witness_script(&self) -> Option<ScriptBuf> {
                self.channel_transaction_parameters.as_ref()
                        .and_then(|channel_params|
-                                if channel_params.channel_type_features.supports_anchors_zero_fee_htlc_tx() {
+                                if channel_params.supports_anchors() {
                                        let payment_point = channel_params.holder_pubkeys.payment_point;
                                        Some(chan_utils::get_to_countersignatory_with_anchors_redeemscript(&payment_point))
                                 } else {
@@ -170,9 +169,7 @@ impl StaticPaymentOutputDescriptor {
        /// Note: If you have the grind_signatures feature enabled, this will be at least 1 byte
        /// shorter.
        pub fn max_witness_length(&self) -> u64 {
-               if self.channel_transaction_parameters.as_ref()
-                       .map(|channel_params| channel_params.channel_type_features.supports_anchors_zero_fee_htlc_tx())
-                       .unwrap_or(false)
+               if self.channel_transaction_parameters.as_ref().map_or(false, |p| p.supports_anchors())
                {
                        let witness_script_weight = 1 /* pubkey push */ + 33 /* pubkey */ +
                                1 /* OP_CHECKSIGVERIFY */ + 1 /* OP_1 */ + 1 /* OP_CHECKSEQUENCEVERIFY */;
@@ -350,15 +347,14 @@ impl SpendableOutputDescriptor {
                let mut input = Vec::with_capacity(descriptors.len());
                let mut input_value = 0;
                let mut witness_weight = 0;
-               let mut output_set = HashSet::with_capacity(descriptors.len());
+               let mut output_set = hash_set_with_capacity(descriptors.len());
                for outp in descriptors {
                        match outp {
                                SpendableOutputDescriptor::StaticPaymentOutput(descriptor) => {
                                        if !output_set.insert(descriptor.outpoint) { return Err(()); }
                                        let sequence =
                                                if descriptor.channel_transaction_parameters.as_ref()
-                                                       .map(|channel_params| channel_params.channel_type_features.supports_anchors_zero_fee_htlc_tx())
-                                                       .unwrap_or(false)
+                                                       .map_or(false, |p| p.supports_anchors())
                                                {
                                                        Sequence::from_consensus(1)
                                                } else {
@@ -372,7 +368,9 @@ impl SpendableOutputDescriptor {
                                        });
                                        witness_weight += descriptor.max_witness_length();
                                        #[cfg(feature = "grind_signatures")]
-                                       { witness_weight -= 1; } // Guarantees a low R signature
+                                       { // Guarantees a low R signature
+                                               witness_weight -= 1;
+                                       }
                                        input_value += descriptor.output.value;
                                },
                                SpendableOutputDescriptor::DelayedPaymentOutput(descriptor) => {
@@ -385,7 +383,9 @@ impl SpendableOutputDescriptor {
                                        });
                                        witness_weight += DelayedPaymentOutputDescriptor::MAX_WITNESS_LENGTH;
                                        #[cfg(feature = "grind_signatures")]
-                                       { witness_weight -= 1; } // Guarantees a low R signature
+                                       { // Guarantees a low R signature
+                                               witness_weight -= 1;
+                                       }
                                        input_value += descriptor.output.value;
                                },
                                SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output, .. } => {
@@ -398,7 +398,9 @@ impl SpendableOutputDescriptor {
                                        });
                                        witness_weight += 1 + 73 + 34;
                                        #[cfg(feature = "grind_signatures")]
-                                       { witness_weight -= 1; } // Guarantees a low R signature
+                                       { // Guarantees a low R signature
+                                               witness_weight -= 1;
+                                       }
                                        input_value += output.value;
                                }
                        }
@@ -736,6 +738,19 @@ pub trait NodeSigner {
        fn sign_gossip_message(&self, msg: UnsignedGossipMessage) -> Result<Signature, ()>;
 }
 
+// Primarily needed in doctests because of https://github.com/rust-lang/rust/issues/67295
+/// A dynamic [`SignerProvider`] temporarily needed for doc tests.
+#[cfg(taproot)]
+#[doc(hidden)]
+#[deprecated(note = "Remove once taproot cfg is removed")]
+pub type DynSignerProvider = dyn SignerProvider<EcdsaSigner = InMemorySigner, TaprootSigner = InMemorySigner>;
+
+/// A dynamic [`SignerProvider`] temporarily needed for doc tests.
+#[cfg(not(taproot))]
+#[doc(hidden)]
+#[deprecated(note = "Remove once taproot cfg is removed")]
+pub type DynSignerProvider = dyn SignerProvider<EcdsaSigner = InMemorySigner>;
+
 /// A trait that can return signer instances for individual channels.
 pub trait SignerProvider {
        /// A type which implements [`WriteableEcdsaChannelSigner`] which will be returned by [`Self::derive_channel_signer`].
@@ -822,11 +837,8 @@ pub struct InMemorySigner {
        channel_value_satoshis: u64,
        /// Key derivation parameters.
        channel_keys_id: [u8; 32],
-       /// Seed from which all randomness produced is derived from.
-       rand_bytes_unique_start: [u8; 32],
-       /// Tracks the number of times we've produced randomness to ensure we don't return the same
-       /// bytes twice.
-       rand_bytes_index: AtomicCounter,
+       /// A source of random bytes.
+       entropy_source: RandomBytes,
 }
 
 impl PartialEq for InMemorySigner {
@@ -857,8 +869,7 @@ impl Clone for InMemorySigner {
                        channel_parameters: self.channel_parameters.clone(),
                        channel_value_satoshis: self.channel_value_satoshis,
                        channel_keys_id: self.channel_keys_id,
-                       rand_bytes_unique_start: self.get_secure_random_bytes(),
-                       rand_bytes_index: AtomicCounter::new(),
+                       entropy_source: RandomBytes::new(self.get_secure_random_bytes()),
                }
        }
 }
@@ -892,8 +903,7 @@ impl InMemorySigner {
                        holder_channel_pubkeys,
                        channel_parameters: None,
                        channel_keys_id,
-                       rand_bytes_unique_start,
-                       rand_bytes_index: AtomicCounter::new(),
+                       entropy_source: RandomBytes::new(rand_bytes_unique_start),
                }
        }
 
@@ -1069,10 +1079,7 @@ impl InMemorySigner {
 
 impl EntropySource for InMemorySigner {
        fn get_secure_random_bytes(&self) -> [u8; 32] {
-               let index = self.rand_bytes_index.get_increment();
-               let mut nonce = [0u8; 16];
-               nonce[..8].copy_from_slice(&index.to_be_bytes());
-               ChaCha20::get_single_block(&self.rand_bytes_unique_start, &nonce)
+               self.entropy_source.get_secure_random_bytes()
        }
 }
 
@@ -1350,8 +1357,7 @@ impl<ES: Deref> ReadableArgs<ES> for InMemorySigner where ES::Target: EntropySou
                        holder_channel_pubkeys,
                        channel_parameters: counterparty_channel_data,
                        channel_keys_id: keys_id,
-                       rand_bytes_unique_start: entropy_source.get_secure_random_bytes(),
-                       rand_bytes_index: AtomicCounter::new(),
+                       entropy_source: RandomBytes::new(entropy_source.get_secure_random_bytes()),
                })
        }
 }
@@ -1379,8 +1385,7 @@ pub struct KeysManager {
        channel_master_key: ExtendedPrivKey,
        channel_child_index: AtomicUsize,
 
-       rand_bytes_unique_start: [u8; 32],
-       rand_bytes_index: AtomicCounter,
+       entropy_source: RandomBytes,
 
        seed: [u8; 32],
        starting_time_secs: u64,
@@ -1449,8 +1454,7 @@ impl KeysManager {
                                        channel_master_key,
                                        channel_child_index: AtomicUsize::new(0),
 
-                                       rand_bytes_unique_start,
-                                       rand_bytes_index: AtomicCounter::new(),
+                                       entropy_source: RandomBytes::new(rand_bytes_unique_start),
 
                                        seed: *seed,
                                        starting_time_secs,
@@ -1631,10 +1635,7 @@ impl KeysManager {
 
 impl EntropySource for KeysManager {
        fn get_secure_random_bytes(&self) -> [u8; 32] {
-               let index = self.rand_bytes_index.get_increment();
-               let mut nonce = [0u8; 16];
-               nonce[..8].copy_from_slice(&index.to_be_bytes());
-               ChaCha20::get_single_block(&self.rand_bytes_unique_start, &nonce)
+               self.entropy_source.get_secure_random_bytes()
        }
 }
 
@@ -1888,6 +1889,35 @@ impl PhantomKeysManager {
        }
 }
 
+/// An implementation of [`EntropySource`] using ChaCha20.
+#[derive(Debug)]
+pub struct RandomBytes {
+       /// Seed from which all randomness produced is derived from.
+       seed: [u8; 32],
+       /// Tracks the number of times we've produced randomness to ensure we don't return the same
+       /// bytes twice.
+       index: AtomicCounter,
+}
+
+impl RandomBytes {
+       /// Creates a new instance using the given seed.
+       pub fn new(seed: [u8; 32]) -> Self {
+               Self {
+                       seed,
+                       index: AtomicCounter::new(),
+               }
+       }
+}
+
+impl EntropySource for RandomBytes {
+       fn get_secure_random_bytes(&self) -> [u8; 32] {
+               let index = self.index.get_increment();
+               let mut nonce = [0u8; 16];
+               nonce[..8].copy_from_slice(&index.to_be_bytes());
+               ChaCha20::get_single_block(&self.seed, &nonce)
+       }
+}
+
 // Ensure that EcdsaChannelSigner can have a vtable
 #[test]
 pub fn dyn_sign() {