8 #include <botan/numthry.h>
9 #include <botan/reducer.h>
10 #include <botan/monty.h>
11 #include <botan/divide.h>
12 #include <botan/rng.h>
13 #include <botan/internal/bit_ops.h>
14 #include <botan/internal/mp_core.h>
15 #include <botan/internal/ct_utils.h>
16 #include <botan/internal/monty_exp.h>
17 #include <botan/internal/primality.h>
24 void 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));
46 for(
size_t i = 0; i != n.
size(); ++i)
56 low_zero += BOTAN_MP_WORD_BITS;
87 const size_t loop_cnt = 4 + 3*std::max(f.
bits(), g.
bits());
90 for(
size_t i = 0; i != loop_cnt; ++i)
96 const bool need_swap = (g.
is_odd() && delta > 0);
143 BigInt u = p, v = a, r = 0, s = 1;
190 for(
size_t i = 0; i != k; ++i)
203 throw Invalid_Argument(
"ct_inverse_mod_odd_modulus: arguments must be non-negative");
207 throw Invalid_Argument(
"ct_inverse_mod_odd_modulus n >= mod not supported");
229 BigInt mp1o2 = (mod + 1) >> 1;
231 const size_t mod_words = mod.
sig_words();
240 v.grow_to(mod_words);
254 size_t bits = 2 * mod.
bits();
281 const word odd_a = a_w[0] & 1;
284 word underflow =
bigint_cnd_sub(odd_a, a_w.data(), b_w.data(), mod_words);
295 word borrow =
bigint_cnd_sub(odd_a, u_w.data(), v_w.data(), mod_words);
300 const word odd_u = u_w[0] & 1;
334 if(mod.
is_odd() && n < mod)
351 BigInt A = 1, B = 0, C = 0, D = 1;
362 const bool u_gte_v = (u >= v);
364 for(
size_t i = 0; i != u_zero_bits; ++i)
366 const bool needs_adjust = A.
is_odd() || B.is_odd();
372 B.ct_cond_assign(needs_adjust, T1);
378 for(
size_t i = 0; i != v_zero_bits; ++i)
380 const bool needs_adjust = C.
is_odd() || D.is_odd();
385 D.ct_cond_assign(needs_adjust, T1);
401 B.ct_cond_assign(u_gte_v, T2);
403 v.ct_cond_assign(!u_gte_v, T0);
404 C.ct_cond_assign(!u_gte_v, T1);
405 D.ct_cond_assign(!u_gte_v, T2);
411 while(D.is_negative())
432 for(
size_t i = 0; i != BOTAN_MP_WORD_BITS; ++i)
434 const word bi = b % 2;
436 r += bi << (BOTAN_MP_WORD_BITS - 1);
467 const size_t exp_bits = exp.
bits();
471 const size_t powm_window = 4;
473 auto monty_mod = std::make_shared<Montgomery_Params>(mod, reduce_mod);
486 for(
size_t i = 0; i != exp_bits; ++i)
503 const size_t n = C.
bits();
504 const size_t m = (n + 1) / 2;
512 X = (X2 + C) / (2*X);
538 const size_t n_bits = n.
bits();
543 const uint16_t num =
static_cast<uint16_t
>(n.
word_at(0));
const size_t PRIME_TABLE_SIZE
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
word word_at(size_t n) const
bool is_lucas_probable_prime(const BigInt &C, const Modular_Reducer &mod_C)
size_t low_zero_bits(const BigInt &n)
BigInt gcd(const BigInt &a, const BigInt &b)
size_t miller_rabin_test_iterations(size_t n_bits, size_t prob, bool random)
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
BigInt inverse_euclid(const BigInt &n, const BigInt &mod)
secure_vector< word > & get_word_vector()
BigInt ct_inverse_mod_odd_modulus(const BigInt &n, const BigInt &mod)
BigInt normalized_montgomery_inverse(const BigInt &a, const BigInt &p)
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
void poison(const T *p, size_t n)
#define BOTAN_ASSERT(expr, assertion_made)
bool is_bailie_psw_probable_prime(const BigInt &n, const Modular_Reducer &mod_n)
static Mask< T > expand(T v)
word bigint_cnd_sub(word cnd, word x[], size_t x_size, const word y[], size_t y_size)
std::vector< T, secure_allocator< T >> secure_vector
CT::Mask< word > bigint_sub_abs(word z[], const word x[], const word y[], size_t N, word ws[])
BigInt lcm(const BigInt &a, const BigInt &b)
BigInt multiply(const BigInt &x, const BigInt &y) const
void ct_divide(const BigInt &x, const BigInt &y, BigInt &q_out, BigInt &r_out)
BigInt is_perfect_square(const BigInt &C)
void ct_cond_swap(bool predicate, BigInt &other)
void bigint_cnd_swap(word cnd, word x[], word y[], size_t size)
word bigint_cnd_add(word cnd, word x[], word x_size, const word y[], size_t y_size)
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
const word * data() const
std::shared_ptr< const Montgomery_Exponentation_State > monty_precompute(std::shared_ptr< const Montgomery_Params > params, const BigInt &g, size_t window_bits, bool const_time)
static BigInt power_of_2(size_t n)
BigInt reduce(const BigInt &x) const
void cond_flip_sign(bool predicate)
word monty_inverse(word a)
virtual bool is_seeded() const =0
size_t almost_montgomery_inverse(BigInt &result, const BigInt &a, const BigInt &p)
void grow_to(size_t n) const
bool is_miller_rabin_probable_prime(const BigInt &n, const Modular_Reducer &mod_n, RandomNumberGenerator &rng, size_t test_iterations)
void unpoison(const T *p, size_t n)
bool get_bit(size_t n) const
void bigint_cnd_abs(word cnd, word x[], size_t size)
void shrink_to_fit(size_t min_size=0)
BigInt monty_execute(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k, size_t max_k_bits)
BigInt square(const BigInt &x) const
void ct_cond_assign(bool predicate, const BigInt &other)