visual_cryptography/
lib.rs

1//! Visual Cryptography Library
2//!
3//! This library implements various visual cryptography algorithms including:
4//! - Basic (k,n) threshold schemes
5//! - Progressive visual cryptography
6//! - Support for binary, greyscale, and color images
7//! - Configurable block sizes
8
9pub mod algorithms;
10pub mod error;
11pub mod matrix;
12pub mod share;
13pub mod utils;
14
15pub use algorithms::{Algorithm, VCScheme};
16pub use error::{Result, VCError};
17pub use share::{Share, ShareType};
18
19// Re-export common types
20pub use image::{DynamicImage, ImageBuffer, Luma, Rgb, Rgba};
21
22/// Configuration for visual cryptography operations
23#[derive(Debug, Clone)]
24pub struct VCConfig {
25    /// Number of shares to generate
26    pub num_shares: usize,
27    /// Minimum shares needed to reconstruct (k in (k,n) scheme)
28    pub threshold: usize,
29    /// Block size for pixel expansion (e.g., 2 for 2x2, 3 for 3x3)
30    pub block_size: usize,
31    /// Algorithm to use
32    pub algorithm: Algorithm,
33    /// Whether to use meaningful shares (with cover images)
34    pub use_meaningful_shares: bool,
35}
36
37impl Default for VCConfig {
38    fn default() -> Self {
39        Self {
40            num_shares: 2,
41            threshold: 2,
42            block_size: 2,
43            algorithm: Algorithm::BasicThreshold,
44            use_meaningful_shares: false,
45        }
46    }
47}
48
49/// Main struct for visual cryptography operations
50pub struct VisualCryptography {
51    config: VCConfig,
52}
53
54impl VisualCryptography {
55    /// Create a new VisualCryptography instance with the given configuration
56    pub fn new(config: VCConfig) -> Result<Self> {
57        if config.threshold > config.num_shares {
58            return Err(VCError::InvalidConfiguration(
59                "Threshold cannot be greater than number of shares".to_string(),
60            ));
61        }
62        if config.threshold == 0 || config.num_shares == 0 {
63            return Err(VCError::InvalidConfiguration(
64                "Threshold and number of shares must be greater than 0".to_string(),
65            ));
66        }
67        if config.block_size == 0 {
68            return Err(VCError::InvalidConfiguration(
69                "Block size must be greater than 0".to_string(),
70            ));
71        }
72
73        Ok(Self { config })
74    }
75
76    /// Encrypt an image into shares
77    pub fn encrypt(
78        &self,
79        image: &DynamicImage,
80        cover_images: Option<Vec<DynamicImage>>,
81    ) -> Result<Vec<Share>> {
82        algorithms::encrypt(image, &self.config, cover_images)
83    }
84
85    /// Decrypt shares back into an image
86    pub fn decrypt(&self, shares: &[Share]) -> Result<DynamicImage> {
87        if shares.len() < self.config.threshold {
88            return Err(VCError::InsufficientShares {
89                required: self.config.threshold,
90                provided: shares.len(),
91            });
92        }
93        algorithms::decrypt(shares, &self.config)
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn test_config_validation() {
103        // Valid config
104        let config = VCConfig {
105            num_shares: 3,
106            threshold: 2,
107            block_size: 2,
108            algorithm: Algorithm::BasicThreshold,
109            use_meaningful_shares: false,
110        };
111        assert!(VisualCryptography::new(config).is_ok());
112
113        // Invalid: threshold > num_shares
114        let config = VCConfig {
115            num_shares: 2,
116            threshold: 3,
117            ..Default::default()
118        };
119        assert!(VisualCryptography::new(config).is_err());
120
121        // Invalid: zero threshold
122        let config = VCConfig {
123            threshold: 0,
124            ..Default::default()
125        };
126        assert!(VisualCryptography::new(config).is_err());
127    }
128}