Skip to content
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

Validate the number of commitments and signature shares #14

Merged
merged 2 commits into from
Oct 1, 2024
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
21 changes: 20 additions & 1 deletion frost/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ package frost

import (
"errors"
"fmt"
"math/big"
)

// Coordinator represents a coordinator of the [FROST] signing protocol.
type Coordinator struct {
Participant
threshold int
}

// NewCoordinator creates a new [FROST] Coordinator instance.
func NewCoordinator(
ciphersuite Ciphersuite,
publicKey *Point,
threshold int,
) *Coordinator {
return &Coordinator{
Participant: Participant{
ciphersuite: ciphersuite,
publicKey: publicKey,
},
threshold: threshold,
}
}

Expand Down Expand Up @@ -64,7 +68,22 @@ func (c *Coordinator) Aggregate(
// - (R, z), a Schnorr signature consisting of an Element R and
// Scalar z.

// TODO: validate the number of signature shares
if len(signatureShares) < c.threshold {
return nil, fmt.Errorf(
"not enough shares; has [%d] for threshold [%d]",
len(signatureShares),
c.threshold,
)
}
lukasz-zimnoch marked this conversation as resolved.
Show resolved Hide resolved

if len(commitments) != len(signatureShares) {
return nil, fmt.Errorf(
"the number of commitments and signature shares do not match; "+
"has [%d] commitments and [%d] signature shares",
len(commitments),
len(signatureShares),
)
}

validationErrors, _ := c.validateGroupCommitmentsBase(commitments)
if len(validationErrors) != 0 {
Expand Down
59 changes: 59 additions & 0 deletions frost/coordinator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package frost

import (
"testing"

"threshold.network/roast/internal/testutils"
)

// This test covers failure paths in the Aggregate function. The happy path is
// covered as a part of the roundtrip test in frost_test.go.
func TestAggregate_Failures(t *testing.T) {
message := []byte("For even the very wise cannot see all ends")

signers := createSigners(t)
publicKey := signers[0].publicKey

nonces, commitments := executeRound1(t, signers)
signatureShares := executeRound2(t, signers, message, nonces, commitments)

coordinator := NewCoordinator(ciphersuite, publicKey, threshold)

tests := map[string]struct {
numberOfCommitments int
numberOfSignatureShares int
expectedErr string
}{
"number of commitments and signature shares do not match": {
numberOfCommitments: groupSize,
numberOfSignatureShares: groupSize - 1,
expectedErr: "the number of commitments and signature shares do not match; has [100] commitments and [99] signature shares",
},
"number of commitments and signature shares below threshold": {
numberOfCommitments: threshold - 1,
numberOfSignatureShares: threshold - 1,
expectedErr: "not enough shares; has [50] for threshold [51]",
},
}

for testName, test := range tests {
t.Run(testName, func(t *testing.T) {
signature, err := coordinator.Aggregate(
message,
commitments[:test.numberOfCommitments],
signatureShares[:test.numberOfSignatureShares],
)

testutils.AssertStringsEqual(
t,
"aggregate signature share error message",
test.expectedErr,
err.Error(),
)

if signature != nil {
t.Error("expected nil signature")
}
})
}
}
2 changes: 1 addition & 1 deletion frost/frost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestFrostRoundtrip(t *testing.T) {
nonces, commitments := executeRound1(t, signers)
signatureShares := executeRound2(t, signers, message, nonces, commitments)

coordinator := NewCoordinator(ciphersuite, publicKey)
coordinator := NewCoordinator(ciphersuite, publicKey, threshold)
signature, err := coordinator.Aggregate(message, commitments, signatureShares)
if err != nil {
t.Fatal(err)
Expand Down
Loading