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

randombit / botan / 13456189926

21 Feb 2025 11:50AM UTC coverage: 91.663%. Remained the same
13456189926

Pull #4555

github

web-flow
Merge b5264ec68 into 7295027f5
Pull Request #4555: Remove the workspace argument to various ECC interfaces

95007 of 103648 relevant lines covered (91.66%)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

159
   BufferStuffer stuffer(bytes);
8,206✔
160
   stuffer.append(y_is_odd ? 0x03 : 0x02);
12,413✔
161
   this->serialize_x_to(stuffer.next(fe_bytes));
8,206✔
162
}
8,206✔
163

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

171
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
172
EC_Point EC_AffinePoint_Data_PC::to_legacy_point() const {
21,876✔
173
   if(this->is_identity()) {
21,876✔
174
      return EC_Point(m_group->curve());
96✔
175
   } else {
176
      const size_t fe_bytes = this->field_element_bytes();
21,780✔
177
      return EC_Point(m_group->curve(),
21,780✔
178
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
43,560✔
179
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
65,340✔
180
   }
181
}
182
#endif
183

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

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

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

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

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

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

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

208
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
17,635✔
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");
17,635✔
212

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

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