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

randombit / botan / 13257645065

11 Feb 2025 07:10AM UTC coverage: 91.65% (-0.009%) from 91.659%
13257645065

Pull #4647

github

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

94860 of 103502 relevant lines covered (91.65%)

11460675.71 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,526✔
14
   const auto* p = dynamic_cast<const EC_Scalar_Data_PC*>(&data);
159,526✔
15
   if(!p) {
159,526✔
16
      throw Invalid_State("Failed conversion to EC_Scalar_Data_PC");
×
17
   }
18
   return *p;
159,526✔
19
}
20

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

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

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

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

38
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
1,672✔
39
   auto& pcurve = group()->pcurve();
1,672✔
40
   return pcurve.scalar_equal(m_v, checked_ref(other).m_v);
1,672✔
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,423✔
53
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_negate(m_v));
7,423✔
54
}
55

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

60
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::invert_vartime() const {
19,231✔
61
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_invert_vartime(m_v));
19,231✔
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,487✔
69
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_sub(m_v, checked_ref(other).m_v));
6,487✔
70
}
71

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

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

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

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

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

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

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

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

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

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

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

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

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

138
void EC_AffinePoint_Data_PC::serialize_x_to(std::span<uint8_t> bytes) const {
8,626✔
139
   BOTAN_STATE_CHECK(!this->is_identity());
8,626✔
140
   const size_t fe_bytes = this->field_element_bytes();
8,626✔
141
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
8,626✔
142
   copy_mem(bytes, std::span{m_xy}.subspan(1, fe_bytes));
8,626✔
143
}
8,626✔
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,203✔
160
   BOTAN_STATE_CHECK(!this->is_identity());
8,203✔
161
   const size_t fe_bytes = this->field_element_bytes();
8,203✔
162
   BOTAN_ARG_CHECK(bytes.size() == 1 + fe_bytes, "Invalid output size");
8,203✔
163
   const bool y_is_odd = (m_xy.back() & 0x01) == 0x01;
8,203✔
164

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

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

177
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
178
EC_Point EC_AffinePoint_Data_PC::to_legacy_point() const {
21,871✔
179
   if(this->is_identity()) {
21,871✔
180
      return EC_Point(m_group->curve());
96✔
181
   } else {
182
      const size_t fe_bytes = this->field_element_bytes();
21,775✔
183
      return EC_Point(m_group->curve(),
21,775✔
184
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
43,550✔
185
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
65,325✔
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,098✔
199
                                                                        const EC_Scalar_Data& yd) const {
200
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
1,098✔
201

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

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

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

214
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
17,622✔
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,622✔
218

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

223
   return m_group->pcurve().mul2_vartime_x_mod_order_eq(*m_tbl, v.value(), x.value(), y.value());
17,622✔
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