Beispiel #1
0
def ord(n, b): # Multiplicative order
    """Return the smallest positive integer e such that b**e=1(mod n)."""
    if gcd(n, b) != 1 or n == 1:
        raise Exception("Multiplicative order is not defined");
    e = 1;
    power = b%n;
    while power != 1:
        power = (power*b)%n;
        e += 1;
    return e;
Beispiel #2
0
def linear(n, a, b):
    """Give general solution to linear congruence ax=b (mod n)"""
    if a == 0:
        raise Exception("Not a linear congruence.");
    g = gcd(a, n);
    if b % g:
        return [];
    a, b, s = a//g, b//g, n//g;
    x = (multiplicative_inverse(s, a)*b) % n;
    solutions = [];
    for i in range(g):
        solutions.append(x);
        x = (x+s) % n;
    solutions.sort();
    return solutions;
Beispiel #3
0
    def generate_keys(cls, digit):
        """Generate public and private keys.
        The length of each number is roughly twice the value of 'digit'"""
        p = generate_large_prime(digit);
        q = generate_large_prime(digit);
        n = p * q;
        period = (p-1) * (q-1);
        while(True):
            e = randint(1, n-1);
            if gcd(period, e) == 1:
                break;
        # e has to be invertible
        d = multiplicative_inverse(period, e);

        return (RSA_public_key(n, e), RSA_private_key(n, d));
def fermat_test(n, trials=DEFAULT_TRIALS, show_witness=False):
    """Using Fermet test, to check (with certain confidence) whether a
    positive integer 'n'(>=3) is prime.
    Carmichael numbers, which are composites satisfying the korselt criterion,
    can fool this algorithm."""
    for i in range(trials):
        while True:
            witness = randint(2, n - 1)
            if gcd(n, witness) == 1:
                break
        if (mod(n, witness, n - 1) != 1):
            if show_witness:
                print("Witness:", witness)
            return 0
    return 1
Beispiel #5
0
def euclid():
    """Compute the gcd and lcm of two numbers"""

    # render the template "euclid"
    if request.method == "GET":
        return render_template("euclid.html")

    if request.method == "POST":

        # if the first number is not provided, return apology
        if not request.form.get("first"):
            return apology("must provide a number")

        # if the second number not provided, return apology
        if not request.form.get("second"):
            return apology("must provide a number")

        # extract the numbers from the request form
        first = int(request.form.get("first"))
        second = int(request.form.get("second"))

        # if numbers less than 0, return apology
        if first <= 0:
            return apology("must provide a positive integer")

        if second <= 0:
            return apology("must provide a positive integer")

        # calculate gcd and lcm
        divisor = gcd(first, second)
        multiplicity = lcm(first, second)

        # render a template that will show the results
        return render_template("gcd_lcm.html",
                               x=first,
                               y=second,
                               gcd=divisor,
                               lcm=multiplicity)