def test_limb_conversion(self, run_eagerly, x_np, tf_type, max_bitlen, tf_shape): context = tf_execution_context(run_eagerly) with context.scope(): x = import_tensor(x_np) assert x.shape.as_list() == [1, 2], x.shape x_limbs = export_limbs_tensor(x, dtype=tf_type, max_bitlen=max_bitlen) assert x_limbs.shape.as_list() == x.shape.as_list() + ( [tf_shape] if run_eagerly else [None]), x_limbs.shape x_norm = import_limbs_tensor(x_limbs) assert x_norm.shape.as_list() == x.shape.as_list(), x_norm.shape y = import_tensor(np.array([[30, 40]])) assert y.shape.as_list() == [1, 2], y.shape y_limbs = export_limbs_tensor(y, dtype=tf_type, max_bitlen=max_bitlen) assert y_limbs.shape.as_list() == y.shape.as_list() + ( [tf_shape] if run_eagerly else [None]), y_limbs.shape y_norm = import_limbs_tensor(y_limbs) assert y_norm.shape.as_list() == y.shape.as_list(), y_norm.shape z = x_norm + y_norm res = export_tensor(z) np.testing.assert_array_equal( context.evaluate(res).astype(str), np.array([["40", "60"]]))
def test_is_tensor(self, run_eagerly): context = tf_execution_context(run_eagerly) with context.scope(): x = import_tensor(np.array([[10, 20]])) assert tf.is_tensor(x)
def test_inv(self, run_eagerly): def egcd(a, b): if a == 0: return (b, 0, 1) g, y, x = egcd(b % a, a) return (g, x - (b // a) * y, y) def inv(a, m): g, b, _ = egcd(a, m) return b % m x_raw = np.array([[123456789123456789123456789]]) n_raw = np.array([[10000000]]) y_raw = np.array([[inv(123456789123456789123456789, 10000000)]]) context = tf_execution_context(run_eagerly) with context.scope(): x = import_tensor(x_raw) n = import_tensor(n_raw) y = x.inv(n) y = export_tensor(y) np.testing.assert_array_equal( context.evaluate(y).astype(str), y_raw.astype(str))
def test_convert_to_tensor(self): context = tf_execution_context(False) with context.scope(): x = import_tensor(np.array([[10, 20]])) y = tf.convert_to_tensor(x) assert y.dtype is tf.string
def test_register_tensor_conversion_function(self): context = tf_execution_context(False) with context.scope(): x = import_tensor(np.array([[10, 20]])) y = tf.convert_to_tensor(np.array([[30, 40]])) z = x + y np.testing.assert_array_equal(context.evaluate(z), np.array([["40", "60"]]))
def test_uniform_random(self, run_eagerly): shape = (2, 2) maxval = 2**100 context = tf_execution_context(run_eagerly) with context.scope(): x = random_uniform(shape=shape, maxval=maxval) x = export_tensor(x) assert x.shape == shape assert context.evaluate(x).shape == shape
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_eval(self, run_eagerly): x_raw = np.array( [[123456789123456789123456789, 123456789123456789123456789]]) context = tf_execution_context(run_eagerly) with context.scope(): x = import_tensor(x_raw) assert x.shape == x_raw.shape x = export_tensor(x) assert x.shape == x_raw.shape np.testing.assert_array_equal( context.evaluate(x).astype(str), x_raw.astype(str))
def test_op(self, run_eagerly, op_name, op, x_raw, y_raw): z_raw = op(x_raw, y_raw) context = tf_execution_context(run_eagerly) with context.scope(): x = import_tensor(x_raw) y = import_tensor(y_raw) z = op(x, y) z = export_tensor(z) np.testing.assert_array_equal( context.evaluate(z).astype(str), z_raw.astype(str))
def test_matmul(self, run_eagerly): a = np.array([[5, 5], [5, 5]]).astype(np.int32) b = np.array([[6, 6], [6, 6]]).astype(np.int32) expected = a.dot(b) context = tf_execution_context(run_eagerly) with context.scope(): a_var = big_import(a) b_var = big_import(b) c_var = big_matmul(a_var, b_var) c_str = big_export(c_var, tf.int32) np.testing.assert_equal(context.evaluate(c_str), expected)
def test_add(self, run_eagerly): a = "5453452435245245245242534" b = "1424132412341234123412341234134" expected = int(a) + int(b) context = tf_execution_context(run_eagerly) with context.scope(): a_var = big_import([[a]]) b_var = big_import([[b]]) c_var = big_add(a_var, b_var) c_str = big_export(c_var, tf.string) np.testing.assert_equal(int(context.evaluate(c_str)), expected)
def test_mod(self, run_eagerly): x = np.array([[123, 234], [345, 456]]).astype(np.int32) n = np.array([[37]]).astype(np.int32) expected = x % n context = tf_execution_context(run_eagerly) with context.scope(): x_big = big_import(x) n_big = big_import(n) y_big = big_mod(x_big, n_big) y_str = big_export(y_big, tf.int32) np.testing.assert_equal(context.evaluate(y_str), expected)
def test_pow(self, run_eagerly, secure): base = "54" exp = "3434" modulus = "35" expected = pow(54, 3434, 35) context = tf_execution_context(run_eagerly) with context.scope(): base_var = big_import([[base]]) exp_var = big_import([[exp]]) mod_var = big_import([[modulus]]) out = big_pow(base_var, exp_var, mod_var, secure=secure) out_str = big_export(out, tf.string) np.testing.assert_equal(int(context.evaluate(out_str)), expected)
def test_mod(self, run_eagerly): x_raw = np.array( [[123456789123456789123456789, 123456789123456789123456789]]) n_raw = np.array([[10000]]) y_raw = x_raw % n_raw context = tf_execution_context(run_eagerly) with context.scope(): x = import_tensor(x_raw) n = import_tensor(n_raw) y = x % n y = export_tensor(y) np.testing.assert_array_equal( context.evaluate(y).astype(str), y_raw.astype(str))
def test_pow(self, run_eagerly, x_raw, y_raw): m_raw = np.array([[5]]) z_raw = np.mod(np.power(x_raw, y_raw), m_raw) context = tf_execution_context(run_eagerly) with context.scope(): x = import_tensor(x_raw) y = import_tensor(y_raw) m = import_tensor(m_raw) z = pow(x, y, m) z = export_tensor(z) np.testing.assert_array_equal( context.evaluate(z).astype(str), z_raw.astype(str))
def test_random_rsa_modulus(self, run_eagerly): bitlength = 128 expected_shape = (1, 1) context = tf_execution_context(run_eagerly) with context.scope(): p, q, n = random_rsa_modulus(bitlength=bitlength) p = export_tensor(p) q = export_tensor(q) n = export_tensor(n) assert p.shape == expected_shape assert q.shape == expected_shape assert n.shape == expected_shape assert isinstance(context.evaluate(p)[0][0], bytes) assert isinstance(context.evaluate(q)[0][0], bytes) assert isinstance(context.evaluate(n)[0][0], bytes)
def test_foo( self, x, tf_cast, np_cast, expected, convert_to_tf_tensor, run_eagerly, ): context = tf_execution_context(run_eagerly) with context.scope(): y = tf.convert_to_tensor(x) if convert_to_tf_tensor else x y = import_tensor(y) z = export_tensor(y, dtype=tf_cast) actual = context.evaluate(z) actual = actual.astype(np_cast) if np_cast else actual assert (actual.dtype == expected.dtype ), "'{}' did not match expected '{}'".format( actual.dtype, expected.dtype) np.testing.assert_array_equal(actual, expected)
def test_import_export(self, run_eagerly, raw, dtype): context = tf_execution_context(run_eagerly) with context.scope(): variant = big_import(raw) output = big_export(variant, dtype) assert context.evaluate(output) == raw