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

randombit / botan / 13474418621

22 Feb 2025 03:12PM UTC coverage: 91.687% (-0.004%) from 91.691%
13474418621

push

github

web-flow
Merge pull request #4555 from randombit/jack/remove-ws-arg

Remove the workspace argument to various ECC interfaces

95796 of 104482 relevant lines covered (91.69%)

11191920.09 hits per line

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

94.74
/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/internal/fmt.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
namespace {
18

19
#if defined(BOTAN_HAS_ECC_GROUP)
20

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

189
            const auto zero = r - r;
28✔
190
            result.confirm("Computed EC_Scalar is zero", zero.is_zero());
56✔
191

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

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

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

204
            results.push_back(result);
28✔
205
         }
28✔
206

207
         return results;
1✔
208
      }
×
209
};
210

211
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2_inf", ECC_Mul2_Inf_Tests);
212

213
class ECC_Point_Addition_Tests final : public Test {
×
214
   public:
215
      std::vector<Test::Result> run() override {
1✔
216
         std::vector<Test::Result> results;
1✔
217

218
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
219
            Test::Result result("ECC addition " + group_id);
28✔
220

221
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
222

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

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

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

233
            const auto g_bytes = g.serialize_uncompressed();
28✔
234

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

239
            const auto nz = z.negate();
28✔
240

241
            check_expr_is_g("g + id", g.add(id));
28✔
242
            check_expr_is_g("id + g", id.add(g));
28✔
243
            check_expr_is_g("g + id", g.add(id));
28✔
244
            check_expr_is_g("g + -id", g.add(id.negate()));
28✔
245
            check_expr_is_g("g + g + -g", g.add(g).add(g.negate()));
28✔
246
            check_expr_is_g("-id + g", id.negate().add(g));
28✔
247
            check_expr_is_g("z + g - z", z.add(g).add(nz));
28✔
248
            check_expr_is_g("z - z + g", z.add(nz).add(g));
28✔
249
            check_expr_is_g("z + z + g - z - z", z.add(z).add(g).add(nz).add(nz));
28✔
250
            check_expr_is_g("z + id + g + z - z - z", z.add(id).add(g).add(z).add(nz).add(nz));
28✔
251

252
            results.push_back(result);
28✔
253
         }
56✔
254

255
         return results;
1✔
256
      }
×
257
};
258

259
BOTAN_REGISTER_TEST("pubkey", "ecc_pt_addition", ECC_Point_Addition_Tests);
260

261
class ECC_Scalar_Arithmetic_Tests final : public Test {
×
262
   public:
263
      std::vector<Test::Result> run() override {
1✔
264
         std::vector<Test::Result> results;
1✔
265

266
         auto& rng = Test::rng();
1✔
267

268
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
269
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
270

271
            Test::Result result("ECC scalar arithmetic " + group_id);
28✔
272
            test_scalar_arith(result, group, rng);
28✔
273
            results.push_back(result);
28✔
274
         }
28✔
275
         return results;
1✔
276
      }
×
277

278
   private:
279
      void test_scalar_arith(Test::Result& result,
28✔
280
                             const Botan::EC_Group& group,
281
                             Botan::RandomNumberGenerator& rng) const {
282
         const auto one = Botan::EC_Scalar::one(group);
28✔
283
         const auto zero = one - one;
28✔
284
         const auto two = one + one;
28✔
285

286
         const size_t order_bytes = group.get_order_bytes();
28✔
287

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

290
         const auto ser_one = [=]() {
56✔
291
            auto b = ser_zero;
28✔
292
            BOTAN_ASSERT_NOMSG(b.size() > 1);
28✔
293
            b[b.size() - 1] = 1;
28✔
294
            return b;
28✔
295
         }();
28✔
296

297
         result.test_eq("Serialization of zero is expected value", zero.serialize(), ser_zero);
56✔
298
         result.test_eq("Serialization of one is expected value", one.serialize(), ser_one);
56✔
299

300
         result.test_eq("Zero is zero", zero.is_zero(), true);
28✔
301
         result.test_eq("Negation of zero is zero", zero.negate().is_zero(), true);
56✔
302
         result.test_eq("One is not zero", one.is_zero(), false);
28✔
303

304
         // Zero inverse is not mathematically correct, but works out for our purposes
305
         result.test_eq("Inverse of zero is zero", zero.invert().serialize(), ser_zero);
84✔
306
         result.test_eq("Inverse of one is one", one.invert().serialize(), ser_one);
84✔
307

308
         result.test_eq("Inverse (vt) of zero is zero", zero.invert_vartime().serialize(), ser_zero);
84✔
309
         result.test_eq("Inverse (vt) of one is one", one.invert_vartime().serialize(), ser_one);
84✔
310

311
         constexpr size_t test_iter = 128;
28✔
312

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

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

319
            // Serialization and deserialization are inverses
320
            const auto r_bytes = r.serialize();
3,584✔
321
            result.test_eq("Deserialization of r round trips",
10,752✔
322
                           Botan::EC_Scalar::deserialize(group, r_bytes).value().serialize(),
10,752✔
323
                           r_bytes);
324

325
            // Multiplication and inversion are inverses
326
            const auto r2 = r * r;
3,584✔
327
            const auto r_inv = r.invert();
3,584✔
328
            result.test_eq("r * r^-1 = 1", (r * r_inv).serialize(), ser_one);
10,752✔
329

330
            const auto r_inv_vt = r.invert_vartime();
3,584✔
331
            result.confirm("CT and variable time inversions produced same result", r_inv == r_inv_vt);
7,168✔
332
         }
7,168✔
333

334
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
335
            const auto a = Botan::EC_Scalar::random(group, rng);
3,584✔
336
            const auto b = Botan::EC_Scalar::random(group, rng);
3,584✔
337

338
            const auto ab = a * b;
3,584✔
339
            const auto a_inv = a.invert();
3,584✔
340
            const auto b_inv = b.invert();
3,584✔
341

342
            result.test_eq("a * b / b = a", (ab * b_inv).serialize(), a.serialize());
10,752✔
343
            result.test_eq("a * b / a = b", (ab * a_inv).serialize(), b.serialize());
10,752✔
344

345
            auto a_plus_b = a + b;
3,584✔
346
            result.test_eq("(a + b) - b == a", (a_plus_b - b).serialize(), a.serialize());
10,752✔
347
            result.test_eq("(a + b) - a == b", (a_plus_b - a).serialize(), b.serialize());
10,752✔
348
            result.test_eq("b - (a + b) == -a", (b - a_plus_b).serialize(), a.negate().serialize());
14,336✔
349
            result.test_eq("a - (a + b) == -b", (a - a_plus_b).serialize(), b.negate().serialize());
14,336✔
350
         }
3,584✔
351

352
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
353
            const auto a = Botan::EC_Scalar::random(group, rng);
3,584✔
354
            const auto b = Botan::EC_Scalar::random(group, rng);
3,584✔
355
            const auto c = Botan::EC_Scalar::random(group, rng);
3,584✔
356

357
            const auto ab_c = (a + b) * c;
3,584✔
358
            const auto ac_bc = a * c + b * c;
3,584✔
359

360
            result.test_eq("(a + b)*c == a * c + b * c", ab_c.serialize(), ac_bc.serialize());
10,752✔
361
         }
3,584✔
362

363
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
364
            const auto a = Botan::EC_Scalar::random(group, rng);
3,584✔
365
            const auto b = Botan::EC_Scalar::random(group, rng);
3,584✔
366
            const auto c = a * b;
3,584✔
367

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

371
            const auto c_wide_bytes = c_bn.serialize();
3,584✔
372
            result.test_lte("Expected size", c_wide_bytes.size(), 2 * order_bytes);
3,584✔
373

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

376
            result.test_eq("from_bytes_mod_order", c.serialize(), z.serialize());
10,752✔
377
         }
7,168✔
378

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

382
            rng.randomize(r);
3,584✔
383

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

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

388
            result.test_eq("from_bytes_mod_order (random)", scalar.serialize(), ref.serialize(group.get_order_bytes()));
10,752✔
389
         }
7,168✔
390

391
         result.end_timer();
28✔
392
      }
56✔
393
};
394

395
BOTAN_REGISTER_TEST("pubkey", "ecc_scalar_arith", ECC_Scalar_Arithmetic_Tests);
396

397
#endif
398

399
}  // namespace
400

401
}  // 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