From d79ef14ca8eb1b064cf3093d51e32e30f2b9012f Mon Sep 17 00:00:00 2001 From: Fabian Boemer Date: Thu, 3 Oct 2024 14:08:19 -0700 Subject: [PATCH] Revert 'Adopt Swift 6' --- .github/workflows/ci.yml | 2 +- Package.resolved | 6 +++--- Package.swift | 10 +++++----- README.md | 4 ++-- .../HomomorphicEncryption/DoubleWidthUInt.swift | 1 + .../UsingSwiftHomomorphicEncryption.md | 2 +- .../PolyRq/PolyRq+Randomize.swift | 6 +++--- .../Random/NistCtrDrbg.swift | 10 ++-------- Sources/HomomorphicEncryption/Scalar.swift | 4 ++-- Sources/HomomorphicEncryption/Util.swift | 15 +++++++++++++++ .../MessageExtensions.swift | 2 +- .../HomomorphicEncryptionTests/Array2dTests.swift | 4 ++-- 12 files changed, 38 insertions(+), 28 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9e823c98..1ed8f5de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,7 @@ jobs: strategy: fail-fast: false matrix: - swift: ["6.0"] + swift: ["5.10", "6.0"] name: swift ${{ matrix.swift }} tests steps: - uses: slashmo/install-swift@v0.4.0 diff --git a/Package.resolved b/Package.resolved index 8a26fe6d..5d5fc16d 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "35058120a76c9add338a72688983f31eb5403042763150a08ca6e53652580567", + "originHash" : "8fa2cd62efd12ea7c4d07fe29cae38bf3817c0818b3db7d057d5cde48d63b525", "pins" : [ { "identity" : "hdrhistogram-swift", @@ -105,8 +105,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-protobuf", "state" : { - "revision" : "edb6ed4919f7756157fe02f2552b7e3850a538e5", - "version" : "1.28.1" + "revision" : "ebc7251dd5b37f627c93698e4374084d98409633", + "version" : "1.28.2" } }, { diff --git a/Package.swift b/Package.swift index 0f1c451f..6abfbd2b 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 6.0 +// swift-tools-version: 5.10 // The swift-tools-version declares the minimum version of Swift required to build this package. // Remember to update CI if changing @@ -17,7 +17,9 @@ // limitations under the License. import PackageDescription -let librarySettings: [SwiftSetting] = [] +let librarySettings: [SwiftSetting] = [ + .enableExperimentalFeature("StrictConcurrency"), +] let executableSettings: [SwiftSetting] = librarySettings + @@ -271,9 +273,7 @@ package.targets += [ ]), ] -#if canImport(Darwin) // Set the minimum macOS version for the package package.platforms = [ - .macOS(.v15), // Constrained by Swift 6 support for Xcode (https://developer.apple.com/support/xcode/) + .macOS(.v14), // Constrained by Swift 5.10 support for Xcode (https://developer.apple.com/support/xcode/) ] -#endif diff --git a/README.md b/README.md index 709912b2..1c328b1b 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ The PNNS implementation in Swift Homomorphic Encryption uses homomorphic encrypt Swift Homomorphic Encryption requires: * 64-bit processor with little-endian memory representation * macOS or Linux operating system -* [Swift](https://www.swift.org/) version 6.0 or later +* [Swift](https://www.swift.org/) version 5.10 or later > [!NOTE] > Swift Homomorphic Encryption relies on [SystemRandomNumberGenerator](https://developer.apple.com/documentation/swift/systemrandomnumbergenerator) as a cryptographically secure random number generator, which may have platform-dependent behavior. @@ -134,7 +134,7 @@ swift run -c release EncryptionParametersSnippet ## Developing Swift Homomorphic Encryption ### Dependencies Building Swift Homomorphic Encryption requires: -* [Swift](https://www.swift.org/) version 6.0 or later +* [Swift](https://www.swift.org/) version 5.10 or later Additionally, developing Swift Homomorphic Encryption requires: * [Nick Lockwood SwiftFormat](https://github.com/nicklockwood/SwiftFormat), v0.54.0 diff --git a/Sources/HomomorphicEncryption/DoubleWidthUInt.swift b/Sources/HomomorphicEncryption/DoubleWidthUInt.swift index f9432c27..374e337b 100644 --- a/Sources/HomomorphicEncryption/DoubleWidthUInt.swift +++ b/Sources/HomomorphicEncryption/DoubleWidthUInt.swift @@ -717,5 +717,6 @@ extension DoubleWidthUInt: UnsignedInteger where Base: FixedWidthInteger & Unsig } } +@usableFromInline typealias DWUInt128 = DoubleWidthUInt @usableFromInline typealias QuadWidth = DoubleWidthUInt> @usableFromInline typealias OctoWidth = DoubleWidthUInt> diff --git a/Sources/HomomorphicEncryption/HomomorphicEncryption.docc/UsingSwiftHomomorphicEncryption.md b/Sources/HomomorphicEncryption/HomomorphicEncryption.docc/UsingSwiftHomomorphicEncryption.md index af4d1faa..b2923ded 100644 --- a/Sources/HomomorphicEncryption/HomomorphicEncryption.docc/UsingSwiftHomomorphicEncryption.md +++ b/Sources/HomomorphicEncryption/HomomorphicEncryption.docc/UsingSwiftHomomorphicEncryption.md @@ -6,7 +6,7 @@ Get started using Swift Homomorphic Encryption. Swift Homomorphic Encryption requires: * 64-bit processor with little-endian memory representation * macOS or Linux operating system -* [Swift](https://www.swift.org/) version 6.0 or later +* [Swift](https://www.swift.org/) version 5.10 or later > Note: Swift Homomorphic Encryption relies on [SystemRandomNumberGenerator](https://developer.apple.com/documentation/swift/systemrandomnumbergenerator) as a cryptographically secure random number generator, which may have platform-dependent behavior. diff --git a/Sources/HomomorphicEncryption/PolyRq/PolyRq+Randomize.swift b/Sources/HomomorphicEncryption/PolyRq/PolyRq+Randomize.swift index 739c3326..af85e06c 100644 --- a/Sources/HomomorphicEncryption/PolyRq/PolyRq+Randomize.swift +++ b/Sources/HomomorphicEncryption/PolyRq/PolyRq+Randomize.swift @@ -53,7 +53,7 @@ extension PolyRq { @inlinable public mutating func randomizeUniform(using rng: inout some PseudoRandomNumberGenerator) { let chunkCount = min(degree, 1024) - let uint128ByteCount = MemoryLayout.size + let uint128ByteCount = MemoryLayout.size var randomBytes: [UInt8] = .init(repeating: 0, count: chunkCount * uint128ByteCount) // We can sample directly in Coeff or Eval domain for (rnsIndex, reduceModulus) in context.reduceModuliUInt64.enumerated() { @@ -63,7 +63,7 @@ extension PolyRq { for index in 0.. public typealias SignedScalar = Int64 public static var rnsCorrectionFactor: UInt64 { @@ -161,7 +161,7 @@ extension UInt64: DoubleWidthType { public typealias Scalar = UInt32 } -extension UInt128: DoubleWidthType { +extension DWUInt128: DoubleWidthType { /// Single-width scalar, with bit-width half that of the ``DoubleWidthType``. public typealias Scalar = UInt64 } diff --git a/Sources/HomomorphicEncryption/Util.swift b/Sources/HomomorphicEncryption/Util.swift index 5a3dd1ad..9355ab2a 100644 --- a/Sources/HomomorphicEncryption/Util.swift +++ b/Sources/HomomorphicEncryption/Util.swift @@ -25,6 +25,21 @@ extension Sequence where Element: Hashable { } } +// https://github.com/swiftlang/swift-evolution/blob/main/proposals/0220-count-where.md +// introduced in swift 6 +#if swift(<6.0) +extension Sequence { + @inlinable + package func count(where predicate: (Element) throws -> Bool) rethrows -> Int { + var count = 0 + for element in self where try predicate(element) { + count += 1 + } + return count + } +} +#endif + extension FixedWidthInteger { // not a constant time operation @inlinable diff --git a/Sources/HomomorphicEncryptionProtobuf/MessageExtensions.swift b/Sources/HomomorphicEncryptionProtobuf/MessageExtensions.swift index d7f39784..884899be 100644 --- a/Sources/HomomorphicEncryptionProtobuf/MessageExtensions.swift +++ b/Sources/HomomorphicEncryptionProtobuf/MessageExtensions.swift @@ -24,7 +24,7 @@ extension Message { /// - Throws: Error upon failure to initialize message. public init(from path: String) throws { if path.hasSuffix(".txtpb") { - try self.init(textFormatString: String(contentsOfFile: path, encoding: .utf8)) + try self.init(textFormatString: String(contentsOfFile: path)) } else { let serializedData = try Data(contentsOf: URL(fileURLWithPath: path)) try self.init(serializedBytes: serializedData) diff --git a/Tests/HomomorphicEncryptionTests/Array2dTests.swift b/Tests/HomomorphicEncryptionTests/Array2dTests.swift index 60e1b04d..4e34397c 100644 --- a/Tests/HomomorphicEncryptionTests/Array2dTests.swift +++ b/Tests/HomomorphicEncryptionTests/Array2dTests.swift @@ -33,7 +33,7 @@ class Array2dTests: XCTestCase { runTest(Int32.self) runTest(Int64.self) runTest(UInt64.self) - runTest(UInt128.self) + runTest(DWUInt128.self) } func testZeroAndZeroize() { @@ -54,7 +54,7 @@ class Array2dTests: XCTestCase { runTest(Int32.self) runTest(Int64.self) runTest(UInt64.self) - runTest(UInt128.self) + runTest(DWUInt128.self) } func testShape() {