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

randombit / botan / 12847647540

18 Jan 2025 09:39PM UTC coverage: 91.211% (+0.003%) from 91.208%
12847647540

Pull #4555

github

web-flow
Merge 449f252b4 into 5e2bd0320
Pull Request #4555: Remove the workspace argument to various ECC interfaces

93418 of 102420 relevant lines covered (91.21%)

11426977.61 hits per line

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

94.5
/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

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

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

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

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

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

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

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

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

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

68
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
69
         const Botan::EC_Point p1 = group.OS2ECP(p) * k;
1,658✔
70
         result.test_eq("EC_Point Montgomery ladder", p1.encode(Botan::EC_Point::Compressed), z);
1,658✔
71
   #endif
72

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

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

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

84
         return result;
1,658✔
85
      }
3,316✔
86
};
87

88
BOTAN_REGISTER_TEST("pubkey", "ecc_varmul", ECC_Varpoint_Mul_Tests);
89

90
class ECC_Mul2_Tests final : public Text_Based_Test {
×
91
   public:
92
      ECC_Mul2_Tests() : Text_Based_Test("pubkey/ecc_var_point_mul2.vec", "P,x,Q,y,Z") {}
2✔
93

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

97
         const auto Z_bytes = vars.get_req_bin("Z");
819✔
98

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

115
            // Now check the same using naive multiply and add:
116
            auto z = p.mul(x, rng()).add(q.mul(y, rng()));
3,276✔
117
            if(with_final_negation) {
3,276✔
118
               z = z.negate();
1,638✔
119
            }
120
            result.test_eq("p*x + q*y naive", z.serialize_compressed(), Z_bytes);
6,552✔
121
         };
3,276✔
122

123
         const auto group = Botan::EC_Group::from_name(group_id);
819✔
124

125
         const auto p = Botan::EC_AffinePoint::deserialize(group, vars.get_req_bin("P")).value();
3,276✔
126
         const auto q = Botan::EC_AffinePoint::deserialize(group, vars.get_req_bin("Q")).value();
3,276✔
127
         const auto x = Botan::EC_Scalar::from_bigint(group, vars.get_req_bn("x"));
1,638✔
128
         const auto y = Botan::EC_Scalar::from_bigint(group, vars.get_req_bn("y"));
1,638✔
129

130
         const auto np = p.negate();
819✔
131
         const auto nq = q.negate();
819✔
132
         const auto nx = x.negate();
819✔
133
         const auto ny = y.negate();
819✔
134

135
         check_px_qy("p*x + q*y", p, x, q, y);
819✔
136
         check_px_qy("-p*-x + -q*-y", np, nx, nq, ny);
819✔
137
         check_px_qy("-(p*-x + q*-y)", p, nx, q, ny, true);
819✔
138
         check_px_qy("-(-p*x + -q*y)", np, x, nq, y, true);
819✔
139

140
         return result;
1,638✔
141
      }
1,638✔
142
};
143

144
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2", ECC_Mul2_Tests);
145

146
class ECC_Mul2_Inf_Tests final : public Test {
×
147
   public:
148
      std::vector<Test::Result> run() override {
1✔
149
         std::vector<Test::Result> results;
1✔
150

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

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

166
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
167

168
            const auto g = Botan::EC_AffinePoint::generator(group);
28✔
169

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

173
            const auto r = Botan::EC_Scalar::random(group, rng());
28✔
174
            const auto neg_r = r.negate();
28✔
175
            const auto neg_r2 = neg_r + neg_r;
28✔
176

177
            const auto zero = r - r;
28✔
178
            result.confirm("Computed EC_Scalar is zero", zero.is_zero());
56✔
179

180
            const auto g2 = g.add(g);
28✔
181

182
            const auto id = Botan::EC_AffinePoint::identity(group);
28✔
183

184
            check_px_qy("0*g + r*id", g, zero, id, r);
28✔
185
            check_px_qy("0*id + r*id", id, zero, id, r);
28✔
186
            check_px_qy("0*g + 0*z", g, zero, z, zero);
28✔
187
            check_px_qy("r*g + -r*g", g, r, g, neg_r);
28✔
188
            check_px_qy("-r*g + r*g", g, neg_r, g, r);
28✔
189
            check_px_qy("r*g + r*-g", g, r, g.negate(), r);
28✔
190
            check_px_qy("r*g2 + -r2*g", g2, r, g, neg_r2);
28✔
191

192
            results.push_back(result);
28✔
193
         }
28✔
194

195
         return results;
1✔
196
      }
×
197
};
198

199
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2_inf", ECC_Mul2_Inf_Tests);
200

201
class ECC_Point_Addition_Tests final : public Test {
×
202
   public:
203
      std::vector<Test::Result> run() override {
1✔
204
         std::vector<Test::Result> results;
1✔
205

206
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
207
            Test::Result result("ECC addition " + group_id);
28✔
208

209
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
210

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

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

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

221
            const auto g_bytes = g.serialize_uncompressed();
28✔
222

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

227
            const auto nz = z.negate();
28✔
228

229
            check_expr_is_g("g + id", g.add(id));
28✔
230
            check_expr_is_g("id + g", id.add(g));
28✔
231
            check_expr_is_g("g + id", g.add(id));
28✔
232
            check_expr_is_g("g + -id", g.add(id.negate()));
28✔
233
            check_expr_is_g("g + g + -g", g.add(g).add(g.negate()));
28✔
234
            check_expr_is_g("-id + g", id.negate().add(g));
28✔
235
            check_expr_is_g("z + g - z", z.add(g).add(nz));
28✔
236
            check_expr_is_g("z - z + g", z.add(nz).add(g));
28✔
237
            check_expr_is_g("z + z + g - z - z", z.add(z).add(g).add(nz).add(nz));
28✔
238
            check_expr_is_g("z + id + g + z - z - z", z.add(id).add(g).add(z).add(nz).add(nz));
28✔
239

240
            results.push_back(result);
28✔
241
         }
56✔
242

243
         return results;
1✔
244
      }
×
245
};
246

247
BOTAN_REGISTER_TEST("pubkey", "ecc_pt_addition", ECC_Point_Addition_Tests);
248

249
class ECC_Scalar_Arithmetic_Tests final : public Test {
×
250
   public:
251
      std::vector<Test::Result> run() override {
1✔
252
         std::vector<Test::Result> results;
1✔
253

254
         auto& rng = Test::rng();
1✔
255

256
         for(const auto& group_id : Botan::EC_Group::known_named_groups()) {
29✔
257
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
258

259
            Test::Result result("ECC scalar arithmetic " + group_id);
28✔
260
            test_scalar_arith(result, group, rng);
28✔
261
            results.push_back(result);
28✔
262
         }
28✔
263
         return results;
1✔
264
      }
×
265

266
   private:
267
      void test_scalar_arith(Test::Result& result,
28✔
268
                             const Botan::EC_Group& group,
269
                             Botan::RandomNumberGenerator& rng) const {
270
         const auto one = Botan::EC_Scalar::one(group);
28✔
271
         const auto zero = one - one;
28✔
272
         const auto two = one + one;
28✔
273

274
         const size_t order_bytes = group.get_order_bytes();
28✔
275

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

278
         const auto ser_one = [=]() {
56✔
279
            auto b = ser_zero;
28✔
280
            BOTAN_ASSERT_NOMSG(b.size() > 1);
28✔
281
            b[b.size() - 1] = 1;
28✔
282
            return b;
28✔
283
         }();
28✔
284

285
         result.test_eq("Serialization of zero is expected value", zero.serialize(), ser_zero);
56✔
286
         result.test_eq("Serialization of one is expected value", one.serialize(), ser_one);
56✔
287

288
         result.test_eq("Zero is zero", zero.is_zero(), true);
28✔
289
         result.test_eq("Negation of zero is zero", zero.negate().is_zero(), true);
56✔
290
         result.test_eq("One is not zero", one.is_zero(), false);
28✔
291

292
         // Zero inverse is not mathematically correct, but works out for our purposes
293
         result.test_eq("Inverse of zero is zero", zero.invert().serialize(), ser_zero);
84✔
294
         result.test_eq("Inverse of one is one", one.invert().serialize(), ser_one);
84✔
295

296
         constexpr size_t test_iter = 128;
28✔
297

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

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

304
            // Serialization and deserialization are inverses
305
            const auto r_bytes = r.serialize();
3,584✔
306
            result.test_eq("Deserialization of r round trips",
10,752✔
307
                           Botan::EC_Scalar::deserialize(group, r_bytes).value().serialize(),
10,752✔
308
                           r_bytes);
309

310
            // Multiplication and inversion are inverses
311
            const auto r2 = r * r;
3,584✔
312
            const auto r_inv = r.invert();
3,584✔
313
            result.test_eq("r * r^-1 = 1", (r * r_inv).serialize(), ser_one);
10,752✔
314
         }
7,168✔
315

316
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
317
            const auto a = Botan::EC_Scalar::random(group, rng);
3,584✔
318
            const auto b = Botan::EC_Scalar::random(group, rng);
3,584✔
319

320
            const auto ab = a * b;
3,584✔
321
            const auto a_inv = a.invert();
3,584✔
322
            const auto b_inv = b.invert();
3,584✔
323

324
            result.test_eq("a * b / b = a", (ab * b_inv).serialize(), a.serialize());
10,752✔
325
            result.test_eq("a * b / a = b", (ab * a_inv).serialize(), b.serialize());
10,752✔
326

327
            auto a_plus_b = a + b;
3,584✔
328
            result.test_eq("(a + b) - b == a", (a_plus_b - b).serialize(), a.serialize());
10,752✔
329
            result.test_eq("(a + b) - a == b", (a_plus_b - a).serialize(), b.serialize());
10,752✔
330
            result.test_eq("b - (a + b) == -a", (b - a_plus_b).serialize(), a.negate().serialize());
14,336✔
331
            result.test_eq("a - (a + b) == -b", (a - a_plus_b).serialize(), b.negate().serialize());
14,336✔
332
         }
3,584✔
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
            const auto c = Botan::EC_Scalar::random(group, rng);
3,584✔
338

339
            const auto ab_c = (a + b) * c;
3,584✔
340
            const auto ac_bc = a * c + b * c;
3,584✔
341

342
            result.test_eq("(a + b)*c == a * c + b * c", ab_c.serialize(), ac_bc.serialize());
10,752✔
343
         }
3,584✔
344

345
         for(size_t i = 0; i != test_iter; ++i) {
3,612✔
346
            const auto a = Botan::EC_Scalar::random(group, rng);
3,584✔
347
            const auto b = Botan::EC_Scalar::random(group, rng);
3,584✔
348
            const auto c = a * b;
3,584✔
349

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

353
            const auto c_wide_bytes = c_bn.serialize();
3,584✔
354
            result.test_lte("Expected size", c_wide_bytes.size(), 2 * order_bytes);
3,584✔
355

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

358
            result.test_eq("from_bytes_mod_order", c.serialize(), z.serialize());
10,752✔
359
         }
10,752✔
360

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

364
            rng.randomize(r);
3,584✔
365

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

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

370
            result.test_eq("from_bytes_mod_order (random)", scalar.serialize(), ref.serialize(group.get_order_bytes()));
10,752✔
371
         }
10,752✔
372

373
         result.end_timer();
28✔
374
      }
56✔
375
};
376

377
BOTAN_REGISTER_TEST("pubkey", "ecc_scalar_arith", ECC_Scalar_Arithmetic_Tests);
378

379
#endif
380

381
}  // namespace
382

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