Ejemplo n.º 1
0
def main():

    degree = 8
    # Ciphertext modulus is a prime congruent to 1 (mod 16).
    plain_modulus = 17
    ciph_modulus = 8000000000000
    params = BFVParameters(poly_degree=degree,
                           plain_modulus=plain_modulus,
                           ciph_modulus=ciph_modulus)
    key_generator = BFVKeyGenerator(params)
    public_key = key_generator.public_key
    secret_key = key_generator.secret_key
    relin_key = key_generator.relin_key
    encoder = BatchEncoder(params)
    encryptor = BFVEncryptor(params, public_key)
    decryptor = BFVDecryptor(params, secret_key)
    evaluator = BFVEvaluator(params)

    message1 = [0, 5, 8, 2, 5, 16, 4, 5]
    message2 = [1, 2, 3, 4, 5, 6, 7, 8]

    plain1 = encoder.encode(message1)
    plain2 = encoder.encode(message2)
    ciph1 = encryptor.encrypt(plain1)
    ciph2 = encryptor.encrypt(plain2)
    ciph_prod = evaluator.multiply(ciph1, ciph2, relin_key)
    decrypted_prod = decryptor.decrypt(ciph_prod)
    decoded_prod = encoder.decode(decrypted_prod)

    print(decoded_prod)
 def run_test_large_encrypt_decrypt(self, message):
     params = BFVParameters(poly_degree=self.large_degree,
                            plain_modulus=self.large_plain_modulus,
                            ciph_modulus=self.large_ciph_modulus)
     key_generator = BFVKeyGenerator(params)
     public_key = key_generator.public_key
     secret_key = key_generator.secret_key
     encryptor = BFVEncryptor(params, public_key)
     decryptor = BFVDecryptor(params, secret_key)
     message = Plaintext(Polynomial(self.large_degree, message))
     ciphertext = encryptor.encrypt(message)
     decrypted_message = decryptor.decrypt(ciphertext)
     self.assertEqual(str(message), str(decrypted_message))
Ejemplo n.º 3
0
 def setUp(self):
     self.degree = int(arg)
     self.plain_modulus = 256
     self.ciph_modulus = 0x3fffffff000001
     self.params = BFVParameters(poly_degree=self.degree,
                                 plain_modulus=self.plain_modulus,
                                 ciph_modulus=self.ciph_modulus)
     key_generator = BFVKeyGenerator(self.params)
     public_key = key_generator.public_key
     secret_key = key_generator.secret_key
     self.relin_key = key_generator.relin_key
     self.encryptor = BFVEncryptor(self.params, public_key)
     self.decryptor = BFVDecryptor(self.params, secret_key)
     self.evaluator = BFVEvaluator(self.params)
Ejemplo n.º 4
0
class TestEvaluator(unittest.TestCase):
    def setUp(self):
        self.degree = 512
        self.plain_modulus = 256
        self.ciph_modulus = 0x3fffffff000001
        params = BFVParameters(poly_degree=self.degree,
                               plain_modulus=self.plain_modulus,
                               ciph_modulus=self.ciph_modulus)
        key_generator = BFVKeyGenerator(params)
        public_key = key_generator.public_key
        secret_key = key_generator.secret_key
        self.relin_key = key_generator.relin_key
        self.encryptor = BFVEncryptor(params, public_key)
        self.decryptor = BFVDecryptor(params, secret_key)
        self.evaluator = BFVEvaluator(params)

    def run_test_add(self, message1, message2):
        poly1 = Polynomial(self.degree, message1)
        poly2 = Polynomial(self.degree, message2)
        plain1 = Plaintext(poly1)
        plain2 = Plaintext(poly2)
        plain_sum = Plaintext(poly1.add(poly2, self.plain_modulus))
        ciph1 = self.encryptor.encrypt(plain1)
        ciph2 = self.encryptor.encrypt(plain2)
        ciph_sum = self.evaluator.add(ciph1, ciph2)
        decrypted_sum = self.decryptor.decrypt(ciph_sum)
        self.assertEqual(str(plain_sum), str(decrypted_sum))

    def test_add_01(self):
        vec1 = sample_uniform(0, self.plain_modulus, self.degree)
        vec2 = sample_uniform(0, self.plain_modulus, self.degree)
        self.run_test_add(vec1, vec2)

    def run_test_multiply(self, message1, message2):
        poly1 = Polynomial(self.degree, message1)
        poly2 = Polynomial(self.degree, message2)
        plain1 = Plaintext(poly1)
        plain2 = Plaintext(poly2)
        plain_prod = Plaintext(poly1.multiply(poly2, self.plain_modulus))
        ciph1 = self.encryptor.encrypt(plain1)
        ciph2 = self.encryptor.encrypt(plain2)
        ciph_prod = self.evaluator.multiply(ciph1, ciph2, self.relin_key)
        decrypted_prod = self.decryptor.decrypt(ciph_prod)
        self.assertEqual(str(plain_prod), str(decrypted_prod))

    def test_multiply_01(self):
        vec1 = sample_uniform(0, self.plain_modulus, self.degree)
        vec2 = sample_uniform(0, self.plain_modulus, self.degree)
        self.run_test_multiply(vec1, vec2)
Ejemplo n.º 5
0
class TestMultiply(unittest.TestCase):
    def setUp(self):
        self.degree = int(arg)
        self.plain_modulus = 256
        self.ciph_modulus = 0x3fffffff000001
        self.params = BFVParameters(poly_degree=self.degree,
                                    plain_modulus=self.plain_modulus,
                                    ciph_modulus=self.ciph_modulus)
        key_generator = BFVKeyGenerator(self.params)
        public_key = key_generator.public_key
        secret_key = key_generator.secret_key
        self.relin_key = key_generator.relin_key
        self.encryptor = BFVEncryptor(self.params, public_key)
        self.decryptor = BFVDecryptor(self.params, secret_key)
        self.evaluator = BFVEvaluator(self.params)

    def run_test_multiply(self, message1, message2):
        poly1 = Polynomial(self.degree, message1)
        poly2 = Polynomial(self.degree, message2)
        plain1 = Plaintext(poly1)
        plain2 = Plaintext(poly2)
        plain_prod = Plaintext(poly1.multiply(poly2, self.plain_modulus))
        ciph1 = self.encryptor.encrypt(plain1)
        ciph2 = self.encryptor.encrypt(plain2)
        start_time = time.clock()
        ciph_prod = self.evaluator.multiply(ciph1, ciph2, self.relin_key)
        total_time = time.clock() - start_time
        decrypted_prod = self.decryptor.decrypt(ciph_prod)
        self.assertEqual(str(plain_prod), str(decrypted_prod))
        return total_time

    def test_multiply_time(self):
        self.params.print_parameters()
        num_iterations = 1
        print("Number of multiplications: %d" % (num_iterations))
        total_time = 0

        for _ in range(num_iterations):
            vec1 = sample_uniform(0, self.plain_modulus, self.degree)
            vec2 = sample_uniform(0, self.plain_modulus, self.degree)
            total_time += self.run_test_multiply(vec1, vec2)

        print("Average time per multiply operation: %f seconds" %
              (total_time / num_iterations))