codex32 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.prettierignore +2 -0
- data/.rspec +3 -0
- data/.rubocop.yml +23 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/codex32.gemspec +37 -0
- data/lib/codex32/errors.rb +49 -0
- data/lib/codex32/share.rb +71 -0
- data/lib/codex32/version.rb +5 -0
- data/lib/codex32.rb +235 -0
- data/sig/codex32.rbs +4 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e303c201093a87d6b4c458ccd892a8f217c0fdef841e6d3e94360e1f607b22e2
|
4
|
+
data.tar.gz: f2c8aa7553fb789a7e3b743cd8860a4e36c3e815574b8ac32dfa62fd7535f143
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b42a64b0bc476c46d8c2bf40515d3ada761721cffc5332624384492e3755c64ba0e06480debc96cee011ee498b9531f51fd8f2bbc01b3c6955f88750939c754a
|
7
|
+
data.tar.gz: 9e1c7ad5976a72dc5dfcc043b979a71faf7f025b415d53030cd907591592c736a4e15a60f2f22f4ba962bbc8b01817c3916876071b4e5a9957a004d50f4b10d5
|
data/.prettierignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rake
|
3
|
+
- rubocop-rspec
|
4
|
+
inherit_gem:
|
5
|
+
prettier: rubocop.yml
|
6
|
+
AllCops:
|
7
|
+
TargetRubyVersion: 2.7
|
8
|
+
RSpec/ExampleLength:
|
9
|
+
Enabled: false
|
10
|
+
Metrics:
|
11
|
+
Enabled: false
|
12
|
+
RSpec/MultipleExpectations:
|
13
|
+
Max: 5
|
14
|
+
Style/IfUnlessModifier:
|
15
|
+
Enabled: false
|
16
|
+
Style/WhileUntilModifier:
|
17
|
+
Enabled: false
|
18
|
+
Naming/MethodParameterName:
|
19
|
+
Enabled: false
|
20
|
+
RSpec/FilePath:
|
21
|
+
Enabled: false
|
22
|
+
RSpec/MultipleExpectations:
|
23
|
+
Enabled: false
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
codex32
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-3.2.0
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at azuchi@chaintope.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in codex32.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem 'prettier'
|
13
|
+
|
14
|
+
gem 'rubocop-rake'
|
15
|
+
gem 'rubocop-rspec'
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 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,44 @@
|
|
1
|
+
# Codex32
|
2
|
+
|
3
|
+
This library is [Codex32](https://secretcodex32.com/index.html) ruby implementation.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'codex32'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle install
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install codex32
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'codex32'
|
26
|
+
|
27
|
+
# Parse codex32 share.
|
28
|
+
share = Codex32.parse("ms10testsxxxxxxxxxxxxxxxxxxxxxxxxxx4nzvca9cmczlw")
|
29
|
+
# Get share data.
|
30
|
+
share.data
|
31
|
+
|
32
|
+
# Recovery master seed using shares.
|
33
|
+
share1 = Codex32.parse("MS12NAMEA320ZYXWVUTSRQPNMLKJHGFEDCAXRPP870HKKQRM")
|
34
|
+
share2 = Codex32.parse("MS12NAMECACDEFGHJKLMNPQRSTUVWXYZ023FTR2GDZMPY6PN")
|
35
|
+
|
36
|
+
secret = Codex32.generate_share([share1, share2], Codex32::SECRET_INDEX)
|
37
|
+
# Obtain master seed.
|
38
|
+
secret.data
|
39
|
+
|
40
|
+
# Generate new share with index 'd'
|
41
|
+
share3 = Codex32.generate_share([share1, share2], "d")
|
42
|
+
# Obtain bech32 string.
|
43
|
+
share3.to_s
|
44
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "codex32"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/codex32.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/codex32/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "codex32"
|
7
|
+
spec.version = Codex32::VERSION
|
8
|
+
spec.authors = ["azuchi"]
|
9
|
+
spec.email = ["techmedia.think@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Ruby implementation of codex32"
|
12
|
+
spec.description = spec.description
|
13
|
+
spec.homepage = "https://github.com/azuchi/codex32rb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.7.0"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
19
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
# Uncomment to register a new dependency of your gem
|
33
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, check out our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Codex32
|
3
|
+
module Errors
|
4
|
+
class Error < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class InvalidChecksum < Error
|
8
|
+
end
|
9
|
+
|
10
|
+
class InvalidLength < Error
|
11
|
+
end
|
12
|
+
|
13
|
+
class IncompleteGroup < Error
|
14
|
+
end
|
15
|
+
|
16
|
+
class InvalidBech32Character < Error
|
17
|
+
end
|
18
|
+
|
19
|
+
class InvalidThreshold < Error
|
20
|
+
end
|
21
|
+
|
22
|
+
class InvalidHRP < Error
|
23
|
+
end
|
24
|
+
|
25
|
+
class SeparatorNotFound < Error
|
26
|
+
end
|
27
|
+
|
28
|
+
class InvalidIdentifier < Error
|
29
|
+
end
|
30
|
+
|
31
|
+
class ThresholdMismatch < Error
|
32
|
+
end
|
33
|
+
|
34
|
+
class InsufficientShares < Error
|
35
|
+
end
|
36
|
+
|
37
|
+
class DuplicateShareIndex < Error
|
38
|
+
end
|
39
|
+
|
40
|
+
class IdentifierMismatch < Error
|
41
|
+
end
|
42
|
+
|
43
|
+
class InvalidShareIndex < Error
|
44
|
+
end
|
45
|
+
|
46
|
+
class InvalidCase < Error
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codex32
|
4
|
+
# Codex32 share class.
|
5
|
+
class Share
|
6
|
+
include Codex32
|
7
|
+
attr_reader :id, :payload, :index, :threshold
|
8
|
+
|
9
|
+
# @param [String] id Identifier of this share.
|
10
|
+
# @param [Integer] threshold Threshold.
|
11
|
+
# @param [String] index Share index.
|
12
|
+
# @param [String] payload Share payload.
|
13
|
+
def initialize(id, threshold, index, payload)
|
14
|
+
unless CHARSET.include?(index.downcase)
|
15
|
+
raise Codex32::Errors::InvalidBech32Character
|
16
|
+
end
|
17
|
+
unless threshold.zero? || (threshold > 1 && threshold < 10)
|
18
|
+
raise Codex32::Errors::InvalidThreshold
|
19
|
+
end
|
20
|
+
@id = id.downcase
|
21
|
+
@payload = payload.downcase
|
22
|
+
@index = index.downcase
|
23
|
+
@threshold = threshold
|
24
|
+
|
25
|
+
if threshold.zero? && index != SECRET_INDEX
|
26
|
+
raise Codex32::Errors::InvalidShareIndex
|
27
|
+
end
|
28
|
+
|
29
|
+
incomplete_group = payload.length * 5 % 8
|
30
|
+
return unless incomplete_group > 4
|
31
|
+
raise Codex32::Errors::IncompleteGroup,
|
32
|
+
"Incomplete group #{incomplete_group}"
|
33
|
+
end
|
34
|
+
|
35
|
+
# Calculate checksum.
|
36
|
+
# @return [String]
|
37
|
+
def checksum
|
38
|
+
data = bech32_to_array(content)
|
39
|
+
return create_long_checksum(data) if data.length > 80
|
40
|
+
poly_value = polymod(data + [0] * 13) ^ MS32_CONST
|
41
|
+
result = 13.times.map { |i| (poly_value >> 5 * (12 - i)) & 31 }
|
42
|
+
array_to_bech32(result)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Return decoded payload.
|
46
|
+
# @return [String] Decoded payload.
|
47
|
+
def data
|
48
|
+
convert_bits(bech32_to_array(payload), 5, 8, padding: false).pack(
|
49
|
+
"C*"
|
50
|
+
).unpack1("H*")
|
51
|
+
end
|
52
|
+
|
53
|
+
# Return bech32 string.
|
54
|
+
# @return [String]
|
55
|
+
def to_s
|
56
|
+
HRP + SEPARATOR + content + checksum
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def content
|
62
|
+
threshold.to_s + id + index + payload
|
63
|
+
end
|
64
|
+
|
65
|
+
def create_long_checksum(data)
|
66
|
+
poly_value = long_polymod(data + [0] * 15) ^ MS32_LONG_CONST
|
67
|
+
result = 15.times.map { |i| (poly_value >> 5 * (14 - i)) & 31 }
|
68
|
+
array_to_bech32(result)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/codex32.rb
ADDED
@@ -0,0 +1,235 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "codex32/version"
|
4
|
+
require_relative "codex32/errors"
|
5
|
+
require_relative "codex32/share"
|
6
|
+
|
7
|
+
# Codex32 library.
|
8
|
+
module Codex32
|
9
|
+
HRP = "ms"
|
10
|
+
|
11
|
+
SEPARATOR = "1"
|
12
|
+
|
13
|
+
# stree-ignore
|
14
|
+
CHARSET = %w[q p z r y 9 x 8 g f 2 t v d w 0 s 3 j n 5 4 k h c e 6 m u a 7 l].freeze
|
15
|
+
|
16
|
+
# stree-ignore
|
17
|
+
BECH32_INV = [0, 1, 20, 24, 10, 8, 12, 29, 5, 11, 4, 9, 6, 28, 26, 31,
|
18
|
+
22, 18, 17, 23, 2, 25, 16, 19, 3, 21, 14, 30, 13, 7, 27, 15].freeze
|
19
|
+
|
20
|
+
MS32_CONST = 0x10ce0795c2fd1e62a
|
21
|
+
MS32_LONG_CONST = 0x43381e570bf4798ab26
|
22
|
+
|
23
|
+
SECRET_INDEX = "s"
|
24
|
+
|
25
|
+
module_function
|
26
|
+
|
27
|
+
# Parse codex32 string.
|
28
|
+
# @param [String] codex32 Codex32 string
|
29
|
+
# @return [Codex32::Share]
|
30
|
+
def parse(codex32)
|
31
|
+
if codex32.downcase != codex32 && codex32.upcase != codex32
|
32
|
+
raise Errors::InvalidCase
|
33
|
+
end
|
34
|
+
hrp, remain = codex32.downcase.split(SEPARATOR)
|
35
|
+
raise Errors::InvalidHRP unless hrp.downcase == HRP
|
36
|
+
raise Errors::InvalidLength if codex32.length < 48 || codex32.length > 127
|
37
|
+
raise Errors::SeparatorNotFound if remain.nil?
|
38
|
+
unless valid_checksum?(bech32_to_array(remain))
|
39
|
+
raise Errors::InvalidChecksum
|
40
|
+
end
|
41
|
+
|
42
|
+
checksum_len = remain.chars.length <= 93 ? 13 : 15
|
43
|
+
|
44
|
+
remain = remain.chars
|
45
|
+
threshold = remain[0].to_i
|
46
|
+
raise Errors::InvalidThreshold unless threshold.to_s == remain[0]
|
47
|
+
id = remain[1..4].join
|
48
|
+
share_index = remain[5]
|
49
|
+
payload_end = remain.length - checksum_len
|
50
|
+
payload = remain[6...payload_end].join
|
51
|
+
Share.new(id, threshold, share_index, payload)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Create codex32 string.
|
55
|
+
# @param [String] seed Secret with hex format.
|
56
|
+
# @param [Integer] threshold Threshold value.
|
57
|
+
# @param [String] id Identifier.
|
58
|
+
# @param [String] share_index Index of share.
|
59
|
+
# @return [Codex32::Share]
|
60
|
+
def from(seed:, id:, share_index:, threshold: 0)
|
61
|
+
raise Errors::InvalidThreshold unless threshold.is_a?(Integer)
|
62
|
+
raise Errors::InvalidIdentifier unless id.length == 4
|
63
|
+
raise Errors::InvalidBech32Character if CHARSET.index(share_index).nil?
|
64
|
+
payload =
|
65
|
+
array_to_bech32(
|
66
|
+
convert_bits([seed].pack("H*").unpack("C*"), 8, 5, padding: true)
|
67
|
+
)
|
68
|
+
Share.new(id, threshold, share_index, payload)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Convert bech32 string to array.
|
72
|
+
# @param [String] bech32_str bech32 string.
|
73
|
+
# @return [Array(Integer)] array of bech32 data.
|
74
|
+
def bech32_to_array(bech32_str)
|
75
|
+
bech32_str.downcase.each_char.map do |c|
|
76
|
+
i = CHARSET.index(c)
|
77
|
+
raise Errors::InvalidBech32Character if i.nil?
|
78
|
+
i
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Recover secret using +shares+.
|
83
|
+
# @param [Array(Codex32::Share)] shares Array of share.
|
84
|
+
# @param [String] share_index A share index.
|
85
|
+
# @return [Codex32::Share] Recovery secret.
|
86
|
+
def generate_share(shares, share_index)
|
87
|
+
raise ArgumentError, "shares must be array." unless shares.is_a?(Array)
|
88
|
+
raise IdentifierMismatch unless shares.map(&:id).uniq.length == 1
|
89
|
+
threshold = shares.map(&:threshold).uniq
|
90
|
+
threshold.delete(0)
|
91
|
+
raise Errors::ThresholdMismatch unless threshold.length == 1
|
92
|
+
index = CHARSET.index(share_index.downcase)
|
93
|
+
raise Errors::InvalidBech32Character if index.nil?
|
94
|
+
indices = shares.map(&:index).uniq
|
95
|
+
unless indices.length == shares.length
|
96
|
+
raise ArgumentError, "Share index duplicate."
|
97
|
+
end
|
98
|
+
raise Errors::DuplicateShareIndex if indices.first == index
|
99
|
+
raise Errors::InsufficientShares if shares.length < shares[0].threshold
|
100
|
+
|
101
|
+
data =
|
102
|
+
shares.map do |share|
|
103
|
+
bech32_to_array(
|
104
|
+
share.threshold.to_s + share.id + share.index + share.payload
|
105
|
+
)
|
106
|
+
end
|
107
|
+
result = interpolate_at(data, index)
|
108
|
+
Share.new(
|
109
|
+
shares.first.id,
|
110
|
+
threshold.first,
|
111
|
+
CHARSET[result[5]],
|
112
|
+
array_to_bech32(result[6..])
|
113
|
+
)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Convert array to bech32 string.
|
117
|
+
# @param [Array(Integer)] data An array.
|
118
|
+
# @return [String] bech32 string.
|
119
|
+
def array_to_bech32(data)
|
120
|
+
data.map { |d| CHARSET[d] }.join
|
121
|
+
end
|
122
|
+
|
123
|
+
# @param [Array(Integer)] data
|
124
|
+
# @return [Array(Integer)]
|
125
|
+
def polymod(data)
|
126
|
+
generators = [
|
127
|
+
0x19dc500ce73fde210,
|
128
|
+
0x1bfae00def77fe529,
|
129
|
+
0x1fbd920fffe7bee52,
|
130
|
+
0x1739640bdeee3fdad,
|
131
|
+
0x07729a039cfc75f5a
|
132
|
+
]
|
133
|
+
residue = 0x23181b3
|
134
|
+
data.each do |d|
|
135
|
+
b = residue >> 60
|
136
|
+
residue = (residue & 0x0fffffffffffffff) << 5 ^ d
|
137
|
+
5.times { |i| residue ^= ((b >> i) & 1).zero? ? 0 : generators[i] }
|
138
|
+
end
|
139
|
+
residue
|
140
|
+
end
|
141
|
+
|
142
|
+
# @param [Array(Integer)] data
|
143
|
+
# @return [Array(Integer)]
|
144
|
+
def long_polymod(data)
|
145
|
+
generators = [
|
146
|
+
0x3d59d273535ea62d897,
|
147
|
+
0x7a9becb6361c6c51507,
|
148
|
+
0x543f9b7e6c38d8a2a0e,
|
149
|
+
0x0c577eaeccf1990d13c,
|
150
|
+
0x1887f74f8dc71b10651
|
151
|
+
]
|
152
|
+
residue = 0x23181b3
|
153
|
+
data.each do |d|
|
154
|
+
b = residue >> 70
|
155
|
+
residue = (residue & 0x3fffffffffffffffff) << 5 ^ d
|
156
|
+
5.times { |i| residue ^= ((b >> i) & 1).zero? ? 0 : generators[i] }
|
157
|
+
end
|
158
|
+
residue
|
159
|
+
end
|
160
|
+
|
161
|
+
# Convert a +data+ where each byte is encoding +from+ bits to a byte slice where each byte is encoding +to+ bits.
|
162
|
+
# @param [Array] data
|
163
|
+
# @param [Integer] from
|
164
|
+
# @param [Integer] to
|
165
|
+
# @param [Boolean] padding
|
166
|
+
# @return [Array]
|
167
|
+
def convert_bits(data, from, to, padding: true)
|
168
|
+
acc = 0
|
169
|
+
bits = 0
|
170
|
+
ret = []
|
171
|
+
maxv = (1 << to) - 1
|
172
|
+
max_acc = (1 << (from + to - 1)) - 1
|
173
|
+
data.each do |v|
|
174
|
+
return nil if v.negative? || (v >> from) != 0
|
175
|
+
acc = ((acc << from) | v) & max_acc
|
176
|
+
bits += from
|
177
|
+
while bits >= to
|
178
|
+
bits -= to
|
179
|
+
ret << ((acc >> bits) & maxv)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
ret << ((acc << (to - bits)) & maxv) if padding && bits != 0
|
183
|
+
ret
|
184
|
+
end
|
185
|
+
|
186
|
+
# Interpolate a set of shares to derive a share at a specific index.
|
187
|
+
# @param [Array(Integer)] data A set of shares.
|
188
|
+
# Each share is an array of the following data transformed in a bech32 table:
|
189
|
+
# threshold + id + index + payload.
|
190
|
+
# @param [Integer] x index value.
|
191
|
+
def interpolate_at(data, x)
|
192
|
+
indices = data.map { |d| d[5] }
|
193
|
+
w = bech32_lagrange(indices, x)
|
194
|
+
data.first.length.times.map do |i|
|
195
|
+
n = 0
|
196
|
+
data.length.times { |j| n ^= bech32_mul(w[j], data[j][i]) }
|
197
|
+
n
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
# Check whether checksum is valid or not.
|
202
|
+
# @param [Array(Integer)] data A part as a list of integers representing the characters converted.
|
203
|
+
# @return [Boolean]
|
204
|
+
def valid_checksum?(data)
|
205
|
+
if data.length <= 93
|
206
|
+
polymod(data) == MS32_CONST
|
207
|
+
elsif data.length >= 96
|
208
|
+
long_polymod(data) == MS32_LONG_CONST
|
209
|
+
else
|
210
|
+
false
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def bech32_lagrange(data, x)
|
215
|
+
n = 1
|
216
|
+
c = []
|
217
|
+
data.each do |i|
|
218
|
+
n = bech32_mul(n, i ^ x)
|
219
|
+
m = 1
|
220
|
+
data.each { |j| m = bech32_mul(m, (i == j ? x : i) ^ j) }
|
221
|
+
c << m
|
222
|
+
end
|
223
|
+
c.map { |i| bech32_mul(n, BECH32_INV[i]) }
|
224
|
+
end
|
225
|
+
|
226
|
+
def bech32_mul(a, b)
|
227
|
+
result = 0
|
228
|
+
5.times do |i|
|
229
|
+
result ^= ((b >> i) & 1).zero? ? 0 : a
|
230
|
+
a *= 2
|
231
|
+
a ^= a >= 32 ? 41 : 0
|
232
|
+
end
|
233
|
+
result
|
234
|
+
end
|
235
|
+
end
|
data/sig/codex32.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codex32
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- azuchi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-03-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ''
|
14
|
+
email:
|
15
|
+
- techmedia.think@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".prettierignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- ".ruby-gemset"
|
24
|
+
- ".ruby-version"
|
25
|
+
- CHANGELOG.md
|
26
|
+
- CODE_OF_CONDUCT.md
|
27
|
+
- Gemfile
|
28
|
+
- LICENSE.txt
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- bin/console
|
32
|
+
- bin/setup
|
33
|
+
- codex32.gemspec
|
34
|
+
- lib/codex32.rb
|
35
|
+
- lib/codex32/errors.rb
|
36
|
+
- lib/codex32/share.rb
|
37
|
+
- lib/codex32/version.rb
|
38
|
+
- sig/codex32.rbs
|
39
|
+
homepage: https://github.com/azuchi/codex32rb
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata:
|
43
|
+
homepage_uri: https://github.com/azuchi/codex32rb
|
44
|
+
source_code_uri: https://github.com/azuchi/codex32rb
|
45
|
+
changelog_uri: https://github.com/azuchi/codex32rb
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.7.0
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubygems_version: 3.4.1
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Ruby implementation of codex32
|
65
|
+
test_files: []
|