Skip to content

Commit eebab40

Browse files
authored
Merge pull request #3004 from TheBlueMatt/2024-04-2761-followups
#2761 Followups
2 parents 9a438ee + 6ab91cb commit eebab40

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

ci/rustfmt.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ set -eox pipefail
44
# Generate initial exclusion list
55
#find . -name '*.rs' -type f |sort >rustfmt_excluded_files
66

7+
# The +rustversion syntax only works with rustup-installed rust toolchains,
8+
# not with any distro-provided ones. Thus, we check for a rustup install and
9+
# only pass +1.63.0 if we find one.
10+
VERS=""
11+
[ "$(which rustup)" != "" ] && VERS="+1.63.0"
12+
713
# Run fmt
814
TMP_FILE=$(mktemp)
915
find . -name '*.rs' -type f |sort >$TMP_FILE
1016
for file in $(comm -23 $TMP_FILE rustfmt_excluded_files); do
1117
echo "Checking formatting of $file"
12-
rustfmt +1.63.0 --check $file
18+
rustfmt $VERS --check $file
1319
done

lightning/src/ln/channel_keys.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,30 @@ macro_rules! doc_comment {
3131
};
3232
}
3333
macro_rules! basepoint_impl {
34-
($BasepointT:ty) => {
34+
($BasepointT:ty $(, $KeyName: expr)?) => {
3535
impl $BasepointT {
3636
/// Get inner Public Key
3737
pub fn to_public_key(&self) -> PublicKey {
3838
self.0
3939
}
4040

41-
/// Derives a per-commitment-transaction (eg an htlc key or delayed_payment key) private key addition tweak
42-
/// from a basepoint and a per_commitment_point:
43-
/// `privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)`
44-
/// This calculates the hash part in the tweak derivation process, which is used to ensure
45-
/// that each key is unique and cannot be guessed by an external party. It is equivalent
46-
/// to the `from_basepoint` method, but without the addition operation, providing just the
47-
/// tweak from the hash of the per_commitment_point and the basepoint.
48-
pub fn derive_add_tweak(&self, per_commitment_point: &PublicKey) -> [u8; 32] {
49-
let mut sha = Sha256::engine();
50-
sha.input(&per_commitment_point.serialize());
51-
sha.input(&self.to_public_key().serialize());
52-
Sha256::from_engine(sha).to_byte_array()
53-
}
41+
$(doc_comment!(
42+
concat!(
43+
"Derives the \"tweak\" used in calculate [`", $KeyName, "::from_basepoint`].\n",
44+
"\n",
45+
"[`", $KeyName, "::from_basepoint`] calculates a private key as:\n",
46+
"`privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)`\n",
47+
"\n",
48+
"This calculates the hash part in the tweak derivation process, which is used to\n",
49+
"ensure that each key is unique and cannot be guessed by an external party."
50+
),
51+
pub fn derive_add_tweak(&self, per_commitment_point: &PublicKey) -> Sha256 {
52+
let mut sha = Sha256::engine();
53+
sha.input(&per_commitment_point.serialize());
54+
sha.input(&self.to_public_key().serialize());
55+
Sha256::from_engine(sha)
56+
});
57+
)?
5458
}
5559

5660
impl From<PublicKey> for $BasepointT {
@@ -110,7 +114,7 @@ macro_rules! key_read_write {
110114
/// state broadcasted was previously revoked.
111115
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
112116
pub struct DelayedPaymentBasepoint(pub PublicKey);
113-
basepoint_impl!(DelayedPaymentBasepoint);
117+
basepoint_impl!(DelayedPaymentBasepoint, "DelayedPaymentKey");
114118
key_read_write!(DelayedPaymentBasepoint);
115119

116120
/// A derived key built from a [`DelayedPaymentBasepoint`] and `per_commitment_point`.
@@ -137,7 +141,7 @@ key_read_write!(DelayedPaymentKey);
137141
/// Thus, both channel counterparties' HTLC keys will appears in each HTLC output's script.
138142
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
139143
pub struct HtlcBasepoint(pub PublicKey);
140-
basepoint_impl!(HtlcBasepoint);
144+
basepoint_impl!(HtlcBasepoint, "HtlcKey");
141145
key_read_write!(HtlcBasepoint);
142146

143147
/// A derived key built from a [`HtlcBasepoint`] and `per_commitment_point`.
@@ -166,18 +170,20 @@ fn derive_public_key<T: secp256k1::Signing>(
166170
let mut sha = Sha256::engine();
167171
sha.input(&per_commitment_point.serialize());
168172
sha.input(&base_point.serialize());
169-
let res = Sha256::from_engine(sha).to_byte_array();
173+
let res = Sha256::from_engine(sha);
170174

171175
add_public_key_tweak(secp_ctx, base_point, &res)
172176
}
173177

174178
/// Adds a tweak to a public key to derive a new public key.
179+
///
180+
/// May panic if `tweak` is not the output of a SHA-256 hash.
175181
pub fn add_public_key_tweak<T: secp256k1::Signing>(
176-
secp_ctx: &Secp256k1<T>, base_point: &PublicKey, tweak: &[u8; 32],
182+
secp_ctx: &Secp256k1<T>, base_point: &PublicKey, tweak: &Sha256,
177183
) -> PublicKey {
178184
let hashkey = PublicKey::from_secret_key(
179185
&secp_ctx,
180-
&SecretKey::from_slice(tweak)
186+
&SecretKey::from_slice(tweak.as_byte_array())
181187
.expect("Hashes should always be valid keys unless SHA-256 is broken"),
182188
);
183189
base_point.combine(&hashkey)

lightning/src/sign/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ pub struct DelayedPaymentOutputDescriptor {
110110
pub channel_keys_id: [u8; 32],
111111
/// The value of the channel which this output originated from, possibly indirectly.
112112
pub channel_value_satoshis: u64,
113-
/// The channel public keys and other parameters needed to generate a spending transaction or to provide to a re-derived signer through
114-
/// [`ChannelSigner::provide_channel_parameters`].
113+
/// The channel public keys and other parameters needed to generate a spending transaction or
114+
/// to provide to a re-derived signer through [`ChannelSigner::provide_channel_parameters`].
115115
///
116116
/// Added as optional, but always `Some` if the descriptor was produced in v0.0.123 or later.
117117
pub channel_transaction_parameters: Option<ChannelTransactionParameters>,
@@ -405,7 +405,7 @@ impl SpendableOutputDescriptor {
405405
subtype: 0,
406406
key: "add_tweak".as_bytes().to_vec(),
407407
},
408-
add_tweak.to_vec(),
408+
add_tweak.as_byte_array().to_vec(),
409409
)]
410410
.into_iter()
411411
.collect()

rustfmt_excluded_files

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@
190190
./lightning/src/ln/chanmon_update_fail_tests.rs
191191
./lightning/src/ln/channel.rs
192192
./lightning/src/ln/channel_id.rs
193-
./lightning/src/ln/channel_keys.rs
194193
./lightning/src/ln/channelmanager.rs
195194
./lightning/src/ln/features.rs
196195
./lightning/src/ln/functional_test_utils.rs

0 commit comments

Comments
 (0)