├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build_and_run.sh ├── build_and_run_mpc.sh ├── capsule ├── __init__.py └── zmq_client.py ├── capsule_mpc_server ├── local_server.py └── tasks.py ├── capsule_zmq ├── local_server.py └── tasks.py ├── notebooks ├── .ipynb_checkpoints │ ├── Capsule - Basic Local Server Example-checkpoint.ipynb │ ├── Capsule MPC-checkpoint.ipynb │ ├── Capsule with Django-checkpoint.ipynb │ └── Capsule with ZMQ-checkpoint.ipynb ├── Capsule with RSSMPCTensor.ipynb ├── Capsule with SPDZMPCTensor.ipynb └── Capsule with ZMQ.ipynb ├── requirements.txt ├── setup.cfg ├── setup.py └── test-requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | ### Python ### 2 | # Byte-compiled / optimized / DLL files 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | capsule.egg-info/ 7 | */**/__pycache__ 8 | *.rdb 9 | */*.rdb 10 | *.sqlite3 11 | 12 | # C extensions 13 | *.so 14 | 15 | # Distribution / packaging 16 | .Python 17 | env/ 18 | build/ 19 | develop-eggs/ 20 | dist/ 21 | downloads/ 22 | eggs/ 23 | .eggs/ 24 | lib/ 25 | lib64/ 26 | parts/ 27 | sdist/ 28 | var/ 29 | wheels/ 30 | *.egg-info/ 31 | .installed.cfg 32 | *.egg 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .coverage 48 | .coverage.* 49 | .cache 50 | nosetests.xml 51 | coverage.xml 52 | *,cover 53 | .hypothesis/ 54 | pytest_runner* 55 | 56 | # Translations 57 | *.mo 58 | *.pot 59 | 60 | # Django stuff: 61 | *.log 62 | local_settings.py 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # pyenv 81 | .python-version 82 | 83 | # celery beat schedule file 84 | celerybeat-schedule 85 | 86 | # SageMath parsed files 87 | *.sage.py 88 | 89 | # dotenv 90 | .env 91 | 92 | # virtualenv 93 | .venv 94 | venv/ 95 | ENV/ 96 | 97 | # Spyder project settings 98 | .spyderproject 99 | .spyproject 100 | 101 | # Rope project settings 102 | .ropeproject 103 | 104 | # mkdocs documentation 105 | /site 106 | 107 | *.pickle 108 | lastfailed 109 | 110 | 111 | ### VisualStudioCode ### 112 | .vscode/* 113 | !.vscode/settings.json 114 | !.vscode/tasks.json 115 | !.vscode/launch.json 116 | !.vscode/extensions.json 117 | .history 118 | 119 | # for some Temporary objects created by some extensions in vscode 120 | .DS_Store 121 | 122 | # code coverage reports 123 | .coverage 124 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.6" 4 | 5 | # install dependencies 6 | install: 7 | - "pip install -r test-requirements.txt" 8 | - "python setup.py install" 9 | 10 | # run tests 11 | script: 12 | - flake8 13 | 14 | notifications: 15 | slack: openmined:vOUE1d9jSb6V7R1r6zrjRD4E 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License, Version 2.0 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ 2 | 3 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 4 | 5 | 1. Definitions. 6 | 7 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 8 | 9 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 10 | 11 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 12 | 13 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 14 | 15 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 16 | 17 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 18 | 19 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 20 | 21 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 22 | 23 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 24 | 25 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 26 | 27 | 2. Grant of Copyright License. 28 | 29 | Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 30 | 31 | 3. Grant of Patent License. 32 | 33 | Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 34 | 35 | 4. Redistribution. 36 | 37 | You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 38 | 39 | You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 40 | 41 | 5. Submission of Contributions. 42 | 43 | Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 44 | 45 | 6. Trademarks. 46 | 47 | This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 48 | 49 | 7. Disclaimer of Warranty. 50 | 51 | Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 52 | 53 | 8. Limitation of Liability. 54 | 55 | In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 56 | 57 | 9. Accepting Warranty or Additional Liability. 58 | 59 | While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 60 | 61 | END OF TERMS AND CONDITIONS 62 | 63 | APPENDIX: How to apply the Apache License to your work 64 | 65 | To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. 66 | 67 | Copyright 2017 Open Mined contributors. 68 | 69 | 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 70 | 71 | http://www.apache.org/licenses/LICENSE-2.0 72 | 73 | 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. 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # capsule 2 | [![Build Status](https://travis-ci.org/OpenMined/Capsule.svg?branch=master)](https://travis-ci.org/OpenMined/Capsule) 3 | 4 | ❗ 5 | This library has been deprecated due to changes in strategy and roadmap. To actively contribute based on our current roadmap, checkout [OpenMined](https://github.com/OpenMined/OpenMined), [PySyft](https://github.com/OpenMined/PySyft), or [join our slack](https://openmined.slack.com/messages/team_pysyft) 6 | --- 7 | 8 | > Service for Storing And Interacting with Secrets (particularly Keys or MPC Shares) Off Blockchain 9 | 10 | The goal of this library is to allow developers of OpenMined to simulate participating in a network where others have access to secret information that developers do not. In the trivial case, Capsule can generate a private Encryption key and never reveal it to the developer. See the [notebooks](./notebooks) folder for tutorials on how to use the library. 11 | 12 | ## Step 1: Open Terminal and Run: 13 | 14 | ```sh 15 | sh build_and_run.sh 16 | ``` 17 | 18 | ## Step 2: Open Terminal and Run: 19 | 20 | ```sh 21 | jupyter notebook 22 | ``` 23 | 24 | ## Step 3: Select Notebook 25 | 26 | In the [notebooks](./notebooks) folder, you'll find that there are several example notebooks showing how to use the Capsule. You'll find that the general setup is a server (which you started in Step 1) which holds onto secret information. From the Jupyter notebook you can interact with the server as if you were interacting with individuals on a live global network. 27 | 28 | ## Current Research 29 | 30 | ### NuCypherKMS: decentralized key management system 31 | - https://arxiv.org/pdf/1707.06140.pdf 32 | 33 | ### ZoE: Zcash over Ethereum cross chain zk-snarks 34 | - https://z.cash/blog/zcash-eth.html 35 | - https://github.com/zcash/babyzoe 36 | - https://github.com/zcash/babyzoe/blob/master/talks/2016-07-27-IC3---SNARKs-for-Ethereum.pdf 37 | - https://blog.ethereum.org/2016/12/05/zksnarks-in-a-nutshell/ 38 | - https://media.consensys.net/introduction-to-zksnarks-with-examples-3283b554fc3b 39 | 40 | ### Hawk: blockchain model of cryptography and privacy-preserving smart contracts 41 | - https://eprint.iacr.org/2015/675.pdf 42 | 43 | ### Fast and Secure Linear Regression and Biometric Authentication with Security Update 44 | - https://eprint.iacr.org/2015/692.pdf 45 | 46 | ### A Comparison of the Homomorphic Encryption Schemes: 47 | - https://eprint.iacr.org/2014/062.pdf 48 | 49 | ### Multi-bit homomorphic encryption based on learning with errors over rings 50 | - https://eprint.iacr.org/2013/138.pdf 51 | -------------------------------------------------------------------------------- /build_and_run.sh: -------------------------------------------------------------------------------- 1 | pip install -r requirements.txt 2 | python setup.py install 3 | python capsule_zmq/local_server.py 4 | -------------------------------------------------------------------------------- /build_and_run_mpc.sh: -------------------------------------------------------------------------------- 1 | pip install -r requirements.txt 2 | python setup.py install 3 | python capsule_mpc_server/local_server.py 4 | -------------------------------------------------------------------------------- /capsule/__init__.py: -------------------------------------------------------------------------------- 1 | # flake8: noqa 2 | from capsule import zmq_client 3 | -------------------------------------------------------------------------------- /capsule/zmq_client.py: -------------------------------------------------------------------------------- 1 | from syft.he.paillier.keys import PublicKey 2 | import syft 3 | import random 4 | import zmq 5 | import pickle 6 | 7 | 8 | class LocalCapsuleClient(): 9 | 10 | def __init__(self, host='127.0.0.1', port='5000'): 11 | self.host = host 12 | self.port = port 13 | ctx = zmq.Context() 14 | self.task_socket = ctx.socket(zmq.REQ) 15 | self.task_socket.connect('tcp://127.0.0.1:5002') 16 | 17 | def keygen(self, scheme='paillier'): 18 | id = str(random.randint(0, 2**32)) 19 | task_kwargs = { 20 | "id": id, 21 | "scheme": scheme, 22 | } 23 | self.task_socket.send_string(str({ 24 | "task": "create_keys", 25 | "task_kwargs": task_kwargs 26 | })) 27 | r = self.task_socket.recv() 28 | pk = PublicKey.deserialize(r) 29 | pk.id = id 30 | return pk 31 | 32 | def bootstrap(self, x, id=None): 33 | if(id is None): 34 | id = x.public_key.id 35 | data = x.serialize() 36 | task_kwargs = { 37 | "key_id": id, 38 | "data": data, 39 | } 40 | self.task_socket.send_string(str({ 41 | "task": "bootstrap", 42 | "task_kwargs": task_kwargs 43 | })) 44 | r = self.task_socket.recv() 45 | return syft.tensor.TensorBase.deserialize(r) 46 | 47 | def decrypt(self, x, id=None): 48 | if(id is None): 49 | id = x.public_key.id 50 | data = x.serialize() 51 | task_kwargs = { 52 | "key_id": id, 53 | "data": data, 54 | } 55 | self.task_socket.send_string(str({ 56 | "task": "decrypt", 57 | "task_kwargs": task_kwargs 58 | })) 59 | r = self.task_socket.recv() 60 | try: 61 | # print("Hello.........................") 62 | out = syft.tensor.TensorBase.deserialize(r) 63 | except Exception as e: 64 | # print("Hello.........................") 65 | print(e) 66 | out = float(r) 67 | return out 68 | 69 | 70 | class MPCCapsuleClient(): 71 | def __init__(self, repo): 72 | self.id = str(random.randint(0, 2**32)) 73 | self.repo = repo 74 | ctx = zmq.Context() 75 | self.task_socket = ctx.socket(zmq.REQ) 76 | self.task_socket.connect('tcp://127.0.0.1:5003') 77 | 78 | def create_siblings(self): 79 | """ 80 | This works with syft.mpc.rss.MPCRepo. 81 | """ 82 | data = pickle.dumps(self.repo) 83 | task_kwargs = { 84 | "key_id": self.id, 85 | "data": data, 86 | } 87 | self.task_socket.send_string(str({ 88 | "task": "create_siblings", 89 | "task_kwargs": task_kwargs 90 | })) 91 | r = self.task_socket.recv() 92 | self.repo1 = pickle.loads(r) 93 | return self.repo1 94 | 95 | def create_parties(self): 96 | """ 97 | This works with syft.mpc.spdz.MPCRepo. 98 | """ 99 | data = pickle.dumps(self.repo) 100 | task_kwargs = { 101 | "key_id": self.id, 102 | "data": data, 103 | } 104 | self.task_socket.send_string(str({ 105 | "task": "create_parties", 106 | "task_kwargs": task_kwargs 107 | })) 108 | r = self.task_socket.recv() 109 | self.repo1 = pickle.loads(r) 110 | return self.repo1 111 | 112 | def save(self, repo1): 113 | data = pickle.dumps(repo1) 114 | task_kwargs = { 115 | "key_id": self.id, 116 | "data": data, 117 | } 118 | self.task_socket.send_string(str({ 119 | "task": 'save_ints', 120 | "task_kwargs": task_kwargs 121 | })) 122 | self.task_socket.recv() 123 | return True 124 | 125 | def save_spdz(self, repo1): 126 | data = pickle.dumps(repo1) 127 | task_kwargs = { 128 | "key_id": self.id, 129 | "data": data, 130 | } 131 | self.task_socket.send_string(str({ 132 | "task": 'save_ints_spdz', 133 | "task_kwargs": task_kwargs 134 | })) 135 | self.task_socket.recv() 136 | return True 137 | -------------------------------------------------------------------------------- /capsule_mpc_server/local_server.py: -------------------------------------------------------------------------------- 1 | import zmq 2 | import tasks 3 | from ast import literal_eval 4 | # import logging 5 | 6 | context = zmq.Context() 7 | socket = context.socket(zmq.REP) 8 | socket.bind('tcp://127.0.0.1:5003') 9 | 10 | while True: 11 | try: 12 | task_data = socket.recv() 13 | task_data = literal_eval(task_data.decode('utf-8')) 14 | task = task_data.pop('task') 15 | task_kwargs = task_data.pop('task_kwargs') 16 | server_data = getattr(tasks, task)(**task_kwargs) 17 | if type(server_data) != bytes: 18 | socket.send_string(server_data) 19 | else: 20 | socket.send(server_data) 21 | except Exception as e: 22 | print(str(e)) 23 | 24 | socket.close() 25 | context.term() 26 | -------------------------------------------------------------------------------- /capsule_mpc_server/tasks.py: -------------------------------------------------------------------------------- 1 | # from flask import Flask, request, Response 2 | import os 3 | import pickle 4 | import redis 5 | from syft.mpc.rss import MPCRepo as RSSMPCRepo 6 | from syft.mpc.spdz import MPCRepo as SPDZMPCRepo 7 | 8 | redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379') 9 | conn = redis.from_url(redis_url) 10 | 11 | 12 | def create_siblings(key_id, data): 13 | repo = pickle.loads(data) 14 | bob = RSSMPCRepo() 15 | sam = RSSMPCRepo() 16 | bob.set_siblings(repo, sam) 17 | repo.set_siblings(sam, bob) 18 | sam.set_siblings(bob, repo) 19 | return pickle.dumps(repo) 20 | 21 | 22 | def create_parties(key_id, data): 23 | repo = pickle.loads(data) 24 | bob = SPDZMPCRepo() 25 | bob.set_parties(repo) 26 | repo.set_parties(bob) 27 | 28 | return pickle.dumps(repo) 29 | 30 | 31 | def save_ints(key_id, data): 32 | id = key_id 33 | repo = pickle.loads(data) 34 | bob = repo.siblings[0] 35 | sam = repo.siblings[1] 36 | save_siblings(id, conn, bob, sam) 37 | return ('True') 38 | 39 | 40 | def save_ints_spdz(key_id, data): 41 | id = key_id 42 | repo = pickle.loads(data) 43 | bob = repo.another_party[0] 44 | conn.set(id + '_bob', bob.ints) 45 | return ('True') 46 | 47 | 48 | def save_siblings(id, conn, bob, sam): 49 | conn.set(id + '_bob', bob.ints) 50 | conn.set(id + '_sam', sam.ints) 51 | -------------------------------------------------------------------------------- /capsule_zmq/local_server.py: -------------------------------------------------------------------------------- 1 | import zmq 2 | import tasks 3 | from ast import literal_eval 4 | # import logging 5 | 6 | context = zmq.Context() 7 | socket = context.socket(zmq.REP) 8 | socket.bind('tcp://127.0.0.1:5002') 9 | 10 | while True: 11 | try: 12 | task_data = socket.recv() 13 | task_data = literal_eval(task_data.decode('utf-8')) 14 | task = task_data.pop('task') 15 | task_kwargs = task_data.pop('task_kwargs') 16 | server_data = getattr(tasks, task)(**task_kwargs) 17 | if type(server_data) != bytes: 18 | socket.send_string(server_data) 19 | else: 20 | socket.send(server_data) 21 | 22 | except Exception as e: 23 | print(e) 24 | 25 | socket.close() 26 | context.term() 27 | -------------------------------------------------------------------------------- /capsule_zmq/tasks.py: -------------------------------------------------------------------------------- 1 | from syft.he.paillier.keys import KeyPair 2 | from syft.he.keys import Paillier 3 | import syft as sy 4 | # from flask import Flask, request, Response 5 | import os 6 | import redis 7 | 8 | 9 | redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379') 10 | conn = redis.from_url(redis_url) 11 | # app = Flask(__name__) 12 | 13 | 14 | def create_keys(id, scheme): 15 | if(scheme == 'paillier'): 16 | pk, sk = Paillier() 17 | save_keys(conn, id, pk, sk) 18 | return pk.serialize() 19 | else: 20 | return "Unknown Scheme:" + str(scheme) 21 | 22 | 23 | def bootstrap(key_id, data): 24 | cyphertext = sy.tensor.TensorBase.deserialize(data) 25 | pk, sk = get_keys(key_id) 26 | plaintext = cyphertext.decrypt(sk) 27 | clean_cyphertext = plaintext.encrypt(pk) 28 | return clean_cyphertext.serialize() 29 | 30 | 31 | def decrypt(key_id, data): 32 | cyphertext = sy.tensor.TensorBase.deserialize(data) 33 | pk, sk = get_keys(key_id) 34 | plaintext = cyphertext.decrypt(sk) 35 | try: 36 | b = plaintext.serialize() 37 | except Exception: 38 | b = str(plaintext) 39 | return b 40 | 41 | 42 | def save_keys(conn, id, pk, sk): 43 | conn.set(id + '_public', pk.serialize()) 44 | conn.set(id + '_private', sk.serialize()) 45 | 46 | 47 | def get_keys(id): 48 | pk_bin = conn.get(id + '_public') 49 | sk_bin = conn.get(id + '_private') 50 | pk, sk = KeyPair().deserialize(pk_bin, sk_bin) 51 | return (pk, sk) 52 | -------------------------------------------------------------------------------- /notebooks/.ipynb_checkpoints/Capsule - Basic Local Server Example-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Local Capsule Example\n", 8 | "\n", 9 | "To run this example\n", 10 | "\n", 11 | "- install redis by running \"brew install redis\"\n", 12 | "- start redis by running \"redis-server\"\n", 13 | "- install dependencies by running \"pip install -r requirements.txt\"\n", 14 | "- start local capsule server by running \"sh build_and_run.sh\"" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 15, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "from capsule.client import LocalCapsuleClient\n", 26 | "import syft as sy" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 20, 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "cc = LocalCapsuleClient()\n", 38 | "pk = cc.keygen()" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 32, 44 | "metadata": { 45 | "collapsed": true 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "x = pk.ones(10)\n", 50 | "y = sy.ones(10)*0.2" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 33, 56 | "metadata": { 57 | "collapsed": true 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "out = x.dot(y)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 34, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/plain": [ 72 | "BaseTensor: array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" 73 | ] 74 | }, 75 | "execution_count": 34, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "x.decrypt(cc)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 35, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "BaseTensor: array([ 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2])" 93 | ] 94 | }, 95 | "execution_count": 35, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "y.decrypt(cc)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 36, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "BaseTensor: array(2.0)" 113 | ] 114 | }, 115 | "execution_count": 36, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "out.decrypt(cc)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 37, 127 | "metadata": { 128 | "collapsed": true 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "out = cc.bootstrap(x)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 38, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "PaillierTensor: array([e, e, e, e, e, e, e, e, e, e], dtype=object)" 144 | ] 145 | }, 146 | "execution_count": 38, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "out" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": null, 158 | "metadata": { 159 | "collapsed": true 160 | }, 161 | "outputs": [], 162 | "source": [] 163 | } 164 | ], 165 | "metadata": { 166 | "kernelspec": { 167 | "display_name": "Python 3", 168 | "language": "python", 169 | "name": "python3" 170 | }, 171 | "language_info": { 172 | "codemirror_mode": { 173 | "name": "ipython", 174 | "version": 3 175 | }, 176 | "file_extension": ".py", 177 | "mimetype": "text/x-python", 178 | "name": "python", 179 | "nbconvert_exporter": "python", 180 | "pygments_lexer": "ipython3", 181 | "version": "3.6.1" 182 | } 183 | }, 184 | "nbformat": 4, 185 | "nbformat_minor": 2 186 | } 187 | -------------------------------------------------------------------------------- /notebooks/.ipynb_checkpoints/Capsule MPC-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 13, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "from syft.mpc.spdz import MPCRepo as SPDZRepo\n", 13 | "from syft.mpc.spdz.tensor import SPDZMPCTensor\n", 14 | "\n", 15 | "from capsule.zmq_client import MPCCapsuleClient" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 14, 21 | "metadata": { 22 | "collapsed": true 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "client = MPCCapsuleClient(SPDZRepo())\n", 27 | "alice = client.create_parties()" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 15, 33 | "metadata": { 34 | "scrolled": true 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "[]" 41 | ] 42 | }, 43 | "execution_count": 15, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "alice.another_party" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 16, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "ename": "AttributeError", 59 | "evalue": "'MPCCapsuleClient' object has no attribute 'save_spdz'", 60 | "output_type": "error", 61 | "traceback": [ 62 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 63 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 64 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSPDZMPCTensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0malice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave_spdz\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0malice\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Necessary to call save() for saving bob and sam data in redis server\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 65 | "\u001b[0;31mAttributeError\u001b[0m: 'MPCCapsuleClient' object has no attribute 'save_spdz'" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "r = SPDZMPCTensor(alice, np.array([1,2,3,4,5]))\n", 71 | "client.save_spdz(alice) # Necessary to call save() for saving bob and sam data in redis server" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "r = r + r\n", 81 | "r" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "client.save_spdz(alice)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": { 97 | "collapsed": true 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "r*r" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": { 108 | "collapsed": true 109 | }, 110 | "outputs": [], 111 | "source": [ 112 | "r - r" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": { 119 | "collapsed": true 120 | }, 121 | "outputs": [], 122 | "source": [ 123 | "r.dot(r)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": { 130 | "collapsed": true 131 | }, 132 | "outputs": [], 133 | "source": [ 134 | "r.sum()" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "metadata": { 141 | "collapsed": true 142 | }, 143 | "outputs": [], 144 | "source": [] 145 | } 146 | ], 147 | "metadata": { 148 | "kernelspec": { 149 | "display_name": "Python 3", 150 | "language": "python", 151 | "name": "python3" 152 | }, 153 | "language_info": { 154 | "codemirror_mode": { 155 | "name": "ipython", 156 | "version": 3 157 | }, 158 | "file_extension": ".py", 159 | "mimetype": "text/x-python", 160 | "name": "python", 161 | "nbconvert_exporter": "python", 162 | "pygments_lexer": "ipython3", 163 | "version": "3.6.3" 164 | } 165 | }, 166 | "nbformat": 4, 167 | "nbformat_minor": 2 168 | } 169 | -------------------------------------------------------------------------------- /notebooks/.ipynb_checkpoints/Capsule with Django-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Local Capsule Example Using Django\n", 8 | "\n", 9 | "To run this example\n", 10 | "\n", 11 | "- install redis by running \"brew install redis\"\n", 12 | "- start redis by running \"redis-server\"\n", 13 | "- install dependencies by running \"pip install -r requirements.txt\"\n", 14 | "- start local capsule server by running \"sh build_and_run_django.sh\"" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 33, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "from capsule.django_client import LocalDjangoCapsuleClient\n", 26 | "import syft as sy" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 34, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "cc = LocalDjangoCapsuleClient()\n", 36 | "pk = cc.keygen()" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 35, 42 | "metadata": { 43 | "collapsed": true 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "x = pk.ones(10)\n", 48 | "y = sy.ones(10)*0.2" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 36, 54 | "metadata": { 55 | "collapsed": true 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "out = x.dot(y)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 37, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "BaseTensor: array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" 71 | ] 72 | }, 73 | "execution_count": 37, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "x.decrypt(cc)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 38, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "BaseTensor: array([ 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2])" 91 | ] 92 | }, 93 | "execution_count": 38, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "y.decrypt(cc)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 39, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "BaseTensor: array(2.0)" 111 | ] 112 | }, 113 | "execution_count": 39, 114 | "metadata": {}, 115 | "output_type": "execute_result" 116 | } 117 | ], 118 | "source": [ 119 | "out.decrypt(cc)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 40, 125 | "metadata": { 126 | "collapsed": true 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "out = cc.bootstrap(x)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 41, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "PaillierTensor: array([e, e, e, e, e, e, e, e, e, e], dtype=object)" 142 | ] 143 | }, 144 | "execution_count": 41, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "out" 151 | ] 152 | } 153 | ], 154 | "metadata": { 155 | "kernelspec": { 156 | "display_name": "capsule_kernel", 157 | "language": "python", 158 | "name": "capsule_kernel" 159 | }, 160 | "language_info": { 161 | "codemirror_mode": { 162 | "name": "ipython", 163 | "version": 3 164 | }, 165 | "file_extension": ".py", 166 | "mimetype": "text/x-python", 167 | "name": "python", 168 | "nbconvert_exporter": "python", 169 | "pygments_lexer": "ipython3", 170 | "version": "3.6.1" 171 | } 172 | }, 173 | "nbformat": 4, 174 | "nbformat_minor": 2 175 | } 176 | -------------------------------------------------------------------------------- /notebooks/.ipynb_checkpoints/Capsule with ZMQ-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Local Capsule Example Using Django\n", 8 | "\n", 9 | "To run this example\n", 10 | "\n", 11 | "- install redis by running \"brew install redis\"\n", 12 | "- start redis by running \"redis-server\"\n", 13 | "- install dependencies by running \"pip install -r requirements.txt\"\n", 14 | "- start local capsule server by running \"sh build_and_run_zmq.sh\"" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "from capsule.zmq_client import LocalCapsuleClient\n", 26 | "import syft as sy" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 2, 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "cc = LocalCapsuleClient()\n", 38 | "pk = cc.keygen()" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 3, 44 | "metadata": { 45 | "collapsed": true 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "x = pk.ones(10)\n", 50 | "y = sy.ones(10)*0.2" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 10, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "PaillierTensor: array([e, e, e, e, e, e, e, e, e, e], dtype=object)" 62 | ] 63 | }, 64 | "execution_count": 10, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "x # Encrpyted Tensor" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": { 77 | "collapsed": true 78 | }, 79 | "outputs": [], 80 | "source": [ 81 | "y # Encrpyted Tensor" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 14, 87 | "metadata": { 88 | "scrolled": true 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "# Computations on encrypted tensor\n", 93 | "x = x + 2 \n", 94 | "y = y*2" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 15, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "BaseTensor: array([ 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.])" 106 | ] 107 | }, 108 | "execution_count": 15, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "x.decrypt(cc)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": { 121 | "collapsed": true 122 | }, 123 | "outputs": [], 124 | "source": [ 125 | "y.decrypt(cc)" 126 | ] 127 | } 128 | ], 129 | "metadata": { 130 | "kernelspec": { 131 | "display_name": "Python 3", 132 | "language": "python", 133 | "name": "python3" 134 | }, 135 | "language_info": { 136 | "codemirror_mode": { 137 | "name": "ipython", 138 | "version": 3 139 | }, 140 | "file_extension": ".py", 141 | "mimetype": "text/x-python", 142 | "name": "python", 143 | "nbconvert_exporter": "python", 144 | "pygments_lexer": "ipython3", 145 | "version": "3.6.1" 146 | } 147 | }, 148 | "nbformat": 4, 149 | "nbformat_minor": 2 150 | } 151 | -------------------------------------------------------------------------------- /notebooks/Capsule with RSSMPCTensor.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "from syft.mpc.rss import MPCRepo as RSSRepo\n", 11 | "from syft.mpc.rss.tensor import RSSMPCTensor\n", 12 | "\n", 13 | "from capsule.zmq_client import MPCCapsuleClient" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 3, 19 | "metadata": { 20 | "collapsed": true 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "client = MPCCapsuleClient(RSSRepo())\n", 25 | "alice = client.create_siblings()" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 4, 31 | "metadata": { 32 | "scrolled": true 33 | }, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "[,\n", 39 | " ]" 40 | ] 41 | }, 42 | "execution_count": 4, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "alice.siblings" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 5, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "True" 60 | ] 61 | }, 62 | "execution_count": 5, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "r = RSSMPCTensor(alice, np.array([1,2,3,4,5]))\n", 69 | "client.save(alice) # Necessary to call save() for saving bob and sam data in redis server" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 6, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "RSSMPCTensor: array([2.0, 4.0, 6.0, 8.0, 10.0], dtype=object)" 81 | ] 82 | }, 83 | "execution_count": 6, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "r = r + r\n", 90 | "r" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 7, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "True" 102 | ] 103 | }, 104 | "execution_count": 7, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "client.save(alice)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 8, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "RSSMPCTensor: array([4.0, 16.0, 36.0, 64.0, 100.0], dtype=object)" 122 | ] 123 | }, 124 | "execution_count": 8, 125 | "metadata": {}, 126 | "output_type": "execute_result" 127 | } 128 | ], 129 | "source": [ 130 | "r*r" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 9, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "RSSMPCTensor: array([0, 0, 0, 0, 0], dtype=object)" 142 | ] 143 | }, 144 | "execution_count": 9, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "r - r" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 10, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "data": { 160 | "text/plain": [ 161 | "220.0" 162 | ] 163 | }, 164 | "execution_count": 10, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "r.dot(r)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 11, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "30.0" 182 | ] 183 | }, 184 | "execution_count": 11, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "r.sum()" 191 | ] 192 | } 193 | ], 194 | "metadata": { 195 | "kernelspec": { 196 | "display_name": "Python 3", 197 | "language": "python", 198 | "name": "python3" 199 | }, 200 | "language_info": { 201 | "codemirror_mode": { 202 | "name": "ipython", 203 | "version": 3 204 | }, 205 | "file_extension": ".py", 206 | "mimetype": "text/x-python", 207 | "name": "python", 208 | "nbconvert_exporter": "python", 209 | "pygments_lexer": "ipython3", 210 | "version": "3.6.3" 211 | } 212 | }, 213 | "nbformat": 4, 214 | "nbformat_minor": 2 215 | } 216 | -------------------------------------------------------------------------------- /notebooks/Capsule with SPDZMPCTensor.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "from syft.mpc.spdz import MPCRepo as SPDZRepo\n", 13 | "from syft.mpc.spdz.tensor import SPDZMPCTensor\n", 14 | "\n", 15 | "from capsule.zmq_client import MPCCapsuleClient" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 2, 21 | "metadata": { 22 | "collapsed": true 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "client = MPCCapsuleClient(SPDZRepo())\n", 27 | "alice = client.create_parties()" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 3, 33 | "metadata": { 34 | "scrolled": true 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "[]" 41 | ] 42 | }, 43 | "execution_count": 3, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "alice.another_party" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 4, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "True" 61 | ] 62 | }, 63 | "execution_count": 4, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "r = SPDZMPCTensor(alice, np.array([1,2,3,4,5]))\n", 70 | "client.save_spdz(alice) # Necessary to call save() for saving bob data in redis server" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 5, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "SPDZMPCTensor: array([2.0, 4.0, 6.0, 8.0, 10.0], dtype=object)" 82 | ] 83 | }, 84 | "execution_count": 5, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "r = r + r\n", 91 | "r" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 6, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "True" 103 | ] 104 | }, 105 | "execution_count": 6, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "client.save_spdz(alice)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 7, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "SPDZMPCTensor: array([4.0, 16.0, 36.0, 64.0, 100.0], dtype=object)" 123 | ] 124 | }, 125 | "execution_count": 7, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "r*r" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 8, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "SPDZMPCTensor: array([0, 0, 0, 0, 0], dtype=object)" 143 | ] 144 | }, 145 | "execution_count": 8, 146 | "metadata": {}, 147 | "output_type": "execute_result" 148 | } 149 | ], 150 | "source": [ 151 | "r - r" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 9, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "data": { 161 | "text/plain": [ 162 | "220.0" 163 | ] 164 | }, 165 | "execution_count": 9, 166 | "metadata": {}, 167 | "output_type": "execute_result" 168 | } 169 | ], 170 | "source": [ 171 | "r.dot(r)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 10, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "data": { 181 | "text/plain": [ 182 | "30.0" 183 | ] 184 | }, 185 | "execution_count": 10, 186 | "metadata": {}, 187 | "output_type": "execute_result" 188 | } 189 | ], 190 | "source": [ 191 | "r.sum()" 192 | ] 193 | } 194 | ], 195 | "metadata": { 196 | "kernelspec": { 197 | "display_name": "Python 3", 198 | "language": "python", 199 | "name": "python3" 200 | }, 201 | "language_info": { 202 | "codemirror_mode": { 203 | "name": "ipython", 204 | "version": 3 205 | }, 206 | "file_extension": ".py", 207 | "mimetype": "text/x-python", 208 | "name": "python", 209 | "nbconvert_exporter": "python", 210 | "pygments_lexer": "ipython3", 211 | "version": "3.6.3" 212 | } 213 | }, 214 | "nbformat": 4, 215 | "nbformat_minor": 2 216 | } 217 | -------------------------------------------------------------------------------- /notebooks/Capsule with ZMQ.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Local Capsule Example Using Django\n", 8 | "\n", 9 | "To run this example\n", 10 | "\n", 11 | "- install redis by running \"brew install redis\"\n", 12 | "- start redis by running \"redis-server\"\n", 13 | "- install dependencies by running \"pip install -r requirements.txt\"\n", 14 | "- start local capsule server by running \"sh build_and_run_zmq.sh\"" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 9, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "from capsule.zmq_client import LocalCapsuleClient\n", 26 | "import syft as sy" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 10, 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "cc = LocalCapsuleClient()\n", 38 | "pk = cc.keygen()" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 11, 44 | "metadata": { 45 | "collapsed": true 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "x = pk.ones(10)\n", 50 | "y = sy.ones(10)*0.2" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 12, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "PaillierTensor: array([e, e, e, e, e, e, e, e, e, e], dtype=object)" 62 | ] 63 | }, 64 | "execution_count": 12, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "x # Encrpyted Tensor" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 13, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "BaseTensor: array([ 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2])" 82 | ] 83 | }, 84 | "execution_count": 13, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "y # Encrpyted Tensor" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 14, 96 | "metadata": { 97 | "collapsed": true, 98 | "scrolled": true 99 | }, 100 | "outputs": [], 101 | "source": [ 102 | "# Computations on encrypted tensor\n", 103 | "x = x + 2 \n", 104 | "y = y*2" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 15, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "BaseTensor: array([ 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.])" 116 | ] 117 | }, 118 | "execution_count": 15, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "x.decrypt(cc)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 16, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "data": { 134 | "text/plain": [ 135 | "BaseTensor: array([ 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4])" 136 | ] 137 | }, 138 | "execution_count": 16, 139 | "metadata": {}, 140 | "output_type": "execute_result" 141 | } 142 | ], 143 | "source": [ 144 | "y.decrypt(cc)" 145 | ] 146 | } 147 | ], 148 | "metadata": { 149 | "kernelspec": { 150 | "display_name": "Python 3", 151 | "language": "python", 152 | "name": "python3" 153 | }, 154 | "language_info": { 155 | "codemirror_mode": { 156 | "name": "ipython", 157 | "version": 3 158 | }, 159 | "file_extension": ".py", 160 | "mimetype": "text/x-python", 161 | "name": "python", 162 | "nbconvert_exporter": "python", 163 | "pygments_lexer": "ipython3", 164 | "version": "3.6.1" 165 | } 166 | }, 167 | "nbformat": 4, 168 | "nbformat_minor": 2 169 | } 170 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | redis 2 | requests 3 | pytest-flake8 4 | pytest 5 | pyzmq 6 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [aliases] 2 | test=pytest 3 | 4 | [flake8] 5 | ignore = E501 6 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from setuptools import setup 3 | 4 | # Utility function to read the README file. 5 | # Used for the long_description. It's nice, because now 1) we have a top level 6 | # README file and 2) it's easier to type in the README file than to put a raw 7 | # string in below ... 8 | 9 | 10 | def read(fname): 11 | return open(os.path.join(os.path.dirname(__file__), fname)).read() 12 | 13 | 14 | setup( 15 | name="capsule", 16 | version="0.1.0", 17 | author="Amber Trask", 18 | author_email="contact@openmined.org", 19 | description=( 20 | "A library for time released public/private homomorphic \ 21 | encryption keys." 22 | ), 23 | license="Apache-2.0", 24 | keywords="deep learning machine artificial intelligence \ 25 | homomorphic encryption", 26 | packages=['capsule'], 27 | long_description=read('README.md'), 28 | classifiers=[ 29 | "Development Status :: 1 - Alpha", 30 | ], 31 | ) 32 | -------------------------------------------------------------------------------- /test-requirements.txt: -------------------------------------------------------------------------------- 1 | -r ./requirements.txt 2 | 3 | pytest 4 | pytest-flake8 5 | --------------------------------------------------------------------------------