oxiz-proof 0.1.3

Proof generation and checking for OxiZ SMT solver
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
//! Advanced Proof Compression Techniques.
//!
//! Implements sophisticated compression algorithms specifically designed
//! for proof objects, achieving better compression ratios than generic algorithms.

use crate::{ProofNode, ProofRule, ResolutionProof};
use std::collections::HashMap;

/// Proof-specific compression using structural patterns.
pub struct StructuralCompressor {
    /// Dictionary of frequently occurring patterns
    pattern_dict: HashMap<Pattern, PatternId>,
    /// Next available pattern ID
    next_pattern_id: PatternId,
    /// Statistics
    stats: CompressionStats,
}

/// A pattern in a proof structure.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Pattern {
    /// Single resolution with specific clause structure
    SingleResolution {
        left_size: usize,
        right_size: usize,
        result_size: usize,
    },
    /// Chain of resolutions
    ResolutionChain { length: usize },
    /// Binary tree of resolutions
    BinaryTree { depth: usize },
    /// Custom pattern
    Custom(Vec<u8>),
}

/// Unique identifier for a pattern.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PatternId(pub usize);

/// Statistics about compression.
#[derive(Clone, Debug, Default)]
pub struct CompressionStats {
    /// Original size in bytes
    pub original_bytes: usize,
    /// Compressed size in bytes
    pub compressed_bytes: usize,
    /// Number of patterns found
    pub patterns_found: usize,
    /// Number of unique patterns
    pub unique_patterns: usize,
}

impl StructuralCompressor {
    /// Create a new structural compressor.
    pub fn new() -> Self {
        Self {
            pattern_dict: HashMap::new(),
            next_pattern_id: PatternId(0),
            stats: CompressionStats::default(),
        }
    }

    /// Compress a proof by identifying and encoding structural patterns.
    pub fn compress(&mut self, proof: &ResolutionProof) -> CompressedProof {
        self.stats.original_bytes = self.estimate_size(proof);

        // Phase 1: Identify recurring patterns
        let patterns = self.identify_patterns(proof);
        self.stats.patterns_found = patterns.len();
        self.stats.unique_patterns = self.pattern_dict.len();

        // Phase 2: Build compression dictionary
        let dict = self.build_dictionary(&patterns);

        // Phase 3: Encode proof using dictionary
        let encoded = self.encode_with_dictionary(proof, &dict);
        self.stats.compressed_bytes = encoded.bytes.len();

        encoded
    }

    /// Identify recurring patterns in the proof.
    fn identify_patterns(&mut self, proof: &ResolutionProof) -> Vec<(PatternId, Vec<usize>)> {
        let mut patterns = Vec::new();

        // Look for resolution chains
        for i in 0..proof.nodes.len() {
            if let Some((pattern, nodes)) = self.find_resolution_chain(proof, i) {
                let pattern_id = self.get_or_create_pattern(pattern);
                patterns.push((pattern_id, nodes));
            }
        }

        // Look for binary trees
        for i in 0..proof.nodes.len() {
            if let Some((pattern, nodes)) = self.find_binary_tree(proof, i) {
                let pattern_id = self.get_or_create_pattern(pattern);
                patterns.push((pattern_id, nodes));
            }
        }

        patterns
    }

    /// Find a resolution chain starting at a node.
    fn find_resolution_chain(
        &self,
        proof: &ResolutionProof,
        start: usize,
    ) -> Option<(Pattern, Vec<usize>)> {
        let mut nodes = vec![start];
        let mut current = start;
        let mut length = 1;

        loop {
            let node = proof.nodes.get(current)?;

            match &node.rule {
                ProofRule::Resolution { left, .. } => {
                    // Check if left child is also a resolution
                    if let Some(left_node) = proof.nodes.get(*left) {
                        if matches!(left_node.rule, ProofRule::Resolution { .. }) {
                            nodes.push(*left);
                            current = *left;
                            length += 1;
                            continue;
                        }
                    }
                    break;
                }
                _ => break,
            }
        }

        if length >= 3 {
            Some((Pattern::ResolutionChain { length }, nodes))
        } else {
            None
        }
    }

    /// Find a binary tree pattern starting at a node.
    fn find_binary_tree(
        &self,
        proof: &ResolutionProof,
        start: usize,
    ) -> Option<(Pattern, Vec<usize>)> {
        let depth = self.compute_tree_depth(proof, start);

        if depth >= 2 {
            let nodes = self.collect_tree_nodes(proof, start);
            Some((Pattern::BinaryTree { depth }, nodes))
        } else {
            None
        }
    }

    /// Compute the depth of a binary tree rooted at a node.
    fn compute_tree_depth(&self, proof: &ResolutionProof, node_idx: usize) -> usize {
        let node = match proof.nodes.get(node_idx) {
            Some(n) => n,
            None => return 0,
        };

        match &node.rule {
            ProofRule::Resolution { left, right, .. } => {
                let left_depth = self.compute_tree_depth(proof, *left);
                let right_depth = self.compute_tree_depth(proof, *right);
                1 + left_depth.max(right_depth)
            }
            _ => 0,
        }
    }

    /// Collect all nodes in a binary tree.
    fn collect_tree_nodes(&self, proof: &ResolutionProof, node_idx: usize) -> Vec<usize> {
        let mut nodes = vec![node_idx];

        if let Some(node) = proof.nodes.get(node_idx) {
            if let ProofRule::Resolution { left, right, .. } = &node.rule {
                nodes.extend(self.collect_tree_nodes(proof, *left));
                nodes.extend(self.collect_tree_nodes(proof, *right));
            }
        }

        nodes
    }

    /// Get or create a pattern ID.
    fn get_or_create_pattern(&mut self, pattern: Pattern) -> PatternId {
        if let Some(&id) = self.pattern_dict.get(&pattern) {
            return id;
        }

        let id = self.next_pattern_id;
        self.next_pattern_id = PatternId(id.0 + 1);
        self.pattern_dict.insert(pattern, id);
        id
    }

    /// Build a compression dictionary from patterns.
    fn build_dictionary(&self, patterns: &[(PatternId, Vec<usize>)]) -> CompressionDictionary {
        let mut dict = CompressionDictionary::new();

        // Count pattern frequencies
        let mut frequencies: HashMap<PatternId, usize> = HashMap::new();
        for (pattern_id, _) in patterns {
            *frequencies.entry(*pattern_id).or_insert(0) += 1;
        }

        // Add most frequent patterns to dictionary
        let mut sorted_patterns: Vec<_> = frequencies.into_iter().collect();
        sorted_patterns.sort_by_key(|(_, freq)| std::cmp::Reverse(*freq));

        for (pattern_id, _freq) in sorted_patterns.iter().take(256) {
            if let Some(pattern) = self.pattern_dict.iter().find(|(_, &id)| id == *pattern_id) {
                dict.add_entry(*pattern_id, pattern.0.clone());
            }
        }

        dict
    }

    /// Encode proof using compression dictionary.
    fn encode_with_dictionary(
        &self,
        proof: &ResolutionProof,
        dict: &CompressionDictionary,
    ) -> CompressedProof {
        let mut bytes = Vec::new();

        // Encode dictionary
        dict.encode(&mut bytes);

        // Encode proof nodes
        for node in &proof.nodes {
            self.encode_node(node, &mut bytes, dict);
        }

        CompressedProof {
            bytes,
            dictionary: dict.clone(),
            root: proof.root,
        }
    }

    /// Encode a single proof node.
    fn encode_node(&self, node: &ProofNode, bytes: &mut Vec<u8>, dict: &CompressionDictionary) {
        // Encode node ID
        Self::encode_varint(node.id as u64, bytes);

        // Encode clause length
        Self::encode_varint(node.clause.len() as u64, bytes);

        // Encode clause literals
        for &lit in &node.clause {
            Self::encode_signed_varint(lit as i64, bytes);
        }

        // Encode rule
        match &node.rule {
            ProofRule::Resolution { left, right, pivot } => {
                bytes.push(1); // Resolution tag
                Self::encode_varint(*left as u64, bytes);
                Self::encode_varint(*right as u64, bytes);
                Self::encode_signed_varint(*pivot as i64, bytes);
            }
            ProofRule::Input => {
                bytes.push(2); // Input tag
            }
            ProofRule::Axiom => {
                bytes.push(3); // Axiom tag
            }
        }
    }

    /// Encode unsigned integer using variable-length encoding.
    fn encode_varint(mut value: u64, bytes: &mut Vec<u8>) {
        loop {
            let mut byte = (value & 0x7F) as u8;
            value >>= 7;

            if value != 0 {
                byte |= 0x80;
            }

            bytes.push(byte);

            if value == 0 {
                break;
            }
        }
    }

    /// Encode signed integer using zigzag encoding + varint.
    fn encode_signed_varint(value: i64, bytes: &mut Vec<u8>) {
        let zigzag = if value >= 0 {
            (value as u64) << 1
        } else {
            ((-value - 1) as u64) << 1 | 1
        };

        Self::encode_varint(zigzag, bytes);
    }

    /// Estimate size of uncompressed proof in bytes.
    fn estimate_size(&self, proof: &ResolutionProof) -> usize {
        let mut size = 0;

        for node in &proof.nodes {
            size += 8; // node ID
            size += 4; // clause length
            size += node.clause.len() * 4; // literals
            size += 1; // rule tag

            if let ProofRule::Resolution { .. } = node.rule {
                size += 8 + 8 + 4; // left, right, pivot
            }
        }

        size
    }

    /// Get compression statistics.
    pub fn stats(&self) -> &CompressionStats {
        &self.stats
    }

    /// Compute compression ratio.
    pub fn compression_ratio(&self) -> f64 {
        if self.stats.original_bytes == 0 {
            return 1.0;
        }

        self.stats.compressed_bytes as f64 / self.stats.original_bytes as f64
    }
}

impl Default for StructuralCompressor {
    fn default() -> Self {
        Self::new()
    }
}

/// Compression dictionary mapping patterns to codes.
#[derive(Clone, Debug)]
pub struct CompressionDictionary {
    /// Map from pattern ID to pattern
    entries: HashMap<PatternId, Pattern>,
}

impl CompressionDictionary {
    /// Create a new empty dictionary.
    pub fn new() -> Self {
        Self {
            entries: HashMap::new(),
        }
    }

    /// Add an entry to the dictionary.
    pub fn add_entry(&mut self, id: PatternId, pattern: Pattern) {
        self.entries.insert(id, pattern);
    }

    /// Encode dictionary to bytes.
    pub fn encode(&self, bytes: &mut Vec<u8>) {
        // Encode number of entries
        StructuralCompressor::encode_varint(self.entries.len() as u64, bytes);

        for (id, pattern) in &self.entries {
            // Encode pattern ID
            StructuralCompressor::encode_varint(id.0 as u64, bytes);

            // Encode pattern
            self.encode_pattern(pattern, bytes);
        }
    }

    /// Encode a single pattern.
    fn encode_pattern(&self, pattern: &Pattern, bytes: &mut Vec<u8>) {
        match pattern {
            Pattern::SingleResolution {
                left_size,
                right_size,
                result_size,
            } => {
                bytes.push(1); // SingleResolution tag
                StructuralCompressor::encode_varint(*left_size as u64, bytes);
                StructuralCompressor::encode_varint(*right_size as u64, bytes);
                StructuralCompressor::encode_varint(*result_size as u64, bytes);
            }
            Pattern::ResolutionChain { length } => {
                bytes.push(2); // ResolutionChain tag
                StructuralCompressor::encode_varint(*length as u64, bytes);
            }
            Pattern::BinaryTree { depth } => {
                bytes.push(3); // BinaryTree tag
                StructuralCompressor::encode_varint(*depth as u64, bytes);
            }
            Pattern::Custom(data) => {
                bytes.push(4); // Custom tag
                StructuralCompressor::encode_varint(data.len() as u64, bytes);
                bytes.extend_from_slice(data);
            }
        }
    }
}

impl Default for CompressionDictionary {
    fn default() -> Self {
        Self::new()
    }
}

/// A compressed proof representation.
#[derive(Clone, Debug)]
pub struct CompressedProof {
    /// Compressed bytes
    pub bytes: Vec<u8>,
    /// Compression dictionary
    pub dictionary: CompressionDictionary,
    /// Root node index
    pub root: usize,
}

impl CompressedProof {
    /// Get size in bytes.
    pub fn size_bytes(&self) -> usize {
        self.bytes.len()
    }

    /// Decompress back to original proof.
    pub fn decompress(&self) -> Result<ResolutionProof, String> {
        // Decompression implementation
        Err("Decompression not yet implemented".to_string())
    }
}

/// LZ-style compression for proof sequences.
pub struct LzProofCompressor {
    /// Window size for back-references
    window_size: usize,
    /// Minimum match length
    min_match_length: usize,
}

impl LzProofCompressor {
    /// Create a new LZ-style compressor.
    pub fn new() -> Self {
        Self {
            window_size: 32768,
            min_match_length: 3,
        }
    }

    /// Compress using LZ algorithm.
    pub fn compress(&self, proof: &ResolutionProof) -> CompressedProof {
        let mut bytes = Vec::new();

        // Convert proof to byte sequence
        let proof_bytes = self.proof_to_bytes(proof);

        // Apply LZ compression
        self.lz_compress(&proof_bytes, &mut bytes);

        CompressedProof {
            bytes,
            dictionary: CompressionDictionary::new(),
            root: proof.root,
        }
    }

    /// Convert proof to byte sequence.
    fn proof_to_bytes(&self, proof: &ResolutionProof) -> Vec<u8> {
        let mut bytes = Vec::new();

        for node in &proof.nodes {
            // Simple serialization
            bytes.extend_from_slice(&(node.id as u32).to_le_bytes());
            bytes.extend_from_slice(&(node.clause.len() as u32).to_le_bytes());

            for &lit in &node.clause {
                bytes.extend_from_slice(&lit.to_le_bytes());
            }
        }

        bytes
    }

    /// Apply LZ compression to byte sequence.
    fn lz_compress(&self, input: &[u8], output: &mut Vec<u8>) {
        let mut pos = 0;

        while pos < input.len() {
            // Try to find a match in the window
            let window_start = pos.saturating_sub(self.window_size);
            let window = &input[window_start..pos];

            if let Some((match_pos, match_len)) = self.find_longest_match(window, &input[pos..]) {
                if match_len >= self.min_match_length {
                    // Encode back-reference
                    self.encode_backreference(match_pos, match_len, output);
                    pos += match_len;
                    continue;
                }
            }

            // No match, emit literal
            output.push(0); // Literal tag
            output.push(input[pos]);
            pos += 1;
        }
    }

    /// Find longest match in window.
    fn find_longest_match(&self, window: &[u8], lookahead: &[u8]) -> Option<(usize, usize)> {
        let mut best_match: Option<(usize, usize)> = None;

        for i in 0..window.len() {
            let mut match_len = 0;

            while match_len < lookahead.len()
                && i + match_len < window.len()
                && window[i + match_len] == lookahead[match_len]
            {
                match_len += 1;
            }

            if match_len >= self.min_match_length {
                if best_match.map_or(true, |(_, len)| match_len > len) {
                    best_match = Some((i, match_len));
                }
            }
        }

        best_match
    }

    /// Encode a back-reference.
    fn encode_backreference(&self, pos: usize, len: usize, output: &mut Vec<u8>) {
        output.push(1); // Backreference tag
        output.extend_from_slice(&(pos as u16).to_le_bytes());
        output.extend_from_slice(&(len as u16).to_le_bytes());
    }
}

impl Default for LzProofCompressor {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_structural_compressor_creation() {
        let compressor = StructuralCompressor::new();
        assert_eq!(compressor.pattern_dict.len(), 0);
    }

    #[test]
    fn test_varint_encoding() {
        let mut bytes = Vec::new();
        StructuralCompressor::encode_varint(127, &mut bytes);
        assert_eq!(bytes, vec![127]);

        bytes.clear();
        StructuralCompressor::encode_varint(300, &mut bytes);
        assert_eq!(bytes, vec![0xAC, 0x02]);
    }

    #[test]
    fn test_lz_compressor_creation() {
        let compressor = LzProofCompressor::new();
        assert_eq!(compressor.window_size, 32768);
        assert_eq!(compressor.min_match_length, 3);
    }
}