Skip to content

Commit 6f4fcca

Browse files
committed
Inline plaintext and database deserialization (#129)
1 parent 1d7e43a commit 6f4fcca

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

Benchmarks/RlweBenchmark/RlweBenchmark.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,66 @@ func ciphertextSwapRowsBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void
411411
}
412412
}
413413

414+
// MARK: Serialization
415+
416+
func coeffPlaintextSerializeBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
417+
{
418+
benchmark("CoeffPlaintextSerialize", Scheme.self) { benchmark in
419+
let benchmarkContext: RlweBenchmarkContext<Scheme> = try StaticRlweBenchmarkContext.getBenchmarkContext()
420+
let plaintext = benchmarkContext.coeffPlaintext
421+
benchmark.startMeasurement()
422+
for _ in benchmark.scaledIterations {
423+
blackHole(plaintext.serialize())
424+
}
425+
}
426+
}
427+
}
428+
429+
func evalPlaintextSerializeBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
430+
{
431+
benchmark("EvalPlaintextSerialize", Scheme.self) { benchmark in
432+
let benchmarkContext: RlweBenchmarkContext<Scheme> = try StaticRlweBenchmarkContext.getBenchmarkContext()
433+
let plaintext = benchmarkContext.evalPlaintext
434+
benchmark.startMeasurement()
435+
for _ in benchmark.scaledIterations {
436+
blackHole(plaintext.serialize())
437+
}
438+
}
439+
}
440+
}
441+
442+
func coeffPlaintextDeserializeBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
443+
{
444+
benchmark("CoeffPlaintextDeserialize", Scheme.self) { benchmark in
445+
let benchmarkContext: RlweBenchmarkContext<Scheme> = try StaticRlweBenchmarkContext.getBenchmarkContext()
446+
let plaintext = benchmarkContext.coeffPlaintext
447+
let serialized = plaintext.serialize()
448+
benchmark.startMeasurement()
449+
for _ in benchmark.scaledIterations {
450+
try blackHole(_ = Scheme.CoeffPlaintext(
451+
deserialize: serialized,
452+
context: benchmarkContext.context))
453+
}
454+
}
455+
}
456+
}
457+
458+
func evalPlaintextDeserializeBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
459+
{
460+
benchmark("EvalPlaintextDeserialize", Scheme.self) { benchmark in
461+
let benchmarkContext: RlweBenchmarkContext<Scheme> = try StaticRlweBenchmarkContext.getBenchmarkContext()
462+
let plaintext = benchmarkContext.evalPlaintext
463+
let serialized = plaintext.serialize()
464+
benchmark.startMeasurement()
465+
for _ in benchmark.scaledIterations {
466+
try blackHole(_ = Scheme.EvalPlaintext(
467+
deserialize: serialized,
468+
context: benchmarkContext.context))
469+
}
470+
}
471+
}
472+
}
473+
414474
func ciphertextSerializeFullBenchmark<Scheme: HeScheme>(_: Scheme.Type) -> () -> Void {
415475
{
416476
benchmark("CiphertextSerializeFull", Scheme.self) { benchmark in
@@ -553,6 +613,16 @@ nonisolated(unsafe) let benchmarks: () -> Void = {
553613
ciphertextSwapRowsBenchmark(Bfv<UInt64>.self)()
554614

555615
// Serialization
616+
coeffPlaintextSerializeBenchmark(Bfv<UInt32>.self)()
617+
coeffPlaintextSerializeBenchmark(Bfv<UInt64>.self)()
618+
evalPlaintextSerializeBenchmark(Bfv<UInt32>.self)()
619+
evalPlaintextSerializeBenchmark(Bfv<UInt64>.self)()
620+
621+
coeffPlaintextDeserializeBenchmark(Bfv<UInt32>.self)()
622+
coeffPlaintextDeserializeBenchmark(Bfv<UInt64>.self)()
623+
evalPlaintextDeserializeBenchmark(Bfv<UInt32>.self)()
624+
evalPlaintextDeserializeBenchmark(Bfv<UInt64>.self)()
625+
556626
ciphertextSerializeFullBenchmark(Bfv<UInt32>.self)()
557627
ciphertextSerializeFullBenchmark(Bfv<UInt64>.self)()
558628
ciphertextSerializeSeedBenchmark(Bfv<UInt32>.self)()

Sources/HomomorphicEncryption/SerializedPlaintext.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extension Plaintext where Format == Coeff {
3737
/// - serialized: Serialized plaintext.
3838
/// - context: Context to associate with the plaintext.
3939
/// - Throws: Error upon failure to deserialize.
40+
@inlinable
4041
public init(deserialize serialized: SerializedPlaintext, context: Context<Scheme>) throws {
4142
self.context = context
4243
self.poly = try PolyRq(deserialize: serialized.poly, context: context.plaintextContext)
@@ -51,6 +52,7 @@ extension Plaintext where Format == Eval {
5152
/// - moduliCount: Optional number of moduli to associate with the plaintext. If not set, the plaintext will have
5253
/// the top-level ciphertext context with all the moduli.
5354
/// - Throws: Error upon failure to deserialize.
55+
@inlinable
5456
public init(deserialize serialized: SerializedPlaintext, context: Context<Scheme>, moduliCount: Int? = nil) throws {
5557
self.context = context
5658
let moduliCount = moduliCount ?? context.ciphertextContext.moduli.count

Sources/PrivateInformationRetrieval/IndexPirProtocol.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public struct ProcessedDatabase<Scheme: HeScheme>: Equatable, Sendable {
166166
/// - path: Filepath storing serialized plaintexts.
167167
/// - context: Context for HE computation.
168168
/// - Throws: Error upon failure to load the database.
169+
@inlinable
169170
public init(from path: String, context: Context<Scheme>) throws {
170171
let loadedFile = try [UInt8](Data(contentsOf: URL(fileURLWithPath: path)))
171172
try self.init(from: loadedFile, context: context)
@@ -176,6 +177,7 @@ public struct ProcessedDatabase<Scheme: HeScheme>: Equatable, Sendable {
176177
/// - buffer: Serialized plaintexts.
177178
/// - context: Context for HE computation.
178179
/// - Throws: Error upon failure to deserialize.
180+
@inlinable
179181
public init(from buffer: [UInt8], context: Context<Scheme>) throws {
180182
var offset = buffer.startIndex
181183
let versionNumber = buffer[offset]

0 commit comments

Comments
 (0)