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

randombit / botan / 12787475777

15 Jan 2025 11:35AM UTC coverage: 91.198% (+0.001%) from 91.197%
12787475777

Pull #4555

github

web-flow
Merge 8704987c7 into 1afcdf2cd
Pull Request #4555: Remove the workspace argument to various ECC interfaces

93407 of 102422 relevant lines covered (91.2%)

11406983.13 hits per line

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

94.78
/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 {
533✔
26
         Test::Result result("ECC base point multiply " + group_id);
533✔
27

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

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

33
         const Botan::BigInt k(k_bytes);
533✔
34

35
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
36
         std::vector<Botan::BigInt> ws;
533✔
37

38
         const auto pt = group.OS2ECP(P_bytes);
533✔
39

40
         const Botan::EC_Point& base_point = group.get_base_point();
533✔
41

42
         const Botan::EC_Point p1 = base_point * k;
533✔
43
         result.test_eq("mul with *", p1, pt);
533✔
44

45
         const Botan::EC_Point p2 = group.blinded_base_point_multiply(k, this->rng(), ws);
533✔
46
         result.test_eq("blinded_base_point_multiply", p2, pt);
533✔
47

48
         const Botan::EC_Point p3 = group.blinded_var_point_multiply(base_point, k, this->rng(), ws);
533✔
49
         result.test_eq("blinded_var_point_multiply", p3, pt);
533✔
50
   #endif
51

52
         const auto scalar = Botan::EC_Scalar::from_bigint(group, k);
533✔
53
         const auto apg = Botan::EC_AffinePoint::g_mul(scalar, this->rng());
533✔
54
         result.test_eq("AffinePoint::g_mul", apg.serialize_uncompressed(), P_bytes);
1,066✔
55

56
         const auto ag = Botan::EC_AffinePoint::generator(group);
533✔
57
         const auto ap = ag.mul(scalar, this->rng());
533✔
58
         result.test_eq("AffinePoint::mul", ap.serialize_uncompressed(), P_bytes);
1,066✔
59

60
         return result;
1,066✔
61
      }
2,132✔
62
};
63

64
BOTAN_REGISTER_TEST("pubkey", "ecc_basemul", ECC_Basepoint_Mul_Tests);
65

66
class ECC_Varpoint_Mul_Tests final : public Text_Based_Test {
×
67
   public:
68
      ECC_Varpoint_Mul_Tests() : Text_Based_Test("pubkey/ecc_var_point_mul.vec", "P,k,Z") {}
2✔
69

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

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

77
         const auto group = Botan::EC_Group::from_name(group_id);
829✔
78

79
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
80
         std::vector<Botan::BigInt> ws;
829✔
81

82
         const Botan::EC_Point pt = group.OS2ECP(p);
829✔
83

84
         result.confirm("Input point is on the curve", pt.on_the_curve());
1,658✔
85

86
         const Botan::EC_Point p1 = pt * k;
829✔
87
         result.test_eq("p * k", p1.encode(Botan::EC_Point::Compressed), z);
1,658✔
88

89
         result.confirm("Output point is on the curve", p1.on_the_curve());
1,658✔
90

91
         const Botan::EC_Point p2 = group.blinded_var_point_multiply(pt, k, this->rng(), ws);
829✔
92
         result.test_eq("p * k (blinded)", p2.encode(Botan::EC_Point::Compressed), z);
1,658✔
93
   #endif
94

95
         const auto s_k = Botan::EC_Scalar::from_bigint(group, k);
829✔
96
         const auto apt = Botan::EC_AffinePoint::deserialize(group, p).value();
1,658✔
97
         const auto apt_k = apt.mul(s_k, this->rng());
829✔
98
         result.test_eq("p * k (AffinePoint)", apt_k.serialize_compressed(), z);
1,658✔
99

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

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

106
         return result;
1,658✔
107
      }
3,316✔
108
};
109

110
BOTAN_REGISTER_TEST("pubkey", "ecc_varmul", ECC_Varpoint_Mul_Tests);
111

112
class ECC_Mul2_Tests final : public Text_Based_Test {
×
113
   public:
114
      ECC_Mul2_Tests() : Text_Based_Test("pubkey/ecc_var_point_mul2.vec", "P,x,Q,y,Z") {}
2✔
115

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

119
         const auto Z_bytes = vars.get_req_bin("Z");
819✔
120

121
         const auto check_px_qy = [&](const char* what,
4,095✔
122
                                      const Botan::EC_AffinePoint& p,
123
                                      const Botan::EC_Scalar& x,
124
                                      const Botan::EC_AffinePoint& q,
125
                                      const Botan::EC_Scalar& y,
126
                                      bool with_final_negation = false) {
127
            if(const auto z = Botan::EC_AffinePoint::mul_px_qy(p, x, q, y, rng())) {
3,276✔
128
               if(with_final_negation) {
3,276✔
129
                  result.test_eq(what, z->negate().serialize_compressed(), Z_bytes);
4,914✔
130
               } else {
131
                  result.test_eq(what, z->serialize_compressed(), Z_bytes);
4,914✔
132
               }
133
            } else {
134
               result.test_failure("EC_AffinePoint::mul_px_qy failed to produce a result");
×
135
            }
×
136

137
            // Now check the same using naive multiply and add:
138
            auto z = p.mul(x, rng()).add(q.mul(y, rng()));
3,276✔
139
            if(with_final_negation) {
3,276✔
140
               z = z.negate();
1,638✔
141
            }
142
            result.test_eq("p*x + q*y naive", z.serialize_compressed(), Z_bytes);
6,552✔
143
         };
3,276✔
144

145
         const auto group = Botan::EC_Group::from_name(group_id);
819✔
146

147
         const auto p = Botan::EC_AffinePoint::deserialize(group, vars.get_req_bin("P")).value();
3,276✔
148
         const auto q = Botan::EC_AffinePoint::deserialize(group, vars.get_req_bin("Q")).value();
3,276✔
149
         const auto x = Botan::EC_Scalar::from_bigint(group, vars.get_req_bn("x"));
1,638✔
150
         const auto y = Botan::EC_Scalar::from_bigint(group, vars.get_req_bn("y"));
1,638✔
151

152
         const auto np = p.negate();
819✔
153
         const auto nq = q.negate();
819✔
154
         const auto nx = x.negate();
819✔
155
         const auto ny = y.negate();
819✔
156

157
         check_px_qy("p*x + q*y", p, x, q, y);
819✔
158
         check_px_qy("-p*-x + -q*-y", np, nx, nq, ny);
819✔
159
         check_px_qy("-(p*-x + q*-y)", p, nx, q, ny, true);
819✔
160
         check_px_qy("-(-p*x + -q*y)", np, x, nq, y, true);
819✔
161

162
         return result;
1,638✔
163
      }
1,638✔
164
};
165

166
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2", ECC_Mul2_Tests);
167

168
class ECC_Mul2_Inf_Tests final : public Test {
×
169
   public:
170
      std::vector<Test::Result> run() override {
1✔
171
         std::vector<Test::Result> results;
1✔
172

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

176
            const auto check_px_qy = [&](const char* what,
224✔
177
                                         const Botan::EC_AffinePoint& p,
178
                                         const Botan::EC_Scalar& x,
179
                                         const Botan::EC_AffinePoint& q,
180
                                         const Botan::EC_Scalar& y) {
181
               if(const auto z = Botan::EC_AffinePoint::mul_px_qy(p, x, q, y, rng())) {
196✔
182
                  result.test_failure(Botan::fmt("EC_AffinePoint::mul_px_qy {} unexpectedly produced a result", what));
×
183
               } else {
184
                  result.test_success(Botan::fmt("EC_AffinePoint::mul_px_qy {} returned nullopt as expected", what));
196✔
185
               }
196✔
186
            };
196✔
187

188
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
189

190
            const auto g = Botan::EC_AffinePoint::generator(group);
28✔
191

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

195
            const auto r = Botan::EC_Scalar::random(group, rng());
28✔
196
            const auto neg_r = r.negate();
28✔
197
            const auto neg_r2 = neg_r + neg_r;
28✔
198

199
            const auto zero = r - r;
28✔
200
            result.confirm("Computed EC_Scalar is zero", zero.is_zero());
56✔
201

202
            const auto g2 = g.add(g);
28✔
203

204
            const auto id = Botan::EC_AffinePoint::identity(group);
28✔
205

206
            check_px_qy("0*g + r*id", g, zero, id, r);
28✔
207
            check_px_qy("0*id + r*id", id, zero, id, r);
28✔
208
            check_px_qy("0*g + 0*z", g, zero, z, zero);
28✔
209
            check_px_qy("r*g + -r*g", g, r, g, neg_r);
28✔
210
            check_px_qy("-r*g + r*g", g, neg_r, g, r);
28✔
211
            check_px_qy("r*g + r*-g", g, r, g.negate(), r);
28✔
212
            check_px_qy("r*g2 + -r2*g", g2, r, g, neg_r2);
28✔
213

214
            results.push_back(result);
28✔
215
         }
28✔
216

217
         return results;
1✔
218
      }
×
219
};
220

221
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2_inf", ECC_Mul2_Inf_Tests);
222

223
class ECC_Point_Addition_Tests final : public Test {
×
224
   public:
225
      std::vector<Test::Result> run() override {
1✔
226
         std::vector<Test::Result> results;
1✔
227

228
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
229
            Test::Result result("ECC addition " + group_id);
28✔
230

231
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
232

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

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

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

243
            const auto g_bytes = g.serialize_uncompressed();
28✔
244

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

249
            const auto nz = z.negate();
28✔
250

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

262
            results.push_back(result);
28✔
263
         }
56✔
264

265
         return results;
1✔
266
      }
×
267
};
268

269
BOTAN_REGISTER_TEST("pubkey", "ecc_pt_addition", ECC_Point_Addition_Tests);
270

271
class ECC_Scalar_Arithmetic_Tests final : public Test {
×
272
   public:
273
      std::vector<Test::Result> run() override {
1✔
274
         std::vector<Test::Result> results;
1✔
275

276
         auto& rng = Test::rng();
1✔
277

278
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
279
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
280

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

288
   private:
289
      void test_scalar_arith(Test::Result& result,
28✔
290
                             const Botan::EC_Group& group,
291
                             Botan::RandomNumberGenerator& rng) const {
292
         const auto one = Botan::EC_Scalar::one(group);
28✔
293
         const auto zero = one - one;
28✔
294
         const auto two = one + one;
28✔
295

296
         const size_t order_bytes = group.get_order_bytes();
28✔
297

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

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

307
         result.test_eq("Serialization of zero is expected value", zero.serialize(), ser_zero);
56✔
308
         result.test_eq("Serialization of one is expected value", one.serialize(), ser_one);
56✔
309

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

314
         // Zero inverse is not mathematically correct, but works out for our purposes
315
         result.test_eq("Inverse of zero is zero", zero.invert().serialize(), ser_zero);
84✔
316
         result.test_eq("Inverse of one is one", one.invert().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
         }
7,168✔
337

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

342
            const auto ab = a * b;
3,584✔
343
            const auto a_inv = a.invert();
3,584✔
344
            const auto b_inv = b.invert();
3,584✔
345

346
            result.test_eq("a * b / b = a", (ab * b_inv).serialize(), a.serialize());
14,336✔
347
            result.test_eq("a * b / a = b", (ab * a_inv).serialize(), b.serialize());
14,336✔
348

349
            auto a_plus_b = a + b;
3,584✔
350
            result.test_eq("(a + b) - b == a", (a_plus_b - b).serialize(), a.serialize());
14,336✔
351
            result.test_eq("(a + b) - a == b", (a_plus_b - a).serialize(), b.serialize());
14,336✔
352
            result.test_eq("b - (a + b) == -a", (b - a_plus_b).serialize(), a.negate().serialize());
17,920✔
353
            result.test_eq("a - (a + b) == -b", (a - a_plus_b).serialize(), b.negate().serialize());
17,920✔
354
         }
3,584✔
355

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

361
            const auto ab_c = (a + b) * c;
3,584✔
362
            const auto ac_bc = a * c + b * c;
3,584✔
363

364
            result.test_eq("(a + b)*c == a * c + b * c", ab_c.serialize(), ac_bc.serialize());
10,752✔
365
         }
3,584✔
366

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

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

375
            const auto c_wide_bytes = c_bn.serialize();
3,584✔
376
            result.test_lte("Expected size", c_wide_bytes.size(), 2 * order_bytes);
3,584✔
377

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

380
            result.test_eq("from_bytes_mod_order", c.serialize(), z.serialize());
10,752✔
381
         }
10,752✔
382

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

386
            rng.randomize(r);
3,584✔
387

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

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

392
            result.test_eq("from_bytes_mod_order (random)", scalar.serialize(), ref.serialize(group.get_order_bytes()));
10,752✔
393
         }
10,752✔
394

395
         result.end_timer();
28✔
396
      }
56✔
397
};
398

399
BOTAN_REGISTER_TEST("pubkey", "ecc_scalar_arith", ECC_Scalar_Arithmetic_Tests);
400

401
#endif
402

403
}  // namespace
404

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