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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eaa0d4d13ee5bd6ff5e8b81aeb50396b8932f6efea765cc64dd1ff19614ebd9e
4
- data.tar.gz: aa59ca4b87da23aa28707d4434209b36c03a7942b7635af036253c2dd7161607
3
+ metadata.gz: ce4faaabf24a85b734bd6cfe35081dfcc897bf418c5b391e68a84f8ec0aa9977
4
+ data.tar.gz: 944144e97463b2d0ad1ff7b2a882210548786035f52fa0090d505bbad7b9f88e
5
5
  SHA512:
6
- metadata.gz: fcf1016cc2bdbc721185ca647219c036cf05f13ad4534b4775d0ebb9ef3d585a9b97f1b40aac46b8f32b4aa3071821ba32d9ba7147ba86bf6c3a49eb0550290e
7
- data.tar.gz: 36aec619bb2280e91b015409933c57d050787535a6890a4ee41efd86b6380af3300f81e488e2d5871fbb617b2af955ca4c9ba2437f012107fcff6c8f94eb3ecb
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 Adaptive Tree implementations
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
- data = [data].pack('H*') if hex_string?(data)
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
- tag_hash = Digest::SHA256.digest(tag)
48
- data = tag_hash + tag_hash + data
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(data)
55
+ Digest::SHA256.digest(data_bin)
54
56
  when :double_sha256
55
- Digest::SHA256.digest(Digest::SHA256.digest(data))
57
+ Digest::SHA256.digest(Digest::SHA256.digest(data_bin))
56
58
  end
57
59
  end
58
60
  end
59
- end
61
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Merkle
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merkle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi