Skip to content

Commit

Permalink
Adopt Swift 6 (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
fboemer authored Sep 13, 2024
1 parent 45bdb74 commit 8e6bef0
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
strategy:
fail-fast: false
matrix:
swift: [ "5.10", "swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-06-a" ]
swift: ["swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-06-a" ]
name: swift ${{ matrix.swift }} tests
steps:
- uses: slashmo/install-swift@v0.4.0
Expand Down
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "287ba43d965ab226c778e6332f9fbb9244920f2452fbe9bccef6f422c30d7e49",
"originHash" : "924e29f4ba35b567344548cae01b03bc8f2c3a884de7a16bcd7dc8475e890dfb",
"pins" : [
{
"identity" : "hdrhistogram-swift",
Expand Down
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.10
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
// Remember to update CI if changing

Expand All @@ -17,9 +17,7 @@
// limitations under the License.
import PackageDescription

let librarySettings: [SwiftSetting] = [
.enableExperimentalFeature("StrictConcurrency"),
]
let librarySettings: [SwiftSetting] = []

let executableSettings: [SwiftSetting] =
librarySettings +
Expand Down Expand Up @@ -273,7 +271,9 @@ package.targets += [
]),
]

#if canImport(Darwin)
// Set the minimum macOS version for the package
package.platforms = [
.macOS(.v14), // Constrained by Swift 5.10 support for Xcode (https://developer.apple.com/support/xcode/)
.macOS(.v15), // Constrained by Swift 6 support for Xcode (https://developer.apple.com/support/xcode/)
]
#endif
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,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 5.10 or later
* [Swift](https://www.swift.org/) version 6.0 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.
Expand Down Expand Up @@ -135,7 +135,7 @@ swift run -c release EncryptionParametersSnippet
## Developing Swift Homomorphic Encryption
### Dependencies
Building Swift Homomorphic Encryption requires:
* [Swift](https://www.swift.org/) version 5.10 or later
* [Swift](https://www.swift.org/) version 6.0 or later

Additionally, developing Swift Homomorphic Encryption requires:
* [Nick Lockwood SwiftFormat](https://github.com/nicklockwood/SwiftFormat), v0.54.0
Expand Down
1 change: 0 additions & 1 deletion Sources/HomomorphicEncryption/DoubleWidthUInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,5 @@ extension DoubleWidthUInt: UnsignedInteger where Base: FixedWidthInteger & Unsig
}
}

@usableFromInline typealias DWUInt128 = DoubleWidthUInt<UInt64>
@usableFromInline typealias QuadWidth<T: FixedWidthInteger & UnsignedInteger> = DoubleWidthUInt<DoubleWidthUInt<T>>
@usableFromInline typealias OctoWidth<T: FixedWidthInteger & UnsignedInteger> = DoubleWidthUInt<QuadWidth<T>>
Original file line number Diff line number Diff line change
Expand Up @@ -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 5.10 or later
* [Swift](https://www.swift.org/) version 6.0 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.
Expand Down
6 changes: 3 additions & 3 deletions Sources/HomomorphicEncryption/PolyRq/PolyRq+Randomize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extension PolyRq {
@inlinable
public mutating func randomizeUniform(using rng: inout some PseudoRandomNumberGenerator) {
let chunkCount = min(degree, 1024)
let uint128ByteCount = MemoryLayout<DWUInt128>.size
let uint128ByteCount = MemoryLayout<UInt128>.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() {
Expand All @@ -63,7 +63,7 @@ extension PolyRq {
for index in 0..<chunkCount {
// NOTE: for interoperability always ask rng for a UInt128 and reduces it
let u128 =
DWUInt128(littleEndianBytes: randomBytes[index * uint128ByteCount..<(index + 1) *
UInt128(littleEndianBytes: randomBytes[index * uint128ByteCount..<(index + 1) *
uint128ByteCount])
let u64 = reduceModulus.reduce(u128)
self[offset + index] = T(u64)
Expand Down Expand Up @@ -92,7 +92,7 @@ extension PolyRq {
// consumed for each coefficient.
let u64: UInt64 = rng.next()
let u32: UInt32 = rng.next()
let u128 = DWUInt128(u64) &<< 32 | DWUInt128(u32)
let u128 = UInt128(u64) &<< 32 | UInt128(u32)

let val = T(reductionModulus.reduce(u128))
for (index, modulus) in zip(rnsIndices(coeffIndex: coeffIndex), moduli) {
Expand Down
10 changes: 8 additions & 2 deletions Sources/HomomorphicEncryption/Random/NistCtrDrbg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@ import Foundation
@usableFromInline
package struct NistCtrDrbg {
@usableFromInline static let ReseedInterval: Int64 = 1 << 48

@usableFromInline static let MaxByteCountPerRequest: Int = 1 << 16

/// Size of AES block.
@usableFromInline static let BlockCount: Int = 16

/// Size of AES key.
@usableFromInline static let KeyCount: Int = 16

/// Size of the seed.
@usableFromInline static let SeedCount: Int = KeyCount + BlockCount

@usableFromInline var key: SymmetricKey

/// This is called `V` in the NIST specification.
@usableFromInline var nonce: DWUInt128
@usableFromInline var nonce: UInt128

@usableFromInline var reseedCounter: Int64

@usableFromInline var nonceBytes: [UInt8] {
Expand Down Expand Up @@ -70,7 +76,7 @@ package struct NistCtrDrbg {

let zeroes = [UInt8](repeating: 0, count: requestedByteCount)
let output = try AES._CTR.encrypt(zeroes, using: key, nonce: .init(nonceBytes: nonceBytes))
nonce &+= DWUInt128(requestedByteCount.dividingCeil(Self.BlockCount, variableTime: true))
nonce &+= UInt128(requestedByteCount.dividingCeil(Self.BlockCount, variableTime: true))

let additionalInput = [UInt8](repeating: 0, count: Self.SeedCount)
try ctrDrbgUpdate(providedData: additionalInput)
Expand Down
4 changes: 2 additions & 2 deletions Sources/HomomorphicEncryption/Scalar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ extension UInt32: ScalarType {
}

extension UInt64: ScalarType {
public typealias DoubleWidth = DoubleWidthUInt<UInt64>
public typealias DoubleWidth = UInt128
public typealias SignedScalar = Int64

public static var rnsCorrectionFactor: UInt64 {
Expand Down Expand Up @@ -161,7 +161,7 @@ extension UInt64: DoubleWidthType {
public typealias Scalar = UInt32
}

extension DWUInt128: DoubleWidthType {
extension UInt128: DoubleWidthType {
/// Single-width scalar, with bit-width half that of the ``DoubleWidthType``.
public typealias Scalar = UInt64
}
Expand Down
15 changes: 0 additions & 15 deletions Sources/HomomorphicEncryption/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,6 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
try self.init(textFormatString: String(contentsOfFile: path, encoding: .utf8))
} else {
let serializedData = try Data(contentsOf: URL(fileURLWithPath: path))
try self.init(serializedBytes: serializedData)
Expand Down
4 changes: 2 additions & 2 deletions Tests/HomomorphicEncryptionTests/Array2dTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Array2dTests: XCTestCase {
runTest(Int32.self)
runTest(Int64.self)
runTest(UInt64.self)
runTest(DWUInt128.self)
runTest(UInt128.self)
}

func testZeroAndZeroize() {
Expand All @@ -54,7 +54,7 @@ class Array2dTests: XCTestCase {
runTest(Int32.self)
runTest(Int64.self)
runTest(UInt64.self)
runTest(DWUInt128.self)
runTest(UInt128.self)
}

func testShape() {
Expand Down

0 comments on commit 8e6bef0

Please sign in to comment.