9#include <botan/numthry.h>
11#include <botan/reducer.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/divide.h>
15#include <botan/internal/monty.h>
16#include <botan/internal/monty_exp.h>
17#include <botan/internal/mp_core.h>
18#include <botan/internal/primality.h>
32 if(p == 2 || a <= 1) {
43 auto monty_p = std::make_shared<Montgomery_Params>(p, mod_p);
140 word y_mod_8 = y % 8;
141 if(y_mod_8 == 3 || y_mod_8 == 5) {
146 if(x % 4 == 3 && y % 4 == 3) {
172 for(
size_t i = 0; i != n.
size(); ++i) {
176 const size_t tz_x =
ctz(x);
180 low_zero += seen_nonempty_word.if_not_set_return(tz_x);
187 return static_cast<size_t>(seen_nonempty_word.if_set_return(low_zero));
201 const size_t sz = std::max(a.
sig_words(),
b.sig_words());
214 const size_t loop_cnt = u.bits() + v.bits();
223 size_t factors_of_two = 0;
224 for(
size_t i = 0; i != loop_cnt; ++i) {
225 auto both_odd = WordMask::expand(u.is_odd()) & WordMask::expand(v.is_odd());
228 auto u_gt_v = WordMask::expand(
bigint_cmp(u._data(), u.size(), v._data(), v.size()) > 0);
230 u.ct_cond_assign((u_gt_v & both_odd).as_bool(), tmp);
231 v.ct_cond_assign((~u_gt_v & both_odd).as_bool(), tmp);
233 const auto u_is_even = WordMask::expand(u.is_even());
234 const auto v_is_even = WordMask::expand(v.is_even());
239 factors_of_two += (u_is_even & v_is_even).if_set_return(1);
243 u.ct_cond_assign(u_is_even.as_bool(), tmp);
247 v.ct_cond_assign(v_is_even.as_bool(), tmp);
257 u.ct_cond_assign(u.is_even() , v);
260 u.ct_shift_left(factors_of_two);
277 const auto g =
gcd(a,
b);
298 const size_t exp_bits = exp.
bits();
301 auto monty_params = std::make_shared<Montgomery_Params>(mod, reduce_mod);
302 return monty_exp(monty_params, reduce_mod.
reduce(base), exp, exp_bits);
313 for(
size_t i = 0; i != exp_bits; ++i) {
329 const size_t n = C.
bits();
330 const size_t m = (n + 1) / 2;
337 X = (X2 + C) / (2 *
X);
363 const size_t n_bits = n.
bits();
367 const uint16_t num =
static_cast<uint16_t
>(n.
word_at(0));
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ARG_CHECK(expr, msg)
void ct_cond_assign(bool predicate, const BigInt &other)
word word_at(size_t n) const
static BigInt power_of_2(size_t n)
static BigInt from_s32(int32_t n)
static BigInt from_word(word n)
BigInt & square(secure_vector< word > &ws)
bool get_bit(size_t n) const
static BigInt with_capacity(size_t n)
static constexpr Mask< T > expand(T v)
static constexpr Mask< T > cleared()
BigInt square(const BigInt &x) const
BigInt multiply(const BigInt &x, const BigInt &y) const
BigInt reduce(const BigInt &x) const
virtual bool is_seeded() const =0
constexpr void unpoison_all(Ts &&... ts)
constexpr void poison_all(Ts &&... ts)
BigInt monty_exp(const std::shared_ptr< const Montgomery_Params > ¶ms_p, const BigInt &g, const BigInt &k, size_t max_k_bits)
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
constexpr void bigint_shr2(W y[], const W x[], size_t x_size, size_t shift)
BigInt lcm(const BigInt &a, const BigInt &b)
BigInt square(const BigInt &x)
size_t low_zero_bits(const BigInt &n)
BigInt abs(const BigInt &n)
const size_t PRIME_TABLE_SIZE
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
bool is_miller_rabin_probable_prime(const BigInt &n, const Modular_Reducer &mod_n, RandomNumberGenerator &rng, size_t test_iterations)
constexpr int32_t bigint_cmp(const W x[], size_t x_size, const W y[], size_t y_size)
constexpr size_t ctz(T n)
void ct_divide(const BigInt &x, const BigInt &y, BigInt &q_out, BigInt &r_out)
bool is_bailie_psw_probable_prime(const BigInt &n, const Modular_Reducer &mod_n)
BigInt monty_exp_vartime(const std::shared_ptr< const Montgomery_Params > ¶ms_p, const BigInt &g, const BigInt &k)
constexpr auto bigint_sub_abs(W z[], const W x[], const W y[], size_t N, W ws[]) -> CT::Mask< W >
BigInt gcd(const BigInt &a, const BigInt &b)
std::vector< T, secure_allocator< T > > secure_vector
BigInt sqrt_modulo_prime(const BigInt &a, const BigInt &p)
BigInt is_perfect_square(const BigInt &C)
size_t miller_rabin_test_iterations(size_t n_bits, size_t prob, bool random)
int32_t jacobi(const BigInt &a, const BigInt &n)
bool is_lucas_probable_prime(const BigInt &C, const Modular_Reducer &mod_C)