mssmt 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 +17 -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 +66 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/mssmt/branch_node.rb +25 -0
- data/lib/mssmt/leaf_node.rb +34 -0
- data/lib/mssmt/store/default_store.rb +48 -0
- data/lib/mssmt/store.rb +8 -0
- data/lib/mssmt/tree.rb +172 -0
- data/lib/mssmt/version.rb +5 -0
- data/lib/mssmt.rb +15 -0
- data/mssmt.gemspec +37 -0
- data/sig/mssmt.rbs +4 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c2bf77bb8efa4e33fe73b3bd1b54e577e02097fadda8420924caf8df0ad9df21
|
4
|
+
data.tar.gz: a5715d5289504147a0b76f8c3935910df965cae05df2a284136735389f8daa27
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 30e1c75109384039d6d87ed9702e7bfbe2848e499285460b67da66dd80668e32590ce409d5deedd64a08ed5cae6f558d40817c73275a271c0b92c6a0f1cab8e0
|
7
|
+
data.tar.gz: 6231535740549dfdb317e122945464ec92789c24479f164058ebdb06a3bfb2cabd688a0b6c0eca4c48881e9a891f5b72532a7609b4ec27edf132f9986f6ad558
|
data/.prettierignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,17 @@
|
|
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
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
mssmtrb
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-3.1.2
|
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
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 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,66 @@
|
|
1
|
+
# Merkle Sum Sparse Merkle Tree
|
2
|
+
|
3
|
+
This is the Merkle Sum Sparse Merkle Tree(MS-SMT) ruby library.
|
4
|
+
[MS-SMT](https://github.com/Roasbeef/bips/blob/bip-taro/bip-taro.mediawiki#MerkleSum_Sparse_Merkle_Trees) is a tree
|
5
|
+
that combines the Sparse Merkle Tree and the Merkle-Sum Tree and is defined by the [Taro protocol](https://github.com/Roasbeef/bips/blob/bip-taro/bip-taro.mediawiki).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'mssmt'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mssmt
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'mssmt'
|
27
|
+
|
28
|
+
# initialize tree
|
29
|
+
tree = MSSMT::Tree.new
|
30
|
+
|
31
|
+
# Add leaf node to tree with key.
|
32
|
+
leaf = MSSMT::LeafNode.new("Data", 1_000)
|
33
|
+
key = "5bc565ef18dbe0636cd3398a870ae24e1f184e5c484e1af3a502f77a0aceb0c5"
|
34
|
+
tree.insert(key, leaf)
|
35
|
+
|
36
|
+
# Get root node
|
37
|
+
root_node = tree.root_node
|
38
|
+
# Root hash
|
39
|
+
root_hash = tree.root_hash
|
40
|
+
# or
|
41
|
+
root_node.node_hash
|
42
|
+
|
43
|
+
# Get leaf node in tree
|
44
|
+
leaf = tree.get(key)
|
45
|
+
|
46
|
+
# Get merkle proof correspond to key
|
47
|
+
proof = tree.merkle_proof(key)
|
48
|
+
|
49
|
+
# Check merkle proof validity.
|
50
|
+
result = tree.valid_merkle_proof?(key, leaf, proof)
|
51
|
+
|
52
|
+
# Delete leaf node
|
53
|
+
tree.delete(key)
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mssmt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/mssmt/blob/master/CODE_OF_CONDUCT.md).
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
63
|
+
|
64
|
+
## Code of Conduct
|
65
|
+
|
66
|
+
Everyone interacting in the Mssmt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/mssmt/blob/master/CODE_OF_CONDUCT.md).
|
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 'mssmt'
|
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
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MSSMT
|
4
|
+
# Intermediate or root node within a MS-SMT.
|
5
|
+
class BranchNode
|
6
|
+
attr_reader :left, :right, :node_hash, :sum
|
7
|
+
|
8
|
+
def initialize(left, right)
|
9
|
+
if !left.is_a?(BranchNode) && !left.is_a?(LeafNode)
|
10
|
+
raise ArgumentError, "left must be a branch or leaf node"
|
11
|
+
end
|
12
|
+
if !right.is_a?(BranchNode) && !right.is_a?(LeafNode)
|
13
|
+
raise ArgumentError, "right must be a branch or leaf node"
|
14
|
+
end
|
15
|
+
|
16
|
+
@left = left
|
17
|
+
@right = right
|
18
|
+
@sum = left.sum + right.sum
|
19
|
+
@node_hash =
|
20
|
+
Digest::SHA256.digest(
|
21
|
+
"#{left.node_hash}#{right.node_hash}#{[@sum].pack("Q>")}"
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MSSMT
|
4
|
+
# Leaf node within a MS-SMT that commit to a value and some integer value (the sum) associated with the value.
|
5
|
+
class LeafNode
|
6
|
+
attr_reader :value, :sum
|
7
|
+
|
8
|
+
# Constructor
|
9
|
+
# @param [String] value node value with binary format.
|
10
|
+
# @param [Integer] sum integer value associated with the value
|
11
|
+
def initialize(value, sum)
|
12
|
+
@value = value
|
13
|
+
@sum = sum
|
14
|
+
end
|
15
|
+
|
16
|
+
# Generate empty leaf node.
|
17
|
+
# @return [MSSMT::LeafNode]
|
18
|
+
def self.empty_leaf
|
19
|
+
LeafNode.new(nil, 0)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Calculate node hash.
|
23
|
+
# @return [String] hash value.
|
24
|
+
def node_hash
|
25
|
+
Digest::SHA256.digest("#{value}#{[sum].pack("Q>")}")
|
26
|
+
end
|
27
|
+
|
28
|
+
# Check whether value and sum is empty.
|
29
|
+
# @return [Boolean]
|
30
|
+
def empty?
|
31
|
+
(value.nil? or value.empty?) && sum.zero?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MSSMT
|
4
|
+
module Store
|
5
|
+
# In-memory implementation of the tree store.
|
6
|
+
class DefaultStore
|
7
|
+
attr_accessor :branches, :leaves, :root
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@branches = {}
|
11
|
+
@leaves = {}
|
12
|
+
@root = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
# Insert branch node.
|
16
|
+
# @param [MSSMT::BranchNode] branch
|
17
|
+
# @raise [ArgumentError]
|
18
|
+
def insert_branch(branch)
|
19
|
+
unless branch.is_a?(MSSMT::BranchNode)
|
20
|
+
raise "branch must be MSSMT::BranchNode"
|
21
|
+
end
|
22
|
+
|
23
|
+
branches[branch.node_hash] = branch
|
24
|
+
end
|
25
|
+
|
26
|
+
# Insert leaf node.
|
27
|
+
# @param [MSSMT::LeafNode] leaf
|
28
|
+
# @raise [ArgumentError]
|
29
|
+
def insert_leaf(leaf)
|
30
|
+
raise "leaf must be MSSMT::LeafNode" unless leaf.is_a?(MSSMT::LeafNode)
|
31
|
+
|
32
|
+
leaves[leaf.node_hash] = leaf
|
33
|
+
end
|
34
|
+
|
35
|
+
# Delete branch node.
|
36
|
+
# @param [String] node_hash node hash of branch node.
|
37
|
+
def delete_branch(node_hash)
|
38
|
+
branches.delete(node_hash)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Delete leaf node
|
42
|
+
# @param [String] node_hash node hash of leaf node.
|
43
|
+
def delete_leaf(node_hash)
|
44
|
+
leaves.delete(node_hash)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/mssmt/store.rb
ADDED
data/lib/mssmt/tree.rb
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MSSMT
|
4
|
+
# MS-SMT tree
|
5
|
+
class Tree
|
6
|
+
# The size of a SHA256 checksum in bytes.
|
7
|
+
HASH_SIZE = 32
|
8
|
+
# Depth of the MS-SMT(using SHA256)
|
9
|
+
MAX_LEVEL = HASH_SIZE * 8
|
10
|
+
# Index of the last bit for MS-SMT keys
|
11
|
+
LAST_BIT_INDEX = MAX_LEVEL - 1
|
12
|
+
|
13
|
+
attr_reader :empty_tree, :store
|
14
|
+
|
15
|
+
def initialize(store: MSSMT::Store::DefaultStore.new)
|
16
|
+
@empty_tree = build_empty_tree
|
17
|
+
@store = store
|
18
|
+
end
|
19
|
+
|
20
|
+
# Root hash of this tree
|
21
|
+
# @return [String] root hash value.
|
22
|
+
def root_hash
|
23
|
+
root_node.node_hash
|
24
|
+
end
|
25
|
+
|
26
|
+
# Get root node in tree.
|
27
|
+
# @return [MSSMT::BranchNode]
|
28
|
+
def root_node
|
29
|
+
store.root.nil? ? empty_tree[0] : store.root
|
30
|
+
end
|
31
|
+
|
32
|
+
# Insert a leaf node at the given key within the MS-SMT.
|
33
|
+
# @param [String] key key with hex format.
|
34
|
+
# @param [MSSMT::LeafNode] leaf leaf node.
|
35
|
+
# @return [MSSMT::BranchNode] Updated root node.
|
36
|
+
# @raise [MSSMT::Error]
|
37
|
+
def insert(key, leaf)
|
38
|
+
unless [key].pack("H*").bytesize == HASH_SIZE
|
39
|
+
raise MSSMT::Error, "key must be #{HASH_SIZE} bytes"
|
40
|
+
end
|
41
|
+
|
42
|
+
prev_parents = Array.new(MSSMT::Tree::MAX_LEVEL)
|
43
|
+
siblings = Array.new(MSSMT::Tree::MAX_LEVEL)
|
44
|
+
walk_down(key) do |i, _, sibling, parent|
|
45
|
+
prev_parents[MSSMT::Tree::MAX_LEVEL - 1 - i] = parent.node_hash
|
46
|
+
siblings[MSSMT::Tree::MAX_LEVEL - 1 - i] = sibling
|
47
|
+
end
|
48
|
+
|
49
|
+
root =
|
50
|
+
walk_up(key, leaf, siblings) do |i, _, _, parent|
|
51
|
+
prev_parent = prev_parents[MSSMT::Tree::MAX_LEVEL - 1 - i]
|
52
|
+
unless prev_parent == empty_tree[i].node_hash
|
53
|
+
store.delete_branch(prev_parent)
|
54
|
+
end
|
55
|
+
unless parent.node_hash == empty_tree[i].node_hash
|
56
|
+
store.insert_branch(parent)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
leaf.empty? ? store.delete_leaf(key) : store.insert_leaf(leaf)
|
61
|
+
store.root = root
|
62
|
+
end
|
63
|
+
|
64
|
+
# Delete the leaf node found at the given key within the MS-SMT.
|
65
|
+
# @param [String] key key with hex format.
|
66
|
+
def delete(key)
|
67
|
+
store.root = insert(key, MSSMT::LeafNode.empty_leaf)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Get leaf node found at the given key within the MS-SMT.
|
71
|
+
# @param [String] key key with hex format.
|
72
|
+
# @return [MSSMT::LeafNode] leaf node.
|
73
|
+
def get(key)
|
74
|
+
walk_down(key)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Generate a merkle proof for the leaf node found at the given +key+.
|
78
|
+
# @param [String] key key with hex format.
|
79
|
+
# @return [Array] merkle proof
|
80
|
+
def merkle_proof(key)
|
81
|
+
proof = Array.new(MAX_LEVEL)
|
82
|
+
walk_down(key) { |i, _, sibling, _| proof[MAX_LEVEL - 1 - i] = sibling }
|
83
|
+
proof
|
84
|
+
end
|
85
|
+
|
86
|
+
# Verify whether a merkle proof for the leaf found at the given key is valid.
|
87
|
+
# @param [String] key key with hex format.
|
88
|
+
# @param [MSSMT::LeafNode] leaf leaf node.
|
89
|
+
# @param [Array] proof merkle proof.
|
90
|
+
def valid_merkle_proof?(key, leaf, proof)
|
91
|
+
root_hash == walk_up(key, leaf, proof).node_hash
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
# Get children
|
97
|
+
# @param [Integer] height Tree height.
|
98
|
+
# @param [String] key
|
99
|
+
# @return [Array(MSSMT::BranchNode|MSSMT::LeafNode)]
|
100
|
+
# @raise [MSSMT::Error]
|
101
|
+
def get_children(height, key)
|
102
|
+
node = get_node(height, key)
|
103
|
+
if node.is_a?(MSSMT::BranchNode)
|
104
|
+
return [
|
105
|
+
get_node(height + 1, node.left.node_hash),
|
106
|
+
get_node(height + 1, node.right.node_hash)
|
107
|
+
]
|
108
|
+
end
|
109
|
+
raise MSSMT::Error, "unexpected node type with key: #{key}"
|
110
|
+
end
|
111
|
+
|
112
|
+
def get_node(height, key)
|
113
|
+
return empty_tree[height] if empty_tree[height].node_hash == key
|
114
|
+
store.branches[key] || store.leaves[key]
|
115
|
+
end
|
116
|
+
|
117
|
+
# Walk down tree from root and return leaf node.
|
118
|
+
# @param [String] key
|
119
|
+
# @return [MSSMT::LeafNode] Leaf node corresponding to key.
|
120
|
+
def walk_down(key)
|
121
|
+
current = root_node
|
122
|
+
MSSMT::Tree::MAX_LEVEL.times do |i|
|
123
|
+
left, right = get_children(i, current.node_hash)
|
124
|
+
next_node, sibling =
|
125
|
+
bit_index(i, key).zero? ? [left, right] : [right, left]
|
126
|
+
yield(i, next_node, sibling, current) if block_given?
|
127
|
+
current = next_node
|
128
|
+
end
|
129
|
+
current
|
130
|
+
end
|
131
|
+
|
132
|
+
# Walk up from the +start+ leaf node up to the root with the help of +siblings+.
|
133
|
+
# @param [String] key
|
134
|
+
# @param [MSSMT::LeafNode] start Start leaf node.
|
135
|
+
# @param [Array] siblings
|
136
|
+
# @return [MSSMT::BranchNode] The root branch node computed.
|
137
|
+
def walk_up(key, start, siblings)
|
138
|
+
current = start
|
139
|
+
MAX_LEVEL.times do |index|
|
140
|
+
i = LAST_BIT_INDEX - index
|
141
|
+
sibling = siblings[LAST_BIT_INDEX - i]
|
142
|
+
left, right =
|
143
|
+
bit_index(i, key).zero? ? [current, sibling] : [sibling, current]
|
144
|
+
parent = BranchNode.new(left, right)
|
145
|
+
yield(i, current, sibling, parent) if block_given?
|
146
|
+
current = parent
|
147
|
+
end
|
148
|
+
current
|
149
|
+
end
|
150
|
+
|
151
|
+
def bit_index(idx, key)
|
152
|
+
value = [key].pack("H*")[idx / 8].ord
|
153
|
+
(value >> (idx % 8)) & 1
|
154
|
+
end
|
155
|
+
|
156
|
+
# Generate empty tree.
|
157
|
+
# @return [Array]
|
158
|
+
def build_empty_tree
|
159
|
+
tree = Array.new(MSSMT::Tree::MAX_LEVEL + 1)
|
160
|
+
tree[MSSMT::Tree::MAX_LEVEL] = MSSMT::LeafNode.empty_leaf
|
161
|
+
MSSMT::Tree::MAX_LEVEL.times do |i|
|
162
|
+
branch =
|
163
|
+
MSSMT::BranchNode.new(
|
164
|
+
tree[MSSMT::Tree::MAX_LEVEL - i],
|
165
|
+
tree[MSSMT::Tree::MAX_LEVEL - i]
|
166
|
+
)
|
167
|
+
tree[MSSMT::Tree::MAX_LEVEL - (i + 1)] = branch
|
168
|
+
end
|
169
|
+
tree
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
data/lib/mssmt.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "digest"
|
4
|
+
require_relative "mssmt/version"
|
5
|
+
|
6
|
+
# Merkle Sum Sparse Merkle Tree
|
7
|
+
module MSSMT
|
8
|
+
class Error < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
autoload :Store, "mssmt/store"
|
12
|
+
autoload :LeafNode, "mssmt/leaf_node"
|
13
|
+
autoload :BranchNode, "mssmt/branch_node"
|
14
|
+
autoload :Tree, "mssmt/tree"
|
15
|
+
end
|
data/mssmt.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/mssmt/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'mssmt'
|
7
|
+
spec.version = MSSMT::VERSION
|
8
|
+
spec.authors = ['azuchi']
|
9
|
+
spec.email = ['azuchi@chaintope.com']
|
10
|
+
|
11
|
+
spec.summary = 'Ruby implementation of Merkle Sum Sparse Merkle Tree'
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = 'https://github.com/azuchi/mssmtrb'
|
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
|
data/sig/mssmt.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mssmt
|
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: 2022-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby implementation of Merkle Sum Sparse Merkle Tree
|
14
|
+
email:
|
15
|
+
- azuchi@chaintope.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
|
+
- lib/mssmt.rb
|
34
|
+
- lib/mssmt/branch_node.rb
|
35
|
+
- lib/mssmt/leaf_node.rb
|
36
|
+
- lib/mssmt/store.rb
|
37
|
+
- lib/mssmt/store/default_store.rb
|
38
|
+
- lib/mssmt/tree.rb
|
39
|
+
- lib/mssmt/version.rb
|
40
|
+
- mssmt.gemspec
|
41
|
+
- sig/mssmt.rbs
|
42
|
+
homepage: https://github.com/azuchi/mssmtrb
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata:
|
46
|
+
homepage_uri: https://github.com/azuchi/mssmtrb
|
47
|
+
source_code_uri: https://github.com/azuchi/mssmtrb
|
48
|
+
changelog_uri: https://github.com/azuchi/mssmtrb
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 2.7.0
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubygems_version: 3.3.23
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Ruby implementation of Merkle Sum Sparse Merkle Tree
|
68
|
+
test_files: []
|