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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

96.12
/src/tests/test_ecc_pointmul.cpp
1
/*
2
* (C) 2014,2015,2019 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_ECC_GROUP)
10
   #include <botan/bigint.h>
11
   #include <botan/ec_group.h>
12
   #include <botan/rng.h>
13
   #include <botan/internal/fmt.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
namespace {
19

20
#if defined(BOTAN_HAS_ECC_GROUP)
21

22
class ECC_Basepoint_Mul_Tests final : public Text_Based_Test {
×
23
   public:
24
      ECC_Basepoint_Mul_Tests() : Text_Based_Test("pubkey/ecc_base_point_mul.vec", "k,P") {}
2✔
25

26
      bool skip_this_test(const std::string& group_id, const VarMap& /*vars*/) override {
535✔
27
         return !Botan::EC_Group::supports_named_group(group_id);
535✔
28
      }
29

30
      Test::Result run_one_test(const std::string& group_id, const VarMap& vars) override {
535✔
31
         Test::Result result("ECC base point multiply " + group_id);
535✔
32

33
         const auto k_bytes = vars.get_req_bin("k");
535✔
34
         const auto P_bytes = vars.get_req_bin("P");
535✔
35

36
         const auto group = Botan::EC_Group::from_name(group_id);
535✔
37

38
         const Botan::BigInt k(k_bytes);
535✔
39

40
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
41
         const auto pt = group.OS2ECP(P_bytes);
535✔
42
         const Botan::EC_Point p1 = group.get_base_point() * k;
535✔
43
         result.test_eq("EC_Point Montgomery ladder", p1.encode(Botan::EC_Point_Format::Uncompressed), P_bytes);
1,070✔
44
   #endif
45

46
         const auto scalar = Botan::EC_Scalar::from_bigint(group, k);
535✔
47
         const auto apg = Botan::EC_AffinePoint::g_mul(scalar, this->rng());
535✔
48
         result.test_eq("AffinePoint::g_mul", apg.serialize_uncompressed(), P_bytes);
1,070✔
49

50
         const auto ag = Botan::EC_AffinePoint::generator(group);
535✔
51
         const auto ap = ag.mul(scalar, this->rng());
535✔
52
         result.test_eq("AffinePoint::mul", ap.serialize_uncompressed(), P_bytes);
1,070✔
53

54
         return result;
1,070✔
55
      }
1,605✔
56
};
57

58
BOTAN_REGISTER_TEST("pubkey", "ecc_basemul", ECC_Basepoint_Mul_Tests);
59

60
class ECC_Varpoint_Mul_Tests final : public Text_Based_Test {
×
61
   public:
62
      ECC_Varpoint_Mul_Tests() : Text_Based_Test("pubkey/ecc_var_point_mul.vec", "P,k,Z") {}
2✔
63

64
      bool skip_this_test(const std::string& group_id, const VarMap& /*vars*/) override {
829✔
65
         return !Botan::EC_Group::supports_named_group(group_id);
829✔
66
      }
67

68
      Test::Result run_one_test(const std::string& group_id, const VarMap& vars) override {
829✔
69
         Test::Result result("ECC var point multiply " + group_id);
829✔
70

71
         const auto p = vars.get_req_bin("P");
829✔
72
         const Botan::BigInt k = vars.get_req_bn("k");
829✔
73
         const auto z = vars.get_req_bin("Z");
829✔
74

75
         const auto group = Botan::EC_Group::from_name(group_id);
829✔
76

77
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
78
         const Botan::EC_Point p1 = group.OS2ECP(p) * k;
1,658✔
79
         result.test_eq("EC_Point Montgomery ladder", p1.encode(Botan::EC_Point::Compressed), z);
1,658✔
80
   #endif
81

82
         const auto s_k = Botan::EC_Scalar::from_bigint(group, k);
829✔
83
         const auto apt = Botan::EC_AffinePoint::deserialize(group, p).value();
1,658✔
84
         const auto apt_k = apt.mul(s_k, this->rng());
829✔
85
         result.test_eq("p * k (AffinePoint)", apt_k.serialize_compressed(), z);
1,658✔
86

87
         const auto apt_k_neg = apt.negate().mul(s_k.negate(), this->rng());
829✔
88
         result.test_eq("-p * -k (AffinePoint)", apt_k_neg.serialize_compressed(), z);
1,658✔
89

90
         const auto neg_apt_neg_k = apt.mul(s_k.negate(), this->rng()).negate();
829✔
91
         result.test_eq("-(p * -k) (AffinePoint)", neg_apt_neg_k.serialize_compressed(), z);
1,658✔
92

93
         return result;
1,658✔
94
      }
2,487✔
95
};
96

97
BOTAN_REGISTER_TEST("pubkey", "ecc_varmul", ECC_Varpoint_Mul_Tests);
98

99
class ECC_Mul2_Tests final : public Text_Based_Test {
×
100
   public:
101
      ECC_Mul2_Tests() : Text_Based_Test("pubkey/ecc_var_point_mul2.vec", "P,x,Q,y,Z") {}
2✔
102

103
      bool skip_this_test(const std::string& group_id, const VarMap& /*vars*/) override {
819✔
104
         return !Botan::EC_Group::supports_named_group(group_id);
819✔
105
      }
106

107
      Test::Result run_one_test(const std::string& group_id, const VarMap& vars) override {
819✔
108
         Test::Result result("ECC mul2 " + group_id);
819✔
109

110
         const auto Z_bytes = vars.get_req_bin("Z");
819✔
111

112
         const auto check_px_qy = [&](const char* what,
4,095✔
113
                                      const Botan::EC_AffinePoint& p,
114
                                      const Botan::EC_Scalar& x,
115
                                      const Botan::EC_AffinePoint& q,
116
                                      const Botan::EC_Scalar& y,
117
                                      bool with_final_negation = false) {
118
            if(const auto z = Botan::EC_AffinePoint::mul_px_qy(p, x, q, y, rng())) {
3,276✔
119
               if(with_final_negation) {
3,276✔
120
                  result.test_eq(what, z->negate().serialize_compressed(), Z_bytes);
4,914✔
121
               } else {
122
                  result.test_eq(what, z->serialize_compressed(), Z_bytes);
4,914✔
123
               }
124
            } else {
125
               result.test_failure("EC_AffinePoint::mul_px_qy failed to produce a result");
×
126
            }
×
127

128
            // Now check the same using naive multiply and add:
129
            auto z = p.mul(x, rng()).add(q.mul(y, rng()));
3,276✔
130
            if(with_final_negation) {
3,276✔
131
               z = z.negate();
1,638✔
132
            }
133
            result.test_eq("p*x + q*y naive", z.serialize_compressed(), Z_bytes);
6,552✔
134
         };
3,276✔
135

136
         const auto group = Botan::EC_Group::from_name(group_id);
819✔
137

138
         const auto p = Botan::EC_AffinePoint::deserialize(group, vars.get_req_bin("P")).value();
3,276✔
139
         const auto q = Botan::EC_AffinePoint::deserialize(group, vars.get_req_bin("Q")).value();
3,276✔
140
         const auto x = Botan::EC_Scalar::from_bigint(group, vars.get_req_bn("x"));
819✔
141
         const auto y = Botan::EC_Scalar::from_bigint(group, vars.get_req_bn("y"));
819✔
142

143
         const auto np = p.negate();
819✔
144
         const auto nq = q.negate();
819✔
145
         const auto nx = x.negate();
819✔
146
         const auto ny = y.negate();
819✔
147

148
         check_px_qy("p*x + q*y", p, x, q, y);
819✔
149
         check_px_qy("-p*-x + -q*-y", np, nx, nq, ny);
819✔
150
         check_px_qy("-(p*-x + q*-y)", p, nx, q, ny, true);
819✔
151
         check_px_qy("-(-p*x + -q*y)", np, x, nq, y, true);
819✔
152

153
         return result;
1,638✔
154
      }
1,638✔
155
};
156

157
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2", ECC_Mul2_Tests);
158

159
class ECC_Mul2_Inf_Tests final : public Test {
1✔
160
   public:
161
      std::vector<Test::Result> run() override {
1✔
162
         std::vector<Test::Result> results;
1✔
163

164
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
165
            Test::Result result("ECC mul2 inf " + group_id);
28✔
166

167
            const auto check_px_qy = [&](const char* what,
224✔
168
                                         const Botan::EC_AffinePoint& p,
169
                                         const Botan::EC_Scalar& x,
170
                                         const Botan::EC_AffinePoint& q,
171
                                         const Botan::EC_Scalar& y) {
172
               if(const auto z = Botan::EC_AffinePoint::mul_px_qy(p, x, q, y, rng())) {
196✔
173
                  result.test_failure(Botan::fmt("EC_AffinePoint::mul_px_qy {} unexpectedly produced a result", what));
×
174
               } else {
175
                  result.test_success(Botan::fmt("EC_AffinePoint::mul_px_qy {} returned nullopt as expected", what));
392✔
176
               }
196✔
177
            };
196✔
178

179
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
180

181
            const auto g = Botan::EC_AffinePoint::generator(group);
28✔
182

183
            // Choose some other random point z
184
            const auto z = g.mul(Botan::EC_Scalar::random(group, rng()), rng());
28✔
185

186
            const auto r = Botan::EC_Scalar::random(group, rng());
28✔
187
            const auto neg_r = r.negate();
28✔
188
            const auto neg_r2 = neg_r + neg_r;
28✔
189

190
            const auto zero = r - r;  // NOLINT(*-redundant-expression)
28✔
191
            result.confirm("Computed EC_Scalar is zero", zero.is_zero());
56✔
192

193
            const auto g2 = g.add(g);
28✔
194

195
            const auto id = Botan::EC_AffinePoint::identity(group);
28✔
196

197
            check_px_qy("0*g + r*id", g, zero, id, r);
28✔
198
            check_px_qy("0*id + r*id", id, zero, id, r);
28✔
199
            check_px_qy("0*g + 0*z", g, zero, z, zero);
28✔
200
            check_px_qy("r*g + -r*g", g, r, g, neg_r);
28✔
201
            check_px_qy("-r*g + r*g", g, neg_r, g, r);
28✔
202
            check_px_qy("r*g + r*-g", g, r, g.negate(), r);
28✔
203
            check_px_qy("r*g2 + -r2*g", g2, r, g, neg_r2);
28✔
204

205
            // Test 'zeroization' (explicit erasure of the scalar content)
206
            auto r2 = Botan::EC_Scalar::random(group, rng());
28✔
207
            result.confirm("random value is not zero", !r2.is_zero());
56✔
208
            r2.zeroize();
28✔
209
            result.confirm("value is zero", r2.is_zero());
56✔
210

211
            results.push_back(result);
28✔
212
         }
28✔
213

214
         return results;
1✔
215
      }
×
216
};
217

218
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2_inf", ECC_Mul2_Inf_Tests);
219

220
class ECC_Point_Addition_Tests final : public Test {
1✔
221
   public:
222
      std::vector<Test::Result> run() override {
1✔
223
         std::vector<Test::Result> results;
1✔
224

225
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
226
            Test::Result result("ECC addition " + group_id);
28✔
227

228
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
229

230
            const auto g = Botan::EC_AffinePoint::generator(group);
28✔
231
            result.test_eq("g is not the identity element", g.is_identity(), false);
28✔
232

233
            // Choose some other random point z
234
            const auto z = g.mul(Botan::EC_Scalar::random(group, rng()), rng());
28✔
235
            result.test_eq("z is not the identity element", z.is_identity(), false);
28✔
236

237
            const auto id = Botan::EC_AffinePoint::identity(group);
28✔
238
            result.test_eq("id is the identity element", id.is_identity(), true);
28✔
239

240
            const auto g_bytes = g.serialize_uncompressed();
28✔
241

242
            auto check_expr_is_g = [&](const char* msg, const Botan::EC_AffinePoint& pt) {
308✔
243
               result.test_eq(Botan::fmt("{} is g", msg), pt.serialize_uncompressed(), g_bytes);
560✔
244
            };
280✔
245

246
            const auto nz = z.negate();
28✔
247

248
            check_expr_is_g("g + id", g.add(id));
28✔
249
            check_expr_is_g("id + g", id.add(g));
28✔
250
            check_expr_is_g("g + id", g.add(id));
28✔
251
            check_expr_is_g("g + -id", g.add(id.negate()));
28✔
252
            check_expr_is_g("g + g + -g", g.add(g).add(g.negate()));
28✔
253
            check_expr_is_g("-id + g", id.negate().add(g));
28✔
254
            check_expr_is_g("z + g - z", z.add(g).add(nz));
28✔
255
            check_expr_is_g("z - z + g", z.add(nz).add(g));
28✔
256
            check_expr_is_g("z + z + g - z - z", z.add(z).add(g).add(nz).add(nz));
28✔
257
            check_expr_is_g("z + id + g + z - z - z", z.add(id).add(g).add(z).add(nz).add(nz));
28✔
258

259
            results.push_back(result);
28✔
260
         }
56✔
261

262
         return results;
1✔
263
      }
×
264
};
265

266
BOTAN_REGISTER_TEST("pubkey", "ecc_pt_addition", ECC_Point_Addition_Tests);
267

268
class ECC_Scalar_Arithmetic_Tests final : public Test {
1✔
269
   public:
270
      std::vector<Test::Result> run() override {
1✔
271
         std::vector<Test::Result> results;
1✔
272

273
         auto& rng = Test::rng();
1✔
274

275
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
276
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
277

278
            Test::Result result("ECC scalar arithmetic " + group_id);
28✔
279
            test_scalar_arith(result, group, rng);
28✔
280
            results.push_back(result);
28✔
281
         }
28✔
282
         return results;
1✔
283
      }
×
284

285
   private:
286
      void test_scalar_arith(Test::Result& result,
28✔
287
                             const Botan::EC_Group& group,
288
                             Botan::RandomNumberGenerator& rng) const {
289
         const auto one = Botan::EC_Scalar::one(group);
28✔
290
         const auto zero = one - one;  // NOLINT(*-redundant-expression)
28✔
291
         const auto two = one + one;
28✔
292

293
         const size_t order_bytes = group.get_order_bytes();
28✔
294

295
         const auto ser_zero = std::vector<uint8_t>(order_bytes);
28✔
296

297
         const auto ser_one = [=]() {
56✔
298
            auto b = ser_zero;
28✔
299
            BOTAN_ASSERT_NOMSG(b.size() > 1);
28✔
300
            b[b.size() - 1] = 1;
28✔
301
            return b;
28✔
302
         }();
28✔
303

304
         result.test_eq("Serialization of zero is expected value", zero.serialize(), ser_zero);
56✔
305
         result.test_eq("Serialization of one is expected value", one.serialize(), ser_one);
56✔
306

307
         result.test_eq("Zero is zero", zero.is_zero(), true);
28✔
308
         result.test_eq("Negation of zero is zero", zero.negate().is_zero(), true);
56✔
309
         result.test_eq("One is not zero", one.is_zero(), false);
28✔
310

311
         // Zero inverse is not mathematically correct, but works out for our purposes
312
         result.test_eq("Inverse of zero is zero", zero.invert().serialize(), ser_zero);
84✔
313
         result.test_eq("Inverse of one is one", one.invert().serialize(), ser_one);
84✔
314

315
         result.test_eq("Inverse (vt) of zero is zero", zero.invert_vartime().serialize(), ser_zero);
84✔
316
         result.test_eq("Inverse (vt) of one is one", one.invert_vartime().serialize(), ser_one);
84✔
317

318
         constexpr size_t test_iter = 128;
28✔
319

320
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
321
            const auto r = Botan::EC_Scalar::random(group, rng);
3,584✔
322

323
            // Negation and addition are inverses
324
            result.test_eq("r + -r == 0", (r + r.negate()).serialize(), ser_zero);
14,336✔
325

326
            // Serialization and deserialization are inverses
327
            const auto r_bytes = r.serialize();
3,584✔
328
            result.test_eq("Deserialization of r round trips",
10,752✔
329
                           Botan::EC_Scalar::deserialize(group, r_bytes).value().serialize(),
10,752✔
330
                           r_bytes);
331

332
            // Multiplication and inversion are inverses
333
            const auto r2 = r * r;
3,584✔
334
            const auto r_inv = r.invert();
3,584✔
335
            result.test_eq("r * r^-1 = 1", (r * r_inv).serialize(), ser_one);
10,752✔
336

337
            const auto r_inv_vt = r.invert_vartime();
3,584✔
338
            result.confirm("CT and variable time inversions produced same result", r_inv == r_inv_vt);
7,168✔
339
         }
7,168✔
340

341
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
342
            const auto a = Botan::EC_Scalar::random(group, rng);
3,584✔
343
            const auto b = Botan::EC_Scalar::random(group, rng);
3,584✔
344

345
            const auto ab = a * b;
3,584✔
346
            const auto a_inv = a.invert();
3,584✔
347
            const auto b_inv = b.invert();
3,584✔
348

349
            result.test_eq("a * b / b = a", (ab * b_inv).serialize(), a.serialize());
10,752✔
350
            result.test_eq("a * b / a = b", (ab * a_inv).serialize(), b.serialize());
10,752✔
351

352
            auto a_plus_b = a + b;
3,584✔
353
            result.test_eq("(a + b) - b == a", (a_plus_b - b).serialize(), a.serialize());
10,752✔
354
            result.test_eq("(a + b) - a == b", (a_plus_b - a).serialize(), b.serialize());
10,752✔
355
            result.test_eq("b - (a + b) == -a", (b - a_plus_b).serialize(), a.negate().serialize());
14,336✔
356
            result.test_eq("a - (a + b) == -b", (a - a_plus_b).serialize(), b.negate().serialize());
14,336✔
357
         }
3,584✔
358

359
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
360
            const auto a = Botan::EC_Scalar::random(group, rng);
3,584✔
361
            const auto b = Botan::EC_Scalar::random(group, rng);
3,584✔
362
            const auto c = Botan::EC_Scalar::random(group, rng);
3,584✔
363

364
            const auto ab_c = (a + b) * c;
3,584✔
365
            const auto ac_bc = a * c + b * c;
3,584✔
366

367
            result.test_eq("(a + b)*c == a * c + b * c", ab_c.serialize(), ac_bc.serialize());
10,752✔
368
         }
3,584✔
369

370
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
371
            const auto a = Botan::EC_Scalar::random(group, rng);
3,584✔
372
            const auto b = Botan::EC_Scalar::random(group, rng);
3,584✔
373
            const auto c = a * b;
3,584✔
374

375
            const auto c_bn = (a.to_bigint() * b.to_bigint());
3,584✔
376
            result.test_eq("matches BigInt", c.serialize(), (c_bn % group.get_order()).serialize(order_bytes));
14,336✔
377

378
            const auto c_wide_bytes = c_bn.serialize();
3,584✔
379
            result.test_lte("Expected size", c_wide_bytes.size(), 2 * order_bytes);
3,584✔
380

381
            const auto z = Botan::EC_Scalar::from_bytes_mod_order(group, c_wide_bytes);
3,584✔
382

383
            result.test_eq("from_bytes_mod_order", c.serialize(), z.serialize());
10,752✔
384
         }
7,168✔
385

386
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
387
            std::vector<uint8_t> r(2 * group.get_order_bytes());
3,584✔
388

389
            rng.randomize(r);
3,584✔
390

391
            const auto ref = Botan::BigInt::from_bytes(r) % group.get_order();
3,584✔
392

393
            const auto scalar = Botan::EC_Scalar::from_bytes_mod_order(group, r);
3,584✔
394

395
            result.test_eq("from_bytes_mod_order (random)", scalar.serialize(), ref.serialize(group.get_order_bytes()));
10,752✔
396
         }
7,168✔
397

398
         result.end_timer();
28✔
399
      }
56✔
400
};
401

402
BOTAN_REGISTER_TEST("pubkey", "ecc_scalar_arith", ECC_Scalar_Arithmetic_Tests);
403

404
#endif
405

406
}  // namespace
407

408
}  // namespace Botan_Tests
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