|
| 1 | +// Copyright 2024-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 | + |
| 17 | +extension HeScheme { |
| 18 | + /// Rotates the columns of a ciphertext throw multiple steps in async way. |
| 19 | + /// - seealso: `HeScheme/rotateColumnsAsync(of:by:using:)` for the single-step API. |
| 20 | + @inlinable |
| 21 | + public static func rotateColumnsMultiStepAsync( |
| 22 | + of ciphertext: inout CanonicalCiphertext, |
| 23 | + by step: Int, |
| 24 | + using evaluationKey: EvaluationKey) async throws |
| 25 | + { |
| 26 | + if step == 0 { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + guard let galoisKey = evaluationKey.galoisKey else { |
| 31 | + throw HeError.missingGaloisKey |
| 32 | + } |
| 33 | + |
| 34 | + // Short-circuit to single rotation if possible. |
| 35 | + let degree = ciphertext.degree |
| 36 | + let galoisElement = try GaloisElement.rotatingColumns(by: step, degree: degree) |
| 37 | + if galoisKey.keys.keys.contains(galoisElement) { |
| 38 | + try await rotateColumnsAsync(of: &ciphertext, by: step, using: evaluationKey) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + let galoisElements = Array(galoisKey.keys.keys) |
| 43 | + let steps = GaloisElement.stepsFor(elements: galoisElements, degree: degree).values.compactMap(\.self) |
| 44 | + |
| 45 | + let positiveStep = if step < 0 { |
| 46 | + step + degree / 2 |
| 47 | + } else { |
| 48 | + step |
| 49 | + } |
| 50 | + |
| 51 | + let plan = try GaloisElement.planMultiStep(supportedSteps: steps, step: positiveStep, degree: degree) |
| 52 | + guard let plan else { |
| 53 | + throw HeError.invalidRotationStep(step: step, degree: degree) |
| 54 | + } |
| 55 | + for (step, count) in plan { |
| 56 | + for _ in 0..<count { |
| 57 | + try await rotateColumnsAsync(of: &ciphertext, by: step, using: evaluationKey) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Rotates the columns of a ciphertext through one or more rotations. |
| 63 | + /// - seealso: `HeScheme/rotateColumns(of:by:using:)` for the single-step API. |
| 64 | + @inlinable |
| 65 | + public static func rotateColumnsMultiStep( |
| 66 | + of ciphertext: inout CanonicalCiphertext, |
| 67 | + by step: Int, |
| 68 | + using evaluationKey: EvaluationKey) throws |
| 69 | + { |
| 70 | + if step == 0 { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + guard let galoisKey = evaluationKey.galoisKey else { |
| 75 | + throw HeError.missingGaloisKey |
| 76 | + } |
| 77 | + |
| 78 | + // Short-circuit to single rotation if possible. |
| 79 | + let degree = ciphertext.degree |
| 80 | + let galoisElement = try GaloisElement.rotatingColumns(by: step, degree: degree) |
| 81 | + if galoisKey.keys.keys.contains(galoisElement) { |
| 82 | + try ciphertext.rotateColumns(by: step, using: evaluationKey) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + let galoisElements = Array(galoisKey.keys.keys) |
| 87 | + let steps = GaloisElement.stepsFor(elements: galoisElements, degree: degree).values.compactMap(\.self) |
| 88 | + |
| 89 | + let positiveStep = if step < 0 { |
| 90 | + step + degree / 2 |
| 91 | + } else { |
| 92 | + step |
| 93 | + } |
| 94 | + |
| 95 | + let plan = try GaloisElement.planMultiStep(supportedSteps: steps, step: positiveStep, degree: degree) |
| 96 | + guard let plan else { |
| 97 | + throw HeError.invalidRotationStep(step: step, degree: degree) |
| 98 | + } |
| 99 | + for (step, count) in plan { |
| 100 | + try (0..<count).forEach { _ in try ciphertext.rotateColumns(by: step, using: evaluationKey) } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// Sum up an array of ciphertexts after rotate their columns one-by-one. |
| 105 | + /// |
| 106 | + /// The i-th (starting from 0) ciphertext is rotated by i * `step` steps before adding up. |
| 107 | + /// - Parameters: |
| 108 | + /// - ciphertexts: ciphertexts to be added up. |
| 109 | + /// - step: the rotation steps for each ciphertext. |
| 110 | + /// - evaluationKey: the evaluation key for rotation. |
| 111 | + /// - Throws: Error upon failure to compute the inverse. |
| 112 | + @inlinable |
| 113 | + public static func rotateColumnsAndSumAsync( |
| 114 | + _ ciphertexts: consuming [CanonicalCiphertext], |
| 115 | + by step: Int, |
| 116 | + using evaluationKey: EvaluationKey) async throws -> CanonicalCiphertext |
| 117 | + { |
| 118 | + guard var accumulator = ciphertexts.popLast() else { |
| 119 | + preconditionFailure("No ciphertexts to sum up.") |
| 120 | + } |
| 121 | + if ciphertexts.isEmpty { |
| 122 | + return accumulator |
| 123 | + } |
| 124 | + |
| 125 | + for ciphertext in ciphertexts.reversed() { |
| 126 | + try await rotateColumnsMultiStepAsync( |
| 127 | + of: &accumulator, |
| 128 | + by: step, |
| 129 | + using: evaluationKey) |
| 130 | + try await addAssignAsync(&accumulator, ciphertext) |
| 131 | + } |
| 132 | + return accumulator |
| 133 | + } |
| 134 | + |
| 135 | + /// Sum up two ciphertexts after swap the row of second one. |
| 136 | + /// |
| 137 | + /// - Parameters: |
| 138 | + /// - ciphertexts: ciphertexts to be added up. |
| 139 | + /// - step: the rotation steps for each ciphertext. |
| 140 | + /// - evaluationKey: the evaluation key for rotation. |
| 141 | + /// - Throws: Error upon failure to compute the inverse. |
| 142 | + @inlinable |
| 143 | + public static func swapRowsAndAddAsync( |
| 144 | + swapping ciphertext0: consuming CanonicalCiphertext, |
| 145 | + addingTo ciphertext1: consuming CanonicalCiphertext, |
| 146 | + using evaluationKey: EvaluationKey) async throws -> CanonicalCiphertext |
| 147 | + { |
| 148 | + try await swapRowsAsync(of: &ciphertext0, using: evaluationKey) |
| 149 | + try await addAssignAsync(&ciphertext0, ciphertext1) |
| 150 | + return ciphertext0 |
| 151 | + } |
| 152 | +} |
0 commit comments