copia 0.1.3

Pure Rust rsync-style delta synchronization library
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! Signature generation and lookup for rsync delta computation.
//!
//! The signature contains rolling checksums and strong hashes for each block
//! of the basis file, enabling efficient block matching during delta computation.

use std::io::Read;

use rayon::prelude::*;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};

use crate::checksum::RollingChecksum;
use crate::error::{CopiaError, Result};
use crate::hash::StrongHash;

/// Signature for a single block in the basis file.
///
/// Contains both the weak (rolling) checksum for fast filtering
/// and the strong (BLAKE3) hash for verification.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlockSignature {
    /// Block index (0-based position in file).
    pub index: u32,
    /// Rolling checksum for fast matching.
    pub weak_hash: u32,
    /// Strong cryptographic hash for verification.
    pub strong_hash: StrongHash,
}

impl BlockSignature {
    /// Create a new block signature.
    ///
    /// # Arguments
    ///
    /// * `index` - Block index in the file
    /// * `weak_hash` - Rolling checksum value
    /// * `strong_hash` - BLAKE3 hash
    #[must_use]
    pub const fn new(index: u32, weak_hash: u32, strong_hash: StrongHash) -> Self {
        Self {
            index,
            weak_hash,
            strong_hash,
        }
    }

    /// Compute signature for a data block.
    ///
    /// # Arguments
    ///
    /// * `index` - Block index
    /// * `data` - Block data
    #[must_use]
    pub fn compute(index: u32, data: &[u8]) -> Self {
        Self {
            index,
            weak_hash: RollingChecksum::new(data).digest(),
            strong_hash: StrongHash::compute(data),
        }
    }
}

/// Complete signature of a basis file.
///
/// Contains all block signatures and metadata needed for delta computation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Signature {
    /// Block size used during signature generation.
    pub block_size: usize,
    /// Total size of the basis file in bytes.
    pub file_size: u64,
    /// Signatures for each block.
    pub blocks: Vec<BlockSignature>,
}

impl Signature {
    /// Create a new signature.
    #[must_use]
    pub const fn new(block_size: usize, file_size: u64) -> Self {
        Self {
            block_size,
            file_size,
            blocks: Vec::new(),
        }
    }

    /// Generate signature from a reader.
    ///
    /// # Arguments
    ///
    /// * `reader` - Data source to generate signature from
    /// * `block_size` - Size of each block
    ///
    /// # Errors
    ///
    /// Returns an I/O error if reading fails.
    pub fn generate<R: Read>(reader: &mut R, block_size: usize) -> Result<Self> {
        // Read all data first for parallel processing
        let mut data = Vec::new();
        reader.read_to_end(&mut data)?;

        let file_size = data.len() as u64;

        if data.is_empty() {
            return Ok(Self {
                block_size,
                file_size: 0,
                blocks: Vec::new(),
            });
        }

        // Parallel signature computation for large files
        let blocks: Vec<BlockSignature> = if data.len() > 64 * 1024 {
            // For files >64KB, use parallel processing
            data.par_chunks(block_size)
                .enumerate()
                .map(|(i, chunk)| {
                    #[allow(clippy::cast_possible_truncation)]
                    BlockSignature::compute(i as u32, chunk)
                })
                .collect()
        } else {
            // For small files, sequential is faster
            data.chunks(block_size)
                .enumerate()
                .map(|(i, chunk)| {
                    #[allow(clippy::cast_possible_truncation)]
                    BlockSignature::compute(i as u32, chunk)
                })
                .collect()
        };

        Ok(Self {
            block_size,
            file_size,
            blocks,
        })
    }

    /// Get the number of blocks in the signature.
    #[must_use]
    pub fn block_count(&self) -> usize {
        self.blocks.len()
    }

    /// Check if the signature is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.blocks.is_empty()
    }

    /// Build a lookup table from this signature.
    #[must_use]
    pub fn into_table(self) -> SignatureTable {
        SignatureTable::from_signature(self)
    }
}

/// Efficient lookup table for block matching.
///
/// Uses a two-level lookup: first by weak hash (fast), then by strong hash (verification).
/// This enables O(1) average-case block matching with `FxHash` for fast u32 keys.
#[derive(Debug)]
pub struct SignatureTable {
    /// First level: rolling checksum -> candidate block indices.
    /// Uses `FxHashMap` for ~2x faster lookups with integer keys.
    weak_index: FxHashMap<u32, Vec<usize>>,
    /// Full signature data.
    signature: Signature,
}

impl SignatureTable {
    /// Build a signature table from a signature.
    #[must_use]
    pub fn from_signature(signature: Signature) -> Self {
        let mut weak_index: FxHashMap<u32, Vec<usize>> =
            FxHashMap::with_capacity_and_hasher(signature.blocks.len(), rustc_hash::FxBuildHasher);

        for (i, block) in signature.blocks.iter().enumerate() {
            weak_index.entry(block.weak_hash).or_default().push(i);
        }

        Self {
            weak_index,
            signature,
        }
    }

    /// Build a signature table directly from a reader.
    ///
    /// # Arguments
    ///
    /// * `reader` - Data source
    /// * `block_size` - Block size for signature generation
    ///
    /// # Errors
    ///
    /// Returns an I/O error if reading fails.
    pub fn build<R: Read>(reader: &mut R, block_size: usize) -> Result<Self> {
        let signature = Signature::generate(reader, block_size)?;
        Ok(Self::from_signature(signature))
    }

    /// Find a matching block for the given weak hash and data.
    ///
    /// First filters by weak hash, then verifies with strong hash.
    ///
    /// # Arguments
    ///
    /// * `weak` - Rolling checksum of the candidate data
    /// * `data` - Actual data to verify against strong hash
    ///
    /// # Returns
    ///
    /// The matching block signature if found, or `None` if no match.
    #[must_use]
    pub fn find_match(&self, weak: u32, data: &[u8]) -> Option<&BlockSignature> {
        let candidates = self.weak_index.get(&weak)?;
        let strong = StrongHash::compute(data);

        candidates
            .iter()
            .map(|&i| &self.signature.blocks[i])
            .find(|sig| sig.strong_hash == strong)
    }

    /// Find a matching block with optimized strong hash computation.
    ///
    /// For sequential matching (checking block at position `expected_index`),
    /// checks that index first and skips strong hash if it's the only candidate.
    /// This provides a significant speedup for mostly-identical files.
    #[must_use]
    pub fn find_match_optimized(
        &self,
        weak: u32,
        data: &[u8],
        expected_index: u32,
    ) -> Option<&BlockSignature> {
        let candidates = self.weak_index.get(&weak)?;

        // Fast path: if there's only one candidate and it's the expected one,
        // verify with strong hash (can't skip entirely for correctness)
        if candidates.len() == 1 {
            let sig = &self.signature.blocks[candidates[0]];
            if sig.index == expected_index {
                // Still verify, but this is the common case
                let strong = StrongHash::compute(data);
                if sig.strong_hash == strong {
                    return Some(sig);
                }
            }
        }

        // Check expected index first if present
        for &i in candidates {
            let sig = &self.signature.blocks[i];
            if sig.index == expected_index {
                let strong = StrongHash::compute(data);
                if sig.strong_hash == strong {
                    return Some(sig);
                }
                // Expected didn't match, check others
                break;
            }
        }

        // Fall back to checking all candidates
        let strong = StrongHash::compute(data);
        candidates
            .iter()
            .map(|&i| &self.signature.blocks[i])
            .find(|sig| sig.strong_hash == strong)
    }

    /// Find a matching block using only weak hash for sequential matching.
    ///
    /// Returns the block index if weak hash matches and the expected index is found.
    /// This is safe for sequential matching where final checksum verification is used.
    #[must_use]
    pub fn find_weak_match(&self, weak: u32, expected_index: u32) -> Option<u32> {
        let candidates = self.weak_index.get(&weak)?;

        for &i in candidates {
            let sig = &self.signature.blocks[i];
            if sig.index == expected_index {
                return Some(sig.index);
            }
        }
        None
    }

    /// Find a matching block using only strong hash (slower but definitive).
    ///
    /// # Arguments
    ///
    /// * `data` - Data to find match for
    #[must_use]
    pub fn find_match_strong(&self, data: &[u8]) -> Option<&BlockSignature> {
        let strong = StrongHash::compute(data);
        self.signature
            .blocks
            .iter()
            .find(|sig| sig.strong_hash == strong)
    }

    /// Check if a weak hash has any candidates.
    #[must_use]
    pub fn has_weak_match(&self, weak: u32) -> bool {
        self.weak_index.contains_key(&weak)
    }

    /// Get the number of weak hash buckets.
    #[must_use]
    pub fn bucket_count(&self) -> usize {
        self.weak_index.len()
    }

    /// Get the underlying signature.
    #[must_use]
    pub const fn signature(&self) -> &Signature {
        &self.signature
    }

    /// Get the block size.
    #[must_use]
    pub const fn block_size(&self) -> usize {
        self.signature.block_size
    }

    /// Get the file size.
    #[must_use]
    pub const fn file_size(&self) -> u64 {
        self.signature.file_size
    }

    /// Get block count.
    #[must_use]
    pub fn block_count(&self) -> usize {
        self.signature.blocks.len()
    }

    /// Check if empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.signature.blocks.is_empty()
    }

    /// Validate that the block size is within acceptable bounds.
    ///
    /// # Errors
    ///
    /// Returns `InvalidBlockSize` if block size is invalid.
    pub fn validate_block_size(block_size: usize) -> Result<()> {
        if !(512..=65536).contains(&block_size) || !block_size.is_power_of_two() {
            return Err(CopiaError::InvalidBlockSize(block_size));
        }
        Ok(())
    }
}

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

    // ==========================================================================
    // BLOCK SIGNATURE TESTS
    // ==========================================================================

    #[test]
    fn block_signature_new() {
        let sig = BlockSignature::new(0, 12345, StrongHash::zero());
        assert_eq!(sig.index, 0);
        assert_eq!(sig.weak_hash, 12345);
        assert_eq!(sig.strong_hash, StrongHash::zero());
    }

    #[test]
    fn block_signature_compute() {
        let data = b"test block data";
        let sig = BlockSignature::compute(5, data);

        assert_eq!(sig.index, 5);
        assert_eq!(sig.weak_hash, RollingChecksum::new(data).digest());
        assert_eq!(sig.strong_hash, StrongHash::compute(data));
    }

    #[test]
    fn block_signature_compute_empty() {
        let sig = BlockSignature::compute(0, b"");
        assert_eq!(sig.index, 0);
        assert_eq!(sig.weak_hash, 0); // Empty data has zero checksum
    }

    #[test]
    fn block_signature_deterministic() {
        let data = b"consistent data";
        let sig1 = BlockSignature::compute(0, data);
        let sig2 = BlockSignature::compute(0, data);
        assert_eq!(sig1, sig2);
    }

    #[test]
    fn block_signature_different_data() {
        let sig1 = BlockSignature::compute(0, b"data1");
        let sig2 = BlockSignature::compute(0, b"data2");
        assert_ne!(sig1.weak_hash, sig2.weak_hash);
        assert_ne!(sig1.strong_hash, sig2.strong_hash);
    }

    #[test]
    fn block_signature_serde_roundtrip() {
        let sig = BlockSignature::compute(42, b"serialization test");
        let serialized = bincode::serialize(&sig).unwrap();
        let deserialized: BlockSignature = bincode::deserialize(&serialized).unwrap();
        assert_eq!(sig, deserialized);
    }

    // ==========================================================================
    // SIGNATURE TESTS
    // ==========================================================================

    #[test]
    fn signature_new() {
        let sig = Signature::new(1024, 0);
        assert_eq!(sig.block_size, 1024);
        assert_eq!(sig.file_size, 0);
        assert!(sig.is_empty());
    }

    #[test]
    fn signature_generate_empty() {
        let data: &[u8] = b"";
        let mut cursor = Cursor::new(data);
        let sig = Signature::generate(&mut cursor, 1024).unwrap();

        assert_eq!(sig.block_size, 1024);
        assert_eq!(sig.file_size, 0);
        assert_eq!(sig.block_count(), 0);
        assert!(sig.is_empty());
    }

    #[test]
    fn signature_generate_single_block() {
        let data = b"small data";
        let mut cursor = Cursor::new(data.as_slice());
        let sig = Signature::generate(&mut cursor, 1024).unwrap();

        assert_eq!(sig.block_size, 1024);
        assert_eq!(sig.file_size, data.len() as u64);
        assert_eq!(sig.block_count(), 1);
        assert_eq!(sig.blocks[0].index, 0);
    }

    #[test]
    fn signature_generate_multiple_blocks() {
        let data = vec![42u8; 3000]; // Will create 3 blocks with block_size=1024
        let mut cursor = Cursor::new(data.as_slice());
        let sig = Signature::generate(&mut cursor, 1024).unwrap();

        assert_eq!(sig.block_size, 1024);
        assert_eq!(sig.file_size, 3000);
        assert_eq!(sig.block_count(), 3);
        assert_eq!(sig.blocks[0].index, 0);
        assert_eq!(sig.blocks[1].index, 1);
        assert_eq!(sig.blocks[2].index, 2);
    }

    #[test]
    fn signature_generate_exact_block_boundary() {
        let data = vec![0u8; 2048]; // Exactly 2 blocks
        let mut cursor = Cursor::new(data.as_slice());
        let sig = Signature::generate(&mut cursor, 1024).unwrap();

        assert_eq!(sig.block_count(), 2);
        assert_eq!(sig.file_size, 2048);
    }

    #[test]
    fn signature_deterministic() {
        let data = b"reproducible signature generation";
        let sig1 = Signature::generate(&mut Cursor::new(data.as_slice()), 512).unwrap();
        let sig2 = Signature::generate(&mut Cursor::new(data.as_slice()), 512).unwrap();

        assert_eq!(sig1.block_size, sig2.block_size);
        assert_eq!(sig1.file_size, sig2.file_size);
        assert_eq!(sig1.blocks.len(), sig2.blocks.len());
        for (b1, b2) in sig1.blocks.iter().zip(sig2.blocks.iter()) {
            assert_eq!(b1, b2);
        }
    }

    #[test]
    fn signature_into_table() {
        let data = vec![42u8; 2048];
        let mut cursor = Cursor::new(data.as_slice());
        let sig = Signature::generate(&mut cursor, 1024).unwrap();
        let block_count = sig.block_count();

        let table = sig.into_table();
        assert_eq!(table.block_count(), block_count);
    }

    #[test]
    fn signature_serde_roundtrip() {
        let data = vec![1, 2, 3, 4, 5];
        let mut cursor = Cursor::new(data.as_slice());
        let original = Signature::generate(&mut cursor, 512).unwrap();

        let serialized = bincode::serialize(&original).unwrap();
        let deserialized: Signature = bincode::deserialize(&serialized).unwrap();

        assert_eq!(original.block_size, deserialized.block_size);
        assert_eq!(original.file_size, deserialized.file_size);
        assert_eq!(original.blocks.len(), deserialized.blocks.len());
    }

    // ==========================================================================
    // SIGNATURE TABLE TESTS
    // ==========================================================================

    #[test]
    fn signature_table_build_empty() {
        let data: &[u8] = b"";
        let mut cursor = Cursor::new(data);
        let table = SignatureTable::build(&mut cursor, 1024).unwrap();

        assert!(table.is_empty());
        assert_eq!(table.block_count(), 0);
        assert_eq!(table.bucket_count(), 0);
    }

    #[test]
    fn signature_table_build_single_block() {
        let data = b"single block";
        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 1024).unwrap();

        assert_eq!(table.block_count(), 1);
        assert_eq!(table.block_size(), 1024);
        assert_eq!(table.file_size(), data.len() as u64);
    }

    #[test]
    fn signature_table_find_match_exists() {
        let data = b"block data for matching";
        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 1024).unwrap();

        let weak = RollingChecksum::new(data).digest();
        let result = table.find_match(weak, data);

        assert!(result.is_some());
        let sig = result.unwrap();
        assert_eq!(sig.index, 0);
    }

    #[test]
    fn signature_table_find_match_not_exists() {
        let data = b"original block";
        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 1024).unwrap();

        let other_data = b"different data!";
        let weak = RollingChecksum::new(other_data).digest();
        let result = table.find_match(weak, other_data);

        assert!(result.is_none());
    }

    #[test]
    fn signature_table_find_match_weak_collision() {
        // Create data that might have weak hash collision
        let block1 = vec![1u8; 100];
        let block2 = vec![2u8; 100];

        let mut data = block1.clone();
        data.extend(&block2);

        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 100).unwrap();

        // Try to find block1
        let weak = RollingChecksum::new(&block1).digest();
        let result = table.find_match(weak, &block1);

        assert!(result.is_some());
        assert_eq!(result.unwrap().index, 0);
    }

    #[test]
    fn signature_table_has_weak_match() {
        let data = b"test data";
        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 1024).unwrap();

        let weak = RollingChecksum::new(data).digest();
        assert!(table.has_weak_match(weak));
        assert!(!table.has_weak_match(weak.wrapping_add(1)));
    }

    #[test]
    fn signature_table_find_match_strong() {
        let data = b"block for strong match";
        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 1024).unwrap();

        let result = table.find_match_strong(data);
        assert!(result.is_some());

        let no_match = table.find_match_strong(b"different");
        assert!(no_match.is_none());
    }

    #[test]
    fn signature_table_multiple_blocks_same_hash() {
        // All zero blocks will have the same weak hash
        let data = vec![0u8; 2048];
        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 1024).unwrap();

        // Both blocks have weak_hash = 0
        let weak = RollingChecksum::new(&[0u8; 1024]).digest();
        assert_eq!(weak, 0);

        // Should find one of the blocks
        let result = table.find_match(weak, &[0u8; 1024]);
        assert!(result.is_some());
    }

    #[test]
    fn signature_table_validate_block_size_valid() {
        assert!(SignatureTable::validate_block_size(512).is_ok());
        assert!(SignatureTable::validate_block_size(1024).is_ok());
        assert!(SignatureTable::validate_block_size(2048).is_ok());
        assert!(SignatureTable::validate_block_size(4096).is_ok());
        assert!(SignatureTable::validate_block_size(65536).is_ok());
    }

    #[test]
    fn signature_table_validate_block_size_invalid() {
        // Too small
        assert!(SignatureTable::validate_block_size(256).is_err());
        assert!(SignatureTable::validate_block_size(0).is_err());

        // Too large
        assert!(SignatureTable::validate_block_size(131072).is_err());

        // Not power of 2
        assert!(SignatureTable::validate_block_size(1000).is_err());
        assert!(SignatureTable::validate_block_size(1023).is_err());
        assert!(SignatureTable::validate_block_size(1025).is_err());
    }

    // ==========================================================================
    // EDGE CASES
    // ==========================================================================

    #[test]
    fn signature_table_large_file() {
        let data = vec![42u8; 100_000];
        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 1024).unwrap();

        // 100_000 / 1024 = 97.65... so 98 blocks
        assert_eq!(table.block_count(), 98);
        assert_eq!(table.file_size(), 100_000);
    }

    #[test]
    fn signature_table_binary_data() {
        let data: Vec<u8> = (0..=255).cycle().take(5000).collect();
        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 512).unwrap();

        assert_eq!(table.block_count(), 10);
    }

    #[test]
    fn signature_table_getters() {
        let data = vec![0u8; 4096];
        let mut cursor = Cursor::new(data.as_slice());
        let table = SignatureTable::build(&mut cursor, 2048).unwrap();

        assert_eq!(table.block_size(), 2048);
        assert_eq!(table.file_size(), 4096);
        assert_eq!(table.block_count(), 2);
        assert!(!table.is_empty());

        let sig = table.signature();
        assert_eq!(sig.block_size, 2048);
    }
}

#[cfg(test)]
mod proptests {
    use super::*;
    use proptest::prelude::*;
    use std::io::Cursor;

    proptest! {
        /// Generated signatures are deterministic
        #[test]
        fn signature_deterministic(
            data in prop::collection::vec(any::<u8>(), 0..5000),
            block_size in prop::sample::select(vec![512usize, 1024, 2048, 4096])
        ) {
            let sig1 = Signature::generate(&mut Cursor::new(&data), block_size).unwrap();
            let sig2 = Signature::generate(&mut Cursor::new(&data), block_size).unwrap();

            prop_assert_eq!(sig1.block_count(), sig2.block_count());
            prop_assert_eq!(sig1.file_size, sig2.file_size);

            for (b1, b2) in sig1.blocks.iter().zip(sig2.blocks.iter()) {
                prop_assert_eq!(b1.weak_hash, b2.weak_hash);
                prop_assert_eq!(b1.strong_hash, b2.strong_hash);
            }
        }

        /// File size equals sum of data
        #[test]
        fn signature_file_size_correct(
            data in prop::collection::vec(any::<u8>(), 0..10000)
        ) {
            let sig = Signature::generate(&mut Cursor::new(&data), 1024).unwrap();
            prop_assert_eq!(sig.file_size, data.len() as u64);
        }

        /// Block count is ceiling division
        #[test]
        fn signature_block_count_correct(
            data in prop::collection::vec(any::<u8>(), 1..10000),
            block_size in prop::sample::select(vec![512usize, 1024, 2048])
        ) {
            let sig = Signature::generate(&mut Cursor::new(&data), block_size).unwrap();
            let expected = data.len().div_ceil(block_size);
            prop_assert_eq!(sig.block_count(), expected);
        }

        /// Find match returns correct block
        #[test]
        fn find_match_correct(
            data in prop::collection::vec(any::<u8>(), 512..5000)
        ) {
            let block_size = 512;
            let table = SignatureTable::build(&mut Cursor::new(&data), block_size).unwrap();

            // Try to find the first block
            let first_block = &data[..block_size.min(data.len())];
            let weak = RollingChecksum::new(first_block).digest();

            if let Some(found) = table.find_match(weak, first_block) {
                prop_assert_eq!(found.index, 0);
            }
        }

        /// Serde roundtrip preserves data
        #[test]
        fn signature_serde_preserves(
            data in prop::collection::vec(any::<u8>(), 0..2000)
        ) {
            let original = Signature::generate(&mut Cursor::new(&data), 512).unwrap();
            let serialized = bincode::serialize(&original).unwrap();
            let restored: Signature = bincode::deserialize(&serialized).unwrap();

            prop_assert_eq!(original.block_size, restored.block_size);
            prop_assert_eq!(original.file_size, restored.file_size);
            prop_assert_eq!(original.blocks.len(), restored.blocks.len());
        }
    }
}