arcanum-threshold 0.1.1

Threshold cryptography for the Arcanum cryptographic engine
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! FROST (Flexible Round-Optimized Schnorr Threshold) signatures.
//!
//! FROST provides threshold Schnorr signatures with the following properties:
//! - t-of-n threshold: any t participants can create a valid signature
//! - Two-round signing: optimized for efficiency
//! - Robust against malicious participants
//!
//! ## Curve Support
//!
//! - **Ed25519**: For EdDSA-compatible signatures
//! - **secp256k1**: For Bitcoin/Ethereum compatibility

use crate::error::{Result, ThresholdError};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

#[cfg(feature = "frost-ed25519")]
use frost_ed25519 as frost;

#[cfg(all(feature = "frost-secp256k1", not(feature = "frost-ed25519")))]
use frost_secp256k1 as frost;

/// A participant's signing share (private key share).
#[derive(Clone)]
pub struct SigningShare {
    /// The participant's identifier.
    identifier: frost::Identifier,
    /// The secret signing share.
    share: frost::keys::SigningShare,
}

impl SigningShare {
    /// Create from FROST signing share.
    pub fn from_frost(id: frost::Identifier, share: frost::keys::SigningShare) -> Self {
        Self {
            identifier: id,
            share,
        }
    }

    /// Get the participant identifier.
    pub fn identifier(&self) -> frost::Identifier {
        self.identifier
    }

    /// Get the underlying FROST signing share.
    pub fn inner(&self) -> &frost::keys::SigningShare {
        &self.share
    }
}

impl std::fmt::Debug for SigningShare {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "SigningShare(id={:?})", self.identifier)
    }
}

/// A participant's verifying share (public key share).
#[derive(Clone, Serialize, Deserialize)]
pub struct VerifyingShare {
    /// The participant's identifier (serialized).
    identifier_bytes: Vec<u8>,
    /// Serialized verifying share.
    bytes: Vec<u8>,
}

impl VerifyingShare {
    /// Create from FROST verifying share.
    pub fn from_frost(id: frost::Identifier, share: &frost::keys::VerifyingShare) -> Result<Self> {
        Ok(Self {
            identifier_bytes: id.serialize(),
            bytes: share
                .serialize()
                .map_err(|e| ThresholdError::SerializationError(e.to_string()))?,
        })
    }

    /// Get the serialized bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }
}

impl std::fmt::Debug for VerifyingShare {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "VerifyingShare({} bytes)", self.identifier_bytes.len())
    }
}

/// Group verifying key (public key for the threshold group).
#[derive(Clone, Serialize, Deserialize)]
pub struct GroupVerifyingKey {
    bytes: Vec<u8>,
}

impl GroupVerifyingKey {
    /// Create from FROST verifying key.
    pub fn from_frost(key: &frost::VerifyingKey) -> Result<Self> {
        Ok(Self {
            bytes: key
                .serialize()
                .map_err(|e| ThresholdError::SerializationError(e.to_string()))?,
        })
    }

    /// Get the serialized bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Convert to FROST verifying key.
    pub fn to_frost(&self) -> Result<frost::VerifyingKey> {
        frost::VerifyingKey::deserialize(&self.bytes)
            .map_err(|e| ThresholdError::InternalError(e.to_string()))
    }
}

impl std::fmt::Debug for GroupVerifyingKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "GroupVerifyingKey({} bytes)", self.bytes.len())
    }
}

/// FROST signer for creating threshold signatures.
pub struct FrostSigner {
    /// Key package containing signing share and group info.
    key_package: frost::keys::KeyPackage,
}

impl FrostSigner {
    /// Create a signer from a key package.
    pub fn new(key_package: frost::keys::KeyPackage) -> Self {
        Self { key_package }
    }

    /// Get the participant's identifier.
    pub fn identifier(&self) -> frost::Identifier {
        *self.key_package.identifier()
    }

    /// Get the group verifying key.
    pub fn group_verifying_key(&self) -> Result<GroupVerifyingKey> {
        GroupVerifyingKey::from_frost(self.key_package.verifying_key())
    }

    /// Generate round 1 commitment for signing.
    pub fn round1(&self) -> Result<(SigningNonces, SigningCommitments)> {
        let mut rng = rand::rngs::OsRng;
        let (nonces, commitments) =
            frost::round1::commit(self.key_package.signing_share(), &mut rng);

        Ok((
            SigningNonces { inner: nonces },
            SigningCommitments::from_frost(self.identifier(), &commitments)?,
        ))
    }

    /// Generate signature share in round 2.
    pub fn round2(
        &self,
        _message: &[u8],
        nonces: &SigningNonces,
        signing_package: &SigningPackage,
    ) -> Result<SignatureShare> {
        let sig_share =
            frost::round2::sign(&signing_package.inner, &nonces.inner, &self.key_package)
                .map_err(|e| ThresholdError::SigningError(e.to_string()))?;

        Ok(SignatureShare::from_frost(self.identifier(), &sig_share))
    }
}

/// Signing nonces (kept secret by each participant).
pub struct SigningNonces {
    inner: frost::round1::SigningNonces,
}

/// Signing commitments (shared with coordinator).
#[derive(Clone, Serialize, Deserialize)]
pub struct SigningCommitments {
    identifier_bytes: Vec<u8>,
    bytes: Vec<u8>,
}

impl SigningCommitments {
    fn from_frost(id: frost::Identifier, c: &frost::round1::SigningCommitments) -> Result<Self> {
        Ok(Self {
            identifier_bytes: id.serialize(),
            bytes: c
                .serialize()
                .map_err(|e| ThresholdError::SerializationError(e.to_string()))?,
        })
    }

    fn to_frost(&self) -> Result<(frost::Identifier, frost::round1::SigningCommitments)> {
        let id = frost::Identifier::deserialize(&self.identifier_bytes)
            .map_err(|e| ThresholdError::InternalError(e.to_string()))?;

        let commitments = frost::round1::SigningCommitments::deserialize(&self.bytes)
            .map_err(|e| ThresholdError::InternalError(e.to_string()))?;

        Ok((id, commitments))
    }
}

/// Signing package containing all commitments.
pub struct SigningPackage {
    inner: frost::SigningPackage,
}

impl SigningPackage {
    /// Create a signing package from commitments.
    pub fn new(commitments: &[SigningCommitments], message: &[u8]) -> Result<Self> {
        let mut commitment_map = BTreeMap::new();

        for c in commitments {
            let (id, frost_c) = c.to_frost()?;
            commitment_map.insert(id, frost_c);
        }

        let inner = frost::SigningPackage::new(commitment_map, message);
        Ok(Self { inner })
    }
}

/// A participant's signature share.
#[derive(Clone, Serialize, Deserialize)]
pub struct SignatureShare {
    identifier_bytes: Vec<u8>,
    bytes: Vec<u8>,
}

impl SignatureShare {
    fn from_frost(id: frost::Identifier, share: &frost::round2::SignatureShare) -> Self {
        Self {
            identifier_bytes: id.serialize(),
            bytes: share.serialize(),
        }
    }

    fn to_frost(&self) -> Result<(frost::Identifier, frost::round2::SignatureShare)> {
        let id = frost::Identifier::deserialize(&self.identifier_bytes)
            .map_err(|e| ThresholdError::InternalError(e.to_string()))?;

        let share = frost::round2::SignatureShare::deserialize(&self.bytes)
            .map_err(|e| ThresholdError::InternalError(e.to_string()))?;

        Ok((id, share))
    }
}

/// FROST verifier and signature aggregator.
pub struct FrostVerifier {
    /// Group verifying key.
    verifying_key: frost::VerifyingKey,
}

impl FrostVerifier {
    /// Create a verifier from the group verifying key.
    pub fn new(key: &GroupVerifyingKey) -> Result<Self> {
        Ok(Self {
            verifying_key: key.to_frost()?,
        })
    }

    /// Aggregate signature shares into a complete signature.
    pub fn aggregate(
        &self,
        signing_package: &SigningPackage,
        signature_shares: &[SignatureShare],
        pubkey_package: &PublicKeyPackage,
    ) -> Result<Signature> {
        let mut share_map = BTreeMap::new();

        for share in signature_shares {
            let (id, frost_share) = share.to_frost()?;
            share_map.insert(id, frost_share);
        }

        let signature = frost::aggregate(&signing_package.inner, &share_map, &pubkey_package.inner)
            .map_err(|e| ThresholdError::SigningError(e.to_string()))?;

        Ok(Signature {
            bytes: signature
                .serialize()
                .map_err(|e| ThresholdError::SerializationError(e.to_string()))?,
        })
    }

    /// Verify a threshold signature.
    #[must_use = "signature verification must be checked - ignoring bypasses authentication"]
    pub fn verify(&self, message: &[u8], signature: &Signature) -> Result<bool> {
        let sig = frost::Signature::deserialize(&signature.bytes)
            .map_err(|_| ThresholdError::InvalidSignature)?;

        self.verifying_key
            .verify(message, &sig)
            .map(|_| true)
            .map_err(|_| ThresholdError::InvalidSignature)
    }
}

/// Public key package for signature verification.
#[derive(Clone)]
pub struct PublicKeyPackage {
    inner: frost::keys::PublicKeyPackage,
}

impl PublicKeyPackage {
    /// Create from FROST public key package.
    pub fn from_frost(pkg: frost::keys::PublicKeyPackage) -> Self {
        Self { inner: pkg }
    }

    /// Get the group verifying key.
    pub fn group_verifying_key(&self) -> Result<GroupVerifyingKey> {
        GroupVerifyingKey::from_frost(self.inner.verifying_key())
    }
}

/// A complete threshold signature.
#[derive(Clone, Serialize, Deserialize)]
pub struct Signature {
    bytes: Vec<u8>,
}

impl Signature {
    /// Get signature bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Signature length.
    pub fn len(&self) -> usize {
        self.bytes.len()
    }

    /// Check if empty.
    pub fn is_empty(&self) -> bool {
        self.bytes.is_empty()
    }
}

impl std::fmt::Debug for Signature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Signature({} bytes)", self.bytes.len())
    }
}

/// Generate key shares using a trusted dealer.
///
/// For production, use DKG instead of trusted dealer.
pub fn trusted_dealer_keygen(
    threshold: u16,
    total: u16,
) -> Result<(Vec<frost::keys::SecretShare>, frost::keys::PublicKeyPackage)> {
    let mut rng = rand::rngs::OsRng;

    let (shares, pubkeys) = frost::keys::generate_with_dealer(
        total,
        threshold,
        frost::keys::IdentifierList::Default,
        &mut rng,
    )
    .map_err(|e| ThresholdError::InternalError(e.to_string()))?;

    Ok((shares.into_values().collect(), pubkeys))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_frost_trusted_dealer() {
        let threshold = 2u16;
        let total = 3u16;

        // Generate keys with trusted dealer
        let (shares, _pubkey_package) = trusted_dealer_keygen(threshold, total).unwrap();
        assert_eq!(shares.len(), total as usize);

        // Verify we can create key packages
        for share in &shares {
            let key_package = frost::keys::KeyPackage::try_from(share.clone()).unwrap();
            let _ = FrostSigner::new(key_package);
        }
    }

    #[test]
    fn test_frost_signing_flow() {
        let threshold = 2u16;
        let total = 3u16;
        let message = b"Test message for FROST signing";

        // Generate keys
        let (shares, pubkey_package) = trusted_dealer_keygen(threshold, total).unwrap();

        // Create signers from first 2 participants
        let key_packages: Vec<_> = shares
            .iter()
            .take(threshold as usize)
            .map(|s| frost::keys::KeyPackage::try_from(s.clone()).unwrap())
            .collect();

        let signers: Vec<_> = key_packages
            .iter()
            .map(|kp| FrostSigner::new(kp.clone()))
            .collect();

        // Round 1: Generate commitments
        let mut all_nonces = Vec::new();
        let mut all_commitments = Vec::new();

        for signer in &signers {
            let (nonces, commitments) = signer.round1().unwrap();
            all_nonces.push(nonces);
            all_commitments.push(commitments);
        }

        // Create signing package
        let signing_package = SigningPackage::new(&all_commitments, message).unwrap();

        // Round 2: Generate signature shares
        let mut signature_shares = Vec::new();
        for (i, signer) in signers.iter().enumerate() {
            let share = signer
                .round2(message, &all_nonces[i], &signing_package)
                .unwrap();
            signature_shares.push(share);
        }

        // Aggregate and verify
        let group_key = GroupVerifyingKey::from_frost(pubkey_package.verifying_key()).unwrap();
        let verifier = FrostVerifier::new(&group_key).unwrap();
        let pkg = PublicKeyPackage::from_frost(pubkey_package);

        let signature = verifier
            .aggregate(&signing_package, &signature_shares, &pkg)
            .unwrap();
        assert!(verifier.verify(message, &signature).unwrap());
    }

    #[test]
    fn test_frost_wrong_message_fails() {
        let threshold = 2u16;
        let total = 3u16;
        let message = b"Original message";
        let wrong_message = b"Wrong message";

        // Generate keys
        let (shares, pubkey_package) = trusted_dealer_keygen(threshold, total).unwrap();

        let key_packages: Vec<_> = shares
            .iter()
            .take(threshold as usize)
            .map(|s| frost::keys::KeyPackage::try_from(s.clone()).unwrap())
            .collect();

        let signers: Vec<_> = key_packages
            .iter()
            .map(|kp| FrostSigner::new(kp.clone()))
            .collect();

        // Sign the original message
        let mut all_nonces = Vec::new();
        let mut all_commitments = Vec::new();

        for signer in &signers {
            let (nonces, commitments) = signer.round1().unwrap();
            all_nonces.push(nonces);
            all_commitments.push(commitments);
        }

        let signing_package = SigningPackage::new(&all_commitments, message).unwrap();

        let mut signature_shares = Vec::new();
        for (i, signer) in signers.iter().enumerate() {
            let share = signer
                .round2(message, &all_nonces[i], &signing_package)
                .unwrap();
            signature_shares.push(share);
        }

        let group_key = GroupVerifyingKey::from_frost(pubkey_package.verifying_key()).unwrap();
        let verifier = FrostVerifier::new(&group_key).unwrap();
        let pkg = PublicKeyPackage::from_frost(pubkey_package);

        let signature = verifier
            .aggregate(&signing_package, &signature_shares, &pkg)
            .unwrap();

        // Verify with wrong message should fail
        assert!(verifier.verify(wrong_message, &signature).is_err());
    }
}