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

randombit / botan / 13262741994

11 Feb 2025 12:19PM UTC coverage: 91.656% (-0.003%) from 91.659%
13262741994

Pull #4647

github

web-flow
Merge 0b8e56724 into f372b5a9e
Pull Request #4647: Avoid using mem_ops.h or assert.h in public headers

94864 of 103500 relevant lines covered (91.66%)

11330304.66 hits per line

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

95.49
/src/lib/pubkey/ec_group/ec_inner_pc.cpp
1
/*
2
* (C) 2024 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include <botan/internal/ec_inner_pc.h>
8

9
#include <botan/mem_ops.h>
10

11
namespace Botan {
12

13
const EC_Scalar_Data_PC& EC_Scalar_Data_PC::checked_ref(const EC_Scalar_Data& data) {
159,577✔
14
   const auto* p = dynamic_cast<const EC_Scalar_Data_PC*>(&data);
159,577✔
15
   if(!p) {
159,577✔
16
      throw Invalid_State("Failed conversion to EC_Scalar_Data_PC");
×
17
   }
18
   return *p;
159,577✔
19
}
20

21
const std::shared_ptr<const EC_Group_Data>& EC_Scalar_Data_PC::group() const {
105,013✔
22
   return m_group;
41,906✔
23
}
24

25
size_t EC_Scalar_Data_PC::bytes() const {
47,337✔
26
   return this->group()->order_bytes();
47,337✔
27
}
28

29
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::clone() const {
41,906✔
30
   return std::make_unique<EC_Scalar_Data_PC>(this->group(), this->value());
41,906✔
31
}
32

33
bool EC_Scalar_Data_PC::is_zero() const {
39,701✔
34
   auto& pcurve = this->group()->pcurve();
39,701✔
35
   return pcurve.scalar_is_zero(m_v);
39,701✔
36
}
37

38
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
1,697✔
39
   auto& pcurve = group()->pcurve();
1,697✔
40
   return pcurve.scalar_equal(m_v, checked_ref(other).m_v);
1,697✔
41
}
42

43
void EC_Scalar_Data_PC::assign(const EC_Scalar_Data& other) {
×
44
   m_v = checked_ref(other).value();
×
45
}
×
46

47
void EC_Scalar_Data_PC::square_self() {
3,194✔
48
   // TODO square in place
49
   m_v = m_group->pcurve().scalar_square(m_v);
3,194✔
50
}
3,194✔
51

52
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::negate() const {
7,425✔
53
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_negate(m_v));
7,425✔
54
}
55

56
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::invert() const {
6,973✔
57
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_invert(m_v));
6,973✔
58
}
59

60
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::invert_vartime() const {
19,260✔
61
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_invert_vartime(m_v));
19,260✔
62
}
63

64
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::add(const EC_Scalar_Data& other) const {
8,472✔
65
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_add(m_v, checked_ref(other).m_v));
8,472✔
66
}
67

68
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::sub(const EC_Scalar_Data& other) const {
6,488✔
69
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_sub(m_v, checked_ref(other).m_v));
6,488✔
70
}
71

72
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::mul(const EC_Scalar_Data& other) const {
60,291✔
73
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_mul(m_v, checked_ref(other).m_v));
60,291✔
74
}
75

76
void EC_Scalar_Data_PC::serialize_to(std::span<uint8_t> bytes) const {
48,202✔
77
   BOTAN_ARG_CHECK(bytes.size() == m_group->order_bytes(), "Invalid output length");
48,202✔
78
   m_group->pcurve().serialize_scalar(bytes, m_v);
48,202✔
79
}
48,202✔
80

81
EC_AffinePoint_Data_PC::EC_AffinePoint_Data_PC(std::shared_ptr<const EC_Group_Data> group,
71,654✔
82
                                               PCurve::PrimeOrderCurve::AffinePoint pt) :
71,654✔
83
      m_group(std::move(group)), m_pt(std::move(pt)) {
71,654✔
84
   auto& pcurve = m_group->pcurve();
71,654✔
85

86
   if(!pcurve.affine_point_is_identity(m_pt)) {
71,654✔
87
      m_xy.resize(1 + 2 * field_element_bytes());
71,438✔
88
      pcurve.serialize_point(m_xy, m_pt);
71,438✔
89
   }
90
}
71,654✔
91

92
const EC_AffinePoint_Data_PC& EC_AffinePoint_Data_PC::checked_ref(const EC_AffinePoint_Data& data) {
32,788✔
93
   const auto* p = dynamic_cast<const EC_AffinePoint_Data_PC*>(&data);
32,788✔
94
   if(!p) {
32,788✔
95
      throw Invalid_State("Failed conversion to EC_AffinePoint_Data_PC");
×
96
   }
97
   return *p;
32,788✔
98
}
99

100
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::clone() const {
11,705✔
101
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, m_pt);
11,705✔
102
}
103

104
const std::shared_ptr<const EC_Group_Data>& EC_AffinePoint_Data_PC::group() const {
49,594✔
105
   return m_group;
49,594✔
106
}
107

108
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::mul(const EC_Scalar_Data& scalar,
10,021✔
109
                                                                 RandomNumberGenerator& rng,
110
                                                                 std::vector<BigInt>& ws) const {
111
   BOTAN_UNUSED(ws);
10,021✔
112

113
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
10,021✔
114
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
10,021✔
115
   auto& pcurve = m_group->pcurve();
10,021✔
116
   auto pt = pcurve.point_to_affine(pcurve.mul(m_pt, k, rng));
10,021✔
117
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, std::move(pt));
10,021✔
118
}
10,021✔
119

120
secure_vector<uint8_t> EC_AffinePoint_Data_PC::mul_x_only(const EC_Scalar_Data& scalar,
2,651✔
121
                                                          RandomNumberGenerator& rng,
122
                                                          std::vector<BigInt>& ws) const {
123
   BOTAN_UNUSED(ws);
2,651✔
124

125
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
2,651✔
126
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
2,651✔
127
   return m_group->pcurve().mul_x_only(m_pt, k, rng);
2,651✔
128
}
129

130
size_t EC_AffinePoint_Data_PC::field_element_bytes() const {
149,584✔
131
   return m_group->pcurve().field_element_bytes();
149,584✔
132
}
133

134
bool EC_AffinePoint_Data_PC::is_identity() const {
80,522✔
135
   return m_xy.empty();
80,522✔
136
}
137

138
void EC_AffinePoint_Data_PC::serialize_x_to(std::span<uint8_t> bytes) const {
8,629✔
139
   BOTAN_STATE_CHECK(!this->is_identity());
8,629✔
140
   const size_t fe_bytes = this->field_element_bytes();
8,629✔
141
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
8,629✔
142
   copy_mem(bytes, std::span{m_xy}.subspan(1, fe_bytes));
8,629✔
143
}
8,629✔
144

145
void EC_AffinePoint_Data_PC::serialize_y_to(std::span<uint8_t> bytes) const {
20✔
146
   BOTAN_STATE_CHECK(!this->is_identity());
20✔
147
   const size_t fe_bytes = this->field_element_bytes();
20✔
148
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
20✔
149
   copy_mem(bytes, std::span{m_xy}.subspan(1 + fe_bytes, fe_bytes));
20✔
150
}
20✔
151

152
void EC_AffinePoint_Data_PC::serialize_xy_to(std::span<uint8_t> bytes) const {
150✔
153
   BOTAN_STATE_CHECK(!this->is_identity());
150✔
154
   const size_t fe_bytes = this->field_element_bytes();
150✔
155
   BOTAN_ARG_CHECK(bytes.size() == 2 * fe_bytes, "Invalid output size");
150✔
156
   copy_mem(bytes, std::span{m_xy}.last(2 * fe_bytes));
150✔
157
}
150✔
158

159
void EC_AffinePoint_Data_PC::serialize_compressed_to(std::span<uint8_t> bytes) const {
8,206✔
160
   BOTAN_STATE_CHECK(!this->is_identity());
8,206✔
161
   const size_t fe_bytes = this->field_element_bytes();
8,206✔
162
   BOTAN_ARG_CHECK(bytes.size() == 1 + fe_bytes, "Invalid output size");
8,206✔
163
   const bool y_is_odd = (m_xy.back() & 0x01) == 0x01;
8,206✔
164

165
   BufferStuffer stuffer(bytes);
8,206✔
166
   stuffer.append(y_is_odd ? 0x03 : 0x02);
12,402✔
167
   this->serialize_x_to(stuffer.next(fe_bytes));
8,206✔
168
}
8,206✔
169

170
void EC_AffinePoint_Data_PC::serialize_uncompressed_to(std::span<uint8_t> bytes) const {
15,291✔
171
   BOTAN_STATE_CHECK(!this->is_identity());
15,291✔
172
   const size_t fe_bytes = this->field_element_bytes();
15,291✔
173
   BOTAN_ARG_CHECK(bytes.size() == 1 + 2 * fe_bytes, "Invalid output size");
15,291✔
174
   copy_mem(bytes, m_xy);
15,291✔
175
}
15,291✔
176

177
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
178
EC_Point EC_AffinePoint_Data_PC::to_legacy_point() const {
21,870✔
179
   if(this->is_identity()) {
21,870✔
180
      return EC_Point(m_group->curve());
96✔
181
   } else {
182
      const size_t fe_bytes = this->field_element_bytes();
21,774✔
183
      return EC_Point(m_group->curve(),
21,774✔
184
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
43,548✔
185
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
65,322✔
186
   }
187
}
188
#endif
189

190
EC_Mul2Table_Data_PC::EC_Mul2Table_Data_PC(const EC_AffinePoint_Data& q) : m_group(q.group()) {
11,176✔
191
   BOTAN_ARG_CHECK(q.group() == m_group, "Curve mismatch");
11,176✔
192

193
   const auto& pt_q = EC_AffinePoint_Data_PC::checked_ref(q);
11,176✔
194

195
   m_tbl = m_group->pcurve().mul2_setup_g(pt_q.value());
11,176✔
196
}
11,176✔
197

198
std::unique_ptr<EC_AffinePoint_Data> EC_Mul2Table_Data_PC::mul2_vartime(const EC_Scalar_Data& xd,
1,101✔
199
                                                                        const EC_Scalar_Data& yd) const {
200
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
1,101✔
201

202
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
1,101✔
203
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
1,101✔
204

205
   auto& pcurve = m_group->pcurve();
1,101✔
206

207
   if(auto pt = pcurve.mul2_vartime(*m_tbl, x.value(), y.value())) {
1,101✔
208
      return std::make_unique<EC_AffinePoint_Data_PC>(m_group, pcurve.point_to_affine(*pt));
1,101✔
209
   } else {
210
      return nullptr;
×
211
   }
1,101✔
212
}
213

214
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
17,624✔
215
                                                       const EC_Scalar_Data& xd,
216
                                                       const EC_Scalar_Data& yd) const {
217
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
17,624✔
218

219
   const auto& v = EC_Scalar_Data_PC::checked_ref(vd);
17,624✔
220
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
17,624✔
221
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
17,624✔
222

223
   return m_group->pcurve().mul2_vartime_x_mod_order_eq(*m_tbl, v.value(), x.value(), y.value());
17,624✔
224
}
225

226
}  // namespace Botan
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