mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
Update Prysm Libp2p Dependencies (#10958)
* add all changes in * fix issues * fix build * remove curve check * fix tool * add test * add tidy * fmt Co-authored-by: Radosław Kapka <rkapka@wp.pl> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
27
crypto/ecdsa/BUILD.bazel
Normal file
27
crypto/ecdsa/BUILD.bazel
Normal file
@@ -0,0 +1,27 @@
|
||||
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["utils.go"],
|
||||
importpath = "github.com/prysmaticlabs/prysm/crypto/ecdsa",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"@com_github_btcsuite_btcd_btcec_v2//:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//crypto:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_core//crypto:go_default_library",
|
||||
"@com_github_pkg_errors//:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["utils_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//testing/assert:go_default_library",
|
||||
"//testing/require:go_default_library",
|
||||
"@com_github_btcsuite_btcd_btcec_v2//:go_default_library",
|
||||
"@com_github_ethereum_go_ethereum//crypto:go_default_library",
|
||||
"@com_github_libp2p_go_libp2p_core//crypto:go_default_library",
|
||||
],
|
||||
)
|
||||
44
crypto/ecdsa/utils.go
Normal file
44
crypto/ecdsa/utils.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package ecdsa
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"math/big"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
gcrypto "github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func ConvertFromInterfacePrivKey(privkey crypto.PrivKey) (*ecdsa.PrivateKey, error) {
|
||||
secpKey := (privkey.(*crypto.Secp256k1PrivateKey))
|
||||
rawKey, err := secpKey.Raw()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
privKey := new(ecdsa.PrivateKey)
|
||||
k := new(big.Int).SetBytes(rawKey)
|
||||
privKey.D = k
|
||||
privKey.Curve = gcrypto.S256() // Temporary hack, so libp2p Secp256k1 is recognized as geth Secp256k1 in disc v5.1.
|
||||
privKey.X, privKey.Y = gcrypto.S256().ScalarBaseMult(rawKey)
|
||||
return privKey, nil
|
||||
}
|
||||
|
||||
func ConvertToInterfacePrivkey(privkey *ecdsa.PrivateKey) (crypto.PrivKey, error) {
|
||||
return crypto.UnmarshalSecp256k1PrivateKey(privkey.D.Bytes())
|
||||
}
|
||||
|
||||
func ConvertToInterfacePubkey(pubkey *ecdsa.PublicKey) (crypto.PubKey, error) {
|
||||
xVal, yVal := new(btcec.FieldVal), new(btcec.FieldVal)
|
||||
if xVal.SetByteSlice(pubkey.X.Bytes()) {
|
||||
return nil, errors.Errorf("X value overflows")
|
||||
}
|
||||
if yVal.SetByteSlice(pubkey.Y.Bytes()) {
|
||||
return nil, errors.Errorf("Y value overflows")
|
||||
}
|
||||
newKey := crypto.PubKey((*crypto.Secp256k1PublicKey)(btcec.NewPublicKey(xVal, yVal)))
|
||||
// Zero out temporary values.
|
||||
xVal.Zero()
|
||||
yVal.Zero()
|
||||
return newKey, nil
|
||||
}
|
||||
28
crypto/ecdsa/utils_test.go
Normal file
28
crypto/ecdsa/utils_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package ecdsa
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
gcrypto "github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
"github.com/prysmaticlabs/prysm/testing/assert"
|
||||
"github.com/prysmaticlabs/prysm/testing/require"
|
||||
)
|
||||
|
||||
func TestConvertToInterfacePubkey(t *testing.T) {
|
||||
privKey, err := gcrypto.GenerateKey()
|
||||
require.NoError(t, err)
|
||||
|
||||
pubkey, ok := privKey.Public().(*ecdsa.PublicKey)
|
||||
require.NotEqual(t, false, ok)
|
||||
|
||||
altPubkey, err := ConvertToInterfacePubkey(pubkey)
|
||||
require.NoError(t, err)
|
||||
|
||||
nKey := *(altPubkey.(*crypto.Secp256k1PublicKey))
|
||||
rawKey := btcec.PublicKey(nKey).SerializeUncompressed()
|
||||
origRawKey := gcrypto.FromECDSAPub(pubkey)
|
||||
assert.DeepEqual(t, origRawKey, rawKey)
|
||||
}
|
||||
Reference in New Issue
Block a user