Skip to content

Commit

Permalink
Merge branch 'dev-hetero' of github.com:iflytek/iflearner into dev-he…
Browse files Browse the repository at this point in the history
…tero
  • Loading branch information
xs233 committed Oct 9, 2022
2 parents f7142cf + e898a5b commit fef5154
Show file tree
Hide file tree
Showing 16 changed files with 321 additions and 99 deletions.
1 change: 0 additions & 1 deletion examples/mpc/quickstart_piss/client_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# ==============================================================================

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

Expand Down
24 changes: 12 additions & 12 deletions examples/mpc/quickstart_piss/quickstart_piss.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,8 @@
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

Expand All @@ -45,21 +37,29 @@
help="encryption param"
)
parser.add_argument(
"--addr",
default="127.0.0.1:37221",
"--server",
default="127.0.0.1:45551",
type=str,
help="address of client service"
)

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

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

args = parser.parse_args()
print(args)
controller = PissClientController(args)
#controller.init_data()
controller.start_querty()
controller.start_querty()
print(controller.get_secrets_sum())
6 changes: 3 additions & 3 deletions iflearner/business/mpc/piss/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

parser.add_argument(
"--server",
default="127.0.0.1:12095",
default="127.0.0.1:15554",
type=str,
help="address of aggerating server",
)
Expand All @@ -33,7 +33,7 @@

parser.add_argument(
"--addr",
default="127.0.0.1:57221",
default="127.0.0.1:45553",
type=str,
help="address of client service",
)
Expand All @@ -49,6 +49,6 @@
default=None,
type=str,
help="path of server SSL cert"
"""use secure channel to connect to server if not none""",
"""use secure channel to connect to server if not none"""
)

6 changes: 1 addition & 5 deletions iflearner/business/mpc/piss/piss_aggregate_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@
# 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:

Expand All @@ -39,7 +35,7 @@ def main():


parser.add_argument(
"--addr", help="the server address", default="127.0.0.1:12095", type=str
"--addr", help="the server address", default="127.0.0.1:20001", type=str
)
parser.add_argument(
"--name", help="the server name", default="server", type=str
Expand Down
44 changes: 12 additions & 32 deletions iflearner/business/mpc/piss/piss_client_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
from loguru import logger

from iflearner.communication.mpc.piss import piss_client_services
from iflearner.communication.mpc.piss import piss_client
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
from iflearner.communication.base import base_server


class PissClientServicesController:
Expand Down Expand Up @@ -50,39 +50,19 @@ def run(self) -> None:

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)

self._piss_client_inst = piss_client.PissClient(party_name= self._args.name,
server_addr = self._args.server,
cert_path= self._args.cert,
data_path= self._args.data,
encryption_param = self._args.param
)
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())
self._piss_client_inst.start_querty()

resp = self._stub.send(req)
def get_secrets_sum(self):
secrets_sum = self._piss_client_inst.get_secrets_sum()
return secrets_sum



33 changes: 16 additions & 17 deletions iflearner/business/mpc/piss/piss_startegy_base.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
# 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 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
from iflearner.communication.base import base_pb2_grpc


class PissStrategyBase(ABC):
Expand Down
39 changes: 31 additions & 8 deletions iflearner/business/mpc/piss/piss_strategy_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# 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 random import random
from tkinter import NO
from typing import Any, Dict
Expand All @@ -11,6 +27,7 @@
import pandas as pd
import hashlib
import random
from iflearner.communication.mpc.piss import message_type
from iflearner.communication.mpc.piss.piss_exception import PissException
from iflearner.communication.mpc.piss import piss_pb2
from iflearner.business.mpc.piss.piss_startegy_base import PissStrategyBase
Expand Down Expand Up @@ -80,6 +97,7 @@ def __init__(self ,cert_path: str, party_name: str,options , data_path:str) -> N

self._virtual_client = 'virtual_client'
self._party_name_list = [self._virtual_client]
self._secrets_sum_ready = False

def get_party_name_list(self):
"""
Expand Down Expand Up @@ -109,6 +127,13 @@ def get_initiator_stub(self):
"""
return self._initiator_stub

def get_initiator_party_name(self):
"""
Returns:
self._initiator_party_name.
"""
return self._initiator_party_name

def generate_participants_stubs(self, data: piss_pb2.ParticipantsRoutes)-> None:
"""
Generate handles for all parties.
Expand Down Expand Up @@ -275,7 +300,6 @@ def c_sub_keys(self,keys_recv):
return sub_keys_sum

def sub_key_sum(self):

if self._virtual_client in self._party_name_list:
self._party_name_list.remove(self._virtual_client)
self._party_name_list.sort()
Expand All @@ -286,7 +310,7 @@ def sub_key_sum(self):
self._sub_keys_sum_recv[self._party_name] = self._sub_keys_sum
self._sub_keys_sum_recv[self._virtual_client] = self._virtual_sub_keys_sum
self._recv_sum_num += 1
return 'initiator'
return message_type.MSG_INITIATOR
else:
self._sub_keys_sum = self.c_sub_keys(self._sub_keys_recv)
return self._sub_keys_sum
Expand Down Expand Up @@ -329,6 +353,9 @@ def is_start_reconstruct(self):
else:
return False

def secrets_sum_ready(self):
return self._secrets_sum_ready

def reconstruct_sum_secrets(self):
"""
Reconstruct secret
Expand All @@ -347,6 +374,8 @@ def reconstruct_sum_secrets(self):
x.append(self._party_name_list.index(client)+1)
y.append(l)
secrets_sum = self.decrypt(x,y)
#print(secrets_sum)
self._secrets_sum_ready = True
return secrets_sum

def decrypt(self,x,y):
Expand Down Expand Up @@ -416,9 +445,3 @@ def get_data(self , data_index: list):









16 changes: 16 additions & 0 deletions iflearner/business/mpc/piss/piss_strategy_server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# 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 typing import Any, Dict
import numpy as np
from importlib import import_module
Expand Down
2 changes: 1 addition & 1 deletion iflearner/communication/mpc/piss/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Iflybank. All Rights Reserved.
# 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.
Expand Down
11 changes: 11 additions & 0 deletions iflearner/communication/mpc/piss/message_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,16 @@
MSG_RETURN_ENCRYPTED_DATA_SUM = "msg_return_encrypted_data_sum"

MSG_INIT_DATA = "msg_init_data"
MSG_GET_SUM_SECRETS = "msg_get_sum_secrets"
MSG_END_QUERY = "msg_end_query"

"""Define the heartbeat interval between client and client service."""
MSG_HEARTBEAT_INTERVAL = 10

"""Define the heartbeat interval of Sum time."""
MSG_SUM_TIME = 2

MSG_INITIATOR = "msg_initiator"



7 changes: 7 additions & 0 deletions iflearner/communication/mpc/piss/piss.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ message SubSecretsSUM{
map<string, string> sub_keys_sum= 1;
}

message SecretsSUM{
string secrets_sum= 1;
}

message CallBack{
string call_back_msg = 1;
}



Loading

0 comments on commit fef5154

Please sign in to comment.