def _assert(d):
     alphas = []
     while len(set(alphas)) < d and all(a != 0 for a in alphas):
         alphas.append(random_prime(100000))
         alphas = list(set(alphas))
     A = matrix([[alpha ** i for alpha in alphas] for i in range(d)])
     v = [random_prime(100000) for _ in range(d)]
     x = PolynomialRing(QQ, names="x").gens()[0]
     chpy = mul(x - alpha for alpha in alphas)
     self.assertEqual(first_elt_of_kern_of_vandermonde(chpy, alphas[0], v),
                      (A ** (-1) * vector(v))[0])
Esempio n. 2
0
def gen_random_curve(bits):
    """generates a random curve with prime of given bit length"""
    p = random_prime(2**bits, proof=True, lbound=2**(bits - 1))
    while True:
        a = random.randint(1, p - 1)
        b = random.randint(1, p - 1)
        if ((4 * (a**3)) + (27 * (b**2))) % p != 0:
            F = GF(p)
            E = EllipticCurve(F, [a, b])
            return (E, F)
Esempio n. 3
0
def gen_params_from_bits(num_bits,k): 
    """
    Description:
    
        Generates a prime r with num_bits bits and a fundamental discriminant D to use as input to the Cocks-Pinch method
    
    Input:
        
        num_bits - number of bits in r
        k - embedding degree
        
    Output:
        
        r - prime such that r % k == 1 and r is num_bits bits long
        k - embedding degree
        D - (negative) fundamental discriminant where D is a square mod r
    
    """
    r = random_prime(2**num_bits, lbound=2**(num_bits-1))
    while not (r % k == 1 and utils.is_suitable_r(r)):
        r = random_prime(2**num_bits, lbound=2**(num_bits-1))
    return gen_params_from_r(r,k)
Esempio n. 4
0
def gen_params_from_bits(num_bits, k):
    """
    Description:
    
        Generates a prime r with num_bits bits and a fundamental discriminant D to use as input to the Cocks-Pinch method
    
    Input:
        
        num_bits - number of bits in r
        k - embedding degree
        
    Output:
        
        r - prime such that r % k == 1 and r is num_bits bits long
        k - embedding degree
        D - (negative) fundamental discriminant where D is a square mod r
    
    """
    r = random_prime(2**num_bits, lbound=2**(num_bits - 1))
    while not (r % k == 1 and utils.is_suitable_r(r)):
        r = random_prime(2**num_bits, lbound=2**(num_bits - 1))
    return gen_params_from_r(r, k)
Esempio n. 5
0
def test_bp(path, cls, testcases, args):
    success = True
    try:
        if args.zimmerman:
            c = cls(path, verbose=args.verbose)
        else:
            prime = random_prime(2**args.secparam - 1)
            c = cls(path, verbose=args.verbose, obliviate=args.obliviate)
            c.randomize(prime)
    except ParseException as e:
        print('%s %s' % (utils.clr_warn('Parse Error:'), e))
        return False
    for k, v in testcases.items():
        if c.evaluate(k) != v:
            print('%s (%s != %d) ' % (failstr, k, v))
            success = False
    return success
Esempio n. 6
0
def test_bp(path, cls, testcases, args):
    success = True
    try:
        if args.zimmerman:
            c = cls(path, verbose=args.verbose)
        else:
            prime = random_prime(2 ** args.secparam - 1)
            c = cls(path, verbose=args.verbose, obliviate=args.obliviate)
            c.randomize(prime)
    except ParseException as e:
        print('%s %s' % (utils.clr_warn('Parse Error:'), e))
        return False
    for k, v in testcases.items():
        if c.evaluate(k) != v:
            print('%s (%s != %d) ' % (failstr, k, v))
            success = False
    return success
def test_ec(num_tests, num_bits, debug=False):
    print('testing EC chain...')
    func_start = time.time()
    fail = 0
    for i in range(0, num_tests):
        try:
            k = randint(5, 20)
            r = 0
            while not (r % k == 1 and is_prime(r)):
                r = random_prime(2**(num_bits - 1), 2**num_bits)
            k_vector = [k]
            k_vector.append(randint(5, 20))
            k_vector.append(randint(5, 20))
            curves = ec.new_chain(r, k_vector)
            assert ec.is_chain(curves)
        except AssertionError as e:
            fail += 1
    if fail == 0:
        print('test passed')
        return True
    else:
        print("failed %.2f" % (100 * fail / num_tests) + "% of tests!")
        return False
Esempio n. 8
0
def test_ec(num_tests,num_bits, debug=False):
    print('testing EC chain...')
    func_start = time.time()
    fail = 0
    for i in range(0, num_tests):
        try:
            k = randint(5,20)
            r = 0
            while not (r % k == 1 and is_prime(r)):
                r = random_prime(2**(num_bits-1), 2**num_bits)
            k_vector = [k]
            k_vector.append(randint(5,20))
            k_vector.append(randint(5,20))
            curves = ec.new_chain(r, k_vector)
            assert ec.is_chain(curves)
        except AssertionError as e:
            fail += 1
    if fail == 0:
        print('test passed')
        return True
    else:
        print("failed %.2f" %(100*fail/num_tests) + "% of tests!")
        return False
Esempio n. 9
0
def gen_key(lb1, up1, lb2, up2):
    p = random_prime(up1, lbound=lb1, proof=False)
    q = random_prime(up2, lbound=lb2, proof=False)
    return 65537, p * q