Skip to content

Commit 0d32399

Browse files
authored
Generalise ML-DSA with GYB (#355)
Generalises the definition of ML-DSA algorithms with GYB to allow the addition of other parameter sets in the future ### Checklist - [X] I've run tests to see all new and existing tests pass - [X] I've followed the code style of the rest of the project - [X] I've read the [Contribution Guidelines](CONTRIBUTING.md) - [X] I've updated the documentation if necessary #### If you've made changes to `gyb` files - [X] I've run `.script/generate_boilerplate_files_with_gyb` and included updated generated files in a commit of this pull request ### Motivation: BoringSSL recently [made ML-DSA-87 public](https://boringssl.googlesource.com/boringssl/+/2a514a51baebd5a232fc64f7b082f7a8b28cd29d) in its API, in addition to ML-DSA-65 which has already been [integrated into Swift Crypto](#267). By generating the code with GYB we will be able to add support for ML-DSA-87 very easily in the future when the vendored version of BoringSSL is updated. ### Modifications: Generate the code for `MLDSA65` (and in future also for `MLDSA87`) with GYB. ### Result: Nothing changes in the public API, but adding `MLDSA87` in the future will be instantaneous.
1 parent d4d7fef commit 0d32399

File tree

3 files changed

+354
-11
lines changed

3 files changed

+354
-11
lines changed

Sources/_CryptoExtras/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ add_library(_CryptoExtras
4141
"Key Derivation/PBKDF2/PBKDF2.swift"
4242
"Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift"
4343
"Key Derivation/Scrypt/Scrypt.swift"
44-
"MLDSA/MLDSA65_boring.swift"
44+
"MLDSA/MLDSA_boring.swift"
4545
"MLKEM/MLKEM_boring.swift"
4646
"OPRFs/OPRF.swift"
4747
"OPRFs/OPRFClient.swift"

Sources/_CryptoExtras/MLDSA/MLDSA65_boring.swift renamed to Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
// MARK: - Generated file, do NOT edit
16+
// any edits of this file WILL be overwritten and thus discarded
17+
// see section `gyb` in `README` for details.
18+
1519
@_implementationOnly import CCryptoBoringSSL
1620
import Crypto
1721
import Foundation
@@ -86,7 +90,7 @@ extension MLDSA65 {
8690

8791
self.seed = try withUnsafeTemporaryAllocation(
8892
of: UInt8.self,
89-
capacity: MLDSA65.seedByteCount
93+
capacity: MLDSA.seedByteCount
9094
) { seedPtr in
9195
try withUnsafeTemporaryAllocation(
9296
of: UInt8.self,
@@ -102,7 +106,7 @@ extension MLDSA65 {
102106
throw CryptoKitError.internalBoringSSLError()
103107
}
104108

105-
return Data(bytes: seedPtr.baseAddress!, count: MLDSA65.seedByteCount)
109+
return Data(bytes: seedPtr.baseAddress!, count: MLDSA.seedByteCount)
106110
}
107111
}
108112
}
@@ -113,7 +117,7 @@ extension MLDSA65 {
113117
///
114118
/// - Throws: `CryptoKitError.incorrectKeySize` if the seed is not 32 bytes long.
115119
init(seedRepresentation: some DataProtocol) throws {
116-
guard seedRepresentation.count == MLDSA65.seedByteCount else {
120+
guard seedRepresentation.count == MLDSA.seedByteCount else {
117121
throw CryptoKitError.incorrectKeySize
118122
}
119123

@@ -125,7 +129,7 @@ extension MLDSA65 {
125129
CCryptoBoringSSL_MLDSA65_private_key_from_seed(
126130
&self.key,
127131
seedPtr.baseAddress,
128-
MLDSA65.seedByteCount
132+
MLDSA.seedByteCount
129133
)
130134
}) == 1
131135
else {
@@ -172,7 +176,7 @@ extension MLDSA65 {
172176
}
173177

174178
/// The size of the private key in bytes.
175-
static let byteCount = 4032
179+
static let byteCount = Int(MLDSA65_PRIVATE_KEY_BYTES)
176180
}
177181
}
178182
}
@@ -311,16 +315,18 @@ extension MLDSA65 {
311315
}
312316

313317
/// The size of the public key in bytes.
314-
static let byteCount = 1952
318+
static let byteCount = Int(MLDSA65_PUBLIC_KEY_BYTES)
315319
}
316320
}
317321
}
318322

319323
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
320324
extension MLDSA65 {
321-
/// The size of the seed in bytes.
322-
private static let seedByteCount = 32
323-
324325
/// The size of the signature in bytes.
325-
private static let signatureByteCount = 3309
326+
private static let signatureByteCount = Int(MLDSA65_SIGNATURE_BYTES)
327+
}
328+
329+
private enum MLDSA {
330+
/// The size of the seed in bytes.
331+
fileprivate static let seedByteCount = 32
326332
}

0 commit comments

Comments
 (0)