Skip to content
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
18 changes: 18 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ let package = Package(
.library(
name: "PrivateNearestNeighborSearchProtobuf",
targets: ["PrivateNearestNeighborSearchProtobuf"]),
.library(name: "ApplicationProtobuf", targets: ["ApplicationProtobuf"]),
.library(name: "_TestUtilities", targets: ["_TestUtilities"]),
.executable(name: "PIRGenerateDatabase", targets: ["PIRGenerateDatabase"]),
.executable(name: "PIRProcessDatabase", targets: ["PIRProcessDatabase"]),
Expand Down Expand Up @@ -132,6 +133,14 @@ let package = Package(
.product(name: "SwiftProtobuf", package: "swift-protobuf")],
exclude: ["generated/README.md", "protobuf_module_mappings.txtpb"],
swiftSettings: librarySettings),
.target(
name: "ApplicationProtobuf",
dependencies: ["HomomorphicEncryptionProtobuf",
"PrivateInformationRetrieval",
"PrivateNearestNeighborSearch",
.product(name: "SwiftProtobuf", package: "swift-protobuf")],
exclude: ["generated/README.md", "protobuf_module_mappings.txtpb"],
swiftSettings: librarySettings),
.target(
name: "_TestUtilities",
dependencies: [
Expand Down Expand Up @@ -238,6 +247,15 @@ let package = Package(
"PrivateNearestNeighborSearch",
"PrivateNearestNeighborSearchProtobuf",
], swiftSettings: executableSettings),
.testTarget(
name: "ApplicationProtobufTests",
dependencies: [
"ApplicationProtobuf",
"PrivateNearestNeighborSearch",
"PrivateInformationRetrieval",
"_TestUtilities",
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
], swiftSettings: executableSettings),
])

// MARK: - Benchmarks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ``ApplicationProtobuf``

Protocol buffer support for HE applications, including `PrivateInformationRetrieval` and `PrivateNearestNeighborSearch`

## Overview
`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`.
This module contains:
* the generated Swift code to use the protobuf types.
* conversion between protobuf types and [PrivateInformationRetrieval](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privateinformationretrieval) runtime types.
* conversion between protobuf types and [PrivateNearestNeighborSearch](https://swiftpackageindex.com/apple/swift-homomorphic-encryption/main/documentation/privatenearestneighborsearch) runtime types.

The protobuf schemas are defined at [https://github.com/apple/swift-homomorphic-encryption-protobuf](https://github.com/apple/swift-homomorphic-encryption-protobuf).
49 changes: 49 additions & 0 deletions Sources/ApplicationProtobuf/ConversionError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024-2025 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 Foundation
import SwiftProtobuf

/// Error type when converting between protobuf and native objects.
public enum ConversionError: Error {
case unrecognizedEnumValue(enum: any Enum.Type, value: Int)
case unsetField(field: String, message: any Message.Type)
case unsetOneof(oneof: any Message.Type, field: String)
case unspecifiedEnumValue(enum: any Enum.Type)
}

extension ConversionError {
static func unsetOneof(oneof: any Message.Type, field: AnyKeyPath) -> Self {
.unsetOneof(oneof: oneof, field: String(reflecting: field))
}

static func unsetField(_ field: AnyKeyPath, in message: any Message.Type) -> Self {
.unsetField(field: String(reflecting: field), message: message)
}
}

extension ConversionError: LocalizedError {
public var errorDescription: String? {
switch self {
case let .unrecognizedEnumValue(enum: enumeration, value):
"Unrecognized value \(value) in enum \(enumeration)"
case let .unsetField(field, message):
"Unset field \(field) in message \(message)"
case let .unsetOneof(oneof, field):
"Unset oneof in message \(oneof) for field \(field)"
case let .unspecifiedEnumValue(enum: enumeration):
"Unspecified value for enum \(enumeration)"
}
}
}
Loading
Loading