amaters-core 0.2.0

Core kernel for AmateRS - Fully Homomorphic Encrypted Database
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
//! Predicate-to-FHE-Circuit Compiler
//!
//! This module provides compilation of AmateRS query predicates into FHE circuits
//! that can be executed on encrypted data without revealing plaintext values.

use crate::compute::{
    Circuit, CircuitBuilder, CircuitNode, CircuitValue, CompareOperator, EncryptedType,
};
use crate::error::{AmateRSError, ErrorContext, Result};
use crate::types::{CipherBlob, ColumnRef, Predicate};

/// Compiles query predicates into executable FHE circuits
///
/// The PredicateCompiler transforms high-level query predicates (like `age > 18`)
/// into FHE circuits that can evaluate these conditions on encrypted data.
/// The result is always an encrypted boolean indicating whether the predicate
/// matches or not.
///
/// # Example
///
/// ```rust,ignore
/// use amaters_core::compute::{PredicateCompiler, EncryptedType};
/// use amaters_core::types::{Predicate, col, CipherBlob};
///
/// let mut compiler = PredicateCompiler::new();
///
/// // Compile: age > 18
/// let predicate = Predicate::Gt(col("age"), encrypted_18);
/// let circuit = compiler.compile(&predicate, EncryptedType::U8)?;
///
/// // The circuit can now be executed on encrypted age values
/// ```
pub struct PredicateCompiler {
    builder: CircuitBuilder,
}

impl PredicateCompiler {
    /// Create a new predicate compiler
    pub fn new() -> Self {
        Self {
            builder: CircuitBuilder::new(),
        }
    }

    /// Compile a predicate into an FHE circuit
    ///
    /// The resulting circuit will have inputs for:
    /// - `value`: The encrypted column value to test
    /// - `rhs`: The encrypted comparison value (right-hand side)
    ///
    /// The circuit output is an encrypted boolean indicating the predicate result.
    ///
    /// # Arguments
    ///
    /// * `predicate` - The predicate to compile
    /// * `value_type` - The encrypted type of the values being compared
    ///
    /// # Returns
    ///
    /// A `Circuit` that evaluates the predicate on encrypted data
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The predicate references undefined columns
    /// - Type inference fails
    /// - The circuit construction is invalid
    pub fn compile(&mut self, predicate: &Predicate, value_type: EncryptedType) -> Result<Circuit> {
        // Declare variables for the circuit
        self.builder.declare_variable("value", value_type);
        self.builder.declare_variable("rhs", value_type);

        // Compile the predicate into a circuit node
        let root = self.compile_node(predicate)?;

        // Build and return the circuit
        self.builder.build(root)
    }

    /// Recursively compile a predicate node into a circuit node
    fn compile_node(&self, predicate: &Predicate) -> Result<CircuitNode> {
        match predicate {
            Predicate::Eq(col, _value) => {
                // Equality: value == rhs
                self.validate_column(col)?;
                let value_node = self.builder.load("value");
                let rhs_node = self.builder.load("rhs");
                Ok(self.builder.eq(value_node, rhs_node))
            }

            Predicate::Gt(col, _value) => {
                // Greater than: value > rhs
                self.validate_column(col)?;
                let value_node = self.builder.load("value");
                let rhs_node = self.builder.load("rhs");
                Ok(self.builder.gt(value_node, rhs_node))
            }

            Predicate::Lt(col, _value) => {
                // Less than: value < rhs
                self.validate_column(col)?;
                let value_node = self.builder.load("value");
                let rhs_node = self.builder.load("rhs");
                Ok(self.builder.lt(value_node, rhs_node))
            }

            Predicate::Gte(col, _value) => {
                // Greater than or equal: value >= rhs
                self.validate_column(col)?;
                let value_node = self.builder.load("value");
                let rhs_node = self.builder.load("rhs");
                // Implement as NOT (value < rhs)
                let lt_node = self.builder.lt(value_node, rhs_node);
                Ok(self.builder.not(lt_node))
            }

            Predicate::Lte(col, _value) => {
                // Less than or equal: value <= rhs
                self.validate_column(col)?;
                let value_node = self.builder.load("value");
                let rhs_node = self.builder.load("rhs");
                // Implement as NOT (value > rhs)
                let gt_node = self.builder.gt(value_node, rhs_node);
                Ok(self.builder.not(gt_node))
            }

            Predicate::And(left, right) => {
                // Logical AND: left AND right
                // Note: This requires both predicates to reference the same value
                // For now, we'll compile recursively but this may need refinement
                // for multi-column predicates
                let left_circuit = self.compile_node(left)?;
                let right_circuit = self.compile_node(right)?;
                Ok(self.builder.and(left_circuit, right_circuit))
            }

            Predicate::Or(left, right) => {
                // Logical OR: left OR right
                let left_circuit = self.compile_node(left)?;
                let right_circuit = self.compile_node(right)?;
                Ok(self.builder.or(left_circuit, right_circuit))
            }

            Predicate::Not(pred) => {
                // Logical NOT: NOT pred
                let pred_circuit = self.compile_node(pred)?;
                Ok(self.builder.not(pred_circuit))
            }
        }
    }

    /// Validate that a column reference is supported
    ///
    /// For now, we only support single-column predicates with the column named "value"
    fn validate_column(&self, col: &ColumnRef) -> Result<()> {
        // In the current design, we're evaluating predicates on individual values
        // The column reference should match what we're testing
        // For now, we accept any column name since we're binding it to "value"
        let _ = col;
        Ok(())
    }

    /// Extract the RHS (right-hand side) value from a predicate
    ///
    /// This walks the predicate tree to find comparison values.
    /// For composite predicates (And/Or/Not), it extracts from the first
    /// comparison it encounters.
    ///
    /// # Arguments
    ///
    /// * `predicate` - The predicate to extract from
    ///
    /// # Returns
    ///
    /// The encrypted value used in the predicate comparison
    ///
    /// # Errors
    ///
    /// Returns an error if the predicate contains no comparison operations
    pub fn extract_rhs_value(predicate: &Predicate) -> Result<CipherBlob> {
        match predicate {
            Predicate::Eq(_, value)
            | Predicate::Gt(_, value)
            | Predicate::Lt(_, value)
            | Predicate::Gte(_, value)
            | Predicate::Lte(_, value) => Ok(value.clone()),

            Predicate::And(left, _right) => {
                // For AND, extract from left (could also merge both)
                Self::extract_rhs_value(left)
            }

            Predicate::Or(left, _right) => {
                // For OR, extract from left
                Self::extract_rhs_value(left)
            }

            Predicate::Not(pred) => {
                // For NOT, extract from inner predicate
                Self::extract_rhs_value(pred)
            }
        }
    }

    /// Extract all RHS values from a predicate
    ///
    /// For composite predicates, this returns all comparison values.
    /// This is useful for complex predicates like `age > 18 AND age < 65`
    /// which have multiple RHS values.
    ///
    /// # Arguments
    ///
    /// * `predicate` - The predicate to extract from
    ///
    /// # Returns
    ///
    /// A vector of all encrypted values used in comparisons
    pub fn extract_all_rhs_values(predicate: &Predicate) -> Vec<CipherBlob> {
        match predicate {
            Predicate::Eq(_, value)
            | Predicate::Gt(_, value)
            | Predicate::Lt(_, value)
            | Predicate::Gte(_, value)
            | Predicate::Lte(_, value) => vec![value.clone()],

            Predicate::And(left, right) => {
                let mut values = Self::extract_all_rhs_values(left);
                values.extend(Self::extract_all_rhs_values(right));
                values
            }

            Predicate::Or(left, right) => {
                let mut values = Self::extract_all_rhs_values(left);
                values.extend(Self::extract_all_rhs_values(right));
                values
            }

            Predicate::Not(pred) => Self::extract_all_rhs_values(pred),
        }
    }

    /// Get the required encrypted type for a predicate's values
    ///
    /// This analyzes the predicate to determine what type of encrypted values
    /// it operates on. This is useful for automatic type inference.
    ///
    /// # Arguments
    ///
    /// * `predicate` - The predicate to analyze
    ///
    /// # Returns
    ///
    /// The encrypted type hint, or None if it cannot be determined
    pub fn infer_value_type(_predicate: &Predicate) -> Option<EncryptedType> {
        // For now, we don't have type information in the predicate itself
        // This would require extending the Predicate enum with type metadata
        // or analyzing the CipherBlob metadata
        None
    }
}

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

/// Helper function to compile a simple predicate
///
/// This is a convenience wrapper around PredicateCompiler for single predicates.
///
/// # Example
///
/// ```rust,ignore
/// use amaters_core::compute::{compile_predicate, EncryptedType};
/// use amaters_core::types::{Predicate, col};
///
/// let predicate = Predicate::Gt(col("age"), encrypted_18);
/// let circuit = compile_predicate(&predicate, EncryptedType::U8)?;
/// ```
pub fn compile_predicate(predicate: &Predicate, value_type: EncryptedType) -> Result<Circuit> {
    let mut compiler = PredicateCompiler::new();
    compiler.compile(predicate, value_type)
}

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

    fn make_test_blob(value: u8) -> CipherBlob {
        CipherBlob::new(vec![value])
    }

    #[test]
    fn test_compiler_creation() {
        let compiler = PredicateCompiler::new();
        assert_eq!(compiler.builder.variable_types().len(), 0);
    }

    #[test]
    fn test_compile_eq_predicate() -> Result<()> {
        let mut compiler = PredicateCompiler::new();
        let predicate = Predicate::Eq(col("age"), make_test_blob(18));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);
        assert_eq!(circuit.variable_types.len(), 2);
        assert!(circuit.variable_types.contains_key("value"));
        assert!(circuit.variable_types.contains_key("rhs"));

        Ok(())
    }

    #[test]
    fn test_compile_gt_predicate() -> Result<()> {
        let mut compiler = PredicateCompiler::new();
        let predicate = Predicate::Gt(col("age"), make_test_blob(18));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);
        assert!(circuit.gate_count > 0);

        Ok(())
    }

    #[test]
    fn test_compile_lt_predicate() -> Result<()> {
        let mut compiler = PredicateCompiler::new();
        let predicate = Predicate::Lt(col("age"), make_test_blob(65));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);

        Ok(())
    }

    #[test]
    fn test_compile_gte_predicate() -> Result<()> {
        let mut compiler = PredicateCompiler::new();
        let predicate = Predicate::Gte(col("age"), make_test_blob(18));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);
        // Gte is implemented as NOT (value < rhs), so should have a NOT gate
        assert!(matches!(circuit.root, CircuitNode::UnaryOp { .. }));

        Ok(())
    }

    #[test]
    fn test_compile_lte_predicate() -> Result<()> {
        let mut compiler = PredicateCompiler::new();
        let predicate = Predicate::Lte(col("age"), make_test_blob(65));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);
        // Lte is implemented as NOT (value > rhs), so should have a NOT gate
        assert!(matches!(circuit.root, CircuitNode::UnaryOp { .. }));

        Ok(())
    }

    #[test]
    fn test_compile_and_predicate() -> Result<()> {
        let mut compiler = PredicateCompiler::new();

        // age > 18 AND age < 65
        let pred1 = Predicate::Gt(col("age"), make_test_blob(18));
        let pred2 = Predicate::Lt(col("age"), make_test_blob(65));
        let predicate = Predicate::And(Box::new(pred1), Box::new(pred2));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);
        assert!(matches!(circuit.root, CircuitNode::BinaryOp { .. }));

        // Should have more gates due to AND
        assert!(circuit.gate_count >= 2);

        Ok(())
    }

    #[test]
    fn test_compile_or_predicate() -> Result<()> {
        let mut compiler = PredicateCompiler::new();

        // age < 18 OR age > 65
        let pred1 = Predicate::Lt(col("age"), make_test_blob(18));
        let pred2 = Predicate::Gt(col("age"), make_test_blob(65));
        let predicate = Predicate::Or(Box::new(pred1), Box::new(pred2));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);
        assert!(matches!(circuit.root, CircuitNode::BinaryOp { .. }));

        Ok(())
    }

    #[test]
    fn test_compile_not_predicate() -> Result<()> {
        let mut compiler = PredicateCompiler::new();

        // NOT (age == 18)
        let pred = Predicate::Eq(col("age"), make_test_blob(18));
        let predicate = Predicate::Not(Box::new(pred));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);
        assert!(matches!(circuit.root, CircuitNode::UnaryOp { .. }));

        Ok(())
    }

    #[test]
    fn test_compile_complex_predicate() -> Result<()> {
        let mut compiler = PredicateCompiler::new();

        // (age > 18 AND age < 65) OR age == 100
        let pred1 = Predicate::Gt(col("age"), make_test_blob(18));
        let pred2 = Predicate::Lt(col("age"), make_test_blob(65));
        let and_pred = Predicate::And(Box::new(pred1), Box::new(pred2));

        let pred3 = Predicate::Eq(col("age"), make_test_blob(100));
        let predicate = Predicate::Or(Box::new(and_pred), Box::new(pred3));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);
        // Complex predicate should have multiple gates
        assert!(circuit.gate_count >= 3);
        assert!(circuit.depth >= 2);

        Ok(())
    }

    #[test]
    fn test_extract_rhs_value() -> Result<()> {
        let blob = make_test_blob(42);
        let predicate = Predicate::Gt(col("age"), blob.clone());

        let extracted = PredicateCompiler::extract_rhs_value(&predicate)?;
        assert_eq!(extracted, blob);

        Ok(())
    }

    #[test]
    fn test_extract_rhs_from_and() -> Result<()> {
        let blob1 = make_test_blob(18);
        let blob2 = make_test_blob(65);

        let pred1 = Predicate::Gt(col("age"), blob1.clone());
        let pred2 = Predicate::Lt(col("age"), blob2);
        let predicate = Predicate::And(Box::new(pred1), Box::new(pred2));

        // Should extract from left predicate
        let extracted = PredicateCompiler::extract_rhs_value(&predicate)?;
        assert_eq!(extracted, blob1);

        Ok(())
    }

    #[test]
    fn test_extract_all_rhs_values() {
        let blob1 = make_test_blob(18);
        let blob2 = make_test_blob(65);

        let pred1 = Predicate::Gt(col("age"), blob1.clone());
        let pred2 = Predicate::Lt(col("age"), blob2.clone());
        let predicate = Predicate::And(Box::new(pred1), Box::new(pred2));

        let values = PredicateCompiler::extract_all_rhs_values(&predicate);
        assert_eq!(values.len(), 2);
        assert_eq!(values[0], blob1);
        assert_eq!(values[1], blob2);
    }

    #[test]
    fn test_compile_predicate_helper() -> Result<()> {
        let predicate = Predicate::Eq(col("age"), make_test_blob(18));
        let circuit = compile_predicate(&predicate, EncryptedType::U8)?;

        assert_eq!(circuit.result_type, EncryptedType::Bool);

        Ok(())
    }

    #[test]
    fn test_circuit_validation() -> Result<()> {
        let mut compiler = PredicateCompiler::new();
        let predicate = Predicate::Gt(col("age"), make_test_blob(18));

        let circuit = compiler.compile(&predicate, EncryptedType::U8)?;

        // Circuit should be valid
        circuit.validate()?;

        Ok(())
    }
}