Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs]: Confusion in code comments regarding the encryption of justice-kit #9169

Closed
oren-z0 opened this issue Oct 8, 2024 · 2 comments · Fixed by #9220
Closed

[docs]: Confusion in code comments regarding the encryption of justice-kit #9169

oren-z0 opened this issue Oct 8, 2024 · 2 comments · Fixed by #9220
Assignees
Labels
inline documentation documentation within the code P4 low prio question Support questions, not issues with the code watchtower
Milestone

Comments

@oren-z0
Copy link
Contributor

oren-z0 commented Oct 8, 2024

Background

I recently started learning about watchtowers. Many online papers explain that watchtowers receive from clients the parameters for the revocation transaction (a.k.a justice-kit?) in the following way:

  • They receive a "hint" for the breach transaction-id that they are looking for: the first half (16 bytes) of the transaction-id.
  • They receive the revocation transaction encrypted, and the encryption-key the full transaction-id of the breach commitment transaction.

This is useful for privacy reasons - no need for the watchtower to know about the revocation transaction in the optimistic use-case, where the breach transaction is not submitted.

However, I've noticed from your code that:

  • The hint is calculated from the first half of the SHA256 of the trasnaction-id:
    // NewBreachHintFromHash creates a breach hint from a transaction ID.
    func NewBreachHintFromHash(hash *chainhash.Hash) BreachHint {
    h := sha256.New()
    h.Write(hash[:])
    var hint BreachHint
    copy(hint[:], h.Sum(nil))
    return hint
    }
  • The encryption-key is calculated from SHA256(txid || txid):
    // NewBreachHintAndKeyFromHash derives a BreachHint and BreachKey from a given
    // txid in a single pass. The hint and key are computed as:
    //
    // hint = SHA256(txid)
    // key = SHA256(txid || txid)
    func NewBreachHintAndKeyFromHash(hash *chainhash.Hash) (BreachHint, BreachKey) {
    var (
    hint BreachHint
    key BreachKey
    )
    h := sha256.New()
    h.Write(hash[:])
    copy(hint[:], h.Sum(nil))
    h.Write(hash[:])
    copy(key[:], h.Sum(nil))
    return hint, key
    }

This makes sense from a cryptographic viewpoint, because revealing to the watchtower even half of the encryption-key is a bad practice, and its better to have both the hint and the encryption-key derived from hashing the transaction-id. However, I think there are some comments left from the old implementation that don't make sense:

  • // Compute the breach key as SHA256(txid).
    hint, key := blob.NewBreachHintAndKeyFromHash(&breachTxID)
    says "Compute the breach key as SHA256(txid)." instead of SHA256(txid || txid), and says nothing about the hint that is also calculated there.
  • // Verify that the breach hint matches the breach txid's prefix.
    says "Verify that the breach hint matches the breach txid's prefix." even though the hint must match the prefix of the SHA256 of the full txid, not only the txid's prefix.
  • // The decryption key for the state update should be the full
    // txid of the breaching commitment transaction.
    // The decryption key for the state update should be computed as
    // key = SHA256(txid).
    breachTxID := commitTx.TxHash()
    breachKey := blob.NewBreachKeyFromHash(&breachTxID)
    says "The decryption key for the state update should be computed as SHA256(txid)" instead of SHA256(txid || txid)
@oren-z0 oren-z0 added bug Unintended code behaviour needs triage labels Oct 8, 2024
@saubyk saubyk added inline documentation documentation within the code question Support questions, not issues with the code watchtower and removed bug Unintended code behaviour needs triage labels Oct 8, 2024
@saubyk saubyk added the P4 low prio label Oct 8, 2024
@ellemouton ellemouton changed the title [bug]: Confusion in code comments regarding the encryption of justice-kit [docs]: Confusion in code comments regarding the encryption of justice-kit Oct 16, 2024
@ellemouton
Copy link
Collaborator

@oren-z0 - are you interested in creating a PR to these comments more clear? I'd be happy to review it

@oren-z0
Copy link
Contributor Author

oren-z0 commented Oct 23, 2024

@ellemouton Done: #9220

@saubyk saubyk added this to the v0.19.0 milestone Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inline documentation documentation within the code P4 low prio question Support questions, not issues with the code watchtower
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants