Skip to content

Commit

Permalink
add mpc (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
dygslyz authored Sep 27, 2022
1 parent 92e8342 commit 32f3348
Show file tree
Hide file tree
Showing 23 changed files with 1,871 additions and 0 deletions.
Empty file.
30 changes: 30 additions & 0 deletions examples/mpc/quickstart_piss/client_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2022 iFLYTEK. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from importlib import import_module
from loguru import logger
from iflearner.business.mpc.piss.piss_client_controller import PissClientServicesController
from iflearner.business.mpc.piss.argument import parser


if __name__ == "__main__":
args = parser.parse_args()
print(args)
controller = PissClientServicesController(args)
controller.run()




4 changes: 4 additions & 0 deletions examples/mpc/quickstart_piss/piss_data_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ID,Age,Money,Height,Credit_value
10001,18,6.1,1.88,98.6
10002,19,-99.25,1.76,87
10003,20,8.6,1.68,99.5
65 changes: 65 additions & 0 deletions examples/mpc/quickstart_piss/quickstart_piss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2022 iFLYTEK. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import argparse
import json
from importlib import import_module
from threading import Thread
from typing import Any, Dict, Union
from loguru import logger
import time

from iflearner.communication.mpc.piss import piss_client_services
from iflearner.business.mpc.piss import piss_strategy_client
from iflearner.communication.base import base_server
from iflearner.communication.mpc.piss import message_type
from iflearner.communication.mpc.piss import piss_pb2, piss_pb2_grpc
from iflearner.business.mpc.piss.piss_client_controller import PissClientController
from iflearner.business.mpc.piss.argument import parser

if __name__ == "__main__":

parser = argparse.ArgumentParser()

parser.add_argument(
"--data",
default='examples/mpc/quickstart_piss/piss_data_test.csv',
type=str,
help="path of data")

parser.add_argument(
"--param",
default={'10001':'Age', '10002':'Money'},
type=json.loads,
help="encryption param"
)
parser.add_argument(
"--addr",
default="127.0.0.1:37221",
type=str,
help="address of client service"
)

parser.add_argument(
"--name",
default="client_querty",
type=str,
help="querty client name"
)

args = parser.parse_args()
print(args)
controller = PissClientController(args)
#controller.init_data()
controller.start_querty()
4 changes: 4 additions & 0 deletions examples/mpc/quickstart_piss/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from iflearner.business.mpc.piss.piss_aggregate_server import main

if __name__ == "__main__":
main()
54 changes: 54 additions & 0 deletions iflearner/business/mpc/piss/argument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2022 iFLYTEK. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import argparse

parser = argparse.ArgumentParser()


parser.add_argument(
"--server",
default="127.0.0.1:12095",
type=str,
help="address of aggerating server",
)

parser.add_argument(
"--name",
default="client003",
type=str,
help="party name of client",
)

parser.add_argument(
"--addr",
default="127.0.0.1:57221",
type=str,
help="address of client service",
)

parser.add_argument(
"--data",
default='examples/mpc/quickstart_piss/piss_data_test.csv',
type=str,
help="path of data")

parser.add_argument(
"--cert",
default=None,
type=str,
help="path of server SSL cert"
"""use secure channel to connect to server if not none""",
)

55 changes: 55 additions & 0 deletions iflearner/business/mpc/piss/piss_aggregate_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2022 iFLYTEK. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import argparse
import json
from importlib import import_module
from threading import Thread
from typing import Any, Dict, Union

from iflearner.communication.mpc.piss import piss_server
from iflearner.communication.base import base_server
from iflearner.communication.mpc.piss import piss_pb2, piss_pb2_grpc
from iflearner.business.mpc.piss import piss_client_controller

class PissAggregateServer:

def __init__(self, addr: str ,party_name: str) -> None:
self._addr = addr
self._party_name = party_name
self._piss_server_inst = piss_server.PissServer(party_name= party_name)

def run(self) -> None:
"""start piss server"""
base_server.start_server(self._addr,self._piss_server_inst)

def main():
parser = argparse.ArgumentParser()


parser.add_argument(
"--addr", help="the server address", default="127.0.0.1:12095", type=str
)
parser.add_argument(
"--name", help="the server name", default="server", type=str
)
args = parser.parse_args()

global server
server = PissAggregateServer(addr = args.addr, party_name= args.name)
server.run()

if __name__ == "__main__":
main()

88 changes: 88 additions & 0 deletions iflearner/business/mpc/piss/piss_client_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Copyright 2022 iFLYTEK. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import argparse
from importlib import import_module
from typing import Any, Dict, Union
from loguru import logger

from iflearner.communication.mpc.piss import piss_client_services
from iflearner.communication.base import base_server
from iflearner.communication.mpc.piss import message_type
from iflearner.communication.mpc.piss import piss_pb2
import grpc
from iflearner.communication.base import base_pb2, base_pb2_grpc,base_server,constant


class PissClientServicesController:

def __init__(self, args: argparse.Namespace) -> None:

self._args = args
self._piss_client_services_inst = piss_client_services.PissClientServices(
server_addr=self._args.server ,
party_name= self._args.name,
route= self._args.addr,
data_path= self._args.data,
cert_path= self._args.cert
)

def run(self) -> None:
# """REGISTER"""
resp = self._piss_client_services_inst.transport(
type = message_type.MSG_REGISTER,
data = piss_pb2.RegistrationInfo(route = self._args.addr)
)
"""start piss client server"""
if resp.code == 0:
base_server.start_server(self._args.addr,self._piss_client_services_inst)

class PissClientController:
def __init__(self, args: argparse.Namespace) -> None:

self._args = args
self._options = [
("grpc.max_message_length", constant.MAX_MSG_LENGTH),
("grpc.max_send_message_length", constant.MAX_MSG_LENGTH),
("grpc.max_receive_message_length", constant.MAX_MSG_LENGTH),
]

self._channel = grpc.insecure_channel(self._args.addr, options = self._options)
self._stub = base_pb2_grpc.BaseStub(self._channel)
self._encryption_param = self._args.param
self._data_path = self._args.data
self._party_name = self._args.name

def init_data(self):

data = piss_pb2.InitData(data_path = self._data_path)
req = base_pb2.BaseRequest(party_name = self._party_name,
type = message_type.MSG_INIT_DATA,
data = data.SerializeToString())
resp = self._stub.send(req)

def start_querty(self):

#encryption_param = {'10001':'Age', '10002':'Money'}
#encryption_param = json.loads(self._encryption_param)
data = piss_pb2.ShareEncryptionParam(encryption_param = self._encryption_param)

req = base_pb2.BaseRequest(party_name = self._party_name,
type = message_type.MSG_START_QUERY,
data = data.SerializeToString())

resp = self._stub.send(req)



56 changes: 56 additions & 0 deletions iflearner/business/mpc/piss/piss_startegy_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from abc import ABC
from ast import Return
import code
from enum import IntEnum, auto
import imp
from typing import Any, Dict

import numpy as np

import argparse
import json
from importlib import import_module
from threading import Thread
from typing import Any, Dict, Union
from loguru import logger
import time
import grpc
from iflearner.communication.mpc import piss
import threading


from iflearner.communication.mpc.piss.piss_exception import PissException
from iflearner.communication.base import base_server
from iflearner.communication.mpc.piss import message_type
from iflearner.communication.mpc.piss import piss_pb2, piss_pb2_grpc
from iflearner.communication.base import base_pb2, base_pb2_grpc,base_server,constant


class PissStrategyBase(ABC):
def __init__(self ,cert_path: str, party_name: str,options) -> None:

self._cert_path = cert_path
self._party_name = party_name
self._options = options

self._routes: dict = dict()
self._stubs: dict = dict()
self._party_name_list = []

self._initiator_party_name: str = str()
self._initiator_route: str = str()
self._initiator_stub = None

def generate_stub(self, destination_addr: str):

if self._cert_path is None:
channel = grpc.insecure_channel(destination_addr, options = self._options)
else:
with open(self._cert_path, "rb") as f:
cert_bytes = f.read()

channel = grpc.secure_channel(
destination_addr, grpc.ssl_channel_credentials(cert_bytes), options = self._options
)
stub = base_pb2_grpc.BaseStub(channel)
return stub
Loading

0 comments on commit 32f3348

Please sign in to comment.