• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15155511119

21 May 2025 06:52AM UTC coverage: 57.389% (-11.6%) from 68.996%
15155511119

Pull #9844

github

web-flow
Merge 8658c8597 into c52a6ddeb
Pull Request #9844: Refactor Payment PR 3

346 of 493 new or added lines in 4 files covered. (70.18%)

30172 existing lines in 456 files now uncovered.

95441 of 166305 relevant lines covered (57.39%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/keychain/ecdh.go
1
package keychain
2

3
import (
4
        "crypto/sha256"
5

6
        "github.com/btcsuite/btcd/btcec/v2"
7
)
8

9
// NewPubKeyECDH wraps the given key of the key ring so it adheres to the
10
// SingleKeyECDH interface.
11
func NewPubKeyECDH(keyDesc KeyDescriptor, ecdh ECDHRing) *PubKeyECDH {
1✔
12
        return &PubKeyECDH{
1✔
13
                keyDesc: keyDesc,
1✔
14
                ecdh:    ecdh,
1✔
15
        }
1✔
16
}
1✔
17

18
// PubKeyECDH is an implementation of the SingleKeyECDH interface. It wraps an
19
// ECDH key ring so it can perform ECDH shared key generation against a single
20
// abstracted away private key.
21
type PubKeyECDH struct {
22
        keyDesc KeyDescriptor
23
        ecdh    ECDHRing
24
}
25

26
// PubKey returns the public key of the private key that is abstracted away by
27
// the interface.
28
//
29
// NOTE: This is part of the SingleKeyECDH interface.
30
func (p *PubKeyECDH) PubKey() *btcec.PublicKey {
1✔
31
        return p.keyDesc.PubKey
1✔
32
}
1✔
33

34
// ECDH performs a scalar multiplication (ECDH-like operation) between the
35
// abstracted private key and a remote public key. The output returned will be
36
// the sha256 of the resulting shared point serialized in compressed format. If
37
// k is our private key, and P is the public key, we perform the following
38
// operation:
39
//
40
//        sx := k*P
41
//        s := sha256(sx.SerializeCompressed())
42
//
43
// NOTE: This is part of the SingleKeyECDH interface.
44
func (p *PubKeyECDH) ECDH(pubKey *btcec.PublicKey) ([32]byte, error) {
1✔
45
        return p.ecdh.ECDH(p.keyDesc, pubKey)
1✔
46
}
1✔
47

48
// PrivKeyECDH is an implementation of the SingleKeyECDH in which we do have the
49
// full private key. This can be used to wrap a temporary key to conform to the
50
// SingleKeyECDH interface.
51
type PrivKeyECDH struct {
52
        // PrivKey is the private key that is used for the ECDH operation.
53
        PrivKey *btcec.PrivateKey
54
}
55

56
// PubKey returns the public key of the private key that is abstracted away by
57
// the interface.
58
//
59
// NOTE: This is part of the SingleKeyECDH interface.
UNCOV
60
func (p *PrivKeyECDH) PubKey() *btcec.PublicKey {
×
UNCOV
61
        return p.PrivKey.PubKey()
×
UNCOV
62
}
×
63

64
// ECDH performs a scalar multiplication (ECDH-like operation) between the
65
// abstracted private key and a remote public key. The output returned will be
66
// the sha256 of the resulting shared point serialized in compressed format. If
67
// k is our private key, and P is the public key, we perform the following
68
// operation:
69
//
70
//        sx := k*P
71
//        s := sha256(sx.SerializeCompressed())
72
//
73
// NOTE: This is part of the SingleKeyECDH interface.
74
func (p *PrivKeyECDH) ECDH(pub *btcec.PublicKey) ([32]byte, error) {
1✔
75
        var (
1✔
76
                pubJacobian btcec.JacobianPoint
1✔
77
                s           btcec.JacobianPoint
1✔
78
        )
1✔
79
        pub.AsJacobian(&pubJacobian)
1✔
80

1✔
81
        btcec.ScalarMultNonConst(&p.PrivKey.Key, &pubJacobian, &s)
1✔
82
        s.ToAffine()
1✔
83
        sPubKey := btcec.NewPublicKey(&s.X, &s.Y)
1✔
84
        return sha256.Sum256(sPubKey.SerializeCompressed()), nil
1✔
85
}
1✔
86

87
var _ SingleKeyECDH = (*PubKeyECDH)(nil)
88
var _ SingleKeyECDH = (*PrivKeyECDH)(nil)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc