Skip to content

Commit

Permalink
Cleaned up code, added tests and GitHub actions and support storing M…
Browse files Browse the repository at this point in the history
…erkle Tree artifacts in LMDB datastore.
  • Loading branch information
pfeairheller committed Jul 18, 2024
1 parent 36220e1 commit 5f1813f
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 306 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/python-app-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions


name: GitHub Actions for keripy
on:
push:
branches:
- 'main'
pull_request:
workflow_dispatch:

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ macos-13, ubuntu-latest ]

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.12.2
uses: actions/setup-python@v2
with:
python-version: 3.12.2
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint changes
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --ignore=E7,F841,E301,E302,E303 --max-complexity=10 --max-line-length=127 --statistics
- name: Run core KERKLE tests
run: |
pytest tests/
8 changes: 1 addition & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,7 @@
'keri>=1.2.0-dev10',
'lmdb>=1.4.1',
'pysodium>=0.7.17',
'blake3>=0.4.1',
'msgpack>=1.0.8',
'cbor2>=5.6.2',
'multidict>=6.0.5',
'ordered-set>=4.1.0',
'hio>=0.6.13',
'cryptography>=42.0.5'
'blake3>=0.4.1'
],
extras_require={
},
Expand Down
16 changes: 8 additions & 8 deletions src/kerkle/core/helping.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
DEFAULTVALUE = b""


def create_leaf(path, leaf_data, code=core.MtrDex.SHA3_256):
value = b"".join([LEAF, path, leaf_data])
hash = digest(value, code=code)
return (hash, value)
def create_leaf(path, code=core.MtrDex.SHA3_256):
value = b"".join([LEAF, path])
dig = digest(value, code=code)
return dig, value


def parse_leaf(data):
return (data[1:33], data[33:])
return data[1:33]


def is_leaf(data):
Expand All @@ -33,12 +33,12 @@ def is_leaf(data):

def create_node(left, right, code=core.MtrDex.SHA3_256):
value = b"".join([NODE, left, right])
hash = digest(value, code=code)
return hash, value
dig = digest(value, code=code)
return dig, value


def parse_node(data):
return (data[1:33], data[33:])
return data[1:33], data[33:]


def digest(data, code=core.MtrDex.Blake3_256):
Expand Down
49 changes: 29 additions & 20 deletions src/kerkle/core/proofing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
kerkle.proofing module
"""

from keri import core

from .helping import DEPTH, KEYSIZE, create_leaf, digest, LEAF, parse_leaf, get_bit, create_node, DEFAULTVALUE, \
PLACEHOLDER, RIGHT
from .helping import DEPTH, KEYSIZE, create_leaf, digest, LEAF, get_bit, create_node, RIGHT


class SparseMerkleProof:
def __init__(self, sidenodes, non_membership_leafdata, siblingdata, code=core.MtrDex.SHA3_256):
def __init__(self, root, value, sidenodes, non_membership_leafdata, siblingdata):
self.root = root
self.value = value
self.sidenodes = sidenodes
self.non_membership_leafdata = non_membership_leafdata
self.sibling_data = siblingdata
self.code = code

def sanity_check(self):
if (
Expand All @@ -30,32 +31,40 @@ def sanity_check(self):
return False

if self.sibling_data:
sibhash = digest(self.sibling_data, code=self.code)
sibhash = digest(self.sibling_data, code=self.root.code)
if self.sidenodes and len(self.sidenodes) > 0:
if self.sidenodes[0] != sibhash:
return False
return True

@property
def pod(self):
return dict(
root=self.root.qb64,
value=self.value.qb64,
side_nodes=[core.Matter(raw=raw, code=self.root.code).qb64 for raw in self.sidenodes or []],
non_membership_leafdata=[core.Matter(raw=raw, code=self.root.code).qb64 for raw in
self.non_membership_leafdata or []],
sibling_data=[core.Matter(raw=raw, code=self.root.code).qb64 for raw in self.sibling_data or []],
)


def verify_proof(proof, root, key, value, code=core.MtrDex.SHA3_256):
path = digest(key, code=code)
def verify_proof(proof, root, saider):
path = saider.raw

if not proof.sanity_check():
return False

current_hash = None

if value == DEFAULTVALUE:
if not proof.non_membership_leafdata:
current_hash = PLACEHOLDER
else:
actual_path, value_hash = parse_leaf(proof.non_membership_leafdata)
if actual_path == path:
return False
current_hash, _current_data = create_leaf(actual_path, value_hash)
else:
value_hash = digest(value, code=code)
current_hash, _current_data = create_leaf(path, value_hash)
# if inverse:
# if not proof.non_membership_leafdata:
# current_hash = PLACEHOLDER
# else:
# actual_path, value_hash = parse_leaf(proof.non_membership_leafdata)
# if actual_path == path:
# return False
# current_hash, _current_data = create_leaf(actual_path, value_hash)
# else:
current_hash, _current_data = create_leaf(path)

for i, node in enumerate(proof.sidenodes):
if get_bit(len(proof.sidenodes) - 1 - i, path) == RIGHT:
Expand Down
Loading

0 comments on commit 5f1813f

Please sign in to comment.