Beispiel #1
0
    def test_paillier(self):
        p = 293
        q = 433
        g = 6497955158
        mu = 53022
        n = p*q
        lmb = 31536 # lcm(p-1, q-1)
        key = paillier.Key(n, g, lmb, mu)
        public = key.public()

        # test private key encrypt(decrypt(x)) == x
        ptxt_original = 521
        ctxt = paillier.encrypt(key, ptxt_original)
        ptxt = paillier.decrypt(key, ctxt)
        self.assertEqual(ptxt_original, ptxt)

        # test public key encrypt(decrypt(x)) == x
        ctxt = paillier.encrypt(public, ptxt_original)
        ptxt = paillier.decrypt(key, ctxt)
        self.assertEqual(ptxt_original, ptxt)

        # test homomorphism
        ptxt1 = 14
        ptxt2 = 19
        ctxt1 = paillier.encrypt(public, ptxt1)
        ctxt2 = paillier.encrypt(public, ptxt2)
        final_ptxt = paillier.decrypt(key, ctxt1 * ctxt2)
        self.assertEqual(final_ptxt, ptxt1 + ptxt2)
Beispiel #2
0
    def test_paillier_key_generation(self):
        key = paillier.generate_keys(bits = 128)
        public = key.public()

        ptxt_original = 521
        ctxt = paillier.encrypt(public, ptxt_original)
        ptxt = paillier.decrypt(key, ctxt)
        self.assertEqual(ptxt_original, ptxt)

        # test homomorphism
        ptxt1 = 14
        ptxt2 = 19
        ctxt1 = paillier.encrypt(public, ptxt1)
        ctxt2 = paillier.encrypt(public, ptxt2)
        final_ptxt = paillier.decrypt(key, ctxt1 * ctxt2)
        self.assertEqual(final_ptxt, ptxt1 + ptxt2)

        # test average
        ptxt3 = 12
        ctxt3 = paillier.encrypt(public, ptxt3)
        ciphertext = [ctxt1, ctxt2, ctxt3]
        numerator, denominator = paillier.average(public, ciphertext)
        numerator = paillier.decrypt(key, numerator)
        denominator = paillier.decrypt(key, denominator)
        average = numerator/denominator
        self.assertAlmostEqual(average, 15)
Beispiel #3
0
 def paillier_encrypt(self, ptxt):
     """Encrypt a number using homomorphic methods."""
     try:
         ptxt = int(ptxt)
     except ValueError:
         raise EDBError("can only homomorphic encrypt integers")
     return str(paillier.encrypt(self.keys['paillier'], ptxt))
Beispiel #4
0
 def paillier_encrypt(self, ptxt):
     """Encrypt a number using homomorphic methods."""
     try:
         ptxt = int(ptxt)
     except ValueError:
         raise EDBError("can only homomorphic encrypt integers")
     return str(paillier.encrypt(self.keys['paillier'], ptxt))