Ejemplo n.º 1
0
    def intersection(self, interval):
        check_intervals_digits_coincide(self, interval)
        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundToNearest,
                                 precision=mpfr_proxy_precision) as ctx:
            if mpfr(self.lower) > mpfr(interval.upper) or mpfr(
                    self.upper) < mpfr(interval.lower):
                return empty_interval

        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundDown,
                                 precision=mpfr_proxy_precision) as ctx:
            if mpfr(self.lower) > mpfr(interval.upper) or mpfr(
                    self.upper) < mpfr(interval.lower):
                return empty_interval
            if mpfr(self.lower) > mpfr(interval.lower):
                min_bound = self.lower
            else:
                min_bound = interval.lower
            #min_bound=round_number_down_to_digits(max(mpfr(self.lower), mpfr(interval.lower)), self.digits)
        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundUp,
                                 precision=mpfr_proxy_precision) as ctx:
            if mpfr(self.lower) > mpfr(interval.upper) or mpfr(
                    self.upper) < mpfr(interval.lower):
                return empty_interval
            if mpfr(self.upper) < mpfr(interval.upper):
                max_bound = self.upper
            else:
                max_bound = interval.upper
            #max_bound=round_number_up_to_digits(min(mpfr(self.upper),mpfr(interval.upper)), self.digits)
        return Interval(min_bound, max_bound, True, True, self.digits)
Ejemplo n.º 2
0
    def union(self, interval):
        check_intervals_digits_coincide(self, interval)
        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundDown,
                                 precision=mpfr_proxy_precision) as ctx:
            if mpfr(self.lower) < mpfr(interval.lower):
                min = self.lower
                include_min = self.include_lower
            elif mpfr(self.lower) > mpfr(interval.lower):
                min = interval.lower
                include_min = interval.include_lower
            else:
                min = interval.lower
                include_min = interval.include_lower or self.include_lower

        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundUp,
                                 precision=mpfr_proxy_precision) as ctx:
            if mpfr(self.upper) < mpfr(interval.upper):
                max = interval.upper
                include_max = interval.include_upper
            elif mpfr(self.upper) > mpfr(interval.upper):
                max = self.upper
                include_max = self.include_upper
            else:
                max = self.upper
                include_max = self.include_upper or interval.include_upper
        return Interval(min, max, include_min, include_max, self.digits)
Ejemplo n.º 3
0
def adjust_ret_list(retlist):
    retlist[0].cdf_low="0.0"
    retlist[-1].cdf_up="1.0"
    with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx:
        min_value=gmpy2.exp10(-digits_for_input_cdf)
        current=min_value
    for pbox in retlist:
        with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx:
            if gmpy2.is_zero(gmpy2.mpfr(pbox.cdf_low)) or gmpy2.mpfr(pbox.cdf_low)<current:
                pbox.cdf_low=round_number_down_to_digits(current, digits_for_input_cdf)
                with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx:
                    current=current+min_value
            if gmpy2.is_zero(gmpy2.mpfr(pbox.cdf_up)) or gmpy2.mpfr(pbox.cdf_up)<current:
                pbox.cdf_up=round_number_down_to_digits(current, digits_for_input_cdf)

    with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx:
        current=gmpy2.sub(gmpy2.mpfr("1.0"), min_value)
    for pbox in retlist[::-1]:
        with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx:
            if gmpy2.mpfr(pbox.cdf_up)==gmpy2.mpfr("1.0") or gmpy2.mpfr(pbox.cdf_up)>current:
                pbox.cdf_up=round_number_up_to_digits(current, digits_for_input_cdf)
                current=gmpy2.sub(current, min_value)
            if gmpy2.mpfr(pbox.cdf_low)==gmpy2.mpfr("1.0") or gmpy2.mpfr(pbox.cdf_low)>current:
                pbox.cdf_low=round_number_up_to_digits(current, digits_for_input_cdf)

    retlist[0].cdf_low = "0.0"
    retlist[-1].cdf_up = "1.0"

    return retlist
Ejemplo n.º 4
0
    def subtraction(self, interval):
        reset_default_precision()
        res_inc_left = False
        res_inc_right = False
        if self.include_lower and interval.include_lower:
            res_inc_left = True
        if self.include_upper and interval.include_upper:
            res_inc_right = True
        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundDown,
                                 precision=mpfr_proxy_precision) as ctx:
            res_left = gmpy2.sub(mpfr(self.lower), mpfr(interval.upper))
        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundUp,
                                 precision=mpfr_proxy_precision) as ctx:
            res_right = gmpy2.sub(mpfr(self.upper), mpfr(interval.lower))

        if res_left <= res_right:
            res_right = round_number_up_to_digits(res_right, self.digits)
            res_left = round_number_down_to_digits(res_left, self.digits)
            return Interval(res_left, res_right, res_inc_left, res_inc_right,
                            self.digits)
        else:
            res_right = round_number_down_to_digits(res_right, self.digits)
            res_left = round_number_up_to_digits(res_left, self.digits)
            return Interval(res_right, res_left, res_inc_right, res_inc_left,
                            self.digits)
Ejemplo n.º 5
0
def check_interval_is_zero(interval):
    with gmpy2.local_context(gmpy2.context(),
                             round=gmpy2.RoundDown,
                             precision=mpfr_proxy_precision) as ctx:
        left = mpfr(interval.lower)
    with gmpy2.local_context(gmpy2.context(),
                             round=gmpy2.RoundUp,
                             precision=mpfr_proxy_precision) as ctx:
        right = mpfr(interval.upper)
    if gmpy2.is_zero(left) and gmpy2.is_zero(right):
        return True
    return False
def mpfr_roundToIntegral(rm, a):
    assert isinstance(a, MPF)
    if rm == RM_RNA:
        with gmpy2.local_context(mpfr_context(a)):
            mpfr_a = mpf_to_mpfr(a)
            mpfr_r = gmpy2.rint_round(mpfr_a)
            return mpfr_to_mpf(mpfr_r)
    else:
        check_rm(rm)
        with gmpy2.local_context(mpfr_context(a, rm)):
            mpfr_a = mpf_to_mpfr(a)
            mpfr_r = gmpy2.rint(mpfr_a)
            return mpfr_to_mpf(mpfr_r)
Ejemplo n.º 7
0
 def round_value_to_interval(str_value):
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundDown,
                              precision=mpfr_proxy_precision) as ctx:
         res_left = mpfr(str_value)
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundUp,
                              precision=mpfr_proxy_precision) as ctx:
         res_right = mpfr(str_value)
     return Interval(
         round_number_down_to_digits(res_left, digits_for_range),
         round_number_up_to_digits(res_right, digits_for_range), True, True,
         digits_for_range)
Ejemplo n.º 8
0
def check_sterbenz_apply(interval1, interval2):
    with gmpy2.local_context(gmpy2.context(),
                             round=gmpy2.RoundDown,
                             precision=mpfr_proxy_precision) as ctx:
        two_y_left = gmpy2.mul(mpfr("2.0"), mpfr(interval2.lower))
        two_x_left = gmpy2.mul(mpfr("2.0"), mpfr(interval1.lower))
    with gmpy2.local_context(gmpy2.context(),
                             round=gmpy2.RoundUp,
                             precision=mpfr_proxy_precision) as ctx:
        y_right = mpfr(interval2.upper)
        x_right = mpfr(interval1.upper)
    if x_right <= two_y_left and y_right <= two_x_left:
        return True
    return False
Ejemplo n.º 9
0
 def test(self):
     with gmpy2.local_context() as ctx1:  # sandbox context
         ctx1.precision = 4
         x = mpfr(1) / 7
         with gmpy2.local_context() as ctx2:
             # base 10: 0.141
             self.assertEqual(x.digits(), ('141', 0, 4))
             # base 2: 0.001001
             self.assertEqual(x.digits(2), ('1001', -2, 4))
             ctx2.precision += 20
             # changing context precision does not impact existing values
             self.assertEqual(x.digits(2), ('1001', -2, 4))
         # returning to distinct context does not impact existing values
         self.assertEqual(x.digits(2), ('1001', -2, 4))
     return
Ejemplo n.º 10
0
 def compute_middle_point_given_interval(low, upper):
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundDown,
                              precision=mpfr_proxy_precision) as ctx:
         res_left = gmpy2.add(gmpy2.div(mpfr(upper), mpfr("2.0")),
                              gmpy2.div(mpfr(low), mpfr("2.0")))
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundUp,
                              precision=mpfr_proxy_precision) as ctx:
         res_right = gmpy2.add(gmpy2.div(mpfr(upper), mpfr("2.0")),
                               gmpy2.div(mpfr(low), mpfr("2.0")))
     return Interval(
         round_number_down_to_digits(res_left, digits_for_range),
         round_number_up_to_digits(res_right, digits_for_range), True, True,
         digits_for_range)
Ejemplo n.º 11
0
def check_zero_is_in_interval(interval):
    with gmpy2.local_context(gmpy2.context(),
                             round=gmpy2.RoundDown,
                             precision=mpfr_proxy_precision) as ctx:
        left = mpfr(interval.lower)
    with gmpy2.local_context(gmpy2.context(),
                             round=gmpy2.RoundUp,
                             precision=mpfr_proxy_precision) as ctx:
        right = mpfr(interval.upper)
    if gmpy2.is_zero(left) and interval.include_lower:
        return True
    if gmpy2.is_zero(right) and interval.include_upper:
        return True
    if left < mpfr("0.0") < right:
        return True
    return False
def mpfr_sqrt(rm, a):
    assert isinstance(a, MPF)
    check_rm(rm)
    with gmpy2.local_context(mpfr_context(a, rm)):
        mpfr_a = mpf_to_mpfr(a)
        mpfr_r = gmpy2.sqrt(mpfr_a)
        return mpfr_to_mpf(mpfr_r)
def mpfr_max(a, b):
    assert isinstance(a, MPF)
    with gmpy2.local_context(mpfr_context(a)):
        mpfr_a = mpf_to_mpfr(a)
        mpfr_b = mpf_to_mpfr(b)
        mpfr_r = gmpy2.max2(mpfr_a, mpfr_b)
        return mpfr_to_mpf(mpfr_r)
Ejemplo n.º 14
0
 def compute_uncertainty_given_interval(low, upper):
     coefficients = {}
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundDown,
                              precision=mpfr_proxy_precision) as ctx:
         res_left = gmpy2.sub(gmpy2.div(mpfr(upper), mpfr("2.0")),
                              gmpy2.div(mpfr(low), mpfr("2.0")))
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundUp,
                              precision=mpfr_proxy_precision) as ctx:
         res_right = gmpy2.sub(gmpy2.div(mpfr(upper), mpfr("2.0")),
                               gmpy2.div(mpfr(low), mpfr("2.0")))
     coefficients[AffineManager.get_new_error_index()]=\
         Interval(round_number_down_to_digits(res_left, digits_for_range),
                  round_number_up_to_digits(res_right, digits_for_range), True, True, digits_for_range)
     return coefficients
def mpfr_div(rm, a, b):
    assert isinstance(a, MPF)
    check_rm(rm)
    with gmpy2.local_context(mpfr_context(a, rm)):
        mpfr_a = mpf_to_mpfr(a)
        mpfr_b = mpf_to_mpfr(b)
        mpfr_r = mpfr_a / mpfr_b
        return mpfr_to_mpf(mpfr_r)
def mpfr_fma(rm, a, b, c):
    assert isinstance(a, MPF)
    check_rm(rm)
    with gmpy2.local_context(mpfr_context(a, rm)):
        mpfr_a = mpf_to_mpfr(a)
        mpfr_b = mpf_to_mpfr(b)
        mpfr_c = mpf_to_mpfr(c)
        mpfr_r = gmpy2.fma(mpfr_a, mpfr_b, mpfr_c)
        return mpfr_to_mpf(mpfr_r)
Ejemplo n.º 17
0
 def add_all_coefficients_upper_abs(self):
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundAwayZero,
                              precision=mpfr_proxy_precision) as ctx:
         res_right = mpfr("0.0")
         for coeff in self.coefficients:
             res_right = gmpy2.add(
                 res_right, abs(mpfr(self.coefficients[coeff].upper)))
         # now the number is positive so we can round up
         return round_number_up_to_digits(res_right, digits_for_range)
Ejemplo n.º 18
0
def createDSIfromDistribution(distribution, n=50):
    #np.logspace(-9, 5, base=2, num=50) spacing should be done by powers of 2
    if distribution.range_()[0]==0.0 and distribution.range_()[-1]==1.0 and use_powers_of_two_spacing:
        lin_space = powers_of_two_spacing()
    elif "FTE" in distribution.name and use_powers_of_two_spacing:
        lin_space = powers_of_two_error(distribution.d.precision)
    else:
        lin_space = np.linspace(distribution.range_()[0], distribution.range_()[-1], num=n + 1, endpoint=True)

    if custom_spacing:
        try:
            lin_space = distribution.get_my_spacing()
        except:
            lin_space = np.linspace(distribution.range_()[0], distribution.range_()[-1], num=n + 1, endpoint=True)

    cdf_distr=distribution.get_piecewise_cdf()
    ret_list=[]
    for i in range(0, len(lin_space)-1):
        with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx:
            lower=round_number_down_to_digits(gmpy2.mpfr(lin_space[i]), digits_for_input_discretization)
        with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx:
            upper=round_number_up_to_digits(gmpy2.mpfr(lin_space[i+1]), digits_for_input_discretization)
        cdf_low_bound=min(1.0, max(0.0, cdf_distr(float(lin_space[i]))))
        cdf_up_bound=min(1.0, max(0.0, cdf_distr(float(lin_space[i+1]))))
        cdf_low_string=dec2Str(round_near(cdf_low_bound, digits_for_input_cdf))
        cdf_up_string=dec2Str(round_near(cdf_up_bound, digits_for_input_cdf))
        pbox = PBox(Interval(lower, upper, True, False, digits_for_range),
                             cdf_low_string, cdf_up_string)
        ret_list.append(pbox)

    ret_list=adjust_ret_list(ret_list)

    with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundDown, precision=mpfr_proxy_precision) as ctx:
        ret_list[0].interval.lower = \
            round_number_down_to_digits(gmpy2.mpfr(distribution.a_real), digits_for_input_discretization)
    with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundUp, precision=mpfr_proxy_precision) as ctx:
        ret_list[-1].interval.upper = \
            round_number_up_to_digits(gmpy2.mpfr(distribution.b_real), digits_for_input_discretization)
    ret_list[-1].interval.include_upper = True
    mixarith=MixedArithmetic(ret_list[0].interval.lower,ret_list[-1].interval.upper,ret_list)
    return mixarith
Ejemplo n.º 19
0
 def clean_affine_operation(self):
     keys = []
     for key in self.coefficients:
         value = self.coefficients[key]
         remove = [False, False]
         with gmpy2.local_context(gmpy2.context(),
                                  round=gmpy2.RoundDown,
                                  precision=mpfr_proxy_precision) as ctx:
             if gmpy2.is_zero(mpfr(value.lower)):
                 remove[0] = True
         with gmpy2.local_context(gmpy2.context(),
                                  round=gmpy2.RoundUp,
                                  precision=mpfr_proxy_precision) as ctx:
             if gmpy2.is_zero(mpfr(value.upper)):
                 remove[1] = True
         if remove[0] and remove[1]:
             keys.append(key)
     for delete in keys:
         del self.coefficients[delete]
     self.update_interval()
     return self
Ejemplo n.º 20
0
def find_min_abs_interval(interval):
    with gmpy2.local_context(gmpy2.context(),
                             round=gmpy2.RoundToZero,
                             precision=mpfr_proxy_precision) as ctx:
        left = abs(mpfr(interval.lower))
        right = abs(mpfr(interval.upper))
    if left < right:
        #Now the number is positive so we can round down
        return round_number_down_to_digits(left, digits_for_range)
    else:
        #Now the number is positive so we can round down
        return round_number_down_to_digits(right, digits_for_range)
Ejemplo n.º 21
0
 def compute_interval(self):
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundDown,
                              precision=mpfr_proxy_precision) as ctx:
         res_left = gmpy2.sub(mpfr(self.center.lower),
                              mpfr(self.add_all_coefficients_lower_abs()))
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundUp,
                              precision=mpfr_proxy_precision) as ctx:
         res_right = gmpy2.add(mpfr(self.center.upper),
                               mpfr(self.add_all_coefficients_upper_abs()))
     if res_left <= res_right:
         return Interval(
             round_number_down_to_digits(res_left, digits_for_range),
             round_number_up_to_digits(res_right, digits_for_range), True,
             True, digits_for_range)
     else:
         return Interval(
             round_number_down_to_digits(res_right, digits_for_range),
             round_number_up_to_digits(res_left, digits_for_range), True,
             True, digits_for_range)
Ejemplo n.º 22
0
 def check_zero_is_in_interval(self):
     zero_is_included = False
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundDown,
                              precision=mpfr_proxy_precision) as ctx:
         if mpfr(self.lower) < mpfr("0.0") < mpfr(self.upper):
             zero_is_included = True
         if mpfr(self.lower) == mpfr("0.0") and self.include_lower:
             zero_is_included = True
         if mpfr(self.upper) == mpfr("0.0") and self.include_upper:
             zero_is_included = True
     with gmpy2.local_context(gmpy2.context(),
                              round=gmpy2.RoundUp,
                              precision=mpfr_proxy_precision) as ctx:
         if mpfr(self.lower) < mpfr("0.0") < mpfr(self.upper):
             zero_is_included = True
         if mpfr(self.lower) == mpfr("0.0") and self.include_lower:
             zero_is_included = True
         if mpfr(self.upper) == mpfr("0.0") and self.include_upper:
             zero_is_included = True
     return zero_is_included
Ejemplo n.º 23
0
def powers_of_two_spacing():
    exp_spacing=[]
    with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundToNearest, precision=mpfr_proxy_precision) as ctx:
        exponent=gmpy2.mpfr("0")
        while abs(exponent)<discretization_points:
            value=gmpy2.exp2(exponent)
            exp_spacing.insert(0, round_number_nearest_to_digits(value, digits_for_input_discretization))
            exponent=gmpy2.sub(exponent,gmpy2.mpfr("1"))
        exp_spacing.insert(0, "0.0")
    for index, value in enumerate(exp_spacing[:-1]):
        if value==exp_spacing[index+1]:
            print("Problem with digits in powers of 2")
            exit(-1)
    return exp_spacing
Ejemplo n.º 24
0
def powers_of_two_error(precision):
    exp_spacing=[]
    with gmpy2.local_context(gmpy2.context(), round=gmpy2.RoundToNearest, precision=mpfr_proxy_precision) as ctx:
        exponent=gmpy2.mpfr(-precision)
        counter=0
        while counter<discretization_points//2:
            value=gmpy2.exp2(exponent)
            exp_spacing.insert(0, round_number_nearest_to_digits(value, digits_for_input_discretization))
            exponent=gmpy2.sub(exponent,gmpy2.mpfr("1"))
            counter=counter+1
    exp_spacing_reverse=copy.deepcopy(exp_spacing)
    exp_spacing_reverse.reverse()
    exp_spacing_reverse=["-"+s for s in exp_spacing_reverse]
    exp_spacing=exp_spacing_reverse+["0.0"]+exp_spacing
    for index, value in enumerate(exp_spacing[:-1]):
        if value==exp_spacing[index+1]:
            print("Problem with digits in powers of 2")
            exit(-1)
    return exp_spacing
Ejemplo n.º 25
0
    def multiplication(self, interval):
        reset_default_precision()
        tmp_res_left = []
        tmp_res_right = []

        zero_is_included = self.check_zero_is_in_interval(
        ) or interval.check_zero_is_in_interval()

        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundDown,
                                 precision=mpfr_proxy_precision) as ctx:
            tmp_res_left.append(
                gmpy2.mul(mpfr(self.lower), mpfr(interval.lower)))
            tmp_res_left.append(
                gmpy2.mul(mpfr(self.lower), mpfr(interval.upper)))
            tmp_res_left.append(
                gmpy2.mul(mpfr(self.upper), mpfr(interval.lower)))
            tmp_res_left.append(
                gmpy2.mul(mpfr(self.upper), mpfr(interval.upper)))
            min_index = [
                i for i, value in enumerate(tmp_res_left)
                if value == min(tmp_res_left)
            ]

        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundUp,
                                 precision=mpfr_proxy_precision) as ctx:
            tmp_res_right.append(
                gmpy2.mul(mpfr(self.lower), mpfr(interval.lower)))
            tmp_res_right.append(
                gmpy2.mul(mpfr(self.lower), mpfr(interval.upper)))
            tmp_res_right.append(
                gmpy2.mul(mpfr(self.upper), mpfr(interval.lower)))
            tmp_res_right.append(
                gmpy2.mul(mpfr(self.upper), mpfr(interval.upper)))
            max_index = [
                i for i, value in enumerate(tmp_res_right)
                if value == max(tmp_res_right)
            ]

        tmp_bounds = [
            self.include_lower * interval.include_lower,
            self.include_lower * interval.include_upper,
            self.include_upper * interval.include_lower,
            self.include_upper * interval.include_upper
        ]

        res_inc_left = any([tmp_bounds[index] for index in min_index])
        res_inc_right = any([tmp_bounds[index] for index in max_index])

        min_value = tmp_res_left[min_index[0]]
        max_value = tmp_res_right[max_index[0]]

        if gmpy2.is_zero(min_value) and zero_is_included:
            res_inc_left = True

        if gmpy2.is_zero(max_value) and zero_is_included:
            res_inc_right = True

        res_left = round_number_down_to_digits(min_value, self.digits)
        res_right = round_number_up_to_digits(max_value, self.digits)
        return Interval(res_left, res_right, res_inc_left, res_inc_right,
                        self.digits)
Ejemplo n.º 26
0
cipher = 2780321436921227845269766067805604547641764672251687438825498122989499386967784164108893743279610287605669769995594639683212592165536863280639528420328182048065518360606262307313806591343147104009274770408926901136562839153074067955850912830877064811031354484452546219065027914838811744269912371819665118277221
n = 23571113171923293137414347535961677173798389971011031071091131271311371391491511571631671731791811911931971992112232272292332392412512572632692712772812832933073113133173313373473493533593673733793833893974014094194214314334394434494574614634674794874914995035095215235415475575635695715775875935996016076136176196316416436476536596616736776836917017097197277337397437517577617697737877978098118218238278298398538578598638778818838879079119199299379419479539679719779839919971009101310191431936117404941729571877755575331917062752829306305198341421305376800954281557410379953262534149212590443063350628712530148541217933209759909975139820841212346188350112608680453894647472456216566674289561525527394398888860917887112180144144965154878409149321280697460295807024856510864232914981820173542223592901476958693572703687098161888680486757805443187028074386001621827485207065876653623459779938558845775617779542038109532989486603799040658192890612331485359615639748042902366550066934348195272617921683

Low Public Exponent Attack
"""

import sys
try:
    import gmpy2
except ImportError:
    print("Install gmpy2 first to run this program")
    sys.exit()

e = 3
cipher = 2780321436921227845269766067805604547641764672251687438825498122989499386967784164108893743279610287605669769995594639683212592165536863280639528420328182048065518360606262307313806591343147104009274770408926901136562839153074067955850912830877064811031354484452546219065027914838811744269912371819665118277221
n = 23571113171923293137414347535961677173798389971011031071091131271311371391491511571631671731791811911931971992112232272292332392412512572632692712772812832933073113133173313373473493533593673733793833893974014094194214314334394434494574614634674794874914995035095215235415475575635695715775875935996016076136176196316416436476536596616736776836917017097197277337397437517577617697737877978098118218238278298398538578598638778818838879079119199299379419479539679719779839919971009101310191431936117404941729571877755575331917062752829306305198341421305376800954281557410379953262534149212590443063350628712530148541217933209759909975139820841212346188350112608680453894647472456216566674289561525527394398888860917887112180144144965154878409149321280697460295807024856510864232914981820173542223592901476958693572703687098161888680486757805443187028074386001621827485207065876653623459779938558845775617779542038109532989486603799040658192890612331485359615639748042902366550066934348195272617921683
n = hex(n)

import gmpy2

with gmpy2.local_context(gmpy2.context(), precision=800) as ctx:
    ctx.precision += 800
    root = gmpy2.cbrt(cipher)

try:
    print(str('%x' % +int(root)).decode('hex'))
except AttributeError:
    print(bytes.fromhex(str('%x' % +int(root))).decode('utf-8'))

# answer!!
# dsc{t0-m355-w1th-m4th-t4k35-4-l0t-0f-sp1n3}
Ejemplo n.º 27
0
out = partial(print, sep='\n', end='\n\n')

if __name__ == '__main__':
    # Reales de precisión arbitraria
    # En el siguiente ejemplo se muestra con la precisión normal
    out ('Raíz de dos: ', gmpy2.sqrt(2))

    # Obtenemos la precisión máxima teórica de la computadora
    out ('Precisión máxima: ', gmpy2.get_max_precision())

    # ¿Cuál es la precisión por defecto?
    out ('Precisión por defecto', gmpy2.get_context().precision)

    # Cambiamos la precisión de la biblioteca
    gmpy2.get_context().precision=100

    # Otra vez la raíz de dos, con precisión aumentada
    out ('Raíz de dos: ', gmpy2.sqrt(2))
    # De hecho es la precisión de cualquier número
    out ('Un flotante: ', mpfr('1.2'))

    # Ahora creamos un contexto para manejar la precisión
    with gmpy2.local_context(gmpy2.context(), precision=200) as ctx:
        out ('Precisión alterada en contexto', gmpy2.sqrt(2))
        ctx.precision += 100
        out ('Aún mejor precision', gmpy2.sqrt(2))

    # De regreso al contexto original, precision normal
    out ('Raíz de dos', gmpy2.sqrt(2))
Ejemplo n.º 28
0
 def wrapped(self: 'FPVector', *args, **kwargs):
     with gmpy2.local_context(self._ctx_):
         return fn(self, *args, **kwargs)
Ejemplo n.º 29
0
    def division(self, interval):
        reset_default_precision()
        tmp_res_left = []
        tmp_res_right = []

        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundDown,
                                 precision=mpfr_proxy_precision) as ctx:
            new_right_lower = gmpy2.div(1.0, mpfr(interval.upper))
            new_right_upper = gmpy2.div(1.0, mpfr(interval.lower))
            tmp_res_left.append(
                gmpy2.mul(mpfr(self.lower), mpfr(new_right_lower)))
            tmp_res_left.append(
                gmpy2.mul(mpfr(self.lower), mpfr(new_right_upper)))
            tmp_res_left.append(
                gmpy2.mul(mpfr(self.upper), mpfr(new_right_lower)))
            tmp_res_left.append(
                gmpy2.mul(mpfr(self.upper), mpfr(new_right_upper)))
            min_index = [
                i for i, value in enumerate(tmp_res_left)
                if value == min(tmp_res_left)
            ]

        with gmpy2.local_context(gmpy2.context(),
                                 round=gmpy2.RoundUp,
                                 precision=mpfr_proxy_precision) as ctx:
            new_right_lower = gmpy2.div(1.0, mpfr(interval.upper))
            new_right_upper = gmpy2.div(1.0, mpfr(interval.lower))
            tmp_res_right.append(
                gmpy2.mul(mpfr(self.lower), mpfr(new_right_lower)))
            tmp_res_right.append(
                gmpy2.mul(mpfr(self.lower), mpfr(new_right_upper)))
            tmp_res_right.append(
                gmpy2.mul(mpfr(self.upper), mpfr(new_right_lower)))
            tmp_res_right.append(
                gmpy2.mul(mpfr(self.upper), mpfr(new_right_upper)))
            max_index = [
                i for i, value in enumerate(tmp_res_right)
                if value == max(tmp_res_right)
            ]

        # We have to swap the boundaries here   1/[1,2]=[.5, 1]
        new_right_lower_inc = interval.include_upper
        new_right_upper_inc = interval.include_lower

        tmp_bounds = [
            self.include_lower * new_right_lower_inc,
            self.include_lower * new_right_upper_inc,
            self.include_upper * new_right_lower_inc,
            self.include_upper * new_right_upper_inc
        ]

        res_inc_left = any([tmp_bounds[index] for index in min_index])
        res_inc_right = any([tmp_bounds[index] for index in max_index])

        res_left = round_number_down_to_digits(tmp_res_left[min_index[0]],
                                               self.digits)
        res_right = round_number_up_to_digits(tmp_res_right[max_index[0]],
                                              self.digits)

        return Interval(res_left, res_right, res_inc_left, res_inc_right,
                        self.digits)
def mpfr_neg(a):
    assert isinstance(a, MPF)
    with gmpy2.local_context(mpfr_context(a)):
        mpfr_a = mpf_to_mpfr(a)
        mpfr_r = -mpfr_a
        return mpfr_to_mpf(mpfr_r)