Skip to content

This project showcases how to perform addition operations on encrypted data without ever decrypting it using Apple's HomomorphicEncryption framework.

License

Notifications You must be signed in to change notification settings

peterspath/HomomorphicSum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Homomorphic Sum

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.

What is Homomorphic Encryption?

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.

What This Project Does

This demonstration:

  1. Generates random numbers (between 100 and 999)
  2. Encrypts each number individually using the BFV (Brakerski-Fan-Vercauteren) homomorphic encryption scheme
  3. Computes the sum of all encrypted values without decrypting them
  4. Verifies that the decrypted result matches the expected sum

Key Features

  • Uses the BFV scheme with UInt64 precision
  • 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

Requirements

  • macOS with Swift
  • Apple's HomomorphicEncryption framework

Usage

Simply run the project:

swift run

The 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

How It Works

Encryption Parameters

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

Key Functions

  • encryptRandomNumbers(): Generates random values and encrypts them individually
  • calculateEncryptedSum(): Adds encrypted values using homomorphic operations
  • demonstrateHomomorphicSum(): Orchestrates the complete workflow

Example Output

=== 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!

Technical Details

  • 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

About

This project showcases how to perform addition operations on encrypted data without ever decrypting it using Apple's HomomorphicEncryption framework.

Topics

Resources

License

Stars

Watchers

Forks

Languages