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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Distributed Key Generation (DKG) using Feldman's VSS.
//!
//! This module provides distributed key generation without a trusted dealer.
//! Multiple parties can jointly generate a shared secret key where no single
//! party knows the full key, but any threshold of parties can reconstruct it.
//!
//! # Features
//!
//! - Feldman's Verifiable Secret Sharing (VSS)
//! - Joint public key derivation
//! - Threshold secret sharing (M-of-N)
//! - Verification of shares without revealing them
//!
//! # Use Cases in CHIE Protocol
//!
//! - Decentralized coordinator setup
//! - Threshold signing for governance
//! - Distributed key management
//!
//! # Example
//!
//! ```
//! use chie_crypto::dkg::{DkgParams, DkgParticipant, aggregate_public_key};
//!
//! // Setup: 3 participants, threshold of 2
//! let params = DkgParams::new(3, 2);
//!
//! // Each participant generates their contribution
//! let mut participants: Vec<_> = (0..3)
//! .map(|i| DkgParticipant::new(¶ms, i))
//! .collect();
//!
//! // Broadcast phase: each participant shares their commitments
//! let commitments: Vec<_> = participants
//! .iter()
//! .map(|p| p.get_commitments())
//! .collect();
//!
//! // Each participant receives shares from others
//! for i in 0..3 {
//! for j in 0..3 {
//! if i != j {
//! let share = participants[j].generate_share(i).unwrap();
//! participants[i].receive_share(j, share, &commitments[j]).unwrap();
//! }
//! }
//! }
//!
//! // Compute joint public key
//! let public_key = aggregate_public_key(&commitments);
//! println!("Joint public key generated!");
//! ```
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use curve25519_dalek::ristretto::RistrettoPoint;
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::traits::Identity;
use rand::RngExt;
use serde::{Deserialize, Serialize};
use thiserror::Error;
// Helper to generate random scalar
fn random_scalar() -> Scalar {
let mut rng = rand::rng();
let mut bytes = [0u8; 32];
rng.fill(&mut bytes);
Scalar::from_bytes_mod_order(bytes)
}
// Helper to generate random point
#[allow(dead_code)]
fn random_point() -> RistrettoPoint {
RISTRETTO_BASEPOINT_POINT * random_scalar()
}
/// DKG-specific errors.
#[derive(Error, Debug)]
pub enum DkgError {
#[error("Invalid threshold: must have 1 <= threshold <= total_parties")]
InvalidThreshold,
#[error("Invalid participant ID")]
InvalidParticipantId,
#[error("Invalid share")]
InvalidShare,
#[error("Share verification failed")]
ShareVerificationFailed,
#[error("Insufficient shares for reconstruction")]
InsufficientShares,
#[error("Duplicate participant ID")]
DuplicateParticipant,
}
pub type DkgResult<T> = Result<T, DkgError>;
/// Parameters for DKG protocol.
#[derive(Clone, Debug)]
pub struct DkgParams {
/// Total number of participants
pub total_parties: usize,
/// Threshold: minimum parties needed to reconstruct secret
pub threshold: usize,
/// Generator point
g: RistrettoPoint,
}
impl DkgParams {
/// Create new DKG parameters.
///
/// # Arguments
///
/// * `total_parties` - Total number of participants
/// * `threshold` - Minimum number of parties needed to reconstruct
///
/// # Example
///
/// ```
/// use chie_crypto::dkg::DkgParams;
///
/// // 5 parties, threshold of 3
/// let params = DkgParams::new(5, 3);
/// assert_eq!(params.total_parties, 5);
/// assert_eq!(params.threshold, 3);
/// ```
pub fn new(total_parties: usize, threshold: usize) -> Self {
assert!(threshold > 0 && threshold <= total_parties);
// Use standard Ristretto basepoint for compatibility with other protocols (e.g., FROST)
let g = RISTRETTO_BASEPOINT_POINT;
Self {
total_parties,
threshold,
g,
}
}
}
/// A participant in the DKG protocol.
pub struct DkgParticipant {
/// Participant's ID (0-indexed)
id: usize,
/// DKG parameters
params: DkgParams,
/// Secret polynomial coefficients (a_0, a_1, ..., a_{t-1})
coefficients: Vec<Scalar>,
/// Commitments to polynomial coefficients (g^a_0, g^a_1, ...)
commitments: Vec<RistrettoPoint>,
/// Shares received from other participants
received_shares: Vec<Option<Scalar>>,
}
/// Public commitments broadcast by a participant.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DkgCommitments {
/// Participant ID
pub participant_id: usize,
/// Commitments to polynomial coefficients
pub commitments: Vec<Vec<u8>>, // Compressed Ristretto points
}
/// A secret share for a participant.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DkgShare {
/// Scalar value of the share
value: Vec<u8>, // Scalar bytes
}
impl DkgParticipant {
/// Create a new DKG participant.
///
/// # Arguments
///
/// * `params` - DKG parameters
/// * `id` - Participant's ID (0-indexed)
pub fn new(params: &DkgParams, id: usize) -> Self {
assert!(id < params.total_parties);
// Generate random polynomial of degree threshold-1
// f(x) = a_0 + a_1*x + a_2*x^2 + ... + a_{t-1}*x^{t-1}
let coefficients: Vec<Scalar> = (0..params.threshold).map(|_| random_scalar()).collect();
// Compute commitments: C_i = g^a_i
let commitments: Vec<RistrettoPoint> =
coefficients.iter().map(|coeff| params.g * coeff).collect();
let received_shares = vec![None; params.total_parties];
Self {
id,
params: params.clone(),
coefficients,
commitments,
received_shares,
}
}
/// Get public commitments to broadcast.
pub fn get_commitments(&self) -> DkgCommitments {
DkgCommitments {
participant_id: self.id,
commitments: self
.commitments
.iter()
.map(|c| c.compress().as_bytes().to_vec())
.collect(),
}
}
/// Generate a share for participant `target_id`.
///
/// # Arguments
///
/// * `target_id` - ID of the participant to receive this share
pub fn generate_share(&self, target_id: usize) -> DkgResult<DkgShare> {
if target_id >= self.params.total_parties {
return Err(DkgError::InvalidParticipantId);
}
// Evaluate polynomial at x = target_id + 1
// (We add 1 to avoid evaluating at 0)
let x = Scalar::from((target_id + 1) as u64);
let share_value = evaluate_polynomial(&self.coefficients, x);
Ok(DkgShare {
value: share_value.to_bytes().to_vec(),
})
}
/// Receive and verify a share from another participant.
///
/// # Arguments
///
/// * `from_id` - ID of the sending participant
/// * `share` - The share received
/// * `commitments` - Public commitments from the sender
pub fn receive_share(
&mut self,
from_id: usize,
share: DkgShare,
commitments: &DkgCommitments,
) -> DkgResult<()> {
if from_id >= self.params.total_parties {
return Err(DkgError::InvalidParticipantId);
}
if commitments.participant_id != from_id {
return Err(DkgError::InvalidShare);
}
if self.received_shares[from_id].is_some() {
return Err(DkgError::DuplicateParticipant);
}
// Deserialize share
if share.value.len() != 32 {
return Err(DkgError::InvalidShare);
}
let mut share_bytes = [0u8; 32];
share_bytes.copy_from_slice(&share.value);
let share_scalar = Scalar::from_bytes_mod_order(share_bytes);
// Verify share using commitments
// Check: g^share == C_0 * C_1^x * C_2^x^2 * ... * C_{t-1}^x^{t-1}
let x = Scalar::from((self.id + 1) as u64);
let mut expected = RistrettoPoint::identity();
let mut x_power = Scalar::ONE;
for commitment_bytes in &commitments.commitments {
if commitment_bytes.len() != 32 {
return Err(DkgError::InvalidShare);
}
let mut bytes = [0u8; 32];
bytes.copy_from_slice(commitment_bytes);
let commitment = curve25519_dalek::ristretto::CompressedRistretto(bytes)
.decompress()
.ok_or(DkgError::InvalidShare)?;
expected += commitment * x_power;
x_power *= x;
}
let actual = self.params.g * share_scalar;
if actual != expected {
return Err(DkgError::ShareVerificationFailed);
}
// Store verified share
self.received_shares[from_id] = Some(share_scalar);
Ok(())
}
/// Get the participant's secret share (sum of received shares + own share).
///
/// This should only be called after receiving shares from all other participants.
pub fn get_secret_share(&self) -> DkgResult<Scalar> {
// Add own share
let own_share = self.generate_share(self.id)?;
let mut own_bytes = [0u8; 32];
own_bytes.copy_from_slice(&own_share.value);
let mut total = Scalar::from_bytes_mod_order(own_bytes);
// Add received shares
for share in self.received_shares.iter().flatten() {
total += share;
}
Ok(total)
}
}
/// Aggregate public keys from all participants to get joint public key.
///
/// # Arguments
///
/// * `all_commitments` - Commitments from all participants
///
/// # Example
///
/// ```
/// use chie_crypto::dkg::{DkgParams, DkgParticipant, aggregate_public_key};
///
/// let params = DkgParams::new(3, 2);
/// let participants: Vec<_> = (0..3)
/// .map(|i| DkgParticipant::new(¶ms, i))
/// .collect();
///
/// let commitments: Vec<_> = participants
/// .iter()
/// .map(|p| p.get_commitments())
/// .collect();
///
/// let public_key = aggregate_public_key(&commitments);
/// ```
pub fn aggregate_public_key(all_commitments: &[DkgCommitments]) -> RistrettoPoint {
// Joint public key is the sum of all first commitments (C_0 from each party)
let mut joint_pk = RistrettoPoint::identity();
for commitments in all_commitments {
if !commitments.commitments.is_empty() {
let mut bytes = [0u8; 32];
bytes.copy_from_slice(&commitments.commitments[0]);
if let Some(point) =
curve25519_dalek::ristretto::CompressedRistretto(bytes).decompress()
{
joint_pk += point;
}
}
}
joint_pk
}
// Helper: Evaluate polynomial at point x
fn evaluate_polynomial(coefficients: &[Scalar], x: Scalar) -> Scalar {
let mut result = Scalar::ZERO;
let mut x_power = Scalar::ONE;
for coeff in coefficients {
result += coeff * x_power;
x_power *= x;
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dkg_basic() {
let params = DkgParams::new(3, 2);
let mut participants: Vec<_> = (0..3).map(|i| DkgParticipant::new(¶ms, i)).collect();
// Broadcast commitments
let commitments: Vec<_> = participants.iter().map(|p| p.get_commitments()).collect();
// Distribute shares
for i in 0..3 {
for j in 0..3 {
if i != j {
let share = participants[j].generate_share(i).unwrap();
participants[i]
.receive_share(j, share, &commitments[j])
.unwrap();
}
}
}
// Each participant gets their secret share
let shares: Vec<_> = participants
.iter()
.map(|p| p.get_secret_share().unwrap())
.collect();
assert_eq!(shares.len(), 3);
}
#[test]
fn test_dkg_aggregate_public_key() {
let params = DkgParams::new(5, 3);
let participants: Vec<_> = (0..5).map(|i| DkgParticipant::new(¶ms, i)).collect();
let commitments: Vec<_> = participants.iter().map(|p| p.get_commitments()).collect();
let public_key = aggregate_public_key(&commitments);
// Public key should not be identity
assert_ne!(public_key, RistrettoPoint::identity());
}
#[test]
fn test_dkg_invalid_threshold() {
let params = DkgParams::new(3, 2);
let participant = DkgParticipant::new(¶ms, 0);
// Try to generate share for invalid participant
assert!(participant.generate_share(10).is_err());
}
#[test]
fn test_dkg_share_verification() {
let params = DkgParams::new(3, 2);
let mut participant0 = DkgParticipant::new(¶ms, 0);
let participant1 = DkgParticipant::new(¶ms, 1);
let commitments1 = participant1.get_commitments();
let share = participant1.generate_share(0).unwrap();
// Should verify correctly
assert!(
participant0
.receive_share(1, share.clone(), &commitments1)
.is_ok()
);
// Should reject duplicate
assert!(participant0.receive_share(1, share, &commitments1).is_err());
}
#[test]
fn test_dkg_different_thresholds() {
for (total, threshold) in [(3, 2), (5, 3), (7, 4)] {
let params = DkgParams::new(total, threshold);
let mut participants: Vec<_> = (0..total)
.map(|i| DkgParticipant::new(¶ms, i))
.collect();
let commitments: Vec<_> = participants.iter().map(|p| p.get_commitments()).collect();
// Distribute shares
for i in 0..total {
for j in 0..total {
if i != j {
let share = participants[j].generate_share(i).unwrap();
participants[i]
.receive_share(j, share, &commitments[j])
.unwrap();
}
}
}
// Verify all participants can get their shares
for p in &participants {
assert!(p.get_secret_share().is_ok());
}
// Verify joint public key
let pk = aggregate_public_key(&commitments);
assert_ne!(pk, RistrettoPoint::identity());
}
}
#[test]
fn test_evaluate_polynomial() {
let coefficients = vec![
Scalar::from(1u64), // constant term
Scalar::from(2u64), // x term
Scalar::from(3u64), // x^2 term
];
// Evaluate at x=2: 1 + 2*2 + 3*4 = 1 + 4 + 12 = 17
let x = Scalar::from(2u64);
let result = evaluate_polynomial(&coefficients, x);
let expected = Scalar::from(17u64);
assert_eq!(result, expected);
}
#[test]
fn test_dkg_partial_shares() {
let params = DkgParams::new(5, 3);
let mut participants: Vec<_> = (0..5).map(|i| DkgParticipant::new(¶ms, i)).collect();
let commitments: Vec<_> = participants.iter().map(|p| p.get_commitments()).collect();
// Only distribute shares from 2 participants (less than threshold of 3)
for i in 0..5 {
for j in 0..2 {
if i != j {
let share = participants[j].generate_share(i).unwrap();
participants[i]
.receive_share(j, share, &commitments[j])
.unwrap();
}
}
}
// Participant can still compute a share, but it won't be the full secret
// This demonstrates that the protocol requires all participants
let result = participants[0].get_secret_share();
assert!(result.is_ok());
// The computed share exists but would be different with all participants
let partial_share = result.unwrap();
assert_ne!(partial_share, Scalar::ZERO);
}
#[test]
fn test_dkg_serialization() {
let params = DkgParams::new(3, 2);
let participant = DkgParticipant::new(¶ms, 0);
let commitments = participant.get_commitments();
// Serialize
let serialized = crate::codec::encode(&commitments).unwrap();
// Deserialize
let deserialized: DkgCommitments = crate::codec::decode(&serialized).unwrap();
assert_eq!(
commitments.commitments.len(),
deserialized.commitments.len()
);
for (orig, deser) in commitments
.commitments
.iter()
.zip(deserialized.commitments.iter())
{
assert_eq!(orig, deser);
}
}
#[test]
fn test_dkg_invalid_share_verification() {
let params = DkgParams::new(3, 2);
let mut participant0 = DkgParticipant::new(¶ms, 0);
let participant1 = DkgParticipant::new(¶ms, 1);
let commitments1 = participant1.get_commitments();
// Generate a valid share for participant 0
let valid_share = participant1.generate_share(0).unwrap();
// Create an invalid share by corrupting the value
let mut corrupted_value = valid_share.value.clone();
corrupted_value[0] = corrupted_value[0].wrapping_add(1); // Corrupt first byte
let invalid_share = DkgShare {
value: corrupted_value,
};
// Should reject invalid share
let result = participant0.receive_share(1, invalid_share, &commitments1);
assert!(result.is_err());
assert!(matches!(result, Err(DkgError::ShareVerificationFailed)));
}
#[test]
fn test_dkg_commitment_consistency() {
let params = DkgParams::new(3, 2);
// Create multiple participants
let participants: Vec<_> = (0..3).map(|i| DkgParticipant::new(¶ms, i)).collect();
// Get commitments from each participant
let commitments: Vec<_> = participants.iter().map(|p| p.get_commitments()).collect();
// Verify all commitments have correct length (threshold)
for commitment in &commitments {
assert_eq!(commitment.commitments.len(), params.threshold);
}
// Verify commitment points are not identity (identity point has specific bytes)
let identity_bytes = RistrettoPoint::identity().compress().to_bytes();
for commitment in &commitments {
for point_bytes in &commitment.commitments {
assert_ne!(point_bytes.as_slice(), identity_bytes.as_slice());
}
}
// Verify joint public key is deterministic from same commitments
let pk1 = aggregate_public_key(&commitments);
let pk2 = aggregate_public_key(&commitments);
assert_eq!(pk1, pk2);
}
}