From ae2c883aafe3c6a3c4e8f82f16e569409d215656 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 25 Oct 2021 12:14:19 -0700 Subject: [PATCH] Generate secret key from big number test only (#9816) * Generate secret key from big number test only * Gazelle Co-authored-by: Raul Jordan --- crypto/bls/BUILD.bazel | 2 -- crypto/bls/bls.go | 17 ------------- crypto/bls/bls_test.go | 31 ------------------------ validator/keymanager/derived/BUILD.bazel | 1 + validator/keymanager/derived/eip_test.go | 23 +++++++++++++++--- 5 files changed, 20 insertions(+), 54 deletions(-) diff --git a/crypto/bls/BUILD.bazel b/crypto/bls/BUILD.bazel index e1171e806..e918793e1 100644 --- a/crypto/bls/BUILD.bazel +++ b/crypto/bls/BUILD.bazel @@ -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", ], diff --git a/crypto/bls/bls.go b/crypto/bls/bls.go index d1ff9c042..41d93407c 100644 --- a/crypto/bls/bls.go +++ b/crypto/bls/bls.go @@ -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) diff --git a/crypto/bls/bls_test.go b/crypto/bls/bls_test.go index 03883575b..e94b33bda 100644 --- a/crypto/bls/bls_test.go +++ b/crypto/bls/bls_test.go @@ -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) - }) -} diff --git a/validator/keymanager/derived/BUILD.bazel b/validator/keymanager/derived/BUILD.bazel index c36d16e2f..ba6aee070 100644 --- a/validator/keymanager/derived/BUILD.bazel +++ b/validator/keymanager/derived/BUILD.bazel @@ -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", ], diff --git a/validator/keymanager/derived/eip_test.go b/validator/keymanager/derived/eip_test.go index 7b96fa931..3fdeef379 100644 --- a/validator/keymanager/derived/eip_test.go +++ b/validator/keymanager/derived/eip_test.go @@ -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) +}