Ejemplo n.º 1
0
    def detail(worker: AbstractWorker, plan_tuple: tuple) -> "Plan":
        """This function reconstructs a Plan object given its attributes in the form of a tuple.
        Args:
            worker: the worker doing the deserialization
            plan_tuple: a tuple holding the attributes of the Plan
        Returns:
            plan: a Plan object
        """
        (id_, role, include_state, is_built, name, tags, description,
         torchscript) = plan_tuple

        id_ = sy.serde.msgpack.serde._detail(worker, id_)
        role = sy.serde.msgpack.serde._detail(worker, role)
        name = sy.serde.msgpack.serde._detail(worker, name)
        tags = sy.serde.msgpack.serde._detail(worker, tags)
        description = sy.serde.msgpack.serde._detail(worker, description)
        torchscript = sy.serde.msgpack.serde._detail(worker, torchscript)

        plan = sy.Plan(
            role=role,
            include_state=include_state,
            is_built=is_built,
            id=id_,
            owner=worker,
            name=name,
            tags=tags,
            description=description,
        )

        plan.torchscript = torchscript

        return plan
Ejemplo n.º 2
0
def test__call__for_method(hook):
    plan = sy.Plan(id="0",
                   owner=hook.local_worker,
                   name="test_plan",
                   is_method=True)
    result_id = 444

    pop_function_original = sy.ID_PROVIDER.pop
    sy.ID_PROVIDER.pop = mock.Mock(return_value=result_id)

    return_value = "return value"
    plan.execute_plan = mock.Mock(return_value=return_value)

    self_value = mock.Mock()
    self_value.send = mock.Mock()
    plan._self = self_value

    arg_list = (100, 200, 356)
    ret_val = plan(*arg_list)

    expected_args = tuple([self_value] + list(arg_list))
    plan.execute_plan.assert_called_with(expected_args, [result_id])

    assert ret_val == return_value

    # reset function
    sy.ID_PROVIDER.pop = pop_function_original
Ejemplo n.º 3
0
def _detail_plan(worker: AbstractWorker, plan_tuple: tuple) -> Plan:
    """This function reconstructs a Plan object given it's attributes in the form of a tuple.
    Args:
        worker: the worker doing the deserialization
        plan_tuple: a tuple holding the attributes of the Plan
    Returns:
        Plan: a Plan object
    """

    readable_plan, id, arg_ids, result_ids, name, tags, description = plan_tuple
    id = id
    if isinstance(id, bytes):
        id = id.decode("utf-8")
    arg_ids = _detail(worker, arg_ids)
    result_ids = _detail(worker, result_ids)

    plan = syft.Plan(
        owner=worker,
        id=id,
        arg_ids=arg_ids,
        result_ids=result_ids,
        readable_plan=_detail(worker, readable_plan),
    )
    if isinstance(name, bytes):
        plan.name = name.decode("utf-8")
    plan.tags = _detail(worker, tags)
    plan.description = _detail(worker, description)

    return plan
Ejemplo n.º 4
0
    def detail(worker: AbstractWorker, plan_tuple: tuple) -> "Plan":
        """This function reconstructs a Plan object given its attributes in the form of a tuple.
        Args:
            worker: the worker doing the deserialization
            plan_tuple: a tuple holding the attributes of the Plan
        Returns:
            plan: a Plan object
        """

        id, procedure, state, include_state, is_built, name, tags, description = plan_tuple
        id = sy.serde._detail(worker, id)
        procedure = sy.serde._detail(worker, procedure)
        state = sy.serde._detail(worker, state)

        plan = sy.Plan(owner=worker,
                       id=id,
                       include_state=include_state,
                       is_built=is_built)

        plan.procedure = procedure
        plan.state = state
        state.plan = plan

        plan.name = sy.serde._detail(worker, name)
        plan.tags = sy.serde._detail(worker, tags)
        plan.description = sy.serde._detail(worker, description)

        return plan
Ejemplo n.º 5
0
def test___call__method(hook):
    plan = sy.Plan(id="0", owner=hook.local_worker, name="test_plan")

    with pytest.raises(AssertionError):
        plan(kwarg1="hello", kwarg2=None)

    result_id = 444

    pop_function_original = sy.ID_PROVIDER.pop
    sy.ID_PROVIDER.pop = mock.Mock(return_value=result_id)

    return_value = "return value"
    plan.execute_plan = mock.Mock(return_value=return_value)

    # test the method case
    self_value = "my_self"
    plan.self = self_value

    arg_list = (100, 200, 356)
    ret_val = plan(*arg_list)

    expected_args = [self_value] + list(arg_list)
    plan.execute_plan.assert_called_with(expected_args, [result_id])

    assert ret_val == return_value

    # reset function
    sy.ID_PROVIDER.pop = pop_function_original
Ejemplo n.º 6
0
    def detail(worker: AbstractWorker, plan_tuple: tuple) -> "Plan":
        """This function reconstructs a Plan object given it's attributes in the form of a tuple.
        Args:
            worker: the worker doing the deserialization
            plan_tuple: a tuple holding the attributes of the Plan
        Returns:
            plan: a Plan object
        """

        readable_plan, id, arg_ids, result_ids, name, tags, description, is_built = plan_tuple
        id = sy.serde._detail(worker, id)
        arg_ids = sy.serde._detail(worker, arg_ids)
        result_ids = sy.serde._detail(worker, result_ids)

        plan = sy.Plan(
            owner=worker,
            id=id,
            arg_ids=arg_ids,
            result_ids=result_ids,
            readable_plan=sy.serde._detail(worker, readable_plan),
            is_built=is_built,
        )

        plan.name = sy.serde._detail(worker, name)
        plan.tags = sy.serde._detail(worker, tags)
        plan.description = sy.serde._detail(worker, description)

        return plan
Ejemplo n.º 7
0
def test_replace_worker_ids_two_ints(hook):
    plan = sy.Plan(id="0", owner=hook.local_worker, name="test_plan")
    _replace_message_ids_orig = sy.federated.Plan._replace_message_ids
    mock_fun = mock.Mock(return_value=[])
    sy.federated.Plan._replace_message_ids = mock_fun
    plan.replace_worker_ids(300, 400)
    args = {"change_id": -1, "obj": [], "to_id": -1}
    calls = [mock.call(from_worker=300, to_worker=400, **args)]
    mock_fun.assert_called_once()
    mock_fun.assert_has_calls(calls, any_order=True)
    sy.federated.Plan._replace_message_ids = _replace_message_ids_orig
Ejemplo n.º 8
0
    def unbufferize(worker: AbstractWorker, protobuf_plan: PlanPB) -> "Plan":
        """This function reconstructs a Plan object given its attributes in the form of a Protobuf message
        Args:
            worker: the worker doing the deserialization
            protobuf_plan: a Protobuf message holding the attributes of the Plan
        Returns:
            plan: a Plan object
        """

        worker._tmp_placeholders = {}
        id = sy.serde.protobuf.proto.get_protobuf_id(protobuf_plan.id)

        operations = []
        for operation in protobuf_plan.operations:
            op_msg = OperationMessagePB()
            op_msg.operation.CopyFrom(operation)
            operations.append(op_msg)

        operations = [
            sy.serde.protobuf.serde._unbufferize(worker, operation)
            for operation in operations
        ]
        state = sy.serde.protobuf.serde._unbufferize(worker,
                                                     protobuf_plan.state)

        placeholders = [
            sy.serde.protobuf.serde._unbufferize(worker, placeholder)
            for placeholder in protobuf_plan.placeholders
        ]
        placeholders = dict([(placeholder.id, placeholder)
                             for placeholder in placeholders])

        plan = sy.Plan(
            include_state=protobuf_plan.include_state,
            is_built=protobuf_plan.is_built,
            operations=operations,
            placeholders=placeholders,
            id=id,
            owner=worker,
        )
        del worker._tmp_placeholders

        plan.state = state
        state.plan = plan

        plan.name = protobuf_plan.name
        if protobuf_plan.tags:
            plan.tags = set(protobuf_plan.tags)
        if protobuf_plan.description:
            plan.description = protobuf_plan.description

        return plan
Ejemplo n.º 9
0
def test_replace_worker_ids_two_strings(hook):
    plan = sy.Plan(id="0", owner=hook.local_worker, name="test_plan")
    _replace_message_ids_orig = sy.federated.Plan._replace_message_ids
    mock_fun = mock.Mock(return_value=[])
    sy.federated.Plan._replace_message_ids = mock_fun
    plan.replace_worker_ids("me", "you")
    args = {"change_id": -1, "obj": [], "to_id": -1}
    calls = [
        mock.call(from_worker="me", to_worker="you", **args),
        mock.call(from_worker=b"me", to_worker=b"you", **args),
    ]
    assert len(mock_fun.mock_calls) == 2
    mock_fun.assert_has_calls(calls, any_order=True)
    sy.federated.Plan._replace_message_ids = _replace_message_ids_orig
Ejemplo n.º 10
0
    def unbufferize(worker: AbstractWorker, protobuf_plan: PlanPB) -> "Plan":
        """This function reconstructs a Plan object given its attributes in the form of a Protobuf message
        Args:
            worker: the worker doing the deserialization
            protobuf_plan: a Protobuf message holding the attributes of the Plan
        Returns:
            plan: a Plan object
        """

        id = sy.serde.protobuf.proto.get_protobuf_id(protobuf_plan.id)

        actions = [
            sy.serde.protobuf.serde._unbufferize(worker, action)
            for action in protobuf_plan.actions
        ]
        state = sy.serde.protobuf.serde._unbufferize(worker,
                                                     protobuf_plan.state)

        placeholders = [
            sy.serde.protobuf.serde._unbufferize(worker, placeholder)
            for placeholder in protobuf_plan.placeholders
        ]
        placeholders = dict([(placeholder.id.value, placeholder)
                             for placeholder in placeholders])

        plan = sy.Plan(
            include_state=protobuf_plan.include_state,
            is_built=protobuf_plan.is_built,
            actions=actions,
            placeholders=placeholders,
            id=id,
            owner=worker,
        )

        plan.state = state
        state.plan = plan

        plan.name = protobuf_plan.name
        if protobuf_plan.tags:
            plan.tags = set(protobuf_plan.tags)
        if protobuf_plan.description:
            plan.description = protobuf_plan.description

        plan = Plan.replace_non_instanciated_placeholders(plan)

        return plan
Ejemplo n.º 11
0
    def detail(worker: AbstractWorker, plan_tuple: tuple) -> "Plan":
        """This function reconstructs a Plan object given its attributes in the form of a tuple.
        Args:
            worker: the worker doing the deserialization
            plan_tuple: a tuple holding the attributes of the Plan
        Returns:
            plan: a Plan object
        """

        (
            id,
            actions,
            state,
            include_state,
            is_built,
            name,
            tags,
            description,
            placeholders,
        ) = plan_tuple

        worker._tmp_placeholders = {}
        id = sy.serde.msgpack.serde._detail(worker, id)
        actions = sy.serde.msgpack.serde._detail(worker, actions)
        state = sy.serde.msgpack.serde._detail(worker, state)
        placeholders = sy.serde.msgpack.serde._detail(worker, placeholders)

        plan = sy.Plan(
            include_state=include_state,
            is_built=is_built,
            actions=actions,
            placeholders=placeholders,
            id=id,
            owner=worker,
        )
        del worker._tmp_placeholders

        plan.state = state
        state.plan = plan

        plan.name = sy.serde.msgpack.serde._detail(worker, name)
        plan.tags = sy.serde.msgpack.serde._detail(worker, tags)
        plan.description = sy.serde.msgpack.serde._detail(worker, description)

        return plan
Ejemplo n.º 12
0
def test___call__function(hook):
    plan = sy.Plan(id="0", owner=hook.local_worker, name="test_plan")
    with pytest.raises(ValueError):
        plan(kwarg1="hello", kwarg2=None)

    result_id = 444

    pop_function_original = sy.ID_PROVIDER.pop
    sy.ID_PROVIDER.pop = mock.Mock(return_value=result_id)

    return_value = "return value"
    plan.execute_plan = mock.Mock(return_value=return_value)

    arg_list = (100, 200, 356)
    ret_val = plan(*arg_list)

    plan.execute_plan.assert_called_with(arg_list, [result_id])

    assert ret_val == return_value

    # reset function
    sy.ID_PROVIDER.pop = pop_function_original
Ejemplo n.º 13
0
def initialize_crypto_plans(worker):
    """
    This is called manually for the moment, to build the plan used to perform
    Function Secret Sharing on a specific worker.
    """
    eq_plan_1 = sy.Plan(
        forward_func=lambda x, y: mask_builder(x, y, "eq"),
        owner=worker,
        tags=["#fss_eq_plan_1"],
        is_built=True,
    )
    worker.register_obj(eq_plan_1)
    eq_plan_2 = sy.Plan(forward_func=eq_eval_plan,
                        owner=worker,
                        tags=["#fss_eq_plan_2"],
                        is_built=True)
    worker.register_obj(eq_plan_2)

    comp_plan_1 = sy.Plan(
        forward_func=lambda x, y: mask_builder(x, y, "comp"),
        owner=worker,
        tags=["#fss_comp_plan_1"],
        is_built=True,
    )
    worker.register_obj(comp_plan_1)
    comp_plan_2 = sy.Plan(forward_func=comp_eval_plan,
                          owner=worker,
                          tags=["#fss_comp_plan_2"],
                          is_built=True)
    worker.register_obj(comp_plan_2)

    xor_add_plan = sy.Plan(forward_func=xor_add_convert_1,
                           owner=worker,
                           tags=["#xor_add_1"],
                           is_built=True)
    worker.register_obj(xor_add_plan)
    xor_add_plan = sy.Plan(forward_func=xor_add_convert_2,
                           owner=worker,
                           tags=["#xor_add_2"],
                           is_built=True)
    worker.register_obj(xor_add_plan)
Ejemplo n.º 14
0
def test__call__raise(hook):
    plan = sy.Plan(id="0", owner=hook.local_worker, name="test_plan")
    with pytest.raises(ValueError):
        plan(kwarg1="hello", kwarg2=None)