From a6bf9abb91ad6576ce23e7f8731e74c2a73d2ea6 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Tue, 8 Jul 2025 16:14:00 +0100 Subject: Organize source into directory structure. * pyhegp/__init__.py: New file. * pyhegp.py: Move to pyhegp/pyhegp.py. * test_pyhegp.py: Move to tests/test_pyhegp.py. Import from pyhegp.pyhegp instead of from pyhegp. * pyproject.toml (project.scripts)[pyhegp]: Switch to pyhegp.pyhegp:main. --- pyhegp.py | 71 ---------------------------------------------------- pyhegp/__init__.py | 0 pyhegp/pyhegp.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- test_pyhegp.py | 45 --------------------------------- tests/test_pyhegp.py | 45 +++++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 117 deletions(-) delete mode 100644 pyhegp.py create mode 100644 pyhegp/__init__.py create mode 100644 pyhegp/pyhegp.py delete mode 100644 test_pyhegp.py create mode 100644 tests/test_pyhegp.py diff --git a/pyhegp.py b/pyhegp.py deleted file mode 100644 index 053ddaf..0000000 --- a/pyhegp.py +++ /dev/null @@ -1,71 +0,0 @@ -### pyhegp --- Homomorphic encryption of genotypes and phenotypes -### Copyright © 2025 Arun Isaac -### -### This file is part of pyhegp. -### -### pyhegp is free software: you can redistribute it and/or modify it -### under the terms of the GNU General Public License as published by -### the Free Software Foundation, either version 3 of the License, or -### (at your option) any later version. -### -### pyhegp is distributed in the hope that it will be useful, but -### WITHOUT ANY WARRANTY; without even the implied warranty of -### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -### General Public License for more details. -### -### You should have received a copy of the GNU General Public License -### along with pyhegp. If not, see . - -import click -import numpy as np -from scipy.stats import special_ortho_group - -def random_key(rng, n): - return special_ortho_group.rvs(n, random_state=rng) - -def standardize(genotype_matrix, maf): - m, _ = genotype_matrix.shape - return ((genotype_matrix - np.tile(maf, (m, 1))) - @ np.diag(1 / np.sqrt(2 * maf * (1 - maf)))) - -def hegp_encrypt(plaintext, maf, key): - return key @ plaintext - # FIXME: Add standardization. - # return key @ standardize(plaintext, maf) - -def hegp_decrypt(ciphertext, key): - return np.transpose(key) @ ciphertext - -def read_genotype(genotype_file): - return np.loadtxt(genotype_file, delimiter=",") - -@click.group() -def main(): - pass - -@main.command() -@click.argument("genotype-file", type=click.File("r")) -@click.argument("maf-file", type=click.File("r")) -@click.argument("key-path", type=click.Path()) -@click.argument("ciphertext-path", type=click.Path()) -def encrypt(genotype_file, maf_file, key_path, ciphertext_path): - genotype = read_genotype(genotype_file) - maf = np.loadtxt(maf_file) - rng = np.random.default_rng() - key = random_key(rng, len(genotype)) - encrypted_genotype = hegp_encrypt(genotype, maf, key) - np.savetxt(key_path, key, delimiter=",", fmt="%f") - np.savetxt(ciphertext_path, encrypted_genotype, delimiter=",", fmt="%f") - -@main.command() -@click.argument("key-file", type=click.File("r")) -@click.argument("ciphertext-file", type=click.File("r")) -@click.argument("plaintext-path", type=click.Path()) -def decrypt(key_file, ciphertext_file, plaintext_path): - key = np.loadtxt(key_file, delimiter=",") - ciphertext = np.loadtxt(ciphertext_file, delimiter=",") - genotype = hegp_decrypt(ciphertext, key) - np.savetxt(plaintext_path, genotype, delimiter=",", fmt="%f") - -if __name__ == "__main__": - main() diff --git a/pyhegp/__init__.py b/pyhegp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyhegp/pyhegp.py b/pyhegp/pyhegp.py new file mode 100644 index 0000000..053ddaf --- /dev/null +++ b/pyhegp/pyhegp.py @@ -0,0 +1,71 @@ +### pyhegp --- Homomorphic encryption of genotypes and phenotypes +### Copyright © 2025 Arun Isaac +### +### This file is part of pyhegp. +### +### pyhegp is free software: you can redistribute it and/or modify it +### under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### pyhegp is distributed in the hope that it will be useful, but +### WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +### General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with pyhegp. If not, see . + +import click +import numpy as np +from scipy.stats import special_ortho_group + +def random_key(rng, n): + return special_ortho_group.rvs(n, random_state=rng) + +def standardize(genotype_matrix, maf): + m, _ = genotype_matrix.shape + return ((genotype_matrix - np.tile(maf, (m, 1))) + @ np.diag(1 / np.sqrt(2 * maf * (1 - maf)))) + +def hegp_encrypt(plaintext, maf, key): + return key @ plaintext + # FIXME: Add standardization. + # return key @ standardize(plaintext, maf) + +def hegp_decrypt(ciphertext, key): + return np.transpose(key) @ ciphertext + +def read_genotype(genotype_file): + return np.loadtxt(genotype_file, delimiter=",") + +@click.group() +def main(): + pass + +@main.command() +@click.argument("genotype-file", type=click.File("r")) +@click.argument("maf-file", type=click.File("r")) +@click.argument("key-path", type=click.Path()) +@click.argument("ciphertext-path", type=click.Path()) +def encrypt(genotype_file, maf_file, key_path, ciphertext_path): + genotype = read_genotype(genotype_file) + maf = np.loadtxt(maf_file) + rng = np.random.default_rng() + key = random_key(rng, len(genotype)) + encrypted_genotype = hegp_encrypt(genotype, maf, key) + np.savetxt(key_path, key, delimiter=",", fmt="%f") + np.savetxt(ciphertext_path, encrypted_genotype, delimiter=",", fmt="%f") + +@main.command() +@click.argument("key-file", type=click.File("r")) +@click.argument("ciphertext-file", type=click.File("r")) +@click.argument("plaintext-path", type=click.Path()) +def decrypt(key_file, ciphertext_file, plaintext_path): + key = np.loadtxt(key_file, delimiter=",") + ciphertext = np.loadtxt(ciphertext_file, delimiter=",") + genotype = hegp_decrypt(ciphertext, key) + np.savetxt(plaintext_path, genotype, delimiter=",", fmt="%f") + +if __name__ == "__main__": + main() diff --git a/pyproject.toml b/pyproject.toml index 36fc07e..c0a6ab2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,4 +22,4 @@ dependencies = [ ] [project.scripts] -pyhegp = "pyhegp:main" +pyhegp = "pyhegp.pyhegp:main" diff --git a/test_pyhegp.py b/test_pyhegp.py deleted file mode 100644 index 19e58d2..0000000 --- a/test_pyhegp.py +++ /dev/null @@ -1,45 +0,0 @@ -### pyhegp --- Homomorphic encryption of genotypes and phenotypes -### Copyright © 2025 Arun Isaac -### -### This file is part of pyhegp. -### -### pyhegp is free software: you can redistribute it and/or modify it -### under the terms of the GNU General Public License as published by -### the Free Software Foundation, either version 3 of the License, or -### (at your option) any later version. -### -### pyhegp is distributed in the hope that it will be useful, but -### WITHOUT ANY WARRANTY; without even the implied warranty of -### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -### General Public License for more details. -### -### You should have received a copy of the GNU General Public License -### along with pyhegp. If not, see . - -from hypothesis import given, settings, strategies as st -from hypothesis.extra.numpy import arrays, array_shapes -import numpy as np -from pytest import approx - -from pyhegp import hegp_encrypt, hegp_decrypt, random_key - -@given(st.one_of( - arrays("int32", - array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=200), - elements=st.integers(min_value=0, max_value=2)), - # The array above is the only realistic input, but we test more - # kinds of inputs for good measure. - arrays("int32", - array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=200), - elements=st.integers(min_value=0, max_value=100)), - arrays("float64", - array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=200), - elements=st.floats(min_value=0, max_value=100))) -) -@settings(deadline=None) -def test_hegp_encryption_decryption_are_inverses(plaintext): - rng = np.random.default_rng() - key = random_key(rng, len(plaintext)) - # FIXME: We don't use maf at the moment. - maf = None - assert hegp_decrypt(hegp_encrypt(plaintext, maf, key), key) == approx(plaintext) diff --git a/tests/test_pyhegp.py b/tests/test_pyhegp.py new file mode 100644 index 0000000..eac499e --- /dev/null +++ b/tests/test_pyhegp.py @@ -0,0 +1,45 @@ +### pyhegp --- Homomorphic encryption of genotypes and phenotypes +### Copyright © 2025 Arun Isaac +### +### This file is part of pyhegp. +### +### pyhegp is free software: you can redistribute it and/or modify it +### under the terms of the GNU General Public License as published by +### the Free Software Foundation, either version 3 of the License, or +### (at your option) any later version. +### +### pyhegp is distributed in the hope that it will be useful, but +### WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +### General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with pyhegp. If not, see . + +from hypothesis import given, settings, strategies as st +from hypothesis.extra.numpy import arrays, array_shapes +import numpy as np +from pytest import approx + +from pyhegp.pyhegp import hegp_encrypt, hegp_decrypt, random_key + +@given(st.one_of( + arrays("int32", + array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=200), + elements=st.integers(min_value=0, max_value=2)), + # The array above is the only realistic input, but we test more + # kinds of inputs for good measure. + arrays("int32", + array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=200), + elements=st.integers(min_value=0, max_value=100)), + arrays("float64", + array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=200), + elements=st.floats(min_value=0, max_value=100))) +) +@settings(deadline=None) +def test_hegp_encryption_decryption_are_inverses(plaintext): + rng = np.random.default_rng() + key = random_key(rng, len(plaintext)) + # FIXME: We don't use maf at the moment. + maf = None + assert hegp_decrypt(hegp_encrypt(plaintext, maf, key), key) == approx(plaintext) -- cgit v1.2.3