コード例 #1
0
ファイル: HE.py プロジェクト: AlexMV12/PyCrCNN
 def __init__(self, m, p, sec=128, int_digits=64, frac_digits=32):
     self.he = Pyfhel()
     self.he.contextGen(p,
                        m=m,
                        sec=sec,
                        fracDigits=frac_digits,
                        intDigits=int_digits)
コード例 #2
0
    def test_Pyfhel_4c_operations_batch_array(self):
        self.pyfhel = Pyfhel()
        self.pyfhel.contextGen(p=1964769281,
                               m=8192,
                               base=2,
                               sec=192,
                               flagBatching=True)
        self.pyfhel.keyGen()
        self.pyfhel.rotateKeyGen(60)
        self.ctxti = self.pyfhel.encryptBatch([1, 2, 3, 4, 5, 6])
        self.ctxti2 = self.pyfhel.encryptArray(
            np.array([-6, -5, -4, -3, -2, -1]))
        self.ptxti = self.pyfhel.encodeArray(np.array([12, 15, 18, 21, 24,
                                                       27]))

        self.ctxt_add = self.pyfhel.add(self.ctxti,
                                        self.ctxti2,
                                        in_new_ctxt=True)
        self.ctxt_add2 = self.pyfhel.add_plain(self.ctxti,
                                               self.ptxti,
                                               in_new_ctxt=True)
        self.ctxt_sub = self.pyfhel.sub(self.ctxti,
                                        self.ctxti2,
                                        in_new_ctxt=True)
        self.ctxt_sub2 = self.pyfhel.sub_plain(self.ctxti,
                                               self.ptxti,
                                               in_new_ctxt=True)
        self.ctxt_mult = self.pyfhel.multiply(self.ctxti,
                                              self.ctxti2,
                                              in_new_ctxt=True)
        self.ctxt_mult2 = self.pyfhel.multiply_plain(self.ctxti,
                                                     self.ptxti,
                                                     in_new_ctxt=True)
        self.ctxt_rotate = self.pyfhel.rotate(self.ctxti, -2, in_new_ctxt=True)
        self.ctxt_rotate2 = self.pyfhel.rotate(self.ctxti, 2, in_new_ctxt=True)
        #self.ctxt_expon = self.pyfhel.power(self.ctxti, 3)
        #self.ctxt_expon2 = self.pyfhel.power(self.ctxti2, 3)
        #self.ctxt_polyEval = self.pyfhel.polyEval(self.ctxti, [1, 2, 1], in_new_ctxt=True)

        self.assertEqual(
            self.pyfhel.decryptBatch(self.ctxt_add)[:6], [-5, -3, -1, 1, 3, 5])
        self.assertEqual(
            self.pyfhel.decryptBatch(self.ctxt_add2)[:6],
            [13, 17, 21, 25, 29, 33])
        self.assertEqual(
            self.pyfhel.decryptBatch(self.ctxt_sub)[:6], [7, 7, 7, 7, 7, 7])
        self.assertEqual(
            self.pyfhel.decryptBatch(self.ctxt_sub2)[:6],
            [-11, -13, -15, -17, -19, -21])
        self.assertEqual(
            self.pyfhel.decryptBatch(self.ctxt_mult)[:6],
            [-6, -10, -12, -12, -10, -6])
        self.assertEqual(
            self.pyfhel.decryptBatch(self.ctxt_mult2)[:6],
            [12, 30, 54, 84, 120, 162])
        self.assertEqual(
            self.pyfhel.decryptBatch(self.ctxt_rotate)[:6], [0, 0, 1, 2, 3, 4])
        self.assertEqual(
            self.pyfhel.decryptBatch(self.ctxt_rotate2)[:6],
            [3, 4, 5, 6, 0, 0])
コード例 #3
0
    def generate_context(self):
        context = self.ctxt_dir + "/context.ctxt"

        self.py = Pyfhel()

        if path.exists(context):
            if self.verbosity:
                print("Restoring the crypto context...")
            self.py.restoreContext(context)
        else:
            if self.verbosity:
                print("Creating the crypto context...")

            self.ctx = self.py.contextGen(self.t, self.n, True, 2, 128, 32, 32)
            self.py.saveContext(context)

        if path.exists(self.keys_dir):
            if self.verbosity:
                print("Restoring keys from local storage...")
            self.py.restorepublicKey(self.keys_dir + "/public.key")
            self.py.restoresecretKey(self.keys_dir + "/secret.key")
            self.py.restorerelinKey(self.keys_dir + "/relin.keys")

        else:
            if self.verbosity:
                print("Creating keys for this contest...")
            createDir(self.keys_dir)
            self.py.keyGen()
            self.py.relinKeyGen(16, 4)

            self.py.saverelinKey(self.keys_dir + "/relin.keys")
            self.py.savepublicKey(self.keys_dir + "/public.key")
            self.py.savesecretKey(self.keys_dir + "/secret.key")
コード例 #4
0
 def test_Pyfhel_3c_encrypt_decrypt_batch(self):
     pyfhel = Pyfhel()
     pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True)
     pyfhel.keyGen()
     self.assertTrue(pyfhel.batchEnabled())
     ctxt = pyfhel.encryptBatch([1, 2, 3, 4, 5, 6])
     self.assertEqual(pyfhel.getnSlots(), 8192)
     self.assertEqual(pyfhel.decryptBatch(ctxt)[:6], [1, 2, 3, 4, 5, 6])
コード例 #5
0
 def test_Pyfhel_1c_rotate_key_generation(self):
     self.pyfhel = Pyfhel()
     self.pyfhel.contextGen(65537)
     self.pyfhel.keyGen()
     self.pyfhel.rotateKeyGen(30)
     self.pyfhel.rotateKeyGen(1)
     self.pyfhel.rotateKeyGen(60)
     self.assertRaises(SystemError, lambda: self.pyfhel.rotateKeyGen(61))
     self.assertRaises(SystemError, lambda: self.pyfhel.rotateKeyGen(0))
コード例 #6
0
 def test_Pyfhel_1d_relin_key_generation(self):
     self.pyfhel = Pyfhel()
     self.pyfhel.contextGen(65537)
     self.pyfhel.keyGen()
     self.pyfhel.relinKeyGen(30, 5)
     self.pyfhel.relinKeyGen(1, 5)
     self.pyfhel.relinKeyGen(60, 5)
     self.assertRaises(SystemError, lambda: self.pyfhel.relinKeyGen(61, 5))
     self.assertRaises(SystemError, lambda: self.pyfhel.relinKeyGen(0, 5))
コード例 #7
0
 def test_Pyfhel_1a_creation_deletion(self):
     try:
         self.pyfhel = Pyfhel()
     except Exception as err:
         self.fail("Pyfhel() creation failed unexpectedly: ", err)
     try:
         del (self.pyfhel)
     except Exception as err:
         self.fail("Pyfhel() deletion failed unexpectedly: ", err)
コード例 #8
0
 def test_Pyfhel_3d_encrypt_decrypt_array(self):
     pyfhel = Pyfhel()
     pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True)
     pyfhel.keyGen()
     self.assertTrue(pyfhel.batchEnabled())
     ctxt = pyfhel.encryptArray(np.array([1, 2, 3, 4, 5, 6], dtype=np.int64))
     self.assertEqual(pyfhel.getnSlots(), 8192)
     self.assertTrue(
         np.alltrue(pyfhel.decryptArray(ctxt)[:6] == np.array([1, 2, 3, 4, 5, 6], dtype=np.int64))
     )
コード例 #9
0
 def test_Pyfhel_2d_encode_decode_array(self):
     pyfhel = Pyfhel()
     pyfhel.contextGen(p=1964769281, m=8192, base=2, sec=192, flagBatching=True)
     pyfhel.keyGen()
     self.assertTrue(pyfhel.batchEnabled())
     ptxt = pyfhel.encodeArray(np.array([1, 2, 3, 4, 5, 6]))
     self.assertEqual(pyfhel.getnSlots(), 8192)
     self.assertTrue(
         np.alltrue(pyfhel.decodeArray(ptxt)[:6] == np.array([1, 2, 3, 4, 5, 6]))
     )
コード例 #10
0
 def test_Pyfhel_5b_restore_objects(self):
     self.pyfhel = Pyfhel()
     self.assertTrue(self.pyfhel.restoreContext(b"context.pycon"))
     self.assertTrue(self.pyfhel.restoresecretKey(b"secret_k.pysk"))
     self.assertTrue(self.pyfhel.restorepublicKey(b"public_k.pypk"))
     #self.assertTrue(self.pyfhel.restorerelinKey(b"relin_k.pyrlk"))
     self.assertTrue(self.pyfhel.restorerotateKey(b"rotate_k.pyrok"))
     os.remove(b"context.pycon")
     os.remove(b"secret_k.pysk")
     os.remove(b"public_k.pypk")
     os.remove(b"rotate_k.pyrok")
コード例 #11
0
 def test_Pyfhel_3a_encrypt_decrypt_int(self):
     self.pyfhel = Pyfhel()
     self.pyfhel.contextGen(p=65537)
     self.pyfhel.keyGen()
     self.ctxt = self.pyfhel.encryptInt(127)
     self.assertEqual(self.pyfhel.decryptInt(self.ctxt), 127)
     self.ctxt2 = PyCtxt(self.ctxt)
     self.pyfhel.encryptInt(-2, self.ctxt)
     self.assertEqual(self.pyfhel.decryptInt(self.ctxt), -2)
     self.assertEqual(self.pyfhel.decryptInt(self.ctxt), -2)
     self.assertEqual(self.pyfhel.decryptInt(self.ctxt2), 127)
コード例 #12
0
 def test_Pyfhel_3b_encrypt_decrypt_float(self):
     self.pyfhel = Pyfhel()
     self.pyfhel.contextGen(p=65537,
                            m=8192,
                            base=2,
                            intDigits=80,
                            fracDigits=20)
     self.pyfhel.keyGen()
     self.ctxt = self.pyfhel.encryptFrac(19.30)
     self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt), 2), 19.30)
     self.pyfhel.encryptFrac(-2.25, self.ctxt)
     self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt), 2), -2.25)
コード例 #13
0
 def test_Pyfhel_2c_encode_decode_batch(self):
     self.pyfhel = Pyfhel()
     self.pyfhel.contextGen(p=1964769281,
                            m=8192,
                            base=2,
                            sec=192,
                            flagBatching=True)
     self.pyfhel.keyGen()
     self.assertTrue(self.pyfhel.batchEnabled())
     self.ptxt = self.pyfhel.encodeBatch([1, 2, 3, 4, 5, 6])
     self.assertEqual(self.pyfhel.getnSlots(), 8192)
     self.assertEqual(
         self.pyfhel.decodeBatch(self.ptxt)[:6], [1, 2, 3, 4, 5, 6])
コード例 #14
0
 def test_Pyfhel_2a_encode_decode_int(self):
     self.pyfhel = Pyfhel()
     self.pyfhel.contextGen(p=65537)
     self.pyfhel.keyGen()
     self.ptxt = self.pyfhel.encodeInt(127)
     self.assertEqual(self.ptxt.to_string(),
                      b'1x^6 + 1x^5 + 1x^4 + 1x^3 + 1x^2 + 1x^1 + 1')
     self.assertEqual(self.pyfhel.decodeInt(self.ptxt), 127)
     self.ptxt2 = PyPtxt(self.ptxt)
     self.pyfhel.encodeInt(-2, self.ptxt)
     self.assertEqual(self.ptxt.to_string(), b'10000x^1')
     self.assertEqual(self.pyfhel.decodeInt(self.ptxt), -2)
     self.assertEqual(self.pyfhel.decodeInt(self.ptxt2), 127)
コード例 #15
0
    def HE(self):
        HE = Pyfhel()
        HE.contextGen(m=2048, p=65537)
        HE.keyGen()
        HE.relinKeyGen(60, 3)

        return HE
コード例 #16
0
 def test_Pyfhel_2b_encode_decode_float(self):
     self.pyfhel = Pyfhel()
     self.pyfhel.contextGen(p=65537,
                            m=8192,
                            base=2,
                            intDigits=80,
                            fracDigits=20)
     self.pyfhel.keyGen()
     self.ptxt = self.pyfhel.encodeFrac(19.30)
     self.assertTrue(self.ptxt.to_string(), b'9x^8190 + 1x^4 + 1x^1 + 1')
     self.assertEqual(round(self.pyfhel.decodeFrac(self.ptxt), 2), 19.30)
     self.pyfhel.encodeFrac(-2.25, self.ptxt)
     self.assertEqual(self.ptxt.to_string(), b'1x^8190 + 10000x^1')
     self.assertEqual(round(self.pyfhel.decodeFrac(self.ptxt), 2), -2.25)
コード例 #17
0
    def test_call(self):
        HE = Pyfhel()
        HE.contextGen(65537)
        HE.keyGen()
        HE.relinKeyGen(30, 100)

        new_weights = np.array([
            [1, 2, 3, 4]
           ,[2, 3, 4, 5]
           ,[3, 4, 5, 6]
        ])
        new_bias = np.array([1, 2, 3])

        linear_layer = lin.LinearLayer(HE, new_weights, new_bias)

        flattened_input = np.array([
            [1, 2, 3, 4]
            , [5, 6, 7, 8]
        ])
        encrypted_input = cr.encrypt_matrix(HE, flattened_input)

        encrypted_result = linear_layer(encrypted_input)
        result = cr.decrypt_matrix(HE, encrypted_result)

        expected_result = np.array([
            [31, 42, 53]
            ,[71, 98, 125]
        ])
        assert np.allclose(result, expected_result)
コード例 #18
0
    def test_Pyfhel_5a_save_objects(self):
        self.pyfhel = Pyfhel()
        self.pyfhel.contextGen(p=1964769281,
                               m=8192,
                               base=2,
                               sec=192,
                               flagBatching=True)
        self.pyfhel.keyGen()
        self.pyfhel.rotateKeyGen(60)
        #self.pyfhel.relinKeyGen(60)

        self.assertTrue(self.pyfhel.saveContext(b"context.pycon"))
        self.assertTrue(self.pyfhel.savepublicKey(b"public_k.pypk"))
        self.assertTrue(self.pyfhel.savesecretKey(b"secret_k.pysk"))
        #self.assertTrue(self.pyfhel.saverelinKey(b"relin_k.pyrlk"))
        self.assertTrue(self.pyfhel.saverotateKey(b"rotate_k.pyrok"))
コード例 #19
0
    def test_get_min_noise(self):

        HE = Pyfhel()
        HE.contextGen(65537)
        HE.keyGen()
        plain_image = np.array([[[1, 2, 3], [1, 3, 4], [1, 3, 4]]])

        cipher_image = encrypt_matrix(HE, plain_image)
        cipher_image[0][1][1] = cipher_image[0][1][1] * HE.encryptFrac(2)

        assert get_min_noise(HE, cipher_image) == HE.noiseLevel(
            cipher_image[0][1][1])
コード例 #20
0
ファイル: hc.py プロジェクト: alltootechnical/thesis
 def __init__(self, hc_type):
     self.hc_type = hc_type
     if self.hc_type == 'paillier':
         self.hc_obj = paillier
     elif self.hc_type == 'bgv':
         self.hc_obj = Pyfhel()
     elif self.hc_type == 'dp':
         self.hc_obj = DasguptaPalInteger()
     else:
         raise NotImplementedError('Cryptosystem {hc_type} not yet supported...')
コード例 #21
0
    def test_Pyfhel_4b_operations_frac(self):
        self.pyfhel = Pyfhel()
        self.pyfhel.contextGen(p=1964769281, m=8192, base=3, sec=192)
        self.pyfhel.keyGen()
        #self.pyfhel.rotateKeyGen(60)
        #self.pyfhel.relinKeyGen(60)

        self.ctxti = self.pyfhel.encryptFrac(19.37)
        self.ctxti2 = self.pyfhel.encryptFrac(-2.25)
        self.ptxti = self.pyfhel.encodeFrac(3.12)

        self.ctxt_add = self.pyfhel.add(self.ctxti,
                                        self.ctxti2,
                                        in_new_ctxt=True)
        self.ctxt_add2 = self.pyfhel.add_plain(self.ctxti,
                                               self.ptxti,
                                               in_new_ctxt=True)
        self.ctxt_sub = self.pyfhel.sub(self.ctxti,
                                        self.ctxti2,
                                        in_new_ctxt=True)
        self.ctxt_sub2 = self.pyfhel.sub_plain(self.ctxti,
                                               self.ptxti,
                                               in_new_ctxt=True)
        self.ctxt_mult = self.pyfhel.multiply(self.ctxti,
                                              self.ctxti2,
                                              in_new_ctxt=True)
        self.ctxt_mult2 = self.pyfhel.multiply_plain(self.ctxti,
                                                     self.ptxti,
                                                     in_new_ctxt=True)

        self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_add), 2),
                         17.12)
        self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_add2), 2),
                         22.49)
        self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_sub), 2),
                         21.62)
        self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_sub2), 2),
                         16.25)
        self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_mult), 2),
                         -43.58)
        self.assertEqual(round(self.pyfhel.decryptFrac(self.ctxt_mult2), 2),
                         60.43)
コード例 #22
0
 def test_PyPtxt_creation_deletion(self):
     try:
         self.ptxt = PyPtxt()
         self.ptxt2 = PyPtxt(other_ptxt=self.ptxt)
         self.pyfhel = Pyfhel()
         self.ptxt3 = PyPtxt(pyfhel=self.pyfhel)
         self.ptxt4 = PyPtxt(other_ptxt=self.ptxt3)
     except Exception as err:
         self.fail("PyPtxt() creation failed unexpectedly: ", err)
     self.assertEqual(self.ptxt._encoding, ENCODING_t.UNDEFINED)
     self.ptxt._encoding = ENCODING_t.INTEGER
     self.assertEqual(self.ptxt._encoding, ENCODING_t.INTEGER)
     del (self.ptxt._encoding)
     self.assertEqual(self.ptxt._encoding, ENCODING_t.UNDEFINED)
     self.ptxt._pyfhel = self.pyfhel
     self.ptxt2._pyfhel = self.ptxt._pyfhel
     try:
         del (self.ptxt)
     except Exception as err:
         self.fail("PyPtxt() deletion failed unexpectedly: ", err)
コード例 #23
0
    def test_Pyfhel_4a_operations_integer(self):
        self.pyfhel = Pyfhel()
        self.pyfhel.contextGen(p=1964769281, m=8192, base=3, sec=192)
        self.pyfhel.keyGen()
        #self.pyfhel.rotateKeyGen(60)
        #self.pyfhel.relinKeyGen(60)

        self.ctxti = self.pyfhel.encryptInt(127)
        self.ctxti2 = self.pyfhel.encryptInt(-2)
        self.ptxti = self.pyfhel.encodeInt(3)

        self.ctxt_add = self.pyfhel.add(self.ctxti,
                                        self.ctxti2,
                                        in_new_ctxt=True)
        self.ctxt_add2 = self.pyfhel.add_plain(self.ctxti,
                                               self.ptxti,
                                               in_new_ctxt=True)
        self.ctxt_sub = self.pyfhel.sub(self.ctxti,
                                        self.ctxti2,
                                        in_new_ctxt=True)
        self.ctxt_sub2 = self.pyfhel.sub_plain(self.ctxti,
                                               self.ptxti,
                                               in_new_ctxt=True)
        self.ctxt_mult = self.pyfhel.multiply(self.ctxti,
                                              self.ctxti2,
                                              in_new_ctxt=True)
        self.ctxt_mult2 = self.pyfhel.multiply_plain(self.ctxti,
                                                     self.ptxti,
                                                     in_new_ctxt=True)
        #self.ctxt_rotate = self.pyfhel.rotate(self.ctxti, 2)
        #self.ctxt_expon = self.pyfhel.power(self.ctxti, 3)
        #self.ctxt_expon2 = self.pyfhel.power(self.ctxti2, 3)
        #self.ctxt_polyEval = self.pyfhel.polyEval(self.ctxti, [1, 2, 1], in_new_ctxt=True)

        self.assertEqual(self.pyfhel.decryptInt(self.ctxt_add), 125)
        self.assertEqual(self.pyfhel.decryptInt(self.ctxt_add2), 130)
        self.assertEqual(self.pyfhel.decryptInt(self.ctxt_sub), 129)
        self.assertEqual(self.pyfhel.decryptInt(self.ctxt_sub2), 124)
        self.assertEqual(self.pyfhel.decryptInt(self.ctxt_mult), -254)
        self.assertEqual(self.pyfhel.decryptInt(self.ctxt_mult2), 381)
コード例 #24
0
 def test_PyCtxt_creation_deletion(self):
     try:
         self.ctxt = PyCtxt()
         self.ctxt2 = PyCtxt(other_ctxt=self.ctxt)
         self.pyfhel = Pyfhel()
         self.ctxt3 = PyCtxt(pyfhel=self.pyfhel)
         self.ctxt4 = PyCtxt(other_ctxt=self.ctxt3)
     except Exception as err:
         self.fail("PyCtxt() creation failed unexpectedly: ", err)
     self.assertEqual(self.ctxt.size(), 2)
     self.assertEqual(self.ctxt._encoding, ENCODING_t.UNDEFINED)
     self.ctxt._encoding = ENCODING_t.FRACTIONAL
     self.assertEqual(self.ctxt._encoding, ENCODING_t.FRACTIONAL)
     del (self.ctxt._encoding)
     self.assertEqual(self.ctxt._encoding, ENCODING_t.UNDEFINED)
     self.assertEqual(self.ctxt.size(), 2)
     self.ctxt._pyfhel = self.pyfhel
     self.ctxt2._pyfhel = self.ctxt._pyfhel
     try:
         del (self.ctxt)
     except Exception as err:
         self.fail("PyCtxt() deletion failed unexpectedly: ", err)
コード例 #25
0
ファイル: enc_util.py プロジェクト: tf369/privacy-CNN
def encryptImg(Img):
    Img_shape = Img.shape
    print(Img_shape)
    encImg = [[0 for j in range(Img_shape[1])] for i in range(Img_shape[0])]
    pyfhel = Pyfhel()  # Creating empty Pyfhel object
    pyfhel.contextGen(p=65537, m=1024,
                      flagBatching=True)  # Generating context. (encrypt pixel)
    pyfhel.keyGen()  # keygeneration

    for py in range(Img_shape[0]):
        for px in range(Img_shape[1]):
            temp_pxl = Img[py, px]

            cxt_pxl = pyfhel.encryptFrac(temp_pxl)  # test floating number
            dec_pxl = pyfhel.decryptFrac(cxt_pxl)  # decrypt
            encImg[py][px] = cxt_pxl
    return encImg, pyfhel
コード例 #26
0
    def test_get_max_error(self):

        HE = Pyfhel()
        HE.contextGen(65537)
        HE.keyGen()
        plain_image = np.array([[1, 2, 3], [1, 3, 4], [1, 3, 4]])

        cipher_image = encrypt_matrix(HE, plain_image)

        cipher_image[2][0] = HE.encryptFrac(-1)

        max_error, position = get_max_error(HE, plain_image, cipher_image)

        assert max_error == 2
        assert position, (2 == 0)
コード例 #27
0
# ContextParameters shows how several parameters affect performance.
#  TODO: Finish for all parameters. Show possible values of each.

import time

from Pyfhel import Pyfhel, PyPtxt, PyCtxt
# Pyfhel class contains most of the functions.
# PyPtxt is the plaintext class
# PyCtxt is the ciphertext class

print("==============================================================")
print("================== Pyfhel CONTEXT PARAMETERS =================")
print("==============================================================")

print("1. p (long): Plaintext modulus. All operations are modulo p. ")
HE = Pyfhel()  # Creating empty Pyfhel object
HE.contextGen(p=65537)  # Generating context. The value of p is important.
#  There are many configurable parameters on this step
HE.keyGen()  # Key Generation.

print("2. m (long=2048): Polynomial coefficient modulus.")
print(
    "   Higher allows more encrypted operations. In batch mode it is the number of integers per ciphertext."
)


def time_demo_helloworld(integer1=127, integer2=-2):
    start = time.time()
    ctxt1 = HE.encryptInt(integer1)  # Encryption makes use of the public key
    ctxt2 = HE.encryptInt(
        integer2)  # For integers, encryptInt function is used.
コード例 #28
0
# Encrypting Demo for Pyfhel, covering the different ways of encrypting
#   and decrypting.

from Pyfhel import Pyfhel, PyPtxt, PyCtxt

print("==============================================================")
print("===================== Pyfhel ENCRYPTING ======================")
print("==============================================================")

print("1. Creating Context and KeyGen in a Pyfhel Object ")
HE = Pyfhel()  # Creating empty Pyfhel object
HE.contextGen(p=65537, m=1024, flagBatching=True)  # Generating context.
# The values of p and m are chosen to enable batching (see Demo_Batching_SIMD.py)
HE.keyGen()  # Key Generation.

print("2. Encrypting integers with encryptInt")
integer1 = 94
integer2 = -235
ctxt_i1 = HE.encryptInt(
    integer1)  # Encrypting integer1 in a new PyCtxt with encryptInt
ctxt_i2 = PyCtxt()  # Empty ciphertexts have no encoding type.
print("    Empty created ctxt_i2: ", str(ctxt_i2))
HE.encryptInt(integer2, ctxt_i2)  # Encrypting integer2 in an existing PyCtxt
print("    int ", integer1, '-> ctxt_i1 ', str(ctxt_i1))
print("    int ", integer2, '-> ctxt_i2 ', str(ctxt_i2))

print("3. Encrypting floating point values with encryptFrac")
float1 = 3.5
float2 = -7.8
ctxt_f1 = HE.encryptInt(
    float1)  # Encrypting float1 in a new PyCtxt with encryptFrac
コード例 #29
0
ファイル: Demo_Float.py プロジェクト: fabyulshi/Pyfhel
# Fractional / float Demo for Pyfhel, covering the different ways of encrypting
#   and decrypting.

from Pyfhel import Pyfhel, PyPtxt, PyCtxt

print("==============================================================")
print("===================== Pyfhel FRACTIONAL ======================")
print("==============================================================")

print("1. Creating Context and KeyGen in a Pyfhel Object. Using 64 ")
print("     bits for integer part and 32 bits for decimal part.")
HE = Pyfhel()  # Creating empty Pyfhel object
HE.contextGen(p=65537, base=2, intDigits=64, fracDigits=32)
# Generating context. The value of p is important.
#  There are many configurable parameters on this step
#  More info in Demo_ContextParameters.py, and
#  in the docs of the function (link to docs in README)
HE.keyGen()  # Key Generation.
print(HE)

print("2. Encrypting Fractionals")
float1 = -7.3
float2 = 3.4
ctxt1 = HE.encryptFrac(float1)  # Encryption makes use of the public key
ctxt2 = HE.encryptFrac(float2)  # For integers, encryptInt function is used.
print("    int ", float1, '-> ctxt1 ', ctxt1)
print("    int ", float2, '-> ctxt2 ', ctxt2)

print("3. Operating with encrypted fractionals")
ctxtSum = ctxt1 + ctxt2  # `ctxt1 += ctxt2` for quicker inplace operation
ctxtSub = ctxt1 - ctxt2  # `ctxt1 -= ctxt2` for quicker inplace operation
コード例 #30
0
from Pyfhel import Pyfhel,PyCtxt,PyPtxt
import numpy as np
import time
he=Pyfhel()
he.contextGen(p=1179649,m=32768,flagBatching=True,fracDigits=20)
he.keyGen()
a=PyCtxt()
b=PyCtxt()
q=0
cc=0

#加密文件
def PPctxt(name):
  fil=open(name)
  lines=fil.readlines()
  strr=''.join(lines)
  global l
  l=len(strr)
  l=l-1
  arr=[]
  for line in strr:
      for ind in line:
          arr.append(ind)
  #arr.remove('\n')
  brr=[]
  for i in arr:
      brr.append(ord(i))
  crr=np.array(brr)
  #列表数据转数组  
  err=he.encryptBatch(crr)
  err.save(name)