Barrett Reduction
   HOME

TheInfoList



OR:

In
modular arithmetic In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" when reaching a certain value, called the modulus. The modern approach to modular arithmetic was developed by Carl Friedrich Gauss in his book ...
, Barrett reduction is a reduction
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
introduced in 1986 by P.D. Barrett. A naive way of computing :c = a \,\bmod\, n \, would be to use a fast
division algorithm A division algorithm is an algorithm which, given two integers N and D, computes their quotient and/or remainder, the result of Euclidean division. Some are applied by hand, while others are employed by digital circuit designs and software. Divis ...
. Barrett reduction is an algorithm designed to optimize this operation assuming n is constant, and a, replacing divisions by multiplications.


General idea

Let s=1/n be the inverse of n as a
floating point In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be ...
number. Then :a \,\bmod\, n = a-\lfloor a s\rfloor n where \lfloor x \rfloor denotes the
floor function In mathematics and computer science, the floor function is the function that takes as input a real number , and gives as output the greatest integer less than or equal to , denoted or . Similarly, the ceiling function maps to the least int ...
. The result is exact, as long as s is computed with sufficient accuracy.


Single-word Barrett reduction

Barrett initially considered an integer version of the above algorithm when the values fit into machine words. When calculating a \,\bmod\, n using the method above, but with integers, the obvious analogue would be to use division by n: func reduce(a uint) uint However, division can be expensive and, in cryptographic settings, may not be a constant-time instruction on some CPUs. Thus Barrett reduction approximates 1/n with a value m/2^k because division by 2^k is just a right-shift and so it is cheap. In order to calculate the best value for m given 2^k consider: :\frac = \frac \;\Longleftrightarrow\; m = \frac In order for m to be an integer, we need to round / somehow. Rounding to the nearest integer will give the best approximation but can result in m/2^k being larger than 1/n, which can cause underflows. Thus m = \lfloor / \rfloor is generally used. Thus we can approximate the function above with: func reduce(a uint) uint However, since m/2^k \le 1/n, the value of q in that function can end up being one too small, and thus a is only guaranteed to be within func_reduce(a_uint)_uint_ Since_m/2^k_is_only_an_approximation,_the_valid_range_of_a_needs_to_be_considered._The_error_of_the_approximation_of_1/n_is: :e_=_\frac_-_\frac Thus_the_error_in_the_value_of_q_is_ae._As_long_as_ae_<_1_then_the_reduction_is_valid_thus_a_<_1/e._The_reduction_function_might_not_immediately_give_the_wrong_answer_when_a_\ge_1/e_but_the_bounds_on_a_must_be_respected_to_ensure_the_correct_answer_in_the_general_case. By_choosing_larger_values_of_k,_the_range_of_values_of_a_for_which_the_reduction_is_valid_can_be_increased,_but_larger_values_of_k_may_cause_overflow_problems_elsewhere.


__Example_

Consider_the_case_of_n_=_101_when_operating_with_16-bit_integers. The_smallest_value_of_k_that_makes_sense_is_k_=_7_because_if_2^k_<_n_then_the_reduction_will_only_be_valid_for_values_that_are_already_minimal!_For_a_value_of_seven,_m_=_\lfloor_2^k_/_n_\rfloor_=_\lfloor_128_/_101_\rfloor_=_1._For_a_value_of_eight_m_=_\lfloor_256_/_101_\rfloor_=_2._Thus_k_=_8_provides_no_advantage_because_the_approximation_of_1/101_in_that_case_(2/256)_is_exactly_the_same_as_1/128._For_k_=_9,_we_get_m_=_\lfloor_512_/_101_\rfloor_=_5,_which_is_a_better_approximation. Now_we_consider_the_valid_input_ranges_for_k_=_7_and_k_=_9._In_the_first_case,_e_=_1/n_-_m/2^k_=_1/101_-_1/128_=_27/12928_so_a_<_1/e_implies_a_<_478.81._Since_a_is_an_integer,_effectively_the_maximum_value_is_478._(In_practice_the_function_happens_to_work_for_values_up_to_504.) If_we_take_k_=_9_then_e_=_1/101_-_5/512_=_7/51712_and_so_the_maximum_value_of_a_is_7387._(In_practice_the_function_happens_to_work_until_7473.) The_next_value_of_k_that_results_in_a_better_approximation_is_13,_giving_81/8192._However,_note_that_the_intermediate_value_ax_in_the_calculation_will_then_overflow_an_unsigned_16-bit_value_when_810_\le_a,_thus_k_=_7_works_better_in_this_situation.


__Proof_for_range_for_a_specific_k_

Let_k_0_be_the_smallest_integer_such_that_2^>n._Take_k_0+1_as_a_reasonable_value_for_k_in_the_above_equations._As_in_the_code_snippets_above,_let *_q_=_\left\lfloor_\frac_\right\rfloor__and *_r_=_a_-_q_n. Because_of_the_floor_function_ In__mathematics_and_computer_science,_the_floor_function_is_the_function__that_takes_as_input_a_real_number_,_and_gives_as_output_the_greatest_integer_less_than_or_equal_to_,_denoted__or_.__Similarly,_the_ceiling_function_maps__to_the_least_int_...
,_q_is_an_integer_and_r_\equiv_a_\pmod._Also,_if_a_\le_2_^_k_then_r_<_2n._In_that_case,_writing_the_snippets_above_as_an_expression: :a_\,\bmod\,_n_=_\begin_r_&_\text_r The_proof_that_r_<_2n_follows: If_a_\le_2_^_k,_then :\frac_\cdot_(2_^_k_\mod_n)_<_n Since_n\cdot\frac_<_n_regardless_of_a,_it_follows_that :_\frac_+_n\cdot\frac_<_2n :_a__-_\left(a_-_\frac\right)_+_\frac_<_2n :_a__-_\frac_\cdot_\left(2^k_-_(2^k_\mod_n)\right)_+_\frac_<_2n :_a__-_\frac_\cdot_\left(\frac\right)_+_\frac_<_2n :_a__-_\frac_\cdot_\left\lfloor\frac\right\rfloor\_+_\frac_<_2n :_a__-_\frac_+_\frac_<_2n :_a__-_\left(\frac\right)\cdot_n_<_2n :_a__-_\left\lfloor\frac\right\rfloor\cdot_n_<_2n :_a__-_q_n_<_2n :r_<_2n


__Multi-word_Barrett_reduction_

Barrett's_primary_motivation_for_considering_reduction_was_the_implementation_of_RSA_(cryptosystem).html" ;"title=", 2n) rather than [0, n) as is generally required. A conditional subtraction will correct this: func reduce(a uint) uint Since m/2^k is only an approximation, the valid range of a needs to be considered. The error of the approximation of 1/n is: :e = \frac - \frac Thus the error in the value of q is ae. As long as ae < 1 then the reduction is valid thus a < 1/e. The reduction function might not immediately give the wrong answer when a \ge 1/e but the bounds on a must be respected to ensure the correct answer in the general case. By choosing larger values of k, the range of values of a for which the reduction is valid can be increased, but larger values of k may cause overflow problems elsewhere.


Example

Consider the case of n = 101 when operating with 16-bit integers. The smallest value of k that makes sense is k = 7 because if 2^k < n then the reduction will only be valid for values that are already minimal! For a value of seven, m = \lfloor 2^k / n \rfloor = \lfloor 128 / 101 \rfloor = 1. For a value of eight m = \lfloor 256 / 101 \rfloor = 2. Thus k = 8 provides no advantage because the approximation of 1/101 in that case (2/256) is exactly the same as 1/128. For k = 9, we get m = \lfloor 512 / 101 \rfloor = 5, which is a better approximation. Now we consider the valid input ranges for k = 7 and k = 9. In the first case, e = 1/n - m/2^k = 1/101 - 1/128 = 27/12928 so a < 1/e implies a < 478.81. Since a is an integer, effectively the maximum value is 478. (In practice the function happens to work for values up to 504.) If we take k = 9 then e = 1/101 - 5/512 = 7/51712 and so the maximum value of a is 7387. (In practice the function happens to work until 7473.) The next value of k that results in a better approximation is 13, giving 81/8192. However, note that the intermediate value ax in the calculation will then overflow an unsigned 16-bit value when 810 \le a, thus k = 7 works better in this situation.


Proof for range for a specific k

Let k_0 be the smallest integer such that 2^>n. Take k_0+1 as a reasonable value for k in the above equations. As in the code snippets above, let * q = \left\lfloor \frac \right\rfloor and * r = a - q n. Because of the
floor function In mathematics and computer science, the floor function is the function that takes as input a real number , and gives as output the greatest integer less than or equal to , denoted or . Similarly, the ceiling function maps to the least int ...
, q is an integer and r \equiv a \pmod. Also, if a \le 2 ^ k then r < 2n. In that case, writing the snippets above as an expression: :a \,\bmod\, n = \begin r & \text r The proof that r < 2n follows: If a \le 2 ^ k, then :\frac \cdot (2 ^ k \mod n) < n Since n\cdot\frac < n regardless of a, it follows that : \frac + n\cdot\frac < 2n : a - \left(a - \frac\right) + \frac < 2n : a - \frac \cdot \left(2^k - (2^k \mod n)\right) + \frac < 2n : a - \frac \cdot \left(\frac\right) + \frac < 2n : a - \frac \cdot \left\lfloor\frac\right\rfloor\ + \frac < 2n : a - \frac + \frac < 2n : a - \left(\frac\right)\cdot n < 2n : a - \left\lfloor\frac\right\rfloor\cdot n < 2n : a - q n < 2n :r < 2n


Multi-word Barrett reduction

Barrett's primary motivation for considering reduction was the implementation of RSA (cryptosystem)">RSA, where the values in question will almost certainly exceed the size of a machine word. In this situation, Barrett provided an algorithm that approximates the single-word version above but for multi-word values. For details see section 14.3.3 of the ''Handbook of Applied Cryptography''.


Barrett algorithm for polynomials

It is also possible to use Barrett algorithm for polynomial division, by reversing polynomials and using X-adic arithmetic.


See also

* Montgomery reduction is another similar algorithm.


References


Sources

* * {{refend Computer arithmetic Modular arithmetic