Skip to content

Commit

Permalink
Load processedDatabaseWithParameters (#42)
Browse files Browse the repository at this point in the history
karulont authored Aug 2, 2024

Verified

This commit was signed with the committer’s verified signature.
0x2142 Matt Schmitz
1 parent 7014c8d commit 76c3363
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/PrivateInformationRetrieval/IndexPirProtocol.swift
Original file line number Diff line number Diff line change
@@ -227,7 +227,7 @@ public struct ProcessedDatabase<Scheme: HeScheme>: Equatable, Sendable {
}

/// A processed database along with PIR parameters describing the database.
public struct ProcessedDatabaseWithParameters<Scheme: HeScheme>: Sendable {
public struct ProcessedDatabaseWithParameters<Scheme: HeScheme>: Equatable, Sendable {
/// Processed database.
public let database: ProcessedDatabase<Scheme>
/// The algorithm that this database was processed for.
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ public struct KeywordPirConfig: Hashable, Codable {
/// Parameters for a keyword PIR lookup.
///
/// Must be the same between client and server for a correct database lookup.
public struct KeywordPirParameter: Sendable {
public struct KeywordPirParameter: Hashable, Codable, Sendable {
/// Number of hash functions in the ``CuckooTableConfig``.
public let hashFunctionCount: Int

15 changes: 15 additions & 0 deletions Sources/PrivateInformationRetrievalProtobuf/ConversionPir.swift
Original file line number Diff line number Diff line change
@@ -97,6 +97,21 @@ extension Apple_SwiftHomomorphicEncryption_Pir_V1_PirParameters {
dimensions: dimensions.map(Int.init),
batchSize: Int(batchSize))
}

/// Converts the protobuf object into a native type.
/// - Parameter database: The database to use.
/// - Returns: A database with parameters.
/// - Throws: Error when the protobuf object cannot be represented.
public func native<Scheme: HeScheme>(database: ProcessedDatabase<Scheme>) throws
-> ProcessedDatabaseWithParameters<Scheme>
{
try ProcessedDatabaseWithParameters(
database: database,
algorithm: algorithm.native(),
evaluationKeyConfiguration: evaluationKeyConfig.native(),
pirParameter: native(),
keywordPirParameter: hasKeywordPirParams ? keywordPirParams.native() : nil)
}
}

extension Apple_SwiftHomomorphicEncryption_Pir_V1_PirAlgorithm {
Original file line number Diff line number Diff line change
@@ -35,4 +35,23 @@ class ConversionTests: XCTestCase {

XCTAssertEqual(native, databaseRows)
}

func testProcessedDatabaseWithParameters() throws {
let rows = (0..<10).map { KeywordValuePair(keyword: Array(String($0).utf8), value: Array(String($0).utf8)) }
let context: Context<Bfv<UInt32>> = try .init(encryptionParameters: .init(from: .n_4096_logq_27_28_28_logt_13))
let config = try KeywordPirConfig(
dimensionCount: 2,
cuckooTableConfig: .defaultKeywordPir(maxSerializedBucketSize: context.bytesPerPlaintext),
unevenDimensions: true)
let processedDatabaseWithParameters = try KeywordPirServer<MulPirServer<Bfv<UInt32>>>.process(
database: rows,
config: config,
with: context)

let processedDatabase = processedDatabaseWithParameters.database

let pirParameters = try processedDatabaseWithParameters.proto(context: context)
let loadedProcessedDatabaseWithParameters = try pirParameters.native(database: processedDatabase)
XCTAssertEqual(loadedProcessedDatabaseWithParameters, processedDatabaseWithParameters)
}
}

0 comments on commit 76c3363

Please sign in to comment.