Skip to content

Commit

Permalink
some refacto and added tests
Browse files Browse the repository at this point in the history
Signed-off-by: bytemare <3641580+bytemare@users.noreply.github.com>
  • Loading branch information
bytemare committed Sep 2, 2024
1 parent 973cc2f commit 0db81f6
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 257 deletions.
15 changes: 13 additions & 2 deletions commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// LICENSE file in the root directory of this source tree or at
// https://spdx.org/licenses/MIT.html

// Package commitment defines the FROST Signer commitment.
package frost

import (
Expand Down Expand Up @@ -238,7 +237,7 @@ func (c CommitmentList) groupCommitment(bf BindingFactors) *group.Element {
}

func (c *Configuration) isSignerRegistered(sid uint64) bool {
for _, peer := range c.SignerPublicKeys {
for _, peer := range c.SignerPublicKeyShares {
if peer.ID == sid {
return true
}
Expand All @@ -249,6 +248,12 @@ func (c *Configuration) isSignerRegistered(sid uint64) bool {

// ValidateCommitment returns an error if the commitment is not valid.
func (c *Configuration) ValidateCommitment(commitment *Commitment) error {
if !c.verified || !c.keysVerified {
if err := c.Init(); err != nil {
return err

Check warning on line 253 in commitment.go

View check run for this annotation

Codecov / codecov/patch

commitment.go#L252-L253

Added lines #L252 - L253 were not covered by tests
}
}

if commitment == nil {
return fmt.Errorf("the commitment list has a nil commitment")
}
Expand Down Expand Up @@ -322,6 +327,12 @@ func (c *Configuration) validateCommitmentListLength(commitments CommitmentList)
// - no duplicated in signer identifiers
// - all commitment signer identifiers are registered in the configuration

Check failure on line 328 in commitment.go

View workflow job for this annotation

GitHub Actions / Lint / GolangCI-Lint

Comment should end in a period (godot)
func (c *Configuration) ValidateCommitmentList(commitments CommitmentList) error {

Check failure on line 329 in commitment.go

View workflow job for this annotation

GitHub Actions / Lint / GolangCI-Lint

cognitive complexity 18 of func `(*Configuration).ValidateCommitmentList` is high (> 16) (gocognit)
if !c.verified || !c.keysVerified {
if err := c.Init(); err != nil {
return err

Check warning on line 332 in commitment.go

View check run for this annotation

Codecov / codecov/patch

commitment.go#L331-L332

Added lines #L331 - L332 were not covered by tests
}
}

if err := c.validateCommitmentListLength(commitments); err != nil {
return err
}
Expand Down
18 changes: 12 additions & 6 deletions coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func (c *Configuration) AggregateSignatures(
commitments CommitmentList,
verify bool,
) (*Signature, error) {
if !c.verified || !c.keysVerified {
if err := c.Init(); err != nil {
return nil, err

Check warning on line 47 in coordinator.go

View check run for this annotation

Codecov / codecov/patch

coordinator.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}
}

groupCommitment, bindingFactors, participants, err := c.prepareSignatureShareVerification(message, commitments)
if err != nil {
return nil, err
Expand Down Expand Up @@ -89,6 +95,12 @@ func (c *Configuration) VerifySignatureShare(
message []byte,
commitments CommitmentList,
) error {
if !c.verified || !c.keysVerified {
if err := c.Init(); err != nil {
return err
}
}

groupCommitment, bindingFactors, participants, err := c.prepareSignatureShareVerification(message, commitments)
if err != nil {
return err
Expand All @@ -100,12 +112,6 @@ func (c *Configuration) VerifySignatureShare(
func (c *Configuration) prepareSignatureShareVerification(message []byte,
commitments CommitmentList,
) (*group.Element, BindingFactors, []*group.Scalar, error) {
if !c.verified {
if err := c.verify(); err != nil {
return nil, nil, nil, err
}
}

commitments.Sort()

// Validate general consistency of the commitment list.
Expand Down
32 changes: 16 additions & 16 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ func encodedLength(encID byte, g group.Group, other ...uint64) uint64 {
func (c *Configuration) Encode() []byte {
g := group.Group(c.Ciphersuite)
pksLen := encodedLength(encPubKeyShare, g, c.Threshold*uint64(g.ElementLength()))
size := encodedLength(encConf, g, uint64(len(c.SignerPublicKeys))*pksLen)
size := encodedLength(encConf, g, uint64(len(c.SignerPublicKeyShares))*pksLen)
out := make([]byte, 25, size)
out[0] = byte(g)
binary.LittleEndian.PutUint64(out[1:9], c.Threshold)
binary.LittleEndian.PutUint64(out[9:17], c.MaxSigners)
binary.LittleEndian.PutUint64(out[17:25], uint64(len(c.SignerPublicKeys)))
binary.LittleEndian.PutUint64(out[17:25], uint64(len(c.SignerPublicKeyShares)))

out = append(out, c.GroupPublicKey.Encode()...)

for _, pk := range c.SignerPublicKeys {
for _, pk := range c.SignerPublicKeyShares {
out = append(out, pk.Encode()...)
}

Expand Down Expand Up @@ -106,7 +106,7 @@ func (c *Configuration) decodeHeader(data []byte) (*confHeader, error) {
pksLen := encodedLength(encPubKeyShare, g, t*uint64(g.ElementLength()))
length := encodedLength(encConf, g, nPks*pksLen)

if t > n {
if t == 0 || t > n {
return nil, errInvalidConfigEncoding
}

Expand Down Expand Up @@ -135,12 +135,15 @@ func (c *Configuration) decode(header *confHeader, data []byte) error {
pks := make([]*PublicKeyShare, header.nPks)

conf := &Configuration{
Ciphersuite: Ciphersuite(header.g),
Threshold: header.t,
MaxSigners: header.n,
GroupPublicKey: gpk,
SignerPublicKeys: pks,
group: header.g,
Ciphersuite: Ciphersuite(header.g),
Threshold: header.t,
MaxSigners: header.n,
GroupPublicKey: gpk,
SignerPublicKeyShares: pks,
}

if err := conf.verifyConfiguration(); err != nil {
return err
}

for j := range header.nPks {
Expand All @@ -149,25 +152,22 @@ func (c *Configuration) decode(header *confHeader, data []byte) error {
return fmt.Errorf("could not decode signer public key share for signer %d: %w", j, err)
}

if err := conf.validatePublicKeyShare(pk); err != nil {
return err
}

offset += pksLen
pks[j] = pk
}

if err := conf.verify(); err != nil {
if err := conf.verifySignerPublicKeyShares(); err != nil {
return err

Check warning on line 160 in encoding.go

View check run for this annotation

Codecov / codecov/patch

encoding.go#L160

Added line #L160 was not covered by tests
}

c.Ciphersuite = conf.Ciphersuite
c.Threshold = conf.Threshold
c.MaxSigners = conf.MaxSigners
c.GroupPublicKey = gpk
c.SignerPublicKeys = pks
c.SignerPublicKeyShares = pks
c.group = group.Group(conf.Ciphersuite)
c.verified = true
c.keysVerified = true

return nil
}
Expand Down
20 changes: 10 additions & 10 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func Example_signer() {

// This is how to set up the Configuration for FROST, the same for every signer and the coordinator.
configuration := &frost.Configuration{
Ciphersuite: ciphersuite,
Threshold: threshold,
MaxSigners: maxSigners,
GroupPublicKey: groupPublicKey,
SignerPublicKeys: publicKeyShares,
Ciphersuite: ciphersuite,
Threshold: threshold,
MaxSigners: maxSigners,
GroupPublicKey: groupPublicKey,
SignerPublicKeyShares: publicKeyShares,
}

if err := configuration.Init(); err != nil {
Expand Down Expand Up @@ -126,11 +126,11 @@ func Example_coordinator() {

// This is how to set up the Configuration for FROST, the same for every signer and the coordinator.
configuration := &frost.Configuration{
Ciphersuite: ciphersuite,
Threshold: threshold,
MaxSigners: maxSigners,
GroupPublicKey: groupPublicKey,
SignerPublicKeys: publicKeyShares,
Ciphersuite: ciphersuite,
Threshold: threshold,
MaxSigners: maxSigners,
GroupPublicKey: groupPublicKey,
SignerPublicKeyShares: publicKeyShares,
}

if err := configuration.Init(); err != nil {
Expand Down
Loading

0 comments on commit 0db81f6

Please sign in to comment.