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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

95.45
/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) {
133✔
113
   return BOTAN_FFI_CHECKED_DELETE(mp);
133✔
114
}
115

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2025 Coveralls, Inc