-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·320 lines (254 loc) · 8.67 KB
/
Copy pathexample.py
File metadata and controls
executable file
·320 lines (254 loc) · 8.67 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python3
"""Example of a full FROST signing session."""
from typing import List, Tuple
import asyncio
import argparse
import secrets
# Import frost_ref first to set up secp256k1lab path
from frost_ref import (
nonce_gen,
nonce_agg,
sign,
partial_sig_agg,
partial_sig_verify,
SignersContext,
SessionContext,
PlainPk,
)
from frost_ref.signing import (
thresh_pubkey_and_tweak,
get_xonly_pk,
partial_sig_verify_internal,
)
from secp256k1lab.bip340 import schnorr_verify
from trusted_dealer import trusted_dealer_keygen
#
# Network mocks to simulate full FROST signing sessions
#
class CoordinatorChannels:
def __init__(self, n):
self.n = n
self.queues = [asyncio.Queue() for _ in range(n)]
self.participant_queues = None
def set_participant_queues(self, participant_queues):
self.participant_queues = participant_queues
def send_to(self, i, m):
assert self.participant_queues is not None
self.participant_queues[i].put_nowait(m)
def send_all(self, m):
assert self.participant_queues is not None
for i in range(self.n):
self.participant_queues[i].put_nowait(m)
async def receive_from(self, i: int) -> bytes:
return await self.queues[i].get()
class ParticipantChannel:
def __init__(self, coord_queue):
self.queue = asyncio.Queue()
self.coord_queue = coord_queue
def send(self, m):
self.coord_queue.put_nowait(m)
async def receive(self):
return await self.queue.get()
#
# Helper functions
#
def generate_frost_keys(
n: int, t: int
) -> Tuple[PlainPk, List[int], List[bytes], List[PlainPk]]:
"""Generate t-of-n FROST keys using trusted dealer.
Returns:
thresh_pk: Threshold public key (33-byte compressed)
ids: List of signer IDs (0-indexed: 0, 1, ..., n-1)
secshares: List of secret shares (32-byte scalars)
pubshares: List of public shares (33-byte compressed)
"""
thresh_pk, secshares, pubshares = trusted_dealer_keygen(
secrets.token_bytes(32), n, t
)
assert len(secshares) == n
ids = list(range(len(secshares))) # ids are 0..n-1
return thresh_pk, ids, secshares, pubshares
#
# Protocol parties
#
async def participant(
chan: ParticipantChannel,
secshare: bytes,
pubshare: PlainPk,
my_id: int,
signers_ctx: SignersContext,
tweaks: List[bytes],
is_xonly: List[bool],
msg: bytes,
) -> Tuple[bytes, bytes]:
"""
Participant in FROST signing protocol.
Returns:
(psig, final_sig): Partial signature and final BIP340 signature
"""
# Get tweaked threshold pubkey
tweak_ctx = thresh_pubkey_and_tweak(signers_ctx.thresh_pk, tweaks, is_xonly)
tweaked_thresh_pk = get_xonly_pk(tweak_ctx)
# Round 1: Nonce generation
secnonce, pubnonce = nonce_gen(secshare, pubshare, tweaked_thresh_pk, msg, None)
chan.send(pubnonce)
aggnonce = await chan.receive()
# Round 2: Signing
session_ctx = SessionContext(aggnonce, signers_ctx, tweaks, is_xonly, msg)
psig = sign(secnonce, secshare, my_id, session_ctx)
assert partial_sig_verify_internal(psig, my_id, pubnonce, pubshare, session_ctx), (
"Partial signature verification failed"
)
chan.send(psig)
# Receive final signature
final_sig = await chan.receive()
return (psig, final_sig)
async def coordinator(
chans: CoordinatorChannels,
signers_ctx: SignersContext,
tweaks: List[bytes],
is_xonly: List[bool],
msg: bytes,
) -> bytes:
"""
Coordinator in FROST signing protocol.
Returns:
final_sig: Final BIP340 signature (64 bytes)
"""
# Determine the signers
signer_ids = signers_ctx.ids
num_signers = len(signer_ids)
# Round 1: Collect pubnonces
pubnonces = []
for i in range(num_signers):
pubnonce = await chans.receive_from(i)
pubnonces.append(pubnonce)
# Aggregate nonces
aggnonce = nonce_agg(pubnonces)
chans.send_all(aggnonce)
# Round 2: Collect partial signatures
session_ctx = SessionContext(aggnonce, signers_ctx, tweaks, is_xonly, msg)
psigs = []
for i in range(num_signers):
psig = await chans.receive_from(i)
assert partial_sig_verify(
psig, pubnonces, signers_ctx, tweaks, is_xonly, msg, i
), f"Partial signature verification failed for singer {i}"
psigs.append(psig)
# Aggregate partial signatures
final_sig = partial_sig_agg(psigs, session_ctx)
chans.send_all(final_sig)
return final_sig
#
# Signing Session
#
def simulate_frost_signing(
secshares: List[bytes],
signers_ctx: SignersContext,
msg: bytes,
tweaks: List[bytes],
is_xonly: List[bool],
) -> Tuple[bytes, List[bytes]]:
"""Run a full FROST signing session.
Returns:
(final_sig, psigs): Final signature and list of partial signatures
"""
# Extract signer set from signers_ctx
signer_ids = signers_ctx.ids
pubshares = signers_ctx.pubshares
num_signers = len(signer_ids)
async def session():
# Set up channels
coord_chans = CoordinatorChannels(num_signers)
participant_chans = [
ParticipantChannel(coord_chans.queues[i]) for i in range(num_signers)
]
coord_chans.set_participant_queues(
[participant_chans[i].queue for i in range(num_signers)]
)
# Create coroutines
coroutines = [coordinator(coord_chans, signers_ctx, tweaks, is_xonly, msg)] + [
participant(
participant_chans[i],
secshares[i],
pubshares[i],
signer_ids[i],
signers_ctx,
tweaks,
is_xonly,
msg,
)
for i in range(num_signers)
]
return await asyncio.gather(*coroutines)
results = asyncio.run(session())
final_sig = results[0]
psigs = [r[0] for r in results[1:]] # Extract psigs from participant results
return final_sig, psigs
def main():
parser = argparse.ArgumentParser(description="FROST Signing example")
parser.add_argument(
"t", nargs="?", type=int, default=2, help="Threshold [default=2]"
)
parser.add_argument(
"n", nargs="?", type=int, default=3, help="Participants [default=3]"
)
args = parser.parse_args()
t, n = args.t, args.n
assert 2 <= t <= n, "Threshold t must satisfy 2 <= t <= n"
print("====== FROST Signing example session ======")
print(f"Using n = {n} participants and a threshold of t = {t}.")
print()
# 1. Generate FROST keys
thresh_pk, all_ids, all_secshares, all_pubshares = generate_frost_keys(n, t)
print("=== Key Configuration ===")
print(f"Threshold public key: {thresh_pk.hex()}")
print()
print("=== Public shares ===")
for i, pubshare in enumerate(all_pubshares):
print(f" Participant {all_ids[i]}: {pubshare.hex()}")
print()
# 2. Select first t signers
signer_indices = list(range(t))
signer_ids = [all_ids[i] for i in signer_indices]
signer_secshares = [all_secshares[i] for i in signer_indices]
signer_pubshares = [all_pubshares[i] for i in signer_indices]
# 3. Initialize the signers context
print("=== Signing Set ===")
print(f"Selected signers: {signer_ids}")
print()
signers_ctx = SignersContext(n, t, signer_ids, signer_pubshares, thresh_pk)
# 4. Create message and tweaks
msg = secrets.token_bytes(32)
# Apply both plain (BIP32-style) and xonly (BIP341-style) tweaks
tweaks = [secrets.token_bytes(32), secrets.token_bytes(32)]
is_xonly = [False, True] # First: plain (BIP32), Second: xonly (BIP341)
tweak_ctx = thresh_pubkey_and_tweak(thresh_pk, tweaks, is_xonly)
tweaked_thresh_pk = get_xonly_pk(tweak_ctx)
print("=== Message and Tweaks ===")
print(f"Message: {msg.hex()}")
print(f"Tweak 1 (plain/BIP32): {tweaks[0].hex()}")
print(f"Tweak 2 (xonly/BIP341): {tweaks[1].hex()}")
print(f"Tweaked threshold public key: {tweaked_thresh_pk.hex()}")
print()
# 5. Run signing protocol
final_sig, psigs = simulate_frost_signing(
signer_secshares,
signers_ctx,
msg,
tweaks,
is_xonly,
)
print("=== Participants Partial Signatures ===")
for i, psig in enumerate(psigs):
print(f" Participant {signer_ids[i]}: {psig.hex()}")
print()
print("=== Final Signature ===")
print(f"BIP340 signature: {final_sig.hex()}")
print()
# 6. Verify signature
assert schnorr_verify(msg, tweaked_thresh_pk, final_sig)
print("=== Verification ===")
print("Signature verified successfully!")
if __name__ == "__main__":
main()