2019-02-19 15:09:50 +00:00
|
|
|
// Package bls implements a go-wrapper around a library implementing the
|
2023-04-18 22:01:27 +00:00
|
|
|
// 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 (
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/crypto/bls/blst"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/crypto/bls/common"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/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() {
|
2023-10-20 16:45:33 +00:00
|
|
|
herumi.Init()
|
2021-04-07 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-16 17:06:26 +00:00
|
|
|
// SignatureFromBytesNoValidation creates a BLS signature from a LittleEndian byte slice.
|
|
|
|
// It does not check validity of the signature, use only when the byte slice has
|
|
|
|
// already been verified
|
|
|
|
func SignatureFromBytesNoValidation(sig []byte) (Signature, error) {
|
|
|
|
return blst.SignatureFromBytesNoValidation(sig)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-21 22:57:29 +00:00
|
|
|
// MultipleSignaturesFromBytes creates a slice of BLS signatures from a LittleEndian 2d-byte slice.
|
|
|
|
func MultipleSignaturesFromBytes(sigs [][]byte) ([]Signature, error) {
|
|
|
|
return blst.MultipleSignaturesFromBytes(sigs)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-04 04:47:53 +00:00
|
|
|
// AggregateMultiplePubkeys aggregates the provided decompressed keys into a single key.
|
|
|
|
func AggregateMultiplePubkeys(pubs []PublicKey) PublicKey {
|
|
|
|
return blst.AggregateMultiplePubkeys(pubs)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-04 04:47:53 +00:00
|
|
|
// AggregateCompressedSignatures converts a list of compressed signatures into a single, aggregated sig.
|
|
|
|
func AggregateCompressedSignatures(multiSigs [][]byte) (common.Signature, error) {
|
|
|
|
return blst.AggregateCompressedSignatures(multiSigs)
|
|
|
|
}
|
|
|
|
|
2022-12-20 10:41:47 +00:00
|
|
|
// VerifySignature verifies a single signature. For performance reason, always use VerifyMultipleSignatures if possible.
|
|
|
|
func VerifySignature(sig []byte, msg [32]byte, pubKey common.PublicKey) (bool, error) {
|
|
|
|
return blst.VerifySignature(sig, msg, pubKey)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|