Skip to content

Commit

Permalink
#6 Command to add encrypted number to unencrypted number + tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hardbyte committed Jan 11, 2016
1 parent d013d87 commit b456515
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 8 deletions.
25 changes: 25 additions & 0 deletions phe/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ def add_encrypted(public, encrypted_a, encrypted_b, output):
print(serialised_result, file=output)


@cli.command("add")
@click.argument('public', type=click.File('r'))
@click.argument('encrypted', type=click.File('r'))
@click.argument('plaintext', type=str)
@click.option('--output', type=click.File('w'),
help="Save to file instead of stdout")
def add_encrypted_to_plaintext(public, encrypted, plaintext, output):
"""Add an encrypted number to a non encrypted number.
"""
log("Loading public key")
publickeydata = json.load(public)
pub = load_public_key(publickeydata)

log("Loading encrypted number")
enc = load_encrypted_number(encrypted, pub)

log("Loading unencrypted number")
num = float(plaintext)

log("Adding")

enc_result = enc + num
serialised_result = serialise_encrypted(enc_result)
print(serialised_result, file=output)


if __name__ == "__main__":
Expand Down
91 changes: 83 additions & 8 deletions phe/tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def setUp(self):
self.enc_b_file = tempfile.NamedTemporaryFile()
self.enc_result_file = tempfile.NamedTemporaryFile()

def adder_helper(self, a, b):
def encrypt_and_add(self, a, b):
self.runner.invoke(cli,
['encrypt', self.public_keyfile.name, '--output', self.enc_a_file.name, '--', str(a)])
self.runner.invoke(cli,
Expand All @@ -222,41 +222,108 @@ def adder_helper(self, a, b):
out = outfile.read()
return float(out)

def encrypt_a_and_add_b(self, a, b):
self.runner.invoke(cli,
['encrypt', self.public_keyfile.name, '--output', self.enc_a_file.name, '--', str(a)])

result = self.runner.invoke(cli, [
'add',
'--output',
self.enc_result_file.name,
self.public_keyfile.name,
self.enc_a_file.name,
'--',
str(b)
])

assert result.exit_code == 0

with tempfile.NamedTemporaryFile() as outfile:
result = self.runner.invoke(cli, [
'decrypt', self.private_keyfile.name, self.enc_result_file.name, '--output', outfile.name
])
assert result.exit_code == 0

out = outfile.read()
return float(out)


class TestConsoleAddition(TestConsoleHelpers):

def test_addenc_int(self):
a, b = 12345, 6789
out = self.encrypt_and_add(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_add_int(self):
a, b = 12345, 6789
out = self.adder_helper(a, b)
out = self.encrypt_a_and_add_b(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_addenc_large_ints(self):
"""Test adding large integers.
"""
a, b = int(1.2e10), int(1e15)
out = self.encrypt_and_add(a, b)
self.assertAlmostEqual(float(a + b), float(out))


def test_add_large_ints(self):
"""Test adding large integers.
"""
a, b = int(1.2e10), int(1e15)
out = self.adder_helper(a, b)
out = self.encrypt_a_and_add_b(a, b)
self.assertAlmostEqual(float(a + b), float(out))


def test_addenc_signed_int(self):
a, b = 12345, -6789
out = self.encrypt_and_add(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_add_signed_int(self):
a, b = 12345, -6789
out = self.adder_helper(a, b)
out = self.encrypt_a_and_add_b(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_addenc_floats(self):
a, b = 123.45, 67.89
out = self.encrypt_and_add(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_add_floats(self):
a, b = 123.45, 67.89
out = self.adder_helper(a, b)
out = self.encrypt_a_and_add_b(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_addenc_large_floats(self):
"""Test adding large integers.
"""
a, b = 2.3e32, 1.4e32
out = self.encrypt_and_add(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_add_large_floats(self):
"""Test adding large integers.
"""
a, b = 2.3e32, 1.4e32
out = self.adder_helper(a, b)
out = self.encrypt_a_and_add_b(a, b)
self.assertAlmostEqual(float(a + b), float(out))


class TestFuzz(TestConsoleHelpers):

def test_addenc_random_ints(self):
"""Test adding random ints
"""
MAX = 1000000000000000
MIN = -MAX

for _ in range(20):
a, b = random.randrange(MIN, MAX), random.randrange(MIN, MAX)
out = self.encrypt_and_add(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_add_random_ints(self):
"""Test adding random ints
"""
Expand All @@ -265,15 +332,23 @@ def test_add_random_ints(self):

for _ in range(20):
a, b = random.randrange(MIN, MAX), random.randrange(MIN, MAX)
out = self.adder_helper(a, b)
out = self.encrypt_a_and_add_b(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_addenc_random_floats(self):
"""Test adding random floating point numbers from the range [0.0, 1.0)
"""
for _ in range(20):
a, b = random.random(), random.random()
out = self.encrypt_and_add(a, b)
self.assertAlmostEqual(float(a + b), float(out))

def test_add_random_floats(self):
"""Test adding random floating point numbers from the range [0.0, 1.0)
"""
for _ in range(20):
a, b = random.random(), random.random()
out = self.adder_helper(a, b)
out = self.encrypt_a_and_add_b(a, b)
self.assertAlmostEqual(float(a + b), float(out))


0 comments on commit b456515

Please sign in to comment.