|
| 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 | +/// The functions in this extension are the default implementation of async methods of HE scheme. |
| 16 | +extension HeScheme { |
| 17 | + // swiftlint:disable missing_docs |
| 18 | + @inlinable |
| 19 | + public static func rotateColumnsAsync( |
| 20 | + of ciphertext: inout CanonicalCiphertext, |
| 21 | + by step: Int, |
| 22 | + using evaluationKey: EvaluationKey) async throws |
| 23 | + { |
| 24 | + try rotateColumns(of: &ciphertext, by: step, using: evaluationKey) |
| 25 | + } |
| 26 | + |
| 27 | + @inlinable |
| 28 | + public static func swapRowsAsync( |
| 29 | + of ciphertext: inout CanonicalCiphertext, |
| 30 | + using evaluationKey: EvaluationKey) async throws |
| 31 | + { |
| 32 | + try swapRows(of: &ciphertext, using: evaluationKey) |
| 33 | + } |
| 34 | + |
| 35 | + @inlinable |
| 36 | + public static func addAssignAsync(_ lhs: inout CoeffPlaintext, _ rhs: CoeffPlaintext) async throws { |
| 37 | + try addAssign(&lhs, rhs) |
| 38 | + } |
| 39 | + |
| 40 | + @inlinable |
| 41 | + public static func addAssignAsync(_ lhs: inout EvalPlaintext, _ rhs: EvalPlaintext) async throws { |
| 42 | + try addAssign(&lhs, rhs) |
| 43 | + } |
| 44 | + |
| 45 | + @inlinable |
| 46 | + public static func addAssignCoeffAsync(_ lhs: inout CoeffCiphertext, _ rhs: CoeffCiphertext) async throws { |
| 47 | + try addAssignCoeff(&lhs, rhs) |
| 48 | + } |
| 49 | + |
| 50 | + @inlinable |
| 51 | + public static func addAssignEvalAsync(_ lhs: inout EvalCiphertext, _ rhs: EvalCiphertext) async throws { |
| 52 | + try addAssignEval(&lhs, rhs) |
| 53 | + } |
| 54 | + |
| 55 | + @inlinable |
| 56 | + public static func subAssignCoeffAsync(_ lhs: inout CoeffCiphertext, _ rhs: CoeffCiphertext) async throws { |
| 57 | + try subAssignCoeff(&lhs, rhs) |
| 58 | + } |
| 59 | + |
| 60 | + @inlinable |
| 61 | + public static func subAssignEvalAsync(_ lhs: inout EvalCiphertext, _ rhs: EvalCiphertext) async throws { |
| 62 | + try subAssignEval(&lhs, rhs) |
| 63 | + } |
| 64 | + |
| 65 | + @inlinable |
| 66 | + public static func addAssignCoeffAsync( |
| 67 | + _ ciphertext: inout CoeffCiphertext, |
| 68 | + _ plaintext: CoeffPlaintext) async throws |
| 69 | + { |
| 70 | + try addAssignCoeff(&ciphertext, plaintext) |
| 71 | + } |
| 72 | + |
| 73 | + @inlinable |
| 74 | + public static func addAssignEvalAsync(_ ciphertext: inout EvalCiphertext, _ plaintext: EvalPlaintext) async throws { |
| 75 | + try addAssignEval(&ciphertext, plaintext) |
| 76 | + } |
| 77 | + |
| 78 | + @inlinable |
| 79 | + public static func subAssignCoeffAsync( |
| 80 | + _ ciphertext: inout CoeffCiphertext, |
| 81 | + _ plaintext: CoeffPlaintext) async throws |
| 82 | + { |
| 83 | + try subAssignCoeff(&ciphertext, plaintext) |
| 84 | + } |
| 85 | + |
| 86 | + @inlinable |
| 87 | + public static func subAssignEvalAsync(_ ciphertext: inout EvalCiphertext, _ plaintext: EvalPlaintext) async throws { |
| 88 | + try subAssignEval(&ciphertext, plaintext) |
| 89 | + } |
| 90 | + |
| 91 | + @inlinable |
| 92 | + public static func subCoeffAsync(_ plaintext: CoeffPlaintext, |
| 93 | + _ ciphertext: CoeffCiphertext) async throws -> CoeffCiphertext |
| 94 | + { |
| 95 | + try subCoeff(plaintext, ciphertext) |
| 96 | + } |
| 97 | + |
| 98 | + @inlinable |
| 99 | + public static func subEvalAsync(_ plaintext: EvalPlaintext, |
| 100 | + _ ciphertext: EvalCiphertext) async throws -> EvalCiphertext |
| 101 | + { |
| 102 | + try subEval(plaintext, ciphertext) |
| 103 | + } |
| 104 | + |
| 105 | + @inlinable |
| 106 | + public static func mulAssignAsync(_ ciphertext: inout EvalCiphertext, _ plaintext: EvalPlaintext) async throws { |
| 107 | + try mulAssign(&ciphertext, plaintext) |
| 108 | + } |
| 109 | + |
| 110 | + @inlinable |
| 111 | + public static func negAssignCoeffAsync(_ ciphertext: inout CoeffCiphertext) async { |
| 112 | + negAssign(&ciphertext) |
| 113 | + } |
| 114 | + |
| 115 | + @inlinable |
| 116 | + public static func negAssignEvalAsync(_ ciphertext: inout EvalCiphertext) async { |
| 117 | + negAssign(&ciphertext) |
| 118 | + } |
| 119 | + |
| 120 | + @inlinable |
| 121 | + public static func innerProductAsync( |
| 122 | + _ lhs: some Collection<CanonicalCiphertext>, |
| 123 | + _ rhs: some Collection<CanonicalCiphertext>) async throws |
| 124 | + -> CanonicalCiphertext |
| 125 | + { |
| 126 | + try innerProduct(lhs, rhs) |
| 127 | + } |
| 128 | + |
| 129 | + @inlinable |
| 130 | + public static func innerProductAsync(ciphertexts: some Collection<EvalCiphertext>, |
| 131 | + plaintexts: some Collection<EvalPlaintext>) async throws -> EvalCiphertext |
| 132 | + { |
| 133 | + try innerProduct(ciphertexts: ciphertexts, plaintexts: plaintexts) |
| 134 | + } |
| 135 | + |
| 136 | + @inlinable |
| 137 | + public static func innerProductAsync(ciphertexts: some Collection<EvalCiphertext>, |
| 138 | + plaintexts: some Collection<EvalPlaintext?>) async throws -> EvalCiphertext |
| 139 | + { |
| 140 | + try innerProduct(ciphertexts: ciphertexts, plaintexts: plaintexts) |
| 141 | + } |
| 142 | + |
| 143 | + @inlinable |
| 144 | + public static func mulAssignAsync(_ lhs: inout CanonicalCiphertext, _ rhs: CanonicalCiphertext) async throws { |
| 145 | + try mulAssign(&lhs, rhs) |
| 146 | + } |
| 147 | + |
| 148 | + @inlinable |
| 149 | + public static func addAssignAsync(_ lhs: inout CanonicalCiphertext, _ rhs: CanonicalCiphertext) async throws { |
| 150 | + try addAssign(&lhs, rhs) |
| 151 | + } |
| 152 | + |
| 153 | + @inlinable |
| 154 | + public static func subAssignAsync(_ lhs: inout CanonicalCiphertext, _ rhs: CanonicalCiphertext) async throws { |
| 155 | + try subAssign(&lhs, rhs) |
| 156 | + } |
| 157 | + |
| 158 | + @inlinable |
| 159 | + public static func subAssignAsync( |
| 160 | + _ ciphertext: inout CanonicalCiphertext, |
| 161 | + _ plaintext: CoeffPlaintext) async throws |
| 162 | + { |
| 163 | + try subAssign(&ciphertext, plaintext) |
| 164 | + } |
| 165 | + |
| 166 | + @inlinable |
| 167 | + public static func subAssignAsync(_ ciphertext: inout CanonicalCiphertext, |
| 168 | + _ plaintext: EvalPlaintext) async throws |
| 169 | + { |
| 170 | + try subAssign(&ciphertext, plaintext) |
| 171 | + } |
| 172 | + |
| 173 | + @inlinable |
| 174 | + public static func subAsync(_ plaintext: CoeffPlaintext, |
| 175 | + _ ciphertext: CanonicalCiphertext) async throws -> CanonicalCiphertext |
| 176 | + { |
| 177 | + try sub(plaintext, ciphertext) |
| 178 | + } |
| 179 | + |
| 180 | + @inlinable |
| 181 | + public static func subAsync(_ plaintext: EvalPlaintext, |
| 182 | + _ ciphertext: CanonicalCiphertext) async throws -> CanonicalCiphertext |
| 183 | + { |
| 184 | + try sub(plaintext, ciphertext) |
| 185 | + } |
| 186 | + |
| 187 | + @inlinable |
| 188 | + public static func modSwitchDownAsync(_ ciphertext: inout CanonicalCiphertext) async throws { |
| 189 | + try modSwitchDown(&ciphertext) |
| 190 | + } |
| 191 | + |
| 192 | + @inlinable |
| 193 | + public static func applyGaloisAsync( |
| 194 | + ciphertext: inout CanonicalCiphertext, |
| 195 | + element: Int, |
| 196 | + using key: EvaluationKey) async throws |
| 197 | + { |
| 198 | + try applyGalois(ciphertext: &ciphertext, element: element, using: key) |
| 199 | + } |
| 200 | + |
| 201 | + @inlinable |
| 202 | + public static func relinearizeAsync(_ ciphertext: inout CanonicalCiphertext, |
| 203 | + using key: EvaluationKey) async throws |
| 204 | + { |
| 205 | + try relinearize(&ciphertext, using: key) |
| 206 | + } |
| 207 | + |
| 208 | + @inlinable |
| 209 | + public static func forwardNttAsync(_ ciphertext: CoeffCiphertext) async throws -> EvalCiphertext { |
| 210 | + try forwardNtt(ciphertext) |
| 211 | + } |
| 212 | + |
| 213 | + @inlinable |
| 214 | + public static func inverseNttAsync(_ ciphertext: EvalCiphertext) async throws -> CoeffCiphertext { |
| 215 | + try inverseNtt(ciphertext) |
| 216 | + } |
| 217 | + |
| 218 | + @inlinable |
| 219 | + public static func modSwitchDownToSingleAsync(_ ciphertext: inout CanonicalCiphertext) async throws { |
| 220 | + try modSwitchDownToSingle(&ciphertext) |
| 221 | + } |
| 222 | + |
| 223 | + @inlinable |
| 224 | + public static func multiplyInversePowerOfXAsync(_ ciphertext: inout CoeffCiphertext, power: Int) async throws { |
| 225 | + try multiplyInversePowerOfX(&ciphertext, power: power) |
| 226 | + } |
| 227 | + // swiftlint:enable missing_docs |
| 228 | +} |
0 commit comments