2020-01-30 23:48:25 +00:00
|
|
|
package bls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-12 23:52:36 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
|
|
|
|
|
|
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
|
|
|
|
2020-01-30 23:48:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type blsSigner struct{}
|
|
|
|
|
|
|
|
func (blsSigner) GenPrivate() ([]byte, error) {
|
|
|
|
pk := ffi.PrivateKeyGenerate()
|
|
|
|
return pk[:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blsSigner) ToPublic(priv []byte) ([]byte, error) {
|
|
|
|
var pk ffi.PrivateKey
|
|
|
|
copy(pk[:], priv)
|
|
|
|
pub := ffi.PrivateKeyPublicKey(pk)
|
|
|
|
return pub[:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blsSigner) Sign(p []byte, msg []byte) ([]byte, error) {
|
|
|
|
var pk ffi.PrivateKey
|
|
|
|
copy(pk[:], p)
|
|
|
|
sig := ffi.PrivateKeySign(pk, msg)
|
|
|
|
return sig[:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blsSigner) Verify(sig []byte, a address.Address, msg []byte) error {
|
|
|
|
|
|
|
|
var pubk ffi.PublicKey
|
|
|
|
copy(pubk[:], a.Payload())
|
|
|
|
pubkeys := []ffi.PublicKey{pubk}
|
2020-04-17 18:16:36 +00:00
|
|
|
digests := []ffi.Message{msg}
|
2020-01-30 23:48:25 +00:00
|
|
|
|
|
|
|
var s ffi.Signature
|
|
|
|
copy(s[:], sig)
|
|
|
|
|
2020-04-17 18:16:36 +00:00
|
|
|
if !ffi.HashVerify(&s, digests, pubkeys) {
|
2020-01-30 23:48:25 +00:00
|
|
|
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{})
|
2020-01-30 23:48:25 +00:00
|
|
|
}
|