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

Inline plaintext and database deserialization #129

Merged
merged 1 commit into from
Nov 11, 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
70 changes: 70 additions & 0 deletions Benchmarks/RlweBenchmark/RlweBenchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,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 @@ -554,6 +614,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
Loading