2019-02-19 15:09:50 +00:00
|
|
|
// Package bls implements a go-wrapper around a library implementing the
|
|
|
|
// the BLS12-381 curve and signature scheme. This package exposes a public API for
|
2021-06-26 19:00:33 +00:00
|
|
|
// verifying and aggregating BLS signatures used by Ethereum.
|
2019-02-15 18:31:07 +00:00
|
|
|
package bls
|
|
|
|
|
|
|
|
import (
|
2020-11-11 21:24:08 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/bls/blst"
|
|
|
|
"github.com/prysmaticlabs/prysm/crypto/bls/common"
|
|
|
|
"github.com/prysmaticlabs/prysm/crypto/bls/herumi"
|
2019-02-15 18:31:07 +00:00
|
|
|
)
|
|
|
|
|
2021-04-07 15:18:19 +00:00
|
|
|
// Initialize herumi temporarily while we transition to blst for ethdo.
|
|
|
|
func init() {
|
|
|
|
herumi.HerumiInit()
|
|
|
|
}
|
|
|
|
|
2019-12-03 20:29:05 +00:00
|
|
|
// SecretKeyFromBytes creates a BLS private key from a BigEndian byte slice.
|
2020-06-25 00:47:51 +00:00
|
|
|
func SecretKeyFromBytes(privKey []byte) (SecretKey, error) {
|
2021-04-07 15:18:19 +00:00
|
|
|
return blst.SecretKeyFromBytes(privKey)
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 21:24:08 +00:00
|
|
|
// SecretKeyFromBigNum takes in a big number string and creates a BLS private key.
|
|
|
|
func SecretKeyFromBigNum(s string) (SecretKey, error) {
|
|
|
|
num := new(big.Int)
|
|
|
|
num, ok := num.SetString(s, 10)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not set big int from string")
|
|
|
|
}
|
|
|
|
bts := num.Bytes()
|
2021-04-27 06:46:47 +00:00
|
|
|
if len(bts) != 32 {
|
|
|
|
return nil, errors.Errorf("provided big number string sets to a key unequal to 32 bytes: %d != 32", len(bts))
|
2020-11-11 21:24:08 +00:00
|
|
|
}
|
|
|
|
return SecretKeyFromBytes(bts)
|
|
|
|
}
|
|
|
|
|
2019-12-03 20:29:05 +00:00
|
|
|
// PublicKeyFromBytes creates a BLS public key from a BigEndian byte slice.
|
2020-06-25 00:47:51 +00:00
|
|
|
func PublicKeyFromBytes(pubKey []byte) (PublicKey, error) {
|
2021-04-07 15:18:19 +00:00
|
|
|
return blst.PublicKeyFromBytes(pubKey)
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 21:39:45 +00:00
|
|
|
// SignatureFromBytes creates a BLS signature from a LittleEndian byte slice.
|
2020-06-25 00:47:51 +00:00
|
|
|
func SignatureFromBytes(sig []byte) (Signature, error) {
|
2021-04-07 15:18:19 +00:00
|
|
|
return blst.SignatureFromBytes(sig)
|
2019-10-02 00:13:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 13:28:28 +00:00
|
|
|
// AggregatePublicKeys aggregates the provided raw public keys into a single key.
|
|
|
|
func AggregatePublicKeys(pubs [][]byte) (PublicKey, error) {
|
2021-04-07 15:18:19 +00:00
|
|
|
return blst.AggregatePublicKeys(pubs)
|
2020-09-16 13:28:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
// AggregateSignatures converts a list of signatures into a single, aggregated sig.
|
2020-10-30 19:06:33 +00:00
|
|
|
func AggregateSignatures(sigs []common.Signature) common.Signature {
|
2021-04-07 15:18:19 +00:00
|
|
|
return blst.AggregateSignatures(sigs)
|
2019-10-02 00:13:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 07:38:13 +00:00
|
|
|
// VerifyMultipleSignatures verifies multiple signatures for distinct messages securely.
|
2020-10-30 19:06:33 +00:00
|
|
|
func VerifyMultipleSignatures(sigs [][]byte, msgs [][32]byte, pubKeys []common.PublicKey) (bool, error) {
|
2021-04-07 15:18:19 +00:00
|
|
|
return blst.VerifyMultipleSignatures(sigs, msgs, pubKeys)
|
2020-07-03 07:38:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 00:47:51 +00:00
|
|
|
// NewAggregateSignature creates a blank aggregate signature.
|
2020-10-30 19:06:33 +00:00
|
|
|
func NewAggregateSignature() common.Signature {
|
2021-04-07 15:18:19 +00:00
|
|
|
return blst.NewAggregateSignature()
|
2020-05-07 12:05:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 00:47:51 +00:00
|
|
|
// RandKey creates a new private key using a random input.
|
2020-10-30 19:06:33 +00:00
|
|
|
func RandKey() (common.SecretKey, error) {
|
2021-04-07 15:18:19 +00:00
|
|
|
return blst.RandKey()
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|