merkle 0.3.0 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +28 -1
- data/lib/merkle/config.rb +8 -6
- data/lib/merkle/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce4faaabf24a85b734bd6cfe35081dfcc897bf418c5b391e68a84f8ec0aa9977
|
4
|
+
data.tar.gz: 944144e97463b2d0ad1ff7b2a882210548786035f52fa0090d505bbad7b9f88e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49988d95199cf3e6a5e5944f69205d6b354ed4e5d811325c4e6ca8d4a868044b6c3dfc380491d1aa9c577ca7848770ea3c7e56d9212a1bb2a60cbc5c330a0060
|
7
|
+
data.tar.gz: 107fc3472cca81407b6fbc97a0981711f5538cdb2e657a032911d7866021fa37a4a30ac00ce5bcbebe2585849f700fc01c892c41670f619150d809c578fbb007
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ A Ruby library for Merkle tree construction and proof generation with support fo
|
|
4
4
|
|
5
5
|
## Features
|
6
6
|
|
7
|
-
- **Multiple tree structures**: Binary Tree (Bitcoin-compatible) and
|
7
|
+
- **Multiple tree structures**: Binary Tree (Bitcoin-compatible), Adaptive Tree, and Custom Tree implementations
|
8
8
|
- **Flexible configuration**: Support for different hash algorithms (SHA256, Double SHA256) and tagged hashing
|
9
9
|
- **Proof generation and verification**: Generate and verify Merkle proofs for any leaf
|
10
10
|
- **Sorted hashing support**: Optional lexicographical sorting for deterministic tree construction
|
@@ -98,6 +98,32 @@ proof = adaptive_tree.generate_proof(0)
|
|
98
98
|
puts "Adaptive tree proof valid: #{proof.valid?}"
|
99
99
|
```
|
100
100
|
|
101
|
+
### Custom Tree Example
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
# CustomTree allows you to define your own tree structure using nested arrays
|
105
|
+
# This gives you precise control over how leaves are grouped
|
106
|
+
|
107
|
+
# Example 1: Basic usage with pre-hashed leaves
|
108
|
+
leaf_a = config.tagged_hash('A')
|
109
|
+
leaf_b = config.tagged_hash('B')
|
110
|
+
leaf_c = config.tagged_hash('C')
|
111
|
+
leaf_d = config.tagged_hash('D')
|
112
|
+
|
113
|
+
# Define structure: [[A, [B, C]], D]
|
114
|
+
nested_leaves = [[leaf_a, [leaf_b, leaf_c]], leaf_d]
|
115
|
+
custom_tree = Merkle::CustomTree.new(config: config, leaves: nested_leaves)
|
116
|
+
|
117
|
+
root = custom_tree.compute_root
|
118
|
+
puts "Custom tree root: #{root}"
|
119
|
+
|
120
|
+
# Valid structures:
|
121
|
+
# - [A, B] → Simple binary node
|
122
|
+
# - [[A, B], C] → Left subtree with right leaf
|
123
|
+
# - [A] → Single child node
|
124
|
+
# Invalid: [A, B, C] → Error (max 2 children per node)
|
125
|
+
```
|
126
|
+
|
101
127
|
### Configuration Options
|
102
128
|
|
103
129
|
```ruby
|
@@ -120,6 +146,7 @@ non_sorted_config = Merkle::Config.new(
|
|
120
146
|
|
121
147
|
- **BinaryTree**: Bitcoin-compatible merkle tree that duplicates odd nodes
|
122
148
|
- **AdaptiveTree**: Unbalanced tree that promotes odd nodes to higher levels for optimized access patterns
|
149
|
+
- **CustomTree**: User-defined tree structure using nested arrays for precise control over leaf grouping
|
123
150
|
|
124
151
|
### Proof System
|
125
152
|
|
data/lib/merkle/config.rb
CHANGED
@@ -41,19 +41,21 @@ module Merkle
|
|
41
41
|
# @return [String] Tagged hash value.
|
42
42
|
def tagged_hash(data, tag = branch_tag)
|
43
43
|
raise ArgumentError, "data must be string." unless data.is_a?(String)
|
44
|
-
|
44
|
+
raise ArgumentError, "tag must be a String." unless tag.is_a?(String)
|
45
|
+
|
46
|
+
data_bin = hex_to_bin(data).b
|
45
47
|
|
46
48
|
unless tag.empty?
|
47
|
-
|
48
|
-
|
49
|
+
tag_bin = Digest::SHA256.digest(tag).b
|
50
|
+
data_bin = tag_bin + tag_bin + data_bin
|
49
51
|
end
|
50
52
|
|
51
53
|
case hash_type
|
52
54
|
when :sha256
|
53
|
-
Digest::SHA256.digest(
|
55
|
+
Digest::SHA256.digest(data_bin)
|
54
56
|
when :double_sha256
|
55
|
-
Digest::SHA256.digest(Digest::SHA256.digest(
|
57
|
+
Digest::SHA256.digest(Digest::SHA256.digest(data_bin))
|
56
58
|
end
|
57
59
|
end
|
58
60
|
end
|
59
|
-
end
|
61
|
+
end
|
data/lib/merkle/version.rb
CHANGED