2020-01-30 23:48:25 +00:00
|
|
|
package bls
|
|
|
|
|
|
|
|
import (
|
2020-07-26 01:38:18 +00:00
|
|
|
"crypto/rand"
|
2020-01-30 23:48:25 +00:00
|
|
|
"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
|
|
|
|
2021-01-19 16:45:39 +00:00
|
|
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
2020-02-12 23:52:36 +00:00
|
|
|
|
2020-01-30 23:48:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
|
|
|
)
|
|
|
|
|
2020-07-26 01:38:18 +00:00
|
|
|
const DST = string("BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_")
|
|
|
|
|
2021-01-19 16:45:39 +00:00
|
|
|
type SecretKey = ffi.PrivateKey
|
|
|
|
type PublicKey = ffi.PublicKey
|
|
|
|
type Signature = ffi.Signature
|
|
|
|
type AggregateSignature = ffi.Signature
|
2020-07-26 01:38:18 +00:00
|
|
|
|
2020-01-30 23:48:25 +00:00
|
|
|
type blsSigner struct{}
|
|
|
|
|
|
|
|
func (blsSigner) GenPrivate() ([]byte, error) {
|
2020-07-26 01:38:18 +00:00
|
|
|
// 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!
|
2021-01-19 16:45:39 +00:00
|
|
|
sk := ffi.PrivateKeyGenerateWithSeed(ikm)
|
2021-01-25 13:02:58 +00:00
|
|
|
return sk[:], nil
|
2020-01-30 23:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (blsSigner) ToPublic(priv []byte) ([]byte, error) {
|
2021-01-19 16:45:39 +00:00
|
|
|
if priv == nil || len(priv) != ffi.PrivateKeyBytes {
|
2020-07-26 01:38:18 +00:00
|
|
|
return nil, fmt.Errorf("bls signature invalid private key")
|
|
|
|
}
|
2021-01-19 16:45:39 +00:00
|
|
|
|
|
|
|
sk := new(SecretKey)
|
|
|
|
copy(sk[:], priv[:ffi.PrivateKeyBytes])
|
|
|
|
|
|
|
|
pubkey := ffi.PrivateKeyPublicKey(*sk)
|
|
|
|
|
2021-01-25 13:02:58 +00:00
|
|
|
return pubkey[:], nil
|
2020-01-30 23:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (blsSigner) Sign(p []byte, msg []byte) ([]byte, error) {
|
2021-01-19 16:45:39 +00:00
|
|
|
if p == nil || len(p) != ffi.PrivateKeyBytes {
|
2020-07-26 01:38:18 +00:00
|
|
|
return nil, fmt.Errorf("bls signature invalid private key")
|
|
|
|
}
|
2021-01-19 16:45:39 +00:00
|
|
|
|
|
|
|
sk := new(SecretKey)
|
|
|
|
copy(sk[:], p[:ffi.PrivateKeyBytes])
|
|
|
|
|
|
|
|
sig := ffi.PrivateKeySign(*sk, msg)
|
|
|
|
|
2021-01-25 13:02:58 +00:00
|
|
|
return sig[:], nil
|
2020-01-30 23:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (blsSigner) Verify(sig []byte, a address.Address, msg []byte) error {
|
2021-01-19 16:45:39 +00:00
|
|
|
payload := a.Payload()
|
|
|
|
if sig == nil || len(sig) != ffi.SignatureBytes || len(payload) != ffi.PublicKeyBytes {
|
2020-01-30 23:48:25 +00:00
|
|
|
return fmt.Errorf("bls signature failed to verify")
|
|
|
|
}
|
2021-01-19 16:45:39 +00:00
|
|
|
|
|
|
|
pk := new(PublicKey)
|
|
|
|
copy(pk[:], payload[:ffi.PublicKeyBytes])
|
|
|
|
|
|
|
|
sigS := new(Signature)
|
|
|
|
copy(sigS[:], sig[:ffi.SignatureBytes])
|
|
|
|
|
|
|
|
msgs := [1]ffi.Message{msg}
|
|
|
|
pks := [1]PublicKey{*pk}
|
|
|
|
|
|
|
|
if !ffi.HashVerify(sigS, msgs[:], pks[:]) {
|
|
|
|
return fmt.Errorf("bls signature failed to verify")
|
|
|
|
}
|
|
|
|
|
2020-01-30 23:48:25 +00:00
|
|
|
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
|
|
|
}
|