Ejemplo n.º 1
0
 def binop_test(self, binop):
     for i in range(ITERATIONS):
         a = self.rand()
         b = self.rand()
         self.assertAlmostEqual(decrypt(binop(encrypt(a), encrypt(b))),
                                binop(a, b),
                                places=3)
Ejemplo n.º 2
0
 def test_running_sum(self):
     true = 0
     target = 0
     for i in range(ITERATIONS):
         x = self.rand()
         true += x
         target += encrypt(x)
     self.assertAlmostEqual(decrypt(target), true, places=3)
Ejemplo n.º 3
0
    generate_keypair,
    set_public_key, set_private_key, set_relin_keys,
    display_config
)

# In a real application, the keypair would be generated once,
# and only the public key would be provided to the server.
# A more realistic example is given later.
display_config()
public_key, private_key, relin_keys = generate_keypair()
set_public_key(public_key)
set_relin_keys(relin_keys)
display_config()

set_private_key(private_key)

display_config()


# The server
def process(x):
    return x**3 - 3*x + 1


# The client
sensitive_data = [-30, -5, 17, 28]
for entry in sensitive_data:
    encrypted = encrypt(entry) # Encrypt the data...
    result = process(encrypted) # Process the encrypted data on the server...
    print(entry, decrypt(result)) # Decrypt the result on the client.
Ejemplo n.º 4
0
 def test_pow(self):
     for i in range(ITERATIONS):
         a = self.rand() / 500
         b = random.randint(0, 4)
         self.assertAlmostEqual(decrypt(encrypt(a)**b), a**b, places=3)
Ejemplo n.º 5
0
 def test_div(self):
     for i in range(ITERATIONS):
         a = self.rand()
         b = (2 * random.randint(0, 1) - 1) * random.uniform(1, 100)
         self.assertAlmostEqual(decrypt(encrypt(a) / b), a / b, places=3)
Ejemplo n.º 6
0
 def test_pow(self):
     for i in range(ITERATIONS):
         a = random.randint(-7, 7)
         b = random.randint(0, 6)
         self.assertEqual(decrypt(encrypt(a)**b), a**b)
Ejemplo n.º 7
0
 def binop_test(self, binop):
     for i in range(ITERATIONS):
         a = self.randint()
         b = self.randint()
         self.assertEqual(decrypt(binop(encrypt(a), encrypt(b))),
                          binop(a, b))
Ejemplo n.º 8
0
from simplefhe import (load_private_key, load_relin_keys, display_config,
                       decrypt, load_encrypted_value)

# Note: this is the only step at which the private key is used!
load_private_key('keys/private.key')
load_relin_keys('keys/relin.key')
display_config()

# Decrypt results from the server (client-side)
sensitive_data = [-30, -5, 17, 28]

for i, entry in enumerate(sensitive_data):
    encrypted = load_encrypted_value(f'outputs/{i}.dat')
    result = decrypt(encrypted)
    print(f'[CLIENT] Result for {entry}: {result}')
Ejemplo n.º 9
0
from simplefhe import (encrypt, decrypt, generate_keypair, set_public_key,
                       set_private_key, set_relin_keys, initialize,
                       display_config)

initialize('int')

public_key, private_key, relin_key = generate_keypair()
set_private_key(private_key)
set_public_key(public_key)
set_relin_keys(relin_key)

display_config()


# The server
def process(x):
    return x**21


# The client
sensitive_data = [-3, 1, 3, 10]
for entry in sensitive_data:
    insecure_result = process(entry)
    secure_result = decrypt(process(encrypt(entry)))
    print(entry, insecure_result, secure_result)
Ejemplo n.º 10
0
 def test_plaintext_init(self):
     N = 109023
     a = EncryptedValue(N)
     b = EncryptedValue(a)
     self.assertEqual(decrypt(a), N)
     self.assertEqual(decrypt(b), N)
Ejemplo n.º 11
0
# Client-side script to decrypt the server's results
import numpy as np

from simplefhe import (initialize, decrypt, load_encrypted_value,
                       load_private_key, load_relin_keys)

# Initialization and keys
initialize('float')
load_private_key('keys/private.key')
load_relin_keys('keys/relin.key')

# Decrypt server's results
XtX = np.zeros(shape=[3, 3])
XtY = np.zeros(shape=3)
for i in range(3):
    XtY[i] = decrypt(load_encrypted_value(f'outputs/XtY-{i}.dat'))
    for j in range(3):
        XtX[i, j] = decrypt(load_encrypted_value(f'outputs/XtX-{i}-{j}.dat'))

# Some post-processing
coefficients = np.linalg.inv(XtX) @ XtY

# Display results
GROUND_TRUTH = [3.2, -1.7, 0.8]
for i, pair in enumerate(zip(GROUND_TRUTH, coefficients)):
    a, b = pair
    print(f'Coefficient {i}: Expected {a:.4f}, Computed {b:.4f}')