Skip to content

Commit 88b9c9b

Browse files
authored
Work around Swift compiler issue with consuming and TSAN (#384)
This patch works around the absence of a fix for swiftlang/swift#76186 on older Swift compilers.
1 parent 9934678 commit 88b9c9b

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

Sources/CryptoBoringWrapper/EC/EllipticCurvePoint.swift

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
6969
try self.multiply(by: rhs, on: group, context: context)
7070
}
7171

72+
// This enhancement can only be present on 6.1 or later because of the
73+
// absence of https://github.com/swiftlang/swift/pull/76186 in older
74+
// compilers.
75+
#if compiler(>=6.1)
7276
@usableFromInline
7377
package consuming func multiplying(
7478
by rhs: ArbitraryPrecisionInteger,
@@ -78,6 +82,18 @@ package struct EllipticCurvePoint: @unchecked Sendable {
7882
try self.multiply(by: rhs, on: group, context: context)
7983
return self
8084
}
85+
#else
86+
@usableFromInline
87+
package func multiplying(
88+
by rhs: ArbitraryPrecisionInteger,
89+
on group: BoringSSLEllipticCurveGroup,
90+
context: FiniteFieldArithmeticContext? = nil
91+
) throws -> EllipticCurvePoint {
92+
var `self` = self
93+
try self.multiply(by: rhs, on: group, context: context)
94+
return self
95+
}
96+
#endif
8197

8298
@usableFromInline
8399
package static func multiplying(
@@ -110,6 +126,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
110126
try self.add(rhs, on: group, context: context)
111127
}
112128

129+
// This enhancement can only be present on 6.1 or later because of the
130+
// absence of https://github.com/swiftlang/swift/pull/76186 in older
131+
// compilers.
132+
#if compiler(>=6.1)
113133
@usableFromInline
114134
package consuming func adding(
115135
_ rhs: consuming EllipticCurvePoint,
@@ -119,7 +139,23 @@ package struct EllipticCurvePoint: @unchecked Sendable {
119139
try self.add(rhs, on: group, context: context)
120140
return self
121141
}
142+
#else
143+
@usableFromInline
144+
package func adding(
145+
_ rhs: consuming EllipticCurvePoint,
146+
on group: BoringSSLEllipticCurveGroup,
147+
context: FiniteFieldArithmeticContext? = nil
148+
) throws -> EllipticCurvePoint {
149+
var `self` = self
150+
try self.add(rhs, on: group, context: context)
151+
return self
152+
}
153+
#endif
122154

155+
// This enhancement can only be present on 6.1 or later because of the
156+
// absence of https://github.com/swiftlang/swift/pull/76186 in older
157+
// compilers.
158+
#if compiler(>=6.1)
123159
@usableFromInline
124160
package static func adding(
125161
_ lhs: consuming EllipticCurvePoint,
@@ -130,6 +166,19 @@ package struct EllipticCurvePoint: @unchecked Sendable {
130166
try lhs.add(rhs, on: group, context: context)
131167
return lhs
132168
}
169+
#else
170+
@usableFromInline
171+
package static func adding(
172+
_ lhs: EllipticCurvePoint,
173+
_ rhs: EllipticCurvePoint,
174+
on group: BoringSSLEllipticCurveGroup,
175+
context: FiniteFieldArithmeticContext? = nil
176+
) throws -> EllipticCurvePoint {
177+
var lhs = lhs
178+
try lhs.add(rhs, on: group, context: context)
179+
return lhs
180+
}
181+
#endif
133182

134183
@usableFromInline
135184
package mutating func invert(
@@ -150,6 +199,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
150199
try self.invert(on: group, context: context)
151200
}
152201

202+
// This enhancement can only be present on 6.1 or later because of the
203+
// absence of https://github.com/swiftlang/swift/pull/76186 in older
204+
// compilers.
205+
#if compiler(>=6.1)
153206
@usableFromInline
154207
package consuming func inverting(
155208
on group: BoringSSLEllipticCurveGroup,
@@ -158,7 +211,22 @@ package struct EllipticCurvePoint: @unchecked Sendable {
158211
try self.invert(on: group, context: context)
159212
return self
160213
}
214+
#else
215+
@usableFromInline
216+
package func inverting(
217+
on group: BoringSSLEllipticCurveGroup,
218+
context: FiniteFieldArithmeticContext? = nil
219+
) throws -> EllipticCurvePoint {
220+
var `self` = self
221+
try self.invert(on: group, context: context)
222+
return self
223+
}
224+
#endif
161225

226+
// This enhancement can only be present on 6.1 or later because of the
227+
// absence of https://github.com/swiftlang/swift/pull/76186 in older
228+
// compilers.
229+
#if compiler(>=6.1)
162230
@usableFromInline
163231
package static func inverting(
164232
_ point: consuming EllipticCurvePoint,
@@ -168,6 +236,18 @@ package struct EllipticCurvePoint: @unchecked Sendable {
168236
try point.invert(on: group, context: context)
169237
return point
170238
}
239+
#else
240+
@usableFromInline
241+
package static func inverting(
242+
_ point: EllipticCurvePoint,
243+
on group: BoringSSLEllipticCurveGroup,
244+
context: FiniteFieldArithmeticContext? = nil
245+
) throws -> EllipticCurvePoint {
246+
var point = point
247+
try point.invert(on: group, context: context)
248+
return point
249+
}
250+
#endif
171251

172252
@usableFromInline
173253
package mutating func subtract(
@@ -190,6 +270,10 @@ package struct EllipticCurvePoint: @unchecked Sendable {
190270
try self.subtract(rhs, on: group, context: context)
191271
}
192272

273+
// This enhancement can only be present on 6.1 or later because of the
274+
// absence of https://github.com/swiftlang/swift/pull/76186 in older
275+
// compilers.
276+
#if compiler(>=6.1)
193277
@usableFromInline
194278
package consuming func subtracting(
195279
_ rhs: consuming EllipticCurvePoint,
@@ -199,7 +283,23 @@ package struct EllipticCurvePoint: @unchecked Sendable {
199283
try self.subtract(rhs, on: group, context: context)
200284
return self
201285
}
286+
#else
287+
@usableFromInline
288+
package func subtracting(
289+
_ rhs: EllipticCurvePoint,
290+
on group: BoringSSLEllipticCurveGroup,
291+
context: FiniteFieldArithmeticContext? = nil
292+
) throws -> EllipticCurvePoint {
293+
var `self` = self
294+
try self.subtract(rhs, on: group, context: context)
295+
return self
296+
}
297+
#endif
202298

299+
// This enhancement can only be present on 6.1 or later because of the
300+
// absence of https://github.com/swiftlang/swift/pull/76186 in older
301+
// compilers.
302+
#if compiler(>=6.1)
203303
@usableFromInline
204304
package static func subtracting(
205305
_ rhs: consuming EllipticCurvePoint,
@@ -210,6 +310,19 @@ package struct EllipticCurvePoint: @unchecked Sendable {
210310
try lhs.subtract(rhs, on: group, context: context)
211311
return lhs
212312
}
313+
#else
314+
@usableFromInline
315+
package static func subtracting(
316+
_ rhs: EllipticCurvePoint,
317+
from lhs: EllipticCurvePoint,
318+
on group: BoringSSLEllipticCurveGroup,
319+
context: FiniteFieldArithmeticContext? = nil
320+
) throws -> EllipticCurvePoint {
321+
var lhs = lhs
322+
try lhs.subtract(rhs, on: group, context: context)
323+
return lhs
324+
}
325+
#endif
213326

214327
@usableFromInline
215328
package init<MessageBytes: ContiguousBytes, DSTBytes: ContiguousBytes>(

Sources/_CryptoExtras/Util/Data+Extensions.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
import Foundation
1616

1717
extension Data {
18+
// This enhancement can only be present on 6.1 or later because of the
19+
// absence of https://github.com/swiftlang/swift/pull/76186 in older
20+
// compilers.
21+
#if compiler(>=6.1)
1822
// This overload reduces allocations when used in a chain of infix operations.
1923
static func + (lhs: consuming Data, rhs: consuming Data) -> Data {
2024
lhs.append(contentsOf: rhs)
2125
return lhs
2226
}
27+
#endif
2328
}

0 commit comments

Comments
 (0)