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)
def OpBLS_Decompress_G2(arg): op = json.loads(arg) compressed = [to_int(op['g1_x']), to_int(op['g1_y'])] try: point = decompress_G2(compressed) except ValueError: r = json.dumps([['0', '0'], ['0', '0']]) return bytes(r, 'utf-8') x = point[0] / point[2] y = point[1] / point[2] point = [[str(x.coeffs[0]), str(y.coeffs[0])], [str(x.coeffs[1]), str(y.coeffs[1])]] r = json.dumps(point) return bytes(r, 'utf-8')
def test_decompress_G2_edge_case(z, error_message): if error_message is None: decompress_G2(z) else: with pytest.raises(ValueError, match=error_message): decompress_G2(z)