|
| 1 | +// Copyright 2025 Apple Inc. and the Swift Homomorphic Encryption project authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import HomomorphicEncryption |
| 16 | +import PrivateNearestNeighborSearch |
| 17 | +import Testing |
| 18 | + |
| 19 | +@inlinable |
| 20 | +func increasingData<T: ScalarType>(dimensions: MatrixDimensions, modulus: T) -> [[T]] { |
| 21 | + (0..<dimensions.rowCount).map { rowIndex in |
| 22 | + (0..<dimensions.columnCount).map { columnIndex in |
| 23 | + let value = 1 + T(rowIndex * dimensions.columnCount + columnIndex) |
| 24 | + return value % modulus |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +@inlinable |
| 30 | +func randomData<T: ScalarType>(dimensions: MatrixDimensions, modulus: T) -> [[T]] { |
| 31 | + (0..<dimensions.rowCount).map { _ in |
| 32 | + (0..<dimensions.columnCount).map { _ in T.random(in: 0..<modulus) } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +extension PrivateNearestNeighborSearchUtil { |
| 37 | + /// Tests for `CiphertextMatrix`. |
| 38 | + public enum CiphertextMatrixTests { |
| 39 | + /// Testing encryption/decryption round-trip. |
| 40 | + @inlinable |
| 41 | + public static func encryptDecryptRoundTrip<Scheme: HeScheme>(for _: Scheme.Type) throws { |
| 42 | + let rlweParams = PredefinedRlweParameters.insecure_n_8_logq_5x18_logt_5 |
| 43 | + let encryptionParameters = try EncryptionParameters<Scheme>(from: rlweParams) |
| 44 | + #expect(encryptionParameters.supportsSimdEncoding) |
| 45 | + let context = try Context<Scheme>(encryptionParameters: encryptionParameters) |
| 46 | + let dimensions = try MatrixDimensions(rowCount: 10, columnCount: 4) |
| 47 | + let encodeValues: [[Scheme.Scalar]] = increasingData( |
| 48 | + dimensions: dimensions, |
| 49 | + modulus: context.plaintextModulus) |
| 50 | + let plaintextMatrix = try PlaintextMatrix<Scheme, Coeff>( |
| 51 | + context: context, |
| 52 | + dimensions: dimensions, |
| 53 | + packing: .denseRow, |
| 54 | + values: encodeValues.flatMap { $0 }) |
| 55 | + let secretKey = try context.generateSecretKey() |
| 56 | + var ciphertextMatrix = try plaintextMatrix.encrypt(using: secretKey) |
| 57 | + let plaintextMatrixRoundTrip = try ciphertextMatrix.decrypt(using: secretKey) |
| 58 | + #expect(plaintextMatrixRoundTrip == plaintextMatrix) |
| 59 | + |
| 60 | + // modSwitchDownToSingle |
| 61 | + do { |
| 62 | + try ciphertextMatrix.modSwitchDownToSingle() |
| 63 | + let plaintextMatrixRoundTrip = try ciphertextMatrix.decrypt(using: secretKey) |
| 64 | + #expect(plaintextMatrixRoundTrip == plaintextMatrix) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// Testing convert to Coeff/Eval format roundtrip. |
| 69 | + @inlinable |
| 70 | + public static func convertFormatRoundTrip<Scheme: HeScheme>(for _: Scheme.Type) throws { |
| 71 | + let rlweParams = PredefinedRlweParameters.insecure_n_8_logq_5x18_logt_5 |
| 72 | + let encryptionParameters = try EncryptionParameters<Scheme>(from: rlweParams) |
| 73 | + #expect(encryptionParameters.supportsSimdEncoding) |
| 74 | + let context = try Context<Scheme>(encryptionParameters: encryptionParameters) |
| 75 | + let dimensions = try MatrixDimensions(rowCount: 10, columnCount: 4) |
| 76 | + let encodeValues: [[Scheme.Scalar]] = increasingData( |
| 77 | + dimensions: dimensions, |
| 78 | + modulus: context.plaintextModulus) |
| 79 | + let plaintextMatrix = try PlaintextMatrix<Scheme, Coeff>( |
| 80 | + context: context, |
| 81 | + dimensions: dimensions, |
| 82 | + packing: .denseRow, |
| 83 | + values: encodeValues.flatMap { $0 }) |
| 84 | + let secretKey = try context.generateSecretKey() |
| 85 | + let ciphertextCoeffMatrix: CiphertextMatrix = try plaintextMatrix.encrypt(using: secretKey) |
| 86 | + let ciphertextEvalMatrix = try ciphertextCoeffMatrix.convertToEvalFormat() |
| 87 | + let ciphertextMatrixRoundTrip = try ciphertextEvalMatrix.convertToCoeffFormat() |
| 88 | + let decoded = try ciphertextMatrixRoundTrip.decrypt(using: secretKey) |
| 89 | + #expect(plaintextMatrix == decoded) |
| 90 | + } |
| 91 | + |
| 92 | + /// Testing `extractDenseRow`. |
| 93 | + @inlinable |
| 94 | + public static func extractDenseRow<Scheme: HeScheme>(for _: Scheme.Type) throws { |
| 95 | + let degree = 16 |
| 96 | + let plaintextModulus = try Scheme.Scalar.generatePrimes( |
| 97 | + significantBitCounts: [9], |
| 98 | + preferringSmall: true, |
| 99 | + nttDegree: degree)[0] |
| 100 | + let coefficientModuli = try Scheme.Scalar.generatePrimes( |
| 101 | + significantBitCounts: Array(repeating: Scheme.Scalar.bitWidth - 4, count: 2), |
| 102 | + preferringSmall: false, |
| 103 | + nttDegree: degree) |
| 104 | + let encryptionParameters = try EncryptionParameters<Scheme>( |
| 105 | + polyDegree: degree, |
| 106 | + plaintextModulus: plaintextModulus, |
| 107 | + coefficientModuli: coefficientModuli, |
| 108 | + errorStdDev: .stdDev32, |
| 109 | + securityLevel: .unchecked) |
| 110 | + #expect(encryptionParameters.supportsSimdEncoding) |
| 111 | + let context = try Context<Scheme>(encryptionParameters: encryptionParameters) |
| 112 | + |
| 113 | + for rowCount in 1..<(2 * degree) { |
| 114 | + for columnCount in 1..<degree / 2 { |
| 115 | + let dimensions = try MatrixDimensions((rowCount, columnCount)) |
| 116 | + let encodeValues: [[Scheme.Scalar]] = increasingData( |
| 117 | + dimensions: dimensions, |
| 118 | + modulus: plaintextModulus) |
| 119 | + |
| 120 | + let plaintextMatrix = try PlaintextMatrix<Scheme, Coeff>( |
| 121 | + context: context, |
| 122 | + dimensions: dimensions, |
| 123 | + packing: .denseRow, |
| 124 | + values: encodeValues.flatMap { $0 }) |
| 125 | + let secretKey = try context.generateSecretKey() |
| 126 | + let ciphertextMatrix: CiphertextMatrix = try plaintextMatrix.encrypt(using: secretKey) |
| 127 | + |
| 128 | + let evaluationKeyConfig = try CiphertextMatrix<Scheme, Coeff>.extractDenseRowConfig( |
| 129 | + for: encryptionParameters, |
| 130 | + dimensions: dimensions) |
| 131 | + let evaluationKey = try context.generateEvaluationKey( |
| 132 | + config: evaluationKeyConfig, |
| 133 | + using: secretKey) |
| 134 | + |
| 135 | + for rowIndex in 0..<rowCount { |
| 136 | + let extractedRow = try ciphertextMatrix.extractDenseRow( |
| 137 | + rowIndex: rowIndex, |
| 138 | + evaluationKey: evaluationKey) |
| 139 | + |
| 140 | + let expectedDimensions = try MatrixDimensions( |
| 141 | + rowCount: 1, |
| 142 | + columnCount: columnCount) |
| 143 | + #expect(extractedRow.dimensions == expectedDimensions) |
| 144 | + |
| 145 | + // Check unpacking |
| 146 | + let decrypted = try extractedRow.decrypt(using: secretKey) |
| 147 | + let unpacked: [Scheme.Scalar] = try decrypted.unpack() |
| 148 | + #expect(unpacked == encodeValues[rowIndex]) |
| 149 | + |
| 150 | + // Check encoded values |
| 151 | + var row = encodeValues[rowIndex] |
| 152 | + row += Array(repeating: 0, count: row.count.nextPowerOfTwo - row.count) |
| 153 | + let expectedRow = Array(repeating: row, count: degree / row.count).flatMap { $0 } |
| 154 | + let decoded: [Scheme.Scalar] = try decrypted.plaintexts[0].decode(format: .simd) |
| 155 | + #expect(decoded == expectedRow) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments