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>
25void sub_abs(BigInt& z,
const BigInt& x,
const BigInt& y) {
26 const size_t x_sw = x.sig_words();
27 const size_t y_sw = y.sig_words();
28 z.resize(std::max(x_sw, y_sw));
44 if(p == 2 || a <= 1) {
55 auto monty_p = std::make_shared<Montgomery_Params>(p, mod_p);
152 word y_mod_8 = y % 8;
153 if(y_mod_8 == 3 || y_mod_8 == 5) {
158 if(x % 4 == 3 && y % 4 == 3) {
184 for(
size_t i = 0; i != n.
size(); ++i) {
188 const size_t tz_x =
ctz(x);
192 low_zero += seen_nonempty_word.if_not_set_return(tz_x);
199 return seen_nonempty_word.if_set_return(low_zero);
204size_t safegcd_loop_bound(
size_t f_bits,
size_t g_bits) {
205 const size_t d = std::max(f_bits, g_bits);
221 if(a == 1 || b == 1) {
245 const size_t loop_cnt = safegcd_loop_bound(f.
bits(), g.
bits());
248 for(
size_t i = 0; i != loop_cnt; ++i) {
251 const bool need_swap = (g.
is_odd() && delta > 0);
284 const auto g =
gcd(a, b);
305 const size_t exp_bits = exp.
bits();
308 auto monty_params = std::make_shared<Montgomery_Params>(mod, reduce_mod);
309 return monty_exp(monty_params, reduce_mod.
reduce(base), exp, exp_bits);
320 for(
size_t i = 0; i != exp_bits; ++i) {
336 const size_t n = C.
bits();
337 const size_t m = (n + 1) / 2;
344 X = (X2 + C) / (2 *
X);
370 const size_t n_bits = n.
bits();
374 const uint16_t num =
static_cast<uint16_t
>(n.
word_at(0));
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
void ct_cond_add(bool predicate, const BigInt &value)
void ct_cond_assign(bool predicate, const BigInt &other)
word word_at(size_t n) const
static BigInt power_of_2(size_t n)
void const_time_poison() const
static BigInt from_s32(int32_t n)
void ct_cond_swap(bool predicate, BigInt &other)
static BigInt from_word(word n)
BigInt & square(secure_vector< word > &ws)
void const_time_unpoison() const
bool get_bit(size_t n) const
static Mask< T > expand(T v)
static 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
void unpoison(const T *p, size_t n)
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
BigInt monty_exp(std::shared_ptr< const Montgomery_Params > params_p, const BigInt &g, const BigInt &k, size_t max_k_bits)
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)
CT::Mask< word > bigint_sub_abs(word z[], const word x[], const word y[], size_t N, word ws[])
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 size_t ctz(T n)
BigInt monty_exp_vartime(std::shared_ptr< const Montgomery_Params > params_p, const BigInt &g, const BigInt &k)
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 gcd(const BigInt &a, const BigInt &b)
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)
std::vector< T, secure_allocator< T > > secure_vector