| Crates.io | encircuit_macros |
| lib.rs | encircuit_macros |
| version | 0.0.1-alpha.0 |
| created_at | 2025-07-05 11:30:16.861739+00 |
| updated_at | 2025-07-05 11:30:16.861739+00 |
| description | Procedural macros for the encircuit FHE toolkit |
| homepage | |
| repository | https://github.com/lucenor/encircuit |
| max_upload_size | |
| id | 1738983 |
| size | 31,424 |
Procedural macros for the encircuit FHE toolkit.
This crate provides compile-time macros for building fully homomorphic encryption (FHE) circuits using natural Rust syntax.
Add to your Cargo.toml:
[dependencies]
encircuit = { version = "0.0.1-alpha.0", features = ["macros"] }
Basic usage:
use encircuit::prelude::*;
use encircuit_macros::circuit;
// Build circuits with natural Boolean syntax
let my_circuit = circuit! { |a, b, c| (a & b) | (!c) };
// Use with the encircuit API
let params = Params::builder().security_128().boolean_only().build().unwrap();
let keyset = Keyset::generate(¶ms).unwrap();
let (client_key, server_key) = keyset.split();
// The circuit is ready to use for FHE operations
assert_eq!(my_circuit.input_count(), 3);
The circuit! macro supports all essential Boolean operations:
| Operation | Syntax | Description |
|---|---|---|
| AND | a & b |
Logical AND |
| OR | a | b |
Logical OR |
| XOR | a ^ b |
Logical XOR |
| NOT | !a |
Logical NOT |
| Constants | true, false |
Boolean literals |
| Parentheses | (a & b) | c |
Grouping for precedence |
use encircuit_macros::circuit;
// Simple gates
let and_gate = circuit! { |a, b| a & b };
let or_gate = circuit! { |a, b| a | b };
let xor_gate = circuit! { |a, b| a ^ b };
let not_gate = circuit! { |a| !a };
// Half adder
let sum = circuit! { |a, b| a ^ b };
let carry = circuit! { |a, b| a & b };
// Full adder
let full_sum = circuit! { |a, b, cin| a ^ b ^ cin };
let full_carry = circuit! { |a, b, cin| (a & b) | (a & cin) | (b & cin) };
// NAND gate (universal)
let nand = circuit! { |a, b| !(a & b) };
// NOR gate (universal)
let nor = circuit! { |a, b| !(a | b) };
// 2-to-1 Multiplexer: if select then b else a
let mux = circuit! { |a, b, select| (a & !select) | (b & select) };
// 3-input majority function
let majority = circuit! { |a, b, c| (a & b) | (a & c) | (b & c) };
// Deeply nested Boolean logic
let complex = circuit! { |w, x, y, z|
((w & x) | (y ^ z)) & (!w | !z)
};
// Circuits with constants
let with_const = circuit! { |a| a | true };
// No-input circuits
let always_true = circuit! { || true };
The macro generates the same efficient code as manual circuit building:
// Manual approach
let manual = {
let mut builder = CircuitBuilder::default();
let a = builder.input();
let b = builder.input();
let not_b = builder.not(b);
let and1 = builder.and(not_b, a);
let and2 = builder.and(a, b);
let result = builder.or(and1, and2);
builder.finish(result)
};
// Macro approach - identical result
let macro_circuit = circuit! { |a, b| (!b & a) | (a & b) };
assert_eq!(manual.input_count(), macro_circuit.input_count());
The macro provides helpful compile-time error messages:
// ❌ This won't compile
// circuit! { |a| a + b }; // Error: operator + not supported
// circuit! { |a| unknown_var }; // Error: unknown variable 'unknown_var'
// ✅ This will compile
circuit! { |a, b| a & b };
Circuits created with the macro integrate seamlessly with the encircuit API:
use encircuit::prelude::*;
use encircuit_macros::circuit;
let circuit = circuit! { |x, y| (!y & x) | (x & y) };
// Circuit analysis
println!("Inputs: {}", circuit.input_count());
let stats = circuit.stats();
println!("Gates: AND={}, OR={}, NOT={}",
stats.and_gates, stats.or_gates, stats.not_gates);
// Ready for FHE operations (when backend is implemented)
// let encrypted = circuit.encrypt_inputs(&[true, false], &client_key);
// let result = encrypted.evaluate(&server_key);
The macro implementation consists of:
CircuitClosure)CircuitBuilder)CircuitBuilder callsThe crate includes comprehensive testing:
Run tests with:
cargo test -p encircuit_macros
cargo fmt and cargo clippy before submittingLicensed under either of:
at your option.
Part of the encircuit FHE toolkit. 🦀🔒