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

randombit / botan / 12787475777

15 Jan 2025 11:35AM UTC coverage: 91.198% (+0.001%) from 91.197%
12787475777

Pull #4555

github

web-flow
Merge 8704987c7 into 1afcdf2cd
Pull Request #4555: Remove the workspace argument to various ECC interfaces

93407 of 102422 relevant lines covered (91.2%)

11406983.13 hits per line

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

85.51
/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
namespace {
12

13
PCurve::PrimeOrderCurve::AffinePoint deserialize_pcurve_pt(const PCurve::PrimeOrderCurve& curve,
×
14
                                                           std::span<const uint8_t> bytes) {
15
   if(auto pt = curve.deserialize_point(bytes)) {
×
16
      return *pt;
×
17
   } else {
18
      throw Decoding_Error("Invalid elliptic curve point encoding");
×
19
   }
×
20
}
21

22
}  // namespace
23

24
const EC_Scalar_Data_PC& EC_Scalar_Data_PC::checked_ref(const EC_Scalar_Data& data) {
159,649✔
25
   const auto* p = dynamic_cast<const EC_Scalar_Data_PC*>(&data);
159,649✔
26
   if(!p) {
159,649✔
27
      throw Invalid_State("Failed conversion to EC_Scalar_Data_PC");
×
28
   }
29
   return *p;
159,649✔
30
}
31

32
const std::shared_ptr<const EC_Group_Data>& EC_Scalar_Data_PC::group() const {
105,644✔
33
   return m_group;
40,833✔
34
}
35

36
size_t EC_Scalar_Data_PC::bytes() const {
46,027✔
37
   return this->group()->order_bytes();
46,027✔
38
}
39

40
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::clone() const {
40,833✔
41
   return std::make_unique<EC_Scalar_Data_PC>(this->group(), this->value());
40,833✔
42
}
43

44
bool EC_Scalar_Data_PC::is_zero() const {
39,745✔
45
   return this->value().is_zero();
39,745✔
46
}
47

48
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
×
49
   return (value() == checked_ref(other).value());
×
50
}
51

52
void EC_Scalar_Data_PC::assign(const EC_Scalar_Data& other) {
×
53
   m_v = checked_ref(other).value();
×
54
}
×
55

56
void EC_Scalar_Data_PC::square_self() {
3,190✔
57
   // TODO square in place
58
   m_v = m_v.square();
3,190✔
59
}
3,190✔
60

61
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::negate() const {
7,425✔
62
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v.negate());
7,425✔
63
}
64

65
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::invert() const {
24,378✔
66
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v.invert());
24,378✔
67
}
68

69
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::add(const EC_Scalar_Data& other) const {
8,470✔
70
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v + checked_ref(other).value());
8,470✔
71
}
72

73
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::sub(const EC_Scalar_Data& other) const {
6,483✔
74
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v - checked_ref(other).value());
6,483✔
75
}
76

77
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::mul(const EC_Scalar_Data& other) const {
60,325✔
78
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v * checked_ref(other).value());
60,325✔
79
}
80

81
void EC_Scalar_Data_PC::serialize_to(std::span<uint8_t> bytes) const {
46,896✔
82
   BOTAN_ARG_CHECK(bytes.size() == m_group->order_bytes(), "Invalid output length");
46,896✔
83
   m_group->pcurve().serialize_scalar(bytes, m_v);
46,896✔
84
}
46,896✔
85

86
EC_AffinePoint_Data_PC::EC_AffinePoint_Data_PC(std::shared_ptr<const EC_Group_Data> group,
81,954✔
87
                                               PCurve::PrimeOrderCurve::AffinePoint pt) :
81,954✔
88
      m_group(std::move(group)), m_pt(std::move(pt)) {
81,954✔
89
   if(!m_pt.is_identity()) {
81,954✔
90
      m_xy = m_pt.serialize<secure_vector<uint8_t>>();
81,738✔
91
      BOTAN_ASSERT_NOMSG(m_xy.size() == 1 + 2 * field_element_bytes());
81,738✔
92
   }
93
}
81,954✔
94

95
EC_AffinePoint_Data_PC::EC_AffinePoint_Data_PC(std::shared_ptr<const EC_Group_Data> group,
×
96
                                               std::span<const uint8_t> bytes) :
×
97
      m_group(std::move(group)), m_pt(deserialize_pcurve_pt(m_group->pcurve(), bytes)) {
×
98
   if(!m_pt.is_identity()) {
×
99
      m_xy = m_pt.serialize<secure_vector<uint8_t>>();
×
100
      BOTAN_ASSERT_NOMSG(m_xy.size() == 1 + 2 * field_element_bytes());
×
101
   }
102
}
×
103

104
const EC_AffinePoint_Data_PC& EC_AffinePoint_Data_PC::checked_ref(const EC_AffinePoint_Data& data) {
42,762✔
105
   const auto* p = dynamic_cast<const EC_AffinePoint_Data_PC*>(&data);
42,762✔
106
   if(!p) {
42,762✔
107
      throw Invalid_State("Failed conversion to EC_AffinePoint_Data_PC");
×
108
   }
109
   return *p;
42,762✔
110
}
111

112
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::clone() const {
11,539✔
113
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, m_pt);
11,539✔
114
}
115

116
const std::shared_ptr<const EC_Group_Data>& EC_AffinePoint_Data_PC::group() const {
48,395✔
117
   return m_group;
48,395✔
118
}
119

120
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::mul(const EC_Scalar_Data& scalar,
11,183✔
121
                                                                 RandomNumberGenerator& rng) const {
122
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
11,183✔
123
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
11,183✔
124
   auto pt = m_group->pcurve().mul(m_pt, k, rng).to_affine();
22,366✔
125
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, std::move(pt));
11,183✔
126
}
11,183✔
127

128
secure_vector<uint8_t> EC_AffinePoint_Data_PC::mul_x_only(const EC_Scalar_Data& scalar,
2,649✔
129
                                                          RandomNumberGenerator& rng) const {
130
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
2,649✔
131
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
2,649✔
132
   return m_group->pcurve().mul_x_only(m_pt, k, rng);
2,649✔
133
}
134

135
size_t EC_AffinePoint_Data_PC::field_element_bytes() const {
155,365✔
136
   return m_group->pcurve().field_element_bytes();
155,365✔
137
}
138

139
bool EC_AffinePoint_Data_PC::is_identity() const {
73,899✔
140
   return m_xy.empty();
73,899✔
141
}
142

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

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

157
void EC_AffinePoint_Data_PC::serialize_xy_to(std::span<uint8_t> bytes) const {
150✔
158
   BOTAN_STATE_CHECK(!this->is_identity());
150✔
159
   const size_t fe_bytes = this->field_element_bytes();
150✔
160
   BOTAN_ARG_CHECK(bytes.size() == 2 * fe_bytes, "Invalid output size");
150✔
161
   copy_mem(bytes, std::span{m_xy}.last(2 * fe_bytes));
150✔
162
}
150✔
163

164
void EC_AffinePoint_Data_PC::serialize_compressed_to(std::span<uint8_t> bytes) const {
8,194✔
165
   BOTAN_STATE_CHECK(!this->is_identity());
8,194✔
166
   const size_t fe_bytes = this->field_element_bytes();
8,194✔
167
   BOTAN_ARG_CHECK(bytes.size() == 1 + fe_bytes, "Invalid output size");
8,194✔
168
   const bool y_is_odd = (m_xy.back() & 0x01) == 0x01;
8,194✔
169

170
   BufferStuffer stuffer(bytes);
8,194✔
171
   stuffer.append(y_is_odd ? 0x03 : 0x02);
12,410✔
172
   this->serialize_x_to(stuffer.next(fe_bytes));
8,194✔
173
}
8,194✔
174

175
void EC_AffinePoint_Data_PC::serialize_uncompressed_to(std::span<uint8_t> bytes) const {
12,864✔
176
   BOTAN_STATE_CHECK(!this->is_identity());
12,864✔
177
   const size_t fe_bytes = this->field_element_bytes();
12,864✔
178
   BOTAN_ARG_CHECK(bytes.size() == 1 + 2 * fe_bytes, "Invalid output size");
12,864✔
179
   copy_mem(bytes, m_xy);
12,864✔
180
}
12,864✔
181

182
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
183
EC_Point EC_AffinePoint_Data_PC::to_legacy_point() const {
22,243✔
184
   if(this->is_identity()) {
22,243✔
185
      return EC_Point(m_group->curve());
96✔
186
   } else {
187
      const size_t fe_bytes = this->field_element_bytes();
22,147✔
188
      return EC_Point(m_group->curve(),
22,147✔
189
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
44,294✔
190
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
66,441✔
191
   }
192
}
193
#endif
194

195
EC_Mul2Table_Data_PC::EC_Mul2Table_Data_PC(const EC_AffinePoint_Data& g, const EC_AffinePoint_Data& h) :
11,177✔
196
      m_group(g.group()) {
11,177✔
197
   BOTAN_ARG_CHECK(h.group() == m_group, "Curve mismatch");
11,177✔
198

199
   const auto& pt_g = EC_AffinePoint_Data_PC::checked_ref(g);
11,177✔
200
   const auto& pt_h = EC_AffinePoint_Data_PC::checked_ref(h);
11,177✔
201

202
   m_tbl = m_group->pcurve().mul2_setup(pt_g.value(), pt_h.value());
11,177✔
203
}
11,177✔
204

205
std::unique_ptr<EC_AffinePoint_Data> EC_Mul2Table_Data_PC::mul2_vartime(const EC_Scalar_Data& xd,
1,101✔
206
                                                                        const EC_Scalar_Data& yd) const {
207
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
1,101✔
208

209
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
1,101✔
210
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
1,101✔
211

212
   if(auto pt = m_group->pcurve().mul2_vartime(*m_tbl, x.value(), y.value())) {
1,101✔
213
      return std::make_unique<EC_AffinePoint_Data_PC>(m_group, pt->to_affine());
1,101✔
214
   } else {
215
      return nullptr;
×
216
   }
1,101✔
217
}
218

219
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
17,652✔
220
                                                       const EC_Scalar_Data& xd,
221
                                                       const EC_Scalar_Data& yd) const {
222
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
17,652✔
223

224
   const auto& v = EC_Scalar_Data_PC::checked_ref(vd);
17,652✔
225
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
17,652✔
226
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
17,652✔
227

228
   return m_group->pcurve().mul2_vartime_x_mod_order_eq(*m_tbl, v.value(), x.value(), y.value());
17,652✔
229
}
230

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