Skip to content

Commit

Permalink
Merge pull request #7 from kcalvinalvin/2024-08-21-verify-dont-return…
Browse files Browse the repository at this point in the history
…-indexes

change verify so it doesn't return indexes
  • Loading branch information
kcalvinalvin authored Aug 25, 2024
2 parents 2380e01 + 033806d commit 6454c69
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
46 changes: 31 additions & 15 deletions pytreexo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,51 @@ def add(self, adds: [bytes]):
self.roots.append(add)
self.numleaves += 1

def verify(self, dels: [bytes], proof: Proof) -> [int]:
def verify(self, dels: [bytes], proof: Proof):
if len(dels) != len(proof.targets):
raise("len of dels and proof.targets differ")

root_candidates = calculate_roots(self.numleaves, dels, proof)
root_idxs = getrootidxs(self.numleaves, proof.targets)

root_idxs = []
for i in range(len(self.roots)):
j = len(self.roots) - (i+1)
if len(root_candidates) > len(root_idxs):
if self.roots[j] == root_candidates[len(root_idxs)]:
root_idxs.append(j)
if len(root_candidates) != len(root_idxs):
raise("length of calculated roots from the proof and expected root count differ")

for i, idx in enumerate(root_idxs):
if self.roots[idx] != root_candidates[i]:
raise("calculated roots from the proof and matched roots differ")

if len(root_idxs) != len(root_candidates):
raise("calculated roots from the proof and matched roots differ")

return root_idxs

def delete(self, dels: [bytes], proof: Proof):
dels_copy = dels.copy()
proof_copy = copy.copy(proof)
root_idxs = self.verify(dels_copy, proof_copy)

def delete(self, proof: Proof):
modified_roots = calculate_roots(self.numleaves, None, proof)

root_idxs = getrootidxs(self.numleaves, proof.targets)
for i, idx in enumerate(root_idxs):
self.roots[idx] = modified_roots[i]


def getrootidxs(numleaves: int, positions: [int]) -> [int]:
indexes = set()
for pos in positions:
idx = root_idx(numleaves, pos)
indexes.add(idx) if idx is not None else None

return sorted(list(indexes), reverse=True)


def root_idx(numleaves: int, position: int) -> int:
idx = 0
for row in range(tree_rows(numleaves), -1, -1):
if numleaves&(1<<row) == 0:
continue
pos = position
for _ in range(row): pos = parent(pos, tree_rows(numleaves))
if isroot(pos, numleaves, tree_rows(numleaves)):
return idx
idx += 1


def parent_hash(left, right):
if left is None: return right
if right is None: return left
Expand Down
2 changes: 1 addition & 1 deletion tests/test_stump.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_delete(self):

proofhashes = [bytes.fromhex(proof_str) for proof_str in test['proofhashes']]
proof = pytreexo.Proof(test['target_values'], proofhashes)
s.delete(del_hashes, proof)
s.delete(proof)

for i, expected_str in enumerate(test['expected_roots']):
root = s.roots[i]
Expand Down

0 comments on commit 6454c69

Please sign in to comment.