Skip to main content

A Python implementation of Merkle Tree

Project description

pymerkle

Merkle-tree cryptography

Build Status codecov Docs Status PyPI version Python >= 3.6

Documentation found at pymerkle.readthedocs.org.

This library provides a Merkle-tree implementation in Python. It supports multiple combinations of hash functions and encding schemas with defense against second-preimage attack enabled.

Install

pip3 install pymerkle

Usage

from pymerkle import MerkleTree, verify_inclusion, verify_consistency

tree = MerkleTree()

# Populate tree with some entries
for data in [b'foo', b'bar', b'baz', b'qux', b'quux']:
    tree.append_entry(data)

# Prove and verify inclusion of `bar`
proof = tree.prove_inclusion(b'bar')
verify_inclusion(b'bar', tree.root, proof)

# Save current state
sublength = tree.length
subroot = tree.root

# Append further entries
for data in [b'corge', b'grault', b'garlpy']:
    tree.append_entry(data)

# Prove and verify previous state
proof = tree.prove_consistency(sublength, subroot)
verify_consistency(subroot, tree.root, proof)

Security

This is currently a prototype requiring security review. However, some steps have been made to this direction:

Defense against second-preimage attack

This consists in the following standard technique:

  • Upon computing the hash of a leaf, prepend its data with 0x00.
  • Upon computing the hash of an interior node, prepend the hashes of its children with 0x01.

Refer here to see how to perform second preimage attack against the present implementation.

Defense against CVE-2012-2459 DOS

Contrary to the bitcoin specification for Merkle-trees, lonely leaves are not duplicated while the tree is growing. Instead, when appending new leaves, a bifurcation node is created at the rightmost branch. As a consequence, the present implementation should be invulnerable to the DOS attack reported as CVE-2012-2459 (see also here for explanation).

Tree structure

The topology turns out to be that of a binary Sakura tree.

Development

pip3 install -r requirements-dev.txt

Tests

./test.sh [pytest options]

to run tests against the limited set of encoding schemas UTF-8, UTF-16 and UTF-32. To run tests against all possible combinations of hash algorithms and encoding schemas, run

./test.sh --extended

Documentation

pymerkle.readthedocs.org.

Build locally

Documentation is built with sphinx:

pip3 install -r requirements-doc.txt

Once installed, build docs with

./build-docs.sh [--help]

and browse at

docs/target/build/html/index.html

to view them.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymerkle_logstransparentes-0.0.3.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymerkle_logstransparentes-0.0.3-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file pymerkle_logstransparentes-0.0.3.tar.gz.

File metadata

File hashes

Hashes for pymerkle_logstransparentes-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e9cc84c17f12515ff3bfd2934c8070c6263d820429e9344622b540e042035f4b
MD5 43bb86fb995035f5a88c0dc14ce5f139
BLAKE2b-256 c9bd94518c1a46497b35f7e31e8b3e4c46167175caec2532c281ddce34a42f74

See more details on using hashes here.

File details

Details for the file pymerkle_logstransparentes-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pymerkle_logstransparentes-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 66816c7d161487f9a14626eec8ad4e40ff5961829c3a941c4efd8e98a37c9096
MD5 62a634fd5060a841aaac895aa35cc8b6
BLAKE2b-256 a3e881ad7f0b1bec0e5aa4fd5723816243b9cdb47c30403a11606fd7cb727d1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page