Exemplo n.º 1
0
def test_ec():
    q = default_ec.q
    g = G1Generator()

    assert g.is_on_curve()
    assert 2 * g == g + g
    assert (3 * g).is_on_curve()
    assert 3 * g == g + g + g

    g2 = G2Generator()
    assert g2.x * (Fq(q, 2) * g2.y) == Fq(q, 2) * (g2.x * g2.y)
    assert g2.is_on_curve()
    s = g2 + g2
    assert untwist(twist(s.to_affine())) == s.to_affine()
    assert untwist(5 * twist(s.to_affine())) == (5 * s).to_affine()
    assert 5 * twist(s.to_affine()) == twist((5 * s).to_affine())
    assert s.is_on_curve()
    assert g2.is_on_curve()
    assert g2 + g2 == 2 * g2
    assert g2 * 5 == (g2 * 2) + (2 * g2) + g2
    y = y_for_x(g2.x, default_ec_twist, Fq2)
    assert y == g2.y or -y == g2.y

    g_j = G1Generator()
    g2_j = G2Generator()
    g2_j2 = G2Generator() * 2
    assert g.to_affine().to_jacobian() == g
    assert (g_j * 2).to_affine() == g.to_affine() * 2
    assert (g2_j + g2_j2).to_affine() == g2.to_affine() * 3
Exemplo n.º 2
0
def test_ec():
    g = generator_Fq(default_ec)

    assert g.is_on_curve()
    assert 2 * g == g + g
    assert (3 * g).is_on_curve()
    assert 3 * g == g + g + g
    P = hash_to_point_Fq(bytes([]))
    assert P.is_on_curve()
    assert P.serialize() == bytes.fromhex(
        "12fc5ad5a2fbe9d4b6eb0bc16d530e5f263b6d59cbaf26c3f2831962924aa588ab84d46cc80d3a433ce064adb307f256"
    )

    g2 = generator_Fq2(default_ec_twist)
    assert g2.x * (2 * g2.y) == 2 * (g2.x * g2.y)
    assert g2.is_on_curve()
    s = g2 + g2
    assert untwist(twist(s)) == s
    assert untwist(5 * twist(s)) == 5 * s
    assert 5 * twist(s) == twist(5 * s)
    assert s.is_on_curve()
    assert g2.is_on_curve()
    assert g2 + g2 == 2 * g2
    assert g2 * 5 == (g2 * 2) + (2 * g2) + g2
    y = y_for_x(g2.x, default_ec_twist, Fq2)
    assert y[0] == g2.y or y[1] == g2.y
    assert hash_to_point_Fq2("chia") == hash_to_point_Fq2("chia")

    g_j = generator_Fq(default_ec_twist).to_jacobian()
    g2_j = generator_Fq2(default_ec_twist).to_jacobian()
    g2_j2 = (generator_Fq2(default_ec_twist) * 2).to_jacobian()
    assert g.to_jacobian().to_affine() == g
    assert (g_j * 2).to_affine() == g * 2
    assert (g2_j + g2_j2).to_affine() == g2 * 3

    assert sw_encode(Fq(default_ec.q, 0)).infinity
    assert sw_encode(Fq(default_ec.q, 1)) == sw_encode(Fq(default_ec.q, -1)).negate()
    assert (
        sw_encode(
            Fq(
                default_ec.q,
                0x019CFABA0C258165D092F6BCA9A081871E62A126C499340DC71C0E9527F923F3B299592A7A9503066CC5362484D96DD7,
            )
        )
        == generator_Fq()
    )
    assert (
        sw_encode(
            Fq(
                default_ec.q,
                0x186417302D5A65347A88B0F999AB2B504614AA5E2EEBDEB1A014C40BCEB7D2306C12A6D436BEFCF94D39C9DB7B263CD4,
            )
        )
        == generator_Fq().negate()
    )
Exemplo n.º 3
0
def test_ec():
    g = generator_Fq(default_ec)

    assert (g.is_on_curve())
    assert (2 * g == g + g)
    assert ((3 * g).is_on_curve())
    assert (3 * g == g + g + g)
    P = hash_to_point_Fq(bytes([]))
    assert (P.is_on_curve())
    assert (P.serialize() == bytes.fromhex(
        "12fc5ad5a2fbe9d4b6eb0bc16d530e5f263b6d59cbaf26c3f2831962924aa588ab84d46cc80d3a433ce064adb307f256"
    ))

    g2 = generator_Fq2(default_ec_twist)
    assert (g2.x * (2 * g2.y) == 2 * (g2.x * g2.y))
    assert (g2.is_on_curve())
    s = g2 + g2
    assert (untwist(twist(s)) == s)
    assert (untwist(5 * twist(s)) == 5 * s)
    assert (5 * twist(s) == twist(5 * s))
    assert (s.is_on_curve())
    assert (g2.is_on_curve())
    assert (g2 + g2 == 2 * g2)
    assert (g2 * 5 == (g2 * 2) + (2 * g2) + g2)
    y = y_for_x(g2.x, default_ec_twist, Fq2)
    assert (y[0] == g2.y or y[1] == g2.y)
    assert (hash_to_point_Fq2("chia") == hash_to_point_Fq2("chia"))

    g_j = generator_Fq(default_ec_twist).to_jacobian()
    g2_j = generator_Fq2(default_ec_twist).to_jacobian()
    g2_j2 = (generator_Fq2(default_ec_twist) * 2).to_jacobian()
    assert (g.to_jacobian().to_affine() == g)
    assert ((g_j * 2).to_affine() == g * 2)
    assert ((g2_j + g2_j2).to_affine() == g2 * 3)

    assert (sw_encode(Fq(default_ec.q, 0)).infinity)
    assert (sw_encode(Fq(default_ec.q, 1)) == sw_encode(Fq(default_ec.q,
                                                           -1)).negate())
    assert (sw_encode(
        Fq(
            default_ec.q,
            0x019cfaba0c258165d092f6bca9a081871e62a126c499340dc71c0e9527f923f3b299592a7a9503066cc5362484d96dd7
        )) == generator_Fq())
    assert (sw_encode(
        Fq(
            default_ec.q,
            0x186417302d5a65347a88b0f999ab2b504614aa5e2eebdeb1a014c40bceb7d2306c12a6d436befcf94d39c9db7b263cd4
        )) == generator_Fq().negate())
Exemplo n.º 4
0
def add_line_eval(R, Q, P, ec=default_ec):
    """
    Creates an equation for a line between R and Q,
    and evaluates this at the point P. f(x) = y - sv - v.
    f(P).
    """
    R12 = untwist(R)
    Q12 = untwist(Q)

    # This is the case of a vertical line, where the denominator
    # will be 0.
    if R12 == Q12.negate():
        return P.x - R12.x

    slope = (Q12.y - R12.y) / (Q12.x - R12.x)
    v = (Q12.y * R12.x - R12.y * Q12.x) / (R12.x - Q12.x)

    return P.y - P.x * slope - v
Exemplo n.º 5
0
def double_line_eval(R, P, ec=default_ec):
    """
    Creates an equation for a line tangent to R,
    and evaluates this at the point P. f(x) = y - sv - v.
    f(P).
    """
    R12 = untwist(R)

    slope = (3 * pow(R12.x, 2) + ec.a) / (2 * R12.y)
    v = R12.y - slope * R12.x

    return P.y - P.x * slope - v
Exemplo n.º 6
0
def double_line_eval(R: AffinePoint, P: AffinePoint, ec=default_ec):
    """
    Creates an equation for a line tangent to R,
    and evaluates this at the point P. f(x) = y - sv - v.
    f(P).
    """
    R12 = untwist(R)

    slope = (Fq(ec.q, 3) * (R12.x**2) + ec.a) / (Fq(ec.q, 2) * R12.y)
    v = R12.y - slope * R12.x

    return P.y - P.x * slope - v