forked from lightningnetwork/lightning-onion
-
Notifications
You must be signed in to change notification settings - Fork 0
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
lightning-onion: route blinding implementation PR review notes #1
Open
calvinrzachman
wants to merge
4
commits into
master
Choose a base branch
from
route-blinding-elle-pr-review
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
606afbf
cmd+go.mod: upgrade cli tool to use urfave lib
ellemouton ee90b33
blinding: add blind route and decrypt data funcs
ellemouton 2f7de78
multi: handle route blinding in ProcessOnionPacket
ellemouton 91a33c8
cmd: add blinded path helper commands
ellemouton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package sphinx | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/btcsuite/btcd/btcec/v2" | ||
) | ||
|
||
const routeBlindingHMACKey = "blinded_node_id" | ||
|
||
// BlindedPath represents all the data that the creator of a blinded path must | ||
// transmit to the builder of route that will send to this path. | ||
type BlindedPath struct { | ||
// IntroductionPoint is the real node ID of the first hop in the blinded | ||
// path. The sender should be able to find this node in the network | ||
// graph and route to it. | ||
IntroductionPoint *btcec.PublicKey | ||
|
||
// BlindingPoint is the first ephemeral blinding point. This is the | ||
// point that the introduction node will use in order to create a shared | ||
// secret with the builder of the blinded route. This point will need | ||
// to be communicated to the introduction point by the sender in some | ||
// way. | ||
BlindingPoint *btcec.PublicKey | ||
|
||
// BlindedHops is a list of ordered blinded node IDs representing the | ||
// blinded route. Note that the first node ID is the blinded node ID of | ||
// the introduction point which does not strictly need to be transmitted | ||
// to the sender. | ||
BlindedHops []*btcec.PublicKey | ||
|
||
// EncryptedData is a list of encrypted_data byte arrays. Each entry | ||
// has been encrypted by the blinded route creator for a hop in the | ||
// blinded route. | ||
EncryptedData [][]byte | ||
} | ||
|
||
// BlindedPathHop represents a single hop in a blinded path. It is the data that | ||
// the blinded route creator must provide about the hop in order for the | ||
// BlindedPath to be constructed. | ||
type BlindedPathHop struct { | ||
// NodePub is the real node ID of this hop. | ||
NodePub *btcec.PublicKey | ||
|
||
// Payload is the cleartext blob that should be encrypted for the hop. | ||
Payload []byte | ||
} | ||
|
||
// BuildBlindedPath creates a new BlindedPath from a list of BlindedPathHops and | ||
// a session key. | ||
func BuildBlindedPath(sessionKey *btcec.PrivateKey, | ||
paymentPath []*BlindedPathHop) (*BlindedPath, error) { | ||
|
||
if len(paymentPath) < 1 { | ||
return nil, fmt.Errorf("at least 1 hop required to create a " + | ||
"blinded path") | ||
} | ||
|
||
bp := BlindedPath{ | ||
IntroductionPoint: paymentPath[0].NodePub, | ||
BlindingPoint: sessionKey.PubKey(), | ||
EncryptedData: make([][]byte, len(paymentPath)), | ||
} | ||
|
||
keys := make([]*btcec.PublicKey, len(paymentPath)) | ||
for i, p := range paymentPath { | ||
keys[i] = p.NodePub | ||
} | ||
|
||
hopSharedSecrets, err := generateSharedSecrets(keys, sessionKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("error generating shared secret: %v", | ||
err) | ||
} | ||
|
||
for i, hop := range paymentPath { | ||
blindingFactorBytes := generateKey( | ||
routeBlindingHMACKey, &hopSharedSecrets[i], | ||
) | ||
|
||
var blindingFactor btcec.ModNScalar | ||
blindingFactor.SetBytes(&blindingFactorBytes) | ||
|
||
blindedNodeID := blindGroupElement(hop.NodePub, blindingFactor) | ||
bp.BlindedHops = append(bp.BlindedHops, blindedNodeID) | ||
|
||
rhoKey := generateKey("rho", &hopSharedSecrets[i]) | ||
enc, err := chacha20polyEncrypt(rhoKey[:], hop.Payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bp.EncryptedData[i] = enc | ||
} | ||
|
||
return &bp, nil | ||
} | ||
|
||
// DecryptBlindedData decrypts the data encrypted by the creator of the blinded | ||
// route. | ||
func DecryptBlindedData(privKey SingleKeyECDH, ephemPub *btcec.PublicKey, | ||
encryptedData []byte) ([]byte, error) { | ||
|
||
ss, err := privKey.ECDH(ephemPub) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ssHash := Hash256(ss) | ||
rho := generateKey("rho", &ssHash) | ||
return chacha20polyDecrypt(rho[:], encryptedData) | ||
} | ||
|
||
// NextEphemeral computes the next ephemeral key given the current ephemeral | ||
// key and this node's private key. | ||
func NextEphemeral(privKey SingleKeyECDH, | ||
ephemPub *btcec.PublicKey) (*btcec.PublicKey, error) { | ||
|
||
ss, err := privKey.ECDH(ephemPub) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
blindingFactor := computeBlindingFactor(ephemPub, ss[:]) | ||
nextEphem := blindGroupElement(ephemPub, blindingFactor) | ||
|
||
return nextEphem, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ This checks out with our implementation. TODO: remove before submitting review.