/src/Botan-3.13.0/src/lib/math/numbertheory/barrett.cpp
Line | Count | Source |
1 | | /* |
2 | | * (C) 2025 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #include <botan/internal/barrett.h> |
8 | | |
9 | | #include <botan/mem_ops.h> |
10 | | #include <botan/internal/ct_utils.h> |
11 | | #include <botan/internal/divide.h> |
12 | | #include <botan/internal/mp_core.h> |
13 | | |
14 | | namespace Botan { |
15 | | |
16 | | Barrett_Reduction::Barrett_Reduction(const BigInt& m, BigInt mu, size_t mw) : |
17 | 64.3k | m_modulus(m), m_mu(std::move(mu)), m_mod_words(mw), m_modulus_bits(m.bits()) { |
18 | | // Give some extra space for Karatsuba |
19 | 64.3k | m_modulus.grow_to(m_mod_words + 8); |
20 | 64.3k | m_mu.grow_to(m_mod_words + 8); |
21 | 64.3k | } |
22 | | |
23 | 0 | Barrett_Reduction Barrett_Reduction::for_secret_modulus(const BigInt& mod) { |
24 | 0 | BOTAN_ARG_CHECK(mod.signum() > 0, "Modulus must be positive"); |
25 | |
|
26 | 0 | const size_t mod_words = mod.sig_words(); |
27 | | |
28 | | // Compute mu = floor(2^{2k} / m) |
29 | 0 | const size_t mu_bits = 2 * WordInfo<word>::bits * mod_words; |
30 | 0 | return Barrett_Reduction(mod, ct_divide_pow2k(mu_bits, mod), mod_words); |
31 | 0 | } |
32 | | |
33 | 64.3k | Barrett_Reduction Barrett_Reduction::for_public_modulus(const BigInt& mod) { |
34 | 64.3k | BOTAN_ARG_CHECK(mod.signum() > 0, "Modulus must be positive"); |
35 | | |
36 | 64.3k | const size_t mod_words = mod.sig_words(); |
37 | | |
38 | | // Compute mu = floor(2^{2k} / m) |
39 | 64.3k | const size_t mu_bits = 2 * WordInfo<word>::bits * mod_words; |
40 | 64.3k | return Barrett_Reduction(mod, vartime_divide_pow2k(mu_bits, mod), mod_words); |
41 | 64.3k | } |
42 | | |
43 | | namespace { |
44 | | |
45 | | /* |
46 | | * Barrett Reduction |
47 | | * |
48 | | * This function assumes that the significant size of x_words (ie the number of |
49 | | * words with a value other than zero) is at most 2 * mod_words. In any case, any |
50 | | * larger value cannot be reduced using Barrett reduction; callers should have |
51 | | * already checked for this. |
52 | | */ |
53 | | BigInt barrett_reduce( |
54 | 189k | size_t mod_words, const BigInt& modulus, const BigInt& mu, std::span<const word> x_words, secure_vector<word>& ws) { |
55 | 189k | BOTAN_ASSERT_NOMSG(modulus.sig_words() == mod_words); |
56 | | |
57 | | // Caller must expand input to be at least this size |
58 | 189k | BOTAN_ASSERT_NOMSG(x_words.size() >= 2 * mod_words); |
59 | | |
60 | | // Normally mod_words + 1 but can be + 2 if the modulus is a power of 2 |
61 | 189k | const size_t mu_words = mu.sig_words(); |
62 | 189k | BOTAN_ASSERT_NOMSG(mu_words <= mod_words + 2); |
63 | | |
64 | 189k | if(ws.size() < 2 * (mod_words + 2)) { |
65 | 56.3k | ws.resize(2 * (mod_words + 2)); |
66 | 56.3k | } |
67 | | |
68 | 189k | CT::poison(x_words); |
69 | | |
70 | | /* |
71 | | * Following the notation of Handbook of Applied Cryptography |
72 | | * Algorithm 14.42 "Barrett modular reduction", page 604 |
73 | | * <https://cacr.uwaterloo.ca/hac/about/chap14.pdf> |
74 | | * |
75 | | * Using `mu` for μ in the code |
76 | | */ |
77 | | |
78 | | // Compute q1 = floor(x / 2^(k - 1)) which is equivalent to ignoring the low (k-1) words |
79 | | |
80 | | // 2 * mod_words + 1 is sufficient, extra is to enable Karatsuba |
81 | 189k | secure_vector<word> r(2 * mu_words + 2); |
82 | | |
83 | 189k | copy_mem(r.data(), x_words.data() + (mod_words - 1), mod_words + 1); |
84 | | |
85 | | // Now compute q2 = q1 * μ |
86 | | |
87 | | // We allocate more size than required since this allows Karatsuba more often; |
88 | | // just `mu_words + (mod_words + 1)` is sufficient |
89 | 189k | const size_t q2_size = 2 * mu_words + 2; |
90 | | |
91 | 189k | secure_vector<word> q2(q2_size); |
92 | | |
93 | 189k | bigint_mul( |
94 | 189k | q2.data(), q2.size(), r.data(), r.size(), mod_words + 1, mu._data(), mu.size(), mu_words, ws.data(), ws.size()); |
95 | | |
96 | | // Compute r2 = (floor(q2 / b^(k+1)) * m) mod 2^(k+1) |
97 | | // The division/floor is again effected by just ignoring the low k + 1 words |
98 | 189k | bigint_mul(r.data(), |
99 | 189k | r.size(), |
100 | 189k | &q2[mod_words + 1], // ignoring the low mod_words + 1 words of the first product |
101 | 189k | q2.size() - (mod_words + 1), |
102 | 189k | mod_words + 1, |
103 | 189k | modulus._data(), |
104 | 189k | modulus.size(), |
105 | 189k | mod_words, |
106 | 189k | ws.data(), |
107 | 189k | ws.size()); |
108 | | |
109 | | // Clear the high words of the product, equivalent to computing mod 2^(k+1) |
110 | | // TODO add masked mul to avoid computing high bits at all |
111 | 189k | clear_mem(std::span{r}.subspan(mod_words + 1)); |
112 | | |
113 | | // Compute r = r1 - r2 |
114 | | |
115 | | // The return value of bigint_sub_abs isn't quite right for what we need here so first compare |
116 | 189k | const int32_t relative_size = bigint_cmp(r.data(), mod_words + 1, x_words.data(), mod_words + 1); |
117 | | |
118 | 189k | bigint_sub_abs(r.data(), r.data(), x_words.data(), mod_words + 1, ws.data()); |
119 | | |
120 | | /* |
121 | | If r is negative then we have to set r to r + 2^(k+1) |
122 | | |
123 | | However for r negative computing this sum is equivalent to computing 2^(k+1) - abs(r) |
124 | | */ |
125 | 189k | clear_mem(ws.data(), mod_words + 2); |
126 | 189k | ws[mod_words + 1] = 1; |
127 | 189k | bigint_sub2(ws.data(), mod_words + 2, r.data(), mod_words + 2); |
128 | | |
129 | | // If relative_size > 0 then assign r to 2^(k+1) - r |
130 | 189k | CT::Mask<word>::is_equal(static_cast<word>(relative_size), 1).select_n(r.data(), ws.data(), r.data(), mod_words + 2); |
131 | | |
132 | | /* |
133 | | * Per HAC Note 14.44 (ii) "step 4 is repeated at most twice since 0 ≤ r < 3m" |
134 | | */ |
135 | 189k | const size_t bound = 2; |
136 | | |
137 | 189k | BOTAN_ASSERT_NOMSG(r.size() >= mod_words + 1); |
138 | 567k | for(size_t i = 0; i != bound; ++i) { |
139 | 378k | const word borrow = bigint_sub3(ws.data(), r.data(), mod_words + 1, modulus._data(), mod_words); |
140 | 378k | CT::Mask<word>::is_zero(borrow).select_n(r.data(), ws.data(), r.data(), mod_words + 1); |
141 | 378k | } |
142 | | |
143 | 189k | CT::unpoison(q2); |
144 | 189k | CT::unpoison(r); |
145 | 189k | CT::unpoison(ws); |
146 | 189k | CT::unpoison(x_words); |
147 | | |
148 | 189k | return BigInt::_from_words(r); |
149 | 189k | } |
150 | | |
151 | 212k | CT::Choice acceptable_barrett_input(const BigInt& x, const BigInt& modulus) { |
152 | 212k | auto x_is_positive = CT::Choice::from_int(static_cast<uint32_t>(x.signum() >= 0)); |
153 | 212k | auto x_lt_mod = bigint_ct_is_lt(x._data(), x.size(), modulus._data(), modulus.sig_words()).as_choice(); |
154 | 212k | return x_is_positive && x_lt_mod; |
155 | 212k | } |
156 | | |
157 | | } // namespace |
158 | | |
159 | 79.9k | BigInt Barrett_Reduction::multiply(const BigInt& x, const BigInt& y) const { |
160 | 79.9k | BOTAN_ARG_CHECK(acceptable_barrett_input(x, m_modulus).as_bool(), "Invalid x param for Barrett multiply"); |
161 | 79.9k | BOTAN_ARG_CHECK(acceptable_barrett_input(y, m_modulus).as_bool(), "Invalid y param for Barrett multiply"); |
162 | | |
163 | 79.9k | secure_vector<word> ws(2 * (m_mod_words + 2)); |
164 | 79.9k | secure_vector<word> xy(2 * m_mod_words); |
165 | | |
166 | 79.9k | bigint_mul(xy.data(), |
167 | 79.9k | xy.size(), |
168 | 79.9k | x._data(), |
169 | 79.9k | x.size(), |
170 | 79.9k | std::min(x.size(), m_mod_words), |
171 | 79.9k | y._data(), |
172 | 79.9k | y.size(), |
173 | 79.9k | std::min(y.size(), m_mod_words), |
174 | 79.9k | ws.data(), |
175 | 79.9k | ws.size()); |
176 | | |
177 | 79.9k | return barrett_reduce(m_mod_words, m_modulus, m_mu, xy, ws); |
178 | 79.9k | } |
179 | | |
180 | 52.7k | BigInt Barrett_Reduction::square(const BigInt& x) const { |
181 | 52.7k | BOTAN_ARG_CHECK(acceptable_barrett_input(x, m_modulus).as_bool(), "Invalid x param for Barrett square"); |
182 | | |
183 | 52.7k | secure_vector<word> ws(2 * (m_mod_words + 2)); |
184 | 52.7k | secure_vector<word> x2(2 * m_mod_words); |
185 | | |
186 | 52.7k | bigint_sqr(x2.data(), x2.size(), x._data(), x.size(), std::min(x.size(), m_mod_words), ws.data(), ws.size()); |
187 | | |
188 | 52.7k | return barrett_reduce(m_mod_words, m_modulus, m_mu, x2, ws); |
189 | 52.7k | } |
190 | | |
191 | 56.3k | BigInt Barrett_Reduction::reduce(const BigInt& x) const { |
192 | 56.3k | BOTAN_ARG_CHECK(x.signum() >= 0, "Argument must be non-negative"); |
193 | | |
194 | 56.3k | const size_t x_sw = x.sig_words(); |
195 | 56.3k | BOTAN_ARG_CHECK(x_sw <= 2 * m_mod_words, "Argument is too large for Barrett reduction"); |
196 | | |
197 | 56.3k | x.grow_to(2 * m_mod_words); |
198 | | |
199 | 56.3k | secure_vector<word> ws; |
200 | 56.3k | return barrett_reduce(m_mod_words, m_modulus, m_mu, x._as_span(), ws); |
201 | 56.3k | } |
202 | | |
203 | | } // namespace Botan |