bsv-rs 0.8.7

BSV SDK for Rust: primitives, the script interpreter, transactions and BEEF/SPV, BRC-42 wallets, BRC-103 auth, the overlay network. Reference parity with the TypeScript SDK, native and wasm32.
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
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
#![cfg(test)]
//! The unit tests of `shamir.rs`, in their own file (the file-size
//! ratchet; beta's bsv-rs#180 the class, #854's port the occasion).

use super::*;

#[test]
fn test_split_recover_roundtrip() {
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 3, 5).unwrap();

    assert_eq!(shares.points.len(), 5);
    assert_eq!(shares.threshold, 3);

    // Recover from first 3 shares
    let subset = KeyShares::new(shares.points[0..3].to_vec(), 3, shares.integrity.clone());
    let recovered = subset.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());
}

#[test]
fn test_split_recover_different_subsets() {
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 3, 5).unwrap();

    // Try different subsets of 3 shares
    let subsets = vec![
        vec![0, 1, 2],
        vec![0, 1, 3],
        vec![0, 1, 4],
        vec![0, 2, 3],
        vec![0, 2, 4],
        vec![0, 3, 4],
        vec![1, 2, 3],
        vec![1, 2, 4],
        vec![1, 3, 4],
        vec![2, 3, 4],
    ];

    for indices in subsets {
        let points: Vec<_> = indices.iter().map(|&i| shares.points[i].clone()).collect();
        let subset = KeyShares::new(points, 3, shares.integrity.clone());
        let recovered = subset.recover_private_key().unwrap();
        assert_eq!(
            key.to_bytes(),
            recovered.to_bytes(),
            "Failed for indices {:?}",
            indices
        );
    }
}

#[test]
fn test_backup_format_roundtrip() {
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 3, 5).unwrap();

    // Export to backup format
    let backup = shares.to_backup_format();
    assert_eq!(backup.len(), 5);

    // Each backup string should have 4 parts
    for s in &backup {
        assert_eq!(s.split('.').count(), 4);
    }

    // Restore from backup (using middle 3 shares)
    let restored = KeyShares::from_backup_format(&backup[1..4]).unwrap();
    let recovered = restored.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());
}

#[test]
fn test_minimum_threshold() {
    // Test with threshold = 2
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 2, 3).unwrap();

    let subset = KeyShares::new(shares.points[0..2].to_vec(), 2, shares.integrity.clone());
    let recovered = subset.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());
}

#[test]
fn test_exact_threshold_equals_total() {
    // Test with threshold = total (all shares needed)
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 5, 5).unwrap();

    let recovered = shares.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());
}

#[test]
fn test_insufficient_shares() {
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 3, 5).unwrap();

    // Try to recover with only 2 shares (less than threshold)
    let subset = KeyShares::new(shares.points[0..2].to_vec(), 3, shares.integrity.clone());
    let result = subset.recover_private_key();
    assert!(matches!(result, Err(Error::CryptoError(_))));
}

#[test]
fn test_invalid_threshold() {
    let key = PrivateKey::random();

    // Threshold less than 2
    assert!(matches!(
        split_private_key(&key, 1, 5),
        Err(Error::CryptoError(_))
    ));
    assert!(matches!(
        split_private_key(&key, 0, 5),
        Err(Error::CryptoError(_))
    ));

    // Total less than threshold
    assert!(matches!(
        split_private_key(&key, 5, 3),
        Err(Error::CryptoError(_))
    ));
}

#[test]
fn test_integrity_check_fails_on_corruption() {
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 2, 3).unwrap();

    // Create shares with wrong integrity
    let corrupted = KeyShares::new(
        shares.points.clone(),
        2,
        "XXXX".to_string(), // Wrong integrity
    );

    let result = corrupted.recover_private_key();
    assert!(matches!(result, Err(Error::CryptoError(_))));
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Integrity check failed"));
}

#[test]
fn test_backup_format_parsing() {
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 2, 3).unwrap();
    let backup = shares.to_backup_format();

    // Verify format
    let parsed = KeyShares::from_backup_format(&backup).unwrap();
    assert_eq!(parsed.threshold, shares.threshold);
    assert_eq!(parsed.integrity, shares.integrity);
    assert_eq!(parsed.points.len(), shares.points.len());
}

#[test]
fn test_mismatched_threshold_in_shares() {
    // Create two shares manually with different thresholds
    let share1 = "2.abc.3.XXXX".to_string();
    let share2 = "3.def.4.XXXX".to_string(); // Different threshold

    let result = KeyShares::from_backup_format(&[share1, share2]);
    assert!(matches!(result, Err(Error::CryptoError(_))));
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Threshold mismatch"));
}

#[test]
fn test_mismatched_integrity_in_shares() {
    // Create two shares manually with different integrity
    let share1 = "2.abc.3.AAAA".to_string();
    let share2 = "3.def.3.BBBB".to_string(); // Different integrity

    let result = KeyShares::from_backup_format(&[share1, share2]);
    assert!(matches!(result, Err(Error::CryptoError(_))));
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Integrity mismatch"));
}

#[test]
fn test_known_private_key() {
    // Test with a known private key value
    let key =
        PrivateKey::from_hex("0000000000000000000000000000000000000000000000000000000000000001")
            .unwrap();

    let shares = split_private_key(&key, 2, 3).unwrap();
    let recovered = shares.recover_private_key().unwrap();

    assert_eq!(key.to_bytes(), recovered.to_bytes());
}

#[test]
fn test_large_number_of_shares() {
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 5, 10).unwrap();

    assert_eq!(shares.points.len(), 10);

    // Recover from any 5 shares
    let subset = KeyShares::new(
        shares.points[5..10].to_vec(), // Last 5 shares
        5,
        shares.integrity.clone(),
    );
    let recovered = subset.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());
}

#[test]
fn test_compute_integrity() {
    let key =
        PrivateKey::from_hex("0000000000000000000000000000000000000000000000000000000000000001")
            .unwrap();

    let integrity = compute_integrity(&key);
    // Should be 4 characters
    assert_eq!(integrity.len(), 4);

    // Integrity should be deterministic
    let integrity2 = compute_integrity(&key);
    assert_eq!(integrity, integrity2);
}

#[test]
fn test_evaluate_polynomial() {
    let p = BigNumber::secp256k1_prime();

    // Test polynomial f(x) = 5 + 3x + 2x^2
    // f(0) = 5, f(1) = 10, f(2) = 19
    let coefficients = vec![
        BigNumber::from_u64(5),
        BigNumber::from_u64(3),
        BigNumber::from_u64(2),
    ];

    assert_eq!(
        evaluate_polynomial(&coefficients, &BigNumber::zero(), &p),
        BigNumber::from_u64(5)
    );
    assert_eq!(
        evaluate_polynomial(&coefficients, &BigNumber::from_u64(1), &p),
        BigNumber::from_u64(10)
    );
    assert_eq!(
        evaluate_polynomial(&coefficients, &BigNumber::from_u64(2), &p),
        BigNumber::from_u64(19)
    );
}

#[test]
fn test_decode_share() {
    // Create a valid share format
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 2, 3).unwrap();
    let backup = shares.to_backup_format();

    let (point, threshold, integrity) = decode_share(&backup[0], 0).unwrap();
    assert_eq!(threshold, 2);
    assert_eq!(integrity, shares.integrity);
    assert_eq!(point.x, shares.points[0].x);
    assert_eq!(point.y, shares.points[0].y);
}

#[test]
fn test_decode_share_invalid_format() {
    // Too few parts
    assert!(matches!(
        decode_share("a.b.c", 0),
        Err(Error::CryptoError(_))
    ));

    // Too many parts
    assert!(matches!(
        decode_share("a.b.c.d.e", 0),
        Err(Error::CryptoError(_))
    ));

    // Invalid threshold
    assert!(matches!(
        decode_share("2.abc.notanumber.XXXX", 0),
        Err(Error::CryptoError(_))
    ));
}

#[test]
fn test_empty_shares() {
    let result = KeyShares::from_backup_format(&[]);
    assert!(matches!(result, Err(Error::CryptoError(_))));
}

// ========================
// Edge case tests (GAP-06)
// ========================

#[test]
fn test_threshold_greater_than_total_shares() {
    // Mirrors Go: TestThresholdLargerThanTotalShares
    let key = PrivateKey::random();
    let result = split_private_key(&key, 50, 5);
    assert!(matches!(result, Err(Error::CryptoError(_))));
    assert!(
        result.unwrap_err().to_string().contains("must be at least"),
        "Expected error about total shares being less than threshold"
    );
}

#[test]
fn test_total_shares_less_than_2() {
    // Mirrors Go: TestTotalSharesLessThanTwo
    let key = PrivateKey::random();

    // total=1 with threshold=2 should fail (total < threshold)
    let result = split_private_key(&key, 2, 1);
    assert!(matches!(result, Err(Error::CryptoError(_))));

    // total=1 with threshold=1 should also fail (threshold < 2)
    let result = split_private_key(&key, 1, 1);
    assert!(matches!(result, Err(Error::CryptoError(_))));
}

#[test]
fn test_duplicate_shares_in_recovery() {
    // Mirrors Go: TestDuplicateShareDetected
    // Providing the same share twice should result in failed recovery
    // (the Lagrange interpolation will produce incorrect results or fail)
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 3, 5).unwrap();
    let backup = shares.to_backup_format();

    // Use share 0, share 1, and share 1 again (duplicate): refused at
    // the door, before interpolating, where the Lagrange weights would
    // divide by zero
    let result =
        KeyShares::from_backup_format(&[backup[0].clone(), backup[1].clone(), backup[1].clone()]);
    assert!(matches!(result, Err(Error::CryptoError(_))));
    let text = result.unwrap_err().to_string();
    assert!(text.contains("Duplicate share detected"));
}

#[test]
fn test_fewer_points_than_threshold() {
    // Mirrors Go: TestFewerPointsThanThreshold
    // Explicitly test the error message when fewer shares than threshold
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 3, 5).unwrap();

    // Manually set only 2 points but keep threshold at 3
    let subset = KeyShares::new(shares.points[..2].to_vec(), 3, shares.integrity.clone());
    let result = subset.recover_private_key();
    assert!(matches!(result, Err(Error::CryptoError(_))));
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("Insufficient shares"),
        "Expected 'Insufficient shares' error message"
    );
}

#[test]
fn test_consistency_across_multiple_splits() {
    // Mirrors Go: TestPolynomialConsistency
    // Splitting the same secret twice gives different shares (randomness)
    // but both sets should recover the same secret
    let key = PrivateKey::random();

    let shares1 = split_private_key(&key, 3, 5).unwrap();
    let shares2 = split_private_key(&key, 3, 5).unwrap();

    // The shares themselves should be different (different random polynomials)
    assert_ne!(
        shares1.points[0].y, shares2.points[0].y,
        "Two splits of the same key should produce different shares due to randomness"
    );

    // But both should recover the same key
    let recovered1 = KeyShares::new(shares1.points[..3].to_vec(), 3, shares1.integrity.clone())
        .recover_private_key()
        .unwrap();
    let recovered2 = KeyShares::new(shares2.points[..3].to_vec(), 3, shares2.integrity.clone())
        .recover_private_key()
        .unwrap();

    assert_eq!(key.to_bytes(), recovered1.to_bytes());
    assert_eq!(key.to_bytes(), recovered2.to_bytes());

    // Integrity should also match since it's derived from the same key
    assert_eq!(shares1.integrity, shares2.integrity);
}

#[test]
fn test_different_recovery_subsets() {
    // Mirrors Go: TestPolynomialReconstructionWithDifferentSubsets
    // Split into 5 shares with threshold 3, recover using all C(5,3)=10 subsets
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 3, 5).unwrap();

    let all_subsets: Vec<Vec<usize>> = vec![
        vec![0, 1, 2],
        vec![0, 1, 3],
        vec![0, 1, 4],
        vec![0, 2, 3],
        vec![0, 2, 4],
        vec![0, 3, 4],
        vec![1, 2, 3],
        vec![1, 2, 4],
        vec![1, 3, 4],
        vec![2, 3, 4],
    ];

    for subset_indices in &all_subsets {
        let subset_points: Vec<_> = subset_indices
            .iter()
            .map(|&i| shares.points[i].clone())
            .collect();
        let subset = KeyShares::new(subset_points, 3, shares.integrity.clone());
        let recovered = subset.recover_private_key().unwrap();
        assert_eq!(
            key.to_bytes(),
            recovered.to_bytes(),
            "Recovery failed for subset {:?}",
            subset_indices
        );
    }
}

#[test]
fn test_single_share_threshold() {
    // Threshold of 1 should be rejected (1-of-N is just copying the secret)
    let key = PrivateKey::random();
    let result = split_private_key(&key, 1, 5);
    assert!(matches!(result, Err(Error::CryptoError(_))));
    assert!(
        result.unwrap_err().to_string().contains("at least 2"),
        "Expected error about threshold being at least 2"
    );
}

#[test]
fn test_max_shares() {
    // Test with the maximum allowed number of shares (255)
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 3, 255).unwrap();
    assert_eq!(shares.points.len(), 255);

    // Recover from first 3 shares
    let subset = KeyShares::new(shares.points[..3].to_vec(), 3, shares.integrity.clone());
    let recovered = subset.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());

    // Recover from last 3 shares
    let subset = KeyShares::new(
        shares.points[252..255].to_vec(),
        3,
        shares.integrity.clone(),
    );
    let recovered = subset.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());

    // Recover from widely spaced shares (first, middle, last)
    let subset = KeyShares::new(
        vec![
            shares.points[0].clone(),
            shares.points[127].clone(),
            shares.points[254].clone(),
        ],
        3,
        shares.integrity.clone(),
    );
    let recovered = subset.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());
}

#[test]
fn test_threshold_exceeds_255() {
    // Threshold > 255 should be rejected
    let key = PrivateKey::random();
    let result = split_private_key(&key, 256, 300);
    assert!(matches!(result, Err(Error::CryptoError(_))));
    assert!(
        result.unwrap_err().to_string().contains("255"),
        "Expected error about threshold exceeding 255"
    );
}

#[test]
fn test_different_thresholds_and_shares() {
    // Mirrors Go: TestPolynomialDifferentThresholdsAndShares
    // Test various threshold/total combinations
    let test_cases = vec![(2, 3), (2, 5), (3, 5), (4, 7), (5, 10), (10, 10)];

    for (threshold, total) in test_cases {
        let key = PrivateKey::random();
        let shares = split_private_key(&key, threshold, total).unwrap();
        assert_eq!(shares.points.len(), total);

        let subset = KeyShares::new(
            shares.points[..threshold].to_vec(),
            threshold,
            shares.integrity.clone(),
        );
        let recovered = subset.recover_private_key().unwrap();
        assert_eq!(
            key.to_bytes(),
            recovered.to_bytes(),
            "Failed for threshold={}, total={}",
            threshold,
            total
        );
    }
}

#[test]
fn test_recovery_with_more_shares_than_threshold() {
    // Providing more shares than the threshold should still work
    let key = PrivateKey::random();
    let shares = split_private_key(&key, 3, 5).unwrap();

    // Use all 5 shares even though only 3 are needed
    let recovered = shares.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());

    // Use 4 shares
    let subset = KeyShares::new(shares.points[..4].to_vec(), 3, shares.integrity.clone());
    let recovered = subset.recover_private_key().unwrap();
    assert_eq!(key.to_bytes(), recovered.to_bytes());
}

#[test]
fn test_recovery_with_wrong_shares_fails_integrity() {
    // Shares from different keys should fail integrity check
    let key1 = PrivateKey::random();
    let key2 = PrivateKey::random();

    let shares1 = split_private_key(&key1, 2, 3).unwrap();
    let shares2 = split_private_key(&key2, 2, 3).unwrap();

    // Mix shares from two different keys but use integrity from key1
    let mixed = KeyShares::new(
        vec![shares1.points[0].clone(), shares2.points[1].clone()],
        2,
        shares1.integrity.clone(),
    );
    let result = mixed.recover_private_key();
    // Should fail with integrity check error (or invalid private key)
    assert!(
        matches!(result, Err(Error::CryptoError(_))),
        "Expected CryptoError when mixing shares from different keys, got {:?}",
        result
    );
}

#[test]
fn test_multiple_recovery_iterations() {
    // Mirrors Go: TestPolynomialConsistency - run multiple iterations
    // to ensure randomness doesn't cause flaky behavior
    for _ in 0..10 {
        let key = PrivateKey::random();
        let shares = split_private_key(&key, 3, 5).unwrap();
        let subset = KeyShares::new(shares.points[..3].to_vec(), 3, shares.integrity.clone());
        let recovered = subset.recover_private_key().unwrap();
        assert_eq!(key.to_bytes(), recovered.to_bytes());
    }
}

#[test]
fn test_backup_recovery_full_roundtrip() {
    // Mirrors Go: TestPrivateKeyToKeyShares - full backup/recovery cycle
    for _ in 0..3 {
        let key = PrivateKey::random();
        let shares = split_private_key(&key, 3, 5).unwrap();
        let backup = shares.to_backup_format();
        assert_eq!(backup.len(), 5);

        // Recover from first 3 backup strings
        let recovered_shares = KeyShares::from_backup_format(&backup[..3]).unwrap();
        let recovered_key = recovered_shares.recover_private_key().unwrap();
        assert_eq!(key.to_bytes(), recovered_key.to_bytes());
    }
}

#[test]
fn test_zero_threshold() {
    // Threshold of 0 should be rejected
    let key = PrivateKey::random();
    let result = split_private_key(&key, 0, 5);
    assert!(matches!(result, Err(Error::CryptoError(_))));
}

#[test]
fn test_zero_total_shares() {
    // Total of 0 should be rejected (threshold >= 2 > 0)
    let key = PrivateKey::random();
    let result = split_private_key(&key, 2, 0);
    assert!(matches!(result, Err(Error::CryptoError(_))));
}