ml_kem 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/LICENSE.txt +21 -0
- data/README.md +88 -0
- data/Rakefile +8 -0
- data/exe/mlkem +7 -0
- data/lib/ml_kem/version.rb +10 -0
- data/lib/ml_kem.rb +89 -0
- data/sig/ml_kem.rbs +4 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5d0131894b513113fb5492afe072bfec6ac2ba8d64531dbbd9c712cf419aff3a
|
4
|
+
data.tar.gz: f61a2aa77bef7d515d8b55c10dc66f07342b408d8884fbeca88e3d1fe673d4c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 83a36606aa8c28521590dcd2c10a60f74b9e76e90562c60f2b1b3af188294567736c45ba128d40a322e63d65640461be8720004b7bd72a553f4db427655d9573
|
7
|
+
data.tar.gz: 1bf7d2be9236d916e4e5043f2377c630632a48277459e17ca521ba5890b61c54c31591c4fef6e86ab2bb22d12ee4c3e3cfefc9cd84c84953775cdf30746796bf
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 MarioRgzLpz
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# MLKEM
|
2
|
+
|
3
|
+
**ml_kem** is a Ruby gem that implements the [ML-KEM (Kyber)](https://csrc.nist.gov/pubs/fips/203/final) post-quantum key encapsulation mechanism (KEM), as selected by NIST for standardization under the FIPS 203. It supports key generation, encapsulation, and decapsulation in pure Ruby and includes a command-line interface for easy integration into workflows.
|
4
|
+
|
5
|
+
# Features
|
6
|
+
|
7
|
+
- Support for all ML-KEM variants: `ml_kem_512`, `ml_kem_768`, and `ml_kem_1024`
|
8
|
+
- Public/private key generation
|
9
|
+
- Secure encapsulation of a shared secret using the public key
|
10
|
+
- Decapsulation of a ciphertext using the private key
|
11
|
+
- Command-Line Interface (CLI) built with Thor
|
12
|
+
- PEM encoding for key files
|
13
|
+
- Easy to use and integrate into Ruby applications
|
14
|
+
|
15
|
+
# Installation
|
16
|
+
|
17
|
+
Install the gem and add to the application's Gemfile by executing:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
bundle add ml_kem
|
21
|
+
```
|
22
|
+
|
23
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
gem install ml_kem
|
27
|
+
```
|
28
|
+
|
29
|
+
# Usage
|
30
|
+
|
31
|
+
## CLI
|
32
|
+
|
33
|
+
The `mlkem` command-line tool allows you to generate keys, encapsulate, and decapsulate secrets using ML-KEM.
|
34
|
+
|
35
|
+
- **Generate keys:**
|
36
|
+
|
37
|
+
```bash
|
38
|
+
mlkem keygen -p public_key.pem -s private_key.pem -v ml_kem_768
|
39
|
+
```
|
40
|
+
|
41
|
+
- **Encapsulate a secret:**
|
42
|
+
|
43
|
+
```bash
|
44
|
+
mlkem encaps -p public_key.pem -c ciphertext.txt -k shared_secret.key
|
45
|
+
```
|
46
|
+
|
47
|
+
- **Decapsulate:**
|
48
|
+
|
49
|
+
```bash
|
50
|
+
mlkem decaps -s private_key.pem -c ciphertext.txt -k shared_secret.key
|
51
|
+
```
|
52
|
+
|
53
|
+
To obtain information about the options available for a command use:
|
54
|
+
```bash
|
55
|
+
mlkem help COMMAND
|
56
|
+
```
|
57
|
+
|
58
|
+
## Code
|
59
|
+
|
60
|
+
```bash
|
61
|
+
require "ml_kem"
|
62
|
+
|
63
|
+
# Create an instance with the desired variant
|
64
|
+
|
65
|
+
kem = MLKEM::MLKEM.new(variant: :ml_kem_768)
|
66
|
+
|
67
|
+
# Generate key pair
|
68
|
+
public_key, private_key = kem.keygen
|
69
|
+
|
70
|
+
# Encapsulate a shared secret
|
71
|
+
shared_secret, ciphertext = kem.encaps(public_key)
|
72
|
+
|
73
|
+
# Decapsulate to recover the shared secret
|
74
|
+
recovered_secret = kem.decaps(private_key, ciphertext)
|
75
|
+
|
76
|
+
```
|
77
|
+
|
78
|
+
## Development
|
79
|
+
|
80
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will start an IRB and allow you to experiment.
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/MarioRgzLpz/ml_kem.
|
85
|
+
|
86
|
+
## License
|
87
|
+
|
88
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/exe/mlkem
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Principal module of ML-KEM, a post-quantum key encapsulation mechanism.
|
4
|
+
# Every class and module in this gem is nested under this module.
|
5
|
+
#
|
6
|
+
# @author MarioRgzLpz <https://github.com/MarioRgzLpz>
|
7
|
+
# @since 0.1.0
|
8
|
+
module MLKEM
|
9
|
+
VERSION = "0.1.0"
|
10
|
+
end
|
data/lib/ml_kem.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'securerandom'
|
4
|
+
require_relative 'ml_kem/version'
|
5
|
+
require_relative 'ml_kem/constants'
|
6
|
+
require_relative 'ml_kem/math/byte_operations'
|
7
|
+
require_relative 'ml_kem/math/polynomial'
|
8
|
+
require_relative 'ml_kem/math/ntt'
|
9
|
+
require_relative 'ml_kem/math/sampling'
|
10
|
+
require_relative 'ml_kem/crypto/hash_functions'
|
11
|
+
require_relative 'ml_kem/crypto/symmetric_primitives'
|
12
|
+
require_relative 'ml_kem/core/k_pke'
|
13
|
+
require_relative 'ml_kem/core/ml_kem_internal'
|
14
|
+
|
15
|
+
module MLKEM
|
16
|
+
# Raised when an invalid ML-KEM variant is selected.
|
17
|
+
class InvalidParameterError < StandardError; end
|
18
|
+
|
19
|
+
# Raised when cryptographic randomness fails.
|
20
|
+
class CryptographicError < StandardError; end
|
21
|
+
|
22
|
+
# Public API class for ML-KEM (Kyber) post-quantum key encapsulation mechanism.
|
23
|
+
# Supports key generation, encapsulation, and decapsulation per NIST standard.
|
24
|
+
#
|
25
|
+
# @author MarioRgzLpz
|
26
|
+
# @since 0.1.0
|
27
|
+
#
|
28
|
+
# @example Basic usage
|
29
|
+
# kem = MLKEM::MLKEM.new(variant: :ml_kem_768)
|
30
|
+
# ek, dk = kem.keygen
|
31
|
+
# k_enc, c = kem.encaps(ek)
|
32
|
+
# k_dec = kem.decaps(dk, c)
|
33
|
+
# raise unless k_enc == k_dec
|
34
|
+
class MLKEM
|
35
|
+
# Initializes the ML-KEM system with a given variant.
|
36
|
+
#
|
37
|
+
# @param [Symbol] variant One of `:ml_kem_512`, `:ml_kem_768`, or `:ml_kem_1024`.
|
38
|
+
# @raise [InvalidParameterError] if the variant is unsupported.
|
39
|
+
def initialize(variant: :ml_kem_768)
|
40
|
+
params = Constants::PARAM_SETS[variant.to_s.upcase.gsub('_', '-')]
|
41
|
+
raise InvalidParameterError, "Unsupported variant: #{variant}" unless params
|
42
|
+
@internal = Core::MLKEMInternal.new(*params)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Key generation method (Algorithm 17).
|
46
|
+
# Produces a public and private key pair.
|
47
|
+
#
|
48
|
+
# @return [Array<String>] [ek, dk] Public and private keys (as binary strings).
|
49
|
+
# @raise [CryptographicError] if randomness generation fails.
|
50
|
+
#
|
51
|
+
# @example
|
52
|
+
# ek, dk = kem.keygen
|
53
|
+
def keygen
|
54
|
+
d = SecureRandom.random_bytes(32)
|
55
|
+
z = SecureRandom.random_bytes(32)
|
56
|
+
raise CryptographicError, "Random bytes generation failed" if d.nil? || z.nil?
|
57
|
+
|
58
|
+
@internal.keygen_internal(d, z)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Encapsulation method (Algorithm 18).
|
62
|
+
# Derives a shared secret and ciphertext using the public key.
|
63
|
+
#
|
64
|
+
# @param [String] ek Public key (as produced by `#keygen`)
|
65
|
+
# @return [Array<String>] [k, c] Shared secret and ciphertext
|
66
|
+
# @raise [CryptographicError] if randomness generation fails.
|
67
|
+
#
|
68
|
+
# @example
|
69
|
+
# k, c = kem.encaps(ek)
|
70
|
+
def encaps(ek)
|
71
|
+
m = SecureRandom.random_bytes(32)
|
72
|
+
raise CryptographicError, "Random bytes generation failed" if m.nil?
|
73
|
+
@internal.encaps_internal(ek, m)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Decapsulation method (Algorithm 19).
|
77
|
+
# Recovers the shared secret from the private key and ciphertext.
|
78
|
+
#
|
79
|
+
# @param [String] dk Private key
|
80
|
+
# @param [String] c Ciphertext (as received from encapsulation)
|
81
|
+
# @return [String] Shared secret (32 bytes)
|
82
|
+
#
|
83
|
+
# @example
|
84
|
+
# k = kem.decaps(dk, c)
|
85
|
+
def decaps(dk, c)
|
86
|
+
@internal.decaps_internal(dk, c)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/sig/ml_kem.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ml_kem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- MarioRgzLpz
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-07-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sha3
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.2.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.2.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: thor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yard
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.22'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.22'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '13.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '13.3'
|
83
|
+
description: A Ruby gem providing an implementation of the ML-KEM (formerly Kyber)
|
84
|
+
key-encapsulation mechanism for post-quantum cryptography standards.
|
85
|
+
email:
|
86
|
+
- MarioRgzLpz@correo.ugr.es
|
87
|
+
executables:
|
88
|
+
- mlkem
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- exe/mlkem
|
96
|
+
- lib/ml_kem.rb
|
97
|
+
- lib/ml_kem/version.rb
|
98
|
+
- sig/ml_kem.rbs
|
99
|
+
homepage: https://github.com/MarioRgzLpz/ml_kem
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata:
|
103
|
+
homepage_uri: https://github.com/MarioRgzLpz/ml_kem
|
104
|
+
source_code_uri: https://github.com/MarioRgzLpz/ml_kem
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: 3.1.0
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubygems_version: 3.3.15
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Implementation of ML-KEM (Kyber) post-quantum cryptography algorithm.
|
124
|
+
test_files: []
|