|
1 | 1 | """Private Set Intersection protocol based on ECDH and Bloom |
2 | 2 | Filters. |
3 | 3 | """ |
| 4 | +from typing import List |
| 5 | + |
4 | 6 | try: |
5 | 7 | # Used in Bazel envs |
6 | 8 | from private_set_intersection.python import _openmined_psi as psi |
7 | 9 | except ImportError: |
8 | 10 | # Default package |
9 | | - import openmined_psi as psi |
| 11 | + import _openmined_psi as psi |
10 | 12 |
|
11 | | -client = psi.client |
12 | | -server = psi.server |
| 13 | +__version__ = psi.__version__ |
13 | 14 |
|
14 | | -proto_server_setup = psi.proto_server_setup |
15 | | -proto_request = psi.proto_request |
16 | | -proto_response = psi.proto_response |
17 | 15 |
|
18 | | -__version__ = psi.__version__ |
| 16 | +proto_server_setup = psi.cpp_proto_server_setup |
| 17 | +proto_request = psi.cpp_proto_request |
| 18 | +proto_response = psi.cpp_proto_response |
| 19 | + |
| 20 | + |
| 21 | +class client: |
| 22 | + def __init__(self, data: psi.cpp_client): |
| 23 | + """Constructor method for the client object. |
| 24 | + Args: |
| 25 | + data: cpp_client object. |
| 26 | + Returns: |
| 27 | + client object. |
| 28 | + """ |
| 29 | + self.data = data |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def CreateWithNewKey(cls, reveal_intersection: bool): |
| 33 | + """Constructor method for the client object. |
| 34 | + Args: |
| 35 | + reveal_intersection: indicates whether the client wants to learn the elements in the intersection or only its size. |
| 36 | + Returns: |
| 37 | + client object. |
| 38 | + """ |
| 39 | + return cls(psi.cpp_client.CreateWithNewKey(reveal_intersection)) |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def CreateFromKey(cls, key_bytes: bytes, reveal_intersection: bool): |
| 43 | + """Constructor method for the client object. |
| 44 | + Args: |
| 45 | + reveal_intersection: indicates whether the client wants to learn the elements in the intersection or only its size. |
| 46 | + key_bytes: existing encryption key. |
| 47 | + Returns: |
| 48 | + client object. |
| 49 | + """ |
| 50 | + return cls(psi.cpp_client.CreateFromKey(key_bytes, reveal_intersection)) |
| 51 | + |
| 52 | + def CreateRequest(self, data: List[str]) -> proto_request: |
| 53 | + """Create a request protobuf to be serialized and sent to the server. |
| 54 | + Args: |
| 55 | + data: client items. |
| 56 | + Returns: |
| 57 | + A Protobuffer with the request. |
| 58 | + """ |
| 59 | + return self.data.CreateRequest(data) |
| 60 | + |
| 61 | + def GetIntersection( |
| 62 | + self, server_setup: proto_server_setup, server_response: proto_response |
| 63 | + ) -> List[int]: |
| 64 | + """Process the server's response and return the intersection of the client and server inputs. |
| 65 | + Args: |
| 66 | + server_setup: A protobuffer with the setup message. |
| 67 | + server_response: A protobuffer with server's response. |
| 68 | + Returns: |
| 69 | + A list of indices in clients set. |
| 70 | + """ |
| 71 | + return self.data.GetIntersection(server_setup, server_response) |
| 72 | + |
| 73 | + def GetIntersectionSize( |
| 74 | + self, server_setup: proto_server_setup, server_response: proto_response |
| 75 | + ) -> int: |
| 76 | + """Process the server's response and return the size of the intersection. |
| 77 | + Args: |
| 78 | + server_setup: A protobuffer with the setup message. |
| 79 | + server_response: A protobuffer with server's response. |
| 80 | + Returns: |
| 81 | + The intersection size. |
| 82 | + """ |
| 83 | + return self.data.GetIntersectionSize(server_setup, server_response) |
| 84 | + |
| 85 | + def GetPrivateKeyBytes(self) -> bytes: |
| 86 | + """Returns this instance's private key. This key should only be used to create other client instances. DO NOT SEND THIS KEY TO ANY OTHER PARTY! |
| 87 | + Returns: |
| 88 | + Bytes containing the key. |
| 89 | + """ |
| 90 | + return self.data.GetPrivateKeyBytes() |
| 91 | + |
| 92 | + |
| 93 | +class server: |
| 94 | + def __init__(self, data: psi.cpp_server): |
| 95 | + """Constructor method for the server object. |
| 96 | + Args: |
| 97 | + data: cpp_server object. |
| 98 | + Returns: |
| 99 | + server object. |
| 100 | + """ |
| 101 | + self.data = data |
| 102 | + |
| 103 | + @classmethod |
| 104 | + def CreateWithNewKey(cls, reveal_intersection: bool): |
| 105 | + """Constructor method for the server object. |
| 106 | + Args: |
| 107 | + reveal_intersection: indicates whether the server supports to return the elements in the intersection or only its size. |
| 108 | + Returns: |
| 109 | + server object. |
| 110 | + """ |
| 111 | + return cls(psi.cpp_server.CreateWithNewKey(reveal_intersection)) |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def CreateFromKey(cls, key_bytes: bytes, reveal_intersection: bool): |
| 115 | + """Constructor method for the server object. |
| 116 | + Args: |
| 117 | + reveal_intersection: indicates whether the server supports to return the elements in the intersection or only its size. |
| 118 | + key_bytes: existing encryption key. |
| 119 | + Returns: |
| 120 | + Returns: |
| 121 | + server object. |
| 122 | + """ |
| 123 | + return cls(psi.cpp_server.CreateFromKey(key_bytes, reveal_intersection)) |
| 124 | + |
| 125 | + def CreateSetupMessage( |
| 126 | + self, fpr: float, num_client_inputs: int, inputs: List[str] |
| 127 | + ) -> proto_server_setup: |
| 128 | + """Create a setup message from the server's dataset to be sent to the client. |
| 129 | + Args: |
| 130 | + fpr: the probability that any query of size `num_client_inputs` will result in a false positive. |
| 131 | + num_client_inputs: Client set size. |
| 132 | + inputs: Server items. |
| 133 | + Returns: |
| 134 | + A Protobuf with the setup message. |
| 135 | + """ |
| 136 | + return self.data.CreateSetupMessage(fpr, num_client_inputs, inputs) |
| 137 | + |
| 138 | + def ProcessRequest(self, client_request: proto_request) -> proto_response: |
| 139 | + """Process a client query and returns the corresponding server response to be sent to the client. |
| 140 | + Args: |
| 141 | + client_request: A Protobuf containing the client request |
| 142 | + Returns: |
| 143 | + A Protobuf with the server response. |
| 144 | + """ |
| 145 | + return self.data.ProcessRequest(client_request) |
| 146 | + |
| 147 | + def GetPrivateKeyBytes(self) -> bytes: |
| 148 | + """Return this instance's private key. This key should only be used to create other server instances. DO NOT SEND THIS KEY TO ANY OTHER PARTY! |
| 149 | + Returns: |
| 150 | + Bytes containing the key. |
| 151 | + """ |
| 152 | + return self.data.GetPrivateKeyBytes() |
| 153 | + |
19 | 154 |
|
20 | 155 | __all__ = [ |
21 | 156 | "client", |
|
0 commit comments