Files
tss-lib/eddsa/keygen/test_utils.go
Fitz 856d77b7fb Curve as parameter (#137)
* move curve into tss.Parameters

* regen proto with full package name

* pass curve through parameter

* add curve name in ecpoint json serialization
2021-07-06 11:51:20 +08:00

118 lines
3.7 KiB
Go

// Copyright © 2019 Binance
//
// This file is part of Binance. The full Binance copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
package keygen
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"path/filepath"
"runtime"
"sort"
"github.com/pkg/errors"
"github.com/binance-chain/tss-lib/test"
"github.com/binance-chain/tss-lib/tss"
)
const (
// To change these parameters, you must first delete the text fixture files in test/_fixtures/ and then run the keygen test alone.
// Then the signing and resharing tests will work with the new n, t configuration using the newly written fixture files.
TestParticipants = test.TestParticipants
TestThreshold = test.TestParticipants / 2
)
const (
testFixtureDirFormat = "%s/../../test/_eddsa_fixtures"
testFixtureFileFormat = "keygen_data_%d.json"
)
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 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.",
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)
}
for _, kbxj := range key.BigXj {
kbxj.SetCurve(tss.Edwards())
}
key.EDDSAPub.SetCurve(tss.Edwards())
keys = append(keys, key)
}
partyIDs := make(tss.UnSortedPartyIDs, len(keys))
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)
}
for _, kbxj := range key.BigXj {
kbxj.SetCurve(tss.Edwards())
}
key.EDDSAPub.SetCurve(tss.Edwards())
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
}
func makeTestFixtureFilePath(partyIndex int) string {
_, callerFileName, _, _ := runtime.Caller(0)
srcDirName := filepath.Dir(callerFileName)
fixtureDirName := fmt.Sprintf(testFixtureDirFormat, srcDirName)
return fmt.Sprintf("%s/"+testFixtureFileFormat, fixtureDirName, partyIndex)
}