mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-22 11:32:09 +00:00
Generate secret key from big number test only (#9816)
* Generate secret key from big number test only * Gazelle Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
parent
ad9ef9d803
commit
ae2c883aaf
@ -15,7 +15,6 @@ go_library(
|
||||
"//crypto/bls/blst:go_default_library",
|
||||
"//crypto/bls/common:go_default_library",
|
||||
"//crypto/bls/herumi:go_default_library",
|
||||
"@com_github_pkg_errors//:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@ -28,7 +27,6 @@ go_test(
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//crypto/bls/common:go_default_library",
|
||||
"//crypto/rand:go_default_library",
|
||||
"//testing/assert:go_default_library",
|
||||
"//testing/require:go_default_library",
|
||||
],
|
||||
|
@ -4,9 +4,6 @@
|
||||
package bls
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/crypto/bls/blst"
|
||||
"github.com/prysmaticlabs/prysm/crypto/bls/common"
|
||||
"github.com/prysmaticlabs/prysm/crypto/bls/herumi"
|
||||
@ -22,20 +19,6 @@ func SecretKeyFromBytes(privKey []byte) (SecretKey, error) {
|
||||
return blst.SecretKeyFromBytes(privKey)
|
||||
}
|
||||
|
||||
// SecretKeyFromBigNum takes in a big number string and creates a BLS private key.
|
||||
func SecretKeyFromBigNum(s string) (SecretKey, error) {
|
||||
num := new(big.Int)
|
||||
num, ok := num.SetString(s, 10)
|
||||
if !ok {
|
||||
return nil, errors.New("could not set big int from string")
|
||||
}
|
||||
bts := num.Bytes()
|
||||
if len(bts) != 32 {
|
||||
return nil, errors.Errorf("provided big number string sets to a key unequal to 32 bytes: %d != 32", len(bts))
|
||||
}
|
||||
return SecretKeyFromBytes(bts)
|
||||
}
|
||||
|
||||
// PublicKeyFromBytes creates a BLS public key from a BigEndian byte slice.
|
||||
func PublicKeyFromBytes(pubKey []byte) (PublicKey, error) {
|
||||
return blst.PublicKeyFromBytes(pubKey)
|
||||
|
@ -1,12 +1,9 @@
|
||||
package bls
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/crypto/bls/common"
|
||||
"github.com/prysmaticlabs/prysm/crypto/rand"
|
||||
"github.com/prysmaticlabs/prysm/testing/assert"
|
||||
"github.com/prysmaticlabs/prysm/testing/require"
|
||||
)
|
||||
|
||||
@ -31,31 +28,3 @@ func TestDisallowZeroPublicKeys_AggregatePubkeys(t *testing.T) {
|
||||
require.Equal(t, common.ErrInfinitePubKey, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidateSecretKeyString(t *testing.T) {
|
||||
t.Run("blst", func(t *testing.T) {
|
||||
zeroNum := new(big.Int).SetUint64(0)
|
||||
_, err := SecretKeyFromBigNum(zeroNum.String())
|
||||
assert.ErrorContains(t, "provided big number string sets to a key unequal to 32 bytes", err)
|
||||
|
||||
rGen := rand.NewDeterministicGenerator()
|
||||
|
||||
randBytes := make([]byte, 40)
|
||||
n, err := rGen.Read(randBytes)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, n, len(randBytes))
|
||||
rBigNum := new(big.Int).SetBytes(randBytes)
|
||||
|
||||
// Expect larger than expected key size to fail.
|
||||
_, err = SecretKeyFromBigNum(rBigNum.String())
|
||||
assert.ErrorContains(t, "provided big number string sets to a key unequal to 32 bytes", err)
|
||||
|
||||
key, err := RandKey()
|
||||
assert.NoError(t, err)
|
||||
rBigNum = new(big.Int).SetBytes(key.Marshal())
|
||||
|
||||
// Expect correct size to pass.
|
||||
_, err = SecretKeyFromBigNum(rBigNum.String())
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
@ -44,6 +44,7 @@ go_test(
|
||||
"//testing/require:go_default_library",
|
||||
"//validator/accounts/testing:go_default_library",
|
||||
"//validator/testing:go_default_library",
|
||||
"@com_github_pkg_errors//:go_default_library",
|
||||
"@com_github_tyler_smith_go_bip39//:go_default_library",
|
||||
"@com_github_wealdtech_go_eth2_util//:go_default_library",
|
||||
],
|
||||
|
@ -3,8 +3,10 @@ package derived
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/crypto/bls"
|
||||
"github.com/prysmaticlabs/prysm/testing/assert"
|
||||
"github.com/prysmaticlabs/prysm/testing/require"
|
||||
@ -30,9 +32,9 @@ func TestDerivationFromMnemonic(t *testing.T) {
|
||||
validatingKey, err := util.PrivateKeyFromSeedAndPath(seedBytes, fmt.Sprintf("m/%d", childIndex))
|
||||
require.NoError(t, err)
|
||||
|
||||
expectedMasterSK, err := bls.SecretKeyFromBigNum(masterSK)
|
||||
expectedMasterSK, err := secretKeyFromBigNum(masterSK)
|
||||
require.NoError(t, err)
|
||||
expectedChildSK, err := bls.SecretKeyFromBigNum(childSK)
|
||||
expectedChildSK, err := secretKeyFromBigNum(childSK)
|
||||
require.NoError(t, err)
|
||||
assert.DeepEqual(t, expectedMasterSK.Marshal(), withdrawalKey.Marshal())
|
||||
assert.DeepEqual(t, expectedChildSK.Marshal(), validatingKey.Marshal())
|
||||
@ -97,12 +99,25 @@ func TestDerivationFromSeed(t *testing.T) {
|
||||
childSK, err := util.PrivateKeyFromSeedAndPath(seedBytes, fmt.Sprintf("m/%d", tt.fields.childIndex))
|
||||
require.NoError(t, err)
|
||||
|
||||
expectedMasterSK, err := bls.SecretKeyFromBigNum(tt.want.masterSK)
|
||||
expectedMasterSK, err := secretKeyFromBigNum(tt.want.masterSK)
|
||||
require.NoError(t, err)
|
||||
expectedChildSK, err := bls.SecretKeyFromBigNum(tt.want.childSK)
|
||||
expectedChildSK, err := secretKeyFromBigNum(tt.want.childSK)
|
||||
require.NoError(t, err)
|
||||
assert.DeepEqual(t, expectedMasterSK.Marshal(), masterSK.Marshal())
|
||||
assert.DeepEqual(t, expectedChildSK.Marshal(), childSK.Marshal())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func secretKeyFromBigNum(s string) (bls.SecretKey, error) {
|
||||
num := new(big.Int)
|
||||
num, ok := num.SetString(s, 10)
|
||||
if !ok {
|
||||
return nil, errors.New("could not set big int from string")
|
||||
}
|
||||
bts := num.Bytes()
|
||||
if len(bts) != 32 {
|
||||
return nil, errors.Errorf("provided big number string sets to a key unequal to 32 bytes: %d != 32", len(bts))
|
||||
}
|
||||
return bls.SecretKeyFromBytes(bts)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user