Skip to content

Commit 34aba2b

Browse files
committed
path: add NextEphemeralPriv helper
This helper is the private key equivilant of `NextEphemeral`.
1 parent e3f2244 commit 34aba2b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

path.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/btcsuite/btcd/btcec/v2"
8+
secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
89
)
910

1011
const (
@@ -281,3 +282,25 @@ func NextEphemeral(privKey SingleKeyECDH,
281282

282283
return nextEphem, nil
283284
}
285+
286+
// NextEphemeralPriv computes the next ephemeral priv key given the current
287+
// ephemeral private key and a node's public key.
288+
func NextEphemeralPriv(ephemPriv *PrivKeyECDH,
289+
pubKey *btcec.PublicKey) (*btcec.PrivateKey, error) {
290+
291+
// ss = e1 * P
292+
ss, err := ephemPriv.ECDH(pubKey)
293+
if err != nil {
294+
return nil, err
295+
}
296+
297+
// bf = H( E1 || ss )
298+
blindingFactor := computeBlindingFactor(ephemPriv.PubKey(), ss[:])
299+
300+
// e2 = e1 * bf
301+
var nextPrivEphem btcec.ModNScalar
302+
nextPrivEphem.Set(&ephemPriv.PrivKey.Key)
303+
nextPrivEphem.Mul(&blindingFactor)
304+
305+
return secp.NewPrivateKey(&nextPrivEphem), nil
306+
}

0 commit comments

Comments
 (0)