예제 #1
0
def test_node_UndecodableRecord(byte, machine):
    with pytest.raises(UndecodableRecord):
        left = Leaf(record=byte,
                    encoding=machine.encoding,
                    hash_func=machine.hash)
        _right = Leaf(record=byte,
                      encoding=machine.encoding,
                      hash_func=machine.hash)
        with pytest.raises(UndecodableRecord):
            Node(left=left,
                 right=_right,
                 encoding=machine.encoding,
                 hash_func=machine.hash)
예제 #2
0
def test_hash_recalculation_UndecodableRecord(byte, machine):
    with pytest.raises(UndecodableRecord):
        left = Leaf(record='left record',
                    encoding=machine.encoding,
                    hash_func=machine.hash)
        right = Leaf(record='right record',
                     encoding=machine.encoding,
                     hash_func=machine.hash)
        node = Node(left=left,
                    right=right,
                    encoding=machine.encoding,
                    hash_func=machine.hash)
        left = Leaf(record=byte,
                    encoding=machine.encoding,
                    hash_func=machine.hash)
        node.set_left(_left)
        node.recalculate_hash(machine.hash)
예제 #3
0
def test_leaf_construction_exception_with_both_record_and_digest():
    """
    Tests `TypeError` if both `record` and `digest` are provided
    """
    with pytest.raises(LeafConstructionError):
        Leaf(hash_func=hash_func,
             encoding=encoding,
             record=b'anything...',
             digest=hash_func('whatever...'))
예제 #4
0
def test_hash_recalculation():
    new_leaf = Leaf(hash_func=hash_func,
                    encoding=encoding,
                    record=b'new record...')
    node_34.set_right(new_leaf)
    node_34.recalculate_hash(hash_func=hash_func)
    root.recalculate_hash(hash_func=hash_func)
    assert node_34.digest == hash_func(leaf_3.digest, new_leaf.digest) \
        and root.digest == hash_func(node_12.digest, node_34.digest)
예제 #5
0
"""
Tests represenation and serialization of nodes
"""

import pytest

from pymerkle.core.nodes import Node, Leaf
from pymerkle.hashing import HashMachine

_ = HashMachine()
encoding = _.encoding
hash_func = _.hash

pair_of_leaves = (
    Leaf(hash_func=hash_func, encoding=encoding, record=b'some record...'),
    Leaf(hash_func=hash_func,
         encoding=encoding,
         digest=
         '5f4e54b52702884b03c21efc76b7433607fa3b35343b9fd322521c9c1ed633b4'))

# Full binary structure (child-parent relations): 4 leaves, 7 nodes in total
leaf_1 = Leaf(hash_func=hash_func,
              encoding=encoding,
              record=b'first record...')
leaf_2 = Leaf(hash_func=hash_func,
              encoding=encoding,
              record=b'second record...')
leaf_3 = Leaf(hash_func=hash_func,
              encoding=encoding,
              record=b'third record...')
leaf_4 = Leaf(hash_func=hash_func,
예제 #6
0
def test_leaf_construction_exception_with_neither_record_nor_digest():
    """
    Tests `TypeError` if neither `record` nor `digest` is provided
    """
    with pytest.raises(LeafConstructionError):
        Leaf(hash_func=hash_func, encoding=encoding)
예제 #7
0
def test_leaf_UndecodableRecord(byte, machine):
    with pytest.raises(UndecodableRecord):
        Leaf(record=byte, encoding=machine.encoding, hash_func=machine.hash)
예제 #8
0
def attribute_access_benchmark():
    from timeit import repeat

    def access_digest_func(node):
        def access_attribute():
            node.digest

        return access_attribute

    def access_left_parent_func(node):
        def access_attribute():
            node.left

        return access_attribute

    def access_right_parent_func(node):
        def access_attribute():
            node.right

        return access_attribute

    def access_child_func(node):
        def access_attribute():
            node.child

        return access_attribute

    left = Leaf(hash_func=HASH, encoding=ENCODING, record='first record...')
    right = Leaf(hash_func=HASH, encoding=ENCODING, record='second record...')
    node = Node(hash_func=HASH, encoding=ENCODING, left=left, right=right)

    write(
        '\n\n-------------------------- Nodes\' attributes access --------------------------'
    )
    write('\n')
    write('\nConfiguration')
    write('\n')
    write('\nNumber of rounds     : %d' % ROUNDS)
    write('\nNumber of iterations : %d' % ITERATIONS)

    # Digest access
    access_digest_measurements = repeat(access_digest_func(left),
                                        repeat=ITERATIONS,
                                        number=ROUNDS)
    mean, stdev = mean_value(access_digest_measurements,
                             PRECISION_1,
                             with_stdev=True)
    show_stats('Time needed to access digest (sec)', mean, stdev)

    # Left parent access
    access_left_parent_measurements = repeat(access_left_parent_func(node),
                                             repeat=ITERATIONS,
                                             number=ROUNDS)
    mean, stdev = mean_value(access_left_parent_measurements,
                             PRECISION_1,
                             with_stdev=True)
    show_stats('Time needed to access left parent (sec)', mean, stdev)

    # Right parent access
    access_right_parent_measurements = repeat(access_right_parent_func(node),
                                              repeat=ITERATIONS,
                                              number=ROUNDS)
    mean, stdev = mean_value(access_right_parent_measurements,
                             PRECISION_1,
                             with_stdev=True)
    show_stats('Time needed to access right parent (sec)', mean, stdev)

    # Child access
    access_child_measurements = repeat(access_child_func(right),
                                       repeat=ITERATIONS,
                                       number=ROUNDS)
    mean, stdev = mean_value(access_child_measurements,
                             PRECISION_1,
                             with_stdev=True)
    show_stats('Time needed to access child (sec)', mean, stdev)