imt_rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1460f5d4546e3b0a202cf8bcc4b86ac30dfa41358c7a800a92588a5ea905bd8
4
+ data.tar.gz: 80ba4ceed54a0b4f9b60441b56dfaa1763bae329617241805b13304f264207c5
5
+ SHA512:
6
+ metadata.gz: beeeb75f23b94063fa18b5c39736cec947fb81bab50f1aa8d46c84ffa5fafc15e4807223ac6190ffdc5d88a28e46d195eab0acf8fd832016f94e23fcaa3042ee
7
+ data.tar.gz: 27afb990ec9904d4a8d1da4add6c65a6ed3475cf7715e549cb09302e2fcb7605543d8f2dbe2390f9e4f0446057040a2467c35ba13feed45f196670b934680266
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
@@ -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 wuminzhe@gmail.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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in imt_rb.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ imt_rb (0.1.0)
5
+ keccak256
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ base64 (0.1.1)
12
+ diff-lcs (1.5.0)
13
+ json (2.6.3)
14
+ keccak256 (2.0.1)
15
+ language_server-protocol (3.17.0.3)
16
+ parallel (1.23.0)
17
+ parser (3.2.2.3)
18
+ ast (~> 2.4.1)
19
+ racc
20
+ racc (1.7.1)
21
+ rainbow (3.1.1)
22
+ rake (13.0.6)
23
+ regexp_parser (2.8.1)
24
+ rexml (3.2.6)
25
+ rspec (3.12.0)
26
+ rspec-core (~> 3.12.0)
27
+ rspec-expectations (~> 3.12.0)
28
+ rspec-mocks (~> 3.12.0)
29
+ rspec-core (3.12.2)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-expectations (3.12.3)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-mocks (3.12.6)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-support (3.12.1)
38
+ rubocop (1.56.3)
39
+ base64 (~> 0.1.1)
40
+ json (~> 2.3)
41
+ language_server-protocol (>= 3.17.0)
42
+ parallel (~> 1.10)
43
+ parser (>= 3.2.2.3)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ regexp_parser (>= 1.8, < 3.0)
46
+ rexml (>= 3.2.5, < 4.0)
47
+ rubocop-ast (>= 1.28.1, < 2.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 2.4.0, < 3.0)
50
+ rubocop-ast (1.29.0)
51
+ parser (>= 3.2.1.0)
52
+ ruby-progressbar (1.13.0)
53
+ unicode-display_width (2.4.2)
54
+
55
+ PLATFORMS
56
+ arm64-darwin-21
57
+
58
+ DEPENDENCIES
59
+ imt_rb!
60
+ rake (~> 13.0)
61
+ rspec (~> 3.0)
62
+ rubocop (~> 1.21)
63
+
64
+ BUNDLED WITH
65
+ 2.4.13
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Aki Wu
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,7 @@
1
+ # ImtRb
2
+
3
+ Ruby implementation of Incremental Merkle tree with height 32.
4
+
5
+ ref:
6
+ 1. https://github.com/darwinia-network/ORMP/blob/main/src/imt/IncrementalMerkleTree.sol
7
+ 2. https://etherscan.io/address/0x00000000219ab540356cbb839cbe05303d7705fa#code
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,23 @@
1
+ module ImtRb
2
+ module Helper
3
+ def self.included(by)
4
+ by.extend(self)
5
+ end
6
+
7
+ def hexdigest(str)
8
+ Digest::Keccak256.new.hexdigest(str)
9
+ end
10
+
11
+ def digest(str)
12
+ Digest::Keccak256.new.digest(str)
13
+ end
14
+
15
+ def to_binary(str)
16
+ [str].pack("H*")
17
+ end
18
+
19
+ def to_hex(binary_str)
20
+ binary_str.unpack1("H*")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,109 @@
1
+ require "keccak256"
2
+ require_relative "helper"
3
+
4
+ module ImtRb
5
+ class IncrementalMerkleTree
6
+ include ImtRb::Helper
7
+
8
+ TREE_DEPTH = 32
9
+ MAX_LEAVES = 2**TREE_DEPTH
10
+ EMPTY_LEAF = "\x00" * 32 # == to_binary("0000000000000000000000000000000000000000000000000000000000000000")
11
+
12
+ # the array index of ZERO_HASHES is level index of the tree
13
+ ZERO_HASHES = [
14
+ to_binary("0000000000000000000000000000000000000000000000000000000000000000"),
15
+ to_binary("ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5"),
16
+ to_binary("b4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30"),
17
+ to_binary("21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85"),
18
+ to_binary("e58769b32a1beaf1ea27375a44095a0d1fb664ce2dd358e7fcbfb78c26a19344"),
19
+ to_binary("0eb01ebfc9ed27500cd4dfc979272d1f0913cc9f66540d7e8005811109e1cf2d"),
20
+ to_binary("887c22bd8750d34016ac3c66b5ff102dacdd73f6b014e710b51e8022af9a1968"),
21
+ to_binary("ffd70157e48063fc33c97a050f7f640233bf646cc98d9524c6b92bcf3ab56f83"),
22
+ to_binary("9867cc5f7f196b93bae1e27e6320742445d290f2263827498b54fec539f756af"),
23
+ to_binary("cefad4e508c098b9a7e1d8feb19955fb02ba9675585078710969d3440f5054e0"),
24
+ to_binary("f9dc3e7fe016e050eff260334f18a5d4fe391d82092319f5964f2e2eb7c1c3a5"),
25
+ to_binary("f8b13a49e282f609c317a833fb8d976d11517c571d1221a265d25af778ecf892"),
26
+ to_binary("3490c6ceeb450aecdc82e28293031d10c7d73bf85e57bf041a97360aa2c5d99c"),
27
+ to_binary("c1df82d9c4b87413eae2ef048f94b4d3554cea73d92b0f7af96e0271c691e2bb"),
28
+ to_binary("5c67add7c6caf302256adedf7ab114da0acfe870d449a3a489f781d659e8becc"),
29
+ to_binary("da7bce9f4e8618b6bd2f4132ce798cdc7a60e7e1460a7299e3c6342a579626d2"),
30
+ to_binary("2733e50f526ec2fa19a22b31e8ed50f23cd1fdf94c9154ed3a7609a2f1ff981f"),
31
+ to_binary("e1d3b5c807b281e4683cc6d6315cf95b9ade8641defcb32372f1c126e398ef7a"),
32
+ to_binary("5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0"),
33
+ to_binary("b46a28b6f55540f89444f63de0378e3d121be09e06cc9ded1c20e65876d36aa0"),
34
+ to_binary("c65e9645644786b620e2dd2ad648ddfcbf4a7e5b1a3a4ecfe7f64667a3f0b7e2"),
35
+ to_binary("f4418588ed35a2458cffeb39b93d26f18d2ab13bdce6aee58e7b99359ec2dfd9"),
36
+ to_binary("5a9c16dc00d6ef18b7933a6f8dc65ccb55667138776f7dea101070dc8796e377"),
37
+ to_binary("4df84f40ae0c8229d0d6069e5c8f39a7c299677a09d367fc7b05e3bc380ee652"),
38
+ to_binary("cdc72595f74c7b1043d0e1ffbab734648c838dfb0527d971b602bc216c9619ef"),
39
+ to_binary("0abf5ac974a1ed57f4050aa510dd9c74f508277b39d7973bb2dfccc5eeb0618d"),
40
+ to_binary("b8cd74046ff337f0a7bf2c8e03e10f642c1886798d71806ab1e888d9e5ee87d0"),
41
+ to_binary("838c5655cb21c6cb83313b5a631175dff4963772cce9108188b34ac87c81c41e"),
42
+ to_binary("662ee4dd2dd7b2bc707961b1e646c4047669dcb6584f0d8d770daf5d7e7deb2e"),
43
+ to_binary("388ab20e2573d171a88108e79d820e98f26c0b84aa8b2f4aa4968dbb818ea322"),
44
+ to_binary("93237c50ba75ee485f4c22adf2f741400bdf8d6a9cc7df7ecae576221665d735"),
45
+ to_binary("8448818bb4ae4562849e949e17ac16e0be16688e156b5cf15e098c627c0056a9")
46
+ ].freeze
47
+
48
+ attr_reader :tree
49
+
50
+ def initialize
51
+ @tree = {
52
+ branch: Array.new(TREE_DEPTH) { EMPTY_LEAF }, # 初始化为零值
53
+ count: 0
54
+ }
55
+ end
56
+
57
+ # leaf: binary
58
+ def append(leaf)
59
+ raise "Merkle tree is full" if @tree[:count] >= MAX_LEAVES
60
+
61
+ @tree[:count] += 1
62
+ size = @tree[:count]
63
+ (0...TREE_DEPTH).each do |level|
64
+ if size & 1 == 1
65
+ @tree[:branch][level] = leaf
66
+ return
67
+ end
68
+ leaf = digest(@tree[:branch][level] + leaf)
69
+ size /= 2
70
+ end
71
+
72
+ raise "unreachable"
73
+ end
74
+
75
+ def root
76
+ index = @tree[:count]
77
+
78
+ hash = ZERO_HASHES[0]
79
+ (0...TREE_DEPTH).each do |level|
80
+ ith_bit = (index >> level) & 0x01
81
+ the_next = @tree[:branch][level]
82
+
83
+ hash =
84
+ if ith_bit == 1
85
+ digest(the_next + hash)
86
+ else
87
+ digest(hash + ZERO_HASHES[level])
88
+ end
89
+ end
90
+ hash
91
+ end
92
+
93
+ def root_hex
94
+ to_hex(root)
95
+ end
96
+
97
+ def proof
98
+ index = @tree[:count] - 1
99
+
100
+ result = []
101
+ (0...TREE_DEPTH - 1).each do |level|
102
+ ith_bit = (index >> level) & 0x01
103
+ result[level] = ith_bit == 1 ? @tree[:branch][level] : ZERO_HASHES[level]
104
+ end
105
+
106
+ result
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImtRb
4
+ VERSION = "0.1.0"
5
+ end
data/lib/imt_rb.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "imt_rb/version"
4
+ require_relative "imt_rb/incremental_merkle_tree"
5
+
6
+ module ImtRb
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
data/sig/imt_rb.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module ImtRb
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imt_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aki Wu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: keccak256
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Incremental Merkle Tree in Ruby
28
+ email:
29
+ - wuminzhe@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CODE_OF_CONDUCT.md
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/imt_rb.rb
43
+ - lib/imt_rb/helper.rb
44
+ - lib/imt_rb/incremental_merkle_tree.rb
45
+ - lib/imt_rb/version.rb
46
+ - sig/imt_rb.rbs
47
+ homepage:
48
+ licenses:
49
+ - MIT
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.6.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.3.7
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Incremental Merkle Tree in Ruby
70
+ test_files: []