示例#1
0
def _generate_params_for_general_disc(disc, n, g):
    """
    Generate curve params for discriminant other than -3 or -4.
    """
    jr = equation.root_Fp([c % n for c in hilbert(disc)[-1]], n)
    c = (jr * arith1.inverse(jr - 1728, n)) % n
    r = (-3 * c) % n
    s = (2 * c) % n
    yield (r, s)
    g2 = (g * g) % n
    yield ((r * g2) % n, (s * g2 * g) % n)
示例#2
0
文件: ecpp.py 项目: nickspoon/part-ii
def _generate_params_for_general_disc(disc, n, g):
    """
    Generate curve params for discriminant other than -3 or -4.
    """
    jr = equation.root_Fp([c % n for c in hilbert(disc)[-1]], n)
    c = (jr * arith1.inverse(jr - 1728, n)) % n
    r = (-3 * c) % n
    s = (2 * c) % n
    yield (r, s)
    g2 = (g * g) % n
    yield ((r * g2) % n, (s * g2 * g) % n)
示例#3
0
def generate_curve(p, d):
    '''
    Essentially Algorithm 7.5.9
    Args:
        p:

    Returns:
        parameters a, b for the curve
    '''
    # calculate quadratic nonresidue
    g = gen_QNR(p, d)
    # find discriminant
    new_d = gen_discriminant(0)
    uv = cornacchia_smith(p, new_d)
    while jacobi(new_d, p) != 1 or uv is None:
        new_d = gen_discriminant(new_d)
        uv = cornacchia_smith(p, new_d)
    u, v = uv   # storing the result of cornacchia. u^2 + v^2 * |D| = 4*p

    # check for -d = 3 or 4
    # Choose one possible output
    # Look at param_gen for comparison.
    answer = []

    if new_d == -3:
        x = -1
        for i in range(0, 6):
            answer.append((0, x))
            x = (x * g) % p
        return answer

    if new_d == -4:
        x = -1
        for i in range(0, 4):
            answer.append((x, 0))
            x = (x * g) % p
        return answer

    # otherwise compute the hilbert polynomial
    _, t, _ = hilbert(new_d)
    s = [i % p for i in t]
    j = equation.root_Fp(s, p) # Find a root for s in Fp. Algorithm 2.3.10
    c = j * inverse(j - 1728, p) % p
    r = -3 * c % p
    s = 2 * c % p

    return [(r, s), (r * g * g % p, s * (g**3) % p)]
示例#4
0
def curve_parameters(d, p):
    '''
    Modified Algorithm 7.5.9 for the use of ecpp
    Args:
        d: discriminant
        p: number for prime proving

    Returns:
        a list of (a, b) parameters for
    '''
    g = gen_QNR(p, d)
    #g = nzmath.ecpp.quasi_primitive(p, d==-3)

    u, v = cornacchia_smith(p, d)
    # go without the check for result of cornacchia because it's done by previous methods.
    if jacobi(d, p) != 1:
        raise ValueError('jacobi(d, p) not equal to 1.')

        # check for -d = 3 or 4
    # Choose one possible output
    # Look at param_gen for comparison.
    answer = []

    if d == -3:
        x = -1 % p
        for i in range(0, 6):
            answer.append((0, x))
            x = (x * g) % p
        return answer

    if d == -4:
        x = -1 % p
        for i in range(0, 4):
            answer.append((x, 0))
            x = (x * g) % p
        return answer

    # otherwise compute the hilbert polynomial
    _, t, _ = hilbert(d)
    s = [int(i % p) for i in t]
    j = equation.root_Fp(s, p) # Find a root for s in Fp. Algorithm 2.3.10
    c = j * inverse(j - 1728, p) % p
    r = -3 * c % p
    s = 2 * c % p

    return [(r, s), (r * g * g % p, s * (g**3) % p)]