Botan 3.14.1
Crypto and TLS for C&
ffi_mp.cpp File Reference
#include <botan/ffi.h>
#include <botan/assert.h>
#include <botan/numthry.h>
#include <botan/internal/barrett.h>
#include <botan/internal/divide.h>
#include <botan/internal/ffi_mp.h>
#include <botan/internal/ffi_rng.h>
#include <botan/internal/ffi_util.h>
#include <botan/internal/mod_inv.h>

Go to the source code of this file.

Functions

int botan_mp_add (botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
int botan_mp_add_u32 (botan_mp_t result, const botan_mp_t x, uint32_t y)
int botan_mp_clear (botan_mp_t mp)
int botan_mp_clear_bit (botan_mp_t mp, size_t bit)
int botan_mp_cmp (int *result, const botan_mp_t x_w, const botan_mp_t y_w)
int botan_mp_destroy (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_equal (const botan_mp_t x_w, const botan_mp_t y_w)
int botan_mp_flip_sign (botan_mp_t mp)
int botan_mp_from_bin (botan_mp_t mp, const uint8_t bin[], size_t bin_len)
int botan_mp_gcd (botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
int botan_mp_get_bit (const botan_mp_t mp, size_t bit)
int botan_mp_init (botan_mp_t *mp_out)
int botan_mp_is_even (const botan_mp_t mp)
int botan_mp_is_negative (const botan_mp_t mp)
int botan_mp_is_odd (const botan_mp_t mp)
int botan_mp_is_positive (const botan_mp_t mp)
int botan_mp_is_prime (const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
int botan_mp_is_zero (const botan_mp_t mp)
int botan_mp_lshift (botan_mp_t out, const botan_mp_t in, size_t shift)
int botan_mp_mod_inverse (botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
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_mul (botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
int botan_mp_num_bits (const botan_mp_t mp, size_t *bits)
int botan_mp_num_bytes (const botan_mp_t mp, size_t *bytes)
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_rand_bits (botan_mp_t rand_out, botan_rng_t rng, size_t bits)
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_rshift (botan_mp_t out, const botan_mp_t in, size_t shift)
int botan_mp_set_bit (botan_mp_t mp, size_t bit)
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_set_from_radix_str (botan_mp_t mp, const char *str, size_t radix)
int botan_mp_set_from_str (botan_mp_t mp, const char *str)
int botan_mp_sub (botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
int botan_mp_sub_u32 (botan_mp_t result, const botan_mp_t x, uint32_t y)
int botan_mp_swap (botan_mp_t x_w, botan_mp_t y_w)
int botan_mp_to_bin (const botan_mp_t mp, uint8_t vec[])
int botan_mp_to_hex (const botan_mp_t mp, char *out)
int botan_mp_to_str (const botan_mp_t mp, uint8_t radix, char *out, size_t *out_len)
int botan_mp_to_uint32 (const botan_mp_t mp, uint32_t *val)
int botan_mp_view_bin (const botan_mp_t mp, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_mp_view_hex (const botan_mp_t mp, botan_view_ctx ctx, botan_view_str_fn view)
int botan_mp_view_str (const botan_mp_t mp, uint8_t radix, botan_view_ctx ctx, botan_view_str_fn view)

Function Documentation

◆ botan_mp_add()

int botan_mp_add ( botan_mp_t result,
botan_mp_t x,
botan_mp_t y )

Set result to x + y

Parameters
resultset to the sum
xthe first addend
ythe second addend
Returns
0 on success, a negative value on failure

Definition at line 160 of file ffi_mp.cpp.

160 {
161 return BOTAN_FFI_VISIT(result, [=](auto& res) {
162 if(result == x) {
163 res += safe_get(y);
164 } else {
165 res = safe_get(x) + safe_get(y);
166 }
167 });
168}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:327
auto safe_get(T *p) -> typename T::element_type &
Definition ffi_util.h:191

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_add_u32()

int botan_mp_add_u32 ( botan_mp_t result,
botan_mp_t x,
uint32_t y )

Set result to x + y

Parameters
resultset to the sum
xthe first addend
ythe second addend
Returns
0 on success, a negative value on failure

Definition at line 180 of file ffi_mp.cpp.

180 {
181 return BOTAN_FFI_VISIT(result, [=](auto& res) {
182 if(result == x) {
183 res += static_cast<Botan::word>(y);
184 } else {
185 res = safe_get(x) + static_cast<Botan::word>(y);
186 }
187 });
188}
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_clear()

int botan_mp_clear ( botan_mp_t mp)

Set the MPI to zero

Definition at line 34 of file ffi_mp.cpp.

34 {
35 return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.clear(); });
36}

References BOTAN_FFI_VISIT.

◆ botan_mp_clear_bit()

int botan_mp_clear_bit ( botan_mp_t n,
size_t bit )

Clear the specified bit

Definition at line 297 of file ffi_mp.cpp.

297 {
298 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
299}

References BOTAN_FFI_VISIT.

◆ botan_mp_cmp()

int botan_mp_cmp ( int * result,
botan_mp_t x,
botan_mp_t y )

Compare two integers

Parameters
resultset to -1 if x < y, 0 if x == y, 1 if x > y
xthe first integer
ythe second integer
Returns
0 on success, a negative value on failure

Definition at line 234 of file ffi_mp.cpp.

234 {
235 if(result == nullptr) {
237 }
238 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
239}
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_destroy()

int botan_mp_destroy ( botan_mp_t mp)

Destroy (deallocate) an MPI

Returns
0 if success, error if invalid object handle

Definition at line 156 of file ffi_mp.cpp.

156 {
157 return BOTAN_FFI_CHECKED_DELETE(mp);
158}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:357

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_mp_div()

int botan_mp_div ( botan_mp_t quotient,
botan_mp_t remainder,
botan_mp_t x,
botan_mp_t y )

Divide x by y, producing both the quotient and the remainder

Parameters
quotientset to x / y
remainderset to x % y
xthe dividend
ythe divisor, which must not be zero
Returns
0 on success, a negative value on failure

Definition at line 210 of file ffi_mp.cpp.

210 {
211 return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
213 Botan::ct_divide(safe_get(x), safe_get(y), q, r);
214 safe_get(remainder) = r;
215 });
216}
void ct_divide(const BigInt &x, const BigInt &y, BigInt &q_out, BigInt &r_out)
Definition divide.cpp:180

References BOTAN_FFI_VISIT, Botan::ct_divide(), and Botan_FFI::safe_get().

◆ botan_mp_equal()

int botan_mp_equal ( botan_mp_t x,
botan_mp_t y )

Test if two integers are equal

Parameters
xthe first integer
ythe second integer
Returns
1 if x == y, 0 if x != y, or a negative value on error

Definition at line 218 of file ffi_mp.cpp.

218 {
219 return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
220}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_flip_sign()

int botan_mp_flip_sign ( botan_mp_t mp)

Negate the MPI, ie replace it by its additive inverse

Parameters
Big Integer (Low-Level)the integer to negate, modified in place
Returns
0 on success, a negative value on failure

Definition at line 79 of file ffi_mp.cpp.

79 {
80 return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
81}

References BOTAN_FFI_VISIT.

◆ botan_mp_from_bin()

int botan_mp_from_bin ( botan_mp_t mp,
const uint8_t vec[],
size_t vec_len )

Set an MP to the big-endian binary value

Parameters
Big Integer (Low-Level)the integer to set
vecthe big-endian encoding to decode
vec_lenlength of vec in bytes
Returns
0 on success, a negative value on failure

Definition at line 83 of file ffi_mp.cpp.

83 {
84 if(bin_len > 0 && bin == nullptr) {
86 }
87 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
88}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_mp_gcd()

int botan_mp_gcd ( botan_mp_t out,
botan_mp_t x,
botan_mp_t y )

Set out to the greatest common divisor of x and y

Parameters
outset to gcd(x, y)
xthe first integer
ythe second integer
Returns
0 on success, a negative value on failure

Definition at line 281 of file ffi_mp.cpp.

281 {
282 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
283}
BigInt gcd(const BigInt &a, const BigInt &b)
Definition numthry.cpp:220

References BOTAN_FFI_VISIT, Botan::gcd(), and Botan_FFI::safe_get().

◆ botan_mp_get_bit()

int botan_mp_get_bit ( botan_mp_t n,
size_t bit )

Returns 0 if specified bit of n is not set Returns 1 if specified bit of n is set Returns negative number on error

Definition at line 289 of file ffi_mp.cpp.

289 {
290 return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
291}

References BOTAN_FFI_VISIT.

◆ botan_mp_init()

int botan_mp_init ( botan_mp_t * mp)

Initialize an MPI

Definition at line 23 of file ffi_mp.cpp.

23 {
24 return ffi_guard_thunk(__func__, [=]() -> int {
25 if(mp_out == nullptr) {
27 }
28
29 auto mp = std::make_unique<Botan::BigInt>();
30 return ffi_new_object(mp_out, std::move(mp));
31 });
32}
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:347
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229

References BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_mp_is_even()

int botan_mp_is_even ( botan_mp_t mp)

Return 1 iff mp is even

Parameters
Big Integer (Low-Level)the integer to test
Returns
1 if even, 0 if odd, or a negative value on error

Definition at line 230 of file ffi_mp.cpp.

230 {
231 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
232}

References BOTAN_FFI_VISIT.

◆ botan_mp_is_negative()

int botan_mp_is_negative ( botan_mp_t mp)

Return 1 iff mp is less than 0

Definition at line 71 of file ffi_mp.cpp.

71 {
72 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.signum() < 0 ? 1 : 0; });
73}

References BOTAN_FFI_VISIT.

◆ botan_mp_is_odd()

int botan_mp_is_odd ( botan_mp_t mp)

Return 1 iff mp is odd

Parameters
Big Integer (Low-Level)the integer to test
Returns
1 if odd, 0 if even, or a negative value on error

Definition at line 226 of file ffi_mp.cpp.

226 {
227 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
228}

References BOTAN_FFI_VISIT.

◆ botan_mp_is_positive()

int botan_mp_is_positive ( botan_mp_t mp)

This function should have been named mp_is_non_negative. Returns 1 iff mp is greater than or equal to zero. Use botan_mp_is_negative to detect negative numbers, botan_mp_is_zero to check for zero.

Definition at line 75 of file ffi_mp.cpp.

75 {
76 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.signum() >= 0 ? 1 : 0; });
77}

References BOTAN_FFI_VISIT.

◆ botan_mp_is_prime()

int botan_mp_is_prime ( botan_mp_t n,
botan_rng_t rng,
size_t test_prob )

Returns 0 if n is not prime Returns 1 if n is prime Returns negative number on error

Definition at line 285 of file ffi_mp.cpp.

285 {
286 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
287}
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition numthry.cpp:449

References BOTAN_FFI_VISIT, Botan::is_prime(), and Botan_FFI::safe_get().

◆ botan_mp_is_zero()

int botan_mp_is_zero ( botan_mp_t mp)

Return 1 iff mp is equal to zero

Parameters
Big Integer (Low-Level)the integer to test
Returns
1 if zero, 0 if not, or a negative value on error

Definition at line 222 of file ffi_mp.cpp.

222 {
223 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
224}

References BOTAN_FFI_VISIT.

◆ botan_mp_lshift()

int botan_mp_lshift ( botan_mp_t out,
botan_mp_t in,
size_t shift )

Set out to in shifted left by the specified number of bits

Parameters
outset to the shifted value
inthe integer to shift
shiftthe number of bits to shift by
Returns
0 on success, a negative value on failure

Definition at line 251 of file ffi_mp.cpp.

251 {
252 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
253}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_mod_inverse()

int botan_mp_mod_inverse ( botan_mp_t out,
botan_mp_t in,
botan_mp_t modulus )

Set out to the inverse of in modulo the specified modulus. If no inverse exists, out is set to zero.

Parameters
outset to the modular inverse
inthe value to invert
modulusthe modulus
Returns
0 on success, a negative value on failure

Definition at line 259 of file ffi_mp.cpp.

259 {
260 return BOTAN_FFI_VISIT(out, [=](auto& o) {
262 });
263}
static BigInt zero()
Definition bigint.h:50
std::optional< BigInt > inverse_mod_general(const BigInt &x, const BigInt &mod)
Definition mod_inv.cpp:175

References BOTAN_FFI_VISIT, Botan::inverse_mod_general(), Botan_FFI::safe_get(), and Botan::BigInt::zero().

◆ botan_mp_mod_mul()

int botan_mp_mod_mul ( botan_mp_t result,
botan_mp_t x,
botan_mp_t y,
botan_mp_t mod )

Set result to (x * y) % mod

Parameters
resultset to the modular product
xthe first factor
ythe second factor
modthe modulus, which must be positive
Returns
0 on success, a negative value on failure

Definition at line 265 of file ffi_mp.cpp.

265 {
266 return BOTAN_FFI_VISIT(out, [=](auto& o) {
267 const auto reducer = Botan::Barrett_Reduction::for_secret_modulus(safe_get(modulus));
268 o = reducer.multiply(safe_get(x), safe_get(y));
269 });
270}
static Barrett_Reduction for_secret_modulus(const BigInt &m)
Definition barrett.cpp:23

References BOTAN_FFI_VISIT, Botan::Barrett_Reduction::for_secret_modulus(), and Botan_FFI::safe_get().

◆ botan_mp_mul()

int botan_mp_mul ( botan_mp_t result,
botan_mp_t x,
botan_mp_t y )

Set result to x * y

Parameters
resultset to the product
xthe first factor
ythe second factor
Returns
0 on success, a negative value on failure

Definition at line 200 of file ffi_mp.cpp.

200 {
201 return BOTAN_FFI_VISIT(result, [=](auto& res) {
202 if(result == x) {
203 res *= safe_get(y);
204 } else {
205 res = safe_get(x) * safe_get(y);
206 }
207 });
208}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_num_bits()

int botan_mp_num_bits ( botan_mp_t n,
size_t * bits )

Return the number of significant bits in the MPI

Definition at line 301 of file ffi_mp.cpp.

301 {
302 if(bits == nullptr) {
304 }
305 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
306}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_mp_num_bytes()

int botan_mp_num_bytes ( botan_mp_t n,
size_t * bytes )

Return the number of significant bytes in the MPI

Definition at line 308 of file ffi_mp.cpp.

308 {
309 if(bytes == nullptr) {
311 }
312 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
313}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_mp_powmod()

int botan_mp_powmod ( botan_mp_t out,
botan_mp_t base,
botan_mp_t exponent,
botan_mp_t modulus )

Set out to (base^exponent) % modulus

Parameters
outset to the result of the modular exponentiation
Base Typesthe base
exponentthe exponent
modulusthe modulus, which must be positive
Returns
0 on success, a negative value on failure

Definition at line 246 of file ffi_mp.cpp.

246 {
247 return BOTAN_FFI_VISIT(
248 out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
249}
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition numthry.cpp:310

References BOTAN_FFI_VISIT, Botan::power_mod(), and Botan_FFI::safe_get().

◆ botan_mp_rand_bits()

int botan_mp_rand_bits ( botan_mp_t rand_out,
botan_rng_t rng,
size_t bits )

Set rand_out to a random integer of the specified bit length

Parameters
rand_outset to the random integer
Random Number Generatorsa random number generator
bitsthe desired bit length
Returns
0 on success, a negative value on failure

Definition at line 272 of file ffi_mp.cpp.

272 {
273 return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
274}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_rand_range()

int botan_mp_rand_range ( botan_mp_t rand_out,
botan_rng_t rng,
botan_mp_t lower_bound,
botan_mp_t upper_bound )

Set rand_out to a random integer within the half-open range [lower_bound, upper_bound)

Parameters
rand_outset to the random integer
Random Number Generatorsa random number generator
lower_boundthe inclusive lower bound
upper_boundthe exclusive upper bound
Returns
0 on success, a negative value on failure

Definition at line 276 of file ffi_mp.cpp.

276 {
277 return BOTAN_FFI_VISIT(
278 rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
279}
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:45

References BOTAN_FFI_VISIT, Botan::BigInt::random_integer(), and Botan_FFI::safe_get().

◆ botan_mp_rshift()

int botan_mp_rshift ( botan_mp_t out,
botan_mp_t in,
size_t shift )

Set out to in shifted right by the specified number of bits

Parameters
outset to the shifted value
inthe integer to shift
shiftthe number of bits to shift by
Returns
0 on success, a negative value on failure

Definition at line 255 of file ffi_mp.cpp.

255 {
256 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
257}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_set_bit()

int botan_mp_set_bit ( botan_mp_t n,
size_t bit )

Set the specified bit

Definition at line 293 of file ffi_mp.cpp.

293 {
294 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
295}

References BOTAN_FFI_VISIT.

◆ botan_mp_set_from_int()

int botan_mp_set_from_int ( botan_mp_t mp,
int initial_value )

Set the MPI value from an int

Definition at line 38 of file ffi_mp.cpp.

38 {
39 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt::from_s32(initial_value); });
40}
static BigInt from_s32(int32_t n)
Definition bigint.cpp:43

References BOTAN_FFI_VISIT, and Botan::BigInt::from_s32().

◆ botan_mp_set_from_mp()

int botan_mp_set_from_mp ( botan_mp_t dest,
botan_mp_t source )

Set the MPI value from another MP object

Definition at line 67 of file ffi_mp.cpp.

67 {
68 return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
69}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_set_from_radix_str()

int botan_mp_set_from_radix_str ( botan_mp_t dest,
const char * str,
size_t radix )

Set the MPI value from a string with arbitrary radix. For arbitrary being 10 or 16.

Definition at line 50 of file ffi_mp.cpp.

50 {
51 if(str == nullptr) {
53 }
54
55 return BOTAN_FFI_VISIT(mp, [=](auto& bn) {
56 if(radix != 10 && radix != 16) {
58 }
59
60 bn = Botan::BigInt::from_radix_digits(std::string_view(str), radix);
61 return BOTAN_FFI_SUCCESS;
62 });
63}
static BigInt from_radix_digits(std::string_view digits, size_t radix)
Definition big_code.cpp:126
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, and Botan::BigInt::from_radix_digits().

◆ botan_mp_set_from_str()

int botan_mp_set_from_str ( botan_mp_t dest,
const char * str )

Set the MPI value from a string

Definition at line 42 of file ffi_mp.cpp.

42 {
43 if(str == nullptr) {
45 }
46
47 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
48}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_mp_sub()

int botan_mp_sub ( botan_mp_t result,
botan_mp_t x,
botan_mp_t y )

Set result to x - y

Parameters
resultset to the difference
xthe minuend
ythe subtrahend
Returns
0 on success, a negative value on failure

Definition at line 170 of file ffi_mp.cpp.

170 {
171 return BOTAN_FFI_VISIT(result, [=](auto& res) {
172 if(result == x) {
173 res -= safe_get(y);
174 } else {
175 res = safe_get(x) - safe_get(y);
176 }
177 });
178}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_sub_u32()

int botan_mp_sub_u32 ( botan_mp_t result,
botan_mp_t x,
uint32_t y )

Set result to x - y

Parameters
resultset to the difference
xthe minuend
ythe subtrahend
Returns
0 on success, a negative value on failure

Definition at line 190 of file ffi_mp.cpp.

190 {
191 return BOTAN_FFI_VISIT(result, [=](auto& res) {
192 if(result == x) {
193 res -= static_cast<Botan::word>(y);
194 } else {
195 res = safe_get(x) - static_cast<Botan::word>(y);
196 }
197 });
198}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_swap()

int botan_mp_swap ( botan_mp_t x,
botan_mp_t y )

Swap the values of two integers

Parameters
xthe first integer
ythe second integer
Returns
0 on success, a negative value on failure

Definition at line 241 of file ffi_mp.cpp.

241 {
242 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
243}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_to_bin()

int botan_mp_to_bin ( botan_mp_t mp,
uint8_t vec[] )

Convert the MPI to a big-endian binary string. Writes botan_mp_num_bytes to vec

Note that the sign of the integer is ignored here; only the absolute value is copied

Parameters
Big Integer (Low-Level)the integer to encode
vecoutput buffer of at least botan_mp_num_bytes(mp) bytes
Returns
0 on success, a negative value on failure

Definition at line 135 of file ffi_mp.cpp.

135 {
136 if(vec == nullptr) {
138 }
139 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
140}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_mp_to_hex()

int botan_mp_to_hex ( botan_mp_t mp,
char * out )

Convert the MPI to a hex string. Writes up to botan_mp_num_bytes(mp)*2 + 5 bytes

Prefer botan_mp_view_hex

Definition at line 90 of file ffi_mp.cpp.

90 {
91 if(out == nullptr) {
93 }
94 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
95 const std::string hex = bn.to_hex_string();
96
97 // Check that we are about to write no more than the documented upper bound
98 const size_t upper_bound = 2 * bn.bytes() + 5;
99 BOTAN_ASSERT_NOMSG(hex.size() + 1 <= upper_bound);
100 std::memcpy(out, hex.c_str(), 1 + hex.size());
101 });
102}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82

References BOTAN_ASSERT_NOMSG, BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_mp_to_str()

int botan_mp_to_str ( botan_mp_t mp,
uint8_t radix,
char * out,
size_t * out_len )

Convert the MPI to a string. Currently radix == 10 and radix == 16 are supported.

Definition at line 111 of file ffi_mp.cpp.

111 {
112 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
113 if(radix == 0 || radix == 10) {
114 return write_str_output(out, out_len, bn.to_dec_string());
115 } else if(radix == 16) {
116 return write_str_output(out, out_len, bn.to_hex_string());
117 } else {
119 }
120 });
121}
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:132
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:440

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_VISIT, and Botan_FFI::write_str_output().

◆ botan_mp_to_uint32()

int botan_mp_to_uint32 ( botan_mp_t mp,
uint32_t * val )

Convert the MPI to a uint32_t, if possible. Fails if MPI is negative or too large.

Parameters
Big Integer (Low-Level)the integer to convert
valset to the value of mp
Returns
0 on success, a negative value on failure

Definition at line 149 of file ffi_mp.cpp.

149 {
150 if(val == nullptr) {
152 }
153 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
154}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_mp_view_bin()

int botan_mp_view_bin ( botan_mp_t mp,
botan_view_ctx ctx,
botan_view_bin_fn view )

View the big-endian binary string encoding of this integer

Note that the sign of the integer is ignored here; only the absolute value is viewed

Parameters
Big Integer (Low-Level)the integer to encode
ctxan application context passed to the view function
viewthe view callback which receives the encoding
Returns
0 on success, a negative value on failure

Definition at line 142 of file ffi_mp.cpp.

142 {
143 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
144 const auto bytes = bn.serialize();
145 return invoke_view_callback(view, ctx, bytes);
146 });
147}
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:359

References BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().

◆ botan_mp_view_hex()

int botan_mp_view_hex ( botan_mp_t mp,
botan_view_ctx ctx,
botan_view_str_fn view )

View the hex string encoding of the MPI.

Definition at line 104 of file ffi_mp.cpp.

104 {
105 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
106 const std::string hex = bn.to_hex_string();
107 return invoke_view_callback(view, ctx, hex);
108 });
109}

References BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().

◆ botan_mp_view_str()

int botan_mp_view_str ( botan_mp_t mp,
uint8_t radix,
botan_view_ctx ctx,
botan_view_str_fn view )

View the MPI as a radix-N integer. Currently only radix 10 and radix 16 are supported

Definition at line 123 of file ffi_mp.cpp.

123 {
124 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
125 if(radix == 10) {
126 return invoke_view_callback(view, ctx, bn.to_dec_string());
127 } else if(radix == 16) {
128 return invoke_view_callback(view, ctx, bn.to_hex_string());
129 } else {
131 }
132 });
133}

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().