Example #1
0
    def test_hash(self) -> None:
        """Tests the Hash class."""
        raw_hash = bytes([0xAB for _ in range(_HASH_BYTES_LEN)])
        hash_obj = Hash(raw_hash)

        self.assertTrue(isinstance(hash_obj, _FixedByteArray))

        self.assertEqual(hash_obj.value, raw_hash)
        self.assertEqual(hash_obj.hex(), raw_hash.hex())
        self.assertEqual(Hash.hash_data(bytes()),
                         Hash(crypto_hash_sha256(bytes())))
        self.assertEqual(Hash.hash_data(bytes([1, 2])),
                         Hash(crypto_hash_sha256(bytes([1, 2]))))
Example #2
0
    def hash_node(left: Hash, right: Hash) -> Hash:
        """ Convenience method to obtain a hashed value of the merkle tree node. """

        data = struct.pack(
            "<B", Hasher.HashTag.LIST_BRANCH_NODE) + left.value + right.value

        return Hash.hash_data(data)
Example #3
0
    def hash_single_entry_map(path: bytes, child_hash: Hash) -> Hash:
        """
        Hash of the map with single entry.
        ``` text
        h = sha-256( HashTag::MapBranchNode || <key> || <child_hash> )
        ```
        """
        data = struct.pack("<B", Hasher.HashTag.MAP_BRANCH_NODE) + path + child_hash.value

        return Hash.hash_data(data)
Example #4
0
    def hash_map_branch(branch_node: bytes) -> Hash:
        """
        Hash of the map branch node.
        ```text
        h = sha-256( HashTag::MapBranchNode || <left_key> || <right_key> || <left_hash> || <right_hash> )
        ```
        """
        data = struct.pack("<B", Hasher.HashTag.MAP_BRANCH_NODE) + branch_node

        return Hash.hash_data(data)
Example #5
0
    def hash_map_node(root: Hash) -> Hash:
        """
        Hash of the map object.
        ```text
        h = sha-256( HashTag::MapNode || merkle_root )
        ```
        """
        data = struct.pack("<B", Hasher.HashTag.MAP_NODE) + root.value

        return Hash.hash_data(data)
Example #6
0
    def hash_list_node(length: int, merkle_root: Hash) -> Hash:
        """
        Hash of the list object.
        ```text
        h = sha-256( HashTag::ListNode || len as u64 || merkle_root )
        ```
        """
        data = struct.pack("<BQ", Hasher.HashTag.LIST_NODE, length) + merkle_root.value

        return Hash.hash_data(data)
Example #7
0
    def hash_leaf(val: bytes) -> Hash:
        """ Convenience method to obtain a hashed value of the merkle tree leaf. """

        data = struct.pack("<B", Hasher.HashTag.BLOB) + val

        return Hash.hash_data(data)
Example #8
0
    def hash_raw_data(data: bytes) -> Hash:
        """ SHA256 hash of the provided data. """

        return Hash.hash_data(data)