mnemonic_words 0.0.1

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.
Files changed (2) hide show
  1. data/lib/mnemonic_words.rb +76 -0
  2. metadata +62 -0
@@ -0,0 +1,76 @@
1
+ require 'contemporary_words'
2
+
3
+ # Bitcoin donation address: 1MWeioFdnwFZC8eqJwxjKcA3SErKxEBu8V
4
+
5
+ class MnemonicWords
6
+
7
+ class << self
8
+
9
+ attr_reader :multipliar
10
+
11
+ end
12
+
13
+ @multipliar = [ 955, 3798375, 4966887791, 4886399928973, 310731420136098447 ]
14
+
15
+ attr_accessor :base, :p_error, :p_error_max, :range, :n_digits, :multiplier, :divisor, :word_list
16
+
17
+ def initialize(range, p_error_max, base = 2357)
18
+ @base = base
19
+ @p_error_max = p_error_max
20
+ @range = range
21
+ @n_digits = (Math.log(@range.size, @base) - Math.log(@p_error_max, @base)).ceil
22
+ @max_result = @base ** @n_digits
23
+ @p_error = @range.size.to_f/@max_result
24
+ @multiplier = (@n_digits > MnemonicWords.multipliar.size) ?
25
+ (0.7 * @max_result).floor : MnemonicWords.multipliar[@n_digits - 1]
26
+ @divisor = inverse(@multiplier, @max_result)
27
+ @word_list = ContemporaryWords.all[0..@base-1]
28
+ true
29
+ end
30
+
31
+ def encode(x)
32
+ y = ((x - @range.begin) * @multiplier) % @max_result
33
+ a = []
34
+ while y > 0 do
35
+ a.unshift y % @base
36
+ y /= @base
37
+ end
38
+ a.map { |z| @word_list[z] }.join(' ')
39
+ end
40
+
41
+ def decode(s)
42
+ indices = s.downcase.split(/\s+/).map { |word| @word_list.index(word) }
43
+ return nil if indices.any? &:nil?
44
+ y = indices.inject(0) { |memo,x| memo * @base + x }
45
+ result = ((y * @divisor) % @max_result) + @range.begin
46
+ (@range === result) ? result : nil
47
+ end
48
+
49
+ def inspect
50
+ "< n: #{@n_digits}, -log10(p): #{-Math.log10(@p_error)} >"
51
+ end
52
+
53
+ private
54
+
55
+ # Adapted from http://www.dzone.com/snippets/modular-inverse-function
56
+ def inverse(a, n )
57
+ i = n
58
+ v = 0
59
+ d = 1
60
+ while a>0 do
61
+ t = (i/a).floor
62
+ x = a
63
+ a = i % x
64
+ i = x
65
+ x = d
66
+ d = v - t*x
67
+ v = x
68
+ end
69
+ v %= n
70
+ if (v<0)
71
+ v = (v+n)%n
72
+ end
73
+ return v
74
+ end
75
+
76
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mnemonic_words
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Oei
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: contemporary_words
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description:
31
+ email: zlkm208-rubygems@yahoo.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/mnemonic_words.rb
37
+ homepage: https://github.com/chrisoei/mnemonic_words
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.24
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Creates a mnemonic sequence of words representing an integer in a specified
61
+ range.
62
+ test_files: []