Skip to content

Commit

Permalink
fix: fixed crypto lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikelle committed Nov 8, 2024
1 parent 0f2d7b9 commit f1c259f
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
24 changes: 12 additions & 12 deletions p2p/pkg/crypto/ecies.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package crypto

import (
"crypto/elliptic"
"errors"
"crypto/ecdsa"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
)

func SerializeEciesPublicKey(pub *ecies.PublicKey) []byte {
return elliptic.MarshalCompressed(elliptic.P256(), pub.X, pub.Y)
ecdsaPub := &ecdsa.PublicKey{
Curve: pub.Curve,
X: pub.X,
Y: pub.Y,
}
return crypto.CompressPubkey(ecdsaPub)
}

func DeserializeEciesPublicKey(data []byte) (*ecies.PublicKey, error) {
x, y := elliptic.UnmarshalCompressed(elliptic.P256(), data)
if x == nil {
return nil, errors.New("invalid public key")
ecdsaPub, err := crypto.DecompressPubkey(data)
if err != nil {
return nil, err
}
return &ecies.PublicKey{
X: x,
Y: y,
Curve: elliptic.P256(),
Params: ecies.ECIES_AES128_SHA256,
}, nil
return ecies.ImportECDSAPublic(ecdsaPub), nil
}
6 changes: 3 additions & 3 deletions p2p/pkg/crypto/ecies_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package crypto

import (
"crypto/elliptic"
"crypto/rand"
"testing"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
)

func TestSerializeEciesPublicKey(t *testing.T) {
privKey, err := ecies.GenerateKey(rand.Reader, elliptic.P256(), nil)
privKey, err := ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
if err != nil {
t.Fatalf("Failed to generate ECIES key: %v", err)
}
Expand All @@ -23,7 +23,7 @@ func TestSerializeEciesPublicKey(t *testing.T) {
}

func TestDeserializeEciesPublicKey(t *testing.T) {
privKey, err := ecies.GenerateKey(rand.Reader, elliptic.P256(), nil)
privKey, err := ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
if err != nil {
t.Fatalf("Failed to generate ECIES key: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions p2p/pkg/keyexchange/keyexchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keyexchange_test
import (
"bytes"
"crypto/ecdh"
"crypto/elliptic"
"crypto/rand"
"errors"
"io"
Expand Down Expand Up @@ -62,7 +61,7 @@ func TestKeyExchange_SendAndHandleTimestampMessage(t *testing.T) {
bidderStore := keysstore.New(inmemstorage.New())
providerStore := keysstore.New(inmemstorage.New())

encryptionPrivateKey, err := ecies.GenerateKey(rand.Reader, elliptic.P256(), nil)
encryptionPrivateKey, err := ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -163,7 +162,7 @@ func TestKeyExchange_Whitelist(t *testing.T) {
bidderStore := keysstore.New(inmemstorage.New())
providerStore := keysstore.New(inmemstorage.New())

encryptionPrivateKey, err := ecies.GenerateKey(rand.Reader, elliptic.P256(), nil)
encryptionPrivateKey, err := ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/pkg/keysstore/keysstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package keysstore

import (
"crypto/ecdh"
"crypto/elliptic"
"errors"
"fmt"
"math/big"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/primev/mev-commit/p2p/pkg/storage"
)
Expand Down Expand Up @@ -62,7 +62,7 @@ func eciesPrivateKeyToBytes(priv *ecies.PrivateKey) []byte {
}

func eciesPrivateKeyFromBytes(data []byte) *ecies.PrivateKey {
curve := elliptic.P256()
curve := crypto.S256()
priv := new(ecies.PrivateKey)
priv.PublicKey.Curve = curve
priv.D = new(big.Int).SetBytes(data)
Expand Down
4 changes: 2 additions & 2 deletions p2p/pkg/keysstore/keysstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package keysstore_test

import (
"crypto/ecdh"
"crypto/elliptic"
"crypto/rand"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/primev/mev-commit/p2p/pkg/keysstore"
inmem "github.com/primev/mev-commit/p2p/pkg/storage/inmem"
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestECIESPrivateKey(t *testing.T) {
assert.Nil(t, retrievedKey)

// Generate ECIES private key
privateKeyECIES, err := ecies.GenerateKey(rand.Reader, elliptic.P256(), nil)
privateKeyECIES, err := ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
assert.NoError(t, err)

// Set and get ECIES private key
Expand Down
10 changes: 5 additions & 5 deletions p2p/pkg/p2p/libp2p/internal/handshake/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"crypto/ecdh"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/libp2p/go-libp2p/core"
"github.com/primev/mev-commit/p2p/pkg/keysstore"
Expand Down Expand Up @@ -44,13 +44,13 @@ func TestHandshake(t *testing.T) {
t.Parallel()

t.Run("ok", func(t *testing.T) {
privKey1, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
privKey1, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
address1 := common.HexToAddress("0x1")
ks1 := mockkeysigner.NewMockKeySigner(privKey1, address1)
privKey2, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
privKey2, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
Expand All @@ -66,7 +66,7 @@ func TestHandshake(t *testing.T) {
if err != nil {
t.Fatal(err)
}
prvKey1, err := ecies.GenerateKey(rand.Reader, elliptic.P256(), nil)
prvKey1, err := ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestHandshake(t *testing.T) {
if err != nil {
t.Fatal(err)
}
prvKey2, err := ecies.GenerateKey(rand.Reader, elliptic.P256(), nil)
prvKey2, err := ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/pkg/p2p/libp2p/providerkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package libp2p

import (
"crypto/ecdh"
"crypto/elliptic"
"crypto/rand"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/primev/mev-commit/p2p/pkg/p2p"
)
Expand Down Expand Up @@ -38,7 +38,7 @@ func getOrSetECIESPublicKey(store Store) (*ecies.PublicKey, error) {
return nil, err
}
if prvKey == nil {
prvKey, err = ecies.GenerateKey(rand.Reader, elliptic.P256(), nil)
prvKey, err = ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/pkg/preconfirmation/preconfirmation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package preconfirmation_test
import (
"context"
"crypto/ecdh"
"crypto/elliptic"
"crypto/rand"
"io"
"log/slog"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
preconfpb "github.com/primev/mev-commit/p2p/gen/go/preconfirmation/v1"
providerapiv1 "github.com/primev/mev-commit/p2p/gen/go/providerapi/v1"
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestPreconfBidSubmission(t *testing.T) {
Type: p2p.PeerTypeBidder,
}

encryptionPrivateKey, err := ecies.GenerateKey(rand.Reader, elliptic.P256(), nil)
encryptionPrivateKey, err := ecies.GenerateKey(rand.Reader, crypto.S256(), nil)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f1c259f

Please sign in to comment.