Exemplo n.º 1
0
def say_hi():
    patient_addresses = repo.web3.eth.accounts[1:10]
    anonymous_diabetics = list(zip(patient_addresses,
                               anonymous_diabetes_users[0],
                               anonymous_diabetes_users[1]))
    cure_diabetes_inc = repo.web3.eth.accounts[1]
    # pubkey,prikey = KeyPair().generate(n_length=1024)
    pubkey, prikey = "hello", "world"
    diabetes_classifier = LinearClassifier(desc="DiabetesClassifier",n_inputs=10,n_labels=1)
    initial_error = diabetes_classifier.evaluate(validation[0],validation[1])
    diabetes_classifier.encrypt(pubkey)

    diabetes_model = Model(owner=cure_diabetes_inc,
                        syft_obj = diabetes_classifier,
                        bounty = 1,
                        initial_error = initial_error,
                        target_error = 10000
                        )
    model_id = repo.submit_model(diabetes_model)
    model = repo[model_id]
    diabetic_address,input_data,target_data = anonymous_diabetics[0]
    repo[model_id].submit_gradient(diabetic_address,input_data,target_data)
    print(repo[model_id])
    old_balance = get_balance(diabetic_address)
    print(old_balance)
    new_error = repo[model_id].evaluate_gradient(cure_diabetes_inc,repo[model_id][0],prikey,pubkey,validation[0],validation[1])
    print(new_error)
    new_balance = get_balance(diabetic_address)
    incentive = new_balance - old_balance
    print(incentive)
    return jsonify({'initial_error':initial_error,'old_balance':old_balance, 
                'new_balance': new_balance,
                'new_error':new_error, 'incentive':incentive})
Exemplo n.º 2
0
    def model_training_demo_notebook(self):
        """If this test fails, you probably broke the demo notebook located at
        PySonar/notebooks/Sonar - Decentralized Model Training Simulation
        (local blockchain).ipynb """
        c = LocalDjangoCapsuleClient()
        d = LinearClassifier(desc="DiabetesClassifier", n_inputs=10, n_labels=1, capsule_client=c)
        d.encrypt()

        self.assertTrue(True)
Exemplo n.º 3
0
    def modelTrainingDemoNotebook(self):
        """If this test fails, you probably broke the demo notebook located at
        PySonar/notebooks/Sonar - Decentralized Model Training Simulation
        (local blockchain).ipynb """

        pubkey, prikey = KeyPair().generate(n_length=1024)
        d = LinearClassifier(desc="DiabetesClassifier",
                             n_inputs=10,
                             n_labels=1)
        d.encrypt(pubkey)

        self.assertTrue(True)
Exemplo n.º 4
0
    def test_paillier_linear_classifier_notebook(self):
        """If this test fails, you probably broke the demo notebook located at
        PySyft/notebooks/Syft - Paillier Homomorphic Encryption Example.ipynb
        """

        capsule = LocalDjangoCapsuleClient()
        model = LinearClassifier(capsule_client=capsule)
        assert (model.capsule == capsule)

        try:
            model = model.encrypt()
            encrypted = True
        except Exception as e:
            encrypted = False
            print('[!]', e)

        input = np.array([[0, 0, 1, 1], [0, 0, 1, 0], [1, 0, 1, 1],
                          [0, 0, 1, 0]])
        target = np.array([[0, 1], [0, 0], [1, 1], [0, 0]])

        for iter in range(3):
            model.learn(input, target, alpha=0.5)

        if encrypted:
            model = model.decrypt()

        for i in range(len(input)):
            model.forward(input[i])
Exemplo n.º 5
0
    def paillierLinearClassifierNotebook(self):
        """If this test fails, you probably broke the demo notebook located at
        PySyft/notebooks/Syft - Paillier Homomorphic Encryption Example.ipynb
        """

        pubkey, prikey = KeyPair().generate(n_length=1024)
        model = LinearClassifier(n_inputs=4, n_labels=2).encrypt(pubkey)
        input = np.array([[0, 0, 1, 1], [0, 0, 1, 0], [1, 0, 1, 1],
                          [0, 0, 1, 0]])
        target = np.array([[0, 1], [0, 0], [1, 1], [0, 0]])

        for iter in range(3):
            for i in range(len(input)):
                model.learn(input=input[i], target=target[i], alpha=0.5)

        model = model.decrypt(prikey)
        for i in range(len(input)):
            model.forward(input[i])
Exemplo n.º 6
0
# we're going to set aside 10 accounts for our 42 patients
# Let's go ahead and pair each data point with each patient's
# address so that we know we don't get them confused
patient_addresses = repo.web3.eth.accounts[1:10]
anonymous_diabetics = list(
    zip(patient_addresses, anonymous_diabetes_users[0],
        anonymous_diabetes_users[1]))

# we're going to set aside 1 account for Cure Diabetes Inc
cure_diabetes_inc = repo.web3.eth.accounts[1]
agg_addr = repo.web3.eth.accounts[2]

pubkey, prikey = KeyPair().generate(n_length=1024)
#pubkey,prikey=paillier.paillier.generate_paillier_keypair()
diabetes_classifier = LinearClassifier(desc="DiabetesClassifier",
                                       n_inputs=30,
                                       n_labels=2)
initial_error = diabetes_classifier.evaluate(validation[0], validation[1])
start_modelencryption = time.time()
diabetes_classifier.encrypt(pubkey)
end_modelencryption = time.time()
print('modelencryption time', end_modelencryption - start_modelencryption)
start_prikeyshare_gen = time.time()
s1, s2 = paillier.paillier.genKeyShares(prikey.sk, pubkey.pk)
st = SecretKey(s1)
sab = SecretKey(s2)
s3, s4 = paillier.paillier.genKeyShares(s2, pubkey.pk)
sa = SecretKey(s3)
scb = SecretKey(s4)
end_prikeyshare_gen = time.time()
print('keygentime', end_prikeyshare_gen - start_prikeyshare_gen)
Exemplo n.º 7
0
# we're going to set aside 10 accounts for our 42 patients
# Let's go ahead and pair each data point with each patient's
# address so that we know we don't get them confused
patient_addresses = repo.web3.eth.accounts[2:11]
#anonymous_diabetics = list(zip(cycle(patient_addresses),anonymous_diabetes_users[0],anonymous_diabetes_users[1]))
#print("validation[0].size",len(anonymous_diabetics))
#anonymous_diabetics=list(zip(cycle(patient_addresses),anonymous_diabetes_users[0],anonymous_diabetes_users[1]))

# we're going to set aside 1 account for Cure Diabetes Inc
cure_diabetes_inc = repo.web3.eth.accounts[0]
agg_addr = repo.web3.eth.accounts[1]

pubkey, prikey = KeyPair().generate(n_length=1024)
#pubkey,prikey=paillier.paillier.generate_paillier_keypair()
diabetes_classifier = LinearClassifier(desc="DiabetesClassifier",
                                       n_inputs=30,
                                       n_labels=1)
#initial_error = diabetes_classifier.evaluate(validation[0],validation[1])
#diabetes_classifier.encrypt(pubkey)
s1, s2 = paillier.paillier.genKeyShares(prikey.sk, pubkey.pk)
st = SecretKey(s1)
sab = SecretKey(s2)
s3, s4 = paillier.paillier.genKeyShares(s2, pubkey.pk)
sa = SecretKey(s3)
scb = SecretKey(s4)

diabetes_model = Model(owner=cure_diabetes_inc,
                       syft_obj=diabetes_classifier,
                       bounty=10,
                       initial_error=210,
                       target_error=0,
Exemplo n.º 8
0
# we're going to set aside 10 accounts for our 42 patients
# Let's go ahead and pair each data point with each patient's
# address so that we know we don't get them confused
patient_addresses = repo.web3.eth.accounts[2:11]
anonymous_patients = list(
    zip(patient_addresses, anonymous_users[0], anonymous_users[1]))

# we're going to set aside 1 account for Cure Cancer Inc
cure_cancer_inc = repo.web3.eth.accounts[0]
agg_addr = repo.web3.eth.accounts[1]

pubkey, prikey = KeyPair().generate(n_length=2048)
#pubkey,prikey=paillier.paillier.generate_paillier_keypair()
cancer_classifier = LinearClassifier(desc="CancerClassifier",
                                     n_inputs=30,
                                     n_labels=1)
initial_error = cancer_classifier.evaluate(validation[0], validation[1])
start_modelencryption = time.time()
cancer_classifier.encrypt(pubkey)
end_modelencryption = time.time()
print('modelencryption time', end_modelencryption - start_modelencryption)
start_prikeyshare_gen = time.time()
s1, s2 = paillier.paillier.genKeyShares(prikey.sk, pubkey.pk)
st = SecretKey(s1)
sab = SecretKey(s2)
s3, s4 = paillier.paillier.genKeyShares(s2, pubkey.pk)
sa = SecretKey(s3)
scb = SecretKey(s4)
end_prikeyshare_gen = time.time()
print('keygentime', end_prikeyshare_gen - start_prikeyshare_gen)