diff --git a/Sources/HomomorphicEncryption/SerializedCiphertext.swift b/Sources/HomomorphicEncryption/SerializedCiphertext.swift index 0674e3b8..65679b2a 100644 --- a/Sources/HomomorphicEncryption/SerializedCiphertext.swift +++ b/Sources/HomomorphicEncryption/SerializedCiphertext.swift @@ -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. @@ -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 { diff --git a/Tests/HomomorphicEncryptionTests/SerializationTests.swift b/Tests/HomomorphicEncryptionTests/SerializationTests.swift index ae3fafd7..7b264e37 100644 --- a/Tests/HomomorphicEncryptionTests/SerializationTests.swift +++ b/Tests/HomomorphicEncryptionTests/SerializationTests.swift @@ -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. @@ -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