Skip to content

Commit 33f27a3

Browse files
chilldkg: Raise SessionNotFinalizedError instead of None
1 parent e8570ae commit 33f27a3

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

reference/chilldkg.py

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Reference implementation of BIP DKG.
2-
from typing import Tuple, List, Optional, NamedTuple, NewType
2+
from typing import Tuple, List, NamedTuple, NewType
33

44
from secp256k1ref.secp256k1 import Scalar
55
from secp256k1ref.bip340 import schnorr_sign, schnorr_verify
@@ -15,6 +15,7 @@
1515
InvalidRecoveryDataError,
1616
DeserializationError,
1717
DuplicateHostpubkeyError,
18+
SessionNotFinalizedError,
1819
)
1920

2021

@@ -195,29 +196,28 @@ def signer_step2(
195196

196197
def signer_finalize(
197198
state2: SignerState2, cert: bytes
198-
) -> Optional[Tuple[DKGOutput, RecoveryData]]:
199-
"""A return value of None indicates that the DKG session has not completed
200-
successfully from our point of view.
199+
) -> Tuple[DKGOutput, RecoveryData]:
200+
"""A SessionNotFinalizedError indicates that finalizing the DKG session was
201+
not successful from our point of view.
201202
202-
WARNING: Even when obtaining a return value of None, you MUST NOT conclude
203-
that the DKG session has failed from the point of view of other
204-
participants, and as a consequence, you MUST NOT erase your seed.
203+
WARNING: Even when obtaining this exception, you MUST NOT conclude that the
204+
DKG session has failed, and as a consequence, you MUST NOT erase your seed.
205205
206206
The underlying reason is that it is possible that some other participant
207207
deems the DKG session successful, and uses the resulting threshold public
208208
key (e.g., by sending funds to it.) That other participant can, at any point
209209
in the future (e.g., when initiating a signing sessions), convince us of the
210-
success of the DKG session by presenting a public backup that is accepted by
211-
`signer_recover`."""
210+
success of the DKG session by presenting recovery data for which
211+
`signer_recover` succeeds and produces the expected session parameters."""
212212
(params, eta, dkg_output) = state2
213213
if not certifying_eq_verify(params.hostpubkeys, eta, cert):
214-
return None
214+
raise SessionNotFinalizedError
215215
return dkg_output, RecoveryData(eta + cert)
216216

217217

218218
async def signer(
219219
chan: SignerChannel, seed: bytes, hostseckey: bytes, params: SessionParams
220-
) -> Optional[Tuple[DKGOutput, RecoveryData]]:
220+
) -> Tuple[DKGOutput, RecoveryData]:
221221
# TODO Top-level error handling
222222
state1, smsg1 = signer_step1(seed, params)
223223
chan.send(smsg1)
@@ -228,9 +228,6 @@ async def signer(
228228
chan.send(eq_round1)
229229
cert = await chan.receive()
230230

231-
# TODO: If signer_finalize fails, we should probably not just return None
232-
# but raise instead. Raising a specific exception is also better for
233-
# testing.
234231
return signer_finalize(state2, cert)
235232

236233

@@ -291,9 +288,7 @@ def coordinator_step(
291288
return CoordinatorMsg(enc_cmsg, enc_shares_sums), dkg_output, eta
292289

293290

294-
async def coordinator(
295-
chans: CoordinatorChannels, params: SessionParams
296-
) -> Optional[DKGOutput]:
291+
async def coordinator(chans: CoordinatorChannels, params: SessionParams) -> DKGOutput:
297292
(hostpubkeys, t, params_id) = params
298293
n = len(hostpubkeys)
299294
smsgs1: List[SignerMsg1] = []
@@ -311,6 +306,6 @@ async def coordinator(
311306

312307
# TODO This should probably go to a coordinator_finalize function
313308
if not certifying_eq_verify(hostpubkeys, eta, cert):
314-
return None
309+
raise SessionNotFinalizedError
315310

316311
return dkg_output

reference/util.py

+4
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ class DeserializationError(Exception):
3333
class DuplicateHostpubkeyError(Exception):
3434
def __init__(self):
3535
pass
36+
37+
38+
class SessionNotFinalizedError(Exception):
39+
pass

0 commit comments

Comments
 (0)