def test_util_baseconverter_conversion(test, out): base1 = util.RNSBase([sealapi.Modulus(2), sealapi.Modulus(3)]) base2 = util.RNSBase( [sealapi.Modulus(3), sealapi.Modulus(4), sealapi.Modulus(5)]) converter = util.BaseConverter(base1, base2) conv = converter.fast_convert(test) assert conv == out
def test_util_rnstool_sanity(): poly_modulus_degree = 4 coeff_base_count = 4 prime_bit_count = 20 plain = sealapi.Modulus(65537) coeff_base = util.RNSBase( util.get_primes(poly_modulus_degree, prime_bit_count, coeff_base_count)) rns_tool = util.RNSTool(poly_modulus_degree, coeff_base, plain) poly_modulus_degree = 2 plain = sealapi.Modulus(0) coeff_base = util.RNSBase([sealapi.Modulus(3)]) rns_tool = util.RNSTool(poly_modulus_degree, coeff_base, plain)
def test_util_baseconverter_sanity(): base1 = util.RNSBase([sealapi.Modulus(2), sealapi.Modulus(3)]) base2 = util.RNSBase( [sealapi.Modulus(2), sealapi.Modulus(3), sealapi.Modulus(11)]) converter = util.BaseConverter(base1, base2) assert converter.ibase_size() == 2 assert converter.ibase().size() == 2 assert converter.obase_size() == 3 assert converter.obase().size() == 3 base1 = util.RNSBase([sealapi.Modulus(2), sealapi.Modulus(3)]) base2 = util.RNSBase( [sealapi.Modulus(3), sealapi.Modulus(4), sealapi.Modulus(5)]) converter = util.BaseConverter(base1, base2) conv = converter.fast_convert_array([0, 1, 1, 0, 1, 2], 3) assert conv == [0, 1, 2, 0, 3, 1, 0, 2, 0]
def test_util_rns(): with pytest.raises(BaseException): util.RNSBase([sealapi.Modulus(0)]) with pytest.raises(BaseException): util.RNSBase([sealapi.Modulus(0), sealapi.Modulus(3)]) with pytest.raises(BaseException): util.RNSBase( [sealapi.Modulus(2), sealapi.Modulus(3), sealapi.Modulus(4)]) testcase = util.RNSBase([sealapi.Modulus(2), sealapi.Modulus(3)]) assert testcase[0] == 2 assert testcase.size() == 2 assert testcase.contains(sealapi.Modulus(2)) is True assert testcase.contains(sealapi.Modulus(7)) is False base1 = util.RNSBase([sealapi.Modulus(2), sealapi.Modulus(3)]) base2 = util.RNSBase( [sealapi.Modulus(2), sealapi.Modulus(3), sealapi.Modulus(11)]) base3 = util.RNSBase( [sealapi.Modulus(5), sealapi.Modulus(3), sealapi.Modulus(11)]) assert base1.is_subbase_of(base2) is True assert base1.is_proper_subbase_of(base2) is True assert base1.is_subbase_of(base3) is False assert base2.is_superbase_of(base1) is True assert base2.is_proper_superbase_of(base1) is True assert base3.is_superbase_of(base1) is False base1 = base1.extend(sealapi.Modulus(11)) assert base1.size() == 3 base2 = util.RNSBase( [sealapi.Modulus(23), sealapi.Modulus(37), sealapi.Modulus(53)]) base1 = base1.extend(base2) assert base1.size() == 6 base1 = base1.drop(sealapi.Modulus(11)) assert base1.size() == 5 base1 = base1.drop() base1 = base1.drop() assert base1.size() == 3 base = util.RNSBase([sealapi.Modulus(3), sealapi.Modulus(5)]) dec = base.decompose([14, 0]) assert dec == [2, 4] comp = base.compose(dec) assert comp == [14, 0] base = util.RNSBase([sealapi.Modulus(3), sealapi.Modulus(5)]) dec = base.decompose_array([14, 0], 1) assert dec == [4, 0] comp = base.compose_array(dec, 1) assert comp == [10, 0] assert base.base_prod() == 15
def test_util_rnstool_sanity(): poly_modulus_degree = 4 coeff_base_count = 4 prime_bit_count = 20 plain = sealapi.Modulus(65537) coeff_base = util.RNSBase( util.get_primes(poly_modulus_degree, prime_bit_count, coeff_base_count)) rns_tool = util.RNSTool(poly_modulus_degree, coeff_base, plain) poly_modulus_degree = 2 plain = sealapi.Modulus(0) coeff_base = util.RNSBase([sealapi.Modulus(3)]) rns_tool = util.RNSTool(poly_modulus_degree, coeff_base, plain) input = [1, 2, 1, 2] dest = rns_tool.fastbconv_sk(input, poly_modulus_degree) assert dest == [1, 2] input = [ rns_tool.m_tilde().value(), 2 * rns_tool.m_tilde().value(), rns_tool.m_tilde().value(), 2 * rns_tool.m_tilde().value(), 0, 0, ] dest = rns_tool.sm_mrq(input, poly_modulus_degree) assert dest == [1, 2, 1, 2] input = [15, 3, 15, 3, 15, 3] dest = rns_tool.fast_floor(input, poly_modulus_degree) assert dest == [5, 1, 5, 1] input = [1, 2] dest = rns_tool.fastbconv_m_tilde(input, poly_modulus_degree) assert dest == [1, 2, 1, 2, 1, 2] poly_modulus_degree = 2 plain = sealapi.Modulus(3) coeff_base = util.RNSBase([sealapi.Modulus(5), sealapi.Modulus(7)]) rns_tool = util.RNSTool(poly_modulus_degree, coeff_base, plain) input = [35, 70, 35, 70] dest = rns_tool.decrypt_scale_and_round(input, poly_modulus_degree) assert dest == [0, 0, 0, 0] input = [29, 30 + 35, 29, 30 + 35] dest = rns_tool.decrypt_scale_and_round(input, poly_modulus_degree) assert dest == [2, 0, 0, 0] poly_modulus_degree = 2 plain = sealapi.Modulus(0) coeff_base = util.RNSBase([sealapi.Modulus(13), sealapi.Modulus(7)]) rns_tool = util.RNSTool(poly_modulus_degree, coeff_base, plain) input = [12, 11, 4, 3] dest = rns_tool.divide_and_round_q_last_inplace(input, poly_modulus_degree) assert dest == [4, 3, 0, 6] poly_modulus_degree = 2 plain = sealapi.Modulus(0) coeff_base = util.RNSBase([sealapi.Modulus(53), sealapi.Modulus(13)]) rns_tool = util.RNSTool(poly_modulus_degree, coeff_base, plain) input = [4, 12, 4, 12] dest = rns_tool.divide_and_round_q_last_inplace(input, poly_modulus_degree) assert dest == [0, 1, 10, 5]