Skip to content

Commit

Permalink
Adapt custom withUnsafeBytes to DataProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Oct 22, 2024
1 parent 5950937 commit e34c7e3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ extension MLDSA {
let output = try Array<UInt8>(unsafeUninitializedCapacity: Signature.bytesCount) { bufferPtr, length in
let bytes: ContiguousBytes = data.regions.count == 1 ? data.regions.first! : Array(data)
let result = bytes.withUnsafeBytes { dataPtr in
context.map { Data($0) }.withUnsafeBytes { contextPtr in
context.withUnsafeBytes { contextPtr in
CCryptoBoringSSL_MLDSA65_sign(
bufferPtr.baseAddress,
self.pointer,
Expand Down Expand Up @@ -271,7 +271,7 @@ extension MLDSA {
signature.withUnsafeBytes { signaturePtr in
let bytes: ContiguousBytes = data.regions.count == 1 ? data.regions.first! : Array(data)
let rc: CInt = bytes.withUnsafeBytes { dataPtr in
context.map { Data($0) }.withUnsafeBytes { contextPtr in
context.withUnsafeBytes { contextPtr in
CCryptoBoringSSL_MLDSA65_verify(
self.pointer,
signaturePtr.baseAddress,
Expand Down
5 changes: 3 additions & 2 deletions Sources/_CryptoExtras/Util/Optional+withUnsafeBytes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

import Foundation

extension Optional where Wrapped == ContiguousBytes {
extension Optional where Wrapped: DataProtocol {
func withUnsafeBytes<ReturnValue>(_ body: (UnsafeRawBufferPointer) throws -> ReturnValue) rethrows -> ReturnValue {
if let self {
return try self.withUnsafeBytes { try body($0) }
let bytes: ContiguousBytes = self.regions.count == 1 ? self.regions.first! : Array(self)
return try bytes.withUnsafeBytes { try body($0) }
} else {
return try body(UnsafeRawBufferPointer(start: nil, count: 0))
}
Expand Down

0 comments on commit e34c7e3

Please sign in to comment.