(no commit message provided)

This commit is contained in:
Prad Nukala
2024-07-06 00:34:41 -04:00
committed by Prad Nukala (aider)
parent 5fd43dfd6b
commit 2f976209db
345 changed files with 409 additions and 72177 deletions
-13
View File
@@ -1,13 +0,0 @@
---
aliases: [README]
tags: []
title: README
linter-yaml-title-alias: README
date created: Wednesday, April 17th 2024, 4:11:40 pm
date modified: Thursday, April 18th 2024, 8:19:25 am
---
## One Round Threshold ECDSA with Identifiable Abort (GG20)
This package is an implementation of the DKG part of
[One Round Threshold ECDSA with Identifiable Abort](https://eprint.iacr.org/2020/540.pdf).
-97
View File
@@ -1,97 +0,0 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
// Package gennaro is an implementation of the DKG part of https://eprint.iacr.org/2020/540.pdf
package gennaro
import (
"crypto/elliptic"
"fmt"
"github.com/onsonr/hway/crypto/core/curves"
"github.com/onsonr/hway/crypto/internal"
v1 "github.com/onsonr/hway/crypto/sharing/v1"
)
// Participant is a DKG player that contains information needed to perform DKG rounds
// and yield a secret key share and public key when finished
type Participant struct {
round int
curve elliptic.Curve
scalar curves.EcScalar
otherParticipantShares map[uint32]*dkgParticipantData
id uint32
skShare *curves.Element
verificationKey *v1.ShareVerifier
feldman *v1.Feldman
pedersen *v1.Pedersen
pedersenResult *v1.PedersenResult
}
// NewParticipant creates a participant ready to perform a DKG
// `id` is the integer value identifier for this participant
// `threshold` is the minimum bound for the secret sharing scheme
// `generator` is the blinding factor generator used by pedersen's verifiable secret sharing
// `otherParticipants` is the integer value identifiers for the other participants
// `id` and `otherParticipants` must be the set of integers 1,2,....,n
func NewParticipant(id, threshold uint32, generator *curves.EcPoint, scalar curves.EcScalar, otherParticipants ...uint32) (*Participant, error) {
if generator == nil || len(otherParticipants) == 0 {
return nil, internal.ErrNilArguments
}
err := validIds(append(otherParticipants, id))
if err != nil {
return nil, err
}
limit := uint32(len(otherParticipants)) + 1
feldman, err := v1.NewFeldman(threshold, limit, generator.Curve)
if err != nil {
return nil, err
}
pedersen, err := v1.NewPedersen(threshold, limit, generator)
if err != nil {
return nil, err
}
otherParticipantShares := make(map[uint32]*dkgParticipantData, len(otherParticipants))
for _, id := range otherParticipants {
otherParticipantShares[id] = &dkgParticipantData{
Id: id,
}
}
return &Participant{
id: id,
round: 1,
curve: generator.Curve,
scalar: scalar,
feldman: feldman,
pedersen: pedersen,
otherParticipantShares: otherParticipantShares,
}, nil
}
// Determines if the SSIDs are exactly the values 1..n.
func validIds(ids []uint32) error {
// Index
idMap := make(map[uint32]bool, len(ids))
for _, id := range ids {
idMap[id] = true
}
// Check
for i := 1; i <= len(ids); i++ {
if ok := idMap[uint32(i)]; !ok {
return fmt.Errorf("the ID list %v is invalid. Values must be 1,2,..,n.", ids)
}
}
return nil
}
type dkgParticipantData struct {
Id uint32
Share *v1.ShamirShare
Verifiers []*v1.ShareVerifier
}
-48
View File
@@ -1,48 +0,0 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package gennaro
import (
"math/big"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/require"
"github.com/onsonr/hway/crypto/core/curves"
"github.com/onsonr/hway/crypto/internal"
)
var testGenerator, _ = curves.NewScalarBaseMult(btcec.S256(), big.NewInt(3333))
func TestNewParticipantWorks(t *testing.T) {
p, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
require.NoError(t, err)
require.NotNil(t, p)
require.Equal(t, p.id, uint32(1))
require.Equal(t, p.round, 1)
require.Equal(t, p.curve, btcec.S256())
require.NotNil(t, p.pedersen)
require.NotNil(t, p.feldman)
require.Nil(t, p.pedersenResult)
require.NotNil(t, p.otherParticipantShares)
require.NotNil(t, p.scalar)
_, ok := p.otherParticipantShares[2]
require.True(t, ok)
}
func TestNewParticipantBadInputs(t *testing.T) {
_, err := NewParticipant(0, 0, nil, nil)
require.Error(t, err)
require.Equal(t, err, internal.ErrNilArguments)
_, err = NewParticipant(1, 2, nil, nil)
require.Error(t, err)
require.Equal(t, err, internal.ErrNilArguments)
_, err = NewParticipant(1, 2, testGenerator, nil)
require.Error(t, err)
require.Equal(t, err, internal.ErrNilArguments)
}
-79
View File
@@ -1,79 +0,0 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package gennaro
import (
"fmt"
"math/big"
"github.com/onsonr/hway/crypto/core"
"github.com/onsonr/hway/crypto/internal"
v1 "github.com/onsonr/hway/crypto/sharing/v1"
)
// Round1Bcast are the values that are broadcast to all other participants
// after round1 completes
type Round1Bcast = []*v1.ShareVerifier
// Round1P2PSend are the values that are sent to individual participants based
// on the id
type Round1P2PSend = map[uint32]*Round1P2PSendPacket
// Round1P2PSendPacket are the shares generated from the secret for a specific participant
type Round1P2PSendPacket struct {
SecretShare *v1.ShamirShare
BlindingShare *v1.ShamirShare
}
// Round1 computes the first round for the DKG
// `secret` can be nil
// NOTE: if `secret` is nil, a new secret is generated which creates a new key
// if `secret` is set, then this performs key resharing aka proactive secret sharing update
func (dp *Participant) Round1(secret []byte) (Round1Bcast, Round1P2PSend, error) {
if dp.round != 1 {
return nil, nil, internal.ErrInvalidRound
}
if secret == nil {
// 1. x $← Zq
s, err := dp.scalar.Random()
if err != nil {
return nil, nil, err
}
secret = s.Bytes()
} else {
s := new(big.Int).SetBytes(secret)
if !dp.scalar.IsValid(s) {
return nil, nil, fmt.Errorf("invalid secret value")
}
if s.Cmp(core.Zero) == 0 {
return nil, nil, internal.ErrZeroValue
}
}
var err error
// 2. {X1,...,Xt},{R1,...,Rt},{x1,...,xn},{r1,...,rn}= PedersenFeldmanShare(E,Q,x,t,{p1,...,pn})
dp.pedersenResult, err = dp.pedersen.Split(secret)
if err != nil {
return nil, nil, err
}
// 4. P2PSend x_j,r_j to participant p_j in {p_1,...,p_n}_{i != j}
p2pSend := make(Round1P2PSend, len(dp.otherParticipantShares))
for id := range dp.otherParticipantShares {
p2pSend[id] = &Round1P2PSendPacket{
SecretShare: dp.pedersenResult.SecretShares[id-1],
BlindingShare: dp.pedersenResult.BlindingShares[id-1],
}
}
// Update internal state
dp.round = 2
// 3. EchoBroadcast {X_1,...,X_t} to all other participants.
return dp.pedersenResult.BlindedVerifiers, p2pSend, nil
}
-90
View File
@@ -1,90 +0,0 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package gennaro
import (
"fmt"
"github.com/onsonr/hway/crypto/internal"
v1 "github.com/onsonr/hway/crypto/sharing/v1"
)
type Round2Bcast = []*v1.ShareVerifier
// Round2 computes the second round for Gennaro DKG
// Algorithm 3 - Gennaro DKG Round 2
// bcast contains all Round1 broadcast from other participants to this participant
// p2p contains all Round1 P2P send message from other participants to this participant
func (dp *Participant) Round2(bcast map[uint32]Round1Bcast, p2p map[uint32]*Round1P2PSendPacket) (Round2Bcast, error) {
// Check participant is not empty
if dp == nil || dp.curve == nil {
return nil, internal.ErrNilArguments
}
// Check participant has the correct dkg round number
if dp.round != 2 {
return nil, internal.ErrInvalidRound
}
// Check the input is valid
if bcast == nil || p2p == nil || len(bcast) == 0 || len(p2p) == 0 {
return nil, internal.ErrNilArguments
}
// 1. set sk = x_{ii}
sk := dp.pedersenResult.SecretShares[dp.id-1].Value
// 2. for j in 1,...,n
for id := range bcast {
// 3. if i = j continue
if id == dp.id {
continue
}
// Ensure a valid p2p entry exists
if p2p[id] == nil {
return nil, fmt.Errorf("missing p2p packet for id=%v", id)
}
// 4. If PedersenVerify(E, Q, x_ji, r_ji, {X_ji,...,X_jt}) = false, abort
xji := p2p[id].SecretShare
rji := p2p[id].BlindingShare
bvs := bcast[id]
if ok, err := dp.pedersen.Verify(xji, rji, bvs); !ok {
if err != nil {
return nil, err
} else {
return nil, fmt.Errorf("invalid share for participant id=%v", id)
}
}
// Store other participants' shares xji for usage in round 3
dp.otherParticipantShares[id].Share = p2p[id].SecretShare
// 5. sk = (sk+xji) mod q
// NOTE: we use the EcScalar class to add instead of
// just using big.Int Add and Mod
// because Ed25519 will fail with the big.Int Add and Mod
// and Ed25519 uses different little endian vs big endian
// in big.Int
t1 := sk.BigInt()
t2 := xji.Value.BigInt()
r := dp.scalar.Add(t1, t2)
sk = sk.Field().NewElement(r)
}
// Update internal state
dp.round = 3
// 7. Store ski as participant i's secret key share
dp.skShare = sk
// 6. EchoBroadcast {R_1,...,R_t} to all other participants.
return dp.pedersenResult.Verifiers, nil
}
-89
View File
@@ -1,89 +0,0 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package gennaro
import (
"fmt"
"github.com/onsonr/hway/crypto/internal"
v1 "github.com/onsonr/hway/crypto/sharing/v1"
)
// Round3Bcast contains values that will be broadcast to other participants.
type Round3Bcast = v1.ShareVerifier
// Round3 computes the third round for Gennaro DKG
// Algorithm 4 - Gennaro DKG Round 3
// bcast contains all Round2 broadcast from other participants to this participant.
func (dp *Participant) Round3(bcast map[uint32]Round2Bcast) (*Round3Bcast, *v1.ShamirShare, error) {
// Check participant is not empty
if dp == nil || dp.curve == nil {
return nil, nil, internal.ErrNilArguments
}
// Check participant has the correct dkg round number
if dp.round != 3 {
return nil, nil, internal.ErrInvalidRound
}
// Check the input is valid
if len(bcast) == 0 {
return nil, nil, internal.ErrNilArguments
}
// 1. SetBigInt Pk = R_i1
Pk := dp.pedersenResult.Verifiers[0]
// 2. for j in 1,...,n
for id := range bcast {
// 3. if i = j continue
if id == dp.id {
continue
}
// 4. If FeldmanVerify(E, xji, {R_j1,...,R_jt}) = false; abort
xji := dp.otherParticipantShares[id].Share
vs := bcast[id]
if ok, err := dp.feldman.Verify(xji, vs); !ok {
if err != nil {
return nil, nil, err
} else {
return nil, nil, fmt.Errorf("invalid share for participant #{id}")
}
}
// Store the feldman verifiers for round 4
dp.otherParticipantShares[id].Verifiers = vs
// 5. Pk = Pk+R_j1
temp, err := Pk.Add(bcast[id][0])
if err != nil {
return nil, nil, fmt.Errorf("error in computing Pk+R_j1")
} //nolint:errcheck
Pk = temp
}
// This is a sanity check to make sure nothing went wrong
// when computing the public key
if !Pk.IsOnCurve() || Pk.IsIdentity() {
return nil, nil, fmt.Errorf("invalid public key")
}
// 6. Store Pk as the public verification key
dp.verificationKey = Pk
// Update internal state
dp.round = 4
skShare := v1.ShamirShare{
Identifier: dp.id,
Value: dp.skShare,
}
// Output Pk as the public verification key
return Pk, &skShare, nil
}
-76
View File
@@ -1,76 +0,0 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package gennaro
import (
"math/big"
"github.com/onsonr/hway/crypto/core"
"github.com/onsonr/hway/crypto/core/curves"
"github.com/onsonr/hway/crypto/internal"
v1 "github.com/onsonr/hway/crypto/sharing/v1"
)
// Round4 computes the public shares used by tECDSA during signing
// that are converted to additive shares once the signing participants
// are known. This function is idempotent
func (dp *Participant) Round4() (map[uint32]*curves.EcPoint, error) {
// Check participant is not empty
if dp == nil || dp.curve == nil {
return nil, internal.ErrNilArguments
}
// Check participant has the correct dkg round number
if dp.round != 4 {
return nil, internal.ErrInvalidRound
}
n := len(dp.otherParticipantShares) + 1 //+1 to include self
// Wj's
publicShares := make(map[uint32]*curves.EcPoint, n)
// 1. R = {{R1,...,Rt},{Rij,...,Rit}i!=j}
r := make(map[uint32][]*v1.ShareVerifier, n)
r[dp.id] = dp.pedersenResult.Verifiers
for j := range dp.otherParticipantShares {
r[j] = dp.otherParticipantShares[j].Verifiers
}
// 2. for j in 1,...,n
for j, v := range r {
// 3. Wj = Pk
publicShares[j] = &curves.EcPoint{
Curve: dp.verificationKey.Curve,
X: new(big.Int).Set(dp.verificationKey.X),
Y: new(big.Int).Set(dp.verificationKey.Y),
}
// 4. for k in 1,...,t
for k := 0; k < len(dp.pedersenResult.Verifiers); k++ {
// 5. ck = pj * k mod q
pj := big.NewInt(int64(j))
ck, err := core.Mul(pj, big.NewInt(int64(k+1)), dp.curve.Params().N)
if err != nil {
return nil, err
}
// 6a. t = ck * Rj
t, err := v[k].ScalarMult(ck)
if err != nil {
return nil, err
}
// 6b. Wj = Wj + t
publicShares[j], err = publicShares[j].Add(t)
if err != nil {
return nil, err
}
}
}
return publicShares, nil
}
-381
View File
@@ -1,381 +0,0 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package gennaro
import (
"fmt"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/require"
"github.com/onsonr/hway/crypto/core/curves"
v1 "github.com/onsonr/hway/crypto/sharing/v1"
)
func TestParticipantRound1Works(t *testing.T) {
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
require.NoError(t, err)
bcast, p2psend, err := p1.Round1(nil)
require.NoError(t, err)
require.NotNil(t, bcast)
require.NotNil(t, p2psend)
require.Equal(t, len(p2psend), 1)
require.Equal(t, len(bcast), 2)
require.NotNil(t, p1.pedersenResult)
require.Equal(t, p1.round, 2)
_, ok := p2psend[2]
require.True(t, ok)
}
func TestParticipantRound1RepeatCall(t *testing.T) {
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
require.NoError(t, err)
_, _, err = p1.Round1(nil)
require.NoError(t, err)
_, _, err = p1.Round1(nil)
require.Error(t, err)
}
func TestParticipantRound1BadSecret(t *testing.T) {
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
require.NoError(t, err)
// secret == 0
secret := []byte{0}
_, _, err = p1.Round1(secret)
require.Error(t, err)
// secret too big
secret = []byte{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}
_, _, err = p1.Round1(secret)
require.Error(t, err)
}
func PrepareRound2Input(t *testing.T) (*Participant, *Participant, Round1Bcast, Round1Bcast, Round1P2PSend) {
// Prepare round 1 output of 2 participants
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
require.NoError(t, err)
require.Equal(t, p1.otherParticipantShares[2].Id, uint32(2))
p2, err := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
require.NoError(t, err)
require.Equal(t, p2.otherParticipantShares[1].Id, uint32(1))
bcast1, _, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
return p1, p2, bcast1, bcast2, p2psend2
}
// Test Gennaro DKG round2 works
func TestParticipantRound2Works(t *testing.T) {
// Prepare Dkg Round 1 output
p1, _, bcast1, bcast2, p2psend2 := PrepareRound2Input(t)
// Actual Test
require.NotNil(t, bcast1)
require.NotNil(t, bcast2)
require.NotNil(t, p2psend2[1])
bcast := make(map[uint32]Round1Bcast)
p2p := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
round2Out, err := p1.Round2(bcast, p2p)
require.NoError(t, err)
require.NotNil(t, round2Out)
require.Equal(t, len(round2Out), 2)
require.NotNil(t, p1.skShare)
require.Equal(t, p1.round, 3)
require.NotNil(t, p1.otherParticipantShares)
}
// Test Gennaro DKG round 2 repeat call
func TestParticipantRound2RepeatCall(t *testing.T) {
// Prepare Dkg Round 1 output
p1, _, bcast1, bcast2, p2psend2 := PrepareRound2Input(t)
// Actual Test
require.NotNil(t, bcast1)
require.NotNil(t, bcast2)
require.NotNil(t, p2psend2[1])
bcast := make(map[uint32]Round1Bcast)
p2p := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
_, err := p1.Round2(bcast, p2p)
require.NoError(t, err)
_, err = p1.Round2(bcast, p2p)
require.Error(t, err)
}
// Test Gennaro Dkg Round 2 Bad Input
func TestParticipantRound2BadInput(t *testing.T) {
// Prepare Dkg Round 1 output
p1, _, _, _, _ := PrepareRound2Input(t)
bcast := make(map[uint32]Round1Bcast)
p2p := make(map[uint32]*Round1P2PSendPacket)
// Test empty bcast and p2p
_, err := p1.Round2(bcast, p2p)
require.Error(t, err)
// Test nil bcast and p2p
p1, _, _, _, _ = PrepareRound2Input(t)
_, err = p1.Round2(nil, nil)
require.Error(t, err)
// Test tampered input bcast and p2p
p1, _, bcast1, bcast2, p2psend2 := PrepareRound2Input(t)
bcast = make(map[uint32]Round1Bcast)
p2p = make(map[uint32]*Round1P2PSendPacket)
// Tamper bcast1 and p2psend2 by doubling their value
bcast1[1].Y = bcast1[1].Y.Add(bcast1[1].Y, bcast1[1].Y)
p2psend2[1].SecretShare.Value = p2psend2[1].SecretShare.Value.Add(p2psend2[1].SecretShare.Value)
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
_, err = p1.Round2(bcast, p2p)
require.Error(t, err)
}
func PrepareRound3Input(t *testing.T) (*Participant, *Participant, map[uint32]Round2Bcast) {
p1, _ := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
p2, _ := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
bcast1, p2psend1, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
bcast := make(map[uint32]Round1Bcast)
p2p1 := make(map[uint32]*Round1P2PSendPacket)
p2p2 := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
p2p1[2] = p2psend2[1]
p2p2[1] = p2psend1[2]
round2Out1, _ := p1.Round2(bcast, p2p1)
round2Out2, _ := p2.Round2(bcast, p2p2)
round3Input := make(map[uint32]Round2Bcast)
round3Input[1] = round2Out1
round3Input[2] = round2Out2
return p1, p2, round3Input
}
// Test Gennaro Dkg Round 3 Works
func TestParticipantRound3Works(t *testing.T) {
// Prepare Gennaro Dkg Round 3 Input
p1, p2, round3Input := PrepareRound3Input(t)
// Actual Test
round3Out1, _, err := p1.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out1)
round3Out2, _, err := p2.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out2)
require.Equal(t, p1.round, 4)
require.Equal(t, p2.round, 4)
require.Equal(t, p1.verificationKey, p2.verificationKey)
// Test if shares recombine properly
s, _ := v1.NewShamir(2, 2, curves.NewField(btcec.S256().N))
sk, err := s.Combine(&v1.ShamirShare{Identifier: p1.id, Value: p1.skShare},
&v1.ShamirShare{Identifier: p2.id, Value: p2.skShare})
require.NoError(t, err)
// Test verification keys are G * sk
x, y := btcec.S256().ScalarBaseMult(sk)
tmp := &curves.EcPoint{
Curve: btcec.S256(),
X: x,
Y: y,
}
require.True(t, tmp.Equals(p1.verificationKey))
require.True(t, tmp.Equals(p2.verificationKey))
}
// Test Gennaro Dkg Round3 Repeat Call
func TestParticipantRound3RepeatCall(t *testing.T) {
// Prepare Round 3 Input
p1, _, round3Input := PrepareRound3Input(t)
// Actual Test
_, _, err := p1.Round3(round3Input)
require.NoError(t, err)
_, _, err = p1.Round3(round3Input)
require.Error(t, err)
}
// Test Gennaro DKG Round 3 Bad Input
func TestParticipantRound3BadInput(t *testing.T) {
// Test empty round 3 input
p1, _, _ := PrepareRound3Input(t)
emptyInput := make(map[uint32]Round2Bcast)
_, _, err := p1.Round3(emptyInput)
require.Error(t, err)
// Test nil round 3 input
p1, _, _ = PrepareRound3Input(t)
_, _, err = p1.Round3(nil)
require.Error(t, err)
// Test tampered round 3 input
p1, _, round3Input := PrepareRound3Input(t)
// Tamper participant2's broadcast
round3Input[2][0], _ = round3Input[2][0].Add(round3Input[2][1])
_, _, err = p1.Round3(round3Input)
require.Error(t, err)
}
// Test Gennaro Dkg Round 4 Works
func TestParticipantRound4Works(t *testing.T) {
// Prepare Gennaro Dkg Round 3 Input
p1, p2, round3Input := PrepareRound3Input(t)
round3Out1, _, err := p1.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out1)
round3Out2, _, err := p2.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out2)
// Actual test
publicShares1, err := p1.Round4()
require.NoError(t, err)
require.NotNil(t, publicShares1)
publicShares2, err := p2.Round4()
require.NoError(t, err)
require.NotNil(t, publicShares2)
require.Equal(t, publicShares1, publicShares2)
}
// Test Gennaro Dkg Round 4 Works
func TestParticipantRound4RepeatCall(t *testing.T) {
// Prepare Gennaro Dkg Round 3 Input
p1, p2, round3Input := PrepareRound3Input(t)
round3Out1, _, err := p1.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out1)
round3Out2, _, err := p2.Round3(round3Input)
require.NoError(t, err)
require.NotNil(t, round3Out2)
// Actual test
publicShares1, err := p1.Round4()
require.NoError(t, err)
require.NotNil(t, publicShares1)
publicShares2, err := p1.Round4()
require.NoError(t, err)
require.NotNil(t, publicShares2)
require.Equal(t, publicShares1, publicShares2)
}
// Test all Gennaro DKG rounds
func TestAllGennaroDkgRounds(t *testing.T) {
// Initiate two participants
p1, _ := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
p2, _ := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
// Running round 1
bcast1, p2psend1, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
bcast := make(map[uint32]Round1Bcast)
p2p1 := make(map[uint32]*Round1P2PSendPacket)
p2p2 := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
p2p1[2] = p2psend2[1]
p2p2[1] = p2psend1[2]
// Running round 2
round2Out1, _ := p1.Round2(bcast, p2p1)
round2Out2, _ := p2.Round2(bcast, p2p2)
round3Input := make(map[uint32]Round2Bcast)
round3Input[1] = round2Out1
round3Input[2] = round2Out2
// Running round 3
round3Out1, _, _ := p1.Round3(round3Input)
round3Out2, _, _ := p2.Round3(round3Input)
require.NotNil(t, round3Out1)
require.NotNil(t, round3Out2)
// Running round 4
publicShares1, _ := p1.Round4()
publicShares2, _ := p2.Round4()
// Test output of all rounds
require.Equal(t, publicShares1, publicShares2)
s, _ := v1.NewShamir(2, 2, curves.NewField(btcec.S256().N))
sk, err := s.Combine(&v1.ShamirShare{Identifier: p1.id, Value: p1.skShare},
&v1.ShamirShare{Identifier: p2.id, Value: p2.skShare})
require.NoError(t, err)
x, y := btcec.S256().ScalarBaseMult(sk)
tmp := &curves.EcPoint{
Curve: btcec.S256(),
X: x,
Y: y,
}
require.True(t, tmp.Equals(p1.verificationKey))
require.True(t, tmp.Equals(p2.verificationKey))
}
// Ensure correct functioning when input is missing
func TestParticipant2BadInput(t *testing.T) {
//
// Setup
//
p1, _ := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
p2, _ := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
bcast1, _, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
bcast := make(map[uint32]Round1Bcast)
p2p1 := make(map[uint32]*Round1P2PSendPacket)
bcast[1] = bcast1
bcast[2] = bcast2
// Exclude p2p 2>1
p2p1[4] = p2psend2[1]
// Run round 2
_, err := p1.Round2(bcast, p2p1)
require.Error(t, err)
}
func TestValidIDs(t *testing.T) {
err := fmt.Errorf("")
tests := []struct {
name string
in []uint32
expected error
}{
{"positive-1,2", []uint32{1, 2}, nil},
{"positive-2,1", []uint32{2, 1}, nil},
{"positive-1-10", []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, nil},
{"positive-1-10-random", []uint32{10, 9, 6, 5, 3, 2, 8, 7, 1, 4}, nil},
{"negative-1,3", []uint32{1, 3}, err},
{"negative-1-10-missing-5", []uint32{10, 9, 6, 3, 2, 8, 7, 1, 4}, err},
}
// Run all the tests!
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := validIds(test.in)
if test.expected == nil {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
// Test newParticipant with arbitrary IDs
func TestParticipantArbitraryIds(t *testing.T) {
_, err := NewParticipant(3, 2, testGenerator, curves.NewK256Scalar(), 4)
require.Error(t, err)
_, err = NewParticipant(0, 2, testGenerator, curves.NewK256Scalar(), 1)
require.Error(t, err)
_, err = NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 2, 3, 5)
require.Error(t, err)
_, err = NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 4)
require.Error(t, err)
}