Skip to content

Commit 192e5bc

Browse files
committed
Some more spanification
1 parent f845d4a commit 192e5bc

File tree

9 files changed

+40
-44
lines changed

9 files changed

+40
-44
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
api_breakage_check_enabled: false
2626
# swift:6.2-noble leads to issues with Snippets
2727
# e.g. https://github.com/apple/swift-homomorphic-encryption/actions/runs/18144503507/job/51643132814#step:5:1087
28-
docs_check_container_image: swift:6.1-noble
28+
docs_check_container_image: swift:6.2.2-noble
2929
format_check_enabled: false
3030
tests:
3131
name: swifttests

.spi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ builder:
1212
- PNNSProcessDatabase
1313
- PrivateInformationRetrieval
1414
- PrivateNearestNeighborSearch
15-
swift_version: 6.0
15+
swift_version: 6.2

.swiftformat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
--nospaceoperators ..<, ...
99
--ranges no-space
1010
--self init-only
11-
--swiftversion 6.0
11+
--swiftversion 6.2

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,6 @@ if enableDocCPlugin {
341341
// Set the minimum macOS version for the package
342342
#if canImport(Darwin)
343343
package.platforms = [
344-
.macOS(.v26), // Constrained by UInt128 support
344+
.macOS(.v26), // Constrained by use of Span
345345
]
346346
#endif

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ We'd like this package to quickly embrace Swift language and toolchain improveme
155155
Developing Swift Homomorphic Encryption requires:
156156
* [Nick Lockwood SwiftFormat](https://github.com/nicklockwood/SwiftFormat), 0.58.6
157157
* [pre-commit](https://pre-commit.com)
158-
* [swift-format](https://github.com/swiftlang/swift-format), 600.0.0
158+
* [swift-format](https://github.com/swiftlang/swift-format), 602.0.0
159159
* [swift-protobuf](https://github.com/apple/swift-protobuf), 1.31.1
160160
* [SwiftLint](https://github.com/realm/SwiftLint), 0.62.2
161161

Sources/HomomorphicEncryption/Array2d.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,35 +76,36 @@ public struct Array2d<T: Equatable & AdditiveArithmetic & Sendable>: Equatable,
7676
columnCount: columnCount)
7777
}
7878

79-
/// Provides scoped access to the underlying buffer storing the array's data.
79+
/// Provides scoped access to the underlying buffer storing the array's data using a Span.
8080
///
8181
/// Use this method when you need temporary read-only access to the array's contiguous storage.
82-
/// The buffer pointer is only valid for the duration of the closure's execution.
82+
/// The Span is only valid for the duration of the closure's execution.
8383
///
84-
/// - Parameter body: A closure that takes an `UnsafeBufferPointer` to the array's data.
85-
/// The buffer pointer argument is valid only for the duration of the closure's execution.
84+
/// - Parameter body: A closure that takes a `Span<T>` to the array's data.
85+
/// The Span argument is valid only for the duration of the closure's execution.
8686
/// - Returns: The return value of the `body` closure.
8787
/// - Throws: Rethrows any error thrown by the `body` closure.
88-
public func withUnsafeData<Return>(_ body: (UnsafeBufferPointer<T>) throws -> Return) rethrows -> Return {
89-
try data.withUnsafeBufferPointer { pointer in
90-
try body(pointer)
91-
}
88+
@inlinable
89+
public func withDataSpan<Return>(_ body: (Span<T>) throws -> Return) rethrows -> Return {
90+
try body(data.span)
9291
}
9392

9493
/// Provides scoped access to the underlying buffer storing the array's data for mutation.
9594
///
9695
/// Use this method when you need temporary read-write access to the array's contiguous storage.
97-
/// The buffer pointer is only valid for the duration of the closure's execution.
96+
/// The MutableSpan is only valid for the duration of the closure's execution.
9897
///
99-
/// - Parameter body: A closure that takes an `UnsafeMutableBufferPointer` to the array's data.
100-
/// The buffer pointer argument is valid only for the duration of the closure's execution.
98+
/// - Parameter body: A closure that takes a `MutableSpan<T>` to the array's data.
99+
/// The MutableSpan argument is valid only for the duration of the closure's execution.
101100
/// - Returns: The return value of the `body` closure.
102101
/// - Throws: Rethrows any error thrown by the `body` closure.
103-
public mutating func withUnsafeMutableData<Return>(_ body: (UnsafeMutableBufferPointer<T>) throws
104-
-> Return) rethrows -> Return
102+
@inlinable
103+
public mutating func withMutableDataSpan<Return>(_ body: (inout MutableSpan<T>) throws -> Return) rethrows
104+
-> Return
105105
{
106-
try data.withUnsafeMutableBufferPointer { pointer in
107-
try body(pointer)
106+
try data.withUnsafeMutableBufferPointer { buffer in
107+
var span = buffer.mutableSpan
108+
return try body(&span)
108109
}
109110
}
110111
}

Sources/HomomorphicEncryption/CrtComposer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public struct _CrtComposer<T: ScalarType>: Sendable {
8787
for column in 0..<data.columnCount {
8888
let tmp = V(inversePuncturedProduct.multiplyMod(data[
8989
row,
90-
column
90+
column,
9191
]))
9292
let addend = tmp &* puncturedProduct
9393
products[column] = products[column].addMod(addend, modulus: q)

Sources/HomomorphicEncryption/PolyRq/PolyRq.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ extension PolyRq {
148148
lhs.validateMetadataEquality(with: rhs)
149149

150150
var lhsData = lhs.data.data.mutableSpan
151-
var rhsData = rhs.data.data.span
151+
let rhsData = rhs.data.data.span
152152
for (rnsIndex, modulus) in rhs.moduli.enumerated() {
153153
for index in rhs.polyIndices(rnsIndex: rnsIndex) {
154154
lhsData[index] = lhsData[index].addMod(rhsData[index], modulus: modulus)
@@ -298,16 +298,13 @@ extension PolyRq {
298298
@inlinable
299299
public static prefix func - (_ poly: Self) -> Self {
300300
var result = Self.zero(context: poly.context)
301-
result.data.data.withUnsafeMutableBufferPointer { resultData in
302-
poly.data.data.withUnsafeBufferPointer { rhsData in
303-
for (rnsIndex, modulus) in poly.moduli.enumerated() {
304-
for index in poly.polyIndices(rnsIndex: rnsIndex) {
305-
resultData[index] = rhsData[index].negateMod(modulus: modulus)
306-
}
307-
}
301+
var resultData = result.data.data.mutableSpan
302+
let rhsData = poly.data.data.span
303+
for (rnsIndex, modulus) in poly.moduli.enumerated() {
304+
for index in poly.polyIndices(rnsIndex: rnsIndex) {
305+
resultData[index] = rhsData[index].negateMod(modulus: modulus)
308306
}
309307
}
310-
311308
return result
312309
}
313310

Tests/HomomorphicEncryptionTests/Array2dTests.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,27 @@ struct Array2dTests {
186186
}
187187

188188
@Test
189-
func withUnsafeData() {
189+
func withDataSpan() {
190190
let data = [Int](0..<32)
191191
let array = Array2d(data: data, rowCount: 4, columnCount: 8)
192-
array.withUnsafeData { dataPointer in
193-
array.data.withUnsafeBufferPointer { expectedDataPointer in
194-
#expect(dataPointer.baseAddress == expectedDataPointer.baseAddress)
192+
var sum = 0
193+
array.withDataSpan { span in
194+
for index in span.indices {
195+
sum += span[index]
195196
}
196197
}
198+
#expect(sum == array.data.sum())
197199
}
198200

199201
@Test
200-
func withUnsafeMutableData() throws {
202+
func withMutableDataSpan() {
201203
let data = [Int](0..<32)
202204
var array = Array2d(data: data, rowCount: 4, columnCount: 8)
203-
// For the comparison we need 'mutable' pointers of the same type.
204-
// But, `withUnsafe*` methods need exclusive ownership of the pointer.
205-
let expectedBaseAddress = try #require(
206-
array.data.withUnsafeMutableBufferPointer { buffer in
207-
buffer.baseAddress
208-
},
209-
"Expected a valid base address")
210-
array.withUnsafeMutableData { dataPointer in
211-
#expect(dataPointer.baseAddress == expectedBaseAddress)
205+
array.withMutableDataSpan { span in
206+
for index in span.indices {
207+
span[index] += 1
208+
}
212209
}
210+
#expect(array.data == [Int](1..<33))
213211
}
214212
}

0 commit comments

Comments
 (0)