def test_seal_and_open(self, run_eagerly, m, dtype, dtype_size): context = tf_execution_context(run_eagerly) with context.scope(): pk_s, sk_s = easy_box.gen_keypair() pk_r, sk_r = easy_box.gen_keypair() plaintext = tf.constant(m, dtype=dtype) nonce = easy_box.gen_nonce() ciphertext, mac = easy_box.seal_detached(plaintext, nonce, pk_r, sk_s) plaintext_recovered = easy_box.open_detached( ciphertext, mac, nonce, pk_s, sk_r, plaintext.dtype) assert isinstance(ciphertext, easy_box.Ciphertext) assert isinstance(ciphertext.raw, tf.Tensor) assert ciphertext.raw.dtype == tf.uint8 assert ciphertext.raw.shape == plaintext.shape + (dtype_size, ) assert isinstance(mac, easy_box.Mac) assert isinstance(mac.raw, tf.Tensor) assert mac.raw.dtype == tf.uint8 assert mac.raw.shape == (16, ) assert isinstance(plaintext_recovered, tf.Tensor) assert plaintext_recovered.dtype == plaintext.dtype assert plaintext_recovered.shape == plaintext.shape plaintext_recovered = context.evaluate(plaintext_recovered) np.testing.assert_equal(plaintext_recovered, np.array(m))
def test_export(self, run_eagerly, export_dtype, export_expansion): x = np.array([[12345, 34342]]) context = tf_execution_context(run_eagerly) with context.scope(): ek, dk = paillier.gen_keypair() assert isinstance(ek, paillier.EncryptionKey) assert isinstance(dk, paillier.DecryptionKey) n_exported = ek.export(export_dtype) assert isinstance(n_exported, tf.Tensor) assert n_exported.dtype == export_dtype assert n_exported.shape == (1, 1), n_exported.shape p_exported, q_exported = dk.export(export_dtype) assert isinstance(p_exported, tf.Tensor) assert p_exported.dtype == export_dtype assert p_exported.shape == (1, 1), p_exported.shape assert isinstance(q_exported, tf.Tensor) assert q_exported.dtype == export_dtype assert q_exported.shape == (1, 1), q_exported.shape r = paillier.gen_randomness(ek, shape=x.shape) assert isinstance(r, paillier.Randomness) r_exported = r.export(export_dtype) assert isinstance(r_exported, tf.Tensor) assert r_exported.dtype == export_dtype assert r_exported.shape == x.shape + export_expansion c = paillier.encrypt(ek, x, r) assert isinstance(c, paillier.Ciphertext) c_exported = c.export(export_dtype) assert isinstance(c_exported, tf.Tensor) assert c_exported.dtype == export_dtype assert c_exported.shape == x.shape + export_expansion
def test_gen_nonce(self, run_eagerly): context = tf_execution_context(run_eagerly) with context.scope(): nonce = easy_box.gen_nonce() assert isinstance(nonce, easy_box.Nonce), type(nonce) assert isinstance(nonce.raw, tf.Tensor) assert nonce.raw.dtype == tf.uint8 assert nonce.raw.shape == (24, )
def test_encrypt_decrypt(self, run_eagerly, x, dtype): context = tf_execution_context(run_eagerly) with context.scope(): ek, dk = paillier.gen_keypair() r = paillier.gen_randomness(ek, shape=x.shape) c = paillier.encrypt(ek, x, r) y = paillier.decrypt(dk, c, dtype=dtype) assert isinstance(y, tf.Tensor) assert y.dtype == dtype np.testing.assert_equal(context.evaluate(y).astype(x.dtype), x)
def test_tf_execution_mode(self, run_eagerly): context = tf_execution_context(run_eagerly) with context.scope(): x = tf.fill(dims=(2, 2), value=5.0) assert tf.executing_eagerly() == run_eagerly assert isinstance(x, tf.Tensor) actual_result = context.evaluate(x) assert isinstance(actual_result, np.ndarray) expected_result = np.array([[5.0, 5.0], [5.0, 5.0]]) np.testing.assert_equal(actual_result, expected_result)
def test_gen_keypair(self, run_eagerly): context = tf_execution_context(run_eagerly) with context.scope(): pk, sk = easy_box.gen_keypair() assert isinstance(pk, easy_box.PublicKey), type(pk) assert isinstance(pk.raw, tf.Tensor) assert pk.raw.dtype == tf.uint8 assert pk.raw.shape == (32, ) assert isinstance(sk, easy_box.SecretKey), type(sk) assert isinstance(sk.raw, tf.Tensor) assert sk.raw.dtype == tf.uint8 assert sk.raw.shape == (32, )
def test_add(self, run_eagerly, dtype, x0, x1): expected = x0 + x1 context = tf_execution_context(run_eagerly) with context.scope(): ek, dk = paillier.gen_keypair() r0 = paillier.gen_randomness(ek, shape=x0.shape) c0 = paillier.encrypt(ek, x0, r0) r1 = paillier.gen_randomness(ek, shape=x1.shape) c1 = paillier.encrypt(ek, x1, r1) c = paillier.add(ek, c0, c1) y = paillier.decrypt(dk, c, dtype=dtype) np.testing.assert_equal( context.evaluate(y).astype(np.int32), expected.astype(np.int32))
def test_correctness(self, run_eagerly): p = 100000015333 q = 100000015021 n = p * q nn = n * n g = 1 + n x = 123456789 r = 5083216764521909821749 c = pow(g, x, nn) * pow(r, n, nn) % nn context = tf_execution_context(run_eagerly) with context.scope(): ek = paillier.EncryptionKey(tf.constant([[str(n)]])) plaintext = np.array([[x]]).astype(str) randomness = paillier.Randomness(tf.constant([[str(r)]])) ciphertext = paillier.encrypt(ek, plaintext, randomness) expected = np.array([[c]]).astype(str) actual = ciphertext.export(tf.string) np.testing.assert_equal(context.evaluate(actual).astype(str), expected)