Skip to content

Commit c89a195

Browse files
committed
Decouple EncryptionParameters and Scheme
1 parent b675808 commit c89a195

File tree

55 files changed

+514
-453
lines changed

Some content is hidden

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

55 files changed

+514
-453
lines changed

Benchmarks/RlweBenchmark/RlweBenchmark.swift

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

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

4848
let data: [Scheme.Scalar]
@@ -130,15 +130,15 @@ extension EncryptionParametersConfig: CustomStringConvertible {
130130

131131
extension EncryptionParameters {
132132
init(config: EncryptionParametersConfig) throws {
133-
let plaintextModuli = try Scheme.Scalar.generatePrimes(
133+
let plaintextModuli = try Scalar.generatePrimes(
134134
significantBitCounts: config.plaintextModulusBits,
135135
preferringSmall: true,
136136
nttDegree: config.polyDegree)
137-
let coefficientModuli = try Scheme.Scalar.generatePrimes(
137+
let coefficientModuli = try Scalar.generatePrimes(
138138
significantBitCounts: config.coefficientModulusBits,
139139
preferringSmall: false,
140140
nttDegree: config.polyDegree)
141-
self = try EncryptionParameters<Scheme>(
141+
self = try EncryptionParameters<Scalar>(
142142
polyDegree: config.polyDegree,
143143
plaintextModulus: plaintextModuli[0],
144144
coefficientModuli: coefficientModuli,
@@ -160,10 +160,10 @@ func contextInitBenchmark<Scheme: HeScheme>(_: Scheme.Type, config: EncryptionPa
160160
{
161161
let benchmarkName = ["ContextInit", String(describing: Scheme.self), config.description].joined(separator: "/")
162162
Benchmark(benchmarkName, configuration: benchmarkConfiguration) { benchmark in
163-
let encryptionParameters = try EncryptionParameters<Scheme>(config: config)
163+
let encryptionParameters = try EncryptionParameters<Scheme.Scalar>(config: config)
164164
benchmark.startMeasurement()
165165
for _ in benchmark.scaledIterations {
166-
try blackHole(_ = Context(encryptionParameters: encryptionParameters))
166+
try blackHole(_ = Context<Scheme>(encryptionParameters: encryptionParameters))
167167
}
168168
}
169169
}

Snippets/.swiftformat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--maxwidth 81 # Copyright header is 81 characters
1+
--maxwidth 87 # Copyright header is 87 characters

Snippets/.swiftlint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
line_length:
2-
# Copyright header is 81 characters
3-
warning: 81
2+
# Copyright header is 87 characters
3+
warning: 87

Snippets/HomomorphicEncryption/BasicsSnippet.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Example showing the basics.
22

33
// snippet.hide
4-
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
4+
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -22,9 +22,9 @@ import HomomorphicEncryption
2222
// We start by choosing some encryption parameters for the Bfv<UInt64> scheme.
2323
// *These encryption parameters are insecure, suitable for testing only.*
2424
let encryptParams =
25-
try EncryptionParameters<Bfv<UInt64>>(from: .insecure_n_8_logq_5x18_logt_5)
25+
try EncryptionParameters<UInt64>(from: .insecure_n_8_logq_5x18_logt_5)
2626
// Perform pre-computation for HE computation with these parameters.
27-
let context = try Context(encryptionParameters: encryptParams)
27+
let context = try Context<Bfv<UInt64>>(encryptionParameters: encryptParams)
2828

2929
// We encode N values using coefficient encoding.
3030
let values: [UInt64] = [8, 5, 12, 12, 15, 0, 8, 5]

Snippets/HomomorphicEncryption/EncryptionParametersSnippet.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Choosing encryption parameters.
22

33
// snippet.hide
4-
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
4+
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -37,22 +37,20 @@ import HomomorphicEncryption
3737
// encryption parameters. Generally, the coefficient moduli should all be as
3838
// large as possible, while remaining under the maxLog2CoefficientModulus, and
3939
// as close to the same size as possible.
40-
let params4096 =
41-
try EncryptionParameters<Bfv<UInt64>>(from: .n_4096_logq_27_28_28_logt_5)
42-
let params8192 =
43-
try EncryptionParameters<Bfv<UInt64>>(from: .n_8192_logq_3x55_logt_24)
40+
let params4096 = try EncryptionParameters<UInt64>(from: .n_4096_logq_27_28_28_logt_5)
41+
let params8192 = try EncryptionParameters<UInt64>(from: .n_8192_logq_3x55_logt_24)
4442

4543
// We can also create custom parameters.
4644
// For instance, for a very small HE circuit, N=2048 might suffice.
4745
// To maintain security, we ensure the coefficient modulus does not exceed the
4846
// maximum log2 coefficient modulus for post-quantum 128-bit security.
4947
let degree = 2048
50-
let maxLog2Q = try EncryptionParameters<Bfv<UInt64>>.maxLog2CoefficientModulus(
48+
let maxLog2Q = try EncryptionParameters<UInt64>.maxLog2CoefficientModulus(
5149
degree: degree,
5250
securityLevel: .quantum128)
5351
precondition(maxLog2Q == 41)
5452
// We can also stay in the range for 32-bit moduli, for faster runtimes.
55-
let max2BitModulus = EncryptionParameters<Bfv<UInt32>>.maxSingleModulus
53+
let max2BitModulus = EncryptionParameters<UInt32>.maxSingleModulus
5654
precondition(max2BitModulus.ceilLog2 == 30)
5755

5856
// The coefficient moduli must be NTT-friendly, so we set nttDegree: degree.
@@ -73,21 +71,21 @@ let plaintextModulus = try UInt32.generatePrimes(
7371
preferringSmall: false)[0]
7472
// This custom parameter set has only a single coefficient modulus, so it is not
7573
// compatible with evaluation key operations.
76-
let customParams = try EncryptionParameters<Bfv<UInt32>>(
74+
let customUInt32Params = try EncryptionParameters<UInt32>(
7775
polyDegree: degree,
7876
plaintextModulus: plaintextModulus,
7977
coefficientModuli: coefficientModuli,
8078
errorStdDev: .stdDev32,
8179
securityLevel: .quantum128)
82-
precondition(!customParams.supportsSimdEncoding)
83-
precondition(!customParams.supportsEvaluationKey)
80+
precondition(!customUInt32Params.supportsSimdEncoding)
81+
precondition(!customUInt32Params.supportsEvaluationKey)
8482

8583
// snippet.hide
8684
func summarize<Scheme: HeScheme>(
87-
parameters: EncryptionParameters<Scheme>) throws
85+
parameters: EncryptionParameters<Scheme.Scalar>, _: Scheme.Type) throws
8886
{
8987
let values = (0..<8).map { Scheme.Scalar($0) }
90-
let context = try Context(encryptionParameters: parameters)
88+
let context = try Context<Scheme>(encryptionParameters: parameters)
9189
let plaintext: Scheme.CoeffPlaintext = try context.encode(
9290
values: values,
9391
format: .coefficient)
@@ -106,6 +104,6 @@ func summarize<Scheme: HeScheme>(
106104
""")
107105
}
108106

109-
try summarize(parameters: customParams)
110-
try summarize(parameters: params4096)
111-
try summarize(parameters: params8192)
107+
try summarize(parameters: customUInt32Params, Bfv<UInt32>.self)
108+
try summarize(parameters: params4096, Bfv<UInt64>.self)
109+
try summarize(parameters: params8192, Bfv<UInt64>.self)

Snippets/HomomorphicEncryption/EvaluationKeySnippet.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Example using an ``EvaluationKey``.
22

33
// snippet.hide
4-
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
4+
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -43,10 +43,10 @@ func checkDecryptsDecodes<Scheme: HeScheme>(
4343
precondition(PredefinedRlweParameters.insecure_n_8_logq_5x18_logt_5
4444
.supportsScalar(Bfv<UInt32>.Scalar.self))
4545
let encryptionParameters =
46-
try EncryptionParameters<Bfv<UInt32>>(from: .insecure_n_8_logq_5x18_logt_5)
46+
try EncryptionParameters<UInt32>(from: .insecure_n_8_logq_5x18_logt_5)
4747
precondition(encryptionParameters.plaintextModulus == 17)
4848
// Perform pre-computation for HE computation with these parameters.
49-
let context = try Context(encryptionParameters: encryptionParameters)
49+
let context = try Context<Bfv<UInt32>>(encryptionParameters: encryptionParameters)
5050

5151
// We encode N values using SIMD encoding.
5252
// Each value is a Bfv<UInt32>.Scalar, which is UInt32.

Snippets/HomomorphicEncryption/MultiplicationSnippet.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Example showing HE multiplication.
22

33
// snippet.hide
4-
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
4+
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -31,10 +31,10 @@ precondition(PredefinedRlweParameters.insecure_n_8_logq_5x18_logt_5
3131
precondition(PredefinedRlweParameters.insecure_n_8_logq_5x18_logt_5
3232
.supportsScalar(UInt32.self))
3333
let encryptParams =
34-
try EncryptionParameters<Bfv<UInt32>>(from: .insecure_n_8_logq_5x18_logt_5)
34+
try EncryptionParameters<UInt32>(from: .insecure_n_8_logq_5x18_logt_5)
3535
precondition(encryptParams.plaintextModulus == 17)
3636
// Perform pre-computation for HE computation with these parameters.
37-
let context = try Context(encryptionParameters: encryptParams)
37+
let context = try Context<Bfv<UInt32>>(encryptionParameters: encryptParams)
3838

3939
// We don't need to use all the slots in the encoding.
4040
// However, performing HE operations on ciphertexts with fewer slots doesn't give

Snippets/HomomorphicEncryption/NoiseBudgetSnippet.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Example for noise budget: ``Ciphertext/noiseBudget(using:variableTime:)``.
22

33
// snippet.hide
4-
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
4+
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -43,9 +43,9 @@ func checkDecryptsDecodes<Scheme: HeScheme>(
4343
// snippet.show
4444
// We start by choosing some encryption parameters for the Bfv<UInt32> scheme.
4545
let encryptionParameters =
46-
try EncryptionParameters<Bfv<UInt32>>(from: .insecure_n_8_logq_5x18_logt_5)
46+
try EncryptionParameters<UInt32>(from: .insecure_n_8_logq_5x18_logt_5)
4747
// Perform pre-computation for HE computation with these parameters.
48-
let context = try Context(encryptionParameters: encryptionParameters)
48+
let context = try Context<Bfv<UInt32>>(encryptionParameters: encryptionParameters)
4949

5050
// We encode N values in coefficient format and SIMD encoding.
5151
let values = (0..<8).map { UInt32($0) }

Snippets/HomomorphicEncryption/SerializationSnippet.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Example for serialization.
22

33
// snippet.hide
4-
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
4+
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -34,9 +34,9 @@ extension SerializedCiphertext {
3434

3535
// We start by choosing some encryption parameters for the Bfv<UInt32> scheme.
3636
let encryptionParameters =
37-
try EncryptionParameters<Bfv<UInt32>>(from: .n_4096_logq_27_28_28_logt_5)
37+
try EncryptionParameters<UInt32>(from: .n_4096_logq_27_28_28_logt_5)
3838
// Perform pre-computation for HE computation with these parameters.
39-
let context = try Context(encryptionParameters: encryptionParameters)
39+
let context = try Context<Bfv<UInt32>>(encryptionParameters: encryptionParameters)
4040

4141
// We encode some values in coefficient format and coefficient encoding.
4242
let values = (0..<8).map { UInt32($0) }

Sources/BenchmarkUtilities/PirBenchmarkUtilities.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ extension PirEncryptionParametersConfig: CustomStringConvertible {
5757

5858
extension EncryptionParameters {
5959
init(from config: PirEncryptionParametersConfig) throws {
60-
let plaintextModulus = try Scheme.Scalar.generatePrimes(
60+
let plaintextModulus = try Scalar.generatePrimes(
6161
significantBitCounts: [config.plaintextModulusBits],
6262
preferringSmall: true)[0]
63-
let coefficientModuli = try Scheme.Scalar.generatePrimes(
63+
let coefficientModuli = try Scalar.generatePrimes(
6464
significantBitCounts: config.coefficientModulusBits,
6565
preferringSmall: false,
6666
nttDegree: config.polyDegree)
@@ -88,7 +88,7 @@ struct ProcessBenchmarkContext<Server: IndexPirServer> {
8888
init(server _: Server.Type, pirConfig: IndexPirConfig,
8989
parameterConfig: PirEncryptionParametersConfig) throws
9090
{
91-
let encryptParameter: EncryptionParameters<Server.Scheme> =
91+
let encryptParameter: EncryptionParameters<Server.Scheme.Scalar> =
9292
try EncryptionParameters(from: parameterConfig)
9393
self.database = getDatabaseForTesting(
9494
numberOfEntries: pirConfig.entryCount,
@@ -169,8 +169,9 @@ struct IndexPirBenchmarkContext<Server: IndexPirServer, Client: IndexPirClient>
169169
pirConfig: IndexPirConfig,
170170
parameterConfig: PirEncryptionParametersConfig) throws
171171
{
172-
let encryptParameter: EncryptionParameters<Server.Scheme> = try EncryptionParameters(from: parameterConfig)
173-
let context = try Context(encryptionParameters: encryptParameter)
172+
let encryptParameter: EncryptionParameters<Server.Scheme.Scalar> =
173+
try EncryptionParameters(from: parameterConfig)
174+
let context = try Context<Server.Scheme>(encryptionParameters: encryptParameter)
174175
let indexPirParameters = Server.generateParameter(config: pirConfig, with: context)
175176
let database = getDatabaseForTesting(
176177
numberOfEntries: pirConfig.entryCount,
@@ -283,9 +284,9 @@ struct KeywordPirBenchmarkContext<IndexServer: IndexPirServer, IndexClient: Inde
283284
parameterConfig: PirEncryptionParametersConfig,
284285
keyCompression: PirKeyCompressionStrategy) async throws
285286
{
286-
let encryptParameter: EncryptionParameters<Server.Scheme> = try EncryptionParameters(from: parameterConfig)
287-
let context = try Context(encryptionParameters: encryptParameter)
288-
287+
let encryptParameter: EncryptionParameters<Server.Scheme.Scalar> =
288+
try EncryptionParameters(from: parameterConfig)
289+
let context = try Context<Server.Scheme>(encryptionParameters: encryptParameter)
289290
let rows = (0..<databaseCount).map { index in KeywordValuePair(
290291
keyword: [UInt8](String(index).utf8),
291292
value: (0..<payloadSize).map { _ in UInt8.random(in: 0..<UInt8.max) })

0 commit comments

Comments
 (0)