forked from apple/swift-homomorphic-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessDatabase.swift
More file actions
322 lines (292 loc) · 13.7 KB
/
ProcessDatabase.swift
File metadata and controls
322 lines (292 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2024-2026 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 ApplicationProtobuf
import ArgumentParser
import Foundation
import HomomorphicEncryption
import Logging
import PrivateNearestNeighborSearch
/// Creates a new `Database` from a given path.
/// - Parameter path: The path to the `Database` file.
/// - Throws: Error upon failure to load the database.
extension Database {
init(from path: String) throws {
let database = try Apple_SwiftHomomorphicEncryption_Pnns_V1_Database(from: path)
self = database.native()
}
}
extension String {
/// Performs validation on proto file name.
/// - Parameter descriptor: The proto file name to validate.
/// - Throws: Error upon invalid file name.
func validateProtoFilename(descriptor: String) throws {
guard hasSuffix(".txtpb") || hasSuffix(".binpb") else {
throw ValidationError("'\(descriptor)' must contain have extension '.txtpb' or '.binpb', found \(self)")
}
}
}
/// A struct that represents the database processing arguments.
struct Arguments: Codable, Equatable, Hashable, Sendable {
/// The default arguments.
static let defaultArguments = Arguments(
inputDatabase: "/path/to/input/database.txtpb",
outputDatabase: "/path/to/output/database.binpb",
outputServerConfig: "path/to/output/server-config.txtpb",
rlweParameters: .n_8192_logq_3x55_logt_30,
extraPlaintextModuli: nil,
distanceMetric: .cosineSimilarity,
batchSize: 1,
scalingFactor: nil,
databasePacking: nil,
queryPacking: nil,
trials: 1,
trialDistanceTolerance: 0.01)
let inputDatabase: String
let outputDatabase: String
let outputServerConfig: String?
let rlweParameters: PredefinedRlweParameters
let extraPlaintextModuli: [UInt64]?
let distanceMetric: DistanceMetric?
let batchSize: Int?
let scalingFactor: Int?
let databasePacking: MatrixPacking?
let queryPacking: MatrixPacking?
let trials: Int?
let trialDistanceTolerance: Float?
static func defaultJsonString(vectorDimension: Int) -> String {
// swiftlint:disable:next force_try
let resolved: ResolvedArguments = try! defaultArguments.resolve(for: vectorDimension, scheme: Bfv<UInt64>.self)
let defaultArguments = Arguments(
inputDatabase: resolved.inputDatabase,
outputDatabase: resolved.outputDatabase,
outputServerConfig: resolved.outputServerConfig,
rlweParameters: resolved.rlweParameters,
extraPlaintextModuli: resolved.extraPlaintextModuli,
distanceMetric: resolved.distanceMetric,
batchSize: resolved.batchSize,
scalingFactor: resolved.scalingFactor,
databasePacking: resolved.databasePacking,
queryPacking: resolved.queryPacking,
trials: resolved.trials,
trialDistanceTolerance: resolved.trialDistanceTolerance)
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
// swiftlint:disable:next force_try
let data = try! encoder.encode(defaultArguments)
// swiftlint:disable:next optional_data_string_conversion
return String(decoding: data, as: UTF8.self)
}
func resolve<Scheme: HeScheme>(for vectorDimension: Int, scheme _: Scheme.Type) throws -> ResolvedArguments {
let distanceMetric = distanceMetric ?? .cosineSimilarity
let databasePacking = databasePacking ?? .diagonal(
babyStepGiantStep: BabyStepGiantStep(
vectorDimension: vectorDimension))
let queryPacking = queryPacking ?? .denseRow
let plaintextModuli = [rlweParameters.plaintextModulus] + (extraPlaintextModuli ?? [])
let scalingFactor = scalingFactor ?? ClientConfig<Scheme>.maxScalingFactor(
distanceMetric: distanceMetric,
vectorDimension: vectorDimension,
plaintextModuli: plaintextModuli.map { Scheme.Scalar($0) })
return try ResolvedArguments(
inputDatabase: inputDatabase,
outputDatabase: outputDatabase,
outputServerConfig: outputServerConfig,
rlweParameters: rlweParameters,
extraPlaintextModuli: extraPlaintextModuli ?? [],
distanceMetric: distanceMetric,
batchSize: batchSize ?? 1,
scalingFactor: scalingFactor,
databasePacking: databasePacking,
queryPacking: queryPacking,
trials: trials ?? 1,
trialDistanceTolerance: trialDistanceTolerance ?? 0.01)
}
}
/// The resolved arguments for the database processing.
struct ResolvedArguments: CustomStringConvertible, Encodable {
let inputDatabase: String
let outputDatabase: String
let outputServerConfig: String?
let rlweParameters: PredefinedRlweParameters
let extraPlaintextModuli: [UInt64]
let distanceMetric: DistanceMetric
let batchSize: Int
let scalingFactor: Int
let databasePacking: MatrixPacking
let queryPacking: MatrixPacking
let trials: Int
let trialDistanceTolerance: Float
var description: String {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
// swiftlint:disable:next force_try
let data = try! encoder.encode(self)
// swiftlint:disable:next optional_data_string_conversion
return String(decoding: data, as: UTF8.self)
}
/// - Parameters:
/// - inputDatabase: Path to the input database.
/// - outputDatabase: Path to save the processed database.
/// - outputServerConfig: Path to save the server configuration.
/// - trials: Number of test queries .
init(
inputDatabase: String,
outputDatabase: String,
outputServerConfig: String?,
rlweParameters: PredefinedRlweParameters,
extraPlaintextModuli: [UInt64],
distanceMetric: DistanceMetric,
batchSize: Int,
scalingFactor: Int,
databasePacking: MatrixPacking,
queryPacking: MatrixPacking,
trials: Int,
trialDistanceTolerance: Float) throws
{
self.inputDatabase = inputDatabase
self.outputDatabase = outputDatabase
self.outputServerConfig = outputServerConfig
self.rlweParameters = rlweParameters
self.extraPlaintextModuli = extraPlaintextModuli
self.distanceMetric = distanceMetric
self.batchSize = batchSize
self.scalingFactor = scalingFactor
self.databasePacking = databasePacking
self.queryPacking = queryPacking
self.trials = trials
self.trialDistanceTolerance = trialDistanceTolerance
}
}
@main
struct ProcessDatabase: AsyncParsableCommand {
static let configuration: CommandConfiguration = .init(
commandName: "PNNSProcessDatabase", version: Version.current.description)
static let logger = Logger(label: "PNNSProcessDatabase")
@Argument(
help: """
Path to json configuration file.
Default for
- \(Arguments.defaultArguments.rlweParameters),
- vectorDimension: 128
\(Arguments.defaultJsonString(vectorDimension: 128))
""")
var configFile: String
/// Performs the processing on the given database.
/// - Parameters:
/// - config: The configuration for the PNNS processing.
/// - scheme: The HE scheme.
/// - Throws: Error upon processing the database.
@inlinable
mutating func process<Scheme: HeScheme>(config: Arguments, scheme _: Scheme.Type) async throws {
let database = try Database(from: config.inputDatabase)
guard let vectorDimension = database.rows.first?.vector.count else {
throw PnnsError.emptyDatabase
}
let plaintextMatrixDimensions = try MatrixDimensions(
rowCount: database.rows.count,
columnCount: vectorDimension)
let config: ResolvedArguments = try config.resolve(for: vectorDimension, scheme: Scheme.self)
ProcessDatabase.logger.info("Processing database with configuration: \(config)")
let encryptionParameters = try EncryptionParameters<Scheme.Scalar>(from: config.rlweParameters)
let clientConfig = try ClientConfig<Scheme>(
encryptionParameters: encryptionParameters,
scalingFactor: config.scalingFactor,
queryPacking: config.queryPacking,
vectorDimension: vectorDimension,
evaluationKeyConfig: MatrixMultiplication.evaluationKeyConfig(
plaintextMatrixDimensions: plaintextMatrixDimensions,
maxQueryCount: config.batchSize,
encryptionParameters: encryptionParameters,
scheme: Scheme.self),
distanceMetric: config.distanceMetric,
extraPlaintextModuli: config.extraPlaintextModuli.map { Scheme.Scalar($0) })
let serverConfig = ServerConfig<Scheme>(
clientConfig: clientConfig,
databasePacking: config.databasePacking)
let processed = try await database.process(config: serverConfig)
ProcessDatabase.logger.info("Processed database")
if config.trials > 0 {
var queryRows = Array2d(data: database.rows.prefix(config.batchSize).map { row in row.vector })
queryRows.append(rows: Array(
repeating: 0,
count: vectorDimension * (config.batchSize - queryRows.rowCount)))
ProcessDatabase.logger.info("Validating")
let validationResult = try await processed.validate(query: queryRows, trials: config.trials)
for row in 0..<min(database.rows.count, config.batchSize) {
let selfProduct = validationResult.databaseDistances.distances.row(row)[row]
let error = abs(selfProduct - 1.0)
guard error <= config.trialDistanceTolerance else {
ProcessDatabase.logger
.error("Result error \(error) exceeds tolerance \(config.trialDistanceTolerance)")
throw ValidationError("Result error \(error) exceeds tolerance \(config.trialDistanceTolerance)")
}
}
let description = try validationResult.description()
ProcessDatabase.logger.info("ValidationResult \(description)")
}
try processed.serialize().proto().save(to: config.outputDatabase)
ProcessDatabase.logger.info("Saved processed database to \(config.outputDatabase)")
if let serverConfigFile = config.outputServerConfig {
let protoServerConfig = try processed.serverConfig.proto()
try protoServerConfig.save(to: serverConfigFile)
ProcessDatabase.logger.info("Saved server configuration to \(serverConfigFile)")
}
}
mutating func run() async throws {
let configURL = URL(fileURLWithPath: configFile)
let configData = try Data(contentsOf: configURL)
let config = try JSONDecoder().decode(Arguments.self, from: configData)
if config.rlweParameters.supportsScalar(UInt32.self) {
try await process(config: config, scheme: Bfv<UInt32>.self)
} else {
try await process(config: config, scheme: Bfv<UInt64>.self)
}
}
}
extension ValidationResult {
/// Returns a description of processed database validation.
func description() throws -> String {
func sizeString(byteCount: Int, count: Int, label: String) -> String {
let sizeKB = String(format: "%.01f", Double(byteCount) / 1000.0)
return "\(sizeKB) KB (\(count) \(label))"
}
var descriptionDict = [String: String]()
let queryCount: Int = query.ciphertextMatrices.map { matrix in matrix.ciphertexts.count }.sum()
descriptionDict["query size"] = try sizeString(byteCount: query.size(), count: queryCount, label: "ciphertexts")
descriptionDict["evaluation key size"] = try sizeString(
byteCount: evaluationKey.size(),
count: evaluationKey.config.keyCount,
label: "keys")
let responseCount: Int = response.ciphertextMatrices.map { matrix in matrix.ciphertexts.count }.sum()
descriptionDict["response size"] = try sizeString(byteCount: response.size(), count: responseCount,
label: "ciphertexts")
descriptionDict["noise budget"] = String(format: "%.01f", noiseBudget)
let runtimeString = computeTimes.sorted().map { runtime in
String(format: "%.01f", runtime.milliseconds)
}.joined(separator: ", ")
descriptionDict["runtime (ms)"] = "[\(runtimeString)]"
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
// swiftlint:disable:next force_try
let data = try! encoder.encode(descriptionDict)
// swiftlint:disable:next optional_data_string_conversion
let description = String(decoding: data, as: UTF8.self)
return description.replacingOccurrences(of: "\"", with: "")
}
}
extension Duration {
var milliseconds: Double {
Double(components.seconds) * 1e3 + Double(components.attoseconds) * 1e-15
}
}