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

randombit / botan / 13192903634

07 Feb 2025 01:53AM UTC coverage: 91.278% (+0.02%) from 91.255%
13192903634

push

github

web-flow
Merge pull request #4634 from randombit/jack/min-pcurves-surface

Remove unused pcurves interfaces

94433 of 103457 relevant lines covered (91.28%)

11248924.26 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
namespace Botan {
10

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

19
const std::shared_ptr<const EC_Group_Data>& EC_Scalar_Data_PC::group() const {
105,004✔
20
   return m_group;
41,909✔
21
}
22

23
size_t EC_Scalar_Data_PC::bytes() const {
47,323✔
24
   return this->group()->order_bytes();
47,323✔
25
}
26

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

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

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

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

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

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

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

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

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

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

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

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

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

84
   if(!pcurve.affine_point_is_identity(m_pt)) {
71,640✔
85
      m_xy.resize(1 + 2 * field_element_bytes());
71,424✔
86
      pcurve.serialize_point(m_xy, m_pt);
71,424✔
87
   }
88
}
71,640✔
89

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

98
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::clone() const {
11,698✔
99
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, m_pt);
11,698✔
100
}
101

102
const std::shared_ptr<const EC_Group_Data>& EC_AffinePoint_Data_PC::group() const {
49,585✔
103
   return m_group;
49,585✔
104
}
105

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

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

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

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

128
size_t EC_AffinePoint_Data_PC::field_element_bytes() const {
149,554✔
129
   return m_group->pcurve().field_element_bytes();
149,554✔
130
}
131

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

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

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

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

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

163
   BufferStuffer stuffer(bytes);
8,204✔
164
   stuffer.append(y_is_odd ? 0x03 : 0x02);
12,435✔
165
   this->serialize_x_to(stuffer.next(fe_bytes));
8,204✔
166
}
8,204✔
167

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

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

188
EC_Mul2Table_Data_PC::EC_Mul2Table_Data_PC(const EC_AffinePoint_Data& q) : m_group(q.group()) {
11,173✔
189
   BOTAN_ARG_CHECK(q.group() == m_group, "Curve mismatch");
11,173✔
190

191
   const auto& pt_q = EC_AffinePoint_Data_PC::checked_ref(q);
11,173✔
192

193
   m_tbl = m_group->pcurve().mul2_setup_g(pt_q.value());
11,173✔
194
}
11,173✔
195

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

200
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
1,100✔
201
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
1,100✔
202

203
   auto& pcurve = m_group->pcurve();
1,100✔
204

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

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

217
   const auto& v = EC_Scalar_Data_PC::checked_ref(vd);
17,627✔
218
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
17,627✔
219
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
17,627✔
220

221
   return m_group->pcurve().mul2_vartime_x_mod_order_eq(*m_tbl, v.value(), x.value(), y.value());
17,627✔
222
}
223

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