Skip to content

Commit a1dbb17

Browse files
author
Andrei Smirnov
committed
Fixed gapped indices
1 parent d9d0b24 commit a1dbb17

4 files changed

Lines changed: 44 additions & 25 deletions

File tree

‎dkg/dkg_test.go‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,16 +437,17 @@ func verifyDKGResults(t *testing.T, def cluster.Definition, dir string) {
437437
}
438438
}
439439

440-
// Ensure keystores can generate valid tbls aggregate signature.
440+
// Ensure keystores can generate valid tbls threshold aggregate signature.
441441
for i := range def.NumValidators {
442-
var sigs []tbls.Signature
442+
sigsByIdx := make(map[int]tbls.Signature)
443+
msg := []byte("data")
443444

444445
for j := range len(def.Operators) {
445-
msg := []byte("data")
446446
sig, err := tbls.Sign(secretShares[i][j], msg)
447447
require.NoError(t, err)
448448

449-
sigs = append(sigs, sig)
449+
// Use 1-based share indices as production does
450+
sigsByIdx[j+1] = sig
450451

451452
// Ensure all public shares can verify the partial signature
452453
for _, lock := range locks {
@@ -461,7 +462,13 @@ func verifyDKGResults(t *testing.T, def cluster.Definition, dir string) {
461462
}
462463
}
463464

464-
_, err := tbls.Aggregate(sigs)
465+
// Use ThresholdAggregate (Lagrange interpolation) instead of simple Aggregate
466+
// to ensure share indices are correct - this is what production uses.
467+
aSig, err := tbls.ThresholdAggregate(sigsByIdx)
468+
require.NoError(t, err)
469+
470+
// Verify against the validator's full public key
471+
err = tbls.Verify(tbls.PublicKey(locks[0].Validators[i].PubKey), msg, aSig)
465472
require.NoError(t, err)
466473
}
467474
}

‎dkg/pedersen/reshare.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ func RunReshareDKG(ctx context.Context, config *Config, board *Board, shares []s
158158
}
159159
}
160160

161+
// For remove-only operations, reassign compact indices to newNodes.
162+
// This ensures new shares are evaluated at x=1,2,3,...,n (compact) rather than
163+
// the original gapped indices. The oldNodes keep their original indices for
164+
// correct Lagrange interpolation of contributed shares.
165+
if len(config.Reshare.RemovedPeers) > 0 && len(config.Reshare.AddedPeers) == 0 {
166+
for i := range newNodes {
167+
newNodes[i].Index = kdkg.Index(i)
168+
}
169+
}
170+
161171
// Validate node classification
162172
if len(config.Reshare.RemovedPeers) > 0 && len(oldNodes) == 0 {
163173
return nil, errors.New("remove operation requires at least one node with existing shares to participate")

‎dkg/pedersen/testutils.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@ func VerifyShares(t *testing.T, nodes []*TestNode, numVals, threshold int) {
130130

131131
for v := range numVals {
132132
var (
133-
sigs []tbls.Signature
134-
pshares []tbls.PublicKey
135-
secrets = make(map[int]tbls.PrivateKey)
133+
sigsByIdx = make(map[int]tbls.Signature)
134+
secrets = make(map[int]tbls.PrivateKey)
136135
)
137136

138137
for _, node := range nodes {
@@ -145,15 +144,16 @@ func VerifyShares(t *testing.T, nodes []*TestNode, numVals, threshold int) {
145144
err = tbls.Verify(pubKeyShare, msg, sig)
146145
require.NoError(t, err)
147146

148-
sigs = append(sigs, sig)
149-
pshares = append(pshares, pubKeyShare)
147+
sigsByIdx[node.NodeIdx.ShareIdx] = sig
150148
secrets[node.NodeIdx.ShareIdx] = node.Shares[v].SecretShare
151149
}
152150

153-
aggSig, err := tbls.Aggregate(sigs)
151+
// Use ThresholdAggregate (Lagrange interpolation) instead of simple Aggregate
152+
// to ensure share indices are correct - this is what production uses.
153+
aggSig, err := tbls.ThresholdAggregate(sigsByIdx)
154154
require.NoError(t, err)
155155

156-
err = tbls.VerifyAggregate(pshares, aggSig, msg)
156+
err = tbls.Verify(nodes[0].Shares[v].PubKey, msg, aggSig)
157157
require.NoError(t, err)
158158

159159
recSecret, err := tbls.RecoverSecret(secrets, uint(len(nodes)), uint(threshold))

‎dkg/protocol_test.go‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -482,35 +482,37 @@ func verifyClusterValidators(t *testing.T, numVals int, nodeDirs []string) { //n
482482
}
483483

484484
data := []byte("test data")
485-
allSigs := make([][]tbls.Signature, numVals)
486-
clusterPubKeys := make([][]tbls.PublicKey, numVals)
485+
allSigs := make([]map[int]tbls.Signature, numVals)
486+
validatorPubKeys := make([]tbls.PublicKey, numVals)
487487

488488
for valIdx := range numVals {
489-
sigs := make([]tbls.Signature, numNodes)
489+
sigsByIdx := make(map[int]tbls.Signature)
490490

491491
for nodeIdx := range numNodes {
492492
sig, err := tbls.Sign(clusterSecrets[nodeIdx][valIdx], data)
493493
require.NoError(t, err)
494494

495-
sigs[nodeIdx] = sig
495+
// Use 1-based share indices as production does
496+
sigsByIdx[nodeIdx+1] = sig
496497
}
497498

498-
allSigs[valIdx] = sigs
499-
clusterPubKeys[valIdx] = make([]tbls.PublicKey, numNodes)
499+
allSigs[valIdx] = sigsByIdx
500500

501-
for nodeIdx := range numNodes {
502-
pubKey, err := tbls.SecretToPublicKey(clusterSecrets[nodeIdx][valIdx])
503-
require.NoError(t, err)
501+
// Get the validator's full public key for verification
502+
lockFilePath := path.Join(nodeDirs[0], clusterLockFile)
503+
lock, err := dkg.LoadAndVerifyClusterLock(t.Context(), lockFilePath, "", false)
504+
require.NoError(t, err)
504505

505-
clusterPubKeys[valIdx][nodeIdx] = pubKey
506-
}
506+
validatorPubKeys[valIdx] = tbls.PublicKey(lock.Validators[valIdx].PubKey)
507507
}
508508

509509
for valIdx := range numVals {
510-
aSig, err := tbls.Aggregate(allSigs[valIdx])
510+
// Use ThresholdAggregate (Lagrange interpolation) instead of simple Aggregate
511+
// to ensure share indices are correct - this is what production uses.
512+
aSig, err := tbls.ThresholdAggregate(allSigs[valIdx])
511513
require.NoError(t, err)
512514

513-
err = tbls.VerifyAggregate(clusterPubKeys[valIdx], aSig, data)
515+
err = tbls.Verify(validatorPubKeys[valIdx], data, aSig)
514516
require.NoError(t, err)
515517
}
516518
}

0 commit comments

Comments
 (0)