From 5ef0b4def8dd547f0ac14fcf255d1c29b68abedf Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Mon, 3 Jun 2024 19:35:28 +0100 Subject: [PATCH] Also move over FiniteFieldArithmeticContext to CryptoBoringWrapper --- .../FiniteFieldArithmeticContext.swift} | 35 +++++++------------ 1 file changed, 13 insertions(+), 22 deletions(-) rename Sources/{Crypto/Util/BoringSSL/FiniteFieldArithmeticContext_boring.swift => CryptoBoringWrapper/FiniteFieldArithmeticContext.swift} (80%) diff --git a/Sources/Crypto/Util/BoringSSL/FiniteFieldArithmeticContext_boring.swift b/Sources/CryptoBoringWrapper/FiniteFieldArithmeticContext.swift similarity index 80% rename from Sources/Crypto/Util/BoringSSL/FiniteFieldArithmeticContext_boring.swift rename to Sources/CryptoBoringWrapper/FiniteFieldArithmeticContext.swift index 3cc0bdd4..1936ce06 100644 --- a/Sources/Crypto/Util/BoringSSL/FiniteFieldArithmeticContext_boring.swift +++ b/Sources/CryptoBoringWrapper/FiniteFieldArithmeticContext.swift @@ -15,8 +15,6 @@ @_exported import CryptoKit #else import CCryptoBoringSSL -import CryptoBoringWrapper -import Foundation /// A context for performing mathematical operations on ArbitraryPrecisionIntegers over a finite field. /// @@ -29,16 +27,14 @@ import Foundation /// /// Annoyingly, because of the way we have implemented ArbitraryPrecisionInteger, we can't actually use these temporary bignums /// ourselves. -@usableFromInline -class FiniteFieldArithmeticContext { +public final class FiniteFieldArithmeticContext { private var fieldSize: ArbitraryPrecisionInteger private var bnCtx: OpaquePointer - @usableFromInline - init(fieldSize: ArbitraryPrecisionInteger) throws { + public init(fieldSize: ArbitraryPrecisionInteger) throws { self.fieldSize = fieldSize guard let bnCtx = CCryptoBoringSSL_BN_CTX_new() else { - throw CryptoKitError.internalBoringSSLError() + throw CryptoBoringWrapperError.internalBoringSSLError() } CCryptoBoringSSL_BN_CTX_start(bnCtx) self.bnCtx = bnCtx @@ -53,8 +49,7 @@ class FiniteFieldArithmeticContext { // MARK: - Arithmetic operations extension FiniteFieldArithmeticContext { - @usableFromInline - func square(_ input: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + public func square(_ input: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var output = ArbitraryPrecisionInteger() let rc = input.withUnsafeBignumPointer { inputPointer in @@ -66,14 +61,13 @@ extension FiniteFieldArithmeticContext { } guard rc == 1 else { - throw CryptoKitError.internalBoringSSLError() + throw CryptoBoringWrapperError.internalBoringSSLError() } return output } - @usableFromInline - func multiply(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + public func multiply(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var output = ArbitraryPrecisionInteger() let rc = x.withUnsafeBignumPointer { xPointer in @@ -87,14 +81,13 @@ extension FiniteFieldArithmeticContext { } guard rc == 1 else { - throw CryptoKitError.internalBoringSSLError() + throw CryptoBoringWrapperError.internalBoringSSLError() } return output } - @usableFromInline - func add(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + public func add(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var output = ArbitraryPrecisionInteger() let rc = x.withUnsafeBignumPointer { xPointer in @@ -108,14 +101,13 @@ extension FiniteFieldArithmeticContext { } guard rc == 1 else { - throw CryptoKitError.internalBoringSSLError() + throw CryptoBoringWrapperError.internalBoringSSLError() } return output } - @usableFromInline - func subtract(_ x: ArbitraryPrecisionInteger, from y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + public func subtract(_ x: ArbitraryPrecisionInteger, from y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var output = ArbitraryPrecisionInteger() let rc = x.withUnsafeBignumPointer { xPointer in @@ -130,14 +122,13 @@ extension FiniteFieldArithmeticContext { } guard rc == 1 else { - throw CryptoKitError.internalBoringSSLError() + throw CryptoBoringWrapperError.internalBoringSSLError() } return output } - @usableFromInline - func positiveSquareRoot(_ x: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + public func positiveSquareRoot(_ x: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { let outputPointer = x.withUnsafeBignumPointer { xPointer in self.fieldSize.withUnsafeBignumPointer { fieldSizePointer in // We can't pass a pointer in as BN_mod_sqrt may attempt to free it. @@ -146,7 +137,7 @@ extension FiniteFieldArithmeticContext { } guard let actualOutputPointer = outputPointer else { - throw CryptoKitError.internalBoringSSLError() + throw CryptoBoringWrapperError.internalBoringSSLError() } // Ok, we own this pointer now.