Skip to content

Commit

Permalink
[crypto] Invert (encode|decode)PublicKey delegation for BLS
Browse files Browse the repository at this point in the history
- check for parametrization of compression
  • Loading branch information
huitseeker committed Aug 17, 2021
1 parent 47a0f2a commit b1ee91a
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions crypto/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,8 @@ func (a *blsBLS12381Algo) decodePrivateKey(privateKeyBytes []byte) (PrivateKey,
}

// decodePublicKey decodes a slice of bytes into a public key.
// since we use the compressed representation by default, this delegates to decodePublicKeyCompressed
func (a *blsBLS12381Algo) decodePublicKey(publicKeyBytes []byte) (PublicKey, error) {
return a.decodePublicKeyCompressed(publicKeyBytes)
}

// decodePublicKeyCompressed decodes a slice of bytes into a public key.
// This function includes a membership check in G2 and rejects the infinity point.
func (a *blsBLS12381Algo) decodePublicKeyCompressed(publicKeyBytes []byte) (PublicKey, error) {
func (a *blsBLS12381Algo) decodePublicKey(publicKeyBytes []byte) (PublicKey, error) {
if len(publicKeyBytes) != pubKeyLengthBLSBLS12381 {
return nil, newInvalidInputsError(
"the input length has to be %d",
Expand All @@ -238,6 +232,15 @@ func (a *blsBLS12381Algo) decodePublicKeyCompressed(publicKeyBytes []byte) (Publ
return &pk, nil
}

// decodePublicKeyCompressed decodes a slice of bytes into a public key.
// since we use the compressed representation by default, this checks the default and delegates to decodePublicKeyCompressed
func (a *blsBLS12381Algo) decodePublicKeyCompressed(publicKeyBytes []byte) (PublicKey, error) {
if serializationG2 != compressed {
panic("library is not configured to use compressed public key serialization")
}
return a.decodePublicKey(publicKeyBytes)
}

// PrKeyBLSBLS12381 is the private key of BLS using BLS12_381, it implements PrivateKey
type PrKeyBLSBLS12381 struct {
// public key
Expand Down Expand Up @@ -346,15 +349,18 @@ func (pk *PubKeyBLSBLS12381) Size() int {
// The encoding is a compressed encoding of the point
// [zcash] https://github.com/zkcrypto/pairing/blob/master/src/bls12_381/README.md#serialization
func (a *PubKeyBLSBLS12381) EncodeCompressed() []byte {
dest := make([]byte, pubKeyLengthBLSBLS12381)
writePointG2(dest, &a.point)
return dest
if serializationG2 != compressed {
panic("library is not configured to use compressed public key serialization")
}
return a.Encode()
}

// Encode returns a byte encoding of the public key.
// Since we use a compressed encoding by default, this delegates to EncodeCompressed
func (a *PubKeyBLSBLS12381) Encode() []byte {
return a.EncodeCompressed()
dest := make([]byte, pubKeyLengthBLSBLS12381)
writePointG2(dest, &a.point)
return dest
}

// Equals checks is two public keys are equal
Expand Down

0 comments on commit b1ee91a

Please sign in to comment.