Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neighbors -> Neighbor in PNNS #115

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ repos:
- //
- --allow-past-years
- --use-current-year
- --detect-license-in-X-top-lines=10
- --detect-license-in-X-top-lines=11
- id: insert-license
name: insert-license-yaml
'types_or': [yaml]
Expand Down
4 changes: 2 additions & 2 deletions .spi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ builder:
- PNNSProcessDatabase
- PrivateInformationRetrieval
- PrivateInformationRetrievalProtobuf
- PrivateNearestNeighborsSearch
- PrivateNearestNeighborsSearchProtobuf
- PrivateNearestNeighborSearch
- PrivateNearestNeighborSearchProtobuf
swift_version: 6.0
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import Benchmark
import Foundation
import HomomorphicEncryption
import HomomorphicEncryptionProtobuf
import PrivateNearestNeighborsSearch
import PrivateNearestNeighborsSearchProtobuf
import PrivateNearestNeighborSearch
import PrivateNearestNeighborSearchProtobuf

@usableFromInline nonisolated(unsafe) let benchmarkConfiguration = Benchmark.Configuration(
metrics: [
Expand Down Expand Up @@ -93,7 +93,7 @@ extension EncryptionParameters {

let noiseBudgetScale = 10

extension PrivateNearestNeighborsSearch.Response {
extension PrivateNearestNeighborSearch.Response {
func scaledNoiseBudget(using secretKey: Scheme.SecretKey) throws -> Int {
try Int(
noiseBudget(using: secretKey, variableTime: true) * Double(
Expand Down
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "924e29f4ba35b567344548cae01b03bc8f2c3a884de7a16bcd7dc8475e890dfb",
"originHash" : "35058120a76c9add338a72688983f31eb5403042763150a08ca6e53652580567",
"pins" : [
{
"identity" : "hdrhistogram-swift",
Expand Down Expand Up @@ -105,8 +105,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-protobuf",
"state" : {
"revision" : "d57a5aecf24a25b32ec4a74be2f5d0a995a47c4b",
"version" : "1.27.0"
"revision" : "edb6ed4919f7756157fe02f2552b7e3850a538e5",
"version" : "1.28.1"
}
},
{
Expand Down
36 changes: 18 additions & 18 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ let package = Package(
name: "PrivateInformationRetrievalProtobuf",
targets: ["PrivateInformationRetrievalProtobuf"]),
.library(
name: "PrivateNearestNeighborsSearch",
targets: ["PrivateNearestNeighborsSearch"]),
name: "PrivateNearestNeighborSearch",
targets: ["PrivateNearestNeighborSearch"]),
.library(
name: "PrivateNearestNeighborsSearchProtobuf",
targets: ["PrivateNearestNeighborsSearchProtobuf"]),
name: "PrivateNearestNeighborSearchProtobuf",
targets: ["PrivateNearestNeighborSearchProtobuf"]),
.executable(name: "PIRGenerateDatabase", targets: ["PIRGenerateDatabase"]),
.executable(name: "PIRProcessDatabase", targets: ["PIRProcessDatabase"]),
.executable(name: "PIRShardDatabase", targets: ["PIRShardDatabase"]),
Expand All @@ -59,7 +59,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-crypto.git", from: "3.4.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-protobuf", from: "1.27.0"), // Keep version in sync with README
.package(url: "https://github.com/apple/swift-protobuf", from: "1.28.1"), // Keep version in sync with README
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.1.0"),
],
targets: [
Expand Down Expand Up @@ -99,15 +99,15 @@ let package = Package(
exclude: ["generated/README.md", "protobuf_module_mappings.txtpb"],
swiftSettings: librarySettings),
.target(
name: "PrivateNearestNeighborsSearch",
name: "PrivateNearestNeighborSearch",
dependencies: [
.product(name: "Algorithms", package: "swift-algorithms"),
"HomomorphicEncryption",
],
swiftSettings: librarySettings),
.target(
name: "PrivateNearestNeighborsSearchProtobuf",
dependencies: ["PrivateNearestNeighborsSearch",
name: "PrivateNearestNeighborSearchProtobuf",
dependencies: ["PrivateNearestNeighborSearch",
"HomomorphicEncryption",
"HomomorphicEncryptionProtobuf",
.product(name: "SwiftProtobuf", package: "swift-protobuf")],
Expand Down Expand Up @@ -151,15 +151,15 @@ let package = Package(
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"HomomorphicEncryption",
"PrivateNearestNeighborsSearchProtobuf",
"PrivateNearestNeighborSearchProtobuf",
],
swiftSettings: executableSettings),
.executableTarget(
name: "PNNSProcessDatabase",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"HomomorphicEncryptionProtobuf",
"PrivateNearestNeighborsSearchProtobuf",
"PrivateNearestNeighborSearchProtobuf",
"HomomorphicEncryption",
.product(name: "Logging", package: "swift-log"),
],
Expand Down Expand Up @@ -201,15 +201,15 @@ let package = Package(
"TestUtilities",
], swiftSettings: executableSettings),
.testTarget(
name: "PrivateNearestNeighborsSearchTests",
name: "PrivateNearestNeighborSearchTests",
dependencies: [
"PrivateNearestNeighborsSearch", "HomomorphicEncryption", "TestUtilities",
"PrivateNearestNeighborSearch", "HomomorphicEncryption", "TestUtilities",
], swiftSettings: executableSettings),
.testTarget(
name: "PrivateNearestNeighborsSearchProtobufTests",
name: "PrivateNearestNeighborSearchProtobufTests",
dependencies: [
"PrivateNearestNeighborsSearch",
"PrivateNearestNeighborsSearchProtobuf",
"PrivateNearestNeighborSearch",
"PrivateNearestNeighborSearchProtobuf",
], swiftSettings: executableSettings),
])

Expand Down Expand Up @@ -261,10 +261,10 @@ package.targets += [
.product(name: "Benchmark", package: "package-benchmark"),
"HomomorphicEncryption",
"HomomorphicEncryptionProtobuf",
"PrivateNearestNeighborsSearch",
"PrivateNearestNeighborsSearchProtobuf",
"PrivateNearestNeighborSearch",
"PrivateNearestNeighborSearchProtobuf",
],
path: "Benchmarks/PrivateNearestNeighborsSearchBenchmark",
path: "Benchmarks/PrivateNearestNeighborSearchBenchmark",
swiftSettings: benchmarkSettings,
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark"),
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ For more information, refer to documentation for the libraries:
* [HomomorphicEncryption](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/homomorphicencryption)
* [PrivateInformationRetrievalProtobuf](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privateinformationretrievalprotobuf)
* [PrivateInformationRetrieval](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privateinformationretrieval)
* [PrivateNearestNeighborsSearchProtobuf](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborssearchprotobuf)
* [PrivateNearestNeighborsSearch](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborssearch)
* [PrivateNearestNeighborSearchProtobuf](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborsearchprotobuf)
* [PrivateNearestNeighborSearch](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborsearch)

and executables:
* [PIRGenerateDatabase](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/pirgeneratedatabase)
Expand Down Expand Up @@ -63,11 +63,11 @@ The PIR implementation in Swift Homomorphic Encryption uses HE to improve upon t
> 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.

### Private Nearest Neighbors Search (PNNS)
Private nearest neighbors 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.*.
### Private Nearest Neighbor Search (PNNS)
Private 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.*.
Each row in the database is a *vector* with an associated *entry identifier* and *entry metadata*.
During the PNNS protocol, the client issues a query using its private vector, and learns the nearest neighbors according to a ``DistanceMetric``.
Specifically, the client learns the distances between the client's query vector to the nearest neighbors, as well as the entry identifier and entry metadata of the nearest neighbors.
During the PNNS protocol, the client issues a query using its private vector, and learns the nearest neighbor according to a ``DistanceMetric``.
Specifically, 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.

A trivial implementation of PNNS is to have the client issue a generic "fetch database" request, independent of its private vector.
Then the server sends the entire database to the client, who computes the distances locally.
Expand Down Expand Up @@ -141,7 +141,7 @@ Additionally, developing Swift Homomorphic Encryption requires:
* [Nick Lockwood SwiftFormat](https://github.com/nicklockwood/SwiftFormat), v0.54.0
* [pre-commit](https://pre-commit.com)
* [swift-format](https://github.com/apple/swift-format), v510.1.0
* [swift-protobuf](https://github.com/apple/swift-protobuf), v1.27.0
* [swift-protobuf](https://github.com/apple/swift-protobuf), v1.28.1
* [SwiftLint](https://github.com/realm/SwiftLint), v0.55.1

### Building
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ For more information, refer to documentation for the libraries:
* [HomomorphicEncryption](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/homomorphicencryption)
* [PrivateInformationRetrievalProtobuf](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privateinformationretrievalprotobuf)
* [PrivateInformationRetrieval](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privateinformationretrieval)
* [PrivateNearestNeighborsSearchProtobuf](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborssearchprotobuf)
* [PrivateNearestNeighborsSearch](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborssearch)
* [PrivateNearestNeighborSearchProtobuf](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborsearchprotobuf)
* [PrivateNearestNeighborSearch](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborsearch)

and executables:
* [PIRGenerateDatabase](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/pirgeneratedatabase)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: apple/swift_homomorphic_encryption/v1/error_stddev.proto
Expand All @@ -21,7 +22,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import SwiftProtobuf

// If the compiler emits an error on this type, it is because this file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: apple/swift_homomorphic_encryption/v1/he.proto
Expand Down
4 changes: 2 additions & 2 deletions Sources/PNNSGenerateDatabase/GenerateDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import ArgumentParser
import Foundation
import HomomorphicEncryption
import PrivateNearestNeighborsSearch
import PrivateNearestNeighborsSearchProtobuf
import PrivateNearestNeighborSearch
import PrivateNearestNeighborSearchProtobuf

enum VectorTypeArguments: String, CaseIterable, ExpressibleByArgument {
/// Each vector's entry is uniform random from `[-1.0, 1.0]`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ``PNNSGenerateDatabase``

Private Nearest Neighbors Search database generation
Private Nearest Neighbor Search database generation

## Overview

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ``PNNSProcessDatabase``

Private Nearest Neighbors Search database processing
Private Nearest Neighbor Search database processing

## Overview
PNNS database processing will transform a database in preparation for hosting PNNS queries.
Expand Down
4 changes: 2 additions & 2 deletions Sources/PNNSProcessDatabase/ProcessDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import ArgumentParser
import Foundation
import HomomorphicEncryption
import Logging
import PrivateNearestNeighborsSearch
import PrivateNearestNeighborsSearchProtobuf
import PrivateNearestNeighborSearch
import PrivateNearestNeighborSearchProtobuf

/// Creates a new `Database` from a given path.
/// - Parameter path: The path to the `Database` file.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: apple/swift_homomorphic_encryption/api/pir/v1/api.proto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: apple/swift_homomorphic_encryption/api/pir/v1/pir.proto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: apple/swift_homomorphic_encryption/api/shared/v1/api_shared.proto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: apple/swift_homomorphic_encryption/pir/v1/pir.proto
Expand All @@ -21,7 +22,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import SwiftProtobuf

import HomomorphicEncryptionProtobuf
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: apple/swift_homomorphic_encryption/pir/v1/pir_algorithm.proto
Expand All @@ -21,7 +22,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import SwiftProtobuf

// If the compiler emits an error on this type, it is because this file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// DO NOT EDIT.
// swift-format-ignore-file
// swiftlint:disable all
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: apple/swift_homomorphic_encryption/pir/v1/pir_database.proto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import Foundation
import HomomorphicEncryption

/// Private nearest neighbors client.
/// Private nearest neighbor search client.
public struct Client<Scheme: HeScheme> {
/// Configuration.
public let config: ClientConfig<Scheme>
Expand Down Expand Up @@ -87,7 +87,7 @@ public struct Client<Scheme: HeScheme> {
return Query(ciphertextMatrices: matrices)
}

/// Decrypts a nearest neighbors search response.
/// Decrypts a nearest neighbor search response.
/// - Parameters:
/// - response: The response.
/// - secretKey: Secret key to decrypt with.
Expand Down Expand Up @@ -131,7 +131,7 @@ public struct Client<Scheme: HeScheme> {
try contexts[0].generateSecretKey()
}

/// Generates an `EvaluationKey` for use in nearest neighbors search.
/// Generates an `EvaluationKey` for use in nearest neighbor search.
/// - Parameter secretKey: Secret key used to generate the evaluation key.
/// - Returns: The evaluation key.
/// - Throws: Error upon failure to generate the evaluation key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public struct ClientConfig<Scheme: HeScheme>: Codable, Equatable, Hashable, Send
public let queryPacking: MatrixPacking
/// Number of entries in each vector.
public let vectorDimension: Int
/// Evaluation key configuration for nearest neighbors computation.
/// Evaluation key configuration for nearest neighbor search.
public let evaluationKeyConfig: EvaluationKeyConfig
/// Metric for distances between vectors.
public let distanceMetric: DistanceMetric
Expand All @@ -72,8 +72,8 @@ public struct ClientConfig<Scheme: HeScheme>: Codable, Equatable, Hashable, Send
/// - scalingFactor: Factor by which to scale floating-point entries before rounding to integers.
/// - queryPacking: Packing for the query.
/// - vectorDimension: Number of entries in each vector.
/// - evaluationKeyConfig: Evaluation key configuration for nearest neighbors computation.
/// - distanceMetric: Metric for nearest neighbors computation
/// - evaluationKeyConfig: Evaluation key configuration for nearest neighbor search.
/// - distanceMetric: Metric for nearest neighbor search.
/// - extraPlaintextModuli: For plaintext CRT, the list of extra plaintext moduli. The first plaintext modulus
/// will be the one in ``ClientConfig/encryptionParameters``.
/// - Throws: Error upon failure to create a new client config.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ public struct DatabaseRow: Codable, Equatable, Hashable, Sendable {
/// Metadata associated with the entry.
public let entryMetadata: [UInt8]

/// Vector for use in nearest neighbors computation.
/// Vector for use in nearest neighbor search.
public let vector: [Float]

/// Creates a new ``DatabaseRow``.
/// - Parameters:
/// - entryId: Unique identifier for the database entry.
/// - entryMetadata: Metadata associated with the entry.
/// - vector: Vector for use in nearest neighbors computation
/// - vector: Vector for use in nearest neighbor search.
public init(entryId: UInt64, entryMetadata: [UInt8], vector: [Float]) {
self.entryId = entryId
self.entryMetadata = entryMetadata
self.vector = vector
}
}

/// Database for nearest-neighbor search.
/// Database for nearest neighbor search.
public struct Database: Codable, Equatable, Hashable, Sendable {
/// Rows in the database.
public let rows: [DatabaseRow]
Expand Down
Loading
Loading