github.com/prysmaticlabs/prysm@v1.4.4/shared/bls/bls.go (about)

     1  // Package bls implements a go-wrapper around a library implementing the
     2  // the BLS12-381 curve and signature scheme. This package exposes a public API for
     3  // verifying and aggregating BLS signatures used by Ethereum.
     4  package bls
     5  
     6  import (
     7  	"math/big"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/prysmaticlabs/prysm/shared/bls/blst"
    11  	"github.com/prysmaticlabs/prysm/shared/bls/common"
    12  	"github.com/prysmaticlabs/prysm/shared/bls/herumi"
    13  )
    14  
    15  // Initialize herumi temporarily while we transition to blst for ethdo.
    16  func init() {
    17  	herumi.HerumiInit()
    18  }
    19  
    20  // SecretKeyFromBytes creates a BLS private key from a BigEndian byte slice.
    21  func SecretKeyFromBytes(privKey []byte) (SecretKey, error) {
    22  	return blst.SecretKeyFromBytes(privKey)
    23  }
    24  
    25  // SecretKeyFromBigNum takes in a big number string and creates a BLS private key.
    26  func SecretKeyFromBigNum(s string) (SecretKey, error) {
    27  	num := new(big.Int)
    28  	num, ok := num.SetString(s, 10)
    29  	if !ok {
    30  		return nil, errors.New("could not set big int from string")
    31  	}
    32  	bts := num.Bytes()
    33  	if len(bts) != 32 {
    34  		return nil, errors.Errorf("provided big number string sets to a key unequal to 32 bytes: %d != 32", len(bts))
    35  	}
    36  	return SecretKeyFromBytes(bts)
    37  }
    38  
    39  // PublicKeyFromBytes creates a BLS public key from a  BigEndian byte slice.
    40  func PublicKeyFromBytes(pubKey []byte) (PublicKey, error) {
    41  	return blst.PublicKeyFromBytes(pubKey)
    42  }
    43  
    44  // SignatureFromBytes creates a BLS signature from a LittleEndian byte slice.
    45  func SignatureFromBytes(sig []byte) (Signature, error) {
    46  	return blst.SignatureFromBytes(sig)
    47  }
    48  
    49  // AggregatePublicKeys aggregates the provided raw public keys into a single key.
    50  func AggregatePublicKeys(pubs [][]byte) (PublicKey, error) {
    51  	return blst.AggregatePublicKeys(pubs)
    52  }
    53  
    54  // AggregateSignatures converts a list of signatures into a single, aggregated sig.
    55  func AggregateSignatures(sigs []common.Signature) common.Signature {
    56  	return blst.AggregateSignatures(sigs)
    57  }
    58  
    59  // VerifyMultipleSignatures verifies multiple signatures for distinct messages securely.
    60  func VerifyMultipleSignatures(sigs [][]byte, msgs [][32]byte, pubKeys []common.PublicKey) (bool, error) {
    61  	return blst.VerifyMultipleSignatures(sigs, msgs, pubKeys)
    62  }
    63  
    64  // NewAggregateSignature creates a blank aggregate signature.
    65  func NewAggregateSignature() common.Signature {
    66  	return blst.NewAggregateSignature()
    67  }
    68  
    69  // RandKey creates a new private key using a random input.
    70  func RandKey() (common.SecretKey, error) {
    71  	return blst.RandKey()
    72  }