code: # pylint: disable=missing-docstring
  import unittest
  
  import numpy as np
  import tensorflow as tf
  from absl.testing import parameterized
  
  from tf_encrypted.primitives import paillier
  from tf_encrypted.test import tf_execution_context
  
  
  class EncryptionTest(parameterized.TestCase):
      @parameterized.parameters(
          {
              "run_eagerly": run_eagerly,
              "export_dtype": export_dtype,
              "export_expansion": export_expansion,
          }
          for run_eagerly in [True, False]
          for export_dtype, export_expansion in [(tf.string, ())]
      )
      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
  
      @parameterized.parameters(
          {"run_eagerly": run_eagerly} for run_eagerly in (True, False)
      )
      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)
  
      @parameterized.parameters(
          {"run_eagerly": run_eagerly, "x": x, "dtype": dtype}
          for run_eagerly in [True, False]
          for x, dtype in [
              (np.array([[12345, 34342]]).astype(np.int32), tf.int32),
              (np.array([["12345", "34342"]]).astype(str), tf.string),
              (
                  np.array(
                      [
                          [
                              "123456789123456789123456789123456789",
                              "987654321987654321987654321987654321",
                          ]
                      ]
                  ).astype(str),
                  tf.string,
              ),
          ]
      )
      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)
  
      @parameterized.parameters(
          {"run_eagerly": run_eagerly, "dtype": dtype, "x0": x0, "x1": x1}
          for run_eagerly in (True, False)
          for dtype in (tf.int32, tf.string)
          for x0 in (np.array([[12345, 123243]]), np.array([[12345]]))
          for x1 in (np.array([[12656, 434234]]),)
      )
      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)
          )
  
  
  if __name__ == "__main__":
      unittest.main()
   # pylint: disable=missing-docstring
  import os
  import unittest
  
  import numpy as np
  import tensorflow as tf
  from tensorflow.python.framework import errors
  from tensorflow.python.framework.errors import NotFoundError
  
  import tf_encrypted as tfe
  
  dirname = os.path.dirname(tfe.__file__)
  so_name = "{dn}/operations/secure_random/secure_random_module_tf_{tfv}.so"
  shared_object = so_name.format(dn=dirname, tfv=tf.__version__)
  notfound_msg = "secure_random_module not found"
  
  try:
      secure_random_module = tf.load_op_library(shared_object)
      seeded_random_uniform = secure_random_module.secure_seeded_random_uniform
      random_uniform = secure_random_module.secure_random_uniform
      seed = secure_random_module.secure_seed
  except NotFoundError:
      secure_random_module = None
  
  
  @unittest.skipIf(secure_random_module is None, notfound_msg)
  class TestSeededRandomUniform(unittest.TestCase):
      def test_int32_return(self):
          expected = [[608, 425, 925], [198, 891, 721]]
  
          with tf.Session():
              output = seeded_random_uniform(
                  [2, 3], [1, 1, 1, 1, 1, 1, 1, 2], 0, 1000
              ).eval()
  
              np.testing.assert_array_equal(output, expected)
  
      def test_int64_return(self):
          expected = [[425, 198, 721], [911, 617, 113]]
  
          with tf.Session():
              minval = tf.constant(0, dtype=tf.int64)
              maxval = tf.constant(1000, dtype=tf.int64)
  
              output = seeded_random_uniform(
                  [2, 3], [1, 1, 1, 1, 1, 1, 1, 2], minval, maxval
              ).eval()
  
              np.testing.assert_array_equal(output, expected)
  
      def test_min_max_range(self):
          with tf.Session():
              minval = tf.constant(-100000000, dtype=tf.int32)
              maxval = tf.constant(100000000, dtype=tf.int32)
  
              output = seeded_random_uniform(
                  [10000], [1, 1, 1, 500, 1, 1, 1, 2], minval, maxval
              ).eval()
  
              for out in output:
                  assert -100000000 <= out < 100000000
  
      def test_invalid_max_min(self):
          with tf.Session():
              minval = tf.constant(1000, dtype=tf.int64)
              maxval = tf.constant(-1000, dtype=tf.int64)
  
              with np.testing.assert_raises(errors.InvalidArgumentError):
                  seeded_random_uniform(
                      [2, 3], [1, 1, 1, 1, 1, 1, 1, 2], minval, maxval
                  ).eval()
  
      def test_negative_numbers(self):
          expected = [[-1575, -1802, -1279], [-1089, -1383, -1887]]
          with tf.Session():
              minval = tf.constant(-2000, dtype=tf.int64)
              maxval = tf.constant(-1000, dtype=tf.int64)
  
              output = seeded_random_uniform(
                  [2, 3], [1, 1, 1, 1, 1, 1, 1, 2], minval, maxval
              ).eval()
  
              np.testing.assert_array_equal(output, expected)
  
  
  @unittest.skipIf(secure_random_module is None, notfound_msg)
  class TestRandomUniform(unittest.TestCase):
      def test_min_max_range(self):
          with tf.Session():
              minval = tf.constant(-10000000, dtype=tf.int32)
              maxval = tf.constant(10000000, dtype=tf.int32)
  
              output = random_uniform([1000], minval, maxval).eval()
  
              for out in output:
                  assert -10000000 <= out < 10000000
  
      def test_small_range(self):
          with tf.Session():
              minval = tf.constant(-10, dtype=tf.int32)
              maxval = tf.constant(10, dtype=tf.int32)
  
              output = random_uniform([1000], minval, maxval).eval()
  
              for out in output:
                  assert -10 <= out < 10
  
      def test_neg_range(self):
          with tf.Session():
              minval = tf.constant(-100, dtype=tf.int32)
              maxval = tf.constant(0, dtype=tf.int32)
  
              output = random_uniform([1000], minval, maxval).eval()
  
              for out in output:
                  assert out < 0
  
  
  @unittest.skipIf(secure_random_module is None, notfound_msg)
  class TestSeed(unittest.TestCase):
      def test_seed(self):
          with tf.Session():
              s = seed()
  
              minval = tf.constant(-2000, dtype=tf.int64)
              maxval = tf.constant(0, dtype=tf.int64)
  
              shape = [2, 3]
  
              output = seeded_random_uniform(shape, s, minval, maxval).eval()
  
              np.testing.assert_array_equal(output.shape, shape)
  
  
  if __name__ == "__main__":
      unittest.main()
  
apis: numpy.array
apis: numpy.testing.assert_raises tensorflow.load_op_library numpy.testing.assert_array_equal tensorflow.Session tensorflow.constant