Skip to content

Commit

Permalink
Add byte encoder/decoder for optional BLS signatures (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
tholonious authored Jul 7, 2023
1 parent 603f525 commit b3bb387
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
15 changes: 15 additions & 0 deletions lib/block_view_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2251,6 +2251,15 @@ func DecodeBLSPublicKey(rr io.Reader) (*bls.PublicKey, error) {
return (&bls.PublicKey{}).FromBytes(publicKeyBytes)
}

// EncodeOptionalBLSSignature is defined explicitly as a wrapper encoding function
// that has known behavior for nil values. It will always have the following behavior:
// - If blsSignature is nil, then it returns []byte{0} as a placeholder
// - If the blsSignature is not nil, then it returns
// []byte{len(blsSignatureBytes), blsSignatureBytes...}
func EncodeOptionalBLSSignature(blsSignature *bls.Signature) []byte {
return EncodeBLSSignature(blsSignature)
}

func EncodeBLSSignature(blsSignature *bls.Signature) []byte {
var blsSignatureBytes []byte
if blsSignature != nil {
Expand All @@ -2259,6 +2268,12 @@ func EncodeBLSSignature(blsSignature *bls.Signature) []byte {
return EncodeByteArray(blsSignatureBytes)
}

// DecodeOptionalBLSSignature is defined explicitly as a bijective decoding function
// for EncodeOptionalBLSSignature above.
func DecodeOptionalBLSSignature(rr io.Reader) (*bls.Signature, error) {
return DecodeBLSSignature(rr)
}

func DecodeBLSSignature(rr io.Reader) (*bls.Signature, error) {
signatureBytes, err := DecodeByteArray(rr)
if err != nil {
Expand Down
28 changes: 11 additions & 17 deletions lib/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2149,21 +2149,21 @@ func (msg *MsgDeSoHeader) EncodeHeaderVersion2(preSignature bool) ([]byte, error
}
retBytes = append(retBytes, encodedValidatorsTimeoutAggregateQC...)

// If preSignature=false, then the ProposerVotePartialSignature must be populated.
if !preSignature && msg.ProposerVotePartialSignature == nil {
return nil, fmt.Errorf("EncodeHeaderVersion2: ProposerVotePartialSignature must be non-nil when preSignature=false")
}

// ProposerVotePartialSignature: we encode the signature if it's present and the preSignature
// flag is set to false. Otherwise, we encode an empty byte array as a placeholder. The placeholder
// ensures, that the DecodeHeaderVersion2 function can properly recognize encoding where a signature
// ensures that the DecodeHeaderVersion2 function can properly recognize encodings where the signature
// isn't populated. It ensures that every possible output from EncodeHeaderVersion2 can be decoded by
// DecodeHeaderVersion2.
proposerSignatureBytes := []byte{}
if !preSignature {
// If the the preSignature flag is set to false, then the caller intends to encode the signature.
// The signature must be non-nil.
if msg.ProposerVotePartialSignature == nil {
return nil, fmt.Errorf("EncodeHeaderVersion2: ProposerVotePartialSignature must be non-nil")
}
proposerSignatureBytes = msg.ProposerVotePartialSignature.ToBytes()
if preSignature {
retBytes = append(retBytes, EncodeOptionalBLSSignature(nil)...)
} else {
retBytes = append(retBytes, EncodeOptionalBLSSignature(msg.ProposerVotePartialSignature)...)
}
retBytes = append(retBytes, EncodeByteArray(proposerSignatureBytes)...)

return retBytes, nil
}
Expand Down Expand Up @@ -2354,16 +2354,10 @@ func DecodeHeaderVersion2(rr io.Reader) (*MsgDeSoHeader, error) {

// ProposerVotePartialSignature: we decode the signature if it's present in the byte encoding.
// If it's not present, then we set the signature to nil.
proposerSignatureBytes, err := DecodeByteArray(rr)
retHeader.ProposerVotePartialSignature, err = DecodeOptionalBLSSignature(rr)
if err != nil {
return nil, errors.Wrapf(err, "MsgDeSoHeader.FromBytes: Problem decoding ProposerVotePartialSignature")
}
if len(proposerSignatureBytes) != 0 {
retHeader.ProposerVotePartialSignature, err = (&bls.Signature{}).FromBytes(proposerSignatureBytes)
if err != nil {
return nil, errors.Wrapf(err, "MsgDeSoHeader.FromBytes: Problem decoding ProposerVotePartialSignature")
}
}

return retHeader, nil
}
Expand Down

0 comments on commit b3bb387

Please sign in to comment.