Skip to content

Commit 7ff2740

Browse files
authored
Add ApplicationProtobuf (#233)
1 parent 9280def commit 7ff2740

File tree

31 files changed

+5910
-13
lines changed

31 files changed

+5910
-13
lines changed

Package.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ let package = Package(
5555
.library(
5656
name: "PrivateNearestNeighborSearchProtobuf",
5757
targets: ["PrivateNearestNeighborSearchProtobuf"]),
58+
.library(name: "ApplicationProtobuf", targets: ["ApplicationProtobuf"]),
5859
.library(name: "_TestUtilities", targets: ["_TestUtilities"]),
5960
.executable(name: "PIRGenerateDatabase", targets: ["PIRGenerateDatabase"]),
6061
.executable(name: "PIRProcessDatabase", targets: ["PIRProcessDatabase"]),
@@ -132,6 +133,14 @@ let package = Package(
132133
.product(name: "SwiftProtobuf", package: "swift-protobuf")],
133134
exclude: ["generated/README.md", "protobuf_module_mappings.txtpb"],
134135
swiftSettings: librarySettings),
136+
.target(
137+
name: "ApplicationProtobuf",
138+
dependencies: ["HomomorphicEncryptionProtobuf",
139+
"PrivateInformationRetrieval",
140+
"PrivateNearestNeighborSearch",
141+
.product(name: "SwiftProtobuf", package: "swift-protobuf")],
142+
exclude: ["generated/README.md", "protobuf_module_mappings.txtpb"],
143+
swiftSettings: librarySettings),
135144
.target(
136145
name: "_TestUtilities",
137146
dependencies: [
@@ -238,6 +247,15 @@ let package = Package(
238247
"PrivateNearestNeighborSearch",
239248
"PrivateNearestNeighborSearchProtobuf",
240249
], swiftSettings: executableSettings),
250+
.testTarget(
251+
name: "ApplicationProtobufTests",
252+
dependencies: [
253+
"ApplicationProtobuf",
254+
"PrivateNearestNeighborSearch",
255+
"PrivateInformationRetrieval",
256+
"_TestUtilities",
257+
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
258+
], swiftSettings: executableSettings),
241259
])
242260

243261
// MARK: - Benchmarks
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ``ApplicationProtobuf``
2+
3+
Protocol buffer support for HE applications, including `PrivateInformationRetrieval` and `PrivateNearestNeighborSearch`
4+
5+
## Overview
6+
`ApplicationProtobuf` contains supports for using [PrivateInformationRetrieval](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privateinformationretrieval) and [PrivateNearestNeighborSearch](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborsearch) with [protocol buffers](https://protobuf.dev/), commonly referred to as `protobuf`.
7+
This module contains:
8+
* the generated Swift code to use the protobuf types.
9+
* conversion between protobuf types and [PrivateInformationRetrieval](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privateinformationretrieval) runtime types.
10+
* conversion between protobuf types and [PrivateNearestNeighborSearch](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborsearch) runtime types.
11+
12+
The protobuf schemas are defined at [https://github.com/apple/swift-homomorphic-encryption-protobuf](https://github.com/apple/swift-homomorphic-encryption-protobuf).
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
import SwiftProtobuf
17+
18+
/// Error type when converting between protobuf and native objects.
19+
public enum ConversionError: Error {
20+
case unrecognizedEnumValue(enum: any Enum.Type, value: Int)
21+
case unsetField(field: String, message: any Message.Type)
22+
case unsetOneof(oneof: any Message.Type, field: String)
23+
case unspecifiedEnumValue(enum: any Enum.Type)
24+
}
25+
26+
extension ConversionError {
27+
static func unsetOneof(oneof: any Message.Type, field: AnyKeyPath) -> Self {
28+
.unsetOneof(oneof: oneof, field: String(reflecting: field))
29+
}
30+
31+
static func unsetField(_ field: AnyKeyPath, in message: any Message.Type) -> Self {
32+
.unsetField(field: String(reflecting: field), message: message)
33+
}
34+
}
35+
36+
extension ConversionError: LocalizedError {
37+
public var errorDescription: String? {
38+
switch self {
39+
case let .unrecognizedEnumValue(enum: enumeration, value):
40+
"Unrecognized value \(value) in enum \(enumeration)"
41+
case let .unsetField(field, message):
42+
"Unset field \(field) in message \(message)"
43+
case let .unsetOneof(oneof, field):
44+
"Unset oneof in message \(oneof) for field \(field)"
45+
case let .unspecifiedEnumValue(enum: enumeration):
46+
"Unspecified value for enum \(enumeration)"
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)