Skip to content

Commit

Permalink
tbls: refactor tss struct for testing (#136)
Browse files Browse the repository at this point in the history
Refactors the tbls.TSS struct to allow for arbitrary data during testing. This allows using know existing validator public keys when testing the scheduler, ensuring that duties that we calculate match those in mainnet.

category: refactor
ticket: #145
  • Loading branch information
corverroos committed Feb 28, 2022
1 parent 3edf484 commit 40ea6a8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
36 changes: 22 additions & 14 deletions tbls/tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,26 @@ type PubShare struct {
type TSS struct {
Verifier *share.FeldmanVerifier
NumShares int
}

// Threshold returns the secret sharing threshold.
func (t TSS) Threshold() int {
return len(t.Verifier.Commitments)
// PublicKey and Threshold are inferred from verifier commitments in NewTSS.

PublicKey *bls_sig.PublicKey
Threshold int
}

// PublicKey returns the root public key for given verifiers.
func (t TSS) PublicKey() (*bls_sig.PublicKey, error) {
pk := &bls_sig.PublicKey{}
err := pk.UnmarshalBinary(t.Verifier.Commitments[0].ToAffineCompressed())
func NewTSS(verifier *share.FeldmanVerifier, numShares int) (TSS, error) {
pk := new(bls_sig.PublicKey)
err := pk.UnmarshalBinary(verifier.Commitments[0].ToAffineCompressed())
if err != nil {
return nil, errors.Wrap(err, "unmarshal pubkey")
return TSS{}, errors.Wrap(err, "unmarshal pubkey")
}

return pk, nil
return TSS{
Verifier: verifier,
PublicKey: pk,
NumShares: numShares,
Threshold: len(verifier.Commitments),
}, nil
}

// GenerateTSS returns a new random instance of threshold signing scheme and associated SecretKeyShares.
Expand All @@ -73,14 +77,18 @@ func GenerateTSS(t, n int, reader io.Reader) (TSS, []*bls_sig.SecretKeyShare, er
return TSS{}, nil, errors.Wrap(err, "generate secret shares")
}

return TSS{Verifier: verifier, NumShares: n}, sks, nil
tss, err := NewTSS(verifier, n)
if err != nil {
return TSS{}, nil, err
}

return tss, sks, nil
}

// AggregateSignatures aggregates partial signatures over the given message.
// Returns aggregated signatures and slice of signers identifiers that had valid partial signatures.
func AggregateSignatures(tss TSS, partialSigs []*bls_sig.PartialSignature, msg []byte) (*bls_sig.Signature, []byte, error) {
threshold := tss.Threshold()
if len(partialSigs) < threshold {
if len(partialSigs) < tss.Threshold {
return nil, nil, errors.New("insufficient signatures")
}

Expand All @@ -105,7 +113,7 @@ func AggregateSignatures(tss TSS, partialSigs []*bls_sig.PartialSignature, msg [
signers = append(signers, psig.Identifier)
}

if len(validShares) < threshold {
if len(validShares) < tss.Threshold {
return nil, nil, errors.New("insufficient valid signatures")
}

Expand Down
6 changes: 2 additions & 4 deletions tbls/tss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestGenerateTSS(t *testing.T) {
require.NotNil(t, tss)
require.NotNil(t, secrets)

require.Equal(t, threshold, tss.Threshold())
require.Equal(t, threshold, tss.Threshold)
require.Equal(t, shares, tss.NumShares)
}

Expand All @@ -58,9 +58,7 @@ func TestAggregateSignatures(t *testing.T) {
sig, _, err := tbls.AggregateSignatures(tss, partialSigs, msg)
require.NoError(t, err)

pubkey, err := tss.PublicKey()
require.NoError(t, err)
result, err := tbls.Verify(pubkey, msg, sig)
result, err := tbls.Verify(tss.PublicKey, msg, sig)
require.NoError(t, err)
require.Equal(t, true, result)
}
24 changes: 11 additions & 13 deletions types/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,7 @@ func (m Manifest) MarshalJSON() ([]byte, error) {
verifiers = append(verifiers, c.ToAffineCompressed())
}

pk, err := tss.PublicKey()
if err != nil {
return nil, errors.Wrap(err, "get pubkey")
}

rawPK, err := pk.MarshalBinary()
rawPK, err := tss.PublicKey.MarshalBinary()
if err != nil {
return nil, errors.Wrap(err, "marshal pubkey")
}
Expand Down Expand Up @@ -198,12 +193,15 @@ func (m *Manifest) UnmarshalJSON(data []byte) error {
commitments = append(commitments, c)
}

dvs = append(dvs, tbls.TSS{
Verifier: &sharing.FeldmanVerifier{
Commitments: commitments,
},
NumShares: len(mjson.PeerENRs),
})
tss, err := tbls.NewTSS(
&sharing.FeldmanVerifier{Commitments: commitments},
len(mjson.PeerENRs),
)
if err != nil {
return err
}

dvs = append(dvs, tss)
}

*m = Manifest{
Expand Down Expand Up @@ -259,7 +257,7 @@ func getDescription(m Manifest) string {

var threshold int
if dv > 0 {
threshold = m.DVs[0].Threshold()
threshold = m.DVs[0].Threshold
}

return fmt.Sprintf("dv/%d/threshold/%d/peer/%d", dv, threshold, peers)
Expand Down
6 changes: 1 addition & 5 deletions types/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ func TestManifestJSON(t *testing.T) {
tss2 := manifest2.DVs[i]
require.Equal(t, tss1.NumShares, tss2.NumShares)
require.Equal(t, tss1.Verifier, tss2.Verifier)
pk1, err := tss1.PublicKey()
require.NoError(t, err)
pk2, err := tss2.PublicKey()
require.NoError(t, err)
require.Equal(t, pk1, pk2)
require.Equal(t, tss1.PublicKey, tss2.PublicKey)
}
}
}
Expand Down

0 comments on commit 40ea6a8

Please sign in to comment.