Exemple #1
0
def map_to_g1(raw_hash: FQ) -> G1Point:
    one = FQ.one()
    x = raw_hash
    while True:
        y = x * x * x + b
        y = sqrt(y)
        if y is not None:
            break
        x += one
    h = (x, y, FQ.one())
    assert is_on_curve(h, b)
    return h
Exemple #2
0
def test_hash_to_point(BLSG2):
    msg = b"\x00\x00\x00\x01"
    _h = hash_to_g1(msg)
    h = BLSG2.hashToPoint(msg)
    assert FQ(h[0]) == _h[0]
    assert FQ(h[1]) == _h[1]

    import os

    msg = os.urandom(256)
    _h = hash_to_g1(msg)
    h = BLSG2.hashToPoint(msg)
    assert FQ(h[0]) == _h[0]
    assert FQ(h[1]) == _h[1]
Exemple #3
0
def test_sqrt(BLSG2):
    def rand_fq() -> FQ:
        from random import randint

        return FQ(randint(1, field_modulus - 1))

    aa = FQ(-1)
    _, ok = BLSG2.sqrt(aa)
    assert ok is False

    a = rand_fq()
    aa = a * a
    a, ok = BLSG2.sqrt(aa)
    assert ok is True
    _a = FQ(a)
    assert _a * _a == aa
Exemple #4
0
def decompress_G1(p):
    if p == 0:
        return (FQ(1), FQ(1), FQ(0))
    x = p % 2**255
    y_mod_2 = p // 2**255
    y = pow((x**3 + b.n) % field_modulus, (field_modulus+1)//4, field_modulus)
    assert pow(y, 2, field_modulus) == (x**3 + b.n) % field_modulus
    if y%2 != y_mod_2:
        y = field_modulus - y
    return (FQ(x), FQ(y), FQ(1))
Exemple #5
0
 def Fq(cls, n: IntOrFE) -> "FieldElement":
     return FQ(n)
Exemple #6
0
def hash_ORBLS(msg: bytes) -> FQ:
    _msg = _hash(msg, b"")
    return FQ(int.from_bytes(_msg, "big"))
Exemple #7
0
def aggregate_pubs(pubs):
    o = FQ(1), FQ(1), FQ(0)
    for p in pubs:
        o = add(o, decompress_G1(p))
    return compress_G1(o)
Exemple #8
0
def test_ec_pair_field_exceed_mod(f1):
    FQ.fielf_modulus = 100
    a = FQ(val=1)
    f1.return_value = (a, a)
    vec_c = [10] * 192
    assert ec_pair(vec_c) == []
Exemple #9
0
def test_ec_pair(f1, f2, f3, f4):
    FQ.fielf_modulus = 100
    a = FQ(val=1)
    f1.return_value = (a, a)
    vec_c = [0] * 192
    assert ec_pair(vec_c) == [0] * 31 + [1]
Exemple #10
0
def priv_to_pub(priv: PrivateKey) -> Pubkey:
    x, y = normalize(multiply(G1, priv))
    g1 = (x, y, FQ.one())
    return G1_to_pubkey(g1)
Exemple #11
0
def signature_to_g1(sig: Signature) -> G1Point:
    a1 = big_endian_to_int(sig[:32])
    a2 = big_endian_to_int(sig[32:])
    g1 = (FQ(a1), FQ(a2), FQ(1))
    assert is_valid_g1_point(g1)
    return g1
Exemple #12
0
def pubkey_to_G1(pubkey: Pubkey) -> G1Point:
    a1 = big_endian_to_int(pubkey[:32])
    a2 = big_endian_to_int(pubkey[32:])
    g1 = (FQ(a1), FQ(a2), FQ(1))
    assert is_g1_on_curve(g1)
    return g1
Exemple #13
0
def test_ec_add(f1, f2, f3):
    FQ.fielf_modulus = 128
    a = FQ(val=1)
    f1.return_value = (a, a)
    assert ec_add(VECTOR_A) == ([0] * 31 + [1]) * 2
Exemple #14
0
    def rand_fq() -> FQ:
        from random import randint

        return FQ(randint(1, field_modulus - 1))
Exemple #15
0
def sign(msg: Message, priv: PrivateKey) -> Signature:
    x, y = normalize(multiply(hash_to_g1(msg), priv))
    g1 = (x, y, FQ.one())
    return g1_to_signature(g1)