-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathdkg.ts
More file actions
216 lines (196 loc) · 8.21 KB
/
dkg.ts
File metadata and controls
216 lines (196 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { ed25519_dkg_round0_process, ed25519_dkg_round1_process, ed25519_dkg_round2_process } from '@bitgo/wasm-mps';
import { encode } from 'cbor-x';
import crypto from 'crypto';
import { DeserializedMessage, DeserializedMessages, DkgState, EddsaReducedKeyShare } from './types';
/**
* EdDSA Distributed Key Generation (DKG) implementation using @bitgo/wasm-mps.
*
* State is explicit: each round function returns `{ msg, state }` bytes.
* The state bytes are stored between rounds and passed to the next round function,
* mirroring the server-side persistence pattern (state would be serialised to DB).
*
* @example
* ```typescript
* const dkg = new DKG(3, 2, 0);
* // X25519 keys come from GPG encryption subkeys (extracted by the orchestrator)
* dkg.initDkg(myX25519PrivKey, [otherParty1X25519PubKey, otherParty2X25519PubKey]);
* const msg1 = dkg.getFirstMessage();
* const msg2s = dkg.handleIncomingMessages(allThreeMsg1s);
* dkg.handleIncomingMessages(allThreeMsg2s); // completes DKG
* const keyShare = dkg.getKeyShare();
* ```
*/
export class DKG {
protected n: number;
protected t: number;
protected partyIdx: number;
/** Private X25519 key (from GPG encryption subkey) */
private decryptionKey: Buffer | null = null;
/** Other parties' X25519 public keys (from their GPG encryption subkeys), sorted by party index */
private otherPubKeys: Buffer[] | null = null;
/** Serialised round state bytes returned by the previous round function */
private dkgStateBytes: Buffer | null = null;
/** Opaque bincode-serialised keyshare from round2 */
private keyShare: Buffer | null = null;
/** 32-byte Ed25519 public key from round2 */
private sharePk: Buffer | null = null;
protected dkgState: DkgState = DkgState.Uninitialized;
constructor(n: number, t: number, partyIdx: number) {
this.n = n;
this.t = t;
this.partyIdx = partyIdx;
}
getState(): DkgState {
return this.dkgState;
}
/**
* Initialises the DKG session with this party's X25519 private key and the other parties'
* X25519 public keys. Keys are extracted from GPG encryption subkeys by the orchestrator.
*
* @param decryptionKey - This party's 32-byte X25519 private key (GPG enc subkey private part).
* @param otherEncPublicKeys - Other parties' 32-byte X25519 public keys, sorted by ascending
* party index (excluding own). For a 3-party setup, this is [party_A_pub, party_B_pub].
*/
initDkg(decryptionKey: Buffer, otherEncPublicKeys: Buffer[]): void {
if (!decryptionKey || decryptionKey.length !== 32) {
throw Error('Missing or invalid decryption key: must be 32 bytes');
}
if (!otherEncPublicKeys || otherEncPublicKeys.length !== this.n - 1) {
throw Error(`Expected ${this.n - 1} other parties' public keys`);
}
if (this.t > this.n || this.partyIdx >= this.n) {
throw Error('Invalid parameters for DKG');
}
this.decryptionKey = decryptionKey;
this.otherPubKeys = otherEncPublicKeys;
this.dkgState = DkgState.Init;
}
/**
* Runs round0 of the DKG protocol. Returns this party's broadcast message.
* Stores the round state bytes internally for the next round.
*
* @param dkgSeed - Optional 32-byte seed for deterministic DKG output (testing only).
*/
getFirstMessage(dkgSeed?: Buffer): DeserializedMessage {
if (this.dkgState !== DkgState.Init) {
throw Error('DKG session not initialized');
}
const seed = dkgSeed ?? crypto.randomBytes(32);
const result = ed25519_dkg_round0_process(this.partyIdx, this.decryptionKey!, this.otherPubKeys!, seed);
this.dkgStateBytes = Buffer.from(result.state);
this.dkgState = DkgState.WaitMsg1;
return { payload: new Uint8Array(result.msg), from: this.partyIdx };
}
/**
* Handles incoming messages from all parties and advances the protocol.
*
* - In WaitMsg1: runs round1, returns this party's round1 broadcast message.
* - In WaitMsg2: runs round2, completes DKG, returns [].
*
* The caller passes all n messages (including own); own message is filtered
* out internally. Other parties' messages are sorted by ascending party index,
* matching the ordering expected by @bitgo/wasm-mps.
*
* @param messagesForIthRound - All n messages for this round (including own).
*/
handleIncomingMessages(messagesForIthRound: DeserializedMessages): DeserializedMessages {
if (this.dkgState === DkgState.Complete) {
throw Error('DKG session already completed');
}
if (this.dkgState === DkgState.Uninitialized) {
throw Error('DKG session not initialized');
}
if (this.dkgState === DkgState.Init) {
throw Error(
'DKG session must call getFirstMessage() before handling incoming messages. Call getFirstMessage() first.'
);
}
if (messagesForIthRound.length !== this.n) {
throw Error('Invalid number of messages for the round. Number of messages should be equal to N');
}
// Extract other parties' messages, sorted by party index (ascending)
const otherMsgs = messagesForIthRound
.filter((m) => m.from !== this.partyIdx)
.sort((a, b) => a.from - b.from)
.map((m) => m.payload);
if (this.dkgState === DkgState.WaitMsg1) {
const result = ed25519_dkg_round1_process(otherMsgs, this.dkgStateBytes!);
// Store new state; this is what would be persisted to DB between API rounds
this.dkgStateBytes = Buffer.from(result.state);
this.dkgState = DkgState.WaitMsg2;
return [{ payload: new Uint8Array(result.msg), from: this.partyIdx }];
}
if (this.dkgState === DkgState.WaitMsg2) {
const share = ed25519_dkg_round2_process(otherMsgs, this.dkgStateBytes!);
this.keyShare = Buffer.from(share.share);
this.sharePk = Buffer.from(share.pk);
this.dkgStateBytes = null;
this.dkgState = DkgState.Complete;
return [];
}
throw Error('Unexpected DKG state');
}
/**
* Returns the opaque bincode-serialised keyshare produced by round2.
* This is used as input to the signing protocol.
*/
getKeyShare(): Buffer {
if (!this.keyShare) {
throw Error('DKG session not initialized');
}
return this.keyShare;
}
/**
* Returns the 32-byte Ed25519 public key agreed by all parties during DKG.
*/
getSharePublicKey(): Buffer {
if (!this.sharePk) {
throw Error('DKG session not initialized');
}
return this.sharePk;
}
/**
* Returns a CBOR-encoded reduced representation containing the public key.
* Note: private key material and chain code are not separately accessible
* from @bitgo/wasm-mps; the full keyshare is available via getKeyShare().
*/
getReducedKeyShare(): Buffer {
if (!this.sharePk) {
throw Error('DKG session not initialized');
}
const reducedKeyShare: EddsaReducedKeyShare = {
pub: Array.from(this.sharePk),
};
return Buffer.from(encode(reducedKeyShare));
}
/**
* Exports the current session state as a JSON string for persistence.
* Includes: round state bytes, current DKG round, decryption key, other parties' pub keys.
* This mirrors what a server would store in a database between API rounds.
*/
getSession(): string {
if (this.dkgState === DkgState.Complete) {
throw Error('DKG session is complete. Exporting the session is not allowed.');
}
if (this.dkgState === DkgState.Uninitialized) {
throw Error('DKG session not initialized');
}
return JSON.stringify({
dkgStateBytes: this.dkgStateBytes?.toString('base64') ?? null,
dkgRound: this.dkgState,
decryptionKey: this.decryptionKey?.toString('base64') ?? null,
otherPubKeys: this.otherPubKeys?.map((k) => k.toString('base64')) ?? null,
});
}
/**
* Restores a previously exported session. Allows the protocol to continue
* from where it left off, as if the round state was loaded from a database.
*/
restoreSession(session: string): void {
const data = JSON.parse(session);
this.dkgStateBytes = data.dkgStateBytes ? Buffer.from(data.dkgStateBytes, 'base64') : null;
this.dkgState = data.dkgRound;
this.decryptionKey = data.decryptionKey ? Buffer.from(data.decryptionKey, 'base64') : null;
this.otherPubKeys = data.otherPubKeys ? (data.otherPubKeys as string[]).map((k) => Buffer.from(k, 'base64')) : null;
}
}