示例#1
0
def base_solution_linear(c, a, b, t=None):
    """
    Return the base solution for a linear diophantine equation with two
    variables. Called repeatedly by diop_linear().

    Usage
    =====

        base_solution_linear(c, a, b, t) -> a, b, c are Integers as in a*x + b*y = c
        and t is the parameter to be used in the solution.

    Details
    =======

        ``c`` is the constant term in a*x + b*y = c
        ``a`` is the integer coefficient of x in a*x + b*y = c
        ``b`` is the integer coefficient of y in a*x + b*y = c
        ``t`` is the parameter to be used in the solution


    Examples
    ========

    >>> from sympy.solvers.diophantine import base_solution_linear
    >>> from sympy.abc import t
    >>> base_solution_linear(5, 2, 3) # equation 2*x + 3*y = 5
    (-5, 5)
    >>> base_solution_linear(0, 5, 7) # equation 5*x + 7*y = 0
    (0, 0)
    >>> base_solution_linear(5, 2, 3, t) # equation 2*x + 3*y = 5
    (3*t - 5, -2*t + 5)
    >>> base_solution_linear(0, 5, 7, t) # equation 5*x + 7*y = 0
    (7*t, -5*t)
    """
    d = igcd(a, igcd(b, c))
    a = a // d
    b = b // d
    c = c // d

    if c == 0:
        if t != None:
            return (b*t , -a*t)
        else:
            return (S.Zero, S.Zero)
    else:
        x0, y0, d = extended_euclid(int(abs(a)), int(abs(b)))

        x0 = x0 * sign(a)
        y0 = y0 * sign(b)

        if divisible(c, d):
            if t != None:
                return (c*x0 + b*t, c*y0 - a*t)
            else:
                return (Integer(c*x0), Integer(c*y0))
        else:
            return (None, None)
示例#2
0
def base_solution_linear(c, a, b, t=None):
    """
    Return the base solution for a linear diophantine equation with two
    variables. Called repeatedly by diop_linear().

    Usage
    =====

        base_solution_linear(c, a, b, t) -> a, b, c are Integers as in a*x + b*y = c
        and t is the parameter to be used in the solution.

    Details
    =======

        ``c`` is the constant term in a*x + b*y = c
        ``a`` is the integer coefficient of x in a*x + b*y = c
        ``b`` is the integer coefficient of y in a*x + b*y = c
        ``t`` is the parameter to be used in the solution


    Examples
    ========

    >>> from sympy.solvers.diophantine import base_solution_linear
    >>> from sympy.abc import t
    >>> base_solution_linear(5, 2, 3) # equation 2*x + 3*y = 5
    (-5, 5)
    >>> base_solution_linear(0, 5, 7) # equation 5*x + 7*y = 0
    (0, 0)
    >>> base_solution_linear(5, 2, 3, t) # equation 2*x + 3*y = 5
    (3*t - 5, -2*t + 5)
    >>> base_solution_linear(0, 5, 7, t) # equation 5*x + 7*y = 0
    (7*t, -5*t)
    """
    d = igcd(a, igcd(b, c))
    a = a // d
    b = b // d
    c = c // d

    if c == 0:
        if t != None:
            return (b * t, -a * t)
        else:
            return (S.Zero, S.Zero)
    else:
        x0, y0, d = extended_euclid(int(abs(a)), int(abs(b)))

        x0 = x0 * sign(a)
        y0 = y0 * sign(b)

        if divisible(c, d):
            if t != None:
                return (c * x0 + b * t, c * y0 - a * t)
            else:
                return (Integer(c * x0), Integer(c * y0))
        else:
            return (None, None)
def key_generator():
    # Se define el rango para las semillas
    lowest_range = 1000
    highest_range = 9999

    # Se generan semillas en un rango para ingresar a la función generadora de primos
    p_seed = random.randint(lowest_range, highest_range)
    p = sympy.nextprime(p_seed)

    q_seed = random.randint(lowest_range, highest_range)
    q = sympy.nextprime(q_seed)

    # Se valida que p y q no sean iguales
    while q == p:
        q_seed = random.randint(lowest_range, highest_range)
        q = sympy.nextprime(q_seed)

    # Se calcula n y phi
    n = p * q
    phi = (p - 1) * (q - 1)

    # Se genera aleatoriamente un e y se valida que sea coprimo con phi
    e = random.randint(lowest_range, phi)

    while sympy.igcd(e, phi) != 1:
        e = random.randint(lowest_range, phi)

    # Ahora se calcula d
    d = gmpy.invert(e, phi)

    return p, q, n, e, d
示例#4
0
def n_order(a, n, f):
    from collections import defaultdict
    a, n = as_int(a), as_int(n)
    if igcd(a, n) != 1:
        raise ValueError("The two numbers should be relatively prime")
    factors = defaultdict(int)
    for px, kx in f.items():
        if kx > 1:
            factors[px] += kx - 1
        fpx = factorint(px - 1)
        for py, ky in fpx.items():
            factors[py] += ky
    group_order = 1
    for px, kx in factors.items():
        group_order *= px**kx
    order = 1
    if a > n:
        a = a % n
    for p, e in factors.items():
        exponent = group_order
        for f in range(e + 1):
            if pow(a, exponent, n) != 1:
                order *= p**(e - f + 1)
                break
            exponent = exponent // p
    return order
示例#5
0
def generate_session_parameters():
    global sess_param_computed
    delta = 1
    alpha = 1
    beta = 1
    temp_q = 1

    for _ in range(m):
        p = sympy.sieve[random.randrange(psize)]
        pm.append(p)
        temp_q *= p

    for _ in range(n):
        p = sympy.sieve[random.randrange(psize)]
        while p in pm:
            p = sympy.sieve[random.randrange(psize)]
        pn.append(p)
        delta *= pow(p, 3)
        alpha *= pow(p, 2)*(p-1)
        beta *= p*(p-1)*temp_q

    y = random.randrange(delta)
    while sympy.igcd(y, delta) != 1:
        y = random.randrange(delta)

    sess_param_computed = True
    params["alpha"] = alpha
    params["delta"] = delta
    params["beta"] = beta
    params["totient_delta"] = totient(delta)
    params["y"] = y
def smallest_root(fraction):
    factorization = sp.factorrat(S(fraction))
    powers = [v for k, v in factorization.items()]
    gcd = 1
    if len(powers) > 1:
        gcd = sp.igcd(*powers)
    if len(powers) == 1:
        gcd = abs(powers[0])
    result = Fraction(1, 1)
    for k, v in factorization.items():
        result *= Fraction(k, 1)**int(v / gcd)
    return result, gcd
示例#7
0
def pollard_pm1(n, B=10, a=2, retries=0, seed=1234):
    n = int(n)
    if n < 4 or B < 3:
        raise ValueError('pollard_pm1 should receive n > 3 and B > 2')
    for i in range(retries + 1):
        aM = a
        for p in sieve.primerange(2, B + 1):
            e = int(math.log(B, p))
            aM = pow(aM, pow(p, e), n)
        g = igcd(aM - 1, n)
        if g == n:
            return True
        elif g == 1:
            return False
        elif 1 < g < n:
            return int(g)
示例#8
0
def _transformation_to_pell(var, coeff):

    x = var[0]
    y = var[1]

    a = coeff[x**2]
    b = coeff[x * y]
    c = coeff[y**2]
    d = coeff[x]
    e = coeff[y]
    f = coeff[Integer(1)]

    g = igcd(a, igcd(b, igcd(c, igcd(d, igcd(e, f)))))
    a = a // g
    b = b // g
    c = c // g
    d = d // g
    e = e // g
    f = f // g

    X, Y = symbols("X, Y", integer=True)

    if b != Integer(0):
        B = (S(2 * a) / b).p
        C = (S(2 * a) / b).q
        A = (S(a) / B**2).p
        T = (S(a) / B**2).q

        # eq_1 = A*B*X**2 + B*(c*T - A*C**2)*Y**2 + d*T*X + (B*e*T - d*T*C)*Y + f*T*B
        coeff = {
            X**2: A * B,
            X * Y: 0,
            Y**2: B * (c * T - A * C**2),
            X: d * T,
            Y: B * e * T - d * T * C,
            Integer(1): f * T * B
        }
        A_0, B_0 = _transformation_to_pell([X, Y], coeff)
        return Matrix(2, 2, [S(1) / B, -S(C) / B, 0, 1]) * A_0, Matrix(
            2, 2, [S(1) / B, -S(C) / B, 0, 1]) * B_0

    else:
        if d != Integer(0):
            B = (S(2 * a) / d).p
            C = (S(2 * a) / d).q
            A = (S(a) / B**2).p
            T = (S(a) / B**2).q

            # eq_2 = A*X**2 + c*T*Y**2 + e*T*Y + f*T - A*C**2
            coeff = {
                X**2: A,
                X * Y: 0,
                Y**2: c * T,
                X: 0,
                Y: e * T,
                Integer(1): f * T - A * C**2
            }
            A_0, B_0 = _transformation_to_pell([X, Y], coeff)
            return Matrix(2, 2, [S(1) / B, 0, 0, 1]) * A_0, Matrix(
                2, 2, [S(1) / B, 0, 0, 1]) * B_0 + Matrix([-S(C) / B, 0])

        else:
            if e != Integer(0):
                B = (S(2 * c) / e).p
                C = (S(2 * c) / e).q
                A = (S(c) / B**2).p
                T = (S(c) / B**2).q

                # eq_3 = a*T*X**2 + A*Y**2 + f*T - A*C**2
                coeff = {
                    X**2: a * T,
                    X * Y: 0,
                    Y**2: A,
                    X: 0,
                    Y: 0,
                    Integer(1): f * T - A * C**2
                }
                A_0, B_0 = _transformation_to_pell([X, Y], coeff)
                return Matrix(2, 2, [1, 0, 0, S(1) / B]) * A_0, Matrix(
                    2, 2, [1, 0, 0, S(1) / B]) * B_0 + Matrix([0, -S(C) / B])

            else:
                # TODO: pre-simplification: Not necessary but may simplify
                # the equation.

                return Matrix(2, 2, [S(1) / a, 0, 0, 1]), Matrix([0, 0])
示例#9
0
def diop_quadratic(var, coeff, t):
    """
    Solves quadratic diophantine equations, i.e equations of the form
    Ax**2 + Bxy + Cy**2 + Dx + Ey + F = 0. Returns a set containing
    the tuples (x, y) which contains the solutions.

    Usage
    =====

        diop_quadratic(var, coeff) -> var is a list of variables and
        coeff is a dictionary containing coefficients of the symbols.

    Details
    =======

        ``var`` a list which contains two variables x and y.
        ``coeff`` a dict which generally contains six key value pairs.
        The set of keys is {x**2, y**2, x*y, x, y, Integer(1)}.
        ``t`` the parameter to be used in the solution.

    Examples
    ========

    >>> from sympy.abc import x, y, t
    >>> from sympy import Integer
    >>> from sympy.solvers.diophantine import diop_quadratic
    >>> diop_quadratic([x, y], {x**2: 1, y**2: 1, x*y: 0, x: 2, y: 2, Integer(1): 2}, t)
    set([(-1, -1)])

    References
    ==========

    .. [1] http://www.alpertron.com.ar/METHODS.HTM
    """
    x = var[0]
    y = var[1]

    for term in [x**2, y**2, x*y, x, y, Integer(1)]:
        if term not in coeff.keys():
            coeff[term] = Integer(0)

    A = coeff[x**2]
    B = coeff[x*y]
    C = coeff[y**2]
    D = coeff[x]
    E = coeff[y]
    F = coeff[Integer(1)]

    d = igcd(A, igcd(B, igcd(C, igcd(D, igcd(E, F)))))
    A = A // d
    B = B // d
    C = C // d
    D = D // d
    E = E // d
    F = F // d

    # (1) Linear case: A = B = C = 0 -> considered under linear diophantine equations

    # (2) Simple-Hyperbolic case:A = C = 0, B != 0
    # In this case equation can be converted to (Bx + E)(By + D) = DE - BF
    # We consider two cases; DE - BF = 0 and DE - BF != 0
    # More details, http://www.alpertron.com.ar/METHODS.HTM#SHyperb

    l = set([])

    if A == 0 and C == 0 and B != 0:

        if D*E - B*F == 0:
            if divisible(int(E), int(B)):
                l.add((-E/B, t))
            if divisible(int(D), int(B)):
                l.add((t, -D/B))

        else:
            div = divisors(D*E - B*F)
            div = div + [-term for term in div]

            for d in div:
                if divisible(int(d - E), int(B)):
                    x0  = (d - E) // B
                    if divisible(int(D*E - B*F), int(d)):
                        if divisible(int((D*E - B*F)// d - D), int(B)):
                            y0 = ((D*E - B*F) // d - D) // B
                            l.add((x0, y0))

    # (3) Elliptical case: B**2 - 4AC < 0
    # More Details, http://www.alpertron.com.ar/METHODS.HTM#Ellipse
    # In this case x should lie between the roots of
    # (B**2 - 4AC)x**2 + 2(BE - 2CD)x + (E**2 - 4CF) = 0

    elif B**2 - 4*A*C < 0:

        z = symbols("z", real=True)
        roots = solve((B**2 - 4*A*C)*z**2 + 2*(B*E - 2*C*D)*z + E**2 - 4*C*F)

        solve_y = lambda x, e: (-(B*x + E) + e*sqrt((B*x + E)**2 - 4*C*(A*x**2 + D*x + F)))/(2*C)

        if len(roots) == 1 and isinstance(roots[0], Integer):
            x_vals = [roots[0]]
        elif len(roots) == 2:
            x_vals = [i for i in range(ceiling(min(roots)), ceiling(max(roots)))] # ceiling = floor +/- 1
        else:
            x_vals = []

        for x0 in x_vals:
            if isinstance(sqrt((B*x0 + E)**2 - 4*C*(A*x0**2 + D*x0 + F)), Integer):
                if isinstance(solve_y(x0, 1), Integer):
                    l.add((Integer(x0), solve_y(x0, 1)))
                if isinstance(solve_y(x0, -1), Integer):
                    l.add((Integer(x0), solve_y(x0, -1)))

    # (4) Parabolic case: B**2 - 4*A*C = 0
    # There are two subcases to be considered in this case.
    # sqrt(c)D - sqrt(a)E = 0 and sqrt(c)D - sqrt(a)E != 0
    # More Details, http://www.alpertron.com.ar/METHODS.HTM#Parabol

    elif B**2 - 4*A*C == 0:

        g = igcd(A, C)
        g = abs(g) * sign(A)
        a = A // g
        b = B // g
        c = C // g
        e = sign(B/A)


        if e*sqrt(c)*D - sqrt(a)*E == 0:
            z = symbols("z", real=True)
            roots = solve(sqrt(a)*g*z**2 + D*z + sqrt(a)*F)

            for root in roots:
                if isinstance(root, Integer):
                    l.add((diop_solve(sqrt(a)*x + e*sqrt(c)*y - root)[x], diop_solve(sqrt(a)*x + e*sqrt(c)*y - root)[y]))

        elif isinstance(e*sqrt(c)*D - sqrt(a)*E, Integer):
            solve_x = lambda u: e*sqrt(c)*g*(sqrt(a)*E - e*sqrt(c)*D)*t**2 - (E + 2*e*sqrt(c)*g*u)*t\
                - (e*sqrt(c)*g*u**2 + E*u + e*sqrt(c)*F) // (e*sqrt(c)*D - sqrt(a)*E)

            solve_y = lambda u: sqrt(a)*g*(e*sqrt(c)*D - sqrt(a)*E)*t**2 + (D + 2*sqrt(a)*g*u)*t \
                + (sqrt(a)*g*u**2 + D*u + sqrt(a)*F) // (e*sqrt(c)*D - sqrt(a)*E)

            for z0 in range(0, abs(e*sqrt(c)*D - sqrt(a)*E)):
                if divisible(sqrt(a)*g*z0**2 + D*z0 + sqrt(a)*F, e*sqrt(c)*D - sqrt(a)*E):
                    l.add((solve_x(z0), solve_y(z0)))

    # (5) B**2 - 4*A*C > 0

    elif B**2 - 4*A*C > 0:
        # Method used when B**2 - 4*A*C is a square, is descibed in p. 6 of the below paper
        # by John P. Robertson.
        # http://www.jpr2718.org/ax2p.pdf

        if isinstance(sqrt(B**2 - 4*A*C), Integer):
            if A != 0:
                r = sqrt(B**2 - 4*A*C)
                u, v = symbols("u, v", integer=True)
                eq = simplify(4*A*r*u*v + 4*A*D*(B*v + r*u + r*v - B*u) + 2*A*4*A*E*(u - v) + 4*A*r*4*A*F)

                sol = diop_solve(eq, t)
                sol = list(sol)

                for solution in sol:
                    s0 = solution[0]
                    t0 = solution[1]

                    x_0 = S(B*t0 + r*s0 + r*t0 - B*s0)/(4*A*r)
                    y_0 = S(s0 - t0)/(2*r)

                    if isinstance(s0, Symbol) or isinstance(t0, Symbol):
                        if check_param(x_0, y_0, 4*A*r, t) != (None, None):
                            l.add((check_param(x_0, y_0, 4*A*r, t)[0], check_param(x_0, y_0, 4*A*r, t)[1]))

                    elif divisible(B*t0 + r*s0 + r*t0 - B*s0, 4*A*r):
                        if divisible(s0 - t0, 2*r):
                            if is_solution_quad(var, coeff, x_0, y_0):
                                l.add((x_0, y_0))
            else:
                var[0], var[1] = var[1], var[0] # Interchange x and y
                s = diop_quadratic(var, coeff, t)

                while len(s) > 0:
                    sol = s.pop()
                    l.add((sol[1], sol[0]))

        else:
            # In this case equation can be transformed into a Pell equation
            A, B = _transformation_to_pell(var, coeff)
            D, N = _find_DN(var, coeff)
            solns_pell = diop_pell(D, N)

            n = symbols("n", integer=True)

            a = diop_pell(D, 1)
            T = a[0][0]
            U = a[0][1]

            if (isinstance(A[0], Integer) and isinstance(A[1], Integer) and isinstance(A[2], Integer)
                and isinstance(A[3], Integer) and isinstance(B[0], Integer) and isinstance(B[1], Integer)):
                for sol in solns_pell:

                    r = sol[0]
                    s = sol[1]
                    x_n = S((r + s*sqrt(D))*(T + U*sqrt(D))**n + (r - s*sqrt(D))*(T - U*sqrt(D))**n)/2
                    y_n = S((r + s*sqrt(D))*(T + U*sqrt(D))**n - (r - s*sqrt(D))*(T - U*sqrt(D))**n)/(2*sqrt(D))

                    x_n, y_n = (A*Matrix([x_n, y_n]) + B)[0], (A*Matrix([x_n, y_n]) + B)[1]

                    l.add((x_n, y_n))

            else:
                L = ilcm(S(A[0]).q, ilcm(S(A[1]).q, ilcm(S(A[2]).q, ilcm(S(A[3]).q, ilcm(S(B[0]).q, S(B[1]).q)))))

                k = 0
                done = False
                T_k = T
                U_k = U

                while not done:
                    k = k + 1
                    if (T_k - 1) % L == 0 and U_k % L == 0:
                        done = True
                    T_k, U_k = T_k*T + D*U_k*U, T_k*U + U_k*T

                for soln in solns_pell:
                    x_0 = soln[0]
                    y_0 = soln[1]

                    x_i = x_0
                    y_i = y_0

                    for i in range(k):

                        X = (A*Matrix([x_i, y_i]) + B)[0]
                        Y = (A*Matrix([x_i, y_i]) + B)[1]

                        if isinstance(X, Integer) and isinstance(Y, Integer):
                            if is_solution_quad(var, coeff, X, Y):
                                x_n = S( (x_i + sqrt(D)*y_i)*(T + sqrt(D)*U)**(n*L) + (x_i - sqrt(D)*y_i)*(T - sqrt(D)*U)**(n*L) )/ 2
                                y_n = S( (x_i + sqrt(D)*y_i)*(T + sqrt(D)*U)**(n*L) - (x_i - sqrt(D)*y_i)*(T - sqrt(D)*U)**(n*L) )/ (2*sqrt(D))

                                x_n, y_n = (A*Matrix([x_n, y_n]) + B)[0], (A*Matrix([x_n, y_n]) + B)[1]
                                l.add((x_n, y_n))

                        x_i = x_i*T + D*U*y_i
                        y_i = x_i*U + y_i*T

    return l
示例#10
0
def divisible(a, b):
    return igcd(int(a), int(b)) == abs(int(b))
示例#11
0
def test_multiple_parameters():
    assert_equal("\\gcd(830,450)", gcd(830, 450))
    assert_equal("\\gcd(6,321,429)", igcd(6, 321, 429))
    assert_equal("\\gcd(14,2324)", gcd(14, 2324))
    assert_equal("\\gcd(3, 6, 2)", igcd(3, 6, 2))
    assert_equal("\\gcd(144, 2988, 37116)", igcd(144, 2988, 37116))
    assert_equal("\\gcd(144,2988, 37116,18, 72)", igcd(144, 2988, 37116, 18, 72))
    assert_equal("\\gcd(144, 2988, 37116, 18, 72, 12, 6)", igcd(144, 2988, 37116, 18, 72, 12, 6))
    assert_equal("\\gcd(32)", gcd(32, 32))
    assert_equal("\\gcd(-8, 4,-2)", gcd(-8, gcd(4, -2)))
    assert_equal("\\gcd(x, y,z)", gcd(x, gcd(y, z)), symbolically=True)
    assert_equal("\\gcd(6*4,48, 3)", igcd(6 * 4, 48, 3))
    assert_equal("\\gcd(6*4,48,3)", igcd(6 * 4, 48, 3))
    assert_equal("\\gcd(2.4,3.6, 0.6)", gcd(Rational('2.4'), gcd(Rational('3.6'), Rational('0.6'))))
    assert_equal("\\gcd(2.4,3.6,0.6)", gcd(Rational('2.4'), gcd(Rational('3.6'), Rational('0.6'))))
    assert_equal("\\gcd(\\sqrt{3},\\sqrt{2}, \\sqrt{100})", gcd(sqrt(3), gcd(sqrt(2), sqrt(100))))
    assert_equal("\\gcd(1E12, 1E6,1E3, 10)", igcd(Rational('1E12'), Rational('1E6'), Rational('1E3'), 10))

    assert_equal("\\operatorname{gcd}(830,450)", gcd(830, 450))
    assert_equal("\\operatorname{gcd}(6,321,429)", igcd(6, 321, 429))
    assert_equal("\\operatorname{gcd}(14,2324)", gcd(14, 2324))
    assert_equal("\\operatorname{gcd}(3, 6, 2)", igcd(3, 6, 2))
    assert_equal("\\operatorname{gcd}(144, 2988, 37116)", igcd(144, 2988, 37116))
    assert_equal("\\operatorname{gcd}(144,2988, 37116,18, 72)", igcd(144, 2988, 37116, 18, 72))
    assert_equal("\\operatorname{gcd}(144, 2988, 37116, 18, 72, 12, 6)", igcd(144, 2988, 37116, 18, 72, 12, 6))
    assert_equal("\\operatorname{gcd}(32)", gcd(32, 32))
    assert_equal("\\operatorname{gcd}(-8, 4,-2)", gcd(-8, gcd(4, -2)))
    assert_equal("\\operatorname{gcd}(x, y,z)", gcd(x, gcd(y, z)), symbolically=True)
    assert_equal("\\operatorname{gcd}(6*4,48, 3)", igcd(6 * 4, 48, 3))
    assert_equal("\\operatorname{gcd}(6*4,48,3)", igcd(6 * 4, 48, 3))
    assert_equal("\\operatorname{gcd}(2.4,3.6, 0.6)", gcd(Rational('2.4'), gcd(Rational('3.6'), Rational('0.6'))))
    assert_equal("\\operatorname{gcd}(2.4,3.6,0.6)", gcd(Rational('2.4'), gcd(Rational('3.6'), Rational('0.6'))))
    assert_equal("\\operatorname{gcd}(\\sqrt{3},\\sqrt{2}, \\sqrt{100})", gcd(sqrt(3), gcd(sqrt(2), sqrt(100))))
    assert_equal("\\operatorname{gcd}(1E12, 1E6,1E3, 10)", igcd(Rational('1E12'), Rational('1E6'), Rational('1E3'), 10))
示例#12
0
        r.recvuntil('[')
        tmp = r.recv().strip()[:-1]
        data = tmp.split(b',')
        nums = []
        for n in data:
            nums.append(int(n.decode()))
        return nums
    except:
        r.interactive()


ri = list()
for i in range(10):
    print(f"[*] Collecting Sample No. [ {i} ] ", end='')
    nums = con()
    ri.append(nums)

print(f"\n[*] Collected Samples:")
with open('nums.txt', 'w') as f:
    for i in ri:
        f.write(str(i) + '\n')
        print(i)

# Attack
print(f"\n[*] Attacking Rands...")
res = ''
for s in range(0, 40):
    res += chr(
        sympy.igcd(ri[0][s], ri[1][s], ri[2][s], ri[3][s], ri[4][s], ri[5][s],
                   ri[6][s], ri[7][s], ri[8][s], ri[9][s]))
print(res)
示例#13
0
 def distractor4(num1, num2, lcm):
   return lcm - num2 + igcd(num1,num2) - 1
示例#14
0
def test_C9():
    assert igcd(igcd(1776, 1554), 5698) == 74
示例#15
0
def divisible(a, b):
    return igcd(int(a), int(b)) == abs(int(b))
示例#16
0
import sympy

a=[476768754,8894820767,1523078577,109097050,3620008784,6228614466,10692033330,4148253936,4914362350,6293619360,1334617473,11093384064,2513960712,653093607,7464384964,3588115659,3355836550,3036048976,297606012,9469395320,7798364275,3515888776,544921655,7380682430,6084885520,5510582388,3432253632,10627364048,1053475598,1823602456,8469988435,7592344452,2736211128,841483321,1446276272,8659798924,654157200,1674510552,2588781195,9025288750,497373740]
b=[10801634553,312118317,3505738789,3106347915,196558992,6161230023,3041856180,4087500048,7750373505,9346514415,7211137743,7546208992,9817475400,2278209168,6421271205,1827480348,5735099480,8949246880,2804449812,2702296105,2745281880,10088703860,3288786765,5194099540,2096730496,5269541130,4079409504,4933684448,4180855959,2027760800,1436361335,9574338708,4753809462,3241713482,5320140224,10723200660,1642755648,1672872201,1620509781,7268232500,5933940]
c=[4399778040,1728538665,1294697218,7244816805,10027798592,7823028780,10524151550,3819014064,5322621715,5778952200,9221521426,7733908784,7152939360,79376859,5257855999,4979746080,1867935740,9176703044,2245582785,8455418470,3020573640,9540954528,7031244880,686674060,370000512,1812917946,2133314928,10767538320,3159269120,4250484628,1299844720,5462493300,6691423800,2636365865,877170896,11098884408,2089303968,1188529683,661470051,7650275500,857306040]
d=[9436075590,3224653670,8989335417,10667861610,4725966112,9245615292,7973416660,3902582688,3221546710,1943548320,10862779225,3131388512,7498414080,1558254,6881318458,259591479,7052660010,1171661132,3528831882,5933911680,357351145,1997567444,6094870175,7654611035,1936886672,10199076462,4168618272,5905515616,3259219222,1393851316,6481123155,7963572771,626380638,2058151410,7624241744,1259555724,2560336320,3079896369,2731117752,10660222625,586755320]
e=[3233836149,7521234707,8002695358,7154442330,1994729632,11075807568,486724260,3413373648,7007697515,10476735255,7854591230,4872103600,4059734148,4874116767,9724387149,1099786542,7115840050,2977999560,948170682,11098843890,6966663500,5780695164,1322709820,5543134005,2741090464,9103109874,3985354464,5630331616,2600352384,3036923188,7955690450,3711085092,4001947770,4442690448,10973258800,3599985644,2839644144,2892432708,1581164739,5358236750,214824920]
f=[9326997000,2584664054,7652859814,1930056155,1951560800,2393889837,10660677940,3122216928,1221472000,6769109865,7588446366,2274609904,8723853468,4764456873,699926823,3544971444,6398564920,5999828444,2348682345,1351068760,2613616725,4022171472,1108260515,993481025,9818255440,1384620858,2234769072,4322430224,126933618,2324809864,1910720845,1072892007,2808768594,1689974671,10946799136,9087638244,1220221776,2881757043,616951500,4250616375,262249450]
g=[11032546077,9065112980,4989499386,8844557770,2680365072,7795391049,897614300,2549136528,7596055220,5440307670,5910204758,3885154672,2221687764,4820223180,4927907658,4612701069,8939178160,9019322480,4626410073,4279028905,2324600505,4420715804,7066655455,5601596815,197775872,8749893186,3537064032,672225680,2161410629,361127780,9333202135,5685650289,3221576274,1374707103,4711323680,11470237760,501212640,85950381,260870313,7224143125,468158820]
h=[3839355135,8545614196,3156555667,9981846015,2683268784,2908566855,3758551830,843171648,2645686635,6131532855,4757846839,4625963888,3745240524,5084203056,8121749249,741037344,10128238120,10533569080,2393219115,94993910,1885832840,829986380,6226726000,6006587515,838370624,11199692082,868741440,8502005792,805732872,1930298292,7563250105,7006590063,8342939292,3011621781,7346173744,9927033616,920833392,1894521090,899293857,9985321000,791454970]
i=[2378193759,1325390108,1598764573,9890302105,3091263616,2115599508,9920811560,4528350960,9386305045,2336235720,612143128,3218317424,2195923716,2811881634,10574926466,4967097672,1292732650,6772579496,3662656239,5409694990,1954029730,10894587760,5541380945,6485641070,9372790560,11142849288,4057757184,8709078896,194233697,1887563028,5032822600,59065875,814684470,4155745223,617628144,11039455868,404004384,2013064812,412095849,7881341875,659734130]
j=[4437529029,6475577304,3005126057,9181648990,8832012112,3086500254,3778140960,4518768240,4434651015,1000398735,9396320911,10729608288,7521429744,2331302412,5670208013,2783254467,1772187780,7486320652,2487319725,10705236955,8021940315,2771728360,6820979955,6468245145,407477168,9978724152,2336782992,2909575088,54018237,3542525220,2948960740,4465924551,4828034520,2305625224,7036745968,1305309372,3018753600,169701114,2076274596,4578676375,213700210]

res = ""
for s in range(0,41):
	res +=	chr ( sympy.igcd(a[s],b[s],c[s],d[s],e[s],f[s],g[s],h[s],i[s],j[s])) 
print (res)
    pn.append(p)
    delta *= pow(p, 3)
    alpha *= pow(p, 2) * (p - 1)
    beta *= p * (p - 1) * temp_q

assert pow(beta, 2) % alpha == 0, 'alpha does not divide beta^2'
assert beta % alpha != 0, 'alpha divides beta'

print('beta % alpha', beta % alpha)

print("pn: {0}\npm: {1}\n".format(pn, pm))

totient_delta = totient(delta)
y = random.randrange(delta)

while sympy.igcd(y, delta) != 1:
    y = random.randrange(delta)

print("delta: {0}\n beta: {1}\n alpha: {2}\n y: {3}".format(
    delta, beta, alpha, y))

# Alice work (recebe delta, beta e alpha de trent)
xa = random.randrange(1, delta)
xb = random.randrange(1, totient_delta)
while xb * beta % totient_delta == 0:
    xb = random.randrange(totient_delta)
gamma = alpha * pow(xa, 2) + beta * xb

# Bob work (recebe gamma e delta de alice)
xa1 = random.randrange(1, delta)
xb1 = random.randrange(1, totient_delta)
示例#18
0
import numpy as np
import sympy as sp
import math

# Heronian Triangles: Triangles with integer side length and integer area
# Triplets a, b, c can be generated by some pythagorean triplets 
# See Wolfram: http://mathworld.wolfram.com/HeronianTriangle.html
# See Wikipedia: https://en.wikipedia.org/wiki/Integer_triangle#Isosceles_Heronian_triangles

# Trial Code to generate some Isosceles Heronian Triangles (isosceles so that we have 2 equal side lengths)
# BUT still not efficient enough as the problem only wants 3rd length to be within difference of isosceles length at most 1 
triplets = []
sum = 0
for u in range(1, 10001):
    for v in range(1, 10001):
        if sp.igcd(u,v)==1 and u>v and (u+v)%2 != 0:
            if np.sum([2*(u**2-v**2), u**2 + v**2, u**2 +v**2]) <= 10**9 and abs(2*(u**2-v**2)-u**2-v**2) <= 1:
                triplets.append((2*(u**2-v**2), u**2 + v**2, u**2 +v**2))
                sum += np.sum([2*(u**2-v**2), u**2 + v**2, u**2 +v**2])
                print((2*(u**2-v**2), u**2 + v**2, u**2 +v**2))
            if np.sum([4*u*v, u**2 + v**2, u**2 +v**2]) <= 10**9 and abs(4*u*v-u**2-v**2) <= 1:
                triplets.append((4*u*v, u**2 + v**2, u**2 +v**2))
                sum += np.sum([4*u*v, u**2 + v**2, u**2 +v**2])
                print((4*u*v, u**2 + v**2, u**2 +v**2))       

# The Stupid Special Heronian Triangle Triplet Sequences we WANT!!! 💯😜
"""
(6, 5, 5)
(16, 17, 17)
(66, 65, 65)
(240, 241, 241)
示例#19
0
def diop_quadratic(var, coeff, t):
    """
    Solves quadratic diophantine equations, i.e equations of the form
    Ax**2 + Bxy + Cy**2 + Dx + Ey + F = 0. Returns a set containing
    the tuples (x, y) which contains the solutions.

    Usage
    =====

        diop_quadratic(var, coeff) -> var is a list of variables and
        coeff is a dictionary containing coefficients of the symbols.

    Details
    =======

        ``var`` a list which contains two variables x and y.
        ``coeff`` a dict which generally contains six key value pairs.
        The set of keys is {x**2, y**2, x*y, x, y, Integer(1)}.
        ``t`` the parameter to be used in the solution.

    Examples
    ========

    >>> from sympy.abc import x, y, t
    >>> from sympy import Integer
    >>> from sympy.solvers.diophantine import diop_quadratic
    >>> diop_quadratic([x, y], {x**2: 1, y**2: 1, x*y: 0, x: 2, y: 2, Integer(1): 2}, t)
    set([(-1, -1)])

    References
    ==========

    .. [1] http://www.alpertron.com.ar/METHODS.HTM
    """
    x = var[0]
    y = var[1]

    for term in [x**2, y**2, x * y, x, y, Integer(1)]:
        if term not in coeff.keys():
            coeff[term] = Integer(0)

    A = coeff[x**2]
    B = coeff[x * y]
    C = coeff[y**2]
    D = coeff[x]
    E = coeff[y]
    F = coeff[Integer(1)]

    d = igcd(A, igcd(B, igcd(C, igcd(D, igcd(E, F)))))
    A = A // d
    B = B // d
    C = C // d
    D = D // d
    E = E // d
    F = F // d

    # (1) Linear case: A = B = C = 0 -> considered under linear diophantine equations

    # (2) Simple-Hyperbolic case:A = C = 0, B != 0
    # In this case equation can be converted to (Bx + E)(By + D) = DE - BF
    # We consider two cases; DE - BF = 0 and DE - BF != 0
    # More details, http://www.alpertron.com.ar/METHODS.HTM#SHyperb

    l = set([])

    if A == 0 and C == 0 and B != 0:

        if D * E - B * F == 0:
            if divisible(int(E), int(B)):
                l.add((-E / B, t))
            if divisible(int(D), int(B)):
                l.add((t, -D / B))

        else:
            div = divisors(D * E - B * F)
            div = div + [-term for term in div]

            for d in div:
                if divisible(int(d - E), int(B)):
                    x0 = (d - E) // B
                    if divisible(int(D * E - B * F), int(d)):
                        if divisible(int((D * E - B * F) // d - D), int(B)):
                            y0 = ((D * E - B * F) // d - D) // B
                            l.add((x0, y0))

    # (3) Elliptical case: B**2 - 4AC < 0
    # More Details, http://www.alpertron.com.ar/METHODS.HTM#Ellipse
    # In this case x should lie between the roots of
    # (B**2 - 4AC)x**2 + 2(BE - 2CD)x + (E**2 - 4CF) = 0

    elif B**2 - 4 * A * C < 0:

        z = symbols("z", real=True)
        roots = solve((B**2 - 4 * A * C) * z**2 + 2 * (B * E - 2 * C * D) * z +
                      E**2 - 4 * C * F)

        solve_y = lambda x, e: (-(B * x + E) + e * sqrt(
            (B * x + E)**2 - 4 * C * (A * x**2 + D * x + F))) / (2 * C)

        if len(roots) == 1 and isinstance(roots[0], Integer):
            x_vals = [roots[0]]
        elif len(roots) == 2:
            x_vals = [
                i for i in range(ceiling(min(roots)), ceiling(max(roots)))
            ]  # ceiling = floor +/- 1
        else:
            x_vals = []

        for x0 in x_vals:
            if isinstance(
                    sqrt((B * x0 + E)**2 - 4 * C * (A * x0**2 + D * x0 + F)),
                    Integer):
                if isinstance(solve_y(x0, 1), Integer):
                    l.add((Integer(x0), solve_y(x0, 1)))
                if isinstance(solve_y(x0, -1), Integer):
                    l.add((Integer(x0), solve_y(x0, -1)))

    # (4) Parabolic case: B**2 - 4*A*C = 0
    # There are two subcases to be considered in this case.
    # sqrt(c)D - sqrt(a)E = 0 and sqrt(c)D - sqrt(a)E != 0
    # More Details, http://www.alpertron.com.ar/METHODS.HTM#Parabol

    elif B**2 - 4 * A * C == 0:

        g = igcd(A, C)
        g = abs(g) * sign(A)
        a = A // g
        b = B // g
        c = C // g
        e = sign(B / A)

        if e * sqrt(c) * D - sqrt(a) * E == 0:
            z = symbols("z", real=True)
            roots = solve(sqrt(a) * g * z**2 + D * z + sqrt(a) * F)

            for root in roots:
                if isinstance(root, Integer):
                    l.add(
                        (diop_solve(sqrt(a) * x + e * sqrt(c) * y - root)[x],
                         diop_solve(sqrt(a) * x + e * sqrt(c) * y - root)[y]))

        elif isinstance(e * sqrt(c) * D - sqrt(a) * E, Integer):
            solve_x = lambda u: e*sqrt(c)*g*(sqrt(a)*E - e*sqrt(c)*D)*t**2 - (E + 2*e*sqrt(c)*g*u)*t\
                - (e*sqrt(c)*g*u**2 + E*u + e*sqrt(c)*F) // (e*sqrt(c)*D - sqrt(a)*E)

            solve_y = lambda u: sqrt(a)*g*(e*sqrt(c)*D - sqrt(a)*E)*t**2 + (D + 2*sqrt(a)*g*u)*t \
                + (sqrt(a)*g*u**2 + D*u + sqrt(a)*F) // (e*sqrt(c)*D - sqrt(a)*E)

            for z0 in range(0, abs(e * sqrt(c) * D - sqrt(a) * E)):
                if divisible(
                        sqrt(a) * g * z0**2 + D * z0 + sqrt(a) * F,
                        e * sqrt(c) * D - sqrt(a) * E):
                    l.add((solve_x(z0), solve_y(z0)))

    # (5) B**2 - 4*A*C > 0

    elif B**2 - 4 * A * C > 0:
        # Method used when B**2 - 4*A*C is a square, is descibed in p. 6 of the below paper
        # by John P. Robertson.
        # http://www.jpr2718.org/ax2p.pdf

        if isinstance(sqrt(B**2 - 4 * A * C), Integer):
            if A != 0:
                r = sqrt(B**2 - 4 * A * C)
                u, v = symbols("u, v", integer=True)
                eq = simplify(4 * A * r * u * v + 4 * A * D *
                              (B * v + r * u + r * v - B * u) +
                              2 * A * 4 * A * E * (u - v) +
                              4 * A * r * 4 * A * F)

                sol = diop_solve(eq, t)
                sol = list(sol)

                for solution in sol:
                    s0 = solution[0]
                    t0 = solution[1]

                    x_0 = S(B * t0 + r * s0 + r * t0 - B * s0) / (4 * A * r)
                    y_0 = S(s0 - t0) / (2 * r)

                    if isinstance(s0, Symbol) or isinstance(t0, Symbol):
                        if check_param(x_0, y_0, 4 * A * r, t) != (None, None):
                            l.add((check_param(x_0, y_0, 4 * A * r, t)[0],
                                   check_param(x_0, y_0, 4 * A * r, t)[1]))

                    elif divisible(B * t0 + r * s0 + r * t0 - B * s0,
                                   4 * A * r):
                        if divisible(s0 - t0, 2 * r):
                            if is_solution_quad(var, coeff, x_0, y_0):
                                l.add((x_0, y_0))
            else:
                var[0], var[1] = var[1], var[0]  # Interchange x and y
                s = diop_quadratic(var, coeff, t)

                while len(s) > 0:
                    sol = s.pop()
                    l.add((sol[1], sol[0]))

        else:
            # In this case equation can be transformed into a Pell equation
            A, B = _transformation_to_pell(var, coeff)
            D, N = _find_DN(var, coeff)
            solns_pell = diop_pell(D, N)

            n = symbols("n", integer=True)

            a = diop_pell(D, 1)
            T = a[0][0]
            U = a[0][1]

            if (isinstance(A[0], Integer) and isinstance(A[1], Integer)
                    and isinstance(A[2], Integer)
                    and isinstance(A[3], Integer)
                    and isinstance(B[0], Integer)
                    and isinstance(B[1], Integer)):
                for sol in solns_pell:

                    r = sol[0]
                    s = sol[1]
                    x_n = S((r + s * sqrt(D)) * (T + U * sqrt(D))**n +
                            (r - s * sqrt(D)) * (T - U * sqrt(D))**n) / 2
                    y_n = S((r + s * sqrt(D)) * (T + U * sqrt(D))**n -
                            (r - s * sqrt(D)) *
                            (T - U * sqrt(D))**n) / (2 * sqrt(D))

                    x_n, y_n = (A * Matrix([x_n, y_n]) +
                                B)[0], (A * Matrix([x_n, y_n]) + B)[1]

                    l.add((x_n, y_n))

            else:
                L = ilcm(
                    S(A[0]).q,
                    ilcm(
                        S(A[1]).q,
                        ilcm(
                            S(A[2]).q,
                            ilcm(S(A[3]).q, ilcm(S(B[0]).q,
                                                 S(B[1]).q)))))

                k = 0
                done = False
                T_k = T
                U_k = U

                while not done:
                    k = k + 1
                    if (T_k - 1) % L == 0 and U_k % L == 0:
                        done = True
                    T_k, U_k = T_k * T + D * U_k * U, T_k * U + U_k * T

                for soln in solns_pell:
                    x_0 = soln[0]
                    y_0 = soln[1]

                    x_i = x_0
                    y_i = y_0

                    for i in range(k):

                        X = (A * Matrix([x_i, y_i]) + B)[0]
                        Y = (A * Matrix([x_i, y_i]) + B)[1]

                        if isinstance(X, Integer) and isinstance(Y, Integer):
                            if is_solution_quad(var, coeff, X, Y):
                                x_n = S((x_i + sqrt(D) * y_i) *
                                        (T + sqrt(D) * U)**(n * L) +
                                        (x_i - sqrt(D) * y_i) *
                                        (T - sqrt(D) * U)**(n * L)) / 2
                                y_n = S((x_i + sqrt(D) * y_i) *
                                        (T + sqrt(D) * U)**(n * L) -
                                        (x_i - sqrt(D) * y_i) *
                                        (T - sqrt(D) * U)**(n * L)) / (2 *
                                                                       sqrt(D))

                                x_n, y_n = (A * Matrix([x_n, y_n]) +
                                            B)[0], (A * Matrix([x_n, y_n]) +
                                                    B)[1]
                                l.add((x_n, y_n))

                        x_i = x_i * T + D * U * y_i
                        y_i = x_i * U + y_i * T

    return l
示例#20
0
def test_C9():
    assert igcd(igcd(1776, 1554), 5698) == 74
示例#21
0
 def distractor1(num1, num2, lcm):
   return lcm - igcd(num1, num2)
示例#22
0
def _igcd(*args):
    return UnevaluatedExpr(igcd(*args))
示例#23
0
 def answer(num1, num2, gcd):
   return gcd - igcd(num1, num2)
示例#24
0
def _transformation_to_pell(var, coeff):

    x = var[0]
    y = var[1]

    a = coeff[x**2]
    b = coeff[x*y]
    c = coeff[y**2]
    d = coeff[x]
    e = coeff[y]
    f = coeff[Integer(1)]

    g = igcd(a, igcd(b, igcd(c, igcd(d, igcd(e, f)))))
    a = a // g
    b = b // g
    c = c // g
    d = d // g
    e = e // g
    f = f // g

    X, Y = symbols("X, Y", integer=True)

    if b != Integer(0):
        B = (S(2*a)/b).p
        C = (S(2*a)/b).q
        A = (S(a)/B**2).p
        T = (S(a)/B**2).q

        # eq_1 = A*B*X**2 + B*(c*T - A*C**2)*Y**2 + d*T*X + (B*e*T - d*T*C)*Y + f*T*B
        coeff = {X**2: A*B, X*Y: 0, Y**2: B*(c*T - A*C**2), X: d*T, Y: B*e*T - d*T*C, Integer(1): f*T*B}
        A_0, B_0 = _transformation_to_pell([X, Y], coeff)
        return Matrix(2, 2, [S(1)/B, -S(C)/B, 0, 1])*A_0, Matrix(2, 2, [S(1)/B, -S(C)/B, 0, 1])*B_0

    else:
        if d != Integer(0):
            B = (S(2*a)/d).p
            C = (S(2*a)/d).q
            A = (S(a)/B**2).p
            T = (S(a)/B**2).q

            # eq_2 = A*X**2 + c*T*Y**2 + e*T*Y + f*T - A*C**2
            coeff = {X**2: A, X*Y: 0, Y**2: c*T, X: 0, Y: e*T, Integer(1): f*T - A*C**2}
            A_0, B_0 = _transformation_to_pell([X, Y], coeff)
            return Matrix(2, 2, [S(1)/B, 0, 0, 1])*A_0, Matrix(2, 2, [S(1)/B, 0, 0, 1])*B_0 + Matrix([-S(C)/B, 0])

        else:
            if e != Integer(0):
                B = (S(2*c)/e).p
                C = (S(2*c)/e).q
                A = (S(c)/B**2).p
                T = (S(c)/B**2).q

                # eq_3 = a*T*X**2 + A*Y**2 + f*T - A*C**2
                coeff = {X**2: a*T, X*Y: 0, Y**2: A, X: 0, Y: 0, Integer(1): f*T - A*C**2}
                A_0, B_0 = _transformation_to_pell([X, Y], coeff)
                return Matrix(2, 2, [1, 0, 0, S(1)/B])*A_0, Matrix(2, 2, [1, 0, 0, S(1)/B])*B_0 + Matrix([0, -S(C)/B])

            else:
                # TODO: pre-simplification: Not necessary but may simplify
                # the equation.

                return Matrix(2, 2, [S(1)/a, 0, 0, 1]), Matrix([0, 0])