8#include <botan/divide.h>
9#include <botan/internal/mp_core.h>
10#include <botan/internal/mp_madd.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/bit_ops.h>
21void sign_fixup(
const BigInt& x,
const BigInt& y, BigInt& q, BigInt& r)
23 q.cond_flip_sign(x.sign() != y.sign());
25 if(x.is_negative() && r.is_nonzero())
32inline bool division_check(word q, word y2, word y1,
33 word x3, word x2, word x1)
44 const word x[3] = { x1, x2, x3 };
45 const word y[3] = { y1, y2, y3 };
57 const size_t x_bits = x.
bits();
63 for(
size_t i = 0; i != x_bits; ++i)
65 const size_t b = x_bits - 1 - i;
77 sign_fixup(x, y, q, r);
85 const size_t x_bits = x.
bits();
90 for(
size_t i = 0; i != x_bits; ++i)
92 const size_t b = x_bits - 1 - i;
101 r = r_gte_y.select(r - y, r);
114 r_out =
static_cast<uint8_t
>(r);
125 const size_t x_bits = x.
bits();
130 for(
size_t i = 0; i != x_bits; ++i)
132 const size_t b = x_bits - 1 - i;
164 const size_t y_words = y_arg.
sig_words();
184 const size_t t = y_words - 1;
185 const size_t n = std::max(y_words, r.
sig_words()) - 1;
193 BigInt shifted_y = y << (BOTAN_MP_WORD_BITS * (n-t));
198 const word y_t0 = y.
word_at(t);
199 const word y_t1 = y.
word_at(t-1);
202 for(
size_t j = n; j != t; --j)
204 const word x_j0 = r.
word_at(j);
205 const word x_j1 = r.
word_at(j-1);
206 const word x_j2 = r.
word_at(j-2);
213 qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
214 qjt -= division_check(qjt, y_t0, y_t1, x_j0, x_j1, x_j2);
217 shifted_y >>= BOTAN_MP_WORD_BITS;
221 r -= qjt * shifted_y;
223 r +=
static_cast<word
>(r.
is_negative()) * shifted_y;
225 q_words[j-t-1] = qjt;
230 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
const word * data() const
word word_at(size_t n) 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 Mask< T > is_equal(T x, T y)
static Mask< T > is_gte(T x, T y)
word bigint_divop(word n1, word n0, word d)
void ct_divide_u8(const BigInt &x, uint8_t y, BigInt &q_out, uint8_t &r_out)
CT::Mask< word > bigint_ct_is_lt(const word x[], size_t x_size, const word y[], size_t y_size, bool lt_or_equal=false)
void ct_divide(const BigInt &x, const BigInt &y, BigInt &q_out, BigInt &r_out)
word word_madd2(word a, word b, word *c)
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
std::vector< T, secure_allocator< T > > secure_vector
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
BigInt ct_modulo(const BigInt &x, const BigInt &y)