Skip to content

Commit

Permalink
some refactor and add 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 1, 2024
1 parent e89a3c6 commit 973cc2f
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 218 deletions.
48 changes: 18 additions & 30 deletions commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ var (
errDecodeCommitmentLength = errors.New("failed to decode commitment: invalid length")
errInvalidCiphersuite = errors.New("ciphersuite not available")
errInvalidLength = errors.New("invalid encoding length")
errHidingNonceCommitment = errors.New("invalid hiding nonce commitment (nil, identity, or generator)")
errBindingNonceCommitment = errors.New("invalid binding nonce commitment (nil, identity, or generator)")
)

// Commitment is a participant's one-time commitment holding its identifier, and hiding and binding nonces.
Expand All @@ -49,11 +47,6 @@ func (c *Commitment) Copy() *Commitment {
}
}

// EncodedSize returns the byte size of the output of Encode().
func EncodedSize(g group.Group) uint64 {
return 1 + 8 + 8 + 2*uint64(g.ElementLength())
}

// CommitmentList is a sortable list of commitments with search functions.
type CommitmentList []*Commitment

Expand Down Expand Up @@ -128,7 +121,7 @@ func (c CommitmentList) Encode() []byte {
}

g := c[0].Group
size := 1 + 8 + uint64(n)*EncodedSize(g)
size := 1 + 8 + uint64(n)*encodedLength(encCommitment, g)
out := make([]byte, 9, size)
out[0] = byte(g)
binary.LittleEndian.PutUint64(out[1:9], uint64(n))
Expand All @@ -151,7 +144,7 @@ func DecodeList(data []byte) (CommitmentList, error) {
}

n := binary.LittleEndian.Uint64(data[1:9])
es := EncodedSize(g)
es := encodedLength(encCommitment, g)
size := 1 + 8 + n*es

if uint64(len(data)) != size {
Expand Down Expand Up @@ -260,17 +253,8 @@ func (c *Configuration) ValidateCommitment(commitment *Commitment) error {
return fmt.Errorf("the commitment list has a nil commitment")
}

if commitment.SignerID == 0 {
return fmt.Errorf("signer identifier for commitment %d is 0", commitment.CommitmentID)
}

if commitment.SignerID > c.MaxSigners {
return fmt.Errorf(
"signer identifier %d for commitment %d is above allowed values (%d)",
commitment.SignerID,
commitment.CommitmentID,
c.MaxSigners,
)
if err := c.validateIdentifier(commitment.SignerID); err != nil {
return fmt.Errorf("invalid identifier for signer in commitment %d, the %w", commitment.CommitmentID, err)
}

if commitment.Group != c.group {
Expand All @@ -283,18 +267,22 @@ func (c *Configuration) ValidateCommitment(commitment *Commitment) error {
)
}

generator := c.group.Base()

if commitment.HidingNonceCommitment == nil || commitment.HidingNonceCommitment.IsIdentity() ||
commitment.HidingNonceCommitment.Equal(generator) == 1 {
return fmt.Errorf("commitment %d for signer %d has an %w", commitment.CommitmentID,
commitment.SignerID, errHidingNonceCommitment)
if err := c.validateGroupElement(commitment.HidingNonceCommitment); err != nil {
return fmt.Errorf(
"invalid commitment %d for signer %d, the hiding nonce commitment %w",
commitment.CommitmentID,
commitment.SignerID,
err,
)
}

if commitment.BindingNonceCommitment == nil || commitment.BindingNonceCommitment.IsIdentity() ||
commitment.BindingNonceCommitment.Equal(generator) == 1 {
return fmt.Errorf("commitment %d for signer %d has an %w", commitment.CommitmentID,
commitment.SignerID, errBindingNonceCommitment)
if err := c.validateGroupElement(commitment.BindingNonceCommitment); err != nil {
return fmt.Errorf(
"invalid commitment %d for signer %d, the binding nonce commitment %w",
commitment.CommitmentID,
commitment.SignerID,
err,
)
}

// Validate that the commitment comes from a registered signer.
Expand Down
18 changes: 5 additions & 13 deletions coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Configuration) AggregateSignatures(
commitments CommitmentList,
verify bool,
) (*Signature, error) {
groupCommitment, bindingFactors, participants, err := c.PrepareSignatureShareVerification(message, commitments)
groupCommitment, bindingFactors, participants, err := c.prepareSignatureShareVerification(message, commitments)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -89,15 +89,15 @@ func (c *Configuration) VerifySignatureShare(
message []byte,
commitments CommitmentList,
) error {
groupCommitment, bindingFactors, participants, err := c.PrepareSignatureShareVerification(message, commitments)
groupCommitment, bindingFactors, participants, err := c.prepareSignatureShareVerification(message, commitments)
if err != nil {
return err
}

return c.verifySignatureShare(sigShare, message, commitments, participants, groupCommitment, bindingFactors)
}

func (c *Configuration) PrepareSignatureShareVerification(message []byte,
func (c *Configuration) prepareSignatureShareVerification(message []byte,
commitments CommitmentList,
) (*group.Element, BindingFactors, []*group.Scalar, error) {
if !c.verified {
Expand Down Expand Up @@ -136,16 +136,8 @@ func (c *Configuration) validateSignatureShareExtensive(sigShare *SignatureShare
return err
}

if sigShare.SignerIdentifier == 0 {
return errors.New("signature share's signer identifier is 0 (invalid)")
}

if sigShare.SignerIdentifier > c.MaxSigners {
return fmt.Errorf(
"signature share has invalid ID %d, above authorized range [1:%d]",
sigShare.SignerIdentifier,
c.MaxSigners,
)
if err := c.validateIdentifier(sigShare.SignerIdentifier); err != nil {
return fmt.Errorf("invalid identifier for signer in signature share, the %w", err)
}

if sigShare.Group != c.group {
Expand Down
Loading

0 comments on commit 973cc2f

Please sign in to comment.