2020-01-30 23:48:25 +00:00
|
|
|
package secp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/minio/blake2b-simd"
|
|
|
|
|
2020-01-30 23:48:25 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2022-09-29 20:46:59 +00:00
|
|
|
gocrypto "github.com/filecoin-project/go-crypto"
|
2020-09-07 03:49:10 +00:00
|
|
|
crypto2 "github.com/filecoin-project/go-state-types/crypto"
|
2020-02-12 23:52:36 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
2020-01-30 23:48:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type secpSigner struct{}
|
|
|
|
|
|
|
|
func (secpSigner) GenPrivate() ([]byte, error) {
|
2022-09-29 20:46:59 +00:00
|
|
|
priv, err := gocrypto.GenerateKey()
|
2020-01-30 23:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return priv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (secpSigner) ToPublic(pk []byte) ([]byte, error) {
|
2022-09-29 20:46:59 +00:00
|
|
|
return gocrypto.PublicKey(pk), nil
|
2020-01-30 23:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (secpSigner) Sign(pk []byte, msg []byte) ([]byte, error) {
|
|
|
|
b2sum := blake2b.Sum256(msg)
|
2022-09-29 20:46:59 +00:00
|
|
|
sig, err := gocrypto.Sign(pk, b2sum[:])
|
2020-01-30 23:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (secpSigner) Verify(sig []byte, a address.Address, msg []byte) error {
|
|
|
|
b2sum := blake2b.Sum256(msg)
|
2022-09-29 20:46:59 +00:00
|
|
|
pubk, err := gocrypto.EcRecover(b2sum[:], sig)
|
2020-01-30 23:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
maybeaddr, err := address.NewSecp256k1Address(pubk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if a != maybeaddr {
|
|
|
|
return fmt.Errorf("signature did not match")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-02-14 14:14:39 +00:00
|
|
|
sigs.RegisterSignature(crypto2.SigTypeSecp256k1, secpSigner{})
|
2020-01-30 23:48:25 +00:00
|
|
|
}
|