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

randombit / botan / 13258120944

11 Feb 2025 07:48AM UTC coverage: 91.661% (+0.002%) from 91.659%
13258120944

Pull #4647

github

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

94869 of 103500 relevant lines covered (91.66%)

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

21
const std::shared_ptr<const EC_Group_Data>& EC_Scalar_Data_PC::group() const {
104,893✔
22
   return m_group;
41,848✔
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,848✔
30
   return std::make_unique<EC_Scalar_Data_PC>(this->group(), this->value());
41,848✔
31
}
32

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

38
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
1,663✔
39
   auto& pcurve = group()->pcurve();
1,663✔
40
   return pcurve.scalar_equal(m_v, checked_ref(other).m_v);
1,663✔
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,938✔
57
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_invert(m_v));
6,938✔
58
}
59

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

72
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::mul(const EC_Scalar_Data& other) const {
60,225✔
73
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_mul(m_v, checked_ref(other).m_v));
60,225✔
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,645✔
82
                                               PCurve::PrimeOrderCurve::AffinePoint pt) :
71,645✔
83
      m_group(std::move(group)), m_pt(std::move(pt)) {
71,645✔
84
   auto& pcurve = m_group->pcurve();
71,645✔
85

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

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

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

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

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

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

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

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

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

195
   m_tbl = m_group->pcurve().mul2_setup_g(pt_q.value());
11,171✔
196
}
11,171✔
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,596✔
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,596✔
218

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

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