示例#1
0
    def test_chinese_preconditioned(self):
        moduli = [3, 5, 7]
        preconditioning_data = crt_preconditioning_data(moduli)

        system = [(2, 3), (3, 5), (2, 7)]
        solution = chinese_preconditioned(system, preconditioning_data)
        self.assertEqual(solution, 23)

        system = [(1, 3), (2, 5), (4, 7)]
        solution = chinese_preconditioned(system, preconditioning_data)
        self.assertEqual(solution, 67)
示例#2
0
    def test_chinese_preconditioned(self):
        moduli = [3, 5, 7]
        preconditioning_data = crt_preconditioning_data(moduli)

        system = [(2, 3), (3, 5), (2, 7)]
        solution = chinese_preconditioned(system, preconditioning_data)
        self.assertEqual(solution, 23)

        system = [(1, 3), (2, 5), (4, 7)]
        solution = chinese_preconditioned(system, preconditioning_data)
        self.assertEqual(solution, 67)
示例#3
0
def sqrts_mod_n(a, n, n_factorization=None):
    """Returns the solutions x to the congruence x**2 = a (mod n).

    Given integers a and n, this function returns all solutions to the
    congruence x**2 = a (mod n).

    Input:
        * a: int
            A square modulo n.

        * n: int
            The modulus.

        * n_factorization: list (default=None)
            The factorization of n.

    Returns:
        * roots: list
            A sorted list of all square roots of a modulo n.

    Examples:
        >>> sqrts_mod_n(3, 11**3*23**3)
        [16152263, 28026114, 150110933, 161984784]
        >>> sqrts_mod_n(49, 53**3*61**4)
        [7, 721465236980, 1339862033577, 2061327270550]
        >>> sqrts_mod_n(-1, 5**3*7**2)
        []
    """
    if n_factorization is None:
        n_factorization = factor.factor(n)

    congruences = []
    moduli = []
    for (p, k) in n_factorization:
        roots = _sqrts_mod_pk(a, p, k)
        pk = p**k
        pairs = [(r, pk) for r in roots]
        congruences.append(pairs)
        moduli.append(pk)

    if len(congruences) == 1:
        values = [r for (r, _) in congruences[0]]
    else:
        preconditioning_data = crt_preconditioning_data(moduli)
        values = []

        for system in itertools.product(*congruences):
            values.append(chinese_preconditioned(system, preconditioning_data))

    values.sort()
    return values
示例#4
0
def sqrts_mod_n(a, n, n_factorization=None):
    """Returns the solutions x to the congruence x**2 = a (mod n).

    Given integers a and n, this function returns all solutions to the
    congruence x**2 = a (mod n).

    Input:
        * a: int
            A square modulo n.

        * n: int
            The modulus.

        * n_factorization: list (default=None)
            The factorization of n.

    Returns:
        * roots: list
            A sorted list of all square roots of a modulo n.

    Examples:
        >>> sqrts_mod_n(3, 11**3*23**3)
        [16152263, 28026114, 150110933, 161984784]
        >>> sqrts_mod_n(49, 53**3*61**4)
        [7, 721465236980, 1339862033577, 2061327270550]
        >>> sqrts_mod_n(-1, 5**3*7**2)
        []
    """
    if n_factorization is None:
        n_factorization = factor.factor(n)

    congruences = []
    moduli = []
    for (p, k) in n_factorization:
        roots = _sqrts_mod_pk(a, p, k)
        pk = p**k
        pairs = [(r, pk) for r in roots]
        congruences.append(pairs)
        moduli.append(pk)

    if len(congruences) == 1:
        values = [r for (r, _) in congruences[0]]
    else:
        preconditioning_data = crt_preconditioning_data(moduli)
        values = []

        for system in itertools.product(*congruences):
            values.append(chinese_preconditioned(system, preconditioning_data))

    values.sort()
    return values
示例#5
0
def sqrts_mod_n(a, n, n_factorization=None):
    """
    Returns all solutions x, 1 <= x <= n, to the congruence x^2 = a (mod n).

    Input:
        * a: int
            A square modulo n.

        * n: int
            The modulus.

        * n_factorization: list (default=None)
            The factorization of n.

    Output:
        * roots: list
            A list of all square roots of a modulo n.
    """
    if n_factorization is None:
        n_factorization = rosemary.number_theory.factorization.factor(n)

    congruences = []
    moduli = []
    for (p, k) in n_factorization:
        roots = sqrts_mod_pk(a, p, k)
        pk = p**k
        pairs = [(r, pk) for r in roots]
        congruences.append(pairs)
        moduli.append(pk)

    if len(congruences) == 1:
        values = [r for (r, _) in congruences[0]]
    else:
        preconditioning_data = crt_preconditioning_data(moduli)
        values = []

        for system in itertools.product(*congruences):
            values.append(chinese_preconditioned(system, preconditioning_data))

    values.sort()
    return values