Skip to content

Commit a85e9d6

Browse files
hamishunngeniseRuiyuZhufboemer
authored
functionality extensions and api changes and tweaks (#243)
Co-authored-by: Nick Genise <ngenise@apple.com> Co-authored-by: Ruiyu Zhu <ruiyu_zhu@apple.com> Co-authored-by: Fabian Boemer <fabian_boemer@apple.com>
1 parent 9d73707 commit a85e9d6

File tree

89 files changed

+2441
-1466
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+2441
-1466
lines changed

.swiftlint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ excluded:
1717
- "**/DoubleWidthUInt.swift"
1818
- "**/.build/"
1919

20+
type_name:
21+
allowed_symbols: "_"
22+
2023
line_length:
2124
warning: 120
2225
ignores_urls: true

Benchmarks/PrivateInformationRetrievalBenchmark/PirBenchmark.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
import _BenchmarkUtilities
2020
import HomomorphicEncryption
21+
import PrivateInformationRetrieval
2122

2223
nonisolated(unsafe) let benchmarks: () -> Void = {
23-
pirProcessBenchmark(Bfv<UInt32>.self)()
24-
pirProcessBenchmark(Bfv<UInt64>.self)()
24+
pirProcessBenchmark(PirUtil<Bfv<UInt32>>.self)()
25+
pirProcessBenchmark(PirUtil<Bfv<UInt64>>.self)()
2526

26-
indexPirBenchmark(Bfv<UInt32>.self)()
27-
indexPirBenchmark(Bfv<UInt64>.self)()
27+
indexPirBenchmark(PirUtil<Bfv<UInt32>>.self)()
28+
indexPirBenchmark(PirUtil<Bfv<UInt64>>.self)()
2829

29-
keywordPirBenchmark(Bfv<UInt32>.self)()
30-
keywordPirBenchmark(Bfv<UInt64>.self)()
30+
keywordPirBenchmark(PirUtil<Bfv<UInt32>>.self)()
31+
keywordPirBenchmark(PirUtil<Bfv<UInt64>>.self)()
3132
}

Benchmarks/RlweBenchmark/RlweBenchmark.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func getRandomPlaintextData<T: ScalarType>(count: Int, in range: Range<T>) -> [T
4343

4444
struct RlweBenchmarkContext<Scheme: HeScheme>: Sendable {
4545
var encryptionParameters: EncryptionParameters<Scheme.Scalar>
46-
var context: Context<Scheme>
46+
var context: Scheme.Context
4747

4848
let data: [Scheme.Scalar]
4949
let signedData: [Scheme.SignedScalar]
@@ -69,7 +69,7 @@ struct RlweBenchmarkContext<Scheme: HeScheme>: Sendable {
6969
Scheme.Scalar.self),
7070
errorStdDev: ErrorStdDev.stdDev32,
7171
securityLevel: SecurityLevel.quantum128)
72-
self.context = try Context(encryptionParameters: encryptionParameters)
72+
self.context = try Scheme.Context(encryptionParameters: encryptionParameters)
7373
self.secretKey = try context.generateSecretKey()
7474
let columnElement = GaloisElement.swappingRows(degree: polyDegree)
7575
let rowElement = try GaloisElement.rotatingColumns(by: rotateColumnsStep, degree: polyDegree)

Package.resolved

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ let executableSettings: [SwiftSetting] =
2727

2828
let benchmarkSettings: [SwiftSetting] = [.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))]
2929

30+
let enableFlags = "SWIFT_HOMOMORPHIC_ENCRYPTION_MODULAR_ARITHMETIC_EXTRA_SWIFT_FLAGS"
31+
func shouldEnableFlags() -> Bool {
32+
if let flag = ProcessInfo.processInfo.environment[enableFlags], flag != "0", flag != "false" {
33+
return true
34+
}
35+
return false
36+
}
37+
38+
var flags: [SwiftSetting] = []
39+
let enableFlagsBool = shouldEnableFlags()
40+
if enableFlagsBool {
41+
print("Building with additional flags. To disable, unset \(enableFlags) in your environment.")
42+
let flagsAsString = (ProcessInfo.processInfo.environment[enableFlags] ?? "") as String
43+
flags += [.unsafeFlags(flagsAsString.components(separatedBy: ","))]
44+
}
45+
3046
let package = Package(
3147
name: "swift-homomorphic-encryption",
3248
products: [
@@ -60,6 +76,7 @@ let package = Package(
6076
dependencies: [
6177
.package(url: "https://github.com/apple/swift-algorithms", from: "1.2.0"),
6278
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.0"),
79+
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.2"),
6380
.package(url: "https://github.com/apple/swift-crypto.git", from: "3.10.0"),
6481
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
6582
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.0"),
@@ -71,7 +88,7 @@ let package = Package(
7188
.target(
7289
name: "ModularArithmetic",
7390
dependencies: [],
74-
swiftSettings: librarySettings),
91+
swiftSettings: librarySettings + flags),
7592
.target(
7693
name: "CUtil",
7794
dependencies: [],
@@ -100,12 +117,14 @@ let package = Package(
100117
.target(
101118
name: "PrivateInformationRetrieval",
102119
dependencies: ["HomomorphicEncryption",
120+
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
103121
.product(name: "Numerics", package: "swift-numerics")],
104122
swiftSettings: librarySettings),
105123
.target(
106124
name: "PrivateNearestNeighborSearch",
107125
dependencies: [
108126
.product(name: "Algorithms", package: "swift-algorithms"),
127+
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
109128
"HomomorphicEncryption",
110129
"_HomomorphicEncryptionExtras",
111130
],

Snippets/HomomorphicEncryption/EncryptionParametersSnippet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func summarize<Scheme: HeScheme>(
8585
parameters: EncryptionParameters<Scheme.Scalar>, _: Scheme.Type) throws
8686
{
8787
let values = (0..<8).map { Scheme.Scalar($0) }
88-
let context = try Context<Scheme>(encryptionParameters: parameters)
88+
let context = try Scheme.Context(encryptionParameters: parameters)
8989
let plaintext: Scheme.CoeffPlaintext = try context.encode(
9090
values: values,
9191
format: .coefficient)

Sources/ApplicationProtobuf/PirConversion.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension Apple_SwiftHomomorphicEncryption_Pir_V1_EncryptedIndices {
2424
/// - Parameter context: Context to associate with the native type.
2525
/// - Returns: The converted native type.
2626
/// - Throws: Error upon invalid protobuf object.
27-
public func native<Scheme: HeScheme>(context: Context<Scheme>) throws -> Query<Scheme> {
27+
public func native<Scheme: HeScheme>(context: Scheme.Context) throws -> Query<Scheme> {
2828
let ciphertexts: [Scheme.CanonicalCiphertext] = try ciphertexts.map { ciphertext in
2929
let serializedCiphertext: SerializedCiphertext<Scheme.Scalar> = try ciphertext.native()
3030
return try Ciphertext(
@@ -54,7 +54,7 @@ extension ProcessedDatabaseWithParameters {
5454
/// - Parameter context: The context that was used to create processed database.
5555
/// - Returns: The PIR parameters protobuf object.
5656
/// - Throws: Error when the parameters cannot be represented as a protobuf object.
57-
public func proto(context: Context<Scheme>) throws -> Apple_SwiftHomomorphicEncryption_Pir_V1_PirParameters {
57+
public func proto(context: Scheme.Context) throws -> Apple_SwiftHomomorphicEncryption_Pir_V1_PirParameters {
5858
let encryptionParameters = context.encryptionParameters
5959
return try Apple_SwiftHomomorphicEncryption_Pir_V1_PirParameters.with { params in
6060
params.encryptionParameters = try encryptionParameters.proto(scheme: Scheme.self)

Sources/ApplicationProtobuf/PirConversionApi.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension Apple_SwiftHomomorphicEncryption_Api_Pir_V1_PIRResponse {
2121
/// - Parameter context: Context to associate with the native type.
2222
/// - Returns: The converted native type.
2323
/// - Throws: Error upon invalid protobuf object.
24-
public func native<Scheme: HeScheme>(context: Context<Scheme>) throws -> Response<Scheme> {
24+
public func native<Scheme: HeScheme>(context: Scheme.Context) throws -> Response<Scheme> {
2525
let ciphertexts: [[Scheme.CoeffCiphertext]] = try replies.map { reply in
2626
let serializedCiphertexts: [SerializedCiphertext<Scheme.Scalar>] = try reply.native()
2727
return try serializedCiphertexts.map { serialized in

Sources/ApplicationProtobuf/PnnsConversion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ extension [Apple_SwiftHomomorphicEncryption_Pnns_V1_SerializedCiphertextMatrix]
315315
/// Converts the native object into a protobuf object.
316316
/// - Returns: The converted protobuf object.
317317
/// - Throws: Error upon unsupported object.
318-
public func native<Scheme: HeScheme>(context: Context<Scheme>) throws -> Query<Scheme> {
318+
public func native<Scheme: HeScheme>(context: Scheme.Context) throws -> Query<Scheme> {
319319
let matrices: [CiphertextMatrix<Scheme, Coeff>] = try map { matrix in
320320
let native: SerializedCiphertextMatrix<Scheme.Scalar> = try matrix.native()
321321
return try CiphertextMatrix(deserialize: native, context: context)

Sources/ApplicationProtobuf/PnnsConversionApi.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension Apple_SwiftHomomorphicEncryption_Api_Pnns_V1_PNNSShardResponse {
2323
/// - Parameter contexts: Contexts to associate with the native type; one context per plaintext modulus.
2424
/// - Returns: The converted native type.
2525
/// - Throws: Error upon invalid protobuf object.
26-
public func native<Scheme: HeScheme>(contexts: [Context<Scheme>]) throws -> Response<Scheme> {
26+
public func native<Scheme: HeScheme>(contexts: [Scheme.Context]) throws -> Response<Scheme> {
2727
precondition(contexts.count == reply.count)
2828
let matrices: [CiphertextMatrix<Scheme, Coeff>] = try zip(reply, contexts).map { matrix, context in
2929
let serialized: SerializedCiphertextMatrix<Scheme.Scalar> = try matrix.native()

0 commit comments

Comments
 (0)