Skip to content

Commit

Permalink
ecdsa: use a random set of fixtures in signing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
notatestuser committed Dec 27, 2019
1 parent 51d3031 commit a110668
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 49 deletions.
28 changes: 14 additions & 14 deletions ecdsa/keygen/save_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,26 @@ func (preParams LocalPreParams) Validate() bool {
}

// BuildLocalSaveDataSubset re-creates the LocalPartySaveData to contain data for only the list of signing parties.
func BuildLocalSaveDataSubset(result LocalPartySaveData, sortedIDs tss.SortedPartyIDs) LocalPartySaveData {
keysToIndices := make(map[string]int, len(result.Ks))
for j, kj := range result.Ks {
func BuildLocalSaveDataSubset(sourceData LocalPartySaveData, sortedIDs tss.SortedPartyIDs) LocalPartySaveData {
keysToIndices := make(map[string]int, len(sourceData.Ks))
for j, kj := range sourceData.Ks {
keysToIndices[hex.EncodeToString(kj.Bytes())] = j
}
newSaveData := NewLocalPartySaveData(sortedIDs.Len())
newSaveData.LocalPreParams = result.LocalPreParams
newSaveData.LocalSecrets = result.LocalSecrets
newSaveData.ECDSAPub = result.ECDSAPub
newData := NewLocalPartySaveData(sortedIDs.Len())
newData.LocalPreParams = sourceData.LocalPreParams
newData.LocalSecrets = sourceData.LocalSecrets
newData.ECDSAPub = sourceData.ECDSAPub
for j, id := range sortedIDs {
savedIdx, ok := keysToIndices[hex.EncodeToString(id.Key)]
if !ok {
common.Logger.Warning("BuildLocalSaveDataSubset: unable to find a signer party in the local save data", id)
}
newSaveData.Ks[j] = result.Ks[savedIdx]
newSaveData.NTildej[j] = result.NTildej[savedIdx]
newSaveData.H1j[j] = result.H1j[savedIdx]
newSaveData.H2j[j] = result.H2j[savedIdx]
newSaveData.BigXj[j] = result.BigXj[savedIdx]
newSaveData.PaillierPKs[j] = result.PaillierPKs[savedIdx]
newData.Ks[j] = sourceData.Ks[savedIdx]
newData.NTildej[j] = sourceData.NTildej[savedIdx]
newData.H1j[j] = sourceData.H1j[savedIdx]
newData.H2j[j] = sourceData.H2j[savedIdx]
newData.BigXj[j] = sourceData.BigXj[savedIdx]
newData.PaillierPKs[j] = sourceData.PaillierPKs[savedIdx]
}
return newSaveData
return newData
}
76 changes: 49 additions & 27 deletions ecdsa/keygen/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"fmt"
"io/ioutil"
"math/big"
"math/rand"
"path/filepath"
"runtime"
"sort"

"github.com/pkg/errors"

Expand All @@ -31,52 +33,72 @@ const (
testFixtureFileFormat = "keygen_data_%d.json"
)

func LoadKeygenTestFixtures(count int, optionalStart ...int) ([]LocalPartySaveData, tss.SortedPartyIDs, error) {
keys := make([]LocalPartySaveData, 0, count)
func LoadKeygenTestFixtures(qty int, optionalStart ...int) ([]LocalPartySaveData, tss.SortedPartyIDs, error) {
keys := make([]LocalPartySaveData, 0, qty)
start := 0
if 0 < len(optionalStart) {
start = optionalStart[0]
}
for j := start; j < count; j++ {
fixtureFilePath := makeTestFixtureFilePath(j)
for i := start; i < qty; i++ {
fixtureFilePath := makeTestFixtureFilePath(i)
bz, err := ioutil.ReadFile(fixtureFilePath)
if err != nil {
return nil, nil, errors.Wrapf(err,
"could not open the test fixture for party %d in the expected location: %s. run keygen tests first.",
j, fixtureFilePath)
i, fixtureFilePath)
}
var key LocalPartySaveData
if err = json.Unmarshal(bz, &key); err != nil {
return nil, nil, errors.Wrapf(err,
"could not unmarshal fixture data for party %d located at: %s",
j, fixtureFilePath)
i, fixtureFilePath)
}
keys = append(keys, LocalPartySaveData{
LocalPreParams: LocalPreParams{
PaillierSK: key.PaillierSK,
NTildei: key.NTildei,
H1i: key.H1i,
H2i: key.H2i,
},
LocalSecrets: LocalSecrets{
Xi: key.Xi,
ShareID: key.ShareID,
},
Ks: key.Ks,
NTildej: key.NTildej,
H1j: key.H1j,
H2j: key.H2j,
BigXj: key.BigXj,
PaillierPKs: key.PaillierPKs,
ECDSAPub: key.ECDSAPub,
})
keys = append(keys, key)
}
partyIDs := make(tss.UnSortedPartyIDs, len(keys))
for j, key := range keys {
pMoniker := fmt.Sprintf("%d", j+start+1)
for i, key := range keys {
pMoniker := fmt.Sprintf("%d", i+start+1)
partyIDs[i] = tss.NewPartyID(pMoniker, pMoniker, key.ShareID)
}
sortedPIDs := tss.SortPartyIDs(partyIDs)
return keys, sortedPIDs, nil
}

func LoadKeygenTestFixturesRandomSet(qty, fixtureCount int) ([]LocalPartySaveData, tss.SortedPartyIDs, error) {
keys := make([]LocalPartySaveData, 0, qty)
plucked := make(map[int]interface{}, qty)
for i := 0; len(plucked) < qty; i = (i + 1) % fixtureCount {
_, have := plucked[i]
if pluck := rand.Float32() < 0.5; !have && pluck {
plucked[i] = new(struct{})
}
}
for i := range plucked {
fixtureFilePath := makeTestFixtureFilePath(i)
bz, err := ioutil.ReadFile(fixtureFilePath)
if err != nil {
return nil, nil, errors.Wrapf(err,
"could not open the test fixture for party %d in the expected location: %s. run keygen tests first.",
i, fixtureFilePath)
}
var key LocalPartySaveData
if err = json.Unmarshal(bz, &key); err != nil {
return nil, nil, errors.Wrapf(err,
"could not unmarshal fixture data for party %d located at: %s",
i, fixtureFilePath)
}
keys = append(keys, key)
}
partyIDs := make(tss.UnSortedPartyIDs, len(keys))
j := 0
for i := range plucked {
key := keys[j]
pMoniker := fmt.Sprintf("%d", i+1)
partyIDs[j] = tss.NewPartyID(pMoniker, pMoniker, key.ShareID)
j++
}
sortedPIDs := tss.SortPartyIDs(partyIDs)
sort.Slice(keys, func(i, j int) bool { return keys[i].ShareID.Cmp(keys[j].ShareID) == -1 })
return keys, sortedPIDs, nil
}

Expand Down
6 changes: 3 additions & 3 deletions ecdsa/resharing/local_party_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

package resharing
package resharing_test

import (
"crypto/ecdsa"
Expand All @@ -20,6 +20,7 @@ import (
"github.com/binance-chain/tss-lib/common"
"github.com/binance-chain/tss-lib/crypto"
"github.com/binance-chain/tss-lib/ecdsa/keygen"
. "github.com/binance-chain/tss-lib/ecdsa/resharing"
"github.com/binance-chain/tss-lib/ecdsa/signing"
"github.com/binance-chain/tss-lib/test"
"github.com/binance-chain/tss-lib/tss"
Expand Down Expand Up @@ -50,7 +51,6 @@ func TestE2EConcurrent(t *testing.T) {

// PHASE: resharing
oldP2PCtx := tss.NewPeerContext(oldPIDs)

// init the new parties; re-use the fixture pre-params for speed
fixtures, _, err := keygen.LoadKeygenTestFixtures(testParticipants)
if err != nil {
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestE2EConcurrent(t *testing.T) {
signing:
// PHASE: signing
var signPIDs tss.SortedPartyIDs
keys, signPIDs, err = keygen.LoadKeygenTestFixtures(testThreshold + 1)
keys, signPIDs, err = keygen.LoadKeygenTestFixturesRandomSet(testThreshold+1, testParticipants)
assert.NoError(t, err)

signP2pCtx := tss.NewPeerContext(signPIDs)
Expand Down
9 changes: 5 additions & 4 deletions ecdsa/signing/local_party_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
)

const (
testThreshold = test.TestThreshold
testParticipants = test.TestParticipants
testThreshold = test.TestThreshold
)

func setUp(level string) {
Expand All @@ -35,13 +36,13 @@ func setUp(level string) {

func TestE2EConcurrent(t *testing.T) {
setUp("info")

threshold := testThreshold

// PHASE: load keygen fixtures
firstPartyIdx := 5
keys, signPIDs, err := keygen.LoadKeygenTestFixtures(testThreshold+1+firstPartyIdx, firstPartyIdx)
keys, signPIDs, err := keygen.LoadKeygenTestFixturesRandomSet(testThreshold+1, testParticipants)
assert.NoError(t, err, "should load keygen fixtures")
assert.Equal(t, testThreshold+1, len(keys))
assert.Equal(t, testThreshold+1, len(signPIDs))

// PHASE: signing
// use a shuffled selection of the list of parties for this test
Expand Down
2 changes: 1 addition & 1 deletion tss/party_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (mpid *MessageWrapper_PartyID) KeyInt() *big.Int {

// NewPartyID constructs a new PartyID
// Exported, used in `tss` client. `key` should remain consistent between runs for each party.
func NewPartyID(id string, moniker string, key *big.Int) *PartyID {
func NewPartyID(id, moniker string, key *big.Int) *PartyID {
return &PartyID{
MessageWrapper_PartyID: &MessageWrapper_PartyID{
Id: id,
Expand Down

0 comments on commit a110668

Please sign in to comment.