act3からlightning messageへ

act3が終わった段階で、両方が暗号化鍵を保持している状態になる。それは、最後のhkdfで取得した鍵になる。

rk, sk = HKDF(ck, zero)

where zero is a zero-length plaintext, rk is the key to be used by the responder to decrypt the messages sent by the initiator, and sk is the key to be used by the responder to encrypt messages to the initiator
The final encryption keys, to be used for sending and receiving messages for the duration of the session, are generated.

俺のpackageでいえば、

mixKey(input) {  
    const info = EMPTY;
    const secret = input;
    const salt = this.chain;

    [this.chain, this.temp] = expand(secret, salt, info);

    this.initKey(this.temp);

    return this;
  }

のthis.chain, this.tempがそれに当たる。

Lightning Message Specification

lightning networkのmessageは、まずざっくり下記の形式を取る。

+-------------------------------
|2-byte encrypted message length|
+-------------------------------
|  16-byte MAC of the encrypted |
|        message length         |
+-------------------------------
|                               |
|                               |
|     encrypted Lightning       |
|            message            |
|                               |
+-------------------------------
|     16-byte MAC of the        |
|      Lightning message        |
+-------------------------------

message exchange

メッセージの暗号化には、sk(this.temp)とnonceが用いられる。解読にはrk(this.chain)が用いられる。メッセージ送信が500回を超えるとhkdfを用いたkey rotationが発生する。

上記のencrypted Lightning messageがここからの鍵になる。

Lightning Message Format

解読したあとのmessageは、下記の形式となる。

  1. type: a 2-byte big-endian field indicating the type of message
  2. payload: a variable-length payload that comprises the remainder of the message and that conforms to a format matching the type

ちなみに、なぜbig-endianかというと、btcのpub keyとかもそれで統一しているから。

そして、このタイプとペイロードによって、lightningがいよいよ動き出せるわけだ。