# SiliconSignature A Rust library for cryptographic image watermarking using Reed-Solomon error correction and LSB steganography. Embed tamper-evident signatures directly into image pixels with software-based Proof-of-Work authentication. ## Features - **Reed-Solomon Error Correction** - Full implementation over GF(2^8) with Berlekamp-Massey algorithm - **SHA-256 Image Hashing** - Cryptographic integrity verification - **Proof-of-Work Nonce** - Simulated ASIC authentication with CPU-based nonce search - **LSB Steganography** - Invisible watermark embedding in all RGB channels - **5x Redundancy** - Signature repeated 5 times for robustness against image manipulation - **WASM Target** - Run in browsers with `wasm-bindgen` - **CLI Tool** - Command-line interface for embedding and verification - **Batch Processing** - Verify entire directories of images ## Architecture ``` JSON payload -> UTF-8 bytes -> Reed-Solomon(nsym=32) -> length header(4 BE) -> 5x repeat -> LSB embed ``` ## Quick Start ### Installation ```bash # Clone the repository git clone https://github.com/siliconsignature/siliconsignature.git cd siliconsignature/rust-lib # Build everything make build # Install CLI tool cargo install --path cli ``` ### CLI Usage #### Embed a watermark ```bash siliconsignature embed input.png --output signed.png --creator "artist_001" ``` #### Verify a watermarked image ```bash siliconsignature verify signed.png ``` Output: ``` File: signed.png Watermark: Found Verified: YES Integrity: FULL Confidence: 100.0% Hash: 65501a37b306f5ac183848bab643350219c18111bfa97c706856b668d3bd5996 Nonce: f16823b5 Status: AUTHENTICATED_BY_BM1387 Creator: artist_001 ``` #### JSON output ```bash siliconsignature verify signed.png --json ``` #### Batch verify a directory ```bash siliconsignature batch-verify ./images/ --report json siliconsignature batch-verify ./images/ --report csv --output report.csv ``` #### Start verification server ```bash siliconsignature server --port 8080 ``` ## Library Usage ### Add to your `Cargo.toml` ```toml [dependencies] siliconsignature = { path = "rust-lib/lib" } image = "0.24" ``` ### Embed a watermark ```rust use siliconsignature::{software_sign, embed_watermark, extract_watermark}; use image::RgbImage; fn main() { // Load or create an image let mut img = image::open("photo.png").unwrap().to_rgb8(); // Sign the image (software mode) let payload = software_sign(&img, Some("my_creator_id")).unwrap(); // Embed the watermark embed_watermark(&mut img, &payload).unwrap(); // Save the watermarked image img.save("signed.png").unwrap(); } ``` ### Extract and verify ```rust use siliconsignature::{extract_watermark, verify_watermark, SignaturePayload}; fn verify_image(img: &image::RgbImage) { // Extract watermark if let Ok(Some(payload)) = extract_watermark(img) { println!("Found watermark from creator: {:?}", payload.creator_id); println!("Image hash: {}", payload.hash); println!("Nonce: {}", payload.nonce); } // Or verify against expected payload let expected = SignaturePayload { hash: "65501a37...".to_string(), nonce: "f16823b5".to_string(), ntime: "6964c85e".to_string(), version: "20000000".to_string(), status: "AUTHENTICATED_BY_BM1387".to_string(), creator_id: Some("my_creator_id".to_string()), timestamp: Some(1715432000), }; let result = verify_watermark(img, &expected); println!("Verified: {}", result.verified); println!("Integrity: {:?}", result.integrity); println!("Confidence: {:.1}%", result.confidence * 100.0); } ``` ### Reed-Solomon codec directly ```rust use siliconsignature::{RsEncoder, RsDecoder}; let data = b"Hello, this is a test message!"; // Encode with 32 error correction symbols let encoder = RsEncoder::new(32); let encoded = encoder.encode(data); // Decode (corrects up to 16 errors) let decoder = RsDecoder::new(32); let decoded = decoder.decode(&encoded).unwrap(); assert_eq!(&decoded[..], data.as_slice()); ``` ## WASM Build ### Prerequisites ```bash cargo install wasm-pack ``` ### Build for web ```bash make wasm # Or directly: wasm-pack build wasm --target web --out-dir pkg ``` ### Use in JavaScript/TypeScript ```typescript import init, { embed_watermark_js, extract_watermark_js, verify_watermark_js, software_sign_js, } from './pkg/siliconsignature_wasm.js'; await init(); // Get pixels from a canvas const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); // Extract RGB channels only const rgbPixels = new Uint8Array(canvas.width * canvas.height * 3); for (let i = 0; i < canvas.width * canvas.height; i++) { rgbPixels[i * 3] = imageData.data[i * 4]; // R rgbPixels[i * 3 + 1] = imageData.data[i * 4 + 1]; // G rgbPixels[i * 3 + 2] = imageData.data[i * 4 + 2]; // B } // Sign the image const payload = software_sign_js(rgbPixels, canvas.width, canvas.height, "creator_001"); console.log("Payload:", payload); // Embed watermark const signedPixels = embed_watermark_js( rgbPixels, canvas.width, canvas.height, JSON.stringify(payload) ); // Put signed pixels back to canvas const signedImageData = ctx.createImageData(canvas.width, canvas.height); for (let i = 0; i < canvas.width * canvas.height; i++) { signedImageData.data[i * 4] = signedPixels[i * 3]; signedImageData.data[i * 4 + 1] = signedPixels[i * 3 + 1]; signedImageData.data[i * 4 + 2] = signedPixels[i * 3 + 2]; signedImageData.data[i * 4 + 3] = 255; // Alpha } ctx.putImageData(signedImageData, 0, 0); // Extract watermark later const extractedJson = extract_watermark_js(signedPixels, canvas.width, canvas.height); if (extractedJson) { const extracted = JSON.parse(extractedJson); console.log("Extracted watermark:", extracted); } // Verify watermark const result = verify_watermark_js( signedPixels, canvas.width, canvas.height, JSON.stringify(payload) ); console.log("Verify result:", result); ``` ## Reed-Solomon Implementation The library implements Reed-Solomon error correction over GF(2^8) with: - **Primitive polynomial**: `0x11d` (x^8 + x^4 + x^3 + x^2 + 1) - **Error correction symbols**: 32 (nsym=32) - **Primitive element**: alpha = 0x02 - **Generator**: Product of (x - alpha^i) for i = 0..31 - **Correction capacity**: Up to 16 symbol errors ### Berlekamp-Massey Algorithm The decoder uses the full Berlekamp-Massey algorithm: 1. **Syndrome computation** - Evaluate received polynomial at powers of alpha 2. **Berlekamp-Massey** - Find error locator polynomial via discrepancy iteration 3. **Chien search** - Find error positions by evaluating roots 4. **Forney algorithm** - Compute error values at error positions ## API Reference ### Core Functions | Function | Description | |----------|-------------| | `software_sign(img, creator_id)` | Sign an image with PoW nonce search | | `embed_watermark(img, payload)` | Embed a signature payload into an image | | `extract_watermark(img)` | Extract a signature payload from an image | | `verify_watermark(img, expected)` | Verify an image against an expected payload | | `hash_image(img)` | Compute SHA-256 hash of image pixels | ### Types | Type | Description | |------|-------------| | `SignaturePayload` | The watermark payload structure | | `VerifyResult` | Result of watermark verification | | `IntegrityLevel` | FULL, PARTIAL, or NONE | | `RsEncoder` | Reed-Solomon encoder | | `RsDecoder` | Reed-Solomon decoder with Berlekamp-Massey | ## Workspace Structure ``` rust-lib/ ├── Cargo.toml # Workspace definition ├── lib/ # Core library │ ├── Cargo.toml │ └── src/ │ ├── lib.rs # Module exports │ ├── reedsolomon.rs # Reed-Solomon codec │ ├── watermark.rs # LSB steganography │ └── crypto.rs # SHA-256 and PoW ├── cli/ # CLI binary │ ├── Cargo.toml │ └── src/main.rs ├── wasm/ # WASM bindings │ ├── Cargo.toml │ └── src/lib.rs ├── README.md └── Makefile ``` ## Building ```bash # Build all targets make build # Build CLI only make cli # Build WASM package make wasm # Run tests make test # Clean build artifacts make clean ``` ## Benchmarks Performance on typical hardware (AMD Ryzen 9 5900X): | Operation | Time | |-----------|------| | SHA-256 hash (4K image) | ~0.1 ms | | Embed watermark (4K image) | ~2 ms | | Extract watermark (4K image) | ~3 ms | | RS encode (100B payload) | ~0.05 ms | | RS decode (100B payload) | ~0.2 ms | | PoW nonce search (easy diff) | ~1-100 ms | ## License Licensed under either of: - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE)) - MIT license ([LICENSE-MIT](LICENSE-MIT)) at your option.