Skip to content

Commit cad6425

Browse files
author
Andrei Smirnov
authored
dkg: fixing processkey (#4261)
This fixes a bug in `restoreKey` routine that affects "remove operators" edit command. category: bug ticket: none
1 parent 5b5af58 commit cad6425

6 files changed

Lines changed: 47 additions & 28 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/dkg.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ func processKey(ctx context.Context, config *Config, board *Board, key *kdkg.Dis
194194

195195
publicShares := make(map[int]tbls.PublicKey)
196196

197-
for i, oi := range oldShareIndices {
197+
for _, oi := range oldShareIndices {
198198
var pk tbls.PublicKey
199199
copy(pk[:], oldShareRevMap[oi])
200-
publicShares[i+1] = pk
200+
publicShares[oi] = pk
201201
}
202202

203203
return share.Share{

‎dkg/pedersen/reshare.go‎

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

162+
// For remove-only operations, reassign compact indices to newNodes.
163+
// This ensures new shares are evaluated at x=1,2,3,...,n (compact) rather than
164+
// the original gapped indices. The oldNodes keep their original indices for
165+
// correct Lagrange interpolation of contributed shares.
166+
if len(config.Reshare.RemovedPeers) > 0 && len(config.Reshare.AddedPeers) == 0 {
167+
for i := range newNodes {
168+
newNodes[i].Index = kdkg.Index(i)
169+
}
170+
}
171+
162172
// Validate old/new node counts against the reshare operation being performed.
163173
if err := validateReshareNodeCounts(len(oldNodes), len(newNodes), config.Threshold, config.Reshare); err != nil {
164174
return nil, err

‎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_removeoperators.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (p *removeOperatorsProtocol) PostInit(ctx context.Context, pctx *ProtocolCo
149149
nodeIdx := slices.Index(newPeerIDs, pctx.ThisPeerID)
150150
pctx.ThisNodeIdx = cluster.NodeIdx{
151151
PeerIdx: nodeIdx,
152-
ShareIdx: nodeIdx + 1,
152+
ShareIdx: peerMap[pctx.ThisPeerID].ShareIdx,
153153
}
154154
pctx.SigExchanger = newExchanger(pctx.ThisNode, nodeIdx, newPeerIDs, []sigType{sigLock}, pctx.Config.Timeout)
155155
}

‎dkg/protocol_test.go‎

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

544544
data := []byte("test data")
545-
allSigs := make([][]tbls.Signature, numVals)
546-
clusterPubKeys := make([][]tbls.PublicKey, numVals)
545+
allSigs := make([]map[int]tbls.Signature, numVals)
546+
validatorPubKeys := make([]tbls.PublicKey, numVals)
547547

548548
for valIdx := range numVals {
549-
sigs := make([]tbls.Signature, numNodes)
549+
sigsByIdx := make(map[int]tbls.Signature)
550550

551551
for nodeIdx := range numNodes {
552552
sig, err := tbls.Sign(clusterSecrets[nodeIdx][valIdx], data)
553553
require.NoError(t, err)
554554

555-
sigs[nodeIdx] = sig
555+
// Use 1-based share indices as production does
556+
sigsByIdx[nodeIdx+1] = sig
556557
}
557558

558-
allSigs[valIdx] = sigs
559-
clusterPubKeys[valIdx] = make([]tbls.PublicKey, numNodes)
559+
allSigs[valIdx] = sigsByIdx
560560

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

565-
clusterPubKeys[valIdx][nodeIdx] = pubKey
566-
}
566+
validatorPubKeys[valIdx] = tbls.PublicKey(lock.Validators[valIdx].PubKey)
567567
}
568568

569569
for valIdx := range numVals {
570-
aSig, err := tbls.Aggregate(allSigs[valIdx])
570+
// Use ThresholdAggregate (Lagrange interpolation) instead of simple Aggregate
571+
// to ensure share indices are correct - this is what production uses.
572+
aSig, err := tbls.ThresholdAggregate(allSigs[valIdx])
571573
require.NoError(t, err)
572574

573-
err = tbls.VerifyAggregate(clusterPubKeys[valIdx], aSig, data)
575+
err = tbls.Verify(validatorPubKeys[valIdx], data, aSig)
574576
require.NoError(t, err)
575577
}
576578
}

0 commit comments

Comments
 (0)