Ejemplo n.º 1
0
    def getNodePosition(cls, start, height=None) -> int:
        """
        Calculates node position based on start and height

        :param start: The sequence number of the first leaf under this tree.
        :param height: Height of this node in the merkle tree
        :return: the node's position
        """
        pwr = highest_bit_set(start) - 1
        height = height or pwr
        if count_bits_set(start) == 1:
            adj = height - pwr
            return start - 1 + adj
        else:
            c = pow(2, pwr)
            return cls.getNodePosition(c, pwr) + \
                cls.getNodePosition(start - c, height)
Ejemplo n.º 2
0
    def getNodePosition(cls, start, height=None) -> int:
        """
        Calculates node position based on start and height

        :param start: The sequence number of the first leaf under this tree.
        :param height: Height of this node in the merkle tree
        :return: the node's position
        """
        pwr = highest_bit_set(start) - 1
        height = height or pwr
        if count_bits_set(start) == 1:
            adj = height - pwr
            return start - 1 + adj
        else:
            c = pow(2, pwr)
            return cls.getNodePosition(c, pwr) + \
                cls.getNodePosition(start - c, height)
Ejemplo n.º 3
0
    def getPath(cls, seqNo, offset=0):
        """
        Get the audit path of the leaf at the position specified by serNo.

        :param seqNo: sequence number of the leaf to calculate the path for
        :param offset: the sequence number of the node from where the path
         should begin.
        :return: tuple of leafs and nodes
        """
        if offset >= seqNo:
            raise ValueError("Offset should be less than serial number")
        pwr = highest_bit_set(seqNo - 1 - offset) - 1
        if pwr <= 0:
            if seqNo % 2 == 0:
                return [seqNo - 1], []
            else:
                return [], []
        c = pow(2, pwr) + offset
        leafs, nodes = cls.getPath(seqNo, c)
        nodes.append(cls.getNodePosition(c, pwr))
        return leafs, nodes
Ejemplo n.º 4
0
    def getPath(cls, seqNo, offset=0):
        """
        Get the audit path of the leaf at the position specified by serNo.

        :param seqNo: sequence number of the leaf to calculate the path for
        :param offset: the sequence number of the node from where the path
         should begin.
        :return: tuple of leafs and nodes
        """
        if offset >= seqNo:
            raise ValueError("Offset should be less than serial number")
        pwr = highest_bit_set(seqNo - 1 - offset) - 1
        if pwr <= 0:
            if seqNo % 2 == 0:
                return [seqNo - 1], []
            else:
                return [], []
        c = pow(2, pwr) + offset
        leafs, nodes = cls.getPath(seqNo, c)
        nodes.append(cls.getNodePosition(c, pwr))
        return leafs, nodes