Skip to content

Commit

Permalink
Refactor bytesCount
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Nov 27, 2024
1 parent 571fd4b commit a975178
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions Sources/_CryptoExtras/SLHDSA/SLHDSA_boring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,11 @@ extension SLHDSA {
self.backing.isValidSignature(signature, for: data, context: context)
}

/// The size of the public key in bytes.
fileprivate static let bytesCount = Backing.bytesCount

fileprivate final class Backing {
private let pointer: UnsafeMutablePointer<UInt8>

init(privateKeyBacking: PrivateKey.Backing) {
self.pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: SLHDSA.PublicKey.bytesCount)
self.pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: SLHDSA.PublicKey.Backing.bytesCount)
privateKeyBacking.withUnsafePointer { privateKeyPtr in
CCryptoBoringSSL_SLHDSA_SHA2_128S_public_from_private(self.pointer, privateKeyPtr)
}
Expand All @@ -204,21 +201,21 @@ extension SLHDSA {
///
/// - Throws: `CryptoKitError.incorrectKeySize` if the raw representation is not the correct size.
init(rawRepresentation: some DataProtocol) throws {
guard rawRepresentation.count == SLHDSA.PublicKey.bytesCount else {
guard rawRepresentation.count == SLHDSA.PublicKey.Backing.bytesCount else {
throw CryptoKitError.incorrectKeySize
}

self.pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: SLHDSA.PublicKey.bytesCount)
self.pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: SLHDSA.PublicKey.Backing.bytesCount)
self.pointer.initialize(
from: Array(rawRepresentation),
count: SLHDSA.PublicKey.bytesCount
count: SLHDSA.PublicKey.Backing.bytesCount
)
}


/// The raw representation of the public key.
var rawRepresentation: Data {
Data(UnsafeBufferPointer(start: self.pointer, count: SLHDSA.PublicKey.bytesCount))
Data(UnsafeBufferPointer(start: self.pointer, count: SLHDSA.PublicKey.Backing.bytesCount))
}

/// Verify a signature for the given data.
Expand Down

0 comments on commit a975178

Please sign in to comment.