How to use the mythril.laser.smt.Concat function in mythril

To help you get started, we’ve selected a few mythril examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ConsenSys / mythril / tests / laser / smt / concat_extract_tests / concat_extract_assignment.py View on Github external
def test_concat_extract_input_assignment():
    inp1 = symbol_factory.BitVecSym("input1", 256)
    inp2 = symbol_factory.BitVecSym("input2", 256)
    output1 = symbol_factory.BitVecFuncSym(
        "Keccak[input]", size=256, func_name="keccak256", input_=Concat(inp1, inp2)
    )
    inp3 = Concat(inp2, inp1)
    cond = And(output1 == inp2, inp1 == inp2)
    Extract(511, 256, inp3).potential_input_cond = cond

    assert Extract(511, 256, inp3).potential_input_cond == cond
github ConsenSys / mythril / tests / laser / smt / concat_extract_tests / concat_extract_assignment.py View on Github external
def test_concat_extract_assignment():
    inp1 = symbol_factory.BitVecSym("input1", 256)
    inp2 = symbol_factory.BitVecSym("input2", 256)
    output1 = symbol_factory.BitVecFuncSym(
        "Keccak[input]", size=256, func_name="keccak256", input_=Concat(inp1, inp2)
    )
    output = Concat(output1, symbol_factory.BitVecVal(0, 256))
    cond = And(output1 == inp2, inp1 == inp2)
    Extract(511, 256, output).potential_input_cond = cond

    assert Extract(511, 256, output).potential_input_cond == cond
github ConsenSys / mythril / tests / laser / smt / concat_extract_tests / concat_extract_assignment.py View on Github external
def test_concat_extract_assignment_nested():
    inp1 = symbol_factory.BitVecSym("input1", 256)
    o1 = symbol_factory.BitVecFuncSym(
        "Keccak[inp1]",
        size=256,
        func_name="keccak256",
        input_=Concat(inp1, symbol_factory.BitVecVal(0, 256)),
    )
    output1 = symbol_factory.BitVecFuncSym(
        "Keccak[Concat(o1, 0)]",
        size=256,
        func_name="keccak256",
        input_=Concat(o1, symbol_factory.BitVecVal(0, 256)),
    )
    cond = And(output1 == o1, inp1 == inp1)
    Extract(511, 256, Extract(511, 256, output1.input_).input_).potential_input_cond = cond

    assert Extract(
        511, 256, Extract(511, 256, output1.input_).input_
    ).potential_input_cond == cond
github ConsenSys / mythril / tests / laser / smt / concat_extract_tests / concat_extract_invariance.py View on Github external
def test_concat_extract_invariance4():
    input_ = symbol_factory.BitVecSym("input", 256)
    output_ = symbol_factory.BitVecFuncSym(
        "Keccak[input]", size=256, func_name="keccak256", input_=input_
    )
    output2_ = symbol_factory.BitVecFuncSym(
        "Keccak[output_]",
        size=256,
        func_name="keccak256",
        input_=Concat(output_, symbol_factory.BitVecVal(0, 256)),
    )

    construct = Concat(output2_, symbol_factory.BitVecVal(0, 256))
    assert Extract(511, 256, Extract(511, 256, construct).input_).input_ == input_
github ConsenSys / mythril / mythril / laser / ethereum / state / account.py View on Github external
key.potential_values = []
            for i, val in enumerate(potential_values):
                key.potential_values.append(
                    (val, And(models[i][1], BitVec(key.raw) == val))
                )

            return key.potential_values
        if key.size() == 512:
            val = simplify(Extract(511, 256, key))
            concrete_vals = self._traverse_concretise(val, models)
            vals2 = self._traverse_concretise(Extract(255, 0, key), models)
            key.potential_values = []
            i = 0
            for val1, val2 in zip(concrete_vals, vals2):
                if val2 and val1:
                    c_val = Concat(val1[0], val2[0])
                    condition = And(
                        models[i][1], BitVec(key.raw) == c_val, val1[1], val2[1]
                    )
                    key.potential_values.append((c_val, condition))
                else:
                    key.potential_values.append((None, None))

        if isinstance(key.input_, BitVec) or (
            isinstance(key.input_, BitVecFunc) and key.input_.func_name == "sha3"
        ):
            self._traverse_concretise(key.input_, models)

        if isinstance(key, BitVecFunc):
            if key.size() == 512:
                p1 = Extract(511, 256, key)
                if not isinstance(p1, BitVecFunc):
github ConsenSys / mythril / mythril / laser / ethereum / state / account.py View on Github external
):
            self._traverse_concretise(key.input_, models)

        if isinstance(key, BitVecFunc):
            if key.size() == 512:
                p1 = Extract(511, 256, key)
                if not isinstance(p1, BitVecFunc):
                    p1 = Extract(255, 0, key)
                p1 = [
                    (self.calc_sha3(val[0], p1.input_.size()), val[1])
                    for val in p1.input_.potential_values
                ]
                key.potential_values = []
                for i, val in enumerate(p1):
                    if val[0]:
                        c_val = Concat(val[0], Extract(255, 0, key))
                        condition = And(models[i][1], val[1], BitVec(key.raw) == c_val)
                        key.potential_values.append((c_val, condition))
                    else:
                        key.potential_values.append((None, None))
            else:
                key.potential_values = []
                for i, val in enumerate(key.input_.potential_values):
                    if val[0]:
                        concrete_val = self.calc_sha3(val[0], key.input_.size())
                        condition = And(
                            models[i][1], val[1], BitVec(key.raw) == concrete_val
                        )
                        key.potential_values.append((concrete_val, condition))
                    else:
                        key.potential_values.append((None, None))
        if key.potential_values[0][0] is not None:
github ConsenSys / mythril / mythril / laser / ethereum / instructions.py View on Github external
def byte_(self, global_state: GlobalState) -> List[GlobalState]:
        """

        :param global_state:
        :return:
        """
        mstate = global_state.mstate
        op0, op1 = mstate.stack.pop(), mstate.stack.pop()
        if not isinstance(op1, Expression):
            op1 = symbol_factory.BitVecVal(op1, 256)
        try:
            index = util.get_concrete_int(op0)
            offset = (31 - index) * 8
            if offset >= 0:
                result = simplify(
                    Concat(
                        symbol_factory.BitVecVal(0, 248),
                        Extract(offset + 7, offset, op1),
                    )
                )  # type: Union[int, Expression]
            else:
                result = 0
        except TypeError:
            log.debug("BYTE: Unsupported symbolic byte offset")
            result = global_state.new_bitvec(
                str(simplify(op1)) + "[" + str(simplify(op0)) + "]", 256
            )

        mstate.stack.append(result)
        return [global_state]
github ConsenSys / mythril / mythril / laser / ethereum / state / account.py View on Github external
def _sanitize(input_: BitVec) -> BitVec:
        if input_.potential_values:
            input_ = input_.potential_values
        if input_.size() == 512:
            return input_
        if input_.size() > 512:
            return Extract(511, 0, input_)
        else:
            return Concat(symbol_factory.BitVecVal(0, 512 - input_.size()), input_)
github ConsenSys / mythril / mythril / laser / ethereum / state / account.py View on Github external
modify = symbol
        size = min(symbol.size(), 256)
        if symbol.size() > 256:
            index = simplify(Extract(255, 0, symbol))
        else:
            index = None
        if index and not index.symbolic:
            modify = Extract(511, 256, modify)
        modify = model.eval(modify.raw)
        try:
            modify = modify.as_long()
        except AttributeError:
            modify = randint(0, 2 ** modify.size() - 1)
        modify = symbol_factory.BitVecVal(modify, size)
        if index and not index.symbolic:
            modify = Concat(modify, index)

        assert modify.size() == symbol.size()
        return modify