Skip to content

Commit

Permalink
tbls: remove legacy Kryptology-based code (#2091)
Browse files Browse the repository at this point in the history
This PR removes the old `tbls` package content, and replaces it with `tbls/v2` content.

As a consequence, the `tbls/v2` package doesn't exist anymore.

All imports have been refactored to accomodate this change.

Kryptology `tbls.Implementation` will be removed on a follow-up PR. 

category: refactor
ticket: #2055
  • Loading branch information
gsora authored Apr 12, 2023
1 parent e755677 commit cca6d5e
Show file tree
Hide file tree
Showing 51 changed files with 741 additions and 1,497 deletions.
16 changes: 8 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ import (
"github.com/obolnetwork/charon/core/validatorapi"
"github.com/obolnetwork/charon/eth2util"
"github.com/obolnetwork/charon/p2p"
tblsv2 "github.com/obolnetwork/charon/tbls/v2"
tblsconv2 "github.com/obolnetwork/charon/tbls/v2/tblsconv"
"github.com/obolnetwork/charon/tbls"
"github.com/obolnetwork/charon/tbls/tblsconv"
"github.com/obolnetwork/charon/testutil/beaconmock"
)

Expand Down Expand Up @@ -98,7 +98,7 @@ type TestConfig struct {
// LcastTransportFunc provides an in-memory leader cast transport.
LcastTransportFunc func() leadercast.Transport
// SimnetKeys provides private key shares for the simnet validatormock signer.
SimnetKeys []tblsv2.PrivateKey
SimnetKeys []tbls.PrivateKey
// SimnetBMockOpts defines additional simnet beacon mock options.
SimnetBMockOpts []beaconmock.Option
// BroadcastCallback is called when a duty is completed and sent to the broadcast component.
Expand Down Expand Up @@ -132,11 +132,11 @@ func Run(ctx context.Context, conf Config) (err error) {

version.LogInfo(ctx, "Charon starting")

tblsv2.SetImplementation(tblsv2.Herumi{})
tbls.SetImplementation(tbls.Herumi{})

if !featureset.Enabled(featureset.HerumiBLS) {
log.Info(ctx, "Enabling Kryptology BLS signature backend")
tblsv2.SetImplementation(tblsv2.Kryptology{})
tbls.SetImplementation(tbls.Kryptology{})
}

// Wire processes and their dependencies
Expand Down Expand Up @@ -325,7 +325,7 @@ func wireCoreWorkflow(ctx context.Context, life *lifecycle.Manager, conf Config,
corePubkeys []core.PubKey
eth2Pubkeys []eth2p0.BLSPubKey
pubshares []eth2p0.BLSPubKey
allPubSharesByKey = make(map[core.PubKey]map[int]tblsv2.PublicKey) // map[pubkey]map[shareIdx]pubshare
allPubSharesByKey = make(map[core.PubKey]map[int]tbls.PublicKey) // map[pubkey]map[shareIdx]pubshare
feeRecipientAddrByCorePubkey = make(map[core.PubKey]string)
)
for i, dv := range lock.Validators {
Expand All @@ -339,9 +339,9 @@ func wireCoreWorkflow(ctx context.Context, life *lifecycle.Manager, conf Config,
return err
}

allPubShares := make(map[int]tblsv2.PublicKey)
allPubShares := make(map[int]tbls.PublicKey)
for i, b := range dv.PubShares {
pubshare, err := tblsconv2.PubkeyFromBytes(b)
pubshare, err := tblsconv.PubkeyFromBytes(b)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions cluster/distvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package cluster

import (
tblsv2 "github.com/obolnetwork/charon/tbls/v2"
tblsconv2 "github.com/obolnetwork/charon/tbls/v2/tblsconv"
"github.com/obolnetwork/charon/tbls"
"github.com/obolnetwork/charon/tbls/tblsconv"
)

// DistValidator is a distributed validator (1x32ETH) managed by the cluster.
Expand All @@ -21,8 +21,8 @@ type DistValidator struct {
}

// PublicKey returns the validator BLS group public key.
func (v DistValidator) PublicKey() (tblsv2.PublicKey, error) {
return tblsconv2.PubkeyFromBytes(v.PubKey)
func (v DistValidator) PublicKey() (tbls.PublicKey, error) {
return tblsconv.PubkeyFromBytes(v.PubKey)
}

// PublicKeyHex returns the validator hex group public key.
Expand All @@ -31,8 +31,8 @@ func (v DistValidator) PublicKeyHex() string {
}

// PublicShare returns a peer's threshold BLS public share.
func (v DistValidator) PublicShare(peerIdx int) (tblsv2.PublicKey, error) {
return tblsconv2.PubkeyFromBytes(v.PubShares[peerIdx])
func (v DistValidator) PublicShare(peerIdx int) (tbls.PublicKey, error) {
return tblsconv.PubkeyFromBytes(v.PubShares[peerIdx])
}

// distValidatorJSONv1x1 is the json formatter of DistValidator for versions v1.0.0 and v1.1.0.
Expand Down
10 changes: 5 additions & 5 deletions cluster/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/obolnetwork/charon/app/k1util"
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/eth2util"
tblsv2 "github.com/obolnetwork/charon/tbls/v2"
"github.com/obolnetwork/charon/tbls"
)

// FetchDefinition fetches cluster definition file from a remote URI.
Expand Down Expand Up @@ -107,11 +107,11 @@ func signOperator(secret *k1.PrivateKey, def Definition, operator Operator) (Ope
}

// aggSign returns a bls aggregate signatures of the message signed by all the shares.
func aggSign(secrets [][]tblsv2.PrivateKey, message []byte) ([]byte, error) {
var sigs []tblsv2.Signature
func aggSign(secrets [][]tbls.PrivateKey, message []byte) ([]byte, error) {
var sigs []tbls.Signature
for _, shares := range secrets {
for _, share := range shares {
sig, err := tblsv2.Sign(share, message)
sig, err := tbls.Sign(share, message)
if err != nil {
return nil, err
}
Expand All @@ -120,7 +120,7 @@ func aggSign(secrets [][]tblsv2.PrivateKey, message []byte) ([]byte, error) {
}
}

aggSig, err := tblsv2.Aggregate(sigs)
aggSig, err := tbls.Aggregate(sigs)
if err != nil {
return nil, errors.Wrap(err, "aggregate signatures")
}
Expand Down
4 changes: 2 additions & 2 deletions cluster/helpers_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (

"github.com/obolnetwork/charon/app/k1util"
"github.com/obolnetwork/charon/eth2util"
tblsv2 "github.com/obolnetwork/charon/tbls/v2"
"github.com/obolnetwork/charon/tbls"
"github.com/obolnetwork/charon/testutil"
)

func TestMain(m *testing.M) {
tblsv2.SetImplementation(tblsv2.Herumi{})
tbls.SetImplementation(tbls.Herumi{})
os.Exit(m.Run())
}

Expand Down
12 changes: 6 additions & 6 deletions cluster/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/app/z"
tblsv2 "github.com/obolnetwork/charon/tbls/v2"
tblsconv2 "github.com/obolnetwork/charon/tbls/v2/tblsconv"
"github.com/obolnetwork/charon/tbls"
"github.com/obolnetwork/charon/tbls/tblsconv"
)

// Lock extends the cluster config Definition with bls threshold public keys and checksums.
Expand Down Expand Up @@ -139,15 +139,15 @@ func (l Lock) VerifySignatures() error {
return errors.New("empty lock aggregate signature")
}

sig, err := tblsconv2.SignatureFromBytes(l.SignatureAggregate)
sig, err := tblsconv.SignatureFromBytes(l.SignatureAggregate)
if err != nil {
return err
}

var pubkeys []tblsv2.PublicKey
var pubkeys []tbls.PublicKey
for _, val := range l.Validators {
for _, share := range val.PubShares {
pubkey, err := tblsconv2.PubkeyFromBytes(share)
pubkey, err := tblsconv.PubkeyFromBytes(share)
if err != nil {
return err
}
Expand All @@ -160,7 +160,7 @@ func (l Lock) VerifySignatures() error {
return err
}

err = tblsv2.VerifyAggregate(pubkeys, sig, hash[:])
err = tbls.VerifyAggregate(pubkeys, sig, hash[:])
if err != nil {
return errors.Wrap(err, "verify lock signature aggregate")
}
Expand Down
16 changes: 8 additions & 8 deletions cluster/test_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (

"github.com/obolnetwork/charon/eth2util"
"github.com/obolnetwork/charon/eth2util/enr"
tblsv2 "github.com/obolnetwork/charon/tbls/v2"
"github.com/obolnetwork/charon/tbls"
"github.com/obolnetwork/charon/testutil"
)

// NewForT returns a new cluster lock with dv number of distributed validators, k threshold and n peers.
// It also returns the peer p2p keys and BLS secret shares. If the seed is zero a random cluster on available loopback
// ports is generated, else a deterministic cluster is generated.
// Note this is not defined in testutil since it is tightly coupled with the cluster package.
func NewForT(t *testing.T, dv, k, n, seed int, opts ...func(*Definition)) (Lock, []*k1.PrivateKey, [][]tblsv2.PrivateKey) {
func NewForT(t *testing.T, dv, k, n, seed int, opts ...func(*Definition)) (Lock, []*k1.PrivateKey, [][]tbls.PrivateKey) {
t.Helper()

var (
vals []DistValidator
p2pKeys []*k1.PrivateKey
ops []Operator
dvShares [][]tblsv2.PrivateKey
dvShares [][]tbls.PrivateKey
)

random := io.Reader(rand.New(rand.NewSource(int64(seed)))) //nolint:gosec // Explicit use of weak random generator for determinism.
Expand All @@ -39,21 +39,21 @@ func NewForT(t *testing.T, dv, k, n, seed int, opts ...func(*Definition)) (Lock,
}

for i := 0; i < dv; i++ {
rootSecret, err := tblsv2.GenerateSecretKey()
rootSecret, err := tbls.GenerateSecretKey()
require.NoError(t, err)

rootPublic, err := tblsv2.SecretToPublicKey(rootSecret)
rootPublic, err := tbls.SecretToPublicKey(rootSecret)
require.NoError(t, err)

shares, err := tblsv2.ThresholdSplit(rootSecret, uint(n), uint(k))
shares, err := tbls.ThresholdSplit(rootSecret, uint(n), uint(k))
require.NoError(t, err)

var pubshares [][]byte
var privshares []tblsv2.PrivateKey
var privshares []tbls.PrivateKey
for i := 0; i < n; i++ {
sharePrivkey := shares[i+1] // Share indexes are 1-indexed.

sharePub, err := tblsv2.SecretToPublicKey(sharePrivkey)
sharePub, err := tbls.SecretToPublicKey(sharePrivkey)
require.NoError(t, err)

pubshares = append(pubshares, sharePub[:])
Expand Down
20 changes: 10 additions & 10 deletions cmd/combine/combine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/cluster"
"github.com/obolnetwork/charon/eth2util/keystore"
tblsv2 "github.com/obolnetwork/charon/tbls/v2"
"github.com/obolnetwork/charon/tbls"
)

// Combine combines validator keys contained in inputDir, and writes the original BLS12-381 private keys.
Expand Down Expand Up @@ -63,7 +63,7 @@ func Combine(ctx context.Context, inputDir, outputDir string, force bool, opts .
return errors.Wrap(err, "cannot open lock file")
}

privkeys := make(map[int][]tblsv2.PrivateKey)
privkeys := make(map[int][]tbls.PrivateKey)

for _, pkp := range possibleKeyPaths {
secrets, err := keystore.LoadKeys(pkp)
Expand All @@ -76,7 +76,7 @@ func Combine(ctx context.Context, inputDir, outputDir string, force bool, opts .
}
}

var combinedKeys []tblsv2.PrivateKey
var combinedKeys []tbls.PrivateKey

for idx, pkSet := range privkeys {
log.Info(ctx, "Recombining key share", z.Int("validator_number", idx))
Expand All @@ -89,7 +89,7 @@ func Combine(ctx context.Context, inputDir, outputDir string, force bool, opts .
return errors.New("insufficient number of keys", z.Int("validator_number", idx))
}

secret, err := tblsv2.RecoverSecret(shares, uint(len(lock.Operators)), uint(lock.Threshold))
secret, err := tbls.RecoverSecret(shares, uint(len(lock.Operators)), uint(lock.Threshold))
if err != nil {
return errors.Wrap(err, "cannot recover shares", z.Int("validator_number", idx))
}
Expand All @@ -102,7 +102,7 @@ func Combine(ctx context.Context, inputDir, outputDir string, force bool, opts .
return errors.Wrap(err, "public key for validator from lockfile", z.Int("validator_number", idx))
}

genPubkey, err := tblsv2.SecretToPublicKey(secret)
genPubkey, err := tbls.SecretToPublicKey(secret)
if err != nil {
return errors.Wrap(err, "public key for validator from generated secret", z.Int("validator_number", idx))
}
Expand All @@ -127,12 +127,12 @@ func Combine(ctx context.Context, inputDir, outputDir string, force bool, opts .
return nil
}

func secretsToShares(lock cluster.Lock, secrets []tblsv2.PrivateKey) (map[int]tblsv2.PrivateKey, error) {
func secretsToShares(lock cluster.Lock, secrets []tbls.PrivateKey) (map[int]tbls.PrivateKey, error) {
n := len(lock.Operators)

resp := make(map[int]tblsv2.PrivateKey)
resp := make(map[int]tbls.PrivateKey)
for idx, secret := range secrets {
pubkey, err := tblsv2.SecretToPublicKey(secret)
pubkey, err := tbls.SecretToPublicKey(secret)
if err != nil {
return nil, errors.Wrap(err, "pubkey from share")
}
Expand Down Expand Up @@ -171,14 +171,14 @@ func secretsToShares(lock cluster.Lock, secrets []tblsv2.PrivateKey) (map[int]tb
// WithInsecureKeysForT is a functional option for Combine that will use the insecure keystore.StoreKeysInsecure function.
func WithInsecureKeysForT(*testing.T) func(*options) {
return func(o *options) {
o.keyStoreFunc = func(secrets []tblsv2.PrivateKey, dir string) error {
o.keyStoreFunc = func(secrets []tbls.PrivateKey, dir string) error {
return keystore.StoreKeysInsecure(secrets, dir, keystore.ConfirmInsecureKeys)
}
}
}

type options struct {
keyStoreFunc func(secrets []tblsv2.PrivateKey, dir string) error
keyStoreFunc func(secrets []tbls.PrivateKey, dir string) error
}

// loadLockfile loads a lockfile from one of the charon directories contained in dir.
Expand Down
Loading

0 comments on commit cca6d5e

Please sign in to comment.