Botan 3.10.0
Crypto and TLS for C&
ffi_mp.cpp File Reference
#include <botan/ffi.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/mem_utils.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,
const botan_mp_t x,
const botan_mp_t y )

Definition at line 148 of file ffi_mp.cpp.

148 {
149 return BOTAN_FFI_VISIT(result, [=](auto& res) {
150 if(result == x) {
151 res += safe_get(y);
152 } else {
153 res = safe_get(x) + safe_get(y);
154 }
155 });
156}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_add_u32()

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

Definition at line 168 of file ffi_mp.cpp.

168 {
169 return BOTAN_FFI_VISIT(result, [=](auto& res) {
170 if(result == x) {
171 res += static_cast<Botan::word>(y);
172 } else {
173 res = safe_get(x) + static_cast<Botan::word>(y);
174 }
175 });
176}
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
Definition types.h:119

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 282 of file ffi_mp.cpp.

282 {
283 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
284}

References BOTAN_FFI_VISIT.

◆ botan_mp_cmp()

int botan_mp_cmp ( int * result,
const botan_mp_t x_w,
const botan_mp_t y_w )

Definition at line 222 of file ffi_mp.cpp.

222 {
223 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
224}

References 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 144 of file ffi_mp.cpp.

144 {
145 return BOTAN_FFI_CHECKED_DELETE(mp);
146}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:185

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_mp_div()

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

Definition at line 198 of file ffi_mp.cpp.

198 {
199 return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
202 safe_get(remainder) = r;
203 });
204}
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
Definition divide.cpp:324

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

◆ botan_mp_equal()

int botan_mp_equal ( const botan_mp_t x_w,
const botan_mp_t y_w )

Definition at line 206 of file ffi_mp.cpp.

206 {
207 return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
208}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_flip_sign()

int botan_mp_flip_sign ( botan_mp_t mp)

Definition at line 76 of file ffi_mp.cpp.

76 {
77 return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
78}

References BOTAN_FFI_VISIT.

◆ botan_mp_from_bin()

int botan_mp_from_bin ( botan_mp_t mp,
const uint8_t bin[],
size_t bin_len )

Definition at line 80 of file ffi_mp.cpp.

80 {
81 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
82}

References BOTAN_FFI_VISIT.

◆ botan_mp_gcd()

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

Definition at line 266 of file ffi_mp.cpp.

266 {
267 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
268}
BigInt gcd(const BigInt &a, const BigInt &b)
Definition numthry.cpp:219

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 274 of file ffi_mp.cpp.

274 {
275 return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
276}

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_NULL_POINTER
Definition ffi.h:132
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95

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 ( const botan_mp_t mp)

Definition at line 218 of file ffi_mp.cpp.

218 {
219 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
220}

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 68 of file ffi_mp.cpp.

68 {
69 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_negative() ? 1 : 0; });
70}

References BOTAN_FFI_VISIT.

◆ botan_mp_is_odd()

int botan_mp_is_odd ( const botan_mp_t mp)

Definition at line 214 of file ffi_mp.cpp.

214 {
215 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
216}

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 72 of file ffi_mp.cpp.

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

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 270 of file ffi_mp.cpp.

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

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

◆ botan_mp_is_zero()

int botan_mp_is_zero ( const botan_mp_t mp)

Definition at line 210 of file ffi_mp.cpp.

210 {
211 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
212}

References BOTAN_FFI_VISIT.

◆ botan_mp_lshift()

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

Definition at line 236 of file ffi_mp.cpp.

236 {
237 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
238}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_mod_inverse()

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

Definition at line 244 of file ffi_mp.cpp.

244 {
245 return BOTAN_FFI_VISIT(out, [=](auto& o) {
247 });
248}
static BigInt zero()
Definition bigint.h:49
std::optional< BigInt > inverse_mod_general(const BigInt &x, const BigInt &mod)
Definition mod_inv.cpp:177

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 out,
const botan_mp_t x,
const botan_mp_t y,
const botan_mp_t modulus )

Definition at line 250 of file ffi_mp.cpp.

250 {
251 return BOTAN_FFI_VISIT(out, [=](auto& o) {
253 o = reducer.multiply(safe_get(x), safe_get(y));
254 });
255}
static Barrett_Reduction for_secret_modulus(const BigInt &m)
Definition barrett.cpp:22

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,
const botan_mp_t x,
const botan_mp_t y )

Definition at line 188 of file ffi_mp.cpp.

188 {
189 return BOTAN_FFI_VISIT(result, [=](auto& res) {
190 if(result == x) {
191 res *= safe_get(y);
192 } else {
193 res = safe_get(x) * safe_get(y);
194 }
195 });
196}

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 286 of file ffi_mp.cpp.

286 {
287 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
288}

References 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 290 of file ffi_mp.cpp.

290 {
291 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
292}

References BOTAN_FFI_VISIT.

◆ botan_mp_powmod()

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

Definition at line 231 of file ffi_mp.cpp.

231 {
232 return BOTAN_FFI_VISIT(
233 out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
234}
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition numthry.cpp:309

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 )

Definition at line 257 of file ffi_mp.cpp.

257 {
258 return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
259}

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,
const botan_mp_t lower,
const botan_mp_t upper )

Definition at line 261 of file ffi_mp.cpp.

261 {
262 return BOTAN_FFI_VISIT(
263 rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
264}
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:43

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

◆ botan_mp_rshift()

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

Definition at line 240 of file ffi_mp.cpp.

240 {
241 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
242}

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 278 of file ffi_mp.cpp.

278 {
279 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
280}

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:41

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 64 of file ffi_mp.cpp.

64 {
65 return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
66}

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 46 of file ffi_mp.cpp.

46 {
47 return BOTAN_FFI_VISIT(mp, [=](auto& bn) {
49 if(radix == 10) {
51 } else if(radix == 16) {
53 } else {
55 }
56
58 return BOTAN_FFI_SUCCESS;
59 });
60}
static BigInt decode(const uint8_t buf[], size_t length)
Definition bigint.h:857
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
std::span< const uint8_t > cstr_as_span_of_bytes(const char *s)
Definition mem_utils.h:41

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, Botan::cstr_as_span_of_bytes(), Botan::BigInt::Decimal, Botan::BigInt::decode(), and Botan::BigInt::Hexadecimal.

◆ 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 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
44}

References BOTAN_FFI_VISIT.

◆ botan_mp_sub()

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

Definition at line 158 of file ffi_mp.cpp.

158 {
159 return BOTAN_FFI_VISIT(result, [=](auto& res) {
160 if(result == x) {
161 res -= safe_get(y);
162 } else {
163 res = safe_get(x) - safe_get(y);
164 }
165 });
166}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_sub_u32()

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

Definition at line 178 of file ffi_mp.cpp.

178 {
179 return BOTAN_FFI_VISIT(result, [=](auto& res) {
180 if(result == x) {
181 res -= static_cast<Botan::word>(y);
182 } else {
183 res = safe_get(x) - static_cast<Botan::word>(y);
184 }
185 });
186}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_swap()

int botan_mp_swap ( botan_mp_t x_w,
botan_mp_t y_w )

Definition at line 226 of file ffi_mp.cpp.

226 {
227 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
228}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_to_bin()

int botan_mp_to_bin ( const botan_mp_t mp,
uint8_t vec[] )

Definition at line 126 of file ffi_mp.cpp.

126 {
127 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
128}

References 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 84 of file ffi_mp.cpp.

84 {
85 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
86 const std::string hex = bn.to_hex_string();
87
88 // Check that we are about to write no more than the documented upper bound
89 const size_t upper_bound = 2 * bn.bytes() + 5;
90 BOTAN_ASSERT_NOMSG(hex.size() + 1 <= upper_bound);
91 std::memcpy(out, hex.c_str(), 1 + hex.size());
92 });
93}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75

References BOTAN_ASSERT_NOMSG, 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 102 of file ffi_mp.cpp.

102 {
103 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
104 if(radix == 0 || radix == 10) {
105 return write_str_output(out, out_len, bn.to_dec_string());
106 } else if(radix == 16) {
107 return write_str_output(out, out_len, bn.to_hex_string());
108 } else {
110 }
111 });
112}
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:133
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:265

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

◆ botan_mp_to_uint32()

int botan_mp_to_uint32 ( const botan_mp_t mp,
uint32_t * val )

Definition at line 137 of file ffi_mp.cpp.

137 {
138 if(val == nullptr) {
140 }
141 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
142}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_mp_view_bin()

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

Definition at line 130 of file ffi_mp.cpp.

130 {
131 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
132 const auto bytes = bn.serialize();
133 return invoke_view_callback(view, ctx, bytes);
134 });
135}
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:187

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 95 of file ffi_mp.cpp.

95 {
96 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
97 const std::string hex = bn.to_hex_string();
98 return invoke_view_callback(view, ctx, hex);
99 });
100}

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 114 of file ffi_mp.cpp.

114 {
115 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
116 if(radix == 10) {
117 return invoke_view_callback(view, ctx, bn.to_dec_string());
118 } else if(radix == 16) {
119 return invoke_view_callback(view, ctx, bn.to_hex_string());
120 } else {
122 }
123 });
124}

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