{"id":13994928,"url":"https://github.com/apple/swift-homomorphic-encryption","last_synced_at":"2025-05-15T16:05:54.385Z","repository":{"id":248153731,"uuid":"816358583","full_name":"apple/swift-homomorphic-encryption","owner":"apple","description":"Homomorphic Encryption library and applications in Swift","archived":false,"fork":false,"pushed_at":"2025-05-08T21:17:57.000Z","size":1289,"stargazers_count":530,"open_issues_count":9,"forks_count":34,"subscribers_count":23,"default_branch":"main","last_synced_at":"2025-05-08T22:26:20.427Z","etag":null,"topics":["cryptography","homomorphic-encryption","swift"],"latest_commit_sha":null,"homepage":"https://swiftpackageindex.com/apple/swift-homomorphic-encryption/documentation/homomorphicencryption","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apple.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":"CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-06-17T15:25:24.000Z","updated_at":"2025-05-08T17:06:11.000Z","dependencies_parsed_at":"2024-08-12T16:37:00.825Z","dependency_job_id":"8b0d121b-1161-4a19-b102-e4c03f741980","html_url":"https://github.com/apple/swift-homomorphic-encryption","commit_stats":null,"previous_names":["apple/swift-homomorphic-encryption"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-homomorphic-encryption","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-homomorphic-encryption/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-homomorphic-encryption/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apple%2Fswift-homomorphic-encryption/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apple","download_url":"https://codeload.github.com/apple/swift-homomorphic-encryption/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254374427,"owners_count":22060611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","homomorphic-encryption","swift"],"created_at":"2024-08-09T14:03:10.958Z","updated_at":"2025-05-15T16:05:54.378Z","avatar_url":"https://github.com/apple.png","language":"Swift","funding_links":[],"categories":["By Language","Swift","Awesome Privacy Engineering [![Awesome](https://awesome.re/badge.svg)](https://awesome.re)"],"sub_categories":["Swift","Homomorphic Encryption"],"readme":"# Swift Homomorphic Encryption\n\n*Swift Homomorphic Encryption* is a Swift implementation of homomorphic encryption (HE) and applications including Private Information Retrieval (PIR).\n\nApplications of Swift Homomorphic Encryption include:\n* [Live Caller ID Lookup](https://github.com/apple/live-caller-id-lookup-example)\n\n## Overview\nSwift Homomorphic Encryption is a collection of libraries and executables.\nFor more information, refer to documentation for the libraries:\n* [HomomorphicEncryptionProtobuf](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/homomorphicencryptionprotobuf)\n* [HomomorphicEncryption](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/homomorphicencryption)\n* [PrivateInformationRetrievalProtobuf](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privateinformationretrievalprotobuf)\n* [PrivateInformationRetrieval](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privateinformationretrieval)\n* [PrivateNearestNeighborSearchProtobuf](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborsearchprotobuf)\n* [PrivateNearestNeighborSearch](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborsearch)\n\nand executables:\n* [PIRGenerateDatabase](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/pirgeneratedatabase)\n* [PIRProcessDatabase](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/pirprocessdatabase)\n* [PIRShardDatabase](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/pirsharddatabase)\n* [PNNSGenerateDatabase](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/pnnsgeneratedatabase)\n* [PNNSProcessDatabase](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/pnnsprocessdatabase)\n\nThe documentation is hosted on the [Swift Package Index](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/documentation).\n\n## Background\n### Homomorphic Encryption (HE)\nSwift Homomorphic Encryption implements a special form of cryptography called homomorphic encryption (HE).\nHE is a cryptosystem which enables computation on encrypted data.\nThe computation is performed directly on the encrypted data, *without revealing the plaintext of that data to the operating process*.\nHE computations therefore happen without decryption or access to the decryption key.\n\nHE thereby allows a client to enable a server to perform operations on encrypted data, and therefore without revealing the data to server.\nA typical HE workflow might be:\n* The client encrypts its sensitive data and sends the resulting ciphertext to the server.\n* The server performs HE computation on the ciphertext (and perhaps its own plaintext inputs), without learning what any ciphertext decrypts to.\n* The server sends the resulting ciphertext response to the client.\n* The client decrypts to learn the response.\n\nSwift Homomorphic Encryption implements the Brakerski-Fan-Vercauteren (BFV) HE scheme, which is based on the ring learning with errors (RLWE) hardness problem.\nThis scheme can be configured to support post-quantum 128-bit security.\n\n\u003e [!WARNING]\n\u003e BFV does not provide IND-CCA security, nor does it provide IND-CPA\u003csup\u003eD\u003c/sup\u003e security when there is a non-negligible decryption error probability. BFV should be used accordingly.\n\u003e In particular, no information 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.\n\u003e\n\u003e Consult a cryptography expert when developing and deploying homomorphic encryption applications.\n\n### Private Information Retrieval (PIR)\nPrivate information retrieval (PIR) is one application of HE.\nPIR enables 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.*.\nEach row in the database is a *keyword* with an associated *value*.\nDuring the PIR protocol, the client issues a query using its private keyword, and learns the value associated with the keyword.\n\nA trivial implementation of PIR is to have the client issue a generic \"fetch database\" request, independent of its private keyword.\nThen the server sends the entire database to the client.\nWhile this *trivial PIR* protocol satisfies the privacy and correctness requirements of PIR, it is only feasible for small databases.\n\nThe PIR implementation in Swift Homomorphic Encryption uses HE to improve upon the trivial PIR protocol.\n\n\u003e [!WARNING]\n\u003e PIR is asymmetric, meaning the client may learn keyword-value pairs not requested, as happens in trivial PIR for instance.\n\u003e 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.\n\n### Private Nearest Neighbor Search (PNNS)\nPrivate nearest neighbor search (PNNS) enables a client with a private vector to search for the nearest vectors in a database hosted by a server, *without the server learning the client's vector.*.\nEach row in the database is a *vector* with an associated *entry identifier* and *entry metadata*.\nDuring the PNNS protocol, the client issues a query using its private vector, and learns the nearest neighbor according to a ``DistanceMetric``.\nSpecifically, the client learns the distances between the client's query vector to the nearest neighbor, as well as the entry identifier and entry metadata of the nearest neighbor.\n\nA trivial implementation of PNNS is to have the client issue a generic \"fetch database\" request, independent of its private vector.\nThen the server sends the entire database to the client, who computes the distances locally.\nWhile this *trivial PNNS* protocol satisfies the privacy and correctness requirements of PNNS, it is only feasible for small databases.\n\nThe PNNS implementation in Swift Homomorphic Encryption uses homomorphic encryption to improve upon the trivial PNNS protocol.\n\n## Using Swift Homomorphic Encryption\nSwift Homomorphic Encryption is available as a Swift Package Manager package.\nTo use Swift Homomorphic Encryption, choose a [tag](https://github.com/apple/swift-homomorphic-encryption/tags).\nThen, add the following dependency in your `Package.swift`\n```swift\n.package(\n    url: \"https://github.com/apple/swift-homomorphic-encryption\",\n    from: \"tag\"),\n```\n, replacing `tag` with your chosen tag, e.g. `1.0.0`.\n\nTo use the `HomomorphicEncryption` library, add\n```swift\n.product(name: \"HomomorphicEncryption\", package: \"swift-homomorphic-encryption\"),\n```\nto your target's dependencies.\n\n\u003e [!IMPORTANT]\n\u003e When linking your executable, make sure to set `-cross-module-optimization`.\n\u003e Without this flag, performance of Swift Homomorphic Encryption degrades dramatically,\n\u003e due to failure to specialize generics. For example,\n\u003e ```swift\n\u003e .executableTarget(\n\u003e    name: \"YourTarget\",\n\u003e    dependencies: [\n\u003e        .product(name: \"HomomorphicEncryption\", package: \"swift-homomorphic-encryption\"),\n\u003e    ],\n\u003e    swiftSettings: [.unsafeFlags([\"-cross-module-optimization\"],\n\u003e       .when(configuration: .release))]\n\u003e )\n\u003e ```\n\nYou can then add\n```swift\n import HomomorphicEncryption\n ```\nto your Swift code to access the functionality in the `HomomorphicEncryption` library.\n\n\u003e [!NOTE]\n\u003e If you are using Swift Homomorphic Encryption for research, please cite using the\n\u003e [CITATION.cff](CITATION.cff) file.\n\n#### Examples\nSee the [Snippets](https://github.com/apple/swift-homomorphic-encryption/tree/main/Snippets) for examples using `HomomorphicEncryption`.\nTo run the `EncryptionParametersSnippet`, run\n```\nswift run -c release EncryptionParametersSnippet\n```\n\n### Supported Platforms\nSwift Homomorphic Encryption aims to support all of the platforms where Swift is supported.\n\n\u003e [!NOTE]\n\u003e Swift Homomorphic Encryption relies on [SystemRandomNumberGenerator](https://developer.apple.com/documentation/swift/systemrandomnumbergenerator) as a cryptographically secure random number generator, which may have platform-dependent behavior.\n\n### Swift / Xcode versions\nThe following table maps Swift Homomorphic Encryption package versions to required Swift and Xcode versions:\n\nPackage version | Swift version | Xcode version\n----------------|---------------|-----------------------------------------\n1.0.x           | \u003e= Swift 5.10 | \u003e= Xcode 15.3\nmain            | \u003e= Swift 6.0  | \u003e= Xcode 16.1\n\n### Source Stability\nSwift Homomorphic Encryption follows [Semantic Versioning 2.0.0](https://semver.org/spec/v2.0.0.html). Source breaking changes to the public API can only land in a new major version, with the following exception:\n\n* Adding a new `case` to a public `enum` type will require only a minor version bump. For instance, we may add a new `enum` to an [HeError](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/documentation/homomorphicencryption/heerror). To avoid breaking source code, add a  `default` case when adding a `switch` on the enum values.\n\nFuture minor versions of the package may introduce changes to these rules as needed.\n\nWe'd like this package to quickly embrace Swift language and toolchain improvements that are relevant to its mandate. Accordingly, from time to time, we expect that new versions of this package will require clients to upgrade to a more recent Swift toolchain release. Requiring a new Swift release will only require a minor version bump.\n\n\u003e [!WARNING]\n\u003e Any symbol beginning with an underscore, and any product beginning with an underscore, is not subject to semantic versioning: these APIs may change without warning.\n\n## Developing Swift Homomorphic Encryption\n### Dependencies\nDeveloping Swift Homomorphic Encryption requires:\n* [Nick Lockwood SwiftFormat](https://github.com/nicklockwood/SwiftFormat), 0.55.5\n* [pre-commit](https://pre-commit.com)\n* [swift-format](https://github.com/swiftlang/swift-format), 600.0.0\n* [swift-protobuf](https://github.com/apple/swift-protobuf), 1.29.0\n* [SwiftLint](https://github.com/realm/SwiftLint), 0.58.2\n\n### Building\nYou can build Swift Homomorphic Encryption either via Xcode or via command line in a terminal.\n\nAfter cloning the repository, run\n```sh\ncd swift-homomorphic-encryption\ngit submodule update --init --recursive\n```\n\n#### Xcode\nTo build Swift Homomorphic Encryption from Xcode, simply open the root directory in Xcode.\nSee the [Xcode documentation](https://developer.apple.com/documentation/Xcode) for more details on developing with Xcode.\n\n#### Command line\nTo 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\n```sh\nswift build -c release\n```\nThe build products will be in the `.build/release/` folder.\n\nTo build in debug mode, run\n```sh\nswift build\n```\nThe build products will be in the `.build/debug/` folder.\n\u003e [!WARNING]\n\u003e Runtimes may be much slower in debug mode.\n\n### Installing\nTo install Swift Homomorphic Encryption targets,  use the `experimental-install` feature of Swift Package Manager.\n\nFirst ensure that the `~/.swiftpm/bin` directory is on your `$PATH`.\nFor example, if using the `zsh` shell, add the following line to your `~/.zshrc`\n```sh\nexport PATH=\"$HOME/.swiftpm/bin:$PATH\"\n```\nMake sure to reload the path via (`source ~/.zshrc`) or by restarting your terminal emulator.\n\nThen, to install the `PIRProcessDatabase`, executable, e.g., run\n```sh\nswift package experimental-install -c release --product PIRProcessDatabase\n```\n\n### Testing\nRun unit tests via\n```sh\nswift test -c release\n```\nTo run tests in debug mode, run\n```sh\nswift test\n```\n\u003e [!WARNING]\n\u003e Tests will be slow in debug mode.\n\n### Benchmarking\nSwift homomorphic encryption uses [Benchmark](https://github.com/ordo-one/package-benchmark) for benchmarking.\nTo enable benchmarking, set the environment variable `SWIFT_HOMOMORPHIC_ENCRYPTION_ENABLE_BENCHMARKING=1`.\nBy default, benchmarking requires the [jemalloc](http://jemalloc.net) dependency.\n\n\u003e [!WARNING]\n\u003e The benchmark may crash intermittently due to a known [issue](https://github.com/ordo-one/package-benchmark/issues/60).\n\u003e For reliable execution, benchmark can be run without `jemalloc` as described [here](https://github.com/ordo-one/package-benchmark/releases/tag/1.2.0).\n\nTwo ways to run the benchmarks are:\n* Xcode\n  * Open the `swift-homomorphic-encryption` folder in Xcode with `SWIFT_HOMOMORPHIC_ENCRYPTION_ENABLE_BENCHMARKING=1` set, e.g.:\n    * `open --env SWIFT_HOMOMORPHIC_ENCRYPTION_ENABLE_BENCHMARKING=1 Package.swift`\n  * Switch to a benchmark target.\n  * Run the target, e.g., via the `Product` menu.\n* Command line\n  * Run `SWIFT_HOMOMORPHIC_ENCRYPTION_ENABLE_BENCHMARKING=1 swift package benchmark`.\n  * See the [Benchmark](https://github.com/ordo-one/package-benchmark) documentation for more information on running benchmarks.\n\n### Contributing\nIf you are interested in making a contribution to Swift Homomorphic Encryption, see our [contributing guide](CONTRIBUTING.md).\n\n# Documentation\nSwift Homomorphic Encryption uses DocC for documentation.\nFor more information, refer to [the DocC documentation](https://www.swift.org/documentation/docc) and the [Swift-DocC Plugin](https://swiftlang.github.io/swift-docc-plugin/documentation/swiftdoccplugin/).\n\n## Xcode\nThe documentation can be built from Xcode via `Product -\u003e Build Documentation`.\n\n## Command line\nThe documentation can be built from command line by running\n```sh\nswift package generate-documentation\n```\nand previewed by running\n```sh\nswift package --disable-sandbox preview-documentation --target HomomorphicEncryption\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapple%2Fswift-homomorphic-encryption","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapple%2Fswift-homomorphic-encryption","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapple%2Fswift-homomorphic-encryption/lists"}