def test_multiply(self): poly1 = Polynomial(4, [0, 1, 4, 5]) poly2 = Polynomial(4, [1, 2, 4, 3]) poly_prod = poly1.multiply(poly2, 73) poly_prod2 = poly2.multiply(poly1, 73) self.assertEqual(poly_prod.coeffs, [44, 42, 64, 17]) self.assertEqual(poly_prod.coeffs, poly_prod2.coeffs)
def generate_switching_key(self, new_key): """Generates a switching key for CKKS scheme. Generates a switching key as described in KSGen in the CKKS paper. Args: new_key (Polynomial): New key to generate switching key. Returns: A switching key. """ mod = self.params.big_modulus mod_squared = mod**2 swk_coeff = Polynomial( self.params.poly_degree, sample_uniform(0, mod_squared, self.params.poly_degree)) swk_error = Polynomial(self.params.poly_degree, sample_triangle(self.params.poly_degree)) sw0 = swk_coeff.multiply(self.secret_key.s, mod_squared) sw0 = sw0.scalar_multiply(-1, mod_squared) sw0 = sw0.add(swk_error, mod_squared) temp = new_key.scalar_multiply(mod, mod_squared) sw0 = sw0.add(temp, mod_squared) sw1 = swk_coeff return PublicKey(sw0, sw1)
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 generate_public_key(self, params): """Generates a public key for BFV scheme. Args: params (Parameters): Parameters including polynomial degree, plaintext, and ciphertext modulus. """ pk_coeff = Polynomial( params.poly_degree, sample_uniform(0, params.ciph_modulus, params.poly_degree)) pk_error = Polynomial(params.poly_degree, sample_triangle(params.poly_degree)) p0 = pk_error.add( pk_coeff.multiply(self.secret_key.s, params.ciph_modulus), params.ciph_modulus).scalar_multiply(-1, params.ciph_modulus) p1 = pk_coeff self.public_key = PublicKey(p0, p1)