Fix Failures With Prysm Starting Up (#11103)

This commit is contained in:
Nishant Das 2022-07-26 21:54:49 +08:00 committed by GitHub
parent 5a4edf897f
commit a7c9c76b18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -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) {

View File

@ -2,6 +2,8 @@ package ecdsa
import (
"crypto/ecdsa"
"crypto/rand"
"math/big"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
@ -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)
}