Skip to content

Commit

Permalink
Inline plaintext and database deserialization (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
fboemer committed Nov 11, 2024
1 parent 1d7e43a commit 6f4fcca
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Benchmarks/RlweBenchmark/RlweBenchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,66 @@ func ciphertextSwapRowsBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void
}
}

// MARK: Serialization

func coeffPlaintextSerializeBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
{
benchmark("CoeffPlaintextSerialize", Scheme.self) { benchmark in
let benchmarkContext: RlweBenchmarkContext<Scheme> = try StaticRlweBenchmarkContext.getBenchmarkContext()
let plaintext = benchmarkContext.coeffPlaintext
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(plaintext.serialize())
}
}
}
}

func evalPlaintextSerializeBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
{
benchmark("EvalPlaintextSerialize", Scheme.self) { benchmark in
let benchmarkContext: RlweBenchmarkContext<Scheme> = try StaticRlweBenchmarkContext.getBenchmarkContext()
let plaintext = benchmarkContext.evalPlaintext
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(plaintext.serialize())
}
}
}
}

func coeffPlaintextDeserializeBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
{
benchmark("CoeffPlaintextDeserialize", Scheme.self) { benchmark in
let benchmarkContext: RlweBenchmarkContext<Scheme> = try StaticRlweBenchmarkContext.getBenchmarkContext()
let plaintext = benchmarkContext.coeffPlaintext
let serialized = plaintext.serialize()
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
try blackHole(_ = Scheme.CoeffPlaintext(
deserialize: serialized,
context: benchmarkContext.context))
}
}
}
}

func evalPlaintextDeserializeBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
{
benchmark("EvalPlaintextDeserialize", Scheme.self) { benchmark in
let benchmarkContext: RlweBenchmarkContext<Scheme> = try StaticRlweBenchmarkContext.getBenchmarkContext()
let plaintext = benchmarkContext.evalPlaintext
let serialized = plaintext.serialize()
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
try blackHole(_ = Scheme.EvalPlaintext(
deserialize: serialized,
context: benchmarkContext.context))
}
}
}
}

func ciphertextSerializeFullBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
{
benchmark("CiphertextSerializeFull", Scheme.self) { benchmark in
Expand Down Expand Up @@ -553,6 +613,16 @@ nonisolated(unsafe) let benchmarks: () -> Void = {
ciphertextSwapRowsBenchmark(Bfv<UInt64>.self)()

// Serialization
coeffPlaintextSerializeBenchmark(Bfv<UInt32>.self)()
coeffPlaintextSerializeBenchmark(Bfv<UInt64>.self)()
evalPlaintextSerializeBenchmark(Bfv<UInt32>.self)()
evalPlaintextSerializeBenchmark(Bfv<UInt64>.self)()

coeffPlaintextDeserializeBenchmark(Bfv<UInt32>.self)()
coeffPlaintextDeserializeBenchmark(Bfv<UInt64>.self)()
evalPlaintextDeserializeBenchmark(Bfv<UInt32>.self)()
evalPlaintextDeserializeBenchmark(Bfv<UInt64>.self)()

ciphertextSerializeFullBenchmark(Bfv<UInt32>.self)()
ciphertextSerializeFullBenchmark(Bfv<UInt64>.self)()
ciphertextSerializeSeedBenchmark(Bfv<UInt32>.self)()
Expand Down
2 changes: 2 additions & 0 deletions Sources/HomomorphicEncryption/SerializedPlaintext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extension Plaintext where Format == Coeff {
/// - serialized: Serialized plaintext.
/// - context: Context to associate with the plaintext.
/// - Throws: Error upon failure to deserialize.
@inlinable
public init(deserialize serialized: SerializedPlaintext, context: Context<Scheme>) throws {
self.context = context
self.poly = try PolyRq(deserialize: serialized.poly, context: context.plaintextContext)
Expand All @@ -51,6 +52,7 @@ extension Plaintext where Format == Eval {
/// - moduliCount: Optional number of moduli to associate with the plaintext. If not set, the plaintext will have
/// the top-level ciphertext context with all the moduli.
/// - Throws: Error upon failure to deserialize.
@inlinable
public init(deserialize serialized: SerializedPlaintext, context: Context<Scheme>, moduliCount: Int? = nil) throws {
self.context = context
let moduliCount = moduliCount ?? context.ciphertextContext.moduli.count
Expand Down
2 changes: 2 additions & 0 deletions Sources/PrivateInformationRetrieval/IndexPirProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public struct ProcessedDatabase<Scheme: HeScheme>: Equatable, Sendable {
/// - path: Filepath storing serialized plaintexts.
/// - context: Context for HE computation.
/// - Throws: Error upon failure to load the database.
@inlinable
public init(from path: String, context: Context<Scheme>) throws {
let loadedFile = try [UInt8](Data(contentsOf: URL(fileURLWithPath: path)))
try self.init(from: loadedFile, context: context)
Expand All @@ -176,6 +177,7 @@ public struct ProcessedDatabase<Scheme: HeScheme>: Equatable, Sendable {
/// - buffer: Serialized plaintexts.
/// - context: Context for HE computation.
/// - Throws: Error upon failure to deserialize.
@inlinable
public init(from buffer: [UInt8], context: Context<Scheme>) throws {
var offset = buffer.startIndex
let versionNumber = buffer[offset]
Expand Down

0 comments on commit 6f4fcca

Please sign in to comment.