From 8eb334c90a158ed9867e2e60a6439c0438623f45 Mon Sep 17 00:00:00 2001 From: Samuel M Smith Date: Tue, 24 Nov 2026 10:53:47 -0700 Subject: [PATCH 1/2] fix trace to be more informative and align better for comparison --- src/keri/sit/siting.py | 17 +++++-- tests/sit/test_siting.py | 103 ++++++++++++++++++++------------------- 2 files changed, 64 insertions(+), 56 deletions(-) diff --git a/src/keri/sit/siting.py b/src/keri/sit/siting.py index 7b66c7ae9..9ade23768 100644 --- a/src/keri/sit/siting.py +++ b/src/keri/sit/siting.py @@ -6,7 +6,7 @@ """ - +import math from collections import namedtuple, deque from .. import Versionage, InvalidCodeError @@ -1733,7 +1733,7 @@ def _trace(self, node, leaves, branch, metrics, terse=True, leafive=True): #size = min(leafage.span.num, 16) leaves.append(f"{side}#{depth}={(leafage.limb.num>>248):08b}@{shows}") else: - leaves.append(f"{side}#{depth}={leafage.limb.numh}@{shows}") + leaves.append(f"{side}#{depth}={leafage.limb.num:032x}@{shows}") metrics['depthsum'] += depth if depth > metrics['depthmax']: metrics['depthmax'] = depth @@ -1750,7 +1750,7 @@ def _trace(self, node, leaves, branch, metrics, terse=True, leafive=True): righter = node.part.right - side = "right" + side = "rght" if leafive and not righter.nonce: # right leaf empty leaves.append(f"{side}#{depth}=empty@{self._showtwig(node, terse=terse)}") @@ -1761,7 +1761,7 @@ def _trace(self, node, leaves, branch, metrics, terse=True, leafive=True): #size = min(leafage.span.num, 16) leaves.append(f"{side}#{depth}={(leafage.limb.num>>248):08b}@{shows}") else: - leaves.append(f"{side}#{depth}={leafage.limb.numh}@{shows}") + leaves.append(f"{side}#{depth}={leafage.limb.num:032x}@{shows}") metrics['depthsum'] += depth if depth > metrics['depthmax']: metrics['depthmax'] = depth @@ -1796,11 +1796,18 @@ def _showtwig(self, node, terse=True): if size < node.part.span.num: show += "..." else: - show = node.part.limb.numh + size = node.part.span.num + if size <= 8: + show = f"s{node.part.span.num}:b.{twig:0{size}b}" + else: + size = math.ceil(size / 4) # 4 bits per hex digit + show = f"s{node.part.span.num}:x.{twig:0{size}x}" + #show = node.part.limb.numh else: show = f"root" + return show def shake(self, node=None): diff --git a/tests/sit/test_siting.py b/tests/sit/test_siting.py index 6300f058c..a8215fe41 100644 --- a/tests/sit/test_siting.py +++ b/tests/sit/test_siting.py @@ -664,7 +664,7 @@ def test_insert_logic(): assert trace == \ [ 'left#1=empty@root', - 'right#1=empty@root' + 'rght#1=empty@root' ] assert metrics == \ { @@ -711,7 +711,7 @@ def test_insert_logic(): right='1AAP') } trace, metrics = siter.trace() - assert trace == ['left#1=01000000@root', 'right#1=empty@root'] + assert trace == ['left#1=01000000@root', 'rght#1=empty@root'] metrics == {'depthmax': 1, 'depthmin': 1, 'depthsum': 1} # test right child @@ -748,7 +748,7 @@ def test_insert_logic(): assert trace == \ [ 'left#1=01000000@root', - 'right#1=10000000@root' + 'rght#1=10000000@root' ] metrics['depthave'] = metrics['depthsum'] / metrics['leafcount'] assert metrics == \ @@ -795,8 +795,8 @@ def test_insert_logic(): assert trace == \ [ 'left#2=01000000@010@root', - 'right#2=01010000@010@root', - 'right#1=10000000@root' + 'rght#2=01010000@010@root', + 'rght#1=10000000@root' ] metrics['depthave'] = metrics['depthsum'] / metrics['leafcount'] assert metrics == \ @@ -849,9 +849,9 @@ def test_insert_logic(): assert trace == \ [ 'left#2=01000000@010@root', - 'right#2=01010000@010@root', + 'rght#2=01010000@010@root', 'left#2=10000000@10@root', - 'right#2=10100000@10@root' + 'rght#2=10100000@10@root' ] metrics['depthave'] = metrics['depthsum'] / metrics['leafcount'] assert metrics == \ @@ -1073,13 +1073,13 @@ def test_insert_logic(): assert trace0 == \ [ 'left#3=01000000@010@01@root', - 'right#3=01010000@010@01@root', + 'rght#3=01010000@010@01@root', 'left#3=01100000@011@01@root', - 'right#3=01110000@011@01@root', + 'rght#3=01110000@011@01@root', 'left#3=10000000@10@1@root', - 'right#3=10100000@10@1@root', + 'rght#3=10100000@10@1@root', 'left#3=11100000@111@1@root', - 'right#3=11110000@111@1@root' + 'rght#3=11110000@111@1@root' ] assert metrics0 == \ { @@ -1120,10 +1120,10 @@ def test_insert_logic(): assert traceT0 == \ [ 'left#4=00001000@000@00@0@root', - 'right#4=00010000@000@00@0@root', - 'right#3=00100000@00@0@root', - 'right#2=01000000@0@root', - 'right#1=empty@root' + 'rght#4=00010000@000@00@0@root', + 'rght#3=00100000@00@0@root', + 'rght#2=01000000@0@root', + 'rght#1=empty@root' ] assert metricsT0 == \ { @@ -1220,23 +1220,23 @@ def test_insert_logic(): assert traceA == \ [ 'left#5=00000000@0000@000@00@0@root', - 'right#5=00001000@0000@000@00@0@root', - 'right#4=00010000@000@00@0@root', + 'rght#5=00001000@0000@000@00@0@root', + 'rght#4=00010000@000@00@0@root', 'left#4=00100000@001@00@0@root', - 'right#4=00110000@001@00@0@root', + 'rght#4=00110000@001@00@0@root', 'left#4=01000000@010@01@0@root', - 'right#4=01010000@010@01@0@root', + 'rght#4=01010000@010@01@0@root', 'left#4=01100000@011@01@0@root', - 'right#4=01110000@011@01@0@root', + 'rght#4=01110000@011@01@0@root', 'left#4=10000000@100@10@1@root', - 'right#4=10010000@100@10@1@root', + 'rght#4=10010000@100@10@1@root', 'left#4=10100000@101@10@1@root', - 'right#4=10110000@101@10@1@root', + 'rght#4=10110000@101@10@1@root', 'left#4=11000000@110@11@1@root', - 'right#4=11010000@110@11@1@root', + 'rght#4=11010000@110@11@1@root', 'left#4=11100000@111@11@1@root', 'left#5=11110000@1111@111@11@1@root', - 'right#5=11111111@1111@111@11@1@root' + 'rght#5=11111111@1111@111@11@1@root' ] assert metricsA == \ { @@ -1263,27 +1263,29 @@ def test_insert_logic(): #siter.root = siter._insert(leaf, siter.root) #siter.pinLeaf(leaf.said, leaf.part) + # notice SIT tree compression to low elevation subtree trace, metrics = siter.trace(terse=False) assert trace == \ [ - 'left#5=0@0@0@0@0@root', - 'right#5=1@0@0@0@0@root', - 'left#5=2@2@0@0@0@root', - 'right#5=3@2@0@0@0@root', - 'left#5=4@4@4@0@0@root', - 'right#5=5@4@4@0@0@root', - 'left#5=6@6@4@0@0@root', - 'right#5=7@6@4@0@0@root', - 'left#5=8@8@8@8@0@root', - 'right#5=9@8@8@8@0@root', - 'left#5=a@a@8@8@0@root', - 'right#5=b@a@8@8@0@root', - 'left#5=c@c@c@8@0@root', - 'right#5=d@c@c@8@0@root', - 'left#5=e@e@c@8@0@root', - 'right#5=f@e@c@8@0@root', - 'right#1=empty@root' - ] # notice SIT tree compression to low elevation subtree + 'left#5=00000000000000000000000000000000@s255:x.0000000000000000000000000000000000000000000000000000000000000000@s254:x.0000000000000000000000000000000000000000000000000000000000000000@s253:x.0000000000000000000000000000000000000000000000000000000000000000@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'rght#5=00000000000000000000000000000001@s255:x.0000000000000000000000000000000000000000000000000000000000000000@s254:x.0000000000000000000000000000000000000000000000000000000000000000@s253:x.0000000000000000000000000000000000000000000000000000000000000000@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'left#5=00000000000000000000000000000002@s255:x.0000000000000000000000000000000000000000000000000000000000000001@s254:x.0000000000000000000000000000000000000000000000000000000000000000@s253:x.0000000000000000000000000000000000000000000000000000000000000000@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'rght#5=00000000000000000000000000000003@s255:x.0000000000000000000000000000000000000000000000000000000000000001@s254:x.0000000000000000000000000000000000000000000000000000000000000000@s253:x.0000000000000000000000000000000000000000000000000000000000000000@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'left#5=00000000000000000000000000000004@s255:x.0000000000000000000000000000000000000000000000000000000000000002@s254:x.0000000000000000000000000000000000000000000000000000000000000001@s253:x.0000000000000000000000000000000000000000000000000000000000000000@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'rght#5=00000000000000000000000000000005@s255:x.0000000000000000000000000000000000000000000000000000000000000002@s254:x.0000000000000000000000000000000000000000000000000000000000000001@s253:x.0000000000000000000000000000000000000000000000000000000000000000@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'left#5=00000000000000000000000000000006@s255:x.0000000000000000000000000000000000000000000000000000000000000003@s254:x.0000000000000000000000000000000000000000000000000000000000000001@s253:x.0000000000000000000000000000000000000000000000000000000000000000@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'rght#5=00000000000000000000000000000007@s255:x.0000000000000000000000000000000000000000000000000000000000000003@s254:x.0000000000000000000000000000000000000000000000000000000000000001@s253:x.0000000000000000000000000000000000000000000000000000000000000000@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'left#5=00000000000000000000000000000008@s255:x.0000000000000000000000000000000000000000000000000000000000000004@s254:x.0000000000000000000000000000000000000000000000000000000000000002@s253:x.0000000000000000000000000000000000000000000000000000000000000001@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'rght#5=00000000000000000000000000000009@s255:x.0000000000000000000000000000000000000000000000000000000000000004@s254:x.0000000000000000000000000000000000000000000000000000000000000002@s253:x.0000000000000000000000000000000000000000000000000000000000000001@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'left#5=0000000000000000000000000000000a@s255:x.0000000000000000000000000000000000000000000000000000000000000005@s254:x.0000000000000000000000000000000000000000000000000000000000000002@s253:x.0000000000000000000000000000000000000000000000000000000000000001@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'rght#5=0000000000000000000000000000000b@s255:x.0000000000000000000000000000000000000000000000000000000000000005@s254:x.0000000000000000000000000000000000000000000000000000000000000002@s253:x.0000000000000000000000000000000000000000000000000000000000000001@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'left#5=0000000000000000000000000000000c@s255:x.0000000000000000000000000000000000000000000000000000000000000006@s254:x.0000000000000000000000000000000000000000000000000000000000000003@s253:x.0000000000000000000000000000000000000000000000000000000000000001@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'rght#5=0000000000000000000000000000000d@s255:x.0000000000000000000000000000000000000000000000000000000000000006@s254:x.0000000000000000000000000000000000000000000000000000000000000003@s253:x.0000000000000000000000000000000000000000000000000000000000000001@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'left#5=0000000000000000000000000000000e@s255:x.0000000000000000000000000000000000000000000000000000000000000007@s254:x.0000000000000000000000000000000000000000000000000000000000000003@s253:x.0000000000000000000000000000000000000000000000000000000000000001@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'rght#5=0000000000000000000000000000000f@s255:x.0000000000000000000000000000000000000000000000000000000000000007@s254:x.0000000000000000000000000000000000000000000000000000000000000003@s253:x.0000000000000000000000000000000000000000000000000000000000000001@s252:x.000000000000000000000000000000000000000000000000000000000000000@root', + 'rght#1=empty@root' + ] + assert metrics == \ { 'depthmax': 5, @@ -1336,16 +1338,15 @@ def test_insert(): trace, metrics = siter.trace(terse=False) assert trace == \ [ - 'left#4=6f7d7c15b1ff92fb9b2d67584efc3c93fa106173a08235b06c1f501ab4ff120@0@0@0@root', - 'left#5=b270a16e41491606e21f2b378f5e9655ff519de6d4aaa485b64d762ea3b4507@800000000000000000000000000000000000000000000000000000000000000@0@0@0@root', - 'right#5=d58c15d9f6b72b223981eaa489763340fb16a1431f49235a4dd398ef0e14f73@800000000000000000000000000000000000000000000000000000000000000@0@0@0@root', - 'right#3=26b6db51531728635d6fead71c240f2837a54e05c35af5c7ca046dd267133bb7@0@0@root', - 'left#3=580e09879f2f1533fa84f33e91972597e2140344c6d827fb9b67ad6c505840c8@4000000000000000000000000000000000000000000000000000000000000000@0@root', - 'right#3=72f0d9ba47e94d898c3742f382e33b9bc888b2f9825294b7ac21c8715e16a58f@4000000000000000000000000000000000000000000000000000000000000000@0@root', - 'left#2=cbeffbaddbe2fd5758857e7e2b633596fe9c5c41dae66fad368faba28372a988@c000000000000000000000000000000000000000000000000000000000000000@root', - 'right#2=e9a52caced99580fc4cc7baa8a682ec6c2fc3762e9981634ff560413840bc845@c000000000000000000000000000000000000000000000000000000000000000@root' + 'left#4=6f7d7c15b1ff92fb9b2d67584efc3c93fa106173a08235b06c1f501ab4ff120@s4:b.0000@s2:b.00@s1:b.0@root', + 'left#5=b270a16e41491606e21f2b378f5e9655ff519de6d4aaa485b64d762ea3b4507@s5:b.00001@s4:b.0000@s2:b.00@s1:b.0@root', + 'rght#5=d58c15d9f6b72b223981eaa489763340fb16a1431f49235a4dd398ef0e14f73@s5:b.00001@s4:b.0000@s2:b.00@s1:b.0@root', + 'rght#3=26b6db51531728635d6fead71c240f2837a54e05c35af5c7ca046dd267133bb7@s2:b.00@s1:b.0@root', + 'left#3=580e09879f2f1533fa84f33e91972597e2140344c6d827fb9b67ad6c505840c8@s2:b.01@s1:b.0@root', + 'rght#3=72f0d9ba47e94d898c3742f382e33b9bc888b2f9825294b7ac21c8715e16a58f@s2:b.01@s1:b.0@root', + 'left#2=cbeffbaddbe2fd5758857e7e2b633596fe9c5c41dae66fad368faba28372a988@s2:b.11@root', + 'rght#2=e9a52caced99580fc4cc7baa8a682ec6c2fc3762e9981634ff560413840bc845@s2:b.11@root' ] - assert metrics == \ { 'depthmax': 5, From f4b32a0fdd2fec8d591080ba15d46fea80f8068b Mon Sep 17 00:00:00 2001 From: Samuel M Smith Date: Tue, 24 Nov 2026 10:58:20 -0700 Subject: [PATCH 2/2] removed notes.md as obsolete updates test --- src/keri/sit/notes.md | 350 --------------------------------------- tests/sit/test_siting.py | 10 +- 2 files changed, 5 insertions(+), 355 deletions(-) delete mode 100644 src/keri/sit/notes.md diff --git a/src/keri/sit/notes.md b/src/keri/sit/notes.md deleted file mode 100644 index ff51b82f1..000000000 --- a/src/keri/sit/notes.md +++ /dev/null @@ -1,350 +0,0 @@ -Background notes on SIT - -OC-SMT core functional support - -Notes: - -The entry index for a key is calculated by taking the Hash of the key and -treating that as a 256 bit big endian integer. The value for that key is stored -in the leaf node indexed by this entry index number. This index is also the -path down the tree from the root. Where each fork starting at msb 0 is left -1 is right. so a tree with 4 leaves has a 2 bit index or path with the path values. - -00, 01, 10, 11 -there are three forks, upper fork at msb and two lower forks each at lsb - - top (upper fork) - 0 1 (lower forks) -0 1 0 1 - -So the tree stores a leaf hash at location = index = path. -then each of the lower fork stores the hash of the hash of its concatenated children -where concat order is left | right -thenb upper fork stores hash of its children which are the two lower forks -also in concat left|righ order - -The leaf hash enables a Merkle proof of inclusion where one only needs to -disclose the leaf hash to prove the hashed value is included if one has the -hashed value. Technically for a proof of inclusion the path itself is not needed -for the proof just the sibling hashes along the path. - -Addressing of the value uses the index=path so in order to do content addressing -using the merkle tree where the index is bound to the merkle proof then we -consider calculating the index (calculated) as the hash of the indexed value. - -We can store the leaf values in a key value store and then use the hash of -the key (index) as the leaf hash. This proves the key in included but not -the value. Its only when the leaf hash includes the stored value that we have -a crypto binding of inclusion of the value itself in the tree. - -The leaf hash can be different from index as content address hash. The leaf hash -has to include the value or a committment to the value for crypto proof of inclusion -but a content address computed from the value for the index (path) can be different -from the hash for the leaf. Both have to include the value in the hash but both -can include other stuff so they don't have to be equal. - -One trick, is when the leaf hash and index are different computations that -the leaf hash include the index. This forces each leaf hash to be unique and bound -to the location of the leaf hash. This prevents a collision where the same -value could stored in different places in the tree and therefore allow a different -tree to give a valid inclusion proof for the same value. - -If the index is the leaf hash then I believe that you also have a collision proof -since there is no way to construct a different tree that hashes the same since -the leave hash is the index so it can only appear in one place in the tree. -So merging up with null leaves is unique. - -When the leaf hash and index are not the same hash that one has to worry about -constructing fake tree because the same value could be stored at different keys. - -For a tree with a path of 256 bits the total number of leaves is 2^256. The total -humber of nodes is almost double that. The nodes in a full tree of height 256 -is 2^(257) -1 In other words the number internal nodes is equal to the number -of leaf nodes - 1. - -So using the exact same hash format for leaf values as internal node hashes -means that a full tree must have collisions, but this would never happen in -a practical application so is not a viable risk. - -A real risk on a sparse tree are attacks where internal nodes with same hash format -as leaf hashes are used to create an alternate tree with same root hash. -So the protection is called domain separation. - -------------------- - -A leaf value is hashed to provide the leaf hash at the location in the tree -given by its index. - -(Zero is a special unique value that should not correspond to a real leaf's value. -(Can we use None in python) In Ethereum world they use the string `0x00000...` - -The docs for merkle trees get this mixed up. Sometimes when they say 0 they -mean literally a 0 bit but other times they mean a special hash value. - -key in sparse merkle tree is the concatenation of the path. A key of 256 bits -is a left-right forking path down tree. -If a bit position is 0 go left else 1 go right -There are 2^256 keys value (0...0) to (1...1) - -In a binary Merkle tree, a 0 typically represents the left child branch and -a 1 represents the right child branch when navigating from the root down toward a leaf. -• 0 = Left Branch: Moving down to the left child node in the pair. -• 1 = Right Branch: Moving down to the right child node in the pair. - -How Branch Direction Works - -• Path Navigation: When walking from the root to a leaf, each step in the path -or Merkle proof is designated by a bit (such as 0 or 1) so the verifier knows -whether the sibling hash should be concatenated on the left or on the right -during hash reconstruction. -• Concatenation Order: Parent hashes are computed as H(left || right), -meaning the position (left vs. right) dictates the sequence in which hashes are joined together. -• Bitwise Indexing: In sparse or binary-indexed structures, binary path digits -correspond directly to choosing the left (0) or right (1) branch at each depth level of the tree. - - -Nervos -Optimization 1: Zero-value optimized hash function -We define the node merging function as following: -if L == 0, return R -if R == 0, return L -otherwise return sha256(L, R) - -With optimization 1, we do not need the pre-calculated hash-set. -For a default SMT, all the intermediate nodes are zero values. -Thus, we only need to calculate the hash for non-zero nodes and return zero -for the default nodes. - - - -There’s only one issue that remains. The optimized hash function produces -the same value from different key-value pairs, for example: -merge(N, 0) == merge(0, N). -This behavior opens a weak point for the SMT. An attacker may construct a -collision of merkle root from a faked key-value map. - -To fix this, we use the result of hash(key | value) as a leaf’s hash. -For example: -merge(N, leaf_hash(N, 0)) != merge(0, leaf_hash(0, N)) -because the leaf_hash(N, 0) is never equals to leaf_hash(0, N) if N != 0, -the attacker can’t construct a collision attacking. - -Additionally, we store leaf_hash -> value in a map to keep the reference -to the original value. - -We can prove the security of this construction. - -Since the key is included in the leaf’s hash, and key is unique in SMT, -no matter what the value is, the leaf_hash(key, value) is unique. -Each node is either merged by two different hashes or merged by a hash with -a zero-value. We already knew that all leaves have a unique hash, so their -parent nodes also have a unique hash, and so on. -The parent of these parent nodes also has a unique hash until the root of the tree. -We got zero value merkle root if the tree is empty. -Otherwise, the root is merged from its two children nodes. Any changes in the -leaves will also change the root hash. -So we believe this construction is security because we can’t construct -a collision of merkle root. - - -Optimization 2: abstract storage mechanics - -In the classic construction of SMT: We store a branch node with its two children -as -Node {left, right}. - -We have implemented branch node as the following data structure: - -pub struct BranchNode -{ - pub left: MergeValue, - pub right: MergeValue, -} -When left and right are both zero, it's not needed to store this zero node. - -We still face problem: too many branch nodes in real world. -Assuming we have only one leaf node in SMT, there are still 255 branch nodes. -They are all ancestors of the only leaf node. -The amount of branch nodes can be easily reached to millions. -When the leaf nodes grows to above 10k, it can consume a lot of memory. -We realize that the databases or kv-store will be used in SMT proof. -Therefore, we introduce a abstracted layer of storage mechanics, trait Store as following: - -/// Trait for customize backend storage -pub trait Store -{ - fn get_branch(&self, branch_key: &BranchKey) -> Result, Error>; - fn get_leaf(&self, leaf_key: &H256) -> Result, Error>; - fn insert_branch(&mut self, node_key: BranchKey, branch: BranchNode) -> Result<(), Error>; - fn insert_leaf(&mut self, leaf_key: H256, leaf: V) -> Result<(), Error>; - fn remove_branch(&mut self, node_key: &BranchKey) -> Result<(), Error>; - fn remove_leaf(&mut self, leaf_key: &H256) -> Result<(), Error>; -} - -If branch nodes are not many, we can put them all in memory. -By default, we have such DefaultStore: - -pub struct DefaultStore -{ - branches_map: Map, - leaves_map: Map, -} - -If branch nodes are too many, it's better to implement a database backend. -We suggest RocksDB: an embedded persistent key-value store for fast storage. -Here is an example of RocksDB backend implementation. -https://github.com/quake/smt-rocksdb-store - -This store mechanics is only used in making SMT proof. -It doesn't affect on-chain SMT verification which is still very fast and memory-efficient. - -Optimization 3: Shortcut nodes - -We can optimize the storage space and performance by using shortcut nodes. -A shortcut is a node that has only one leaf node, it can be merged with its parent node. -The shortcut node is only used in the storage, it doesn't affect the verification process. -We implement it as following algorithm: Aergo Trie. -https://github.com/aergoio/aergo/tree/master/pkg/trie -This algorithm is enabled by trie feature, which is disabled by default. - - - -Rust code - -fn common_height(key1, key2) -{ - for i in 255..0 - { - if key1.get_bit(i) != key2.get_bit(i) - { - // common height - return i; - } - } - return 0; -} - -// get children at height -// return value is (left, right) -fn children(branch_node, height) -{ - let is_rhs = branch_node.key.get_bit(height); - if is_rhs - { - return (branch_node.sibling, branch_node.node) - - } - else - { - return (branch_node.node, branch_node.sibling) - } -} - -fn get(key) -{ - - let node = root; - - // path order by height - - let path = BTreeMap::new(); - - loop - { - let branch_node = match map.get(node) - { - Some(b) => b, - None => break, - } - // common height may be lower than node.fork_height - let height = max(common_height(key, node.key), node.fork_height); - if height > node.fork_height - { - // node is sibling, end search - path.push(heignt, node); - break; - } - - // node is parent - // extract children position from branch - let (left, right) = children(branch_node, height); - - // extract key positon - let is_right = key.get_bit(height); - if is_right - { - path.push(height, left); - node = right; - } - else - { - path.push(height, right); - node = left; - } - } - return self.leaves[node]; -} - -use sparse_merkle_tree:: -{ - blake2b::Blake2bHasher, default_store::DefaultStore, - error::Error, MerkleProof, - SparseMerkleTree, traits::Value, H256 -}; -use blake2b_rs::{Blake2b, Blake2bBuilder}; - -// define SMT -type SMT = SparseMerkleTree>; - -// define SMT value -#[derive(Default, Clone)] -pub struct Word(String); -impl Value for Word { - fn to_h256(&self) -> H256 - { - if self.0.is_empty() - { - return H256::zero(); - } - let mut buf = [0u8; 32]; - let mut hasher = new_blake2b(); - hasher.update(self.0.as_bytes()); - hasher.finalize(&mut buf); - buf.into() - } - fn zero() -> Self - { - Default::default() - } -} - -// helper function -fn new_blake2b() -> Blake2b -{ - Blake2bBuilder::new(32).personal(b"SMT").build() -} - -fn construct_smt() -{ - let mut tree = SMT::default(); - for (i, word) in "The quick brown fox jumps over the lazy dog" - .split_whitespace() - .enumerate() - { - let key: H256 = - { - let mut buf = [0u8; 32]; - let mut hasher = new_blake2b(); - hasher.update(&(i as u32).to_le_bytes()); - hasher.finalize(&mut buf); - buf.into() - }; - let value = Word(word.to_string()); - // insert key value into tree - tree.update(key, value).expect("update"); - } - - - println!("SMT root is {:?} ", tree.root()); -} - diff --git a/tests/sit/test_siting.py b/tests/sit/test_siting.py index a8215fe41..bb4ffa9a0 100644 --- a/tests/sit/test_siting.py +++ b/tests/sit/test_siting.py @@ -1481,15 +1481,15 @@ def test_delete(): leaf = siter.insert(leaf=leaf5.part.limb) rootC = siter.root - leaves, metrics = siter.trace() - assert leaves == \ + trace, metrics = siter.trace() + assert trace == \ [ 'left#2=00100000@0@root', 'left#3=01000000@01@0@root', - 'right#3=01100000@01@0@root', + 'rght#3=01100000@01@0@root', 'left#3=10000000@10@1@root', - 'right#3=10100000@10@1@root', - 'right#2=11000000@1@root' + 'rght#3=10100000@10@1@root', + 'rght#2=11000000@1@root' ] leaf = siter.delete(leaf=leaf5.part.limb)