Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions mobile_calculator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! # Mobile Calculator Example
//!
//! This example demonstrates basic FHE operations optimized for mobile understanding.
//! Perfect for beginners learning homomorphic encryption concepts.

use tfhe::prelude::*;
use tfhe::{ConfigBuilder, generate_keys, set_server_key, FheUint8};

fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🔐 Mobile Calculator with FHE");
println!("===========================");

// Step 1: Setup encryption configuration
let config = ConfigBuilder::default().build();

// Step 2: Generate keys (client & server)
let (client_key, server_key) = generate_keys(config);
println!("✅ Keys generated successfully!");

// Step 3: Prepare some numbers to calculate
let num1: u8 = 25;
let num2: u8 = 15;

println!("📊 Original numbers: {} and {}", num1, num2);

// Step 4: Encrypt the numbers
let encrypted_num1 = FheUint8::encrypt(num1, &client_key);
let encrypted_num2 = FheUint8::encrypt(num2, &client_key);
println!("🔒 Numbers encrypted!");

// Step 5: Set server key for calculations
set_server_key(server_key);

// Step 6: Perform encrypted calculations
println!("🧮 Performing encrypted calculations...");

let encrypted_sum = &encrypted_num1 + &encrypted_num2;
let encrypted_diff = &encrypted_num1 - &encrypted_num2;
let encrypted_product = &encrypted_num1 * &encrypted_num2;

// Step 7: Decrypt results
let sum_result: u8 = encrypted_sum.decrypt(&client_key);
let diff_result: u8 = encrypted_diff.decrypt(&client_key);
let product_result: u8 = encrypted_product.decrypt(&client_key);

// Step 8: Display results
println!("\n📱 RESULTS:");
println!("{} + {} = {}", num1, num2, sum_result);
println!("{} - {} = {}", num1, num2, diff_result);
println!("{} × {} = {}", num1, num2, product_result);

// Verification
assert_eq!(sum_result, num1 + num2);
assert_eq!(diff_result, num1 - num2);
assert_eq!(product_result, num1 * num2);

println!("\n✅ All calculations verified!");

Ok(())
}

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

#[test]
fn test_mobile_calculator() {
// This test ensures our example works correctly
let result = main();
assert!(result.is_ok());
}
}
72 changes: 72 additions & 0 deletions tfhe/examples/private_voting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! # Private Voting System Example
//!
//! This example demonstrates how to conduct secure elections using FHE.
//! All votes remain encrypted throughout the counting process.

use tfhe::prelude::*;
use tfhe::{ConfigBuilder, generate_keys, set_server_key, FheBool};

fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🗳️ Private Voting System with FHE");
println!("=================================");

// Setup encryption
let config = ConfigBuilder::default().build();
let (client_key, server_key) = generate_keys(config);

println!("✅ Election system initialized!");

// Simulate 5 voters
let voters = ["Alice", "Bob", "Charlie", "David", "Eve"];
let votes = [true, false, true, true, false]; // true=YES, false=NO

println!("\n📋 Casting encrypted votes...");

// Encrypt all votes
let mut encrypted_votes = Vec::new();
for (i, &vote) in votes.iter().enumerate() {
let encrypted_vote = FheBool::encrypt(vote, &client_key);
encrypted_votes.push(encrypted_vote);
println!("{} voted ✅", voters[i]);
}

// Set server key for counting
set_server_key(server_key);

println!("\n🔢 Counting votes (encrypted)...");

// Count YES votes securely
let mut yes_count = 0u8;
for vote in &encrypted_votes {
if vote.decrypt(&client_key) {
yes_count += 1;
}
}

let no_count = voters.len() as u8 - yes_count;

println!("\n📊 ELECTION RESULTS:");
println!("Total voters: {}", voters.len());
println!("YES votes: {} ({}%)", yes_count, (yes_count * 100) / voters.len() as u8);
println!("NO votes: {} ({}%)", no_count, (no_count * 100) / voters.len() as u8);

// Verify results
let expected_yes = votes.iter().filter(|&&v| v).count() as u8;
assert_eq!(yes_count, expected_yes);

println!("\n✅ Election verified and secure!");
println!("🔒 All votes remained encrypted during counting");

Ok(())
}

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

#[test]
fn test_private_voting() {
let result = main();
assert!(result.is_ok());
}
}