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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

209
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✔
210
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
5✔
211
      Botan::Modular_Reducer reducer(safe_get(modulus));
212
      o = reducer.multiply(safe_get(x), safe_get(y));
213
   });
214
}
215

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

220
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✔
221
   return BOTAN_FFI_VISIT(
30✔
222
      rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
223
}
224

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

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

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

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

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

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

249
int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) {
45✔
250
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
90✔
251
}
252
}
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