Files
swift-crypto/Sources/CryptoBoringWrapper/Util/ArbitraryPrecisionInteger.swift
a9d1d5ab89 Merge the 5.x branch for the Swift Crypto 5.0 release (#470)
## Motivation

Xcode 27 is now available[^1] so it's time to merge in our 5.x branch to
main and release 5.0.

## Modifications

See "Release Notes" section below for overview of changes and links to
the PRs that landed on the 5.x branch during the beta period.

## Result

The main branch will be ready to tag a 5.0 release.

[^1]: https://developer.apple.com/download/all/?q=Xcode%2027

---

## Release Notes

Swift Crypto 5.0 aligns the cross-platform API surface with CryptoKit as
shipped in Xcode 27 (CryptoKit-383.2.1), and takes the opportunity to
streamline the package for both adopters and maintainers.

Most users will be able to depend on the 1.0.0, 2.0.0, 3.0.0, 4.0.0, or
5.0.0 series of releases.

To do so, please use the following dependency in your `Package.swift`:

```swift
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "6.0.0"),
```

### Requires Swift 6.2

The package now declares 6.2 as the minimum Swift version in its package
manifest.

### New span-based API

CryptoKit's new non-allocating, span-based entry points are now
available cross-platform:

- `SymmetricKey` gains `bytes: RawSpan`, `init(copying: RawSpan)`,
`init(copyingWithZeroing: inout MutableRawSpan)`, and
`init(size:initializingWith:)` which initializes the key through an
`inout OutputRawSpan`.
- `HashFunction` gains `update(bytes: RawSpan)` and `hash(bytes:
RawSpan)`, and `Digest` gains `init?(copying: RawSpan)`.
- `HMAC` gains `authenticationCode(for: RawSpan, using:)`.
- `HKDF` gains `deriveKey(inputKeyMaterial:salt:info:output:)` writing
into an `inout OutputRawSpan`, along with span-based `extract` and
`expand`. The existing `DataProtocol` overload now has a fast path for
contiguous salt and info.
- `AES.GCM` and `ChaChaPoly` gain in-place
`seal(inPlace:using:nonce:authenticating:tag:)` and
`open(inPlace:using:nonce:authenticating:tag:)` over `MutableRawSpan`,
and their `Nonce` types gain `bytes: RawSpan` and `init(copying:
RawSpan)`.

### Single-use KEM keys

A new `KEMOneTimePrivateKey` protocol, refining `~Copyable` and
`Sendable`, provides a `consuming func decapsulate(_:)` so a
decapsulation key cannot be reused. Conforming types are
`MLKEM768.OneTimePrivateKey`, `MLKEM1024.OneTimePrivateKey`, and
`XWingMLKEM768X25519.OneTimePrivateKey`.

### Extensible enums

Historically Swift Crypto needed a new major version each time CryptoKit
added a case to a non-frozen enum, because Swift Crypto is not built
with library evolution. All public non-frozen enums are now marked
`@nonexhaustive` (SE-0487), which should remove that reason for future
major bumps. Adopters who do not switch over these enums should not need
to widen their version constraints again.

### Other changes

- Symbols that were previously marked `@_spi(...) public` have been made
internal; they were never intended to be available.
- `@available` annotations have been removed from `Crypto`; they are
unused in all supported compilation modes.
- Some internal reference types are now marked `final`
- Some unused code, e.g. `ECDSASignature.derBytes`, is removed.

### Build and packaging

- The `CCryptoBoringSSLShims` target is removed, along with all
`bindMemory(to:)` calls.
- The `.gyb` templates for nonces, digests, ECDH, ECDSA, MLDSA, and
MLKEM are removed and the generated Swift is checked in. The BoringSSL
backend templates for MLDSA and MLKEM remain.
- Conditional compilation is simplified to `#if canImport(CryptoKit)`;
branches that could never be taken are gone.
- Developing the BoringSSL backend on macOS is no longer supported. Use
the Dev Container configuration now shipped in the repository, or build
on the target platform.
- Warnings are silenced for vendored C sources: `-Wshorten-64-to-32` for
BoringSSL and `macro-redefined` for XKCP. CI builds with
`-warnings-as-errors`.

### Commits

The changes in this release were reviewed and merged to the 5.x branch
during the beta period:

- 0a26a9e3 [5.x] Align with CryptoKit APIs from Xcode 27 beta 1 (#442)
- 8ee9962f [5.x] Align with CryptoKit APIs from Xcode 27 beta 2 (#444)
- ce8db476 [5.x] Align with CryptoKit APIs from Xcode 27 beta 4 (#453)
- 15b0fb5b [5.x] Adopt CheckImplementationOnly and CI with
-warnings-as-errors (#454)
- b3746b14 [5.x] Remove bindMemory(to:) calls and the
CCryptoBoringSSLShims target (#455)
- 57782dc6 [5.x] Remove duplicate imports from gyb-generated MLDSA
backend code (#458)
- 2c45ca2f [5.x] Remove unused internal code path:
ECDSASignature.derBytes (#457)
- 33785f67 [5.x] Mark some class types as final (#456)
- 2e074efb [5.x] Remove native SwiftPM build-system workaround in CI
(#461)
- 20f90497 [5.x] Align with CryptoKit from Xcode 27 beta 6 (#462)

---------

Co-authored-by: Dmitry <45711841+darkdi@users.noreply.github.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-09-16 14:47:34 +01:00

777 lines
26 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2019 Apple Inc. and the SwiftCrypto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if hasFeature(SourceWarningControl)
@diagnose(ImplementationOnlyDeprecated, as: ignored) @_implementationOnly import CCryptoBoringSSL
#else
@_implementationOnly import CCryptoBoringSSL
#endif
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
/// A wrapper around the OpenSSL BIGNUM object that is appropriately lifetime managed,
/// and that provides better Swift types for this object.
@usableFromInline
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
package struct ArbitraryPrecisionInteger: @unchecked Sendable {
private var _backing: BackingStorage
@usableFromInline
package init() {
self._backing = BackingStorage()
}
package init(copying original: UnsafePointer<BIGNUM>) throws {
self._backing = try BackingStorage(copying: original)
}
@usableFromInline
package init(_ original: ArbitraryPrecisionInteger) throws {
self._backing = try BackingStorage(copying: original._backing)
}
@usableFromInline
package init(integerLiteral value: Int64) {
self._backing = BackingStorage(value)
}
}
// MARK: - BackingStorage
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger {
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
fileprivate final class BackingStorage {
private var _backing: BIGNUM
init() {
self._backing = BIGNUM()
CCryptoBoringSSL_BN_init(&self._backing)
}
init(copying original: UnsafePointer<BIGNUM>) throws {
self._backing = BIGNUM()
guard CCryptoBoringSSL_BN_copy(&self._backing, original) != nil else {
throw CryptoBoringWrapperError.internalBoringSSLError()
}
}
init(copying original: BackingStorage) throws {
self._backing = BIGNUM()
try original.withUnsafeMutableBignumPointer { bnPtr in
guard CCryptoBoringSSL_BN_copy(&self._backing, bnPtr) != nil else {
throw CryptoBoringWrapperError.internalBoringSSLError()
}
}
}
init(_ value: Int64) {
self._backing = BIGNUM()
let rc = CCryptoBoringSSL_BN_set_u64(&self._backing, value.magnitude)
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
if value < 0 {
CCryptoBoringSSL_BN_set_negative(&self._backing, 1)
}
}
deinit {
CCryptoBoringSSL_BN_clear_free(&self._backing)
}
}
}
// MARK: - Extra initializers
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger {
@usableFromInline
package init<Bytes: ContiguousBytes>(bytes: Bytes) throws {
self._backing = try BackingStorage(bytes: bytes)
}
/// Create an `ArbitraryPrecisionInteger` from a hex string.
///
/// - Parameter hexString: Hex byte string (big-endian, no `0x` prefix, may start with `-` for a negative number).
@usableFromInline
package init(hexString: String) throws {
self._backing = try BackingStorage(hexString: hexString)
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger.BackingStorage {
convenience init<Bytes: ContiguousBytes>(bytes: Bytes) throws {
self.init()
let rc: UnsafeMutablePointer<BIGNUM>? = bytes.withUnsafeBytes { bytesPointer in
CCryptoBoringSSL_BN_bin2bn(
bytesPointer.baseAddress,
bytesPointer.count,
&self._backing
)
}
guard rc != nil else {
throw CryptoBoringWrapperError.internalBoringSSLError()
}
}
@inlinable
convenience init(hexString: String) throws {
self.init()
try hexString.withCString { hexStringPtr in
/// `BN_hex2bin` takes a `BIGNUM **` so we need a double WUMP dance.
try withUnsafeMutablePointer(to: &self._backing) { backingPtr in
var backingPtr: UnsafeMutablePointer<BIGNUM>? = backingPtr
try withUnsafeMutablePointer(to: &backingPtr) { backingPtrPtr in
/// `BN_hex2bin` returns the number of bytes of `in` processed or zero on error.
guard CCryptoBoringSSL_BN_hex2bn(backingPtrPtr, hexStringPtr) == hexString.count else {
throw CryptoBoringWrapperError.incorrectParameterSize
}
}
}
}
}
}
// MARK: - Pointer helpers
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger {
package func withUnsafeBignumPointer<T>(
_ body: (UnsafePointer<BIGNUM>) throws -> T
) rethrows
-> T
{
try self._backing.withUnsafeBignumPointer(body)
}
package mutating func withUnsafeMutableBignumPointer<T>(
_ body: (UnsafeMutablePointer<BIGNUM>) throws -> T
) rethrows -> T {
if !isKnownUniquelyReferenced(&self._backing) {
// Failing to CoW is a fatal error here.
self._backing = try! BackingStorage(copying: self._backing)
}
return try self._backing.withUnsafeMutableBignumPointer(body)
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger.BackingStorage {
func withUnsafeBignumPointer<T>(_ body: (UnsafePointer<BIGNUM>) throws -> T) rethrows -> T {
try body(&self._backing)
}
func withUnsafeMutableBignumPointer<T>(
_ body: (UnsafeMutablePointer<BIGNUM>) throws -> T
)
rethrows -> T
{
try body(&self._backing)
}
}
// MARK: - Other helpers
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger {
@usableFromInline static func _compare(
lhs: ArbitraryPrecisionInteger,
rhs: ArbitraryPrecisionInteger
) -> CInt {
lhs.withUnsafeBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
CCryptoBoringSSL_BN_cmp(lhsPtr, rhsPtr)
}
}
}
// This lets us check the sign of an ArbitraryPrecisionInteger.
@usableFromInline var _positive: Bool {
self.withUnsafeBignumPointer {
CCryptoBoringSSL_BN_is_negative($0) == 0
}
}
@usableFromInline
package func squared() -> ArbitraryPrecisionInteger {
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
self.withUnsafeBignumPointer { selfPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_sqr(resultPtr, selfPtr, bnCtx)
}
}
}
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionIntegers")
return result
}
@usableFromInline
package func positiveSquareRoot() throws -> ArbitraryPrecisionInteger {
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
self.withUnsafeBignumPointer { selfPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_sqrt(resultPtr, selfPtr, bnCtx)
}
}
}
guard rc == 1 else {
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return result
}
@usableFromInline
package var byteCount: Int {
self._backing.withUnsafeBignumPointer {
Int(CCryptoBoringSSL_BN_num_bytes($0))
}
}
/// Some functions require a BN_CTX parameter: this obtains one with a scoped lifetime.
private static func withUnsafeBN_CTX<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
// We force unwrap here because this call can only fail if the allocator is broken, and if
// the allocator fails we don't have long to live anyway.
let bnCtx = CCryptoBoringSSL_BN_CTX_new()!
defer {
CCryptoBoringSSL_BN_CTX_free(bnCtx)
}
return try body(bnCtx)
}
}
// MARK: - Equatable
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger: Equatable {
@inlinable
package static func == (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) == 0
}
}
// MARK: - Comparable
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger: Comparable {
@inlinable
package static func < (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) < 0
}
@inlinable
package static func <= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) <= 0
}
@inlinable
package static func > (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) > 0
}
@inlinable
package static func >= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) >= 0
}
}
// MARK: - ExpressibleByIntegerLiteral
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger: ExpressibleByIntegerLiteral {}
// MARK: - AdditiveArithmetic
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger: AdditiveArithmetic {
@inlinable
package static var zero: ArbitraryPrecisionInteger {
0
}
@usableFromInline
package static func + (
lhs: ArbitraryPrecisionInteger,
rhs: ArbitraryPrecisionInteger
)
-> ArbitraryPrecisionInteger
{
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
lhs.withUnsafeBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
CCryptoBoringSSL_BN_add(resultPtr, lhsPtr, rhsPtr)
}
}
}
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
return result
}
@usableFromInline
package static func += (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) {
let rc = lhs.withUnsafeMutableBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
CCryptoBoringSSL_BN_add(lhsPtr, lhsPtr, rhsPtr)
}
}
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
}
@usableFromInline
package static func - (
lhs: ArbitraryPrecisionInteger,
rhs: ArbitraryPrecisionInteger
)
-> ArbitraryPrecisionInteger
{
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
lhs.withUnsafeBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
CCryptoBoringSSL_BN_sub(resultPtr, lhsPtr, rhsPtr)
}
}
}
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
return result
}
@usableFromInline
package static func -= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) {
let rc = lhs.withUnsafeMutableBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
CCryptoBoringSSL_BN_sub(lhsPtr, lhsPtr, rhsPtr)
}
}
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
}
}
// MARK: - Numeric
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger: Numeric {
@usableFromInline
package typealias Magnitude = Self
@usableFromInline
package var magnitude: Magnitude {
if self._positive {
return self
}
// We are negative, we need a copy.
var copy = self
copy.withUnsafeMutableBignumPointer {
// BN_set_negative is poorly named: it should be "BN_set_sign_bit", which we set to 0.
CCryptoBoringSSL_BN_set_negative($0, 0)
}
return copy
}
@usableFromInline
package static func * (
lhs: ArbitraryPrecisionInteger,
rhs: ArbitraryPrecisionInteger
)
-> ArbitraryPrecisionInteger
{
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
lhs.withUnsafeBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_mul(resultPtr, lhsPtr, rhsPtr, bnCtx)
}
}
}
}
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
return result
}
@usableFromInline
package static func *= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) {
let rc = lhs.withUnsafeMutableBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_mul(lhsPtr, lhsPtr, rhsPtr, bnCtx)
}
}
}
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
}
@inlinable
package init?<T: BinaryInteger>(exactly integer: T) {
fatalError("Not currently implemented")
}
}
// MARK: - Modular arithmetic
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger {
@usableFromInline
package func modulo(
_ mod: ArbitraryPrecisionInteger,
nonNegative: Bool = false
) throws
-> ArbitraryPrecisionInteger
{
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
self.withUnsafeBignumPointer { selfPtr in
mod.withUnsafeBignumPointer { modPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
if nonNegative {
CCryptoBoringSSL_BN_nnmod(resultPtr, selfPtr, modPtr, bnCtx)
} else {
// BN_mod is a C macro for BN_div with a nil quotient; call that directly.
CCryptoBoringSSL_BN_div(nil, resultPtr, selfPtr, modPtr, bnCtx)
}
}
}
}
}
guard rc == 1 else { throw CryptoBoringWrapperError.internalBoringSSLError() }
return result
}
@usableFromInline
package func inverse(modulo mod: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
self.withUnsafeBignumPointer { selfPtr in
mod.withUnsafeBignumPointer { modPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_mod_inverse(resultPtr, selfPtr, modPtr, bnCtx)
}
}
}
}
guard rc != nil else { throw CryptoBoringWrapperError.internalBoringSSLError() }
return result
}
@usableFromInline
package static func inverse(
lhs: ArbitraryPrecisionInteger,
modulo mod: ArbitraryPrecisionInteger
) throws -> ArbitraryPrecisionInteger {
try ArbitraryPrecisionInteger(lhs).inverse(modulo: mod)
}
@usableFromInline
package func add(
_ rhs: ArbitraryPrecisionInteger,
modulo modulus: ArbitraryPrecisionInteger? = nil
) throws -> ArbitraryPrecisionInteger {
guard let modulus else { return self + rhs }
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
self.withUnsafeBignumPointer { selfPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
modulus.withUnsafeBignumPointer { modulusPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_mod_add(resultPtr, selfPtr, rhsPtr, modulusPtr, bnCtx)
}
}
}
}
}
guard rc == 1 else { throw CryptoBoringWrapperError.internalBoringSSLError() }
return result
}
@usableFromInline
package func sub(
_ rhs: ArbitraryPrecisionInteger,
modulo modulus: ArbitraryPrecisionInteger? = nil
) throws -> ArbitraryPrecisionInteger {
guard let modulus else { return self - rhs }
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
self.withUnsafeBignumPointer { selfPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
modulus.withUnsafeBignumPointer { modulusPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_mod_sub(resultPtr, selfPtr, rhsPtr, modulusPtr, bnCtx)
}
}
}
}
}
guard rc == 1 else { throw CryptoBoringWrapperError.internalBoringSSLError() }
return result
}
@usableFromInline
package func mul(
_ rhs: ArbitraryPrecisionInteger,
modulo modulus: ArbitraryPrecisionInteger? = nil
) throws -> ArbitraryPrecisionInteger {
guard let modulus else { return self * rhs }
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
self.withUnsafeBignumPointer { selfPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
modulus.withUnsafeBignumPointer { modulusPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_mod_mul(resultPtr, selfPtr, rhsPtr, modulusPtr, bnCtx)
}
}
}
}
}
guard rc == 1 else { throw CryptoBoringWrapperError.internalBoringSSLError() }
return result
}
}
// MARK: - SignedNumeric
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger: SignedNumeric {
@usableFromInline
package mutating func negate() {
let signBit: CInt = self._positive ? 1 : 0
self.withUnsafeMutableBignumPointer {
CCryptoBoringSSL_BN_set_negative($0, signBit)
}
}
}
// MARK: - Other arithmetic operations
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger {
@usableFromInline
package var trailingZeroBitCount: Int32 {
self.withUnsafeBignumPointer {
CCryptoBoringSSL_BN_count_low_zero_bits($0)
}
}
@usableFromInline
package static func gcd(
_ a: ArbitraryPrecisionInteger,
_ b: ArbitraryPrecisionInteger
) throws
-> ArbitraryPrecisionInteger
{
var result = ArbitraryPrecisionInteger()
guard
result.withUnsafeMutableBignumPointer({ resultPtr in
a.withUnsafeBignumPointer { aPtr in
b.withUnsafeBignumPointer { bPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_gcd(resultPtr, aPtr, bPtr, bnCtx)
}
}
}
}) == 1
else {
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return result
}
@usableFromInline
package func isCoprime(with other: ArbitraryPrecisionInteger) throws -> Bool {
try Self.gcd(self, other) == 1
}
@usableFromInline
package static func random(
inclusiveMin: UInt,
exclusiveMax: ArbitraryPrecisionInteger
) throws
-> ArbitraryPrecisionInteger
{
var result = ArbitraryPrecisionInteger()
guard
result.withUnsafeMutableBignumPointer({ resultPtr in
exclusiveMax.withUnsafeBignumPointer { exclusiveMaxPtr in
CCryptoBoringSSL_BN_rand_range_ex(resultPtr, BN_ULONG(inclusiveMin), exclusiveMaxPtr)
}
}) == 1
else {
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return result
}
@usableFromInline
package static func >> (lhs: ArbitraryPrecisionInteger, rhs: Int32) -> ArbitraryPrecisionInteger {
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
lhs.withUnsafeBignumPointer { lhsPtr in
CCryptoBoringSSL_BN_rshift(resultPtr, lhsPtr, rhs)
}
}
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
return result
}
@usableFromInline
package static func / (
lhs: ArbitraryPrecisionInteger,
rhs: ArbitraryPrecisionInteger
)
-> ArbitraryPrecisionInteger
{
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
lhs.withUnsafeBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
CCryptoBoringSSL_BN_div(resultPtr, nil, lhsPtr, rhsPtr, bnCtx)
}
}
}
}
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
return result
}
@usableFromInline
package var isEven: Bool {
self.withUnsafeBignumPointer {
CCryptoBoringSSL_BN_is_odd($0) == 0
}
}
}
// MARK: - Serializing
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension Data {
/// Serializes an ArbitraryPrecisionInteger padded out to a certain minimum size.
@usableFromInline
package mutating func append(
bytesOf integer: ArbitraryPrecisionInteger,
paddedToSize paddingSize: Int
) throws {
let byteCount = integer.byteCount
guard paddingSize >= byteCount else {
throw CryptoBoringWrapperError.incorrectParameterSize
}
// To extend the data we need to write some zeroes into it.
self.append(contentsOf: repeatElement(0, count: paddingSize))
let written: Int = self.withUnsafeMutableBytes { bytesPtr in
// We want to write to the _end_ of the memory we just allocated, as we want to pad with leading zeroes.
let bytesPtr = UnsafeMutableRawBufferPointer(rebasing: bytesPtr.suffix(byteCount))
assert(bytesPtr.count == byteCount)
return integer.withUnsafeBignumPointer { bnPtr in
CCryptoBoringSSL_BN_bn2bin(bnPtr, bytesPtr.baseAddress!)
}
}
assert(written == byteCount)
}
@usableFromInline
package init(bytesOf integer: ArbitraryPrecisionInteger, paddedToSize paddingSize: Int) throws {
self.init(capacity: paddingSize)
try self.append(bytesOf: integer, paddedToSize: paddingSize)
}
}
// MARK: - Printing
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension ArbitraryPrecisionInteger: CustomDebugStringConvertible {
@usableFromInline
package var debugDescription: String {
guard let bio = CCryptoBoringSSL_BIO_new(CCryptoBoringSSL_BIO_s_mem()) else {
return "ArbitraryPrecisionInteger: (error generating representation)"
}
defer {
CCryptoBoringSSL_BIO_free(bio)
}
let rc = self.withUnsafeBignumPointer {
CCryptoBoringSSL_BN_print(bio, $0)
}
guard rc == 1 else {
return "ArbitraryPrecisionInteger: (error generating representation)"
}
var stringPointer: UnsafePointer<UInt8>?
var length: Int = 0
guard CCryptoBoringSSL_BIO_mem_contents(bio, &stringPointer, &length) == 1 else {
return "ArbitraryPrecisionInteger: (error generating representation)"
}
// This line looks scary but it's actually pretty safe.
//
// String.init(decoding:as:) treats the first argument as a Collection of UInt8, and so does not require it to be
// null-terminated. It also doesn't take ownership of the data, instead copying the bytes in to its backing storage.
//
// The other note is that we don't need to free the pointer vended to us by BIO_mem_contents, as this is in fact an
// interior pointer to the storage owned by the BIO. That pointer will therefore be freed when our deferred BIO_free
// call above actually executes, which will be only after this String has been constructed.
//
// I know it looks gross, but it's basically right.
return String(
decoding: UnsafeBufferPointer(start: stringPointer, count: length),
as: Unicode.UTF8.self
)
}
}