Skip to content

BLS Components for Simplex #3993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions simplex/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package simplex

import (
"context"
"encoding/asn1"
"errors"
"fmt"
Expand Down Expand Up @@ -40,12 +41,14 @@ type BLSVerifier struct {
chainID ids.ID
}

func NewBLSAuth(config *Config) (BLSSigner, BLSVerifier) {
func NewBLSAuth(config *Config) (BLSSigner, BLSVerifier, error) {
verifier, err := createVerifier(config)

return BLSSigner{
chainID: config.Ctx.ChainID,
subnetID: config.Ctx.SubnetID,
signBLS: config.SignBLS,
}, createVerifier(config)
}, verifier, err
}

// Sign returns a signature on the given message using BLS signature scheme.
Expand Down Expand Up @@ -110,21 +113,22 @@ func (v BLSVerifier) Verify(message []byte, signature []byte, signer simplex.Nod
return nil
}

func createVerifier(config *Config) BLSVerifier {
func createVerifier(config *Config) (BLSVerifier, error) {
verifier := BLSVerifier{
nodeID2PK: make(map[ids.NodeID]bls.PublicKey),
subnetID: config.Ctx.SubnetID,
chainID: config.Ctx.ChainID,
}

nodes := config.Validators.GetValidatorIDs(config.Ctx.SubnetID)
nodes, err := config.Validators.GetValidatorSet(context.Background(), 0, config.Ctx.SubnetID)
if err != nil {
config.Log.Error("failed to get validator set", zap.Error(err), zap.Stringer("subnetID", config.Ctx.SubnetID))
return BLSVerifier{}, err
}

for _, node := range nodes {
validator, ok := config.Validators.GetValidator(config.Ctx.SubnetID, node)
if !ok {
config.Log.Error("failed to get validator for node %s in subnet %s", zap.Stringer("node", node), zap.Stringer("subnetID", config.Ctx.SubnetID))
continue
}
verifier.nodeID2PK[node] = *validator.PublicKey
verifier.nodeID2PK[node.NodeID] = *node.PublicKey
}
return verifier

return verifier, nil
}
9 changes: 6 additions & 3 deletions simplex/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func TestBLSSignVerify(t *testing.T) {
config, err := newEngineConfig()
require.NoError(t, err)

signer, verifier := NewBLSAuth(config)
signer, verifier, err := NewBLSAuth(config)
require.NoError(t, err)

msg := "Begin at the beginning, and go on till you come to the end: then stop"

Expand All @@ -30,7 +31,8 @@ func TestBLSSignVerify(t *testing.T) {
func TestSignerNotInMemberSet(t *testing.T) {
config, err := newEngineConfig()
require.NoError(t, err)
signer, verifier := NewBLSAuth(config)
signer, verifier, err := NewBLSAuth(config)
require.NoError(t, err)

msg := "Begin at the beginning, and go on till you come to the end: then stop"

Expand All @@ -53,7 +55,8 @@ func TestSignerInvalidMessageEncoding(t *testing.T) {

sigBytes := bls.SignatureToBytes(sig)

_, verifier := NewBLSAuth(config)
_, verifier, err := NewBLSAuth(config)
require.NoError(t, err)
err = verifier.Verify(dummyMsg, sigBytes, config.Ctx.NodeID[:])
require.ErrorIs(t, err, errSignatureVerificationFailed)
}
12 changes: 10 additions & 2 deletions simplex/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
package simplex

import (
"context"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/logging"
)

type ValidatorInfo interface {
GetValidatorIDs(subnetID ids.ID) []ids.NodeID
GetValidator(subnetID ids.ID, nodeID ids.NodeID) (*validators.Validator, bool)
// GetValidatorSet returns the validators of the provided subnet at the
// requested P-chain height.
// The returned map should not be modified.
GetValidatorSet(
ctx context.Context,
height uint64,
subnetID ids.ID,
) (map[ids.NodeID]*validators.GetValidatorOutput, error)
}

// Config wraps all the parameters needed for a simplex engine
Expand Down
29 changes: 11 additions & 18 deletions simplex/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package simplex

import (
"context"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
Expand All @@ -15,34 +17,25 @@ var _ ValidatorInfo = (*testValidatorInfo)(nil)
// testValidatorInfo is a mock implementation of ValidatorInfo for testing purposes.
// it assumes all validators are in the same subnet and returns all of them for any subnetID.
type testValidatorInfo struct {
validators map[ids.NodeID]validators.Validator
}

func (v *testValidatorInfo) GetValidatorIDs(_ ids.ID) []ids.NodeID {
ids := make([]ids.NodeID, 0, len(v.validators))
for id := range v.validators {
ids = append(ids, id)
}
return ids
validators map[ids.NodeID]*validators.GetValidatorOutput
}

func (v *testValidatorInfo) GetValidator(_ ids.ID, nodeID ids.NodeID) (*validators.Validator, bool) {
if v.validators == nil {
return nil, false
}

val, exists := v.validators[nodeID]
return &val, exists
func (t *testValidatorInfo) GetValidatorSet(
context.Context,
uint64,
ids.ID,
) (map[ids.NodeID]*validators.GetValidatorOutput, error) {
return t.validators, nil
}

func newTestValidatorInfo(nodeIds []ids.NodeID, pks []*bls.PublicKey) *testValidatorInfo {
if len(nodeIds) != len(pks) {
panic("nodeIds and pks must have the same length")
}

vds := make(map[ids.NodeID]validators.Validator, len(pks))
vds := make(map[ids.NodeID]*validators.GetValidatorOutput, len(pks))
for i, pk := range pks {
validator := validators.Validator{
validator := &validators.GetValidatorOutput{
PublicKey: pk,
NodeID: nodeIds[i],
}
Expand Down