refactor(crypto): use extracted crypto library
This commit is contained in:
parent
8418464d91
commit
14ecd1929b
@ -7,7 +7,7 @@ import (
|
||||
|
||||
bls "github.com/filecoin-project/filecoin-ffi"
|
||||
"github.com/filecoin-project/go-address"
|
||||
"github.com/filecoin-project/lotus/lib/crypto"
|
||||
"github.com/filecoin-project/go-crypto"
|
||||
"github.com/minio/blake2b-simd"
|
||||
)
|
||||
|
||||
|
@ -19,13 +19,13 @@ import (
|
||||
vtypes "github.com/filecoin-project/chain-validation/pkg/state/types"
|
||||
|
||||
"github.com/filecoin-project/go-address"
|
||||
"github.com/filecoin-project/go-crypto"
|
||||
"github.com/filecoin-project/lotus/chain/actors"
|
||||
"github.com/filecoin-project/lotus/chain/gen"
|
||||
"github.com/filecoin-project/lotus/chain/state"
|
||||
"github.com/filecoin-project/lotus/chain/types"
|
||||
"github.com/filecoin-project/lotus/chain/vm"
|
||||
"github.com/filecoin-project/lotus/chain/wallet"
|
||||
"github.com/filecoin-project/lotus/lib/crypto"
|
||||
)
|
||||
|
||||
type StateWrapper struct {
|
||||
|
@ -14,8 +14,8 @@ import (
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/filecoin-project/go-address"
|
||||
"github.com/filecoin-project/go-crypto"
|
||||
"github.com/filecoin-project/lotus/chain/types"
|
||||
"github.com/filecoin-project/lotus/lib/crypto"
|
||||
)
|
||||
|
||||
var log = logging.Logger("wallet")
|
||||
|
2
go.mod
2
go.mod
@ -14,6 +14,7 @@ require (
|
||||
github.com/filecoin-project/filecoin-ffi v0.0.0-20191213130254-f261762ff8ed
|
||||
github.com/filecoin-project/go-address v0.0.0-20191219011437-af739c490b4f
|
||||
github.com/filecoin-project/go-amt-ipld v0.0.0-20191205011053-79efc22d6cdc
|
||||
github.com/filecoin-project/go-crypto v0.0.0-20191218222705-effae4ea9f03
|
||||
github.com/filecoin-project/go-paramfetch v0.0.0-20200102181131-b20d579f2878
|
||||
github.com/gbrlsnchs/jwt/v3 v3.0.0-beta.1
|
||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||
@ -49,7 +50,6 @@ require (
|
||||
github.com/ipfs/go-path v0.0.7
|
||||
github.com/ipfs/go-unixfs v0.2.2-0.20190827150610-868af2e9e5cb
|
||||
github.com/ipld/go-ipld-prime v0.0.2-0.20191025154717-8dff1cbec43b
|
||||
github.com/ipsn/go-secp256k1 v0.0.0-20180726113642-9d62b9f0bc52
|
||||
github.com/libp2p/go-libp2p v0.4.2
|
||||
github.com/libp2p/go-libp2p-circuit v0.1.4
|
||||
github.com/libp2p/go-libp2p-connmgr v0.1.0
|
||||
|
@ -1,70 +0,0 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
|
||||
secp256k1 "github.com/ipsn/go-secp256k1"
|
||||
)
|
||||
|
||||
// PrivateKeyBytes is the size of a serialized private key.
|
||||
const PrivateKeyBytes = 32
|
||||
|
||||
// PublicKeyBytes is the size of a serialized public key.
|
||||
const PublicKeyBytes = 65
|
||||
|
||||
// PublicKey returns the public key for this private key.
|
||||
func PublicKey(sk []byte) []byte {
|
||||
x, y := secp256k1.S256().ScalarBaseMult(sk)
|
||||
return elliptic.Marshal(secp256k1.S256(), x, y)
|
||||
}
|
||||
|
||||
// Sign signs the given message, which must be 32 bytes long.
|
||||
func Sign(sk, msg []byte) ([]byte, error) {
|
||||
return secp256k1.Sign(msg, sk)
|
||||
}
|
||||
|
||||
// Equals compares two private key for equality and returns true if they are the same.
|
||||
func Equals(sk, other []byte) bool {
|
||||
return bytes.Equal(sk, other)
|
||||
}
|
||||
|
||||
// Verify checks the given signature and returns true if it is valid.
|
||||
func Verify(pk, msg, signature []byte) bool {
|
||||
if len(signature) == 65 {
|
||||
// Drop the V (1byte) in [R | S | V] style signatures.
|
||||
// The V (1byte) is the recovery bit and is not apart of the signature verification.
|
||||
return secp256k1.VerifySignature(pk[:], msg, signature[:len(signature)-1])
|
||||
}
|
||||
|
||||
return secp256k1.VerifySignature(pk[:], msg, signature)
|
||||
}
|
||||
|
||||
// GenerateKeyFromSeed generates a new key from the given reader.
|
||||
func GenerateKeyFromSeed(seed io.Reader) ([]byte, error) {
|
||||
key, err := ecdsa.GenerateKey(secp256k1.S256(), seed)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
privkey := make([]byte, PrivateKeyBytes)
|
||||
blob := key.D.Bytes()
|
||||
|
||||
// the length is guaranteed to be fixed, given the serialization rules for secp2561k curve points.
|
||||
copy(privkey[PrivateKeyBytes-len(blob):], blob)
|
||||
|
||||
return privkey, nil
|
||||
}
|
||||
|
||||
// GenerateKey creates a new key using secure randomness from crypto.rand.
|
||||
func GenerateKey() ([]byte, error) {
|
||||
return GenerateKeyFromSeed(rand.Reader)
|
||||
}
|
||||
|
||||
// EcRecover recovers the public key from a message, signature pair.
|
||||
func EcRecover(msg, signature []byte) ([]byte, error) {
|
||||
return secp256k1.RecoverPubkey(msg, signature)
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
package crypto_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/filecoin-project/lotus/lib/crypto"
|
||||
)
|
||||
|
||||
func TestGenerateKey(t *testing.T) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
sk, err := crypto.GenerateKey()
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, len(sk), 32)
|
||||
|
||||
msg := make([]byte, 32)
|
||||
for i := 0; i < len(msg); i++ {
|
||||
msg[i] = byte(i)
|
||||
}
|
||||
|
||||
digest, err := crypto.Sign(sk, msg)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(digest), 65)
|
||||
pk := crypto.PublicKey(sk)
|
||||
|
||||
// valid signature
|
||||
assert.True(t, crypto.Verify(pk, msg, digest))
|
||||
|
||||
// invalid signature - different message (too short)
|
||||
assert.False(t, crypto.Verify(pk, msg[3:], digest))
|
||||
|
||||
// invalid signature - different message
|
||||
msg2 := make([]byte, 32)
|
||||
copy(msg2, msg)
|
||||
rand.Shuffle(len(msg2), func(i, j int) { msg2[i], msg2[j] = msg2[j], msg2[i] })
|
||||
assert.False(t, crypto.Verify(pk, msg2, digest))
|
||||
|
||||
// invalid signature - different digest
|
||||
digest2 := make([]byte, 65)
|
||||
copy(digest2, digest)
|
||||
rand.Shuffle(len(digest2), func(i, j int) { digest2[i], digest2[j] = digest2[j], digest2[i] })
|
||||
assert.False(t, crypto.Verify(pk, msg, digest2))
|
||||
|
||||
// invalid signature - digest too short
|
||||
assert.False(t, crypto.Verify(pk, msg, digest[3:]))
|
||||
assert.False(t, crypto.Verify(pk, msg, digest[:29]))
|
||||
|
||||
// invalid signature - digest too long
|
||||
digest3 := make([]byte, 70)
|
||||
copy(digest3, digest)
|
||||
assert.False(t, crypto.Verify(pk, msg, digest3))
|
||||
|
||||
recovered, err := crypto.EcRecover(msg, digest)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, recovered, crypto.PublicKey(sk))
|
||||
}
|
Loading…
Reference in New Issue
Block a user