wots 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6efca98c49499aae50fd5225762614026111f16cfc4ccee573e6ea6857e0664
4
+ data.tar.gz: 236faef3ed700463ea083a7cae7de8d6365663e5ad1e65c9819dab21e92fb6d3
5
+ SHA512:
6
+ metadata.gz: b8eefc4c86bf05cfa7820e291bcbacf3d1fba792d91a7392f086147979468e8fe8caa62de59c7f47c00bc65145ab9d82f91cbba35215f295ddb85484dea6d477
7
+ data.tar.gz: 0a3a1affaa6a0d9e9298927cfd16e12a3e84aaa4a8b558f4b739002e2005a4d07f0747156c2716c12e879d5770b3c7aa06a852a47f148b9b3ebeb32f7026d27a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ wots
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.4.3
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 azuchi
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,85 @@
1
+ # WOTS+
2
+
3
+ A Ruby implementation of the Winternitz One-Time Signature Plus (WOTS+) scheme as described in [RFC 8391](https://datatracker.ietf.org/doc/html/rfc8391).
4
+
5
+ WOTS+ is a post-quantum cryptographic signature scheme that provides security against quantum computer attacks.
6
+
7
+ ## Features
8
+
9
+ - Complete implementation of WOTS+ as specified in RFC 8391
10
+ - Support for multiple hash functions:
11
+ - `WOTSP-SHA2_256` (SHA-256)
12
+ - `WOTSP-SHA2_512` (SHA-512)
13
+ - `WOTSP-SHAKE_256` (SHAKE-256)
14
+ - Support for Winternitz parameters w=4 and w=16
15
+ - Key generation from seed
16
+ - Digital signature generation and verification
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'wots'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle install
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install wots
33
+
34
+ ## Usage
35
+
36
+ ### Basic Example
37
+
38
+ ```ruby
39
+ require 'wots'
40
+
41
+ # Use predefined parameters
42
+ param = WOTS::Param::SHA256
43
+
44
+ # Generate private key from seed
45
+ seed = "693141c7ee701d13e1a7c733e0aa8326c19961429bfb54083f2f65b30c32e20b"
46
+ private_key = WOTS::PrivateKey.from_seed(param, seed)
47
+
48
+ # Generate public key
49
+ pub_seed = "46ece585b4c0bfa1186209270e22fa07c4716461b5a026c268e594fb94404f3a"
50
+ public_key = WOTS::PublicKey.from_private_key(private_key, pub_seed)
51
+
52
+ # Sign a message
53
+ message = "f16c96e88fb99a8287a43121962e89ed521699fa3e126c67eaaa168066354477"
54
+ signature = private_key.sign(pub_seed, message)
55
+
56
+ # Verify signature by reconstructing public key
57
+ recovered_pk = WOTS::PublicKey.from_signature(signature, pub_seed, message)
58
+ valid = (recovered_pk == public_key)
59
+ puts "Signature valid: #{valid}"
60
+ ```
61
+
62
+ ### Available Parameter Sets
63
+
64
+ ```ruby
65
+ # SHA-256 based (n=32 bytes, w=16)
66
+ param = WOTS::Param::SHA256
67
+
68
+ # SHA-512 based (n=64 bytes, w=16)
69
+ param = WOTS::Param::SHA512
70
+
71
+ # SHAKE-256 based (n=32 bytes, w=16)
72
+ param = WOTS::Param::SHAKE256
73
+ ```
74
+
75
+ ## Security Considerations
76
+
77
+ **⚠️ IMPORTANT: This is a cryptograFixphic library implementation. While it follows RFC 8391 specifications,
78
+ it has not undergone formal security audits. Use at your own risk in production environments.**
79
+
80
+ WOTS+ is a **one-time signature scheme**. Each private key should only be used to sign **one message**.
81
+ Reusing a private key to sign multiple messages can leak information about the private key and compromise security.
82
+
83
+ ## Specifications
84
+
85
+ This implementation follows [RFC 8391 - XMSS: eXtended Merkle Signature Scheme](https://datatracker.ietf.org/doc/html/rfc8391), Section 3 (WOTS+).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,27 @@
1
+ module WOTS
2
+ class Address
3
+ attr_accessor :layer_addr
4
+ attr_accessor :tree_addr
5
+ attr_accessor :ots_addr
6
+ attr_accessor :chain_addr
7
+ attr_accessor :hash_addr
8
+ attr_accessor :key_and_mask # 0: key generation, 1: bitmask generation
9
+
10
+ def initialize(layer_addr: 0, tree_addr: 0, ots_addr: 0, chain_addr: 0, hash_addr: 0, key_and_mask: 0)
11
+ @layer_addr = layer_addr
12
+ @tree_addr = tree_addr
13
+ @ots_addr = ots_addr
14
+ @chain_addr = chain_addr
15
+ @hash_addr = hash_addr
16
+ @key_and_mask = key_and_mask
17
+ end
18
+
19
+ def type
20
+ 0
21
+ end
22
+
23
+ def to_payload
24
+ [layer_addr, tree_addr, type, ots_addr, chain_addr, hash_addr, key_and_mask].pack('NQ>NNNNN')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ module WOTS
2
+ class Param
3
+ SHA256 = new(
4
+ name: 'WOTSP-SHA2_256',
5
+ n: 32,
6
+ w: 16
7
+ )
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module WOTS
2
+ class Param
3
+ SHA512 = new(
4
+ name: 'WOTSP-SHA2_512',
5
+ n: 64,
6
+ w: 16
7
+ )
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module WOTS
2
+ class Param
3
+ SHAKE256 = new(
4
+ name: 'WOTSP-SHAKE_256',
5
+ n: 32,
6
+ w: 16
7
+ )
8
+ end
9
+ end
data/lib/wots/param.rb ADDED
@@ -0,0 +1,177 @@
1
+ require 'openssl'
2
+
3
+ module WOTS
4
+ # WOTS+ parameters defined in rfc8391.
5
+ # https://datatracker.ietf.org/doc/html/rfc8391
6
+ class Param
7
+ include Util
8
+
9
+ autoload :SHA256, 'wots/param/sha256'
10
+ autoload :SHA512, 'wots/param/sha512'
11
+ autoload :SHAKE256, 'wots/param/shake256'
12
+
13
+ attr_reader :name
14
+ attr_reader :n
15
+ attr_reader :w
16
+
17
+ # @param [Hash] opts
18
+ # @option opts [Integer] :n the message length as well as the length of a private key,
19
+ # public key, or signature element in bytes.
20
+ # @option opts [Integer] :w the Winternitz parameter; it is a member of the set {4, 16}.
21
+ # @option opts [Integer] :len the number of n-byte string elements in a WOTS+ private key, public key, and signature.
22
+ def initialize(opts)
23
+ raise ArgumentError, 'name must be string.' unless opts[:name].is_a?(String)
24
+ raise ArgumentError, 'n must be integer.' unless opts[:n].is_a?(Integer)
25
+ raise ArgumentError, 'w must be integer.' unless opts[:w].is_a?(Integer)
26
+
27
+ @name = opts[:name]
28
+ @n = opts[:n]
29
+ @w = opts[:w]
30
+ end
31
+
32
+ def len1
33
+ @len1 ||= (8.0 * n / Math.log2(w)).ceil
34
+ end
35
+
36
+ def len2
37
+ @len2 ||= (Math.log2(len1 * (w - 1)) / Math.log2(w)).floor + 1
38
+ end
39
+
40
+ def len
41
+ @len ||= len1 + len2
42
+ end
43
+
44
+ def f(k, m)
45
+ keyed_hash(0, k, m)
46
+ end
47
+
48
+ def h(k, m)
49
+ keyed_hash(1, k, m)
50
+ end
51
+
52
+ def h_msg(k, m)
53
+ keyed_hash(2, k, m)
54
+ end
55
+
56
+ # PRF function.
57
+ # @param [String] k key
58
+ # @param [String] m message
59
+ # @return [String] Hex string.
60
+ def prf(k, m)
61
+ keyed_hash(3, k, m)
62
+ end
63
+
64
+ # Convert data as base w representation.
65
+ # @param [String] data The data to be converted.
66
+ # @param [Integer] out_len Output length.
67
+ # @return [Array] An array of integer.
68
+ def base_w(data, out_len)
69
+ x = hex_to_bin(data)
70
+
71
+ basew = []
72
+ in_idx = 0
73
+ total = 0
74
+ bits = 0
75
+ lg_w = Math.log2(w).to_i
76
+
77
+ out_len.times do
78
+ if bits == 0
79
+ break if in_idx >= x.bytesize
80
+ total = x.getbyte(in_idx)
81
+ in_idx += 1
82
+ bits += 8
83
+ end
84
+
85
+ bits -= lg_w
86
+ basew << ((total >> bits) & (w - 1))
87
+ end
88
+
89
+ basew
90
+ end
91
+
92
+ # Compute checksum for +base_w+.
93
+ # @param [Array] base_w
94
+ # @return [String] Checksum binary string.
95
+ def compute_checksum(base_w)
96
+ c_sum = 0
97
+ len1.times do |i|
98
+ c_sum = (c_sum + w - 1 - base_w[i])
99
+ end
100
+ c_sum = (c_sum << (8 - ((len2 * Math.log2(w).to_i) % 8)))
101
+ len_2_bytes = ((len2 * Math.log2(w).to_i) / 8.0).ceil
102
+ to_byte(c_sum, len_2_bytes)
103
+ end
104
+
105
+ # WOTS+ Chaining Function.
106
+ # @see https://datatracker.ietf.org/doc/html/rfc8391#autoid-16
107
+ # @param [String] x Input string.
108
+ # @param [Integer] start_idx Start index.
109
+ # @param [Integer] steps Number of steps.
110
+ # @param [String] seed Seed.
111
+ # @param [WOTS::Address] addr Address.
112
+ # @return [String] Result.
113
+ def chain(x, start_idx, steps, seed, addr)
114
+ return x if steps == 0
115
+ raise "Invalid range" if (start_idx + steps) > (w - 1)
116
+
117
+ result = x.dup
118
+
119
+ steps.times do |i|
120
+ addr.hash_addr = (start_idx + i)
121
+
122
+ addr.key_and_mask = 0
123
+ key = prf(seed, addr.to_payload)
124
+
125
+ addr.key_and_mask = 1
126
+ mask = prf(seed, addr.to_payload)
127
+
128
+ masked = xor_bytes(result, mask)
129
+ result = f(key, masked)
130
+ end
131
+
132
+ result
133
+ end
134
+
135
+ def ==(other)
136
+ return false unless other.is_a?(Param)
137
+ name == other.name && n == other.n && w == other.w
138
+ end
139
+
140
+ # Convert +value+ to +length+ size binary string.
141
+ # @param [Integer] value
142
+ # @param [Integer] length
143
+ # @return [String]
144
+ def to_byte(value, length)
145
+ bytes = []
146
+
147
+ length.times do
148
+ bytes.unshift(value & 0xFF)
149
+ value >>= 8
150
+ end
151
+
152
+ bytes.pack("C*")
153
+ end
154
+
155
+ private
156
+
157
+ def xor_bytes(a, b)
158
+ [a].pack('H*').unpack('C*').zip(
159
+ [b].pack('H*').unpack('C*')
160
+ ).map { |x, y| x ^ y }.pack("C*")
161
+ end
162
+
163
+ def keyed_hash(prefix, k, m)
164
+ payload = to_byte(prefix, n) + hex_to_bin(k) + hex_to_bin(m)
165
+ case name
166
+ when 'WOTSP-SHA2_256'
167
+ Digest::SHA256.hexdigest(payload)
168
+ when 'WOTSP-SHA2_512'
169
+ Digest::SHA512.hexdigest(payload)
170
+ when 'WOTSP-SHAKE_256'
171
+ OpenSSL::Digest.new('shake256', payload).hexdigest
172
+ else
173
+ raise 'Unknown param'
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,69 @@
1
+ module WOTS
2
+ # WOTS+ private key.
3
+ class PrivateKey
4
+ include Util
5
+ extend Util
6
+
7
+ attr_reader :param
8
+ attr_reader :keys
9
+
10
+ def initialize(param, keys)
11
+ raise ArgumentError, "param must be WOTS::Param." unless param.is_a?(WOTS::Param)
12
+ raise ArgumentError, "keys must be Array." unless keys.is_a?(Array)
13
+ raise ArgumentError, "The length of keys must be the same as param#len." unless keys.length == param.len
14
+ keys.each do |key|
15
+ raise ArgumentError, "key must be hex string." unless hex_string?(key)
16
+ raise ArgumentError, "key must be #{param.n} bytes." unless hex_to_bin(key).bytesize == param.n
17
+ end
18
+
19
+ @param = param
20
+ @keys = keys
21
+ end
22
+
23
+ # Generate private key using +seed+.
24
+ # @param [WOTS::Param] param
25
+ # @param [String] seed
26
+ # @raise ArgumentError
27
+ def self.from_seed(param, seed)
28
+ raise ArgumentError, "param must be WOTS::Param." unless param.is_a?(WOTS::Param)
29
+ raise ArgumentError, "seed must be String." unless seed.is_a?(String)
30
+ raise ArgumentError, "seed must be #{param.n} bytes." unless hex_to_bin(seed).bytesize == param.n
31
+ raise ArgumentError, "len parameter too large." if param.len.bit_length > 16
32
+
33
+ keys = param.len.times.map do |i|
34
+ param.prf(seed, param.to_byte(i, 32))
35
+ end
36
+
37
+ PrivateKey.new(param, keys)
38
+ end
39
+
40
+ # Generate signature.
41
+ # @param [String] pub_seed The Public seed.
42
+ # @param [String] message The message to be signed.
43
+ # @return [WOTS::Signature]
44
+ # @raise ArgumentError
45
+ def sign(pub_seed, message)
46
+ raise ArgumentError, 'pub_seed must be hex string.' unless hex_string?(pub_seed)
47
+ raise ArgumentError, "pub_seed must be #{param.n} bytes." unless hex_to_bin(pub_seed).bytesize == param.n
48
+ raise ArgumentError, "message must be string." unless message.is_a?(String)
49
+ raise ArgumentError, "message must be #{param.n} bytes." unless hex_to_bin(message).bytesize == param.n
50
+
51
+ addr = Address.new
52
+
53
+ # Convert message to base w
54
+ base_w = param.base_w(message, param.len1)
55
+
56
+ # Compute checksum
57
+ c_sum = param.compute_checksum(base_w)
58
+
59
+ base_w = base_w + param.base_w(c_sum, param.len2)
60
+
61
+ sigs = param.len.times.map do |i|
62
+ addr.chain_addr = i
63
+ param.chain(keys[i], 0, base_w[i], pub_seed, addr)
64
+ end
65
+
66
+ Signature.new(param, sigs)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,71 @@
1
+ module WOTS
2
+ # WOTS+ public key.
3
+ class PublicKey
4
+ include Util
5
+ extend Util
6
+
7
+ attr_reader :param
8
+ attr_reader :keys
9
+
10
+ def initialize(param, keys)
11
+ raise ArgumentError, "param must be WOTS::Param." unless param.is_a?(WOTS::Param)
12
+ raise ArgumentError, "keys must be Array." unless keys.is_a?(Array)
13
+ raise ArgumentError, "The length of keys must be the same as param#len." unless keys.length == param.len
14
+ keys.each do |key|
15
+ raise ArgumentError, "key must be hex string." unless hex_string?(key)
16
+ raise ArgumentError, "key must be #{param.n} bytes." unless hex_to_bin(key).bytesize == param.n
17
+ end
18
+ @param = param
19
+ @keys = keys
20
+ end
21
+
22
+ # Generate public key from +private_key+ and +pub_seed+.
23
+ # @param [WOTS::PrivateKey] private_key
24
+ # @param [String] pub_seed
25
+ def self.from_private_key(private_key, pub_seed)
26
+ raise ArgumentError, 'private_key must be WOTS::PrivateKey.' unless private_key.is_a?(WOTS::PrivateKey)
27
+ param = private_key.param
28
+ raise ArgumentError, 'pub_seed must be hex string.' unless hex_string?(pub_seed)
29
+ raise ArgumentError, "pub_seed must be #{param.n} bytes." unless hex_to_bin(pub_seed).bytesize == param.n
30
+ addr = WOTS::Address.new
31
+ keys = param.len.times.map do |i|
32
+ addr.chain_addr = i
33
+ sk = private_key.keys[i]
34
+ param.chain(sk, 0, param.w - 1, pub_seed, addr)
35
+ end
36
+ PublicKey.new(private_key.param, keys)
37
+ end
38
+
39
+ # Generate public key from +signature+.
40
+ # @param [WOTS::Signature] signature
41
+ # @param [String] pub_seed
42
+ # @param [String] message
43
+ # @return [WOTS::PublicKey]
44
+ # @raise ArgumentError
45
+ def self.from_signature(signature, pub_seed, message)
46
+ param = signature.param
47
+ raise ArgumentError, 'pub_seed must be hex string.' unless hex_string?(pub_seed)
48
+ raise ArgumentError, "pub_seed must be #{param.n} bytes." unless hex_to_bin(pub_seed).bytesize == param.n
49
+ raise ArgumentError, "message must be string." unless message.is_a?(String)
50
+ raise ArgumentError, "message must be #{param.n} bytes." unless hex_to_bin(message).bytesize == param.n
51
+
52
+ base_w = param.base_w(message, param.len1)
53
+ c_sum = param.compute_checksum(base_w)
54
+ base_w = base_w + param.base_w(c_sum, param.len2)
55
+
56
+ addr = WOTS::Address.new
57
+
58
+ keys = param.len.times.map do |i|
59
+ addr.chain_addr = i
60
+ param.chain(signature.sigs[i], base_w[i], param.w - 1 - base_w[i], pub_seed, addr)
61
+ end
62
+
63
+ PublicKey.new(param, keys)
64
+ end
65
+
66
+ def ==(other)
67
+ return false unless other.is_a?(PublicKey)
68
+ param == other.param && keys == other.keys
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,21 @@
1
+ module WOTS
2
+ # WOTS+ signature.
3
+ class Signature
4
+ include Util
5
+
6
+ attr_reader :param, :sigs
7
+
8
+ def initialize(param, sigs)
9
+ raise ArgumentError, "param must be WOTS::Param." unless param.is_a?(WOTS::Param)
10
+ raise ArgumentError, "sigs must be Array." unless sigs.is_a?(Array)
11
+ raise ArgumentError, "The length of sigs must be the same as param#len." unless sigs.length == param.len
12
+ sigs.each do |sig|
13
+ raise ArgumentError, "sig must be hex string." unless hex_string?(sig)
14
+ raise ArgumentError, "sig must be #{param.n} bytes." unless hex_to_bin(sig).bytesize == param.n
15
+ end
16
+
17
+ @param = param
18
+ @sigs = sigs
19
+ end
20
+ end
21
+ end
data/lib/wots/util.rb ADDED
@@ -0,0 +1,32 @@
1
+ module WOTS
2
+ module Util
3
+
4
+ # Check whether +data+ is hex string or not.
5
+ # @param [String] data
6
+ # @return [Boolean]
7
+ # @raise [ArgumentError]
8
+ def hex_string?(data)
9
+ raise ArgumentError, 'data must be string' unless data.is_a?(String)
10
+ data.match?(/\A[0-9a-fA-F]+\z/)
11
+ end
12
+
13
+ # Convert hex string +data+ to binary.
14
+ # @param [String] data
15
+ # @return [String]
16
+ # @raise [ArgumentError]
17
+ def hex_to_bin(data)
18
+ raise ArgumentError, 'data must be string' unless data.is_a?(String)
19
+ hex_string?(data) ? [data].pack('H*') : data
20
+ end
21
+
22
+ # Convert binary string +data+ to hex string.
23
+ # @param [String] data
24
+ # @return [String]
25
+ # @raise [ArgumentError]
26
+ def bin_to_hex(data)
27
+ raise ArgumentError, 'data must be string' unless data.is_a?(String)
28
+ hex_string?(data) ? data : data.unpack1('H*')
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WOTS
4
+ VERSION = "0.1.0"
5
+ end
data/lib/wots.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ require 'digest'
3
+ require_relative "wots/version"
4
+ require_relative 'wots/util'
5
+ require_relative 'wots/param'
6
+ require_relative 'wots/address'
7
+ require_relative 'wots/private_key'
8
+ require_relative 'wots/public_key'
9
+ require_relative 'wots/signature'
10
+
11
+ module WOTS
12
+ class Error < StandardError; end
13
+ end
data/sig/wots.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Wots
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wots
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - azuchi
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A Ruby implementation of the Winternitz OTS (W-OTS+), as described in
13
+ RFC8391.
14
+ email:
15
+ - azuchi@chaintope.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".ruby-gemset"
22
+ - ".ruby-version"
23
+ - CODE_OF_CONDUCT.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/wots.rb
28
+ - lib/wots/address.rb
29
+ - lib/wots/param.rb
30
+ - lib/wots/param/sha256.rb
31
+ - lib/wots/param/sha512.rb
32
+ - lib/wots/param/shake256.rb
33
+ - lib/wots/private_key.rb
34
+ - lib/wots/public_key.rb
35
+ - lib/wots/signature.rb
36
+ - lib/wots/util.rb
37
+ - lib/wots/version.rb
38
+ - sig/wots.rbs
39
+ homepage: https://github.com/azuchi/wots
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/azuchi/wots
44
+ source_code_uri: https://github.com/azuchi/wots
45
+ changelog_uri: https://github.com/azuchi/wots
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.0.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.6.7
61
+ specification_version: 4
62
+ summary: A Ruby implementation of the Winternitz OTS (W-OTS+), as described in RFC8391.
63
+ test_files: []