Significantly clarify key derivation and expose methods referenced
[rust-lightning] / lightning / src / ln / chan_utils.rs
index d50cc1f41b6f10e7f94be49c9244692f68130cf3..53a6e25f764cfd6fe75c0aa99abf08d08ea34246 100644 (file)
@@ -171,9 +171,11 @@ impl Readable for CounterpartyCommitmentSecrets {
        }
 }
 
-/// Derives a per-commitment-transaction private key (eg an htlc key, payment key or delayed_payment
-/// key) from the base.
-/// private key for that type of key and the per_commitment_point (available in TxCreationKeys)
+/// Derives a per-commitment-transaction private key (eg an htlc key or delayed_payment key)
+/// from the base secret and the per_commitment_point.
+///
+/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
+/// generated (ie our own).
 pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
        let mut sha = Sha256::engine();
        sha.input(&per_commitment_point.serialize());
@@ -185,7 +187,13 @@ pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_co
        Ok(key)
 }
 
-pub(crate) fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
+/// Derives a per-commitment-transaction public key (eg an htlc key or a delayed_payment key)
+/// from the base point and the per_commitment_key. This is the public equivalent of
+/// derive_private_key - using only public keys to derive a public key instead of private keys.
+///
+/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
+/// generated (ie our own).
+pub fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
        let mut sha = Sha256::engine();
        sha.input(&per_commitment_point.serialize());
        sha.input(&base_point.serialize());
@@ -195,7 +203,8 @@ pub(crate) fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>,
        base_point.combine(&hashkey)
 }
 
-/// Derives a revocation key from its constituent parts.
+/// Derives a per-commitment-transaction revocation key from its constituent parts.
+///
 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
 /// generated (ie our own).
 pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_secret: &SecretKey, revocation_base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
@@ -225,7 +234,13 @@ pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1
        Ok(part_a)
 }
 
-pub(crate) fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, revocation_base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
+/// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
+/// the public equivalend of derive_private_revocation_key - using only public keys to derive a
+/// public key instead of private keys.
+///
+/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
+/// generated (ie our own).
+pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, revocation_base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
        let rev_append_commit_hash_key = {
                let mut sha = Sha256::engine();
                sha.input(&revocation_base_point.serialize());
@@ -274,9 +289,9 @@ pub struct ChannelPublicKeys {
        /// on-chain channel lock-in 2-of-2 multisig output.
        pub funding_pubkey: PublicKey,
        /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
-       /// revocation keys. The per-commitment revocation private key is then revealed by the owner of
-       /// a commitment transaction so that their counterparty can claim all available funds if they
-       /// broadcast an old state.
+       /// revocation keys. This is combined with the per-commitment-secret generated by the
+       /// counterparty to create a secret which the counterparty can reveal to revoke previous
+       /// states.
        pub revocation_basepoint: PublicKey,
        /// The public key which receives our immediately spendable primary channel balance in
        /// remote-broadcasted commitment transactions. This key is static across every commitment