apple / swift-homomorphic-encryption

Homomorphic Encryption library and applications in Swift

Home Page:https://swiftpackageindex.com/apple/swift-homomorphic-encryption/documentation/homomorphicencryption

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swift Homomorphic Encryption

Swift Homomorphic Encryption is a Swift implementation of homomorphic encryption (HE) and applications including Private Information Retrieval (PIR).

Applications of Swift Homomorphic Encryption include:

Overview

Swift Homomorphic Encryption is a collection of libraries and executables. For more information, refer to documentation for the libraries:

and executables:

The documentation is hosted on the Swift Package Index.

Background

Homomorphic Encryption (HE)

Swift Homomorphic Encryption implements a special form of cryptography called homomorphic encryption (HE). HE is a cryptosystem which enables computation on encrypted data. The computation is performed directly on the encrypted data, without revealing the plaintext of that data to the operating process. HE computations therefore happen without decryption or access to the decryption key.

HE thereby allows a client to enable a server to perform operations on encrypted data, and therefore without revealing the data to server. A typical HE workflow might be:

  • The client encrypts its sensitive data and sends the resulting ciphertext to the server.
  • The server performs HE computation on the ciphertext (and perhaps its own plaintext inputs), without learning what any ciphertext decrypts to.
  • The server sends the resulting ciphertext response to the client.
  • The client decrypts to learn the response.

Swift Homomorphic Encryption implements the Brakerski-Fan-Vercauteren (BFV) HE scheme, which is based on the ring learning with errors (RLWE) hardness problem. This scheme can be configured to support post-quantum 128-bit security.

Warning

BFV does not provide IND-CCA security, and should be used accordingly. In particular, as little information as possible about each decrypted ciphertext should be sent back to the server. To protect against a malicious server, the client should also validate the decrypted content is in the expected format.

Consult a cryptography expert when developing and deploying homomorphic encryption applications.

Private Information Retrieval (PIR)

Private information retrieval (PIR) is one application of HE. PIR allows a client to perform a database lookup from a server hosting a keyword-value database, without the server learning the keyword in the client's query.. Each row in the database is a keyword with an associated value. During the PIR protocol, the client issues a query using its private keyword, and learns the value associated with the keyword.

A trivial implementation of PIR is to have the client issue a generic "fetch database" request, independent of its private keyword. Then the server server sends the entire database to the client. While this trivial PIR protocol satisfies the privacy and correctness requirements of PIR, it is only feasible for small databases.

The PIR implementation in Swift Homomorphic Encryption uses HE to improve upon the trivial PIR protocol.

Warning

PIR is asymmetric, meaning the client may learn keyword-value pairs not requested, as happens in trivial PIR for instance. A variant of PIR, known as symmetric PIR, would be required to ensure the client does not learn anything about values it did not request.

Using Swift Homomorphic Encryption

Swift Homomorphic Encryption requires:

  • 64-bit processor with little-endian memory representation
  • macOS or Linux operating system
  • Swift version 5.10 or later

Note

Swift Homomorphic Encryption relies on SystemRandomNumberGenerator as a cryptographically secure random number generator, which may have platform-dependent behavior.

Swift Homomorphic Encryption is available as a Swift Package Manager package. To use Swift Homomorphic Encryption, choose a tag. Then, add the following dependency in your Package.swift

.package(
    url: "https://github.com/apple/swift-homomorphic-encryption",
    from: "tag"),

, replacing tag with your chosen tag, e.g. 1.0.0-alpha.3.

To use the HomomorphicEncryption library, add

.product(name: "HomomorphicEncryption", package: "swift-homomorphic-encryption"),

to your target's dependencies.

Important

When linking your executable, make sure to enable cross-module-optimization. Without this flag, performance of Swift Homomorphic Encryption degrades dramatically, due to failure to specialize generics. For example,

.executableTarget(
   name: "YourTarget",
   dependencies: [
       .product(name: "HomomorphicEncryption", package: "swift-homomorphic-encryption"),
   ],
   swiftSettings: [.unsafeFlags(["-cross-module-optimization"],
      .when(configuration: .release))]
)

You can then add

 import HomomorphicEncryption

to your Swift code to access the functionality in the HomomorphicEncryption library.

See the example Snippets for examples of using HomomorphicEncryption. To run the EncryptionParametersSnippet example, run

swift run -c release EncryptionParametersSnippet

Developing Swift Homomorphic Encryption

Dependencies

Building Swift Homomorphic Encryption requires:

  • Swift version 5.10 or later

Additionally, developing Swift Homomorphic Encryption requires:

Building

You can build Swift Homomorphic Encryption either via Xcode or via command line in a terminal.

After cloning the repository, run

cd swift-homomorphic-encryption
git submodule update --init --recursive

Xcode

To build Swift Homomorphic Encryption from Xcode, simply open the root directory in Xcode. See the Xcode documentation for more details on developing with Xcode.

Command line

To build Swift Homomorphic Encryption from command line, open the root directory (i.e., the swift-homomorphic-encryption directory) of the cloned repository in a terminal, and run

swift build -c release

The build products will be in the .build/release/ folder.

To build in debug mode, run

swift build

The build products will be in the .build/debug/ folder.

Warning

Runtimes may be much slower in debug mode.

Installing

To install Swift Homomorphic Encryption targets, use the experimental-install feature of Swift Package Manager.

First ensure sure that the ~/.swiftpm/bin directory is on your $PATH. For example, if using the zsh shell, add the following line to your ~/.zshrc

export PATH="$HOME/.swiftpm/bin:$PATH"

Make sure to reload the path via (source ~/.zshrc) or by restarting your terminal emulator.

Then, to install the PIRProcessDatabase, executable, e.g., run

swift package experimental-install -c release --product PIRProcessDatabase

Testing

Run unit tests via

swift test -c release --parallel

To run tests in debug mode, run

swift test --parallel

Warning

Tests will be slow in debug mode.

Benchmarking

Swift homomorphic encryption uses Benchmark for benchmarking. By default, benchmarking requires the jemalloc dependency.

Warning

The benchmark may crash intermittently due to a known issue. For reliable execution, benchmark can be run without jemalloc as described here.

Two ways to run the benchmarks are:

  • Xcode
    • Open the swift-homomorphic-encryption folder in Xcode.
    • Switch to a benchmark target.
    • Run the target, e.g., via the Product menu.
  • Command line
    • Run swift package benchmark.
    • See the Benchmark documentation for more information on running benchmarks.

Contributing

If you are interested in making a contribution to Swift Homomorphic Encryption, see our contributing guide.

Documentation

Swift Homomorphic Encryption uses DocC for documentation. For more information, refer to the DocC documentation and the Swift-DocC Plugin.

Xcode

The documentation can be built from Xcode via Product -> Build Documentation.

Command line

The documentation can be built from command line by running

swift package generate-documentation

and previewed by running

swift package --disable-sandbox preview-documentation --target HomomorphicEncryption

About

Homomorphic Encryption library and applications in Swift

https://swiftpackageindex.com/apple/swift-homomorphic-encryption/documentation/homomorphicencryption

License:Apache License 2.0


Languages

Language:Swift 99.7%Language:Shell 0.2%Language:C 0.1%