Exemple #1
0
def test_G2_compress_and_decompress_flags(pt, on_curve, is_infinity):
    if on_curve:
        z1, z2 = compress_G2(pt)
        x1 = z1 % POW_2_381
        c_flag1 = (z1 % 2**384) // POW_2_383
        b_flag1 = (z1 % POW_2_383) // POW_2_382
        a_flag1 = (z1 % POW_2_382) // POW_2_381
        x2 = z2 % POW_2_381
        c_flag2 = (z2 % 2**384) // POW_2_383
        b_flag2 = (z2 % POW_2_383) // POW_2_382
        a_flag2 = (z2 % POW_2_382) // POW_2_381
        assert x1 < q
        assert x2 < q
        assert c_flag2 == b_flag2 == a_flag2 == 0
        assert c_flag1 == 1
        if is_infinity:
            assert b_flag1 == 1
            assert a_flag1 == x1 == x2 == 0
        else:
            assert b_flag1 == 0
            _, y = normalize(pt)
            _, y_im = y.coeffs
            # TODO: need a case for y_im == 0
            assert a_flag1 == (y_im * 2) // q
        # Correct flags should decompress correct x, y
        normalize(decompress_G2((z1, z2))) == normalize(pt)
    else:
        with pytest.raises(ValueError):
            compress_G2(pt)
Exemple #2
0
def hash_message(message: bytes, DST: bytes = None) -> bytes:
    from hashlib import sha256
    from py_ecc.bls.hash import i2osp
    from py_ecc.bls.hash_to_curve import hash_to_G2
    from py_ecc.bls.point_compression import compress_G2

    DST = DST or b''  # TODO: Use a valid DST
    mapped_msg = hash_to_G2(message, DST, sha256)
    z1, z2 = compress_G2(mapped_msg)
    return i2osp(z1, 48) + i2osp(z2, 48)
Exemple #3
0
def OpBLS_Compress_G2(arg):
    op = json.loads(arg)

    v = to_int(op['g2_v'])
    w = to_int(op['g2_w'])
    x = to_int(op['g2_x'])
    y = to_int(op['g2_y'])

    g2 = (FQ2((v, x)), FQ2((w, y)), FQ2.one())

    try:
        compressed = compress_G2(g2)
    except ValueError:
        return

    point = [str(compressed[0]), str(compressed[1])]

    r = json.dumps(point)
    return bytes(r, 'utf-8')
Exemple #4
0
            assert b_flag1 == 1
            assert a_flag1 == x1 == x2 == 0
        else:
            assert b_flag1 == 0
            _, y = normalize(pt)
            _, y_im = y.coeffs
            # TODO: need a case for y_im == 0
            assert a_flag1 == (y_im * 2) // q
        # Correct flags should decompress correct x, y
        normalize(decompress_G2((z1, z2))) == normalize(pt)
    else:
        with pytest.raises(ValueError):
            compress_G2(pt)


compressed_g2 = compress_G2(G2)
compressed_z2 = compress_G2(Z2)


@pytest.mark.parametrize(
    'z, error_message',
    [
        (compressed_g2, None),  # baseline
        ((compressed_g2[0] & ~(1 << 383), compressed_g2[1]),
         "c_flag should be 1"),  # set c_flag1 to 0
        ((compressed_g2[0] | (1 << 382), compressed_g2[1]),
         "b_flag should be 0"),  # set b_flag1 to 1
        ((compressed_z2[0] & ~(1 << 382), compressed_z2[1]),
         "b_flag should be 1"),  # set b_flag1 to 0
        ((compressed_z2[0] | (1 << 381), compressed_z2[1]),
         "a point at infinity should have a_flag == 0"),  # set a_flag1 to 1