def get_order_from_curve(cls, curve: ec.EllipticCurve=None): """ Returns the order from the given curve as a CurveBN. """ curve = curve if curve is not None else default_curve() try: curve_nid = backend._elliptic_curve_to_nid(curve) except AttributeError: # Presume that the user passed in the curve_nid curve_nid = curve group = openssl._get_ec_group_by_curve_nid(curve_nid) order = openssl._get_ec_order_by_curve_nid(curve_nid) return CurveBN(order, curve_nid, group, order)
def hash_to_curvebn(*crypto_items, params: UmbralParameters, customization_string: bytes = b'', hash_class: Type[Hash] = Blake2b) -> CurveBN: customization_string = b'hash_to_curvebn' + customization_string hash_function = hash_class(customization_string=customization_string) for item in crypto_items: try: item_bytes = item.to_bytes() except AttributeError: if isinstance(item, bytes): item_bytes = item else: raise TypeError("Input with type {} not accepted".format( type(item))) hash_function.update(item_bytes) hash_digest = openssl._bytes_to_bn(hash_function.finalize()) one = backend._lib.BN_value_one() order_minus_1 = openssl._get_new_BN() res = backend._lib.BN_sub(order_minus_1, params.curve.order, one) backend.openssl_assert(res == 1) bignum = openssl._get_new_BN() with backend._tmp_bn_ctx() as bn_ctx: res = backend._lib.BN_mod(bignum, hash_digest, order_minus_1, bn_ctx) backend.openssl_assert(res == 1) res = backend._lib.BN_add(bignum, bignum, one) backend.openssl_assert(res == 1) return CurveBN(bignum, params.curve)