Skip to content

Commit c196bfb

Browse files
committed
Fix EvalCiphertext serialization
1 parent ea9ff79 commit c196bfb

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

Sources/HomomorphicEncryption/SerializedCiphertext.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
1+
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -115,8 +115,8 @@ extension Ciphertext {
115115
}
116116

117117
let skipLSBs: [Int] = if forDecryption, polyContext().moduli.count == 1,
118-
polys.count == context.encryptionParameters
119-
.skipLSBsForDecryption().count
118+
polys.count == context.encryptionParameters.skipLSBsForDecryption().count,
119+
Format.self == Coeff.self
120120
{
121121
context.encryptionParameters.skipLSBsForDecryption()
122122
} else {

Tests/HomomorphicEncryptionTests/SerializationTests.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Apple Inc. and the Swift Homomorphic Encryption project authors
1+
// Copyright 2024-2025 Apple Inc. and the Swift Homomorphic Encryption project authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -75,6 +75,24 @@ class SerializationTests: XCTestCase {
7575
let decrypted = try deserialized.decrypt(using: secretKey)
7676
XCTAssertEqual(decrypted, plaintext)
7777
}
78+
// serialize for decryption eval format
79+
do {
80+
var ciphertext = ciphertext
81+
try ciphertext.modSwitchDownToSingle()
82+
let evalCiphertext = try ciphertext.convertToEvalFormat()
83+
let serialized = evalCiphertext.serialize(forDecryption: true)
84+
if case let .full(_, skipLSBs, _) = serialized {
85+
XCTAssertTrue(skipLSBs.allSatisfy { $0 == 0 })
86+
} else {
87+
XCTFail("Must be full serialization")
88+
}
89+
let deserialized: Scheme.CanonicalCiphertext = try Ciphertext(
90+
deserialize: serialized,
91+
context: context,
92+
moduliCount: ciphertext.moduli.count)
93+
let decrypted = try deserialized.decrypt(using: secretKey)
94+
XCTAssertEqual(decrypted, plaintext)
95+
}
7896
// serialize indices for decryption
7997
do {
8098
var ciphertext = ciphertext

0 commit comments

Comments
 (0)