Skip to content

Commit 74a0ae7

Browse files
committed
Implement other parameters
1 parent c80fe28 commit 74a0ae7

10 files changed

Lines changed: 43 additions & 44 deletions

File tree

lib/wots.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,4 @@
1010

1111
module WOTS
1212
class Error < StandardError; end
13-
14-
# Check whether +data+ is hex string or not.
15-
# @param [String] data
16-
# @return [Boolean]
17-
# @raise [ArgumentError]
18-
def hex_string?(data)
19-
raise ArgumentError, 'data must be string' unless data.is_a?(String)
20-
data.match?(/\A[0-9a-fA-F]+\z/)
21-
end
22-
23-
# Convert hex string +data+ to binary.
24-
# @param [String] data
25-
# @return [String]
26-
# @raise [ArgumentError]
27-
def hex_to_bin(data)
28-
raise ArgumentError, 'data must be string' unless data.is_a?(String)
29-
hex_string?(data) ? [data].pack('H*') : data
30-
end
3113
end

lib/wots/param.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'openssl'
2+
13
module WOTS
24
# WOTS+ parameters defined in rfc8391.
35
# https://datatracker.ietf.org/doc/html/rfc8391
@@ -7,7 +9,6 @@ class Param
79
autoload :SHA256, 'wots/param/sha256'
810
autoload :SHA512, 'wots/param/sha512'
911
autoload :SHAKE256, 'wots/param/shake256'
10-
autoload :SHAKE512, 'wots/param/shake512'
1112

1213
attr_reader :name
1314
attr_reader :n
@@ -160,11 +161,14 @@ def xor_bytes(a, b)
160161
end
161162

162163
def keyed_hash(prefix, k, m)
164+
payload = to_byte(prefix, n) + hex_to_bin(k) + hex_to_bin(m)
163165
case name
164-
when 'wotsp-sha2_256'
165-
Digest::SHA256.hexdigest(to_byte(prefix, 32) + hex_to_bin(k) + hex_to_bin(m))
166-
when 'wotsp-sha512'
167-
Digest::SHA512.hexdigest(to_byte(prefix, 64) + hex_to_bin(k) + hex_to_bin(m))
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
168172
else
169173
raise 'Unknown param'
170174
end

lib/wots/param/sha256.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module WOTS
22
class Param
33
SHA256 = new(
4-
name: 'wotsp-sha2_256',
4+
name: 'WOTSP-SHA2_256',
55
n: 32,
66
w: 16
77
)

lib/wots/param/sha512.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module WOTS
22
class Param
33
SHA512 = new(
4-
name: 'wotsp-sha512',
4+
name: 'WOTSP-SHA2_512',
55
n: 64,
66
w: 16
77
)

lib/wots/param/shake256.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module WOTS
22
class Param
33
SHAKE256 = new(
4-
name: 'wotsp-shake_256',
4+
name: 'WOTSP-SHAKE_256',
55
n: 32,
66
w: 16
77
)

lib/wots/param/shake512.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/wots/private_key.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ module WOTS
22
# WOTS+ private key.
33
class PrivateKey
44
include Util
5+
extend Util
56

67
attr_reader :param
78
attr_reader :keys
89

910
def initialize(param, keys)
10-
raise ArgumentError "param must be WOTS::Param." unless param.is_a?(WOTS::Param)
11-
raise ArgumentError "keys must be Array." unless keys.is_a?(Array)
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)
1213
raise ArgumentError, "The length of keys must be the same as param#len." unless keys.length == param.len
1314
keys.each do |key|
1415
raise ArgumentError, "key must be hex string." unless hex_string?(key)
@@ -24,9 +25,10 @@ def initialize(param, keys)
2425
# @param [String] seed
2526
# @raise ArgumentError
2627
def self.from_seed(param, seed)
27-
raise ArgumentError "param must be WOTS::Param." unless param.is_a?(WOTS::Param)
28-
raise ArgumentError "seed must be String." unless seed.is_a?(String)
29-
raise ArgumentError "len parameter too large." if param.len.bit_length > 16
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
3032

3133
keys = param.len.times.map do |i|
3234
param.prf(seed, param.to_byte(i, 32))

lib/wots/public_key.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class PublicKey
88
attr_reader :keys
99

1010
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)
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)
1313
raise ArgumentError, "The length of keys must be the same as param#len." unless keys.length == param.len
1414
keys.each do |key|
1515
raise ArgumentError, "key must be hex string." unless hex_string?(key)

lib/wots/signature.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class Signature
66
attr_reader :param, :sigs
77

88
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)
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)
1111
raise ArgumentError, "The length of sigs must be the same as param#len." unless sigs.length == param.len
1212
sigs.each do |sig|
1313
raise ArgumentError, "sig must be hex string." unless hex_string?(sig)

spec/wots_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22
require 'spec_helper'
3+
require 'securerandom'
4+
require 'digest'
35

46
RSpec.describe WOTS do
57

@@ -41,4 +43,22 @@
4143
end
4244
end
4345
end
46+
47+
describe 'All params' do
48+
let(:params) { [ WOTS::Param::SHA256, WOTS::Param::SHA512, WOTS::Param::SHAKE256 ] }
49+
it do
50+
params.each do |param|
51+
seed = SecureRandom.hex(param.n)
52+
pub_seed = SecureRandom.hex(param.n)
53+
message = SecureRandom.hex(param.n)
54+
55+
private_key = WOTS::PrivateKey.from_seed(param, seed)
56+
public_key = WOTS::PublicKey.from_private_key(private_key, pub_seed)
57+
58+
signature = private_key.sign(pub_seed, message)
59+
pubkey_from_sig = WOTS::PublicKey.from_signature(signature, pub_seed, message)
60+
expect(pubkey_from_sig).to eq(public_key)
61+
end
62+
end
63+
end
4464
end

0 commit comments

Comments
 (0)