Skip to content

Commit

Permalink
Fix Failures With Prysm Starting Up (#11103)
Browse files Browse the repository at this point in the history
  • Loading branch information
nisdas authored Jul 26, 2022
1 parent 5a4edf8 commit a7c9c76
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crypto/ecdsa/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ func ConvertFromInterfacePrivKey(privkey crypto.PrivKey) (*ecdsa.PrivateKey, err
}

func ConvertToInterfacePrivkey(privkey *ecdsa.PrivateKey) (crypto.PrivKey, error) {
return crypto.UnmarshalSecp256k1PrivateKey(privkey.D.Bytes())
privBytes := privkey.D.Bytes()
// In the event the number of bytes outputted by the big-int are less than 32,
// we append bytes to the start of the sequence for the missing most significant
// bytes.
if len(privBytes) < 32 {
privBytes = append(make([]byte, 32-len(privBytes)), privBytes...)
}
return crypto.UnmarshalSecp256k1PrivateKey(privBytes)
}

func ConvertToInterfacePubkey(pubkey *ecdsa.PublicKey) (crypto.PubKey, error) {
Expand Down
19 changes: 19 additions & 0 deletions crypto/ecdsa/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ecdsa

import (
"crypto/ecdsa"
"crypto/rand"
"math/big"
"testing"

"github.com/btcsuite/btcd/btcec/v2"
Expand All @@ -26,3 +28,20 @@ func TestConvertToInterfacePubkey(t *testing.T) {
origRawKey := gcrypto.FromECDSAPub(pubkey)
assert.DeepEqual(t, origRawKey, rawKey)
}

func TestConvertToInterfacePrivkey_HandlesShorterKeys(t *testing.T) {
priv, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
assert.NoError(t, err)
rawBytes, err := priv.Raw()
assert.NoError(t, err)
// Zero-out most significant byte so that the big int normalizes
// it by removing it.
rawBytes[0] = 0
privKey := new(ecdsa.PrivateKey)
k := new(big.Int).SetBytes(rawBytes)
privKey.D = k
privKey.Curve = gcrypto.S256()
privKey.X, privKey.Y = gcrypto.S256().ScalarBaseMult(rawBytes)
_, err = ConvertToInterfacePrivkey(privKey)
assert.NoError(t, err)
}

0 comments on commit a7c9c76

Please sign in to comment.