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

randombit / botan / 13474418621

22 Feb 2025 03:12PM UTC coverage: 91.687% (-0.004%) from 91.691%
13474418621

push

github

web-flow
Merge pull request #4555 from randombit/jack/remove-ws-arg

Remove the workspace argument to various ECC interfaces

95796 of 104482 relevant lines covered (91.69%)

11191920.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

21
const std::shared_ptr<const EC_Group_Data>& EC_Scalar_Data_PC::group() const {
141,349✔
22
   return m_group;
53,927✔
23
}
24

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

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

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

38
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
3,220✔
39
   auto& pcurve = group()->pcurve();
3,220✔
40
   return pcurve.scalar_equal(m_v, checked_ref(other).m_v);
3,220✔
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() {
5,522✔
48
   // TODO square in place
49
   m_v = m_group->pcurve().scalar_square(m_v);
5,522✔
50
}
5,522✔
51

52
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::negate() const {
12,723✔
53
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_negate(m_v));
12,723✔
54
}
55

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

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

64
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::add(const EC_Scalar_Data& other) const {
16,205✔
65
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_add(m_v, checked_ref(other).m_v));
16,205✔
66
}
67

68
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::sub(const EC_Scalar_Data& other) const {
12,764✔
69
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_sub(m_v, checked_ref(other).m_v));
12,764✔
70
}
71

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

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

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

86
   if(!pcurve.affine_point_is_identity(m_pt)) {
95,233✔
87
      m_xy.resize(1 + 2 * field_element_bytes());
94,800✔
88
      pcurve.serialize_point(m_xy, m_pt);
94,800✔
89
   }
90
}
95,233✔
91

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

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

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

108
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::mul(const EC_Scalar_Data& scalar,
13,120✔
109
                                                                 RandomNumberGenerator& rng) const {
110
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
13,120✔
111
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
13,120✔
112
   auto& pcurve = m_group->pcurve();
13,120✔
113
   auto pt = pcurve.point_to_affine(pcurve.mul(m_pt, k, rng));
13,120✔
114
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, std::move(pt));
13,120✔
115
}
13,120✔
116

117
secure_vector<uint8_t> EC_AffinePoint_Data_PC::mul_x_only(const EC_Scalar_Data& scalar,
5,073✔
118
                                                          RandomNumberGenerator& rng) const {
119
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
5,073✔
120
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
5,073✔
121
   return m_group->pcurve().mul_x_only(m_pt, k, rng);
5,073✔
122
}
123

124
size_t EC_AffinePoint_Data_PC::field_element_bytes() const {
207,730✔
125
   return m_group->pcurve().field_element_bytes();
207,730✔
126
}
127

128
bool EC_AffinePoint_Data_PC::is_identity() const {
115,647✔
129
   return m_xy.empty();
115,647✔
130
}
131

132
void EC_AffinePoint_Data_PC::serialize_x_to(std::span<uint8_t> bytes) const {
11,283✔
133
   BOTAN_STATE_CHECK(!this->is_identity());
11,283✔
134
   const size_t fe_bytes = this->field_element_bytes();
11,283✔
135
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
11,283✔
136
   copy_mem(bytes, std::span{m_xy}.subspan(1, fe_bytes));
11,283✔
137
}
11,283✔
138

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

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

153
void EC_AffinePoint_Data_PC::serialize_compressed_to(std::span<uint8_t> bytes) const {
10,680✔
154
   BOTAN_STATE_CHECK(!this->is_identity());
10,680✔
155
   const size_t fe_bytes = this->field_element_bytes();
10,680✔
156
   BOTAN_ARG_CHECK(bytes.size() == 1 + fe_bytes, "Invalid output size");
10,680✔
157
   const bool y_is_odd = (m_xy.back() & 0x01) == 0x01;
10,680✔
158

159
   BufferStuffer stuffer(bytes);
10,680✔
160
   stuffer.append(y_is_odd ? 0x03 : 0x02);
16,126✔
161
   this->serialize_x_to(stuffer.next(fe_bytes));
10,680✔
162
}
10,680✔
163

164
void EC_AffinePoint_Data_PC::serialize_uncompressed_to(std::span<uint8_t> bytes) const {
26,561✔
165
   BOTAN_STATE_CHECK(!this->is_identity());
26,561✔
166
   const size_t fe_bytes = this->field_element_bytes();
26,561✔
167
   BOTAN_ARG_CHECK(bytes.size() == 1 + 2 * fe_bytes, "Invalid output size");
26,561✔
168
   copy_mem(bytes, m_xy);
26,561✔
169
}
26,561✔
170

171
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
172
EC_Point EC_AffinePoint_Data_PC::to_legacy_point() const {
26,147✔
173
   if(this->is_identity()) {
26,147✔
174
      return EC_Point(m_group->curve());
193✔
175
   } else {
176
      const size_t fe_bytes = this->field_element_bytes();
25,954✔
177
      return EC_Point(m_group->curve(),
25,954✔
178
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
51,908✔
179
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
77,862✔
180
   }
181
}
182
#endif
183

184
EC_Mul2Table_Data_PC::EC_Mul2Table_Data_PC(const EC_AffinePoint_Data& q) : m_group(q.group()) {
12,952✔
185
   BOTAN_ARG_CHECK(q.group() == m_group, "Curve mismatch");
12,952✔
186

187
   const auto& pt_q = EC_AffinePoint_Data_PC::checked_ref(q);
12,952✔
188

189
   m_tbl = m_group->pcurve().mul2_setup_g(pt_q.value());
12,952✔
190
}
12,952✔
191

192
std::unique_ptr<EC_AffinePoint_Data> EC_Mul2Table_Data_PC::mul2_vartime(const EC_Scalar_Data& xd,
1,869✔
193
                                                                        const EC_Scalar_Data& yd) const {
194
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
1,869✔
195

196
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
1,869✔
197
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
1,869✔
198

199
   auto& pcurve = m_group->pcurve();
1,869✔
200

201
   if(auto pt = pcurve.mul2_vartime(*m_tbl, x.value(), y.value())) {
1,869✔
202
      return std::make_unique<EC_AffinePoint_Data_PC>(m_group, pcurve.point_to_affine(*pt));
1,869✔
203
   } else {
204
      return nullptr;
×
205
   }
1,869✔
206
}
207

208
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
21,662✔
209
                                                       const EC_Scalar_Data& xd,
210
                                                       const EC_Scalar_Data& yd) const {
211
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
21,662✔
212

213
   const auto& v = EC_Scalar_Data_PC::checked_ref(vd);
21,662✔
214
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
21,662✔
215
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
21,662✔
216

217
   return m_group->pcurve().mul2_vartime_x_mod_order_eq(*m_tbl, v.value(), x.value(), y.value());
21,662✔
218
}
219

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