Exemplo n.º 1
0
 def test_jacobi_prime(module):
     sqrs = set()
     for a in xrange(module):
        sqrs.add((a * a) % module)
     for a in xrange(module):
         if gcd(a, module) == 1:
             real = 1 if a in sqrs else -1
         else:
             real = 0
         test = jacobi(a, module)
         assertEqual(real, test)
Exemplo n.º 2
0
def test_sqrt_pp_rand():
    print("\nTesting random residues by random modules")
    for size, maxpow in [(2, 500), (10, 100), (64, 15), (128, 5), (129, 5),
                         (256, 2)]:
        for i in xrange(10):
            p = generate_prime(size, k=25)
            print("    Testing %s-bit prime with max power %s: %s..." %
                  (size, maxpow, str(p)[:32]))
            for j in xrange(10):
                k = random.randint(1, maxpow)
                x = random.randint(0, p**k - 1)
                a = pow(x, 2, p**k)
                check_valid_sqrt_pp(x, a, p, k)
Exemplo n.º 3
0
def test_jacobi():

    def test_jacobi_prime(module):
        sqrs = set()
        for a in xrange(module):
           sqrs.add((a * a) % module)
        for a in xrange(module):
            if gcd(a, module) == 1:
                real = 1 if a in sqrs else -1
            else:
                real = 0
            test = jacobi(a, module)
            assertEqual(real, test)

    plist = primes(100) + [293, 1993, 2969, 2971, 9973, 11311]
    for module in plist[2:]:
        test_jacobi_prime(module)

    plist = primes(10)[2:]
    lezhs = {}

    for p in plist:
        lezhs[p] = [jacobi(a, p) for a in xrange(p)]

    for pnum in xrange(2, 4):
        for f in combinations_with_replacement(plist, pnum):
            n = reduce(operator.mul, f)
            for a in xrange(n):
                real = reduce(operator.mul, [lezhs[p][a % p] for p in f])
                test = jacobi(a, n)
                if real != test:
                    print("")
                    print("%d | %d %s" % (a, n, repr(f)))
                    print("Lezhandre symbols: %s" %
                          repr([lezhs[p][a % p] for p in f]))
                    for p in f:
                        print(lezhs[p])
                    print("real %s" % repr(real))
                    print("test %s" % repr(test))
                    print()
                assertEqual(real, test)

    assertRaises(ValueError, jacobi, 1, 2)
    assertRaises(ValueError, jacobi, 0, 6)
    assertRaises(ValueError, jacobi, 0, 0)
    assertRaises(Exception, jacobi, "qwe", 1024)
    assertRaises(Exception, jacobi, 123, "qwe")
Exemplo n.º 4
0
def test_sqrt_composite_rand():
    print("\nTesting all residues by random composite modules")
    for size, ntries in [(2, 2), (3, 3), (5, 10), (7, 20), (10, 20)]:
        for i in xrange(ntries):
            n = randint_bits(size)
            f = factorize(n)
            print("    Testing %s-bit number: %s..." % (size, str(n)[:32]))
            for x in xrange(n):
                a = pow(x, 2, n)
                is_sqrt = has_sqrtmod(a, f)
                if is_sqrt:
                    check_valid_sqrt_composite(None, a, f)

                check_jacobi(a, n, is_sqrt)

                assertTrue(has_sqrtmod(a, f))
                check_valid_sqrt_composite(x, a, f)
Exemplo n.º 5
0
def test_crt():
    for module in [2, 3, 5, 7, 1993]:
        for a in xrange(module):
            assertEqual(solve_crt([a], [module]), a)
            assertEqual(solve_crt([a, 0], [module, 1]), a)
    modules = [2, 3, 5, 19, 137]
    for i in xrange(1000):
        rems = []
        a = 7
        for m in modules:
            rems.append(a % m)
            a += 31337
        a = solve_crt(rems, modules)
        for i in xrange(len(modules)):
            assertEqual(rems[i], a % modules[i])
    assertRaises(TypeError, solve_crt, [1, 2, 3], [1, 2])
    assertRaises(ValueError, solve_crt, [], [])
Exemplo n.º 6
0
def test_factorial_mod():
    print("\nTesting factorial mod prime powers")
    for p, max_e in [(2, 8), (3, 4), (5, 3), (7, 3), (11, 2)]:
        print("    prime %s pow up to %s" % (repr(p), repr(max_e)))
        for i in xrange(250):
            n = random.randint(1, 3000)
            e = random.randint(1, max_e)
            my = factorial_mod(n, {p: e})
            real = factorial(n) % (p ** e)
            assertEqual(my, real)

    print("\nTesting factorial mod small composites")
    for i in xrange(150):
        n = random.randint(1, 8000)
        x = random.randint(0, n * 2)
        my = factorial_mod(x, factorize(n))
        real = factorial(x) % n
        assertEqual(my, real)
Exemplo n.º 7
0
def test_nck():
    for n in (2, 5, 7, 100):
        csum = 0
        for x in xrange(n + 1):
            csum += nCk(n, x)
        assertEqual(csum, 2**n)

    row = [1]
    for n in xrange(1, 200):
        row = [0] + row + [0]
        row = [row[i - 1] + row[i] for i in xrange(1, len(row))]
        for i in xrange(len(row)):
            assertEqual(row[i], nCk(n, i))

    assertEqual(nCk(10, -1), 0)
    assertEqual(nCk(10, 11), 0)
    assertEqual(nCk(0, 0), 1)
    assertEqual(nCk(0, 1), 0)
    assertRaises(ValueError, nCk, -1, 0)
Exemplo n.º 8
0
def test_nCk_mod_pp():
    print("\nTesting nCk mod prime powers")
    for p, max_e in [(2, 8), (3, 4), (5, 3), (7, 3), (11, 2), (13, 2)]:
        print("    prime %s pow up to %s" % (repr(p), repr(max_e)))
        for i in xrange(100):
            k = random.randint(1, 10000)
            n = k + random.randint(0, 10000)
            e = random.randint(1, max_e)
            my = nCk_mod_prime_power(n, k, p, e)
            real = nCk(n, k) % (p ** e)
            assertEqual(my, real)
Exemplo n.º 9
0
def test_sqrt_pp_all():
    print("\nTesting all residues by small modules")
    for prime, maxpow in [(2, 11), (3, 7), (5, 5), (7, 4), (11, 3), (13, 3),
                          (97, 2)]:
        for k in xrange(1, maxpow + 1):
            n = prime**k
            print("    Testing %s**%s" % (prime, k))
            for x in xrange(n):
                a = pow(x, 2, n)

                is_sqrt = has_sqrtmod(a, {prime: k})
                is_sqrt2 = has_sqrtmod_prime_power(a, prime, k)
                assertEqual(is_sqrt, is_sqrt2)
                if is_sqrt:
                    check_valid_sqrt_pp(None, a, prime, k)

                check_jacobi(a, n, is_sqrt)

                assertTrue(has_sqrtmod_prime_power(a, prime, k))
                assertTrue(has_sqrtmod(a, {prime: k}))
                check_valid_sqrt_pp(x, a, prime, k)
Exemplo n.º 10
0
def test_genprime():
    for size in (2, 10, 64, 128, 129, 256):
        for ntry in xrange(10):
            p = generate_prime(size, k=25)
            assertEqual(len_in_bits(p), size)

            assertTrue(prime_test_ferma(p, k=25))
            assertTrue(prime_test_solovay_strassen(p, k=25))
            assertTrue(prime_test_miller_rabin(p, k=25))
            assertTrue(prime_test(p, k=25))

    assertRaises(ValueError, generate_prime, 1)
    assertRaises(TypeError, generate_prime, "")
Exemplo n.º 11
0
def test_sqrt_composite_all():
    print("\nTesting all residues by small composite modules")
    for n in [
            10, 30, 50, 99, 100, 655, 1025, 1337, 7**3 * 3, 2**6 * 13,
            2**4 * 3**3 * 5, 3 * 3 * 5 * 7, 1024
    ]:
        f = factorize(n)
        print("    Testing %s = %s" % (n, f))
        for x in xrange(n):
            a = pow(x, 2, n)
            is_sqrt = has_sqrtmod(a, f)
            if is_sqrt:
                check_valid_sqrt_composite(None, a, f)

            check_jacobi(a, n, is_sqrt)

            assertTrue(has_sqrtmod(a, f))
            check_valid_sqrt_composite(x, a, f)