Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename configuration -> config #86

Merged
merged 1 commit into from
Sep 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ struct PnnsBenchmarkContext<Scheme: HeScheme> {
precondition(decrypted.distances.data == expected.data, "Wrong response")

self.evaluationKeySize = try evaluationKey.size()
self.evaluationKeyCount = evaluationKey.configuration.keyCount
self.evaluationKeyCount = evaluationKey.config.keyCount
self.querySize = try query.size()
self.queryCiphertextCount = query.ciphertextMatrices.map { matrix in matrix.ciphertexts.count }.sum()
self.responseSize = try response.size()
Expand Down
6 changes: 3 additions & 3 deletions Benchmarks/RlweBenchmark/RlweBenchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct RlweBenchmarkContext<Scheme: HeScheme>: Sendable {
let columnElement = GaloisElement.swappingRows(degree: polyDegree)
let rowElement = try GaloisElement.rotatingColumns(by: rotateColumnsStep, degree: polyDegree)
self.evaluationKey = try context.generateEvaluationKey(
configuration: EvaluationKeyConfiguration(galoisElements: [
config: EvaluationKeyConfig(galoisElements: [
applyGaloisElement,
rowElement,
columnElement,
Expand Down Expand Up @@ -190,11 +190,11 @@ func generateEvaluationKeyBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> V
benchmark("generateEvaluationKey", Scheme.self) { benchmark in
let benchmarkContext: RlweBenchmarkContext<Scheme> = try StaticRlweBenchmarkContext.getBenchmarkContext()
benchmark.startMeasurement()
let configuration = EvaluationKeyConfiguration(galoisElements: [3], hasRelinearizationKey: true)
let config = EvaluationKeyConfig(galoisElements: [3], hasRelinearizationKey: true)
for _ in benchmark.scaledIterations {
try blackHole(
benchmarkContext.context.generateEvaluationKey(
configuration: configuration,
config: config,
using: benchmarkContext.secretKey))
}
}
Expand Down
6 changes: 3 additions & 3 deletions Snippets/HomomorphicEncryption/EvaluationKeySnippet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var ciphertext = try plaintext.encrypt(using: secretKey)
// * rotate columns by 1, indicating a right rotation
// * rotate the columns by -2, indicating a left rotation
// * relinearize.
let evaluationKeyConfig = try EvaluationKeyConfiguration(
let evaluationKeyConfig = try EvaluationKeyConfig(
galoisElements: [
GaloisElement.swappingRows(degree: encryptionParameters.polyDegree),
GaloisElement.rotatingColumns(
Expand All @@ -89,7 +89,7 @@ let evaluationKeyConfig = try EvaluationKeyConfiguration(
],
hasRelinearizationKey: true)
let evaluationKey = try context.generateEvaluationKey(
configuration: evaluationKeyConfig,
config: evaluationKeyConfig,
using: secretKey)

// We swap the first row, [0, 1, 2, 3] and the second row, [4, 5, 6, 7].
Expand Down Expand Up @@ -158,7 +158,7 @@ precondition(ciphertext.polyCount == Bfv<UInt32>.freshCiphertextPolyCount + 1)
// together many ciphertext products. In this case, the relinearization can be
// delayed until after the summation, so the relinearization operation is only
// performed once, rather than on every intermediate product.
precondition(evaluationKey.configuration.hasRelinearizationKey)
precondition(evaluationKey.config.hasRelinearizationKey)
try ciphertext.relinearize(using: evaluationKey)
precondition(ciphertext.polyCount == Bfv<UInt32>.freshCiphertextPolyCount)
try checkDecryptsDecodes(
Expand Down
4 changes: 2 additions & 2 deletions Snippets/HomomorphicEncryption/NoiseBudgetSnippet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ precondition(noiseBudgetAfterMultiplication < noiseBudgetAfterAddition - 3)
// meaningfully affect the noise budget, when encryption parameters are chosen
// with the last ciphertext modulus at least as large as all the other
// ciphertext moduli.
let evaluationKeyConfig = EvaluationKeyConfiguration(hasRelinearizationKey: true)
let evaluationKeyConfig = EvaluationKeyConfig(hasRelinearizationKey: true)
let evaluationKey = try context.generateEvaluationKey(
configuration: evaluationKeyConfig,
config: evaluationKeyConfig,
using: secretKey)
try ciphertext.relinearize(using: evaluationKey)
let noiseBudgetAfterRelinearization = try ciphertext.noiseBudget(
Expand Down
8 changes: 4 additions & 4 deletions Sources/HomomorphicEncryption/Bfv/Bfv+Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ extension Bfv {
// swiftlint:disable:next missing_docs attributes
public static func generateEvaluationKey(
context: Context<Bfv<T>>,
configuration: EvaluationKeyConfiguration,
config: EvaluationKeyConfig,
using secretKey: borrowing SecretKey<Bfv<T>>) throws -> EvaluationKey<Bfv<T>>
{
guard context.supportsEvaluationKey else {
throw HeError.unsupportedHeOperation()
}
var galoisKeys: [Int: KeySwitchKey<Self>] = [:]
for element in configuration.galoisElements where !galoisKeys.keys.contains(element) {
for element in config.galoisElements where !galoisKeys.keys.contains(element) {
let switchedKey = try secretKey.poly.applyGalois(element: element)
galoisKeys[element] = try generateKeySwitchKey(
context: context,
Expand All @@ -46,7 +46,7 @@ extension Bfv {
galoisKey = GaloisKey(keys: galoisKeys)
}
var relinearizationKey: RelinearizationKey<Self>?
if configuration.hasRelinearizationKey {
if config.hasRelinearizationKey {
relinearizationKey = try Self.generateRelinearizationKey(context: context, secretKey: secretKey)
}
return EvaluationKey(galoisKey: galoisKey, relinearizationKey: relinearizationKey)
Expand Down Expand Up @@ -115,7 +115,7 @@ extension Bfv {
/// - keySwitchingKey: keySwitchingKey. The paper calls this `P_{Q_i}(a)`
/// - Returns: The key-switching update for a 2-polynomial ciphertext.
/// - Throws: Error upon failure to compute key-switching update.
/// - seealso: ``Bfv/generateEvaluationKey(context:configuration:using:)``.
/// - seealso: ``Bfv/generateEvaluationKey(context:config:using:)``.
@inlinable
static func computeKeySwitchingUpdate(
context: Context<Bfv<T>>,
Expand Down
14 changes: 7 additions & 7 deletions Sources/HomomorphicEncryption/HeScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ public protocol HeScheme {
/// Generates an ``EvaluationKey``.
/// - Parameters:
/// - context: Context for HE computation.
/// - configuration: Evaluation key configuration.
/// - config: Evaluation key configuration.
/// - secretKey: Secret key used to generate the evaluation key.
/// - Returns: A freshly generated evaluation key.
/// - Throws: Error upon failure to generate an evaluation key.
/// - seealso: ``Context/generateEvaluationKey(configuration:using:)`` for an alternative API.
/// - seealso: ``Context/generateEvaluationKey(config:using:)`` for an alternative API.
static func generateEvaluationKey(
context: Context<Self>,
configuration: EvaluationKeyConfiguration,
config: EvaluationKeyConfig,
using secretKey: SecretKey) throws
-> EvaluationKey

Expand Down Expand Up @@ -1060,17 +1060,17 @@ extension Context {

/// Generates an ``EvaluationKey``.
/// - Parameters:
/// - configuration: Evaluation key configuration.
/// - config: Evaluation key configuration.
/// - secretKey: Secret key used to generate the evaluation key.
/// - Returns: A freshly generated evaluation key.
/// - Throws: Error upon failure to generate an evaluation key.
/// - seealso: ``HeScheme/generateEvaluationKey(context:configuration:using:)`` for an alternative API.
/// - seealso: ``HeScheme/generateEvaluationKey(context:config:using:)`` for an alternative API.
@inlinable
public func generateEvaluationKey(
configuration: EvaluationKeyConfiguration,
config: EvaluationKeyConfig,
using secretKey: SecretKey<Scheme>) throws
-> EvaluationKey<Scheme>
{
try Scheme.generateEvaluationKey(context: self, configuration: configuration, using: secretKey)
try Scheme.generateEvaluationKey(context: self, config: config, using: secretKey)
}
}
16 changes: 8 additions & 8 deletions Sources/HomomorphicEncryption/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public struct EvaluationKey<Scheme: HeScheme>: Equatable, Sendable {
@usableFromInline let relinearizationKey: RelinearizationKey<Scheme>?

/// Returns the configuration for the evaluation key.
public var configuration: EvaluationKeyConfiguration {
EvaluationKeyConfiguration(
public var config: EvaluationKeyConfig {
EvaluationKeyConfig(
// swiftlint:disable:next array_init
galoisElements: galoisKey?.keys.keys.map { $0 } ?? [],
hasRelinearizationKey: relinearizationKey != nil)
Expand All @@ -140,7 +140,7 @@ public struct EvaluationKey<Scheme: HeScheme>: Equatable, Sendable {
}

/// A configuration for generating an evaluation key.
public struct EvaluationKeyConfiguration: Codable, Equatable, Hashable, Sendable {
public struct EvaluationKeyConfig: Codable, Equatable, Hashable, Sendable {
/// Galois elements.
/// - seealso: ``GaloisElement``and ``HeScheme/applyGalois(ciphertext:element:using:)`` for more information.
public let galoisElements: [Int]
Expand All @@ -156,7 +156,7 @@ public struct EvaluationKeyConfiguration: Codable, Equatable, Hashable, Sendable
galoisElements.count + (hasRelinearizationKey ? 1 : 0)
}

/// Initializes an ``EvaluationKeyConfiguration``.
/// Initializes an ``EvaluationKeyConfig``.
/// - Parameters:
/// - galoisElements: Galois elements.
/// - hasRelinearizationKey: Whether the configuration includes a relinearization key.
Expand All @@ -168,18 +168,18 @@ public struct EvaluationKeyConfiguration: Codable, Equatable, Hashable, Sendable
}
}

extension Sequence<EvaluationKeyConfiguration> {
/// Computes the union of ``EvaluationKeyConfiguration``s.
extension Sequence<EvaluationKeyConfig> {
/// Computes the union of ``EvaluationKeyConfig``s.
///
/// The union of ``EvaluationKeyConfiguration``s is a configuration whose:
/// The union of ``EvaluationKeyConfig``s is a configuration whose:
/// * Galois elements is a union of each configuration's Galois elements
/// * `hasRelinearizationKey` is true when any of the sequence of configurations has
/// `hasRelinearizationKey: true`.
///
/// > Note: The union can be used to generate an `EvaluationKey` which supports the HE operations of any of the
/// evaluation key configurations.
/// - Returns: The joint evaluation configuration
public func union() -> EvaluationKeyConfiguration {
public func union() -> EvaluationKeyConfig {
var galoisElements: Set<Int> = []
var hasRelinearizationKey = false
for config in self {
Expand Down
4 changes: 2 additions & 2 deletions Sources/HomomorphicEncryption/NoOpScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public enum NoOpScheme: HeScheme {

public static func generateEvaluationKey(
context: Context<NoOpScheme>,
configuration: EvaluationKeyConfiguration, using _: SecretKey<NoOpScheme>) throws -> EvaluationKey<NoOpScheme>
config: EvaluationKeyConfig, using _: SecretKey<NoOpScheme>) throws -> EvaluationKey<NoOpScheme>
{
let keySwitchKey = KeySwitchKey(context: context, ciphers: [])
let galoisKeys = [Int: KeySwitchKey<NoOpScheme>](
configuration.galoisElements
config.galoisElements
.map { g in (g, keySwitchKey) }) { first, _ in first }
return EvaluationKey(
galoisKey: GaloisKey(keys: galoisKeys),
Expand Down
4 changes: 2 additions & 2 deletions Sources/HomomorphicEncryptionProtobuf/ConversionHe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ extension SerializedEvaluationKey {
extension Apple_SwiftHomomorphicEncryption_V1_EvaluationKeyConfig {
/// Converts the protobuf object to a native type.
/// - Returns: The converted native type.
public func native() -> EvaluationKeyConfiguration {
public func native() -> EvaluationKeyConfig {
.init(galoisElements: galoisElements.map(Int.init), hasRelinearizationKey: hasRelinKey_p)
}
}

extension EvaluationKeyConfiguration {
extension EvaluationKeyConfig {
/// Converts the native object into a protobuf object.
/// - Parameter encryptionParameters: Encryption parameters to associate with the protobuf object.
/// - Returns: The converted protobuf object.
Expand Down
6 changes: 3 additions & 3 deletions Sources/PIRProcessDatabase/ProcessDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ struct ProcessDatabase: ParsableCommand {
keyCompression: config.keyCompression,
trialsPerShard: config.trialsPerShard)

var evaluationKeyConfig = EvaluationKeyConfiguration()
var evaluationKeyConfig = EvaluationKeyConfig()
let context = try Context(encryptionParameters: processArgs.encryptionParameters)
let keywordDatabase = try KeywordDatabase(rows: database, sharding: processArgs.databaseConfig.sharding)
ProcessDatabase.logger
Expand Down Expand Up @@ -368,7 +368,7 @@ struct ProcessDatabase: ParsableCommand {
try processed.database.save(to: outputDatabaseFilename)
ProcessDatabase.logger.info("Saved shard \(shardID) to \(outputDatabaseFilename)")

let shardEvaluationKeyConfig = processed.evaluationKeyConfiguration
let shardEvaluationKeyConfig = processed.evaluationKeyConfig
evaluationKeyConfig = [evaluationKeyConfig, shardEvaluationKeyConfig].union()

let shardPirParameters = try processed.proto(context: context)
Expand Down Expand Up @@ -411,7 +411,7 @@ extension ProcessKeywordDatabase.ShardValidationResult {
label: "ciphertexts")
descriptionDict["evaluation key size"] = try sizeString(
byteCount: evaluationKey.size(),
count: evaluationKey.configuration.keyCount,
count: evaluationKey.config.keyCount,
label: "keys"
)
descriptionDict["response size"] = try sizeString(byteCount: response.size(),
Expand Down
16 changes: 8 additions & 8 deletions Sources/PrivateInformationRetrieval/IndexPirProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public struct IndexPirParameter: Hashable, Codable, Sendable {
/// Number of indices in a query to the database.
public let batchSize: Int
/// Evaluation key configuration.
public let evaluationKeyConfig: EvaluationKeyConfiguration
public let evaluationKeyConfig: EvaluationKeyConfig

/// The number of dimensions in the database.
@usableFromInline var dimensionCount: Int { dimensions.count }
Expand All @@ -117,7 +117,7 @@ public struct IndexPirParameter: Hashable, Codable, Sendable {
entrySizeInBytes: Int,
dimensions: [Int],
batchSize: Int,
evaluationKeyConfig: EvaluationKeyConfiguration)
evaluationKeyConfig: EvaluationKeyConfig)
{
self.entryCount = entryCount
self.entrySizeInBytes = entrySizeInBytes
Expand Down Expand Up @@ -261,7 +261,7 @@ public struct ProcessedDatabaseWithParameters<Scheme: HeScheme>: Equatable, Send
/// The algorithm that this database was processed for.
public let algorithm: PirAlgorithm
/// Evaluation key configuration.
public let evaluationKeyConfiguration: EvaluationKeyConfiguration
public let evaluationKeyConfig: EvaluationKeyConfig
/// Parameters for Index PIR queries.
public let pirParameter: IndexPirParameter
/// Parameters for keyword-value PIR queries.
Expand All @@ -271,19 +271,19 @@ public struct ProcessedDatabaseWithParameters<Scheme: HeScheme>: Equatable, Send
/// - Parameters:
/// - database: Processed database.
/// - algorithm: The PIR algorithm used.
/// - evaluationKeyConfiguration: Evaluation key configuration.
/// - evaluationKeyConfig: Evaluation key configuration.
/// - pirParameter: Index PIR parameters.
/// - keywordPirParameter: Optional keyword PIR parameters.
public init(
database: ProcessedDatabase<Scheme>,
algorithm: PirAlgorithm,
evaluationKeyConfiguration: EvaluationKeyConfiguration,
evaluationKeyConfig: EvaluationKeyConfig,
pirParameter: IndexPirParameter,
keywordPirParameter: KeywordPirParameter? = nil)
{
self.database = database
self.algorithm = algorithm
self.evaluationKeyConfiguration = evaluationKeyConfiguration
self.evaluationKeyConfig = evaluationKeyConfig
self.pirParameter = pirParameter
self.keywordPirParameter = keywordPirParameter
}
Expand Down Expand Up @@ -363,7 +363,7 @@ public protocol IndexPirServer: Sendable {
/// Evaluation key configuration.
///
/// This tells the client what to include in the evaluation key. Must be the same between client and server.
var evaluationKeyConfiguration: EvaluationKeyConfiguration { get }
var evaluationKeyConfig: EvaluationKeyConfig { get }

/// Initializes an ``IndexPirServer`` with a database.
/// - Parameters:
Expand Down Expand Up @@ -445,7 +445,7 @@ public protocol IndexPirClient: Sendable {
/// Evaluation key configuration.
///
/// Must be the same between client and server.
var evaluationKeyConfiguration: EvaluationKeyConfiguration { get }
var evaluationKeyConfig: EvaluationKeyConfig { get }

/// Initializes an ``IndexPirClient``.
/// - Parameters:
Expand Down
14 changes: 7 additions & 7 deletions Sources/PrivateInformationRetrieval/KeywordDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,19 @@ public enum ProcessKeywordDatabase {
/// A processed keyword database.
public struct Processed<Scheme: HeScheme> {
/// Evaluation key configuration.
public let evaluationKeyConfiguration: EvaluationKeyConfiguration
public let evaluationKeyConfig: EvaluationKeyConfig
/// Maps each shardID to the associated database shard and PIR parameters.
public let shards: [String: ProcessedDatabaseWithParameters<Scheme>]

/// Initializes a processed keyword database.
/// - Parameters:
/// - evaluationKeyConfiguration: Evaluation key configuration.
/// - evaluationKeyConfig: Evaluation key configuration.
/// - shards: Database shards.
@inlinable
init(evaluationKeyConfiguration: EvaluationKeyConfiguration,
init(evaluationKeyConfig: EvaluationKeyConfig,
shards: [String: ProcessedDatabaseWithParameters<Scheme>])
{
self.evaluationKeyConfiguration = evaluationKeyConfiguration
self.evaluationKeyConfig = evaluationKeyConfig
self.shards = shards
}
}
Expand Down Expand Up @@ -477,7 +477,7 @@ public enum ProcessKeywordDatabase {
public static func process<Scheme: HeScheme>(rows: some Collection<KeywordValuePair>,
with arguments: Arguments<Scheme>) throws -> Processed<Scheme>
{
var evaluationKeyConfiguration = EvaluationKeyConfiguration()
var evaluationKeyConfig = EvaluationKeyConfig()
let keywordConfig = arguments.databaseConfig.keywordPirConfig

let context = try Context(encryptionParameters: arguments.encryptionParameters)
Expand All @@ -491,13 +491,13 @@ public enum ProcessKeywordDatabase {
let processed = try KeywordPirServer<MulPirServer<Scheme>>.process(database: shardedDatabase,
config: keywordConfig,
with: context)
evaluationKeyConfiguration = [evaluationKeyConfiguration, processed.pirParameter.evaluationKeyConfig]
evaluationKeyConfig = [evaluationKeyConfig, processed.pirParameter.evaluationKeyConfig]
.union()

processedShards[shardID] = processed
}
return Processed(
evaluationKeyConfiguration: evaluationKeyConfiguration,
evaluationKeyConfig: evaluationKeyConfig,
shards: processedShards)
}
}
Loading
Loading