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

randombit / botan / 13230468750

09 Feb 2025 11:07PM UTC coverage: 91.651% (-0.008%) from 91.659%
13230468750

Pull #4647

github

web-flow
Merge 8b67be1f1 into 7deaa69bb
Pull Request #4647: Avoid using mem_ops.h or assert.h in public headers

94826 of 103464 relevant lines covered (91.65%)

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

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

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

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

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

38
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
1,682✔
39
   auto& pcurve = group()->pcurve();
1,682✔
40
   return pcurve.scalar_equal(m_v, checked_ref(other).m_v);
1,682✔
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,192✔
48
   // TODO square in place
49
   m_v = m_group->pcurve().scalar_square(m_v);
3,192✔
50
}
3,192✔
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,956✔
57
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_invert(m_v));
6,956✔
58
}
59

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

64
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::add(const EC_Scalar_Data& other) const {
8,471✔
65
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_add(m_v, checked_ref(other).m_v));
8,471✔
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,282✔
73
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_mul(m_v, checked_ref(other).m_v));
60,282✔
74
}
75

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

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

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

92
const EC_AffinePoint_Data_PC& EC_AffinePoint_Data_PC::checked_ref(const EC_AffinePoint_Data& data) {
32,784✔
93
   const auto* p = dynamic_cast<const EC_AffinePoint_Data_PC*>(&data);
32,784✔
94
   if(!p) {
32,784✔
95
      throw Invalid_State("Failed conversion to EC_AffinePoint_Data_PC");
×
96
   }
97
   return *p;
32,784✔
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,582✔
105
   return m_group;
49,582✔
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,577✔
131
   return m_group->pcurve().field_element_bytes();
149,577✔
132
}
133

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

138
void EC_AffinePoint_Data_PC::serialize_x_to(std::span<uint8_t> bytes) const {
8,630✔
139
   BOTAN_STATE_CHECK(!this->is_identity());
8,630✔
140
   const size_t fe_bytes = this->field_element_bytes();
8,630✔
141
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
8,630✔
142
   copy_mem(bytes, std::span{m_xy}.subspan(1, fe_bytes));
8,630✔
143
}
8,630✔
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,412✔
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,866✔
179
   if(this->is_identity()) {
21,866✔
180
      return EC_Point(m_group->curve());
96✔
181
   } else {
182
      const size_t fe_bytes = this->field_element_bytes();
21,770✔
183
      return EC_Point(m_group->curve(),
21,770✔
184
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
43,540✔
185
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
65,310✔
186
   }
187
}
188
#endif
189

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

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

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

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

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

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

207
   if(auto pt = pcurve.mul2_vartime(*m_tbl, x.value(), y.value())) {
1,102✔
208
      return std::make_unique<EC_AffinePoint_Data_PC>(m_group, pcurve.point_to_affine(*pt));
1,102✔
209
   } else {
210
      return nullptr;
×
211
   }
1,102✔
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