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

randombit / botan / 12800428018

16 Jan 2025 01:44AM UTC coverage: 91.191% (-0.003%) from 91.194%
12800428018

Pull #4555

github

web-flow
Merge 26491cefe into 52e0661e2
Pull Request #4555: Remove the workspace argument to various ECC interfaces

93328 of 102343 relevant lines covered (91.19%)

11502783.22 hits per line

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

94.52
/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
         std::vector<Botan::BigInt> ws;
829✔
70

71
         const Botan::EC_Point p1 = group.OS2ECP(p) * k;
1,658✔
72
         result.test_eq("EC_Point Montgomery ladder", p1.encode(Botan::EC_Point::Compressed), z);
1,658✔
73
   #endif
74

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

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

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

86
         return result;
1,658✔
87
      }
3,316✔
88
};
89

90
BOTAN_REGISTER_TEST("pubkey", "ecc_varmul", ECC_Varpoint_Mul_Tests);
91

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

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

99
         const auto Z_bytes = vars.get_req_bin("Z");
819✔
100

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

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

125
         const auto group = Botan::EC_Group::from_name(group_id);
819✔
126

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

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

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

142
         return result;
1,638✔
143
      }
1,638✔
144
};
145

146
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2", ECC_Mul2_Tests);
147

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

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

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

168
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
169

170
            const auto g = Botan::EC_AffinePoint::generator(group);
28✔
171

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

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

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

182
            const auto g2 = g.add(g);
28✔
183

184
            const auto id = Botan::EC_AffinePoint::identity(group);
28✔
185

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

194
            results.push_back(result);
28✔
195
         }
28✔
196

197
         return results;
1✔
198
      }
×
199
};
200

201
BOTAN_REGISTER_TEST("pubkey", "ecc_mul2_inf", ECC_Mul2_Inf_Tests);
202

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

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

211
            const auto group = Botan::EC_Group::from_name(group_id);
28✔
212

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

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

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

223
            const auto g_bytes = g.serialize_uncompressed();
28✔
224

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

229
            const auto nz = z.negate();
28✔
230

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

242
            results.push_back(result);
28✔
243
         }
56✔
244

245
         return results;
1✔
246
      }
×
247
};
248

249
BOTAN_REGISTER_TEST("pubkey", "ecc_pt_addition", ECC_Point_Addition_Tests);
250

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

256
         auto& rng = Test::rng();
1✔
257

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

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

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

276
         const size_t order_bytes = group.get_order_bytes();
28✔
277

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

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

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

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

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

298
         constexpr size_t test_iter = 128;
28✔
299

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

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

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

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

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

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

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

329
            auto a_plus_b = a + b;
3,584✔
330
            result.test_eq("(a + b) - b == a", (a_plus_b - b).serialize(), a.serialize());
10,752✔
331
            result.test_eq("(a + b) - a == b", (a_plus_b - a).serialize(), b.serialize());
10,752✔
332
            result.test_eq("b - (a + b) == -a", (b - a_plus_b).serialize(), a.negate().serialize());
14,336✔
333
            result.test_eq("a - (a + b) == -b", (a - a_plus_b).serialize(), b.negate().serialize());
14,336✔
334
         }
3,584✔
335

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

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

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

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

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

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

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

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

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

366
            rng.randomize(r);
3,584✔
367

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

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

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

375
         result.end_timer();
28✔
376
      }
56✔
377
};
378

379
BOTAN_REGISTER_TEST("pubkey", "ecc_scalar_arith", ECC_Scalar_Arithmetic_Tests);
380

381
#endif
382

383
}  // namespace
384

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