lotus/lib/sigs/bls/init.go

62 lines
1.6 KiB
Go
Raw Normal View History

package bls
import (
"crypto/rand"
"fmt"
"github.com/filecoin-project/go-address"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/crypto"
2020-02-12 23:52:36 +00:00
blst "github.com/supranational/blst/bindings/go"
2020-02-12 23:52:36 +00:00
"github.com/filecoin-project/lotus/lib/sigs"
)
const DST = string("BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_")
type SecretKey = blst.SecretKey
type PublicKey = blst.P1Affine
type Signature = blst.P2Affine
type AggregateSignature = blst.P2Aggregate
type blsSigner struct{}
func (blsSigner) GenPrivate() ([]byte, error) {
// Generate 32 bytes of randomness
var ikm [32]byte
_, err := rand.Read(ikm[:])
if err != nil {
return nil, fmt.Errorf("bls signature error generating random data")
}
2020-07-26 04:46:23 +00:00
// Note private keys seem to be serialized little-endian!
pk := blst.KeyGen(ikm[:]).ToLEndian()
return pk, nil
}
func (blsSigner) ToPublic(priv []byte) ([]byte, error) {
2020-07-26 04:46:23 +00:00
pk := new(SecretKey).FromLEndian(priv)
if pk == nil || !pk.Valid() {
return nil, fmt.Errorf("bls signature invalid private key")
}
return new(PublicKey).From(pk).Compress(), nil
}
func (blsSigner) Sign(p []byte, msg []byte) ([]byte, error) {
2020-07-26 04:46:23 +00:00
pk := new(SecretKey).FromLEndian(p)
if pk == nil || !pk.Valid() {
return nil, fmt.Errorf("bls signature invalid private key")
}
return new(Signature).Sign(pk, msg, []byte(DST)).Compress(), nil
}
func (blsSigner) Verify(sig []byte, a address.Address, msg []byte) error {
if !new(Signature).VerifyCompressed(sig, a.Payload()[:], msg, []byte(DST)) {
return fmt.Errorf("bls signature failed to verify")
}
return nil
}
func init() {
2020-02-12 23:52:36 +00:00
sigs.RegisterSignature(crypto.SigTypeBLS, blsSigner{})
}