mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-26 05:17:22 +00:00
BLS pubkey from bytes cache (#3583)
* BLS pubkey from bytes cache * lint
This commit is contained in:
parent
f6a3fcb778
commit
ba4f45b180
@ -7,6 +7,7 @@ go_library(
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//shared/bytesutil:go_default_library",
|
||||
"@com_github_karlseguin_ccache//:go_default_library",
|
||||
"@com_github_phoreproject_bls//g1pubs:go_default_library",
|
||||
"@com_github_pkg_errors//:go_default_library",
|
||||
],
|
||||
|
@ -6,12 +6,16 @@ package bls
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/karlseguin/ccache"
|
||||
g1 "github.com/phoreproject/bls/g1pubs"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
)
|
||||
|
||||
var pubkeyCache = ccache.New(ccache.Configure())
|
||||
|
||||
// CurveOrder for the BLS12-381 curve.
|
||||
const CurveOrder = "52435875175126190479447740508185965837690552500527637822603658699938581184513"
|
||||
|
||||
@ -51,12 +55,18 @@ func SecretKeyFromBytes(priv []byte) (*SecretKey, error) {
|
||||
|
||||
// PublicKeyFromBytes creates a BLS public key from a LittleEndian byte slice.
|
||||
func PublicKeyFromBytes(pub []byte) (*PublicKey, error) {
|
||||
cv := pubkeyCache.Get(string(pub))
|
||||
if cv != nil && cv.Value() != nil {
|
||||
return cv.Value().(*PublicKey).Copy(), nil
|
||||
}
|
||||
b := bytesutil.ToBytes48(pub)
|
||||
k, err := g1.DeserializePublicKey(b)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not unmarshal bytes into public key")
|
||||
}
|
||||
return &PublicKey{val: k}, nil
|
||||
pk := &PublicKey{val: k}
|
||||
pubkeyCache.Set(string(pub), pk.Copy(), 48 * time.Hour)
|
||||
return pk, nil
|
||||
}
|
||||
|
||||
// SignatureFromBytes creates a BLS signature from a LittleEndian byte slice.
|
||||
@ -94,6 +104,11 @@ func (p *PublicKey) Marshal() []byte {
|
||||
return k[:]
|
||||
}
|
||||
|
||||
// Copy the public key to a new pointer reference.
|
||||
func (p *PublicKey) Copy() *PublicKey {
|
||||
return &PublicKey{val: p.val.Copy()}
|
||||
}
|
||||
|
||||
// Aggregate two public keys.
|
||||
func (p *PublicKey) Aggregate(p2 *PublicKey) *PublicKey {
|
||||
p1 := p.val
|
||||
|
Loading…
Reference in New Issue
Block a user