ml_dsa 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +104 -0
- data/LICENSE +14 -0
- data/LICENSE-APACHE +185 -0
- data/LICENSE-MIT +21 -0
- data/README.md +234 -0
- data/ext/ml_dsa/extconf.rb +47 -0
- data/ext/ml_dsa/fips202.c +933 -0
- data/ext/ml_dsa/fips202.h +166 -0
- data/ext/ml_dsa/ml-dsa-44/clean/api.h +52 -0
- data/ext/ml_dsa/ml-dsa-44/clean/ntt.c +98 -0
- data/ext/ml_dsa/ml-dsa-44/clean/ntt.h +10 -0
- data/ext/ml_dsa/ml-dsa-44/clean/packing.c +261 -0
- data/ext/ml_dsa/ml-dsa-44/clean/packing.h +31 -0
- data/ext/ml_dsa/ml-dsa-44/clean/params.h +44 -0
- data/ext/ml_dsa/ml-dsa-44/clean/poly.c +848 -0
- data/ext/ml_dsa/ml-dsa-44/clean/poly.h +52 -0
- data/ext/ml_dsa/ml-dsa-44/clean/polyvec.c +415 -0
- data/ext/ml_dsa/ml-dsa-44/clean/polyvec.h +65 -0
- data/ext/ml_dsa/ml-dsa-44/clean/reduce.c +69 -0
- data/ext/ml_dsa/ml-dsa-44/clean/reduce.h +17 -0
- data/ext/ml_dsa/ml-dsa-44/clean/rounding.c +98 -0
- data/ext/ml_dsa/ml-dsa-44/clean/rounding.h +14 -0
- data/ext/ml_dsa/ml-dsa-44/clean/sign.c +417 -0
- data/ext/ml_dsa/ml-dsa-44/clean/sign.h +49 -0
- data/ext/ml_dsa/ml-dsa-44/clean/symmetric-shake.c +26 -0
- data/ext/ml_dsa/ml-dsa-44/clean/symmetric.h +34 -0
- data/ext/ml_dsa/ml-dsa-65/clean/api.h +52 -0
- data/ext/ml_dsa/ml-dsa-65/clean/ntt.c +98 -0
- data/ext/ml_dsa/ml-dsa-65/clean/ntt.h +10 -0
- data/ext/ml_dsa/ml-dsa-65/clean/packing.c +261 -0
- data/ext/ml_dsa/ml-dsa-65/clean/packing.h +31 -0
- data/ext/ml_dsa/ml-dsa-65/clean/params.h +44 -0
- data/ext/ml_dsa/ml-dsa-65/clean/poly.c +799 -0
- data/ext/ml_dsa/ml-dsa-65/clean/poly.h +52 -0
- data/ext/ml_dsa/ml-dsa-65/clean/polyvec.c +415 -0
- data/ext/ml_dsa/ml-dsa-65/clean/polyvec.h +65 -0
- data/ext/ml_dsa/ml-dsa-65/clean/reduce.c +69 -0
- data/ext/ml_dsa/ml-dsa-65/clean/reduce.h +17 -0
- data/ext/ml_dsa/ml-dsa-65/clean/rounding.c +92 -0
- data/ext/ml_dsa/ml-dsa-65/clean/rounding.h +14 -0
- data/ext/ml_dsa/ml-dsa-65/clean/sign.c +415 -0
- data/ext/ml_dsa/ml-dsa-65/clean/sign.h +49 -0
- data/ext/ml_dsa/ml-dsa-65/clean/symmetric-shake.c +26 -0
- data/ext/ml_dsa/ml-dsa-65/clean/symmetric.h +34 -0
- data/ext/ml_dsa/ml-dsa-87/clean/api.h +52 -0
- data/ext/ml_dsa/ml-dsa-87/clean/ntt.c +98 -0
- data/ext/ml_dsa/ml-dsa-87/clean/ntt.h +10 -0
- data/ext/ml_dsa/ml-dsa-87/clean/packing.c +261 -0
- data/ext/ml_dsa/ml-dsa-87/clean/packing.h +31 -0
- data/ext/ml_dsa/ml-dsa-87/clean/params.h +44 -0
- data/ext/ml_dsa/ml-dsa-87/clean/poly.c +823 -0
- data/ext/ml_dsa/ml-dsa-87/clean/poly.h +52 -0
- data/ext/ml_dsa/ml-dsa-87/clean/polyvec.c +415 -0
- data/ext/ml_dsa/ml-dsa-87/clean/polyvec.h +65 -0
- data/ext/ml_dsa/ml-dsa-87/clean/reduce.c +69 -0
- data/ext/ml_dsa/ml-dsa-87/clean/reduce.h +17 -0
- data/ext/ml_dsa/ml-dsa-87/clean/rounding.c +92 -0
- data/ext/ml_dsa/ml-dsa-87/clean/rounding.h +14 -0
- data/ext/ml_dsa/ml-dsa-87/clean/sign.c +415 -0
- data/ext/ml_dsa/ml-dsa-87/clean/sign.h +49 -0
- data/ext/ml_dsa/ml-dsa-87/clean/symmetric-shake.c +26 -0
- data/ext/ml_dsa/ml-dsa-87/clean/symmetric.h +34 -0
- data/ext/ml_dsa/ml_dsa_44_impl.c +10 -0
- data/ext/ml_dsa/ml_dsa_65_impl.c +10 -0
- data/ext/ml_dsa/ml_dsa_87_impl.c +10 -0
- data/ext/ml_dsa/ml_dsa_ext.c +1360 -0
- data/ext/ml_dsa/ml_dsa_impl_template.h +35 -0
- data/ext/ml_dsa/ml_dsa_internal.h +188 -0
- data/ext/ml_dsa/randombytes.c +48 -0
- data/ext/ml_dsa/randombytes.h +15 -0
- data/lib/ml_dsa/batch_builder.rb +57 -0
- data/lib/ml_dsa/config.rb +69 -0
- data/lib/ml_dsa/internal.rb +76 -0
- data/lib/ml_dsa/key_pair.rb +39 -0
- data/lib/ml_dsa/parameter_set.rb +89 -0
- data/lib/ml_dsa/public_key.rb +180 -0
- data/lib/ml_dsa/requests.rb +96 -0
- data/lib/ml_dsa/secret_key.rb +221 -0
- data/lib/ml_dsa/version.rb +5 -0
- data/lib/ml_dsa.rb +277 -0
- data/patches/README.md +55 -0
- data/patches/pqclean-explicit-rnd.patch +64 -0
- data/sig/ml_dsa.rbs +178 -0
- data/test/fixtures/kat_vectors.yaml +16 -0
- metadata +194 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a532543c1f2654a5ee484919c2700a3f46731ee63033fd1ad59b8bd3873fe9fe
|
|
4
|
+
data.tar.gz: 449a09788a28127d8bb17aa98046131db5e7854021f206b8210cd7f95f4c9d0f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c399d7e930247d10232805993dac0a5911f7e8d07b21e7b8dcb3bcba22675be366adfcd694f2da130340532c1d7975f9c505f902fad44b7eefa88c91b3ed265a
|
|
7
|
+
data.tar.gz: c2eb08be27b1582834b6f5e54f610beeca61ec52b727bcdcbf3ac6b1ed76b2fb94dd6e12dc39e07099053bf76ca536025426cabbab7f76d969b641203170ee1e
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2026-03-18
|
|
9
|
+
|
|
10
|
+
Initial release implementing NIST FIPS 204 ML-DSA (Module-Lattice-Based
|
|
11
|
+
Digital Signature Algorithm) as a Ruby C extension.
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
#### Core cryptography
|
|
16
|
+
- Three parameter sets: ML-DSA-44 (NIST Level 2), ML-DSA-65 (Level 3),
|
|
17
|
+
ML-DSA-87 (Level 5), backed by vendored PQClean clean C implementation.
|
|
18
|
+
- `MlDsa.keygen(param_set)` — key pair generation with optional `seed:`
|
|
19
|
+
for deterministic keygen from a 32-byte seed.
|
|
20
|
+
- `SecretKey#sign(message, deterministic:, context:)` — hedged (randomized,
|
|
21
|
+
default) and deterministic signing with optional FIPS 204 context strings.
|
|
22
|
+
- `PublicKey#verify(message, signature, context:)` — signature verification.
|
|
23
|
+
- Batch operations: `MlDsa.sign_many` / `MlDsa.verify_many` execute
|
|
24
|
+
multiple operations in a single GVL release for thread concurrency.
|
|
25
|
+
- `MlDsa.batch { |b| ... }` — block-based batch builder that collects
|
|
26
|
+
sign or verify operations and executes them together.
|
|
27
|
+
- `verify_many` returns `Array[Result]` with per-item `.ok?` and `.reason`
|
|
28
|
+
(distinguishes `wrong_signature_size` from `verification_failed`).
|
|
29
|
+
- Build-time parameter set selection: `--with-ml-dsa-params=44,65` to
|
|
30
|
+
compile only a subset (reduces binary size).
|
|
31
|
+
|
|
32
|
+
#### Key management
|
|
33
|
+
- `SecretKey#public_key` — returns the associated `PublicKey` from keygen
|
|
34
|
+
(nil for deserialized keys).
|
|
35
|
+
- `SecretKey.from_seed(seed, param_set)` — reconstruct from a 32-byte seed
|
|
36
|
+
with `public_key` attached automatically.
|
|
37
|
+
- `SecretKey#seed` — access the keygen seed for compact storage (nil if not
|
|
38
|
+
created from a seed). Securely zeroed on `wipe!` and GC.
|
|
39
|
+
- `PublicKey#fingerprint` — SHA-256 prefix (32 hex chars) for identification
|
|
40
|
+
in logs and UIs.
|
|
41
|
+
- `PublicKey#created_at` / `SecretKey#created_at` — timestamp set at
|
|
42
|
+
construction for audit trails and key rotation policies.
|
|
43
|
+
- `PublicKey#key_usage=` / `SecretKey#key_usage=` — optional `Symbol`
|
|
44
|
+
metadata for application-defined usage labels.
|
|
45
|
+
|
|
46
|
+
#### Serialization
|
|
47
|
+
- Raw bytes: `to_bytes` / `from_bytes` with auto-detection of parameter set
|
|
48
|
+
from byte length.
|
|
49
|
+
- Hex: `to_hex` / `from_hex` with auto-detection.
|
|
50
|
+
- DER: `to_der` / `from_der` — SubjectPublicKeyInfo for public keys,
|
|
51
|
+
PKCS#8 / OneAsymmetricKey for secret keys (OIDs per FIPS 204).
|
|
52
|
+
- PEM: `to_pem` / `from_pem` — PEM-armored DER.
|
|
53
|
+
- DER/PEM handled by the [`pqc_asn1`](https://github.com/msuliq/pqc_asn1)
|
|
54
|
+
gem with no OpenSSL dependency. Secret key DER intermediates are held in
|
|
55
|
+
`PqcAsn1::SecureBuffer` (mmap-protected, securely zeroed).
|
|
56
|
+
|
|
57
|
+
#### Secret key hygiene
|
|
58
|
+
- Key bytes live in C-managed memory, securely zeroed on GC.
|
|
59
|
+
- `SecretKey#with_bytes { |buf| ... }` — controlled access with automatic
|
|
60
|
+
wipe on block exit (even on exception).
|
|
61
|
+
- `SecretKey#wipe!` — explicit zeroing; subsequent operations raise
|
|
62
|
+
`MlDsa::Error`.
|
|
63
|
+
- Memory locking: `mlock` prevents secret key pages from swapping to disk.
|
|
64
|
+
- No `SecretKey#to_bytes` or `#to_hex` — prevents accidental key leakage
|
|
65
|
+
into logs, exception messages, or long-lived Ruby Strings.
|
|
66
|
+
- `dup` / `clone` and `Marshal.dump` raise `TypeError` on both key classes.
|
|
67
|
+
|
|
68
|
+
#### Configuration and instrumentation
|
|
69
|
+
- `MlDsa::Config` — encapsulates mutable state (subscribers, RNG). All
|
|
70
|
+
operations accept optional `config:` for per-Ractor or per-test contexts.
|
|
71
|
+
- `MlDsa.subscribe { |event| ... }` — audit logging hooks with
|
|
72
|
+
`:operation`, `:param_set`, `:count`, `:duration_ns`. No key material
|
|
73
|
+
exposed.
|
|
74
|
+
- `MlDsa.random_source=` — pluggable RNG for testing or HSM integration.
|
|
75
|
+
Keygen uses it to generate a seed; signing uses it for the rnd nonce.
|
|
76
|
+
- `yield_every:` keyword on batch operations for cooperative fiber
|
|
77
|
+
scheduling in async servers.
|
|
78
|
+
|
|
79
|
+
#### Concurrency
|
|
80
|
+
- GVL release: all crypto operations run without the Global VM Lock.
|
|
81
|
+
- `PublicKey` is Ractor-shareable (`RUBY_TYPED_FROZEN_SHAREABLE`, Ruby 3.0+).
|
|
82
|
+
- Thread-safe wipe detection via C11 `_Atomic` with acquire/release semantics.
|
|
83
|
+
- Ractor-compatible instrumentation (silently no-op in non-main Ractors).
|
|
84
|
+
|
|
85
|
+
#### PQC namespace
|
|
86
|
+
- `PQC::MlDsa` alias with `PQC.register` / `PQC.algorithms` /
|
|
87
|
+
`PQC.algorithm(:name)` registry for future multi-algorithm discovery.
|
|
88
|
+
|
|
89
|
+
#### Error handling
|
|
90
|
+
- `MlDsa::Error` base class with subclasses `Error::KeyGeneration`,
|
|
91
|
+
`Error::Signing`, `Error::Deserialization`.
|
|
92
|
+
- Deserialization errors include `format`, `position`, and `reason` metadata.
|
|
93
|
+
- `ParameterSet` includes `Comparable` for sorting and comparison.
|
|
94
|
+
|
|
95
|
+
### Security
|
|
96
|
+
- Secure zeroing: `SecureZeroMemory` (Windows), `explicit_bzero`
|
|
97
|
+
(Linux/BSD/macOS), `memset_s` (C11), or volatile fallback with compiler
|
|
98
|
+
fence.
|
|
99
|
+
- Constant-time secret key comparison via XOR-accumulate with compiler fence.
|
|
100
|
+
- Platform-appropriate CSPRNG: `getrandom(2)` (Linux), `arc4random_buf`
|
|
101
|
+
(macOS/BSD), `BCryptGenRandom` (Windows).
|
|
102
|
+
- `-fvisibility=hidden` prevents PQClean symbols from clashing with other gems.
|
|
103
|
+
- Process-salted hashing for `#hash` method.
|
|
104
|
+
- Heap buffers freed via `rb_ensure` — no leak on exceptions.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Copyright 2026 Suleyman Musayev
|
|
2
|
+
|
|
3
|
+
This project is dual-licensed. You may use, distribute, and modify it under
|
|
4
|
+
the terms of either:
|
|
5
|
+
|
|
6
|
+
* the MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT), or
|
|
7
|
+
* the Apache License, Version 2.0 (LICENSE-APACHE or
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0)
|
|
9
|
+
|
|
10
|
+
at your option.
|
|
11
|
+
|
|
12
|
+
Unless you explicitly state otherwise, any Contribution intentionally submitted
|
|
13
|
+
for inclusion in this project by you, as defined in the Apache-2.0 license,
|
|
14
|
+
shall be dual-licensed as above, without any additional terms or conditions.
|
data/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as
|
|
57
|
+
"Not a Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and subsequently
|
|
61
|
+
incorporated within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or a Contribution
|
|
81
|
+
incorporated within the Work constitutes direct or contributory patent
|
|
82
|
+
infringement, then any patent licenses granted to You under this License
|
|
83
|
+
for that Work shall terminate as of the date such litigation is filed.
|
|
84
|
+
|
|
85
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
86
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
87
|
+
modifications, and in Source or Object form, provided that You
|
|
88
|
+
meet the following conditions:
|
|
89
|
+
|
|
90
|
+
(a) You must give any other recipients of the Work or
|
|
91
|
+
Derivative Works a copy of this License; and
|
|
92
|
+
|
|
93
|
+
(b) You must cause any modified files to carry prominent notices
|
|
94
|
+
stating that You changed the files; and
|
|
95
|
+
|
|
96
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
97
|
+
that You distribute, all copyright, patent, trademark, and
|
|
98
|
+
attribution notices from the Source form of the Work,
|
|
99
|
+
excluding those notices that do not pertain to any part of
|
|
100
|
+
the Derivative Works; and
|
|
101
|
+
|
|
102
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
103
|
+
distribution, You must include a readable copy of the
|
|
104
|
+
attribution notices contained within such NOTICE file, in
|
|
105
|
+
at least one of the following places: within a NOTICE text file
|
|
106
|
+
distributed as part of the Derivative Works; within the Source
|
|
107
|
+
form or documentation, if provided along with the Derivative
|
|
108
|
+
Works; or, within a display generated by the Derivative Works,
|
|
109
|
+
if and wherever such third-party notices normally appear. The
|
|
110
|
+
contents of the NOTICE file are for informational purposes only
|
|
111
|
+
and do not modify the License. You may add Your own attribution
|
|
112
|
+
notices within Derivative Works that You distribute, alongside
|
|
113
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
114
|
+
that such additional attribution notices cannot be construed
|
|
115
|
+
as modifying the License.
|
|
116
|
+
|
|
117
|
+
You may add Your own license statement for Your modifications and
|
|
118
|
+
may provide additional grant of rights to use, reproduce, modify,
|
|
119
|
+
prepare Derivative Works of, publicly display, publicly perform,
|
|
120
|
+
sublicense, and distribute those modifications and additional rights
|
|
121
|
+
as a separate license, provided Your use, reproduction, modification,
|
|
122
|
+
preparation of Derivative Works, distribution, and other activities
|
|
123
|
+
remain in full compliance with the terms of this License.
|
|
124
|
+
|
|
125
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
126
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
127
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
128
|
+
this License, without any additional terms or conditions.
|
|
129
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
130
|
+
the terms of any separate license agreement you may have executed
|
|
131
|
+
with Licensor regarding such Contributions.
|
|
132
|
+
|
|
133
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
134
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
135
|
+
except as required for reasonable and customary use in describing the
|
|
136
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
137
|
+
|
|
138
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
139
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
140
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
141
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
142
|
+
implied, including, without limitation, any warranties or conditions
|
|
143
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
144
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
145
|
+
appropriateness of using or reproducing the Work and assume any
|
|
146
|
+
risks associated with Your exercise of permissions under this License.
|
|
147
|
+
|
|
148
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
149
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
150
|
+
unless required by applicable law (such as deliberate and grossly
|
|
151
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
152
|
+
liable to You for damages, including any direct, indirect, special,
|
|
153
|
+
incidental, or consequential damages of any character arising as a
|
|
154
|
+
result of this License or out of the use or inability to use the
|
|
155
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
156
|
+
work stoppage, computer failure or malfunction, or all other
|
|
157
|
+
commercial damages or losses), even if such Contributor has been
|
|
158
|
+
advised of the possibility of such damages.
|
|
159
|
+
|
|
160
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
161
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
162
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
163
|
+
or other liability obligations and/or rights consistent with this
|
|
164
|
+
License. However, in accepting such obligations, You may act only
|
|
165
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
166
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
167
|
+
defend, and hold each Contributor harmless for any liability
|
|
168
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
169
|
+
of your accepting any such warranty or additional liability.
|
|
170
|
+
|
|
171
|
+
END OF TERMS AND CONDITIONS
|
|
172
|
+
|
|
173
|
+
Copyright 2026 Suleyman Musayev
|
|
174
|
+
|
|
175
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
176
|
+
you may not use this file except in compliance with the License.
|
|
177
|
+
You may obtain a copy of the License at
|
|
178
|
+
|
|
179
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
180
|
+
|
|
181
|
+
Unless required by applicable law or agreed to in writing, software
|
|
182
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
183
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
184
|
+
See the License for the specific language governing permissions and
|
|
185
|
+
limitations under the License.
|
data/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Suleyman Musayev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# ml_dsa
|
|
2
|
+
|
|
3
|
+
Ruby C extension for **ML-DSA** (NIST FIPS 204), the post-quantum digital
|
|
4
|
+
signature algorithm formerly known as CRYSTALS-Dilithium.
|
|
5
|
+
|
|
6
|
+
Bundles the [PQClean](https://github.com/PQClean/PQClean) clean C
|
|
7
|
+
implementation for all three standardized parameter sets:
|
|
8
|
+
|
|
9
|
+
| Parameter Set | NIST Security Level | Public Key | Secret Key | Signature |
|
|
10
|
+
|---------------|---------------------|------------|------------|-----------|
|
|
11
|
+
| ML-DSA-44 | 2 | 1,312 B | 2,560 B | 2,420 B |
|
|
12
|
+
| ML-DSA-65 | 3 | 1,952 B | 4,032 B | 3,309 B |
|
|
13
|
+
| ML-DSA-87 | 5 | 2,592 B | 4,896 B | 4,627 B |
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
gem "ml_dsa"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
gem install ml_dsa
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Compile only a subset of parameter sets to reduce binary size:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
gem install ml_dsa -- --with-ml-dsa-params=44,65
|
|
29
|
+
bundle config build.ml_dsa --with-ml-dsa-params=65
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Requirements
|
|
33
|
+
|
|
34
|
+
- Ruby >= 2.7.2
|
|
35
|
+
- C11-compatible compiler (GCC, Clang, MSVC)
|
|
36
|
+
- Linux, macOS (Intel + ARM), or Windows
|
|
37
|
+
- No OpenSSL dependency
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
### Key generation
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
require "ml_dsa"
|
|
45
|
+
|
|
46
|
+
pk, sk = MlDsa.keygen(MlDsa::ML_DSA_65)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Deterministic keygen from a 32-byte seed:
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
seed = SecureRandom.random_bytes(32)
|
|
53
|
+
pk, sk = MlDsa.keygen(MlDsa::ML_DSA_65, seed: seed)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Signing and verification
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
message = "Hello, post-quantum world!"
|
|
60
|
+
|
|
61
|
+
signature = sk.sign(message) # hedged (randomized)
|
|
62
|
+
signature = sk.sign(message, deterministic: true) # deterministic
|
|
63
|
+
signature = sk.sign(message, context: "app-v1") # with FIPS 204 context
|
|
64
|
+
|
|
65
|
+
pk.verify(message, signature) # => true
|
|
66
|
+
pk.verify(message, signature, context: "app-v1") # => true
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Batch operations
|
|
70
|
+
|
|
71
|
+
Sign or verify multiple messages in a single GVL release:
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
signatures = MlDsa.sign_many([
|
|
75
|
+
MlDsa::SignRequest.new(sk: sk, message: "msg1"),
|
|
76
|
+
MlDsa::SignRequest.new(sk: sk, message: "msg2"),
|
|
77
|
+
])
|
|
78
|
+
|
|
79
|
+
results = MlDsa.verify_many([
|
|
80
|
+
MlDsa::VerifyRequest.new(pk: pk, message: "msg1", signature: signatures[0]),
|
|
81
|
+
MlDsa::VerifyRequest.new(pk: pk, message: "msg2", signature: signatures[1]),
|
|
82
|
+
])
|
|
83
|
+
|
|
84
|
+
results.each do |r|
|
|
85
|
+
puts r.ok? ? "valid" : "failed: #{r.reason}"
|
|
86
|
+
end
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Block-based batch builder:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
sigs = MlDsa.batch { |b|
|
|
93
|
+
b.sign(sk: sk, message: "msg1")
|
|
94
|
+
b.sign(sk: sk, message: "msg2")
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Serialization
|
|
99
|
+
|
|
100
|
+
```ruby
|
|
101
|
+
# Raw bytes — param_set auto-detected from byte size
|
|
102
|
+
pk2 = MlDsa::PublicKey.from_bytes(pk.to_bytes)
|
|
103
|
+
|
|
104
|
+
# Hex — param_set auto-detected from byte size
|
|
105
|
+
pk3 = MlDsa::PublicKey.from_hex(pk.to_hex)
|
|
106
|
+
|
|
107
|
+
# DER (SubjectPublicKeyInfo / PKCS#8) — param_set auto-detected from OID
|
|
108
|
+
pk4 = MlDsa::PublicKey.from_der(pk.to_der)
|
|
109
|
+
sk2 = MlDsa::SecretKey.from_der(sk.to_der)
|
|
110
|
+
|
|
111
|
+
# PEM
|
|
112
|
+
pk5 = MlDsa::PublicKey.from_pem(pk.to_pem)
|
|
113
|
+
sk3 = MlDsa::SecretKey.from_pem(sk.to_pem)
|
|
114
|
+
|
|
115
|
+
# Seed-only compact storage (32 bytes instead of full key)
|
|
116
|
+
sk4 = MlDsa::SecretKey.from_seed(seed, MlDsa::ML_DSA_65)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Key management
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
# Secret keys from keygen/from_seed carry the associated public key
|
|
123
|
+
sk.public_key == pk # => true
|
|
124
|
+
sk.seed # => 32-byte seed (nil if not created from seed)
|
|
125
|
+
|
|
126
|
+
# Keys deserialized from bytes/DER/PEM have no associated public key
|
|
127
|
+
MlDsa::SecretKey.from_der(sk.to_der).public_key # => nil
|
|
128
|
+
|
|
129
|
+
# Fingerprint for logs and UIs (SHA-256 prefix, 32 hex chars)
|
|
130
|
+
pk.fingerprint # => "a3b1c9f0e2d4..."
|
|
131
|
+
|
|
132
|
+
# Timestamps and metadata
|
|
133
|
+
pk.created_at # => Time
|
|
134
|
+
sk.key_usage = :signing # application-defined label
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Secret key hygiene
|
|
138
|
+
|
|
139
|
+
Secret keys live in C-managed memory with `mlock` (prevents swap) and
|
|
140
|
+
`secure_zero` on GC. There is no `to_bytes` or `to_hex` on secret keys.
|
|
141
|
+
|
|
142
|
+
```ruby
|
|
143
|
+
# Controlled access — buffer is wiped on block exit, even on exception
|
|
144
|
+
sk.with_bytes do |buf|
|
|
145
|
+
# buf is a temporary binary String
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Explicit wipe — subsequent operations raise MlDsa::Error
|
|
149
|
+
sk.wipe!
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Pluggable RNG
|
|
153
|
+
|
|
154
|
+
```ruby
|
|
155
|
+
MlDsa.random_source = proc { |n| "\x42" * n } # for testing / HSM
|
|
156
|
+
MlDsa.random_source = nil # restore OS CSPRNG
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Instrumentation
|
|
160
|
+
|
|
161
|
+
```ruby
|
|
162
|
+
subscriber = MlDsa.subscribe do |event|
|
|
163
|
+
logger.info "#{event[:operation]} #{event[:param_set].name} " \
|
|
164
|
+
"count=#{event[:count]} duration=#{event[:duration_ns]}ns"
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
MlDsa.unsubscribe(subscriber)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Isolated configuration for per-Ractor or per-test contexts:
|
|
171
|
+
|
|
172
|
+
```ruby
|
|
173
|
+
cfg = MlDsa::Config.new
|
|
174
|
+
cfg.random_source = proc { |n| SecureRandom.random_bytes(n) }
|
|
175
|
+
pk, sk = MlDsa.keygen(MlDsa::ML_DSA_65, config: cfg)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### PQC namespace
|
|
179
|
+
|
|
180
|
+
```ruby
|
|
181
|
+
PQC::MlDsa == MlDsa # => true
|
|
182
|
+
PQC.algorithms # => { ml_dsa: MlDsa }
|
|
183
|
+
PQC.algorithm(:ml_dsa) # => MlDsa
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Error handling
|
|
187
|
+
|
|
188
|
+
```ruby
|
|
189
|
+
begin
|
|
190
|
+
MlDsa::PublicKey.from_der(bad_data)
|
|
191
|
+
rescue MlDsa::Error::Deserialization => e
|
|
192
|
+
e.message # => "invalid DER: ..."
|
|
193
|
+
e.format # => "DER"
|
|
194
|
+
e.position # => 12
|
|
195
|
+
e.reason # => :unknown_oid
|
|
196
|
+
end
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
- `MlDsa::Error` — base class
|
|
200
|
+
- `MlDsa::Error::KeyGeneration`
|
|
201
|
+
- `MlDsa::Error::Signing`
|
|
202
|
+
- `MlDsa::Error::Deserialization` — includes `format`, `position`, `reason`
|
|
203
|
+
|
|
204
|
+
## Security
|
|
205
|
+
|
|
206
|
+
| Property | Implementation |
|
|
207
|
+
|----------|---------------|
|
|
208
|
+
| Secure zeroing | `SecureZeroMemory` / `explicit_bzero` / `memset_s` / volatile fallback |
|
|
209
|
+
| Constant-time comparison | XOR-accumulate with compiler fence for secret key equality |
|
|
210
|
+
| Memory locking | `mlock` prevents secret key pages from swapping to disk |
|
|
211
|
+
| Thread-safe wipe | C11 `_Atomic` with acquire/release semantics |
|
|
212
|
+
| GVL release | All crypto runs without the Global VM Lock |
|
|
213
|
+
| Ractor safety | `PublicKey` is Ractor-shareable (`RUBY_TYPED_FROZEN_SHAREABLE`) |
|
|
214
|
+
| Symbol isolation | `-fvisibility=hidden` prevents PQClean symbol clashes |
|
|
215
|
+
| No OpenSSL | DER/PEM via [`pqc_asn1`](https://github.com/msuliq/pqc_asn1) gem |
|
|
216
|
+
|
|
217
|
+
## Development
|
|
218
|
+
|
|
219
|
+
```sh
|
|
220
|
+
bundle install
|
|
221
|
+
bundle exec rake compile
|
|
222
|
+
bundle exec rake test
|
|
223
|
+
bundle exec rake bench # benchmarks (requires benchmark-ips)
|
|
224
|
+
bundle exec rake yard # API docs
|
|
225
|
+
bundle exec standardrb # Ruby lint
|
|
226
|
+
bundle exec rake lint:c # C static analysis (requires cppcheck)
|
|
227
|
+
bundle exec rake pqclean:verify # verify vendored PQClean patches
|
|
228
|
+
bundle exec rake generate:impl # regenerate amalgamation files
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## License
|
|
232
|
+
|
|
233
|
+
Dual-licensed under [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE),
|
|
234
|
+
at your option.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mkmf"
|
|
4
|
+
|
|
5
|
+
# Allow selecting which parameter sets to compile at build time.
|
|
6
|
+
# Defaults to all three. To compile only a subset:
|
|
7
|
+
# gem install ml_dsa -- --with-ml-dsa-params=65
|
|
8
|
+
# bundle config build.ml_dsa --with-ml-dsa-params=44,65
|
|
9
|
+
params_config = with_config("ml-dsa-params", "44,65,87").to_s
|
|
10
|
+
enabled = params_config
|
|
11
|
+
.split(",")
|
|
12
|
+
.filter_map { |s| Integer(s.strip) rescue nil } # standard:disable Style/RescueModifier
|
|
13
|
+
.select { |n| [44, 65, 87].include?(n) }
|
|
14
|
+
.uniq
|
|
15
|
+
|
|
16
|
+
if enabled.empty?
|
|
17
|
+
abort "No valid ML-DSA parameter sets. " \
|
|
18
|
+
"Use --with-ml-dsa-params=44,65,87 (any non-empty subset)."
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Pass compile-time flags for each enabled parameter set
|
|
22
|
+
enabled.each { |ps| $CFLAGS << " -DML_DSA_ENABLE_#{ps}" }
|
|
23
|
+
|
|
24
|
+
# Compile only the impl files for enabled parameter sets;
|
|
25
|
+
# fips202.c and randombytes.c are always needed.
|
|
26
|
+
$srcs = %w[ml_dsa_ext.c fips202.c randombytes.c]
|
|
27
|
+
enabled.each { |ps| $srcs << "ml_dsa_#{ps}_impl.c" }
|
|
28
|
+
|
|
29
|
+
# -std=c11 for C11 features; -I$(srcdir) is added automatically by mkmf,
|
|
30
|
+
# making fips202.h and randombytes.h visible to amalgamation impl files.
|
|
31
|
+
# -fvisibility=hidden prevents PQClean helper symbols (shake256, poly_ntt,
|
|
32
|
+
# etc.) from leaking into the process symbol table and conflicting with
|
|
33
|
+
# other gems that bundle different versions of the same libraries.
|
|
34
|
+
$CFLAGS << " -O2 -Wall -Wextra -Wshadow -std=c11"
|
|
35
|
+
$CFLAGS << " -fvisibility=hidden" if try_cflags("-fvisibility=hidden")
|
|
36
|
+
|
|
37
|
+
# Probe for platform-appropriate secure zeroing
|
|
38
|
+
have_func("explicit_bzero", ["string.h"])
|
|
39
|
+
have_func("memset_s", ["string.h"])
|
|
40
|
+
|
|
41
|
+
# Probe for mlock (prevent secret key pages from being swapped to disk)
|
|
42
|
+
have_func("mlock", ["sys/mman.h"])
|
|
43
|
+
|
|
44
|
+
# Probe for Ractor safety API (Ruby 3.0+)
|
|
45
|
+
have_func("rb_ext_ractor_safe", ["ruby.h"])
|
|
46
|
+
|
|
47
|
+
create_makefile("ml_dsa/ml_dsa_ext")
|