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

randombit / botan / 12735159981

12 Jan 2025 04:17PM UTC coverage: 91.245% (+0.008%) from 91.237%
12735159981

push

github

web-flow
Merge pull request #4542 from randombit/jack/pcurves-misc

Add EC_Scalar arithmetic test

93526 of 102500 relevant lines covered (91.24%)

11378819.7 hits per line

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

94.42
/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
         std::vector<Botan::BigInt> ws;
533✔
35

36
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
37
         const auto pt = group.OS2ECP(P_bytes);
533✔
38

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

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

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

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

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

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

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

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

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

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

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

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

78
         std::vector<Botan::BigInt> ws;
829✔
79

80
   #if defined(BOTAN_HAS_LEGACY_EC_POINT)
81
         const Botan::EC_Point pt = group.OS2ECP(p);
829✔
82

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

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

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

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

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

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

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

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

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

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

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

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

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

136
            // Now check the same using naive multiply and add:
137
            std::vector<BigInt> ws;
3,276✔
138
            auto z = p.mul(x, rng(), ws).add(q.mul(y, rng(), ws));
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
            std::vector<Botan::BigInt> ws;
28✔
194
            const auto z = g.mul(Botan::EC_Scalar::random(group, rng()), rng(), ws);
28✔
195

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

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

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

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

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

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

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

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

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

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

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

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

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

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

245
            const auto g_bytes = g.serialize_uncompressed();
28✔
246

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

251
            const auto nz = z.negate();
28✔
252

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

264
            results.push_back(result);
28✔
265
         }
56✔
266

267
         return results;
1✔
268
      }
×
269
};
270

271
BOTAN_REGISTER_TEST("pubkey", "ecc_pt_addition", ECC_Point_Addition_Tests);
272

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

278
         auto& rng = Test::rng();
1✔
279

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

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

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

298
         const size_t order_bytes = group.get_order_bytes();
28✔
299

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

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

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

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

316
         // Zero inverse is not mathematically correct, but works out for our purposes
317
         result.test_eq("Inverse of zero is zero", zero.invert().serialize(), ser_zero);
84✔
318
         result.test_eq("Inverse of one is one", one.invert().serialize(), ser_one);
84✔
319

320
         constexpr size_t test_iter = 128;
28✔
321

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

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

328
            // Serialization and deserialization are inverses
329
            const auto r_bytes = r.serialize();
3,584✔
330
            result.test_eq("Deserialization of r round trips",
10,752✔
331
                           Botan::EC_Scalar::deserialize(group, r_bytes).value().serialize(),
10,752✔
332
                           r_bytes);
333

334
            // Multiplication and inversion are inverses
335
            const auto r2 = r * r;
3,584✔
336
            const auto r_inv = r.invert();
3,584✔
337
            result.test_eq("r * r^-1 = 1", (r * r_inv).serialize(), ser_one);
10,752✔
338
         }
7,168✔
339

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

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

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

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

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

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

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

369
         result.end_timer();
28✔
370
      }
56✔
371
};
372

373
BOTAN_REGISTER_TEST("pubkey", "ecc_scalar_arith", ECC_Scalar_Arithmetic_Tests);
374

375
#endif
376

377
}  // namespace
378

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