def OpBignumCalc_InvMod(arg): op = json.loads(arg) a = to_int(op['a']) mod = to_int(op['mod']) res = prime_field_inv(a, mod) r = json.dumps(str(res)) return bytes(r, 'utf-8')
def get_aggregate_key(keys): r = b.Z1 for i, key in keys.items(): key_point = pubkey_to_G1(key) coef = 1 for j in keys: if j != i: coef = -coef * (j + 1) * prime_field_inv( i - j, b.curve_order) % b.curve_order r = b.add(r, b.multiply(key_point, coef)) return G1_to_pubkey(r)
def reconstruct(signatures): r = b.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, b.curve_order) % b.curve_order r = b.add(r, b.multiply(sig_point, coef)) return G2_to_signature(r)
def __div__(self: T_FQ, other: IntOrFQ) -> T_FQ: if isinstance(other, FQ): on = other.n elif isinstance(other, int): on = other else: raise TypeError( "Expected an int or FQ object, but got object of type {}". format(type(other))) return type(self)(self.n * prime_field_inv(on, self.field_modulus) % self.field_modulus)
def optimized_poly_rounded_div(self, a: Sequence[IntOrFQ], b: Sequence[IntOrFQ]) -> Sequence[IntOrFQ]: dega = deg(a) degb = deg(b) temp = [x for x in a] o = [0 for x in a] for i in range(dega - degb, -1, -1): o[i] = int(o[i] + temp[degb + i] * prime_field_inv(int(b[degb]), self.field_modulus)) for c in range(degb + 1): temp[c + i] = (temp[c + i] - o[c]) return [x % self.field_modulus for x in o[:deg(o) + 1]]
def __div__(self: T_FQP, other: Union[int, T_FQP]) -> T_FQP: if isinstance(other, int): return type(self)([ int(c) * prime_field_inv(other, self.field_modulus) % self.field_modulus for c in self.coeffs ]) elif isinstance(other, type(self)): return self * other.inv() else: raise TypeError( "Expected an int or FQP object, but got object of type {}". format(type(other)))
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 test_prime_field_inv(a, n, result): assert prime_field_inv(a, n) % n == result