Class: WOTS::PublicKey

Inherits:
Object
  • Object
show all
Extended by:
Util
Includes:
Util
Defined in:
lib/wots/public_key.rb

Overview

WOTS+ public key.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

bin_to_hex, hex_string?, hex_to_bin

Constructor Details

#initialize(param, keys) ⇒ PublicKey

Returns a new instance of PublicKey.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/wots/public_key.rb', line 10

def initialize(param, keys)
  raise ArgumentError, "param must be WOTS::Param." unless param.is_a?(WOTS::Param)
  raise ArgumentError, "keys must be Array." unless keys.is_a?(Array)
  raise ArgumentError, "The length of keys must be the same as param#len." unless keys.length == param.len
  keys.each do |key|
    raise ArgumentError, "key must be hex string." unless hex_string?(key)
    raise ArgumentError, "key must be #{param.n} bytes." unless hex_to_bin(key).bytesize == param.n
  end
  @param = param
  @keys = keys
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



8
9
10
# File 'lib/wots/public_key.rb', line 8

def keys
  @keys
end

#paramObject (readonly)

Returns the value of attribute param.



7
8
9
# File 'lib/wots/public_key.rb', line 7

def param
  @param
end

Class Method Details

.from_private_key(private_key, pub_seed) ⇒ Object

Generate public key from private_key and pub_seed.

Parameters:

  • private_key (WOTS::PrivateKey)
  • pub_seed (String)

    Hex string of param.n bytes.

Raises:

  • ArgumentError



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wots/public_key.rb', line 26

def self.from_private_key(private_key, pub_seed)
  raise ArgumentError, 'private_key must be WOTS::PrivateKey.' unless private_key.is_a?(WOTS::PrivateKey)
  param = private_key.param
  raise ArgumentError, 'pub_seed must be hex string.' unless hex_string?(pub_seed)
  raise ArgumentError, "pub_seed must be #{param.n} bytes." unless hex_to_bin(pub_seed).bytesize == param.n
  addr = WOTS::Address.new
  keys = param.len.times.map do |i|
    addr.chain_addr = i
    sk = private_key.keys[i]
    param.chain(sk, 0, param.w - 1, pub_seed, addr)
  end
  PublicKey.new(private_key.param, keys)
end

.from_signature(signature, pub_seed, message) ⇒ WOTS::PublicKey

Generate public key from signature.

Parameters:

  • signature (WOTS::Signature)
  • pub_seed (String)

    Hex string of param.n bytes.

  • message (String)

    Hex string of param.n bytes.

Returns:

Raises:

  • ArgumentError



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wots/public_key.rb', line 46

def self.from_signature(signature, pub_seed, message)
  raise ArgumentError, 'signature must be WOTS::Signature.' unless signature.is_a?(WOTS::Signature)
  param = signature.param
  raise ArgumentError, 'pub_seed must be hex string.' unless hex_string?(pub_seed)
  raise ArgumentError, "pub_seed must be #{param.n} bytes." unless hex_to_bin(pub_seed).bytesize == param.n
  raise ArgumentError, "message must be hex string." unless hex_string?(message)
  raise ArgumentError, "message must be #{param.n} bytes." unless hex_to_bin(message).bytesize == param.n

  base_w = param.base_w(message, param.len1)
  c_sum = param.compute_checksum(base_w)
  base_w = base_w + param.base_w(c_sum, param.len2)

  addr = WOTS::Address.new

  keys = param.len.times.map do |i|
    addr.chain_addr = i
    param.chain(signature.sigs[i], base_w[i], param.w - 1 - base_w[i], pub_seed, addr)
  end

  PublicKey.new(param, keys)
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
# File 'lib/wots/public_key.rb', line 68

def ==(other)
  return false unless other.is_a?(PublicKey)
  param == other.param && keys == other.keys
end