Beispiel #1
0
def compute_layers(elements):
    """ Computes the layers of the merkletree.

    First layer is the list of elements and the last layer is a list with a
    single entry, the merkleroot.
    """

    elements = list(elements)  # consume generators
    assert elements, 'Use EMPTY_MERKLE_TREE if there are no elements'

    if not all(isinstance(item, bytes) for item in elements):
        raise ValueError('all elements must be bytes')

    if any(len(item) != 32 for item in elements):
        raise HashLengthNot32()

    if len(elements) != len(set(elements)):
        raise ValueError('Duplicated element')

    leaves = sorted(item for item in elements)
    tree = [leaves]

    layer = leaves
    while len(layer) > 1:
        paired_items = split_in_pairs(layer)
        layer = [hash_pair(a, b) for a, b in paired_items]
        tree.append(layer)

    return tree
Beispiel #2
0
def compute_layers(elements: List[Keccak256]) -> List[List[Keccak256]]:
    """ Computes the layers of the merkletree.

    First layer is the list of elements and the last layer is a list with a
    single entry, the merkleroot.
    """

    elements = list(elements)  # consume generators
    assert elements, "Use make_empty_merkle_tree if there are no elements"

    if not all(isinstance(item, bytes) for item in elements):
        raise ValueError("all elements must be bytes")

    if any(len(item) != 32 for item in elements):
        raise HashLengthNot32()

    if len(elements) != len(set(elements)):
        raise ValueError("Duplicated element")

    leaves = sorted(item for item in elements)
    tree = [leaves]

    layer = leaves
    while len(layer) > 1:
        paired_items = split_in_pairs(layer)
        layer = [hash_pair(a, b) for a, b in paired_items]
        tree.append(layer)

    return tree
Beispiel #3
0
def compute_layers(elements):
    """ Computes the layers of the merkletree.

    First layer is the list of elements and the last layer is a list with a
    single entry, the merkleroot.
    """

    elements = list(elements)  # consume generators
    assert elements, 'Use EMPTY_MERKLE_TREE if there are no elements'

    if not all(isinstance(item, (str, bytes)) for item in elements):
        raise ValueError('all elements must be str')

    if any(len(item) != 32 for item in elements):
        raise HashLengthNot32()

    if len(elements) != len(set(elements)):
        raise ValueError('Duplicated element')

    leaves = sorted(item for item in elements)
    tree = [leaves]

    layer = leaves
    while len(layer) > 1:
        paired_items = split_in_pairs(layer)
        layer = [hash_pair(a, b) for a, b in paired_items]
        tree.append(layer)

    return tree