mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-07 10:12:19 +00:00
211d9bc0b9
* fix build from source * clean up * update again * change everything * workaround for now * fix versioning * all passing now * fix build issues * clean up * revert use of MulVerify * gaz * stub * Apply suggestions from code review Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * fix all * fix test * todo * fix stub * revert back * make deep source happy * Update shared/bls/herumi/public_key.go * Update shared/bls/blst/signature.go * Update shared/bls/blst/signature_test.go * imports * move iface to common, export errors * rm iface build Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: terence tsao <terence@prysmaticlabs.com>
80 lines
2.4 KiB
Go
80 lines
2.4 KiB
Go
package herumi
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
bls12 "github.com/herumi/bls-eth-go-binary/bls"
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/shared/bls/common"
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
)
|
|
|
|
// bls12SecretKey used in the BLS signature scheme.
|
|
type bls12SecretKey struct {
|
|
p *bls12.SecretKey
|
|
}
|
|
|
|
// RandKey creates a new private key using a random method provided as an io.Reader.
|
|
func RandKey() (common.SecretKey, error) {
|
|
secKey := &bls12.SecretKey{}
|
|
secKey.SetByCSPRNG()
|
|
if secKey.IsZero() {
|
|
return nil, errors.New("generated a zero secret key")
|
|
}
|
|
return &bls12SecretKey{secKey}, nil
|
|
}
|
|
|
|
// SecretKeyFromBytes creates a BLS private key from a BigEndian byte slice.
|
|
func SecretKeyFromBytes(privKey []byte) (common.SecretKey, error) {
|
|
if len(privKey) != params.BeaconConfig().BLSSecretKeyLength {
|
|
return nil, fmt.Errorf("secret key must be %d bytes", params.BeaconConfig().BLSSecretKeyLength)
|
|
}
|
|
secKey := &bls12.SecretKey{}
|
|
err := secKey.Deserialize(privKey)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not unmarshal bytes into secret key")
|
|
}
|
|
wrappedKey := &bls12SecretKey{p: secKey}
|
|
if wrappedKey.IsZero() {
|
|
return nil, common.ErrZeroKey
|
|
}
|
|
return wrappedKey, err
|
|
}
|
|
|
|
// PublicKey obtains the public key corresponding to the BLS secret key.
|
|
func (s *bls12SecretKey) PublicKey() common.PublicKey {
|
|
return &PublicKey{p: s.p.GetPublicKey()}
|
|
}
|
|
|
|
// Sign a message using a secret key - in a beacon/validator client.
|
|
//
|
|
// In IETF draft BLS specification:
|
|
// Sign(SK, message) -> signature: a signing algorithm that generates
|
|
// a deterministic signature given a secret key SK and a message.
|
|
//
|
|
// In ETH2.0 specification:
|
|
// def Sign(SK: int, message: Bytes) -> BLSSignature
|
|
func (s *bls12SecretKey) Sign(msg []byte) common.Signature {
|
|
if featureconfig.Get().SkipBLSVerify {
|
|
return &Signature{}
|
|
}
|
|
signature := s.p.SignByte(msg)
|
|
return &Signature{s: signature}
|
|
}
|
|
|
|
// Marshal a secret key into a LittleEndian byte slice.
|
|
func (s *bls12SecretKey) Marshal() []byte {
|
|
keyBytes := s.p.Serialize()
|
|
if len(keyBytes) < params.BeaconConfig().BLSSecretKeyLength {
|
|
emptyBytes := make([]byte, params.BeaconConfig().BLSSecretKeyLength-len(keyBytes))
|
|
keyBytes = append(emptyBytes, keyBytes...)
|
|
}
|
|
return keyBytes
|
|
}
|
|
|
|
// IsZero checks if the secret key is a zero key.
|
|
func (s *bls12SecretKey) IsZero() bool {
|
|
return s.p.IsZero()
|
|
}
|