mirror of
https://github.com/luxfi/threshold.git
synced 2026-10-30 15:15:53 +00:00
- Remove unnecessary trailing newlines - Fix comment formatting - Adjust indentation and spacing - Remove unused nolint directives
126 lines
3.8 KiB
Go
126 lines
3.8 KiB
Go
package keygen
|
|
|
|
import (
|
|
mrand "math/rand"
|
|
"testing"
|
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
"github.com/luxfi/threshold/internal/round"
|
|
"github.com/luxfi/threshold/internal/test"
|
|
"github.com/luxfi/threshold/pkg/math/curve"
|
|
"github.com/luxfi/threshold/pkg/pool"
|
|
"github.com/luxfi/threshold/protocols/cmp/config"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var group = curve.Secp256k1{}
|
|
|
|
func checkOutput(t *testing.T, rounds []round.Session) {
|
|
N := len(rounds)
|
|
newConfigs := make([]*config.Config, 0, N)
|
|
for _, r := range rounds {
|
|
require.IsType(t, &round.Output{}, r)
|
|
resultRound := r.(*round.Output)
|
|
require.IsType(t, &config.Config{}, resultRound.Result)
|
|
c := resultRound.Result.(*config.Config)
|
|
marshalledConfig, err := cbor.Marshal(c)
|
|
require.NoError(t, err)
|
|
unmarshalledConfig := config.EmptyConfig(group)
|
|
err = cbor.Unmarshal(marshalledConfig, unmarshalledConfig)
|
|
require.NoError(t, err)
|
|
newConfigs = append(newConfigs, unmarshalledConfig)
|
|
}
|
|
|
|
firstConfig := newConfigs[0]
|
|
pk := firstConfig.PublicPoint()
|
|
for _, c := range newConfigs {
|
|
assert.True(t, pk.Equal(c.PublicPoint()), "RID is different")
|
|
assert.Equal(t, firstConfig.RID, c.RID, "RID is different")
|
|
assert.EqualValues(t, firstConfig.ChainKey, c.ChainKey, "ChainKey is different")
|
|
for id, p := range firstConfig.Public {
|
|
assert.True(t, p.ECDSA.Equal(c.Public[id].ECDSA), "ecdsa not the same", id)
|
|
assert.True(t, p.ElGamal.Equal(c.Public[id].ElGamal), "elgamal not the same", id)
|
|
assert.True(t, p.Paillier.Equal(c.Public[id].Paillier), "paillier not the same", id)
|
|
assert.True(t, p.Pedersen.S().Eq(c.Public[id].Pedersen.S()) == 1, "S not the same", id)
|
|
assert.True(t, p.Pedersen.T().Eq(c.Public[id].Pedersen.T()) == 1, "T not the same", id)
|
|
assert.True(t, p.Pedersen.N().Nat().Eq(c.Public[id].Pedersen.N().Nat()) == 1, "N not the same", id)
|
|
}
|
|
data, err := c.MarshalBinary()
|
|
assert.NoError(t, err, "failed to marshal new config", c.ID)
|
|
c2 := config.EmptyConfig(group)
|
|
err = c2.UnmarshalBinary(data)
|
|
assert.NoError(t, err, "failed to unmarshal new config", c.ID)
|
|
}
|
|
}
|
|
|
|
func TestKeygen(t *testing.T) {
|
|
// Test basic CMP keygen with 2 parties, threshold 1
|
|
// Validates key generation, config creation, and serialization
|
|
pl := pool.NewPool(0)
|
|
defer pl.TearDown()
|
|
|
|
N := 2
|
|
partyIDs := test.PartyIDs(N)
|
|
|
|
rounds := make([]round.Session, 0, N)
|
|
for _, partyID := range partyIDs {
|
|
info := round.Info{
|
|
ProtocolID: "cmp/keygen-test",
|
|
FinalRoundNumber: Rounds,
|
|
SelfID: partyID,
|
|
PartyIDs: partyIDs,
|
|
Threshold: N - 1,
|
|
Group: group,
|
|
}
|
|
r, err := Start(info, pl, nil)(nil)
|
|
require.NoError(t, err, "round creation should not result in an error")
|
|
rounds = append(rounds, r)
|
|
}
|
|
|
|
for {
|
|
err, done := test.Rounds(rounds, nil)
|
|
require.NoError(t, err, "failed to process round")
|
|
if done {
|
|
break
|
|
}
|
|
}
|
|
checkOutput(t, rounds)
|
|
}
|
|
|
|
func TestRefresh(t *testing.T) {
|
|
// Test CMP refresh protocol with 4 parties, threshold 3
|
|
// Validates share refresh maintains same public key while updating private shares
|
|
|
|
pl := pool.NewPool(0)
|
|
defer pl.TearDown()
|
|
|
|
N := 4
|
|
T := N - 1
|
|
configs, _ := test.GenerateConfig(group, N, T, mrand.New(mrand.NewSource(1)), pl)
|
|
|
|
rounds := make([]round.Session, 0, N)
|
|
for _, c := range configs {
|
|
info := round.Info{
|
|
ProtocolID: "cmp/refresh-test",
|
|
FinalRoundNumber: Rounds,
|
|
SelfID: c.ID,
|
|
PartyIDs: c.PartyIDs(),
|
|
Threshold: N - 1,
|
|
Group: group,
|
|
}
|
|
r, err := Start(info, pl, c)(nil)
|
|
require.NoError(t, err, "round creation should not result in an error")
|
|
rounds = append(rounds, r)
|
|
}
|
|
|
|
for {
|
|
err, done := test.Rounds(rounds, nil)
|
|
require.NoError(t, err, "failed to process round")
|
|
if done {
|
|
break
|
|
}
|
|
}
|
|
checkOutput(t, rounds)
|
|
}
|