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

randombit / botan / 12805544433

16 Jan 2025 09:08AM UTC coverage: 90.876% (-0.4%) from 91.245%
12805544433

Pull #4540

github

web-flow
Merge cc1ceff51 into 9b798efbb
Pull Request #4540: PKCS #11 Version 3.2 Support

93425 of 102805 relevant lines covered (90.88%)

11409241.89 hits per line

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

94.64
/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
      Test::Result run_one_test(const std::string& group_id, const VarMap& vars) override {
535✔
26
         Test::Result result("ECC base point multiply " + group_id);
535✔
27

28
         const auto k_bytes = vars.get_req_bin("k");
535✔
29
         const auto P_bytes = vars.get_req_bin("P");
535✔
30

31
         const auto group = Botan::EC_Group::from_name(group_id);
535✔
32

33
         const Botan::BigInt k(k_bytes);
535✔
34
         std::vector<Botan::BigInt> ws;
535✔
35

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

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

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

50
         return result;
1,070✔
51
      }
2,140✔
52
};
53

54
BOTAN_REGISTER_TEST("pubkey", "ecc_basemul", ECC_Basepoint_Mul_Tests);
55

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

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

63
         const auto p = vars.get_req_bin("P");
829✔
64
         const Botan::BigInt k = vars.get_req_bn("k");
829✔
65
         const auto z = vars.get_req_bin("Z");
829✔
66

67
         const auto group = Botan::EC_Group::from_name(group_id);
829✔
68

69
         std::vector<Botan::BigInt> ws;
829✔
70

71
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
72
         const Botan::EC_Point p1 = group.OS2ECP(p) * k;
1,658✔
73
         result.test_eq("EC_Point Montgomery ladder", p1.encode(Botan::EC_Point::Compressed), z);
1,658✔
74
         result.confirm("Output point is on the curve", p1.on_the_curve());
1,658✔
75
   #endif
76

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

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

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

88
         return result;
1,658✔
89
      }
3,316✔
90
};
91

92
BOTAN_REGISTER_TEST("pubkey", "ecc_varmul", ECC_Varpoint_Mul_Tests);
93

94
class ECC_Mul2_Tests final : public Text_Based_Test {
×
95
   public:
96
      ECC_Mul2_Tests() : Text_Based_Test("pubkey/ecc_var_point_mul2.vec", "P,x,Q,y,Z") {}
2✔
97

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

101
         const auto Z_bytes = vars.get_req_bin("Z");
819✔
102

103
         const auto check_px_qy = [&](const char* what,
4,095✔
104
                                      const Botan::EC_AffinePoint& p,
105
                                      const Botan::EC_Scalar& x,
106
                                      const Botan::EC_AffinePoint& q,
107
                                      const Botan::EC_Scalar& y,
108
                                      bool with_final_negation = false) {
109
            if(const auto z = Botan::EC_AffinePoint::mul_px_qy(p, x, q, y, rng())) {
3,276✔
110
               if(with_final_negation) {
3,276✔
111
                  result.test_eq(what, z->negate().serialize_compressed(), Z_bytes);
4,914✔
112
               } else {
113
                  result.test_eq(what, z->serialize_compressed(), Z_bytes);
4,914✔
114
               }
115
            } else {
116
               result.test_failure("EC_AffinePoint::mul_px_qy failed to produce a result");
×
117
            }
×
118

119
            // Now check the same using naive multiply and add:
120
            std::vector<BigInt> ws;
3,276✔
121
            auto z = p.mul(x, rng(), ws).add(q.mul(y, rng(), ws));
3,276✔
122
            if(with_final_negation) {
3,276✔
123
               z = z.negate();
1,638✔
124
            }
125
            result.test_eq("p*x + q*y naive", z.serialize_compressed(), Z_bytes);
6,552✔
126
         };
3,276✔
127

128
         const auto group = Botan::EC_Group::from_name(group_id);
819✔
129

130
         const auto p = Botan::EC_AffinePoint::deserialize(group, vars.get_req_bin("P")).value();
3,276✔
131
         const auto q = Botan::EC_AffinePoint::deserialize(group, vars.get_req_bin("Q")).value();
3,276✔
132
         const auto x = Botan::EC_Scalar::from_bigint(group, vars.get_req_bn("x"));
1,638✔
133
         const auto y = Botan::EC_Scalar::from_bigint(group, vars.get_req_bn("y"));
1,638✔
134

135
         const auto np = p.negate();
819✔
136
         const auto nq = q.negate();
819✔
137
         const auto nx = x.negate();
819✔
138
         const auto ny = y.negate();
819✔
139

140
         check_px_qy("p*x + q*y", p, x, q, y);
819✔
141
         check_px_qy("-p*-x + -q*-y", np, nx, nq, ny);
819✔
142
         check_px_qy("-(p*-x + q*-y)", p, nx, q, ny, true);
819✔
143
         check_px_qy("-(-p*x + -q*y)", np, x, nq, y, true);
819✔
144

145
         return result;
1,638✔
146
      }
1,638✔
147
};
148

149
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2", ECC_Mul2_Tests);
150

151
class ECC_Mul2_Inf_Tests final : public Test {
×
152
   public:
153
      std::vector<Test::Result> run() override {
1✔
154
         std::vector<Test::Result> results;
1✔
155

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

159
            const auto check_px_qy = [&](const char* what,
224✔
160
                                         const Botan::EC_AffinePoint& p,
161
                                         const Botan::EC_Scalar& x,
162
                                         const Botan::EC_AffinePoint& q,
163
                                         const Botan::EC_Scalar& y) {
164
               if(const auto z = Botan::EC_AffinePoint::mul_px_qy(p, x, q, y, rng())) {
196✔
165
                  result.test_failure(Botan::fmt("EC_AffinePoint::mul_px_qy {} unexpectedly produced a result", what));
×
166
               } else {
167
                  result.test_success(Botan::fmt("EC_AffinePoint::mul_px_qy {} returned nullopt as expected", what));
392✔
168
               }
196✔
169
            };
196✔
170

171
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
172

173
            const auto g = Botan::EC_AffinePoint::generator(group);
28✔
174

175
            // Choose some other random point z
176
            std::vector<Botan::BigInt> ws;
28✔
177
            const auto z = g.mul(Botan::EC_Scalar::random(group, rng()), rng(), ws);
28✔
178

179
            const auto r = Botan::EC_Scalar::random(group, rng());
28✔
180
            const auto neg_r = r.negate();
28✔
181
            const auto neg_r2 = neg_r + neg_r;
28✔
182

183
            const auto zero = r - r;
28✔
184
            result.confirm("Computed EC_Scalar is zero", zero.is_zero());
56✔
185

186
            const auto g2 = g.add(g);
28✔
187

188
            const auto id = Botan::EC_AffinePoint::identity(group);
28✔
189

190
            check_px_qy("0*g + r*id", g, zero, id, r);
28✔
191
            check_px_qy("0*id + r*id", id, zero, id, r);
28✔
192
            check_px_qy("0*g + 0*z", g, zero, z, zero);
28✔
193
            check_px_qy("r*g + -r*g", g, r, g, neg_r);
28✔
194
            check_px_qy("-r*g + r*g", g, neg_r, g, r);
28✔
195
            check_px_qy("r*g + r*-g", g, r, g.negate(), r);
28✔
196
            check_px_qy("r*g2 + -r2*g", g2, r, g, neg_r2);
28✔
197

198
            results.push_back(result);
28✔
199
         }
28✔
200

201
         return results;
1✔
202
      }
×
203
};
204

205
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2_inf", ECC_Mul2_Inf_Tests);
206

207
class ECC_Point_Addition_Tests final : public Test {
×
208
   public:
209
      std::vector<Test::Result> run() override {
1✔
210
         std::vector<Test::Result> results;
1✔
211

212
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
213
            Test::Result result("ECC addition " + group_id);
28✔
214

215
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
216

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

220
            // Choose some other random point z
221
            std::vector<Botan::BigInt> ws;
28✔
222
            const auto z = g.mul(Botan::EC_Scalar::random(group, rng()), rng(), ws);
28✔
223
            result.test_eq("z is not the identity element", z.is_identity(), false);
28✔
224

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

228
            const auto g_bytes = g.serialize_uncompressed();
28✔
229

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

234
            const auto nz = z.negate();
28✔
235

236
            check_expr_is_g("g + id", g.add(id));
28✔
237
            check_expr_is_g("id + g", id.add(g));
28✔
238
            check_expr_is_g("g + id", g.add(id));
28✔
239
            check_expr_is_g("g + -id", g.add(id.negate()));
28✔
240
            check_expr_is_g("g + g + -g", g.add(g).add(g.negate()));
28✔
241
            check_expr_is_g("-id + g", id.negate().add(g));
28✔
242
            check_expr_is_g("z + g - z", z.add(g).add(nz));
28✔
243
            check_expr_is_g("z - z + g", z.add(nz).add(g));
28✔
244
            check_expr_is_g("z + z + g - z - z", z.add(z).add(g).add(nz).add(nz));
28✔
245
            check_expr_is_g("z + id + g + z - z - z", z.add(id).add(g).add(z).add(nz).add(nz));
28✔
246

247
            results.push_back(result);
28✔
248
         }
56✔
249

250
         return results;
1✔
251
      }
×
252
};
253

254
BOTAN_REGISTER_TEST("pubkey", "ecc_pt_addition", ECC_Point_Addition_Tests);
255

256
class ECC_Scalar_Arithmetic_Tests final : public Test {
×
257
   public:
258
      std::vector<Test::Result> run() override {
1✔
259
         std::vector<Test::Result> results;
1✔
260

261
         auto& rng = Test::rng();
1✔
262

263
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
264
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
265

266
            Test::Result result("ECC scalar arithmetic " + group_id);
28✔
267
            test_scalar_arith(result, group, rng);
28✔
268
            results.push_back(result);
28✔
269
         }
28✔
270
         return results;
1✔
271
      }
×
272

273
   private:
274
      void test_scalar_arith(Test::Result& result,
28✔
275
                             const Botan::EC_Group& group,
276
                             Botan::RandomNumberGenerator& rng) const {
277
         const auto one = Botan::EC_Scalar::one(group);
28✔
278
         const auto zero = one - one;
28✔
279
         const auto two = one + one;
28✔
280

281
         const size_t order_bytes = group.get_order_bytes();
28✔
282

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

285
         const auto ser_one = [=]() {
56✔
286
            auto b = ser_zero;
28✔
287
            BOTAN_ASSERT_NOMSG(b.size() > 1);
28✔
288
            b[b.size() - 1] = 1;
28✔
289
            return b;
28✔
290
         }();
28✔
291

292
         result.test_eq("Serialization of zero is expected value", zero.serialize(), ser_zero);
56✔
293
         result.test_eq("Serialization of one is expected value", one.serialize(), ser_one);
56✔
294

295
         result.test_eq("Zero is zero", zero.is_zero(), true);
28✔
296
         result.test_eq("Negation of zero is zero", zero.negate().is_zero(), true);
56✔
297
         result.test_eq("One is not zero", one.is_zero(), false);
28✔
298

299
         // Zero inverse is not mathematically correct, but works out for our purposes
300
         result.test_eq("Inverse of zero is zero", zero.invert().serialize(), ser_zero);
84✔
301
         result.test_eq("Inverse of one is one", one.invert().serialize(), ser_one);
84✔
302

303
         constexpr size_t test_iter = 128;
28✔
304

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

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

311
            // Serialization and deserialization are inverses
312
            const auto r_bytes = r.serialize();
3,584✔
313
            result.test_eq("Deserialization of r round trips",
10,752✔
314
                           Botan::EC_Scalar::deserialize(group, r_bytes).value().serialize(),
10,752✔
315
                           r_bytes);
316

317
            // Multiplication and inversion are inverses
318
            const auto r2 = r * r;
3,584✔
319
            const auto r_inv = r.invert();
3,584✔
320
            result.test_eq("r * r^-1 = 1", (r * r_inv).serialize(), ser_one);
10,752✔
321
         }
7,168✔
322

323
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
324
            const auto a = Botan::EC_Scalar::random(group, rng);
3,584✔
325
            const auto b = Botan::EC_Scalar::random(group, rng);
3,584✔
326

327
            const auto ab = a * b;
3,584✔
328
            const auto a_inv = a.invert();
3,584✔
329
            const auto b_inv = b.invert();
3,584✔
330

331
            result.test_eq("a * b / b = a", (ab * b_inv).serialize(), a.serialize());
10,752✔
332
            result.test_eq("a * b / a = b", (ab * a_inv).serialize(), b.serialize());
10,752✔
333

334
            auto a_plus_b = a + b;
3,584✔
335
            result.test_eq("(a + b) - b == a", (a_plus_b - b).serialize(), a.serialize());
10,752✔
336
            result.test_eq("(a + b) - a == b", (a_plus_b - a).serialize(), b.serialize());
10,752✔
337
            result.test_eq("b - (a + b) == -a", (b - a_plus_b).serialize(), a.negate().serialize());
14,336✔
338
            result.test_eq("a - (a + b) == -b", (a - a_plus_b).serialize(), b.negate().serialize());
14,336✔
339
         }
3,584✔
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
            const auto c = Botan::EC_Scalar::random(group, rng);
3,584✔
345

346
            const auto ab_c = (a + b) * c;
3,584✔
347
            const auto ac_bc = a * c + b * c;
3,584✔
348

349
            result.test_eq("(a + b)*c == a * c + b * c", ab_c.serialize(), ac_bc.serialize());
10,752✔
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 = a * b;
3,584✔
356

357
            const auto c_bn = (a.to_bigint() * b.to_bigint());
7,168✔
358
            result.test_eq("matches BigInt", c.serialize(), (c_bn % group.get_order()).serialize(order_bytes));
14,336✔
359

360
            const auto c_wide_bytes = c_bn.serialize();
3,584✔
361
            result.test_lte("Expected size", c_wide_bytes.size(), 2 * order_bytes);
3,584✔
362

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

365
            result.test_eq("from_bytes_mod_order", c.serialize(), z.serialize());
10,752✔
366
         }
10,752✔
367

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

371
            rng.randomize(r);
3,584✔
372

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

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

377
            result.test_eq("from_bytes_mod_order (random)", scalar.serialize(), ref.serialize(group.get_order_bytes()));
10,752✔
378
         }
10,752✔
379

380
         result.end_timer();
28✔
381
      }
56✔
382
};
383

384
BOTAN_REGISTER_TEST("pubkey", "ecc_scalar_arith", ECC_Scalar_Arithmetic_Tests);
385

386
#endif
387

388
}  // namespace
389

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

© 2025 Coveralls, Inc