def OpBLS_G2_Add(arg): op = json.loads(arg) a_v = to_int(op['a_v']) a_w = to_int(op['a_w']) a_x = to_int(op['a_x']) a_y = to_int(op['a_y']) b_v = to_int(op['b_v']) b_w = to_int(op['b_w']) b_x = to_int(op['b_x']) b_y = to_int(op['b_y']) A = (FQ2((a_v, a_x)), FQ2((a_w, a_y)), FQ2.one()) B = (FQ2((b_v, b_x)), FQ2((b_w, b_y)), FQ2.one()) if not (is_on_curve(A, b2) and subgroup_check(A)): return if not (is_on_curve(B, b2) and subgroup_check(B)): return result = add(A, B) x = result[0] / result[2] y = result[1] / result[2] result = [[str(x.coeffs[0]), str(y.coeffs[0])], [str(x.coeffs[1]), str(y.coeffs[1])]] r = json.dumps(result) return bytes(r, 'utf-8')
def OpBLS_G1_Add(arg): op = json.loads(arg) a_x = to_int(op['a_x']) a_y = to_int(op['a_y']) b_x = to_int(op['b_x']) b_y = to_int(op['b_y']) if (a_x % MOD, a_y % MOD) == (0, 0): return if (b_x % MOD, b_y % MOD) == (0, 0): return A = [FQ(a_x), FQ(a_y), FQ.one()] B = [FQ(b_x), FQ(b_y), FQ.one()] if not (is_on_curve(A, b) and subgroup_check(A)): return if not (is_on_curve(B, b) and subgroup_check(B)): return result = add(A, B) result = [str(result[0] / result[2]), str(result[1] / result[2])] r = json.dumps(result) return bytes(r, 'utf-8')
def reconstruct_shared_bls_signature( signatures: Dict[int, BLSSignature]) -> BLSSignature: """ Reconstructs shared BLS private key signature. Copied from https://github.com/dankrad/python-ibft/blob/master/bls_threshold.py """ r = Z2 for i, sig in signatures.items(): sig_point = signature_to_G2(sig) coef = 1 for j in signatures: if j != i: coef = -coef * (j + 1) * prime_field_inv(i - j, PRIME) % PRIME r = add(r, multiply(sig_point, coef)) return G2_to_signature(r)
def OpBLS_MapToG2(arg): op = json.loads(arg) u_x = to_int(op['u_x']) u_y = to_int(op['u_y']) v_x = to_int(op['v_x']) v_y = to_int(op['v_y']) u = FQ2((u_x, u_y)) v = FQ2((v_x, v_y)) g2_u = map_to_curve_G2(u) g2_v = map_to_curve_G2(v) r = add(g2_u, g2_v) point = clear_cofactor_G2(r) 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')