• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.4
/src/lib/ffi/ffi_mp.cpp
1
/*
2
* (C) 2015,2017 Jack Lloyd
3
* (C) 2017 Ribose Inc
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/ffi.h>
9

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

17
extern "C" {
18

19
using namespace Botan_FFI;
20

21
int botan_mp_init(botan_mp_t* mp_out) {
133✔
22
   return ffi_guard_thunk(__func__, [=]() -> int {
133✔
23
      if(mp_out == nullptr)
133✔
24
         return BOTAN_FFI_ERROR_NULL_POINTER;
25

26
      auto mp = std::make_unique<Botan::BigInt>();
133✔
27
      *mp_out = new botan_mp_struct(std::move(mp));
133✔
28
      return BOTAN_FFI_SUCCESS;
133✔
29
   });
266✔
30
}
31

32
int botan_mp_clear(botan_mp_t mp) {
×
33
   return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.clear(); });
×
34
}
35

36
int botan_mp_set_from_int(botan_mp_t mp, int initial_value) {
2✔
37
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt::from_s32(initial_value); });
4✔
38
}
39

40
int botan_mp_set_from_str(botan_mp_t mp, const char* str) {
29✔
41
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
58✔
42
}
43

44
int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix) {
4✔
45
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) {
10✔
46
      Botan::BigInt::Base base;
47
      if(radix == 10)
48
         base = Botan::BigInt::Decimal;
49
      else if(radix == 16)
50
         base = Botan::BigInt::Hexadecimal;
51
      else
52
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
53

54
      const uint8_t* bytes = Botan::cast_char_ptr_to_uint8(str);
55
      const size_t len = strlen(str);
56

57
      bn = Botan::BigInt(bytes, len, base);
58
      return BOTAN_FFI_SUCCESS;
59
   });
60
}
61

62
int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source) {
4✔
63
   return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
8✔
64
}
65

66
int botan_mp_is_negative(const botan_mp_t mp) {
10✔
67
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_negative() ? 1 : 0; });
20✔
68
}
69

70
int botan_mp_is_positive(const botan_mp_t mp) {
6✔
71
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_positive() ? 1 : 0; });
12✔
72
}
73

74
int botan_mp_flip_sign(botan_mp_t mp) {
5✔
75
   return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
10✔
76
}
77

78
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len) {
1✔
79
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn.binary_decode(bin, bin_len); });
2✔
80
}
81

82
int botan_mp_to_hex(const botan_mp_t mp, char* out) {
38✔
83
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
76✔
84
      const std::string hex = bn.to_hex_string();
85
      std::memcpy(out, hex.c_str(), 1 + hex.size());
86
   });
87
}
88

89
int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char* out, size_t* out_len) {
14✔
90
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
33✔
91
      if(digit_base == 0 || digit_base == 10)
92
         return write_str_output(out, out_len, bn.to_dec_string());
93
      else if(digit_base == 16)
94
         return write_str_output(out, out_len, bn.to_hex_string());
95
      else
96
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
97
   });
98
}
99

100
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[]) {
3✔
101
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.binary_encode(vec); });
6✔
102
}
103

104
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val) {
5✔
105
   if(val == nullptr) {
5✔
106
      return BOTAN_FFI_ERROR_NULL_POINTER;
107
   }
108
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
10✔
109
}
110

111
int botan_mp_destroy(botan_mp_t mp) { return BOTAN_FFI_CHECKED_DELETE(mp); }
133✔
112

113
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
5✔
114
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
10✔
115
      if(result == x)
116
         res += safe_get(y);
117
      else
118
         res = safe_get(x) + safe_get(y);
119
   });
120
}
121

122
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
1✔
123
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
3✔
124
      if(result == x)
125
         res -= safe_get(y);
126
      else
127
         res = safe_get(x) - safe_get(y);
128
   });
129
}
130

131
int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
2✔
132
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
4✔
133
      if(result == x)
134
         res += static_cast<Botan::word>(y);
135
      else
136
         res = safe_get(x) + static_cast<Botan::word>(y);
137
   });
138
}
139

140
int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
1✔
141
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
2✔
142
      if(result == x)
143
         res -= static_cast<Botan::word>(y);
144
      else
145
         res = safe_get(x) - static_cast<Botan::word>(y);
146
   });
147
}
148

149
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
6✔
150
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
13✔
151
      if(result == x)
152
         res *= safe_get(y);
153
      else
154
         res = safe_get(x) * safe_get(y);
155
   });
156
}
157

158
int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y) {
2✔
159
   return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
6✔
160
      Botan::BigInt r;
161
      Botan::vartime_divide(safe_get(x), safe_get(y), q, r);
162
      safe_get(remainder) = r;
163
   });
164
}
165

166
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w) {
10✔
167
   return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
20✔
168
}
169

170
int botan_mp_is_zero(const botan_mp_t mp) {
3✔
171
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
6✔
172
}
173

174
int botan_mp_is_odd(const botan_mp_t mp) {
2✔
175
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
4✔
176
}
177

178
int botan_mp_is_even(const botan_mp_t mp) {
2✔
179
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
4✔
180
}
181

182
int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w) {
38✔
183
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
76✔
184
}
185

186
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w) {
×
187
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
×
188
}
189

190
// Return (base^exponent) % modulus
191
int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus) {
2✔
192
   return BOTAN_FFI_VISIT(
4✔
193
      out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
194
}
195

196
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
2✔
197
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
4✔
198
}
199

200
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
4✔
201
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
8✔
202
}
203

204
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus) {
2✔
205
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::inverse_mod(safe_get(in), safe_get(modulus)); });
4✔
206
}
207

208
int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus) {
2✔
209
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
5✔
210
      Botan::Modular_Reducer reducer(safe_get(modulus));
211
      o = reducer.multiply(safe_get(x), safe_get(y));
212
   });
213
}
214

215
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits) {
2✔
216
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
4✔
217
}
218

219
int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper) {
10✔
220
   return BOTAN_FFI_VISIT(
30✔
221
      rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
222
}
223

224
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y) {
4✔
225
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
8✔
226
}
227

228
int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob) {
5✔
229
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
10✔
230
}
231

232
int botan_mp_get_bit(const botan_mp_t mp, size_t bit) {
6✔
233
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
12✔
234
}
235

236
int botan_mp_set_bit(botan_mp_t mp, size_t bit) {
2✔
237
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
4✔
238
}
239

240
int botan_mp_clear_bit(botan_mp_t mp, size_t bit) {
2✔
241
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
4✔
242
}
243

244
int botan_mp_num_bits(const botan_mp_t mp, size_t* bits) {
7✔
245
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
14✔
246
}
247

248
int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) {
45✔
249
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
90✔
250
}
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc