mirror of
https://github.com/luxfi/crypto.git
synced 2026-07-27 01:54:50 +00:00
- bls.go: pure Go implementation using cloudflare/circl (!cgo) - bls_cgo.go: CGO-optimized implementation using supranational/blst (cgo) - types.go: shared constants and errors - Simplified bls12381 build tags (cgo vs !cgo) - Removed duplicate implementations (bls_new.go, internal.go) - Fixed tests to work with both implementations
192 lines
4.2 KiB
Go
192 lines
4.2 KiB
Go
// Copyright (C) 2020-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
//go:build !cgo
|
|
|
|
package bls
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"errors"
|
|
|
|
"github.com/cloudflare/circl/ecc/bls12381"
|
|
blssign "github.com/cloudflare/circl/sign/bls"
|
|
)
|
|
|
|
type (
|
|
SecretKey struct {
|
|
sk *blssign.PrivateKey[blssign.KeyG1SigG2]
|
|
}
|
|
|
|
PublicKey struct {
|
|
pk *blssign.PublicKey[blssign.KeyG1SigG2]
|
|
}
|
|
|
|
Signature struct {
|
|
sig blssign.Signature
|
|
}
|
|
|
|
AggregatePublicKey = PublicKey
|
|
AggregateSignature = Signature
|
|
)
|
|
|
|
func NewSecretKey() (*SecretKey, error) {
|
|
ikm := make([]byte, 32)
|
|
if _, err := rand.Read(ikm); err != nil {
|
|
return nil, err
|
|
}
|
|
defer clear(ikm)
|
|
|
|
sk, err := blssign.KeyGen[blssign.KeyG1SigG2](ikm, nil, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &SecretKey{sk: sk}, nil
|
|
}
|
|
|
|
func SecretKeyToBytes(sk *SecretKey) []byte {
|
|
if sk == nil || sk.sk == nil {
|
|
return nil
|
|
}
|
|
data, _ := sk.sk.MarshalBinary()
|
|
return data
|
|
}
|
|
|
|
func SecretKeyFromBytes(skBytes []byte) (*SecretKey, error) {
|
|
if len(skBytes) != SecretKeyLen {
|
|
return nil, ErrFailedSecretKeyDeserialize
|
|
}
|
|
sk := new(blssign.PrivateKey[blssign.KeyG1SigG2])
|
|
if err := sk.UnmarshalBinary(skBytes); err != nil {
|
|
return nil, ErrFailedSecretKeyDeserialize
|
|
}
|
|
return &SecretKey{sk: sk}, nil
|
|
}
|
|
|
|
func (sk *SecretKey) PublicKey() *PublicKey {
|
|
if sk == nil || sk.sk == nil {
|
|
return nil
|
|
}
|
|
return &PublicKey{pk: sk.sk.PublicKey()}
|
|
}
|
|
|
|
func (sk *SecretKey) Sign(msg []byte) (*Signature, error) {
|
|
if sk == nil || sk.sk == nil {
|
|
return nil, errors.New("nil secret key")
|
|
}
|
|
return &Signature{sig: blssign.Sign(sk.sk, msg)}, nil
|
|
}
|
|
|
|
func (sk *SecretKey) SignProofOfPossession(msg []byte) (*Signature, error) {
|
|
return sk.Sign(msg)
|
|
}
|
|
|
|
func PublicKeyToCompressedBytes(pk *PublicKey) []byte {
|
|
if pk == nil || pk.pk == nil {
|
|
return nil
|
|
}
|
|
data, _ := pk.pk.MarshalBinary()
|
|
return data
|
|
}
|
|
|
|
func PublicKeyFromCompressedBytes(pkBytes []byte) (*PublicKey, error) {
|
|
pk := new(blssign.PublicKey[blssign.KeyG1SigG2])
|
|
if err := pk.UnmarshalBinary(pkBytes); err != nil {
|
|
return nil, ErrFailedPublicKeyDecompress
|
|
}
|
|
if !pk.Validate() {
|
|
return nil, ErrInvalidPublicKey
|
|
}
|
|
return &PublicKey{pk: pk}, nil
|
|
}
|
|
|
|
func PublicKeyToUncompressedBytes(key *PublicKey) []byte {
|
|
return PublicKeyToCompressedBytes(key)
|
|
}
|
|
|
|
func PublicKeyFromValidUncompressedBytes(pkBytes []byte) *PublicKey {
|
|
pk := new(blssign.PublicKey[blssign.KeyG1SigG2])
|
|
_ = pk.UnmarshalBinary(pkBytes)
|
|
return &PublicKey{pk: pk}
|
|
}
|
|
|
|
func AggregatePublicKeys(pks []*PublicKey) (*PublicKey, error) {
|
|
if len(pks) == 0 {
|
|
return nil, ErrNoPublicKeys
|
|
}
|
|
|
|
var agg bls12381.G1
|
|
agg.SetIdentity()
|
|
|
|
for _, pk := range pks {
|
|
if pk == nil || pk.pk == nil {
|
|
return nil, ErrInvalidPublicKey
|
|
}
|
|
pkBytes, err := pk.pk.MarshalBinary()
|
|
if err != nil {
|
|
return nil, ErrFailedPublicKeyAggregation
|
|
}
|
|
var pt bls12381.G1
|
|
if err := pt.SetBytes(pkBytes); err != nil {
|
|
return nil, ErrFailedPublicKeyAggregation
|
|
}
|
|
agg.Add(&agg, &pt)
|
|
}
|
|
|
|
result := new(blssign.PublicKey[blssign.KeyG1SigG2])
|
|
if err := result.UnmarshalBinary(agg.BytesCompressed()); err != nil {
|
|
return nil, ErrFailedPublicKeyAggregation
|
|
}
|
|
return &PublicKey{pk: result}, nil
|
|
}
|
|
|
|
func Verify(pk *PublicKey, sig *Signature, msg []byte) bool {
|
|
if pk == nil || pk.pk == nil || sig == nil {
|
|
return false
|
|
}
|
|
return blssign.Verify(pk.pk, msg, sig.sig)
|
|
}
|
|
|
|
func VerifyProofOfPossession(pk *PublicKey, sig *Signature, msg []byte) bool {
|
|
return Verify(pk, sig, msg)
|
|
}
|
|
|
|
func SignatureToBytes(sig *Signature) []byte {
|
|
if sig == nil {
|
|
return nil
|
|
}
|
|
return sig.sig
|
|
}
|
|
|
|
func SignatureFromBytes(sigBytes []byte) (*Signature, error) {
|
|
if len(sigBytes) != SignatureLen {
|
|
return nil, ErrFailedSignatureDecompress
|
|
}
|
|
for _, b := range sigBytes {
|
|
if b != 0 {
|
|
return &Signature{sig: sigBytes}, nil
|
|
}
|
|
}
|
|
return nil, ErrFailedSignatureDecompress
|
|
}
|
|
|
|
func AggregateSignatures(sigs []*Signature) (*Signature, error) {
|
|
if len(sigs) == 0 {
|
|
return nil, ErrNoSignatures
|
|
}
|
|
|
|
sigBytes := make([]blssign.Signature, len(sigs))
|
|
for i, sig := range sigs {
|
|
if sig == nil {
|
|
return nil, ErrFailedSignatureAggregation
|
|
}
|
|
sigBytes[i] = sig.sig
|
|
}
|
|
|
|
aggSig, err := blssign.Aggregate[blssign.KeyG1SigG2](blssign.KeyG1SigG2{}, sigBytes)
|
|
if err != nil {
|
|
return nil, ErrFailedSignatureAggregation
|
|
}
|
|
return &Signature{sig: aggSig}, nil
|
|
}
|