Class: Tapyrus::SLIP39::Share
- Inherits:
-
Object
- Object
- Tapyrus::SLIP39::Share
- Defined in:
- lib/tapyrus/slip39/share.rb
Overview
Share of Shamir’s Secret Sharing Scheme
Instance Attribute Summary collapse
-
#checksum ⇒ Object
30 bits, Integer.
-
#group_count ⇒ Object
4 bits, Integer.
-
#group_index ⇒ Object
4 bits, Integer.
-
#group_threshold ⇒ Object
4 bits, Integer.
-
#id ⇒ Object
15 bits, Integer.
-
#iteration_exp ⇒ Object
5 bits, Integer.
-
#member_index ⇒ Object
4 bits, Integer.
-
#member_threshold ⇒ Object
4 bits, Integer.
-
#value ⇒ Object
8n bits, hex string.
Class Method Summary collapse
-
.from_words(words) ⇒ Tapyrus::SLIP39::Share
Recover Share from the mnemonic words.
- .rs1024_polymod(values) ⇒ Object
Instance Method Summary collapse
-
#calculate_checksum ⇒ Integer
Calculate checksum using current fields.
-
#to_words ⇒ Array[String]
Generate mnemonic words.
Instance Attribute Details
#checksum ⇒ Object
30 bits, Integer
13 14 15 |
# File 'lib/tapyrus/slip39/share.rb', line 13 def checksum @checksum end |
#group_count ⇒ Object
4 bits, Integer
9 10 11 |
# File 'lib/tapyrus/slip39/share.rb', line 9 def group_count @group_count end |
#group_index ⇒ Object
4 bits, Integer
7 8 9 |
# File 'lib/tapyrus/slip39/share.rb', line 7 def group_index @group_index end |
#group_threshold ⇒ Object
4 bits, Integer
8 9 10 |
# File 'lib/tapyrus/slip39/share.rb', line 8 def group_threshold @group_threshold end |
#id ⇒ Object
15 bits, Integer
5 6 7 |
# File 'lib/tapyrus/slip39/share.rb', line 5 def id @id end |
#iteration_exp ⇒ Object
5 bits, Integer
6 7 8 |
# File 'lib/tapyrus/slip39/share.rb', line 6 def iteration_exp @iteration_exp end |
#member_index ⇒ Object
4 bits, Integer
10 11 12 |
# File 'lib/tapyrus/slip39/share.rb', line 10 def member_index @member_index end |
#member_threshold ⇒ Object
4 bits, Integer
11 12 13 |
# File 'lib/tapyrus/slip39/share.rb', line 11 def member_threshold @member_threshold end |
#value ⇒ Object
8n bits, hex string.
12 13 14 |
# File 'lib/tapyrus/slip39/share.rb', line 12 def value @value end |
Class Method Details
.from_words(words) ⇒ Tapyrus::SLIP39::Share
Recover Share from the mnemonic words
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tapyrus/slip39/share.rb', line 18 def self.from_words(words) raise ArgumentError, "Mnemonics should be an array of strings" unless words.is_a?(Array) indices = words.map do |word| index = Tapyrus::SLIP39::WORDS.index(word.downcase) raise IndexError, "word not found in words list." unless index index end raise ArgumentError, "Invalid mnemonic length." if indices.size < MIN_MNEMONIC_LENGTH_WORDS raise ArgumentError, "Invalid mnemonic checksum." unless verify_rs1024_checksum(indices) padding_length = (RADIX_BITS * (indices.size - METADATA_LENGTH_WORDS)) % 16 raise ArgumentError, "Invalid mnemonic length." if padding_length > 8 data = indices.map { |i| i.to_s(2).rjust(10, "0") }.join s = self.new s.id = data[0...ID_LENGTH_BITS].to_i(2) s.iteration_exp = data[ID_LENGTH_BITS...(ID_LENGTH_BITS + ITERATION_EXP_LENGTH_BITS)].to_i(2) s.group_index = data[20...24].to_i(2) s.group_threshold = data[24...28].to_i(2) + 1 s.group_count = data[28...32].to_i(2) + 1 if s.group_threshold > s.group_count raise ArgumentError, "Invalid mnemonic. Group threshold(#{s.group_threshold}) cannot be greater than group count(#{s.group_count})." end s.member_index = data[32...36].to_i(2) s.member_threshold = data[36...40].to_i(2) + 1 value_length = data.length - 70 start_index = 40 + padding_length end_index = start_index + value_length - padding_length padding_value = data[40...(40 + padding_length)] raise ArgumentError, "Invalid mnemonic. padding must only zero." unless padding_value.to_i(2) == 0 s.value = data[start_index...end_index].to_i(2).to_even_length_hex s.checksum = data[(40 + value_length)..-1].to_i(2) s end |
.rs1024_polymod(values) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tapyrus/slip39/share.rb', line 70 def self.rs1024_polymod(values) gen = [ 0xe0e040, 0x1c1c080, 0x3838100, 0x7070200, 0xe0e0009, 0x1c0c2412, 0x38086c24, 0x3090fc48, 0x21b1f890, 0x3f3f120 ] chk = 1 values.each do |v| b = (chk >> 20) chk = (chk & 0xfffff) << 10 ^ v 10.times { |i| chk ^= (((b >> i) & 1 == 1) ? gen[i] : 0) } end chk end |
Instance Method Details
#calculate_checksum ⇒ Integer
Calculate checksum using current fields
65 66 67 68 |
# File 'lib/tapyrus/slip39/share.rb', line 65 def calculate_checksum indices = build_word_indices(false) create_rs1024_checksum(indices).map { |i| i.to_bits(10) }.join.to_i(2) end |