def test_G1_compress_and_decompress_flags(pt, on_curve, is_infinity): assert on_curve == is_on_curve(pt, b) z = compress_G1(pt) if on_curve: x = z % POW_2_381 c_flag = (z % 2**384) // POW_2_383 b_flag = (z % POW_2_383) // POW_2_382 a_flag = (z % POW_2_382) // POW_2_381 assert x < q assert c_flag == 1 if is_infinity: assert b_flag == 1 assert a_flag == x == 0 else: assert b_flag == 0 pt_x, pt_y = normalize(pt) assert a_flag == (pt_y.n * 2) // q assert x == pt_x.n # Correct flags should decompress correct x, y normalize(decompress_G1(z)) == normalize(pt) else: with pytest.raises(ValueError): decompress_G1(z)
def OpBLS_Compress_G1(arg): op = json.loads(arg) x = to_int(op['g1_x']) y = to_int(op['g1_y']) if (x % MOD, y % MOD) == (0, 0): return g1 = [FQ(x), FQ(y), FQ.one()] compressed = compress_G1(g1) if is_valid([x, y]) == True and is_on_curve(g1, b): decompressed = decompress_G1(compressed) assert g1[0] == decompressed[0] and g1[1] == decompressed[1] r = json.dumps(str(compressed)) return bytes(r, 'utf-8')
def OpBLS_Decompress_G1(arg): op = json.loads(arg) compressed = to_int(op['compressed']) try: point = decompress_G1(compressed) except ValueError: r = json.dumps(['0', '0']) return bytes(r, 'utf-8') point = [str(point[0]), str(point[1])] if point == ['1', '1']: point = ['0', '0'] r = json.dumps(point) return bytes(r, 'utf-8')
def test_decompress_G1_edge_case(z, error_message): if error_message is None: decompress_G1(z) else: with pytest.raises(ValueError, match=error_message): decompress_G1(z)