github.com/koko1123/flow-go-1@v0.29.6/module/signature/type_encoder.go (about)

     1  package signature
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/koko1123/flow-go-1/model/encoding"
     7  	"github.com/onflow/flow-go/crypto"
     8  )
     9  
    10  const SigLen = crypto.SignatureLenBLSBLS12381
    11  
    12  // EncodeSingleSig encodes a single signature into signature data as required by the consensus design.
    13  func EncodeSingleSig(sigType encoding.SigType, sig crypto.Signature) []byte {
    14  	t := byte(sigType)
    15  	encoded := make([]byte, 0, len(sig)+1)
    16  	encoded = append(encoded, t)
    17  	encoded = append(encoded, sig[:]...)
    18  	return encoded
    19  }
    20  
    21  // DecodeSingleSig decodes the signature data into a cryptographic signature and a type as required by
    22  // the consensus design. Cryptographic validity of signatures is _not_ checked.
    23  // It returns:
    24  //   - 0, nil, ErrInvalidSignatureFormat if the sig type is invalid (covers nil or empty sigData)
    25  //   - sigType, signature, nil if the sig type is valid and the decoding is done successfully.
    26  func DecodeSingleSig(sigData []byte) (encoding.SigType, crypto.Signature, error) {
    27  	if len(sigData) == 0 {
    28  		return 0, nil, fmt.Errorf("empty sig data: %w", ErrInvalidSignatureFormat)
    29  	}
    30  
    31  	sigType := encoding.SigType(sigData[0])
    32  	if !sigType.Valid() {
    33  		return 0, nil, fmt.Errorf("invalid sig type %v: %w", sigType, ErrInvalidSignatureFormat)
    34  	}
    35  
    36  	sig := crypto.Signature(sigData[1:])
    37  	return sigType, sig, nil
    38  }
    39  
    40  // TODO: to be removed in V3, replace by packer's pack method
    41  // EncodeDoubleSig encodes both the staking signature and random beacon signature
    42  // into one sigData.
    43  func EncodeDoubleSig(stakingSig crypto.Signature, beaconSig crypto.Signature) []byte {
    44  	encoded := make([]byte, 0, len(stakingSig)+len(beaconSig))
    45  	encoded = append(encoded, stakingSig...)
    46  	encoded = append(encoded, beaconSig...)
    47  	return encoded
    48  }
    49  
    50  // TODO: to be removed in V3, replace by packer's unpack method
    51  // DecodeDoubleSig decodes the signature data into a staking signature and an optional
    52  // random beacon signature. The decoding assumes BLS with BLS12-381 is used.
    53  // Cryptographic validity of signatures is _not_ checked.
    54  // Decomposition of the sigData is purely done based on length.
    55  // It returns:
    56  //   - staking signature, random beacon signature, nil:
    57  //     if sigData is twice the size of a BLS signature bytes long, we use the leading half as staking signature
    58  //     and the tailing half random beacon sig
    59  //   - staking signature, nil, nil:
    60  //     if sigData is the size of a BLS signature, we interpret sigData entirely as staking signature
    61  //   - nil, nil, ErrInvalidSignatureFormat if the sig type is invalid (covers nil or empty sigData)
    62  func DecodeDoubleSig(sigData []byte) (crypto.Signature, crypto.Signature, error) {
    63  	sigLength := len(sigData)
    64  	switch sigLength {
    65  	case SigLen:
    66  		return sigData, nil, nil
    67  	case 2 * SigLen:
    68  		return sigData[:SigLen], sigData[SigLen:], nil
    69  	}
    70  
    71  	return nil, nil, fmt.Errorf("invalid sig data length %d: %w", sigLength, ErrInvalidSignatureFormat)
    72  }