10#include <botan/numthry.h>
11#include <botan/reducer.h>
12#include <botan/internal/divide.h>
13#include <botan/internal/ffi_mp.h>
14#include <botan/internal/ffi_rng.h>
15#include <botan/internal/ffi_util.h>
16#include <botan/internal/mod_inv.h>
24 if(mp_out ==
nullptr) {
28 auto mp = std::make_unique<Botan::BigInt>();
29 *mp_out =
new botan_mp_struct(std::move(mp));
51 }
else if(radix == 16) {
58 const size_t len = strlen(str);
70 return BOTAN_FFI_VISIT(mp, [](
const auto& bn) {
return bn.is_negative() ? 1 : 0; });
74 return BOTAN_FFI_VISIT(mp, [](
const auto& bn) {
return bn.is_positive() ? 1 : 0; });
82 return BOTAN_FFI_VISIT(mp, [=](
auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
87 const std::string hex = bn.to_hex_string();
88 std::memcpy(out, hex.c_str(), 1 + hex.size());
94 if(digit_base == 0 || digit_base == 10) {
96 }
else if(digit_base == 16) {
105 return BOTAN_FFI_VISIT(mp, [=](
const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
112 return BOTAN_FFI_VISIT(mp, [=](
const auto& bn) { *val = bn.to_u32bit(); });
142 res +=
static_cast<Botan::word
>(y);
144 res =
safe_get(x) +
static_cast<Botan::word
>(y);
152 res -=
static_cast<Botan::word
>(y);
154 res =
safe_get(x) -
static_cast<Botan::word
>(y);
182 return BOTAN_FFI_VISIT(mp, [](
const auto& bn) ->
int {
return bn.is_zero(); });
186 return BOTAN_FFI_VISIT(mp, [](
const auto& bn) ->
int {
return bn.is_odd(); });
190 return BOTAN_FFI_VISIT(mp, [](
const auto& bn) ->
int {
return bn.is_even(); });
246 return BOTAN_FFI_VISIT(mp, [=](
const auto& n) ->
int {
return n.get_bit(bit); });
262 return BOTAN_FFI_VISIT(mp, [=](
const auto& n) { *bytes = n.bytes(); });
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
static BigInt from_s32(int32_t n)
static Modular_Reducer for_secret_modulus(const BigInt &m)
struct botan_mp_struct * botan_mp_t
struct botan_rng_struct * botan_rng_t
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_NULL_POINTER
@ BOTAN_FFI_ERROR_BAD_PARAMETER
int botan_mp_set_from_int(botan_mp_t mp, int initial_value)
int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source)
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[])
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper)
int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y)
int botan_mp_num_bits(const botan_mp_t mp, size_t *bits)
int botan_mp_set_bit(botan_mp_t mp, size_t bit)
int botan_mp_num_bytes(const botan_mp_t mp, size_t *bytes)
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
int botan_mp_is_zero(const botan_mp_t mp)
int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y)
int botan_mp_is_even(const botan_mp_t mp)
int botan_mp_destroy(botan_mp_t mp)
int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus)
int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
int botan_mp_is_positive(const botan_mp_t mp)
int botan_mp_set_from_str(botan_mp_t mp, const char *str)
int botan_mp_init(botan_mp_t *mp_out)
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits)
int botan_mp_clear_bit(botan_mp_t mp, size_t bit)
int botan_mp_flip_sign(botan_mp_t mp)
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
int botan_mp_clear(botan_mp_t mp)
int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char *out, size_t *out_len)
int botan_mp_is_odd(const botan_mp_t mp)
int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y)
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift)
int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus)
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t *val)
int botan_mp_cmp(int *result, const botan_mp_t x_w, const botan_mp_t y_w)
int botan_mp_set_from_radix_str(botan_mp_t mp, const char *str, size_t radix)
int botan_mp_is_negative(const botan_mp_t mp)
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w)
int botan_mp_to_hex(const botan_mp_t mp, char *out)
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w)
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len)
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift)
int botan_mp_get_bit(const botan_mp_t mp, size_t bit)
#define BOTAN_FFI_VISIT(obj, lambda)
#define BOTAN_FFI_CHECKED_DELETE(o)
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
T & safe_get(botan_struct< T, M > *p)
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
std::optional< BigInt > inverse_mod_general(const BigInt &x, const BigInt &mod)
BigInt gcd(const BigInt &a, const BigInt &b)
const uint8_t * cast_char_ptr_to_uint8(const char *s)