-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds PrivateNearestNeighborsSearchProtobuf with initial protos. (#69)
- Loading branch information
Showing
32 changed files
with
2,065 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import HomomorphicEncryption | ||
|
||
/// Metric for distances between vectors. | ||
public enum DistanceMetric: CaseIterable, Codable, Equatable, Hashable, Sendable { | ||
/// Cosine similarity. | ||
case cosineSimilarity | ||
} | ||
|
||
/// Client configuration. | ||
public struct ClientConfig<Scheme: HeScheme>: Codable, Equatable, Hashable, Sendable { | ||
/// Encryption parameters. | ||
public let encryptionParams: EncryptionParameters<Scheme> | ||
/// Factor by which to scale floating-point entries before rounding to integers. | ||
public let scalingFactor: Int | ||
/// Packing for the query. | ||
public let queryPacking: MatrixPacking | ||
/// Number of entries in each vector vector. | ||
public let vectorDimension: Int | ||
/// Evaluation key configuration for nearest neighbors computation. | ||
public let evaluationKeyConfig: EvaluationKeyConfiguration | ||
/// Metric for distances between vectors. | ||
public let distanceMetric: DistanceMetric | ||
/// For plaintext CRT, the list of extra plaintext moduli. | ||
/// | ||
/// The first plaintext modulus will be the one in ``ClientConfig/encryptionParams``. | ||
public let extraPlaintextModuli: [Scheme.Scalar] | ||
|
||
/// Creates a new ``ClientConfig``. | ||
/// - Parameters: | ||
/// - encryptionParams: Encryption parameters. | ||
/// - 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 vector. | ||
/// - evaluationKeyConfig: Evaluation key configuration for nearest neighbors computation. | ||
/// - distanceMetric: Metric for nearest neighbors computation | ||
/// - extraPlaintextModuli: For plaintext CRT, the list of extra plaintext moduli. The first plaintext modulus | ||
/// will be the one in ``ClientConfig/encryptionParams``. | ||
public init( | ||
encryptionParams: EncryptionParameters<Scheme>, | ||
scalingFactor: Int, | ||
queryPacking: MatrixPacking, | ||
vectorDimension: Int, | ||
evaluationKeyConfig: EvaluationKeyConfiguration, | ||
distanceMetric: DistanceMetric, | ||
extraPlaintextModuli: [Scheme.Scalar] = []) | ||
{ | ||
self.encryptionParams = encryptionParams | ||
self.scalingFactor = scalingFactor | ||
self.queryPacking = queryPacking | ||
self.vectorDimension = vectorDimension | ||
self.evaluationKeyConfig = evaluationKeyConfig | ||
self.distanceMetric = distanceMetric | ||
self.extraPlaintextModuli = extraPlaintextModuli | ||
} | ||
} | ||
|
||
/// Server configuration. | ||
public struct ServerConfig<Scheme: HeScheme>: Codable, Equatable, Hashable, Sendable { | ||
/// Configuration shared with the client. | ||
public let clientConfig: ClientConfig<Scheme> | ||
/// Packing for the plaintext database. | ||
public let databasePacking: MatrixPacking | ||
|
||
/// Creates a new ``ServerConfig``. | ||
/// - Parameters: | ||
/// - clientConfig: Configuration shared with the client. | ||
/// - databasePacking: Packing for the plaintext database. | ||
public init( | ||
clientConfig: ClientConfig<Scheme>, | ||
databasePacking: MatrixPacking) | ||
{ | ||
self.clientConfig = clientConfig | ||
self.databasePacking = databasePacking | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/// One row in a nearest-neighbor search database. | ||
public struct DatabaseRow: Codable, Equatable, Hashable, Sendable { | ||
/// Unique identifier for the database entry. | ||
public let entryID: UInt64 | ||
|
||
/// Metadata associated with the entry. | ||
public let entryMetadata: [UInt8] | ||
|
||
/// Vector for use in nearest neighbors computation. | ||
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 | ||
public init(entryID: UInt64, entryMetadata: [UInt8], vector: [Float]) { | ||
self.entryID = entryID | ||
self.entryMetadata = entryMetadata | ||
self.vector = vector | ||
} | ||
} | ||
|
||
/// Database for nearest-neighbor search. | ||
public struct Database: Codable, Equatable, Hashable, Sendable { | ||
/// Rows in the database. | ||
public let rows: [DatabaseRow] | ||
|
||
/// Creates a new ``Database``. | ||
/// - Parameter rows: Rows in the database. | ||
public init(rows: [DatabaseRow]) { | ||
self.rows = rows | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.