8#include <botan/internal/divide.h>
10#include <botan/internal/bit_ops.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/mp_core.h>
21void sign_fixup(
const BigInt& x,
const BigInt& y, BigInt& q, BigInt& r) {
22 q.cond_flip_sign(x.sign() != y.sign());
24 if(x.is_negative() && r.is_nonzero()) {
30inline bool division_check(word q, word y2, word y1, word x3, word x2, word x1) {
40 const word x[3] = {x1, x2, x3};
41 const word y[3] = {y1, y2, y3};
56 const size_t x_bits = x.
bits();
62 for(
size_t i = 0; i != x_bits; ++i) {
63 const size_t b = x_bits - 1 - i;
75 sign_fixup(x, y, q, r);
86 const size_t x_bits = x.
bits();
91 for(
size_t i = 0; i != x_bits; ++i) {
92 const size_t b = x_bits - 1 - i;
102 r = r_gte_y.select(r - y, r);
124 const size_t x_bits = x.
bits();
129 for(
size_t i = 0; i != x_bits; ++i) {
130 const size_t b = x_bits - 1 - i;
160 const size_t y_words = y_arg.
sig_words();
180 const size_t t = y_words - 1;
181 const size_t n = std::max(y_words, r.
sig_words()) - 1;
194 const word y_t0 = y.
word_at(t);
195 const word y_t1 = y.
word_at(t - 1);
198 for(
size_t j = n; j != t; --j) {
199 const word x_j0 = r.
word_at(j);
200 const word x_j1 = r.
word_at(j - 1);
201 const word x_j2 = r.
word_at(j - 2);
208 qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
209 qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
216 r -= qjt * shifted_y;
218 r +=
static_cast<word
>(r.
is_negative()) * shifted_y;
220 q_words[j - t - 1] = qjt;
225 sign_fixup(x, y_arg, q, r);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
void conditionally_set_bit(size_t n, bool set_it)
void grow_to(size_t n) const
size_t top_bits_free() const
word word_at(size_t n) const
const word * _data() const
void ct_cond_swap(bool predicate, BigInt &other)
size_t reduce_below(const BigInt &mod, secure_vector< word > &ws)
bool get_bit(size_t n) const
static BigInt with_capacity(size_t n)
static constexpr Mask< T > is_gte(T x, T y)
static constexpr Mask< T > expand_top_bit(T v)
static constexpr Mask< T > is_equal(T x, T y)
#define BOTAN_MP_WORD_BITS
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
constexpr auto word_madd2(W a, W b, W *c) -> W
constexpr auto bigint_sub3(W z[], const W x[], size_t x_size, const W y[], size_t y_size) -> W
BigInt ct_modulo(const BigInt &x, const BigInt &y)
void ct_divide(const BigInt &x, const BigInt &y, BigInt &q_out, BigInt &r_out)
void ct_divide_word(const BigInt &x, word y, BigInt &q_out, word &r_out)
constexpr auto bigint_ct_is_lt(const W x[], size_t x_size, const W y[], size_t y_size, bool lt_or_equal=false) -> CT::Mask< W >
std::vector< T, secure_allocator< T > > secure_vector
constexpr auto bigint_divop_vartime(W n1, W n0, W d) -> W