示例#1
0
def pollard_brent_search(f, n: int, x0=2, size=100):
    """Pollard's-rho algorithm to find a non-trivial
    factor of `n` using Brent's cycle detection algorithm,
    and including the product speed improvment."""
    power = 1
    lam = 0
    tortoise = x0
    hare = x0
    d = 1

    while d == 1:
        prod = 1
        last_pos = hare
        for c in range(size):
            if power == lam:
                tortoise = hare
                power *= 2
                lam = 0
            hare = f(hare)
            lam += 1
            prod = (prod * abs(tortoise - hare)) % n
        d = gcd(prod, n)

    if not (is_prime(d)):
        return pollard_rho_search_brent(f, n, last_pos)
    else:
        return d
示例#2
0
def pollard_rho_search_floyd(f, n: int, x0=2):
    """Pollard's-rho algorithm to find a non-trivial
    factor of `n` using Floyd's cycle detection algorithm."""
    d = 1
    tortoise = x0
    hare = x0

    while d == 1:
        tortoise = f(tortoise)
        hare = f(f(hare))
        d = gcd(abs(tortoise - hare), n)

    return d
示例#3
0
def frobenius_trace_mod_2(curve):
    """Computes the trace of the Frobenius endomorphism mod 2"""
    R = Polynomials(curve.field)

    x = R.x()
    A, B = curve.parameters()

    curve_polynomial = x ** 3 + A * x + B
    field_polynomial = R([0] * curve.field.size() + [1]) - x

    if gcd(curve_polynomial, field_polynomial).degree() == 0:
        return FiniteField(2)(1)
    else:
        return FiniteField(2)(0)
示例#4
0
def ECMFactor(n, sieve, limit, times):
    """Finds a non-trivial factor of `n` using
    the elliptic-curve factorization method"""
    for _ in range(times):
        g = n
        while g == n:
            curve, point = randomCurve(n)
            g = gcd(n, curve.discriminant().remainder())

        if g > 1:
            return g

        for prime in sieve:
            prod = prime
            i = 0
            while prod < limit and i < 10:
                i += 1
                try:
                    point = prime * point
                except ZeroDivisionError as e:
                    return gcd(e.args[1], n)
                prod *= prime

    return n
示例#5
0
def pollard_rho_search_brent(f, n: int, x0=2):
    """Pollard's-rho algorithm to find a non-trivial
    factor of `n` using Brent's cycle detection algorithm."""
    power = 1
    lam = 0
    tortoise = x0
    hare = x0
    d = 1
    while d == 1:
        if power == lam:
            tortoise = hare
            power *= 2
            lam = 0
        hare = f(hare)
        d = gcd(abs(tortoise - hare), n)
        lam += 1

    return d