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

randombit / botan / 13191591288

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

push

github

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

Remove unused pcurves interfaces

94430 of 103457 relevant lines covered (91.27%)

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

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

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

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

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

36
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
1,671✔
37
   auto& pcurve = group()->pcurve();
1,671✔
38
   return pcurve.scalar_equal(m_v, checked_ref(other).m_v);
1,671✔
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,194✔
46
   // TODO square in place
47
   m_v = m_group->pcurve().scalar_square(m_v);
3,194✔
48
}
3,194✔
49

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

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

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

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

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

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

74
void EC_Scalar_Data_PC::serialize_to(std::span<uint8_t> bytes) const {
48,200✔
75
   BOTAN_ARG_CHECK(bytes.size() == m_group->order_bytes(), "Invalid output length");
48,200✔
76
   m_group->pcurve().serialize_scalar(bytes, m_v);
48,200✔
77
}
48,200✔
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,700✔
99
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, m_pt);
11,700✔
100
}
101

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

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

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

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

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

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

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

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

163
   BufferStuffer stuffer(bytes);
8,205✔
164
   stuffer.append(y_is_odd ? 0x03 : 0x02);
12,369✔
165
   this->serialize_x_to(stuffer.next(fe_bytes));
8,205✔
166
}
8,205✔
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,865✔
177
   if(this->is_identity()) {
21,865✔
178
      return EC_Point(m_group->curve());
96✔
179
   } else {
180
      const size_t fe_bytes = this->field_element_bytes();
21,769✔
181
      return EC_Point(m_group->curve(),
21,769✔
182
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
43,538✔
183
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
65,307✔
184
   }
185
}
186
#endif
187

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

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

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

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

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

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

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

212
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
17,592✔
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,592✔
216

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

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