Also move over FiniteFieldArithmeticContext to CryptoBoringWrapper

This commit is contained in:
Si Beaumont committed 2024-06-03 19:35:41 +01:00
1 parent 280681b061
commit 5ef0b4def8
1 file changed
+13 -22
@@ -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.