Skip to content

Fix EvalCiphertext serialization #209

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

Open
wants to merge 1 commit into
base: release/1.0
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Sources/HomomorphicEncryption/SerializedCiphertext.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -115,8 +115,8 @@ extension Ciphertext {
}

let skipLSBs: [Int] = if forDecryption, polyContext().moduli.count == 1,
polys.count == context.encryptionParameters
.skipLSBsForDecryption().count
polys.count == context.encryptionParameters.skipLSBsForDecryption().count,
Format.self == Coeff.self
{
context.encryptionParameters.skipLSBsForDecryption()
} else {
Expand Down
20 changes: 19 additions & 1 deletion Tests/HomomorphicEncryptionTests/SerializationTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -75,6 +75,24 @@ class SerializationTests: XCTestCase {
let decrypted = try deserialized.decrypt(using: secretKey)
XCTAssertEqual(decrypted, plaintext)
}
// serialize for decryption eval format
do {
var ciphertext = ciphertext
try ciphertext.modSwitchDownToSingle()
let evalCiphertext = try ciphertext.convertToEvalFormat()
let serialized = evalCiphertext.serialize(forDecryption: true)
if case let .full(_, skipLSBs, _) = serialized {
XCTAssertTrue(skipLSBs.allSatisfy { $0 == 0 })
} else {
XCTFail("Must be full serialization")
}
let deserialized: Scheme.EvalCiphertext = try Ciphertext(
deserialize: serialized,
context: context,
moduliCount: ciphertext.moduli.count)
let decrypted = try deserialized.decrypt(using: secretKey)
XCTAssertEqual(decrypted, plaintext)
}
// serialize indices for decryption
do {
var ciphertext = ciphertext
Expand Down