A Swift demonstration of homomorphic encryption using Apple's HomomorphicEncryption framework. This project showcases how to perform addition operations on encrypted data without ever decrypting it.
Homomorphic encryption is a form of encryption that allows computations to be performed on encrypted data without decrypting it first. The results of these computations remain encrypted, and when decrypted, match the results of operations performed on the plaintext.
This demonstration:
- Generates random numbers (between 100 and 999)
- Encrypts each number individually using the BFV (Brakerski-Fan-Vercauteren) homomorphic encryption scheme
- Computes the sum of all encrypted values without decrypting them
- Verifies that the decrypted result matches the expected sum
- Uses the BFV scheme with
UInt64precision - Encryption parameters configured for large sums (plaintext modulus ≈ 2^26 = 67,108,864)
- Can sum up to 8,192 encrypted values (configurable)
- Demonstrates coefficient encoding format
- Includes verification to prove the homomorphic computation is correct
- macOS with Swift
- Apple's HomomorphicEncryption framework
Simply run the project:
swift runThe program will:
- Generate and encrypt 72 random numbers (configurable in
main.swift:84) - Perform homomorphic addition on the encrypted values
- Decrypt and verify the result
The project uses predefined encryption parameters:
- Polynomial degree: 8192
- Plaintext modulus: ~2^26 (allows for large sums without overflow)
- Coefficient encoding: Each value is encoded as a polynomial coefficient
encryptRandomNumbers(): Generates random values and encrypts them individuallycalculateEncryptedSum(): Adds encrypted values using homomorphic operationsdemonstrateHomomorphicSum(): Orchestrates the complete workflow
=== Encryption of 72 random numbers ===
Values to encrypt: [145, 645, 654, 218, 396, 191, 983, 931, 367, 858, 961, 890, 823, 832, 825, 528, 287, 419, 522, 328, 223, 126, 401, 261, 671, 903, 451, 379, 246, 848, 781, 384, 383, 534, 788, 751, 572, 191, 801, 416, 308, 740, 154, 203, 607, 885, 432, 619, 917, 180, 976, 629, 407, 705, 774, 459, 995, 717, 802, 868, 816, 259, 659, 275, 486, 749, 529, 877, 975, 518, 479, 957]
Successfully encrypted 72 random numbers individually
Expected sum: 41869
=== Calculating encrypted sum ===
Homomorphic sum complete
=== Decrypted Homomorphic Result ===
Result from homomorphic operation: 41869
=== Compare Results ===
Expected sum: 41869
Actual result: 41869
Match: ✓ Success!
- Scheme: BFV (Brakerski-Fan-Vercauteren)
- Security: Uses secret key encryption
- Encoding: Coefficient format for efficient arithmetic operations
- Noise Management: Configured parameters ensure noise doesn't corrupt results