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

randombit / botan / 13073800651

31 Jan 2025 01:32PM UTC coverage: 91.235% (+0.01%) from 91.224%
13073800651

push

github

web-flow
Merge pull request #4618 from randombit/jack/ec-scalar-inv-vartime

Add EC_Scalar::invert_vartime

94175 of 103222 relevant lines covered (91.24%)

11300843.12 hits per line

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

87.14
/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,374✔
25
   const auto* p = dynamic_cast<const EC_Scalar_Data_PC*>(&data);
159,374✔
26
   if(!p) {
159,374✔
27
      throw Invalid_State("Failed conversion to EC_Scalar_Data_PC");
×
28
   }
29
   return *p;
159,374✔
30
}
31

32
const std::shared_ptr<const EC_Group_Data>& EC_Scalar_Data_PC::group() const {
104,985✔
33
   return m_group;
41,909✔
34
}
35

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

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

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

48
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
1,536✔
49
   return (value() == checked_ref(other).value());
1,536✔
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 {
6,811✔
66
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v.invert());
6,811✔
67
}
68

69
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::invert_vartime() const {
19,077✔
70
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v.invert_vartime());
19,077✔
71
}
72

73
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::add(const EC_Scalar_Data& other) const {
8,470✔
74
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v + checked_ref(other).value());
8,470✔
75
}
76

77
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::sub(const EC_Scalar_Data& other) const {
6,484✔
78
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v - checked_ref(other).value());
6,484✔
79
}
80

81
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::mul(const EC_Scalar_Data& other) const {
60,274✔
82
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v * checked_ref(other).value());
60,274✔
83
}
84

85
void EC_Scalar_Data_PC::serialize_to(std::span<uint8_t> bytes) const {
48,157✔
86
   BOTAN_ARG_CHECK(bytes.size() == m_group->order_bytes(), "Invalid output length");
48,157✔
87
   m_group->pcurve().serialize_scalar(bytes, m_v);
48,157✔
88
}
48,157✔
89

90
EC_AffinePoint_Data_PC::EC_AffinePoint_Data_PC(std::shared_ptr<const EC_Group_Data> group,
71,627✔
91
                                               PCurve::PrimeOrderCurve::AffinePoint pt) :
71,627✔
92
      m_group(std::move(group)), m_pt(std::move(pt)) {
71,627✔
93
   if(!m_pt.is_identity()) {
71,627✔
94
      m_xy = m_pt.serialize<secure_vector<uint8_t>>();
71,411✔
95
      BOTAN_ASSERT_NOMSG(m_xy.size() == 1 + 2 * field_element_bytes());
71,411✔
96
   }
97
}
71,627✔
98

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

108
const EC_AffinePoint_Data_PC& EC_AffinePoint_Data_PC::checked_ref(const EC_AffinePoint_Data& data) {
32,786✔
109
   const auto* p = dynamic_cast<const EC_AffinePoint_Data_PC*>(&data);
32,786✔
110
   if(!p) {
32,786✔
111
      throw Invalid_State("Failed conversion to EC_AffinePoint_Data_PC");
×
112
   }
113
   return *p;
32,786✔
114
}
115

116
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::clone() const {
11,700✔
117
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, m_pt);
11,700✔
118
}
119

120
const std::shared_ptr<const EC_Group_Data>& EC_AffinePoint_Data_PC::group() const {
49,598✔
121
   return m_group;
49,598✔
122
}
123

124
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::mul(const EC_Scalar_Data& scalar,
10,019✔
125
                                                                 RandomNumberGenerator& rng,
126
                                                                 std::vector<BigInt>& ws) const {
127
   BOTAN_UNUSED(ws);
10,019✔
128

129
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
10,019✔
130
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
10,019✔
131
   auto pt = m_group->pcurve().mul(m_pt, k, rng).to_affine();
20,038✔
132
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, std::move(pt));
10,019✔
133
}
10,019✔
134

135
secure_vector<uint8_t> EC_AffinePoint_Data_PC::mul_x_only(const EC_Scalar_Data& scalar,
2,649✔
136
                                                          RandomNumberGenerator& rng,
137
                                                          std::vector<BigInt>& ws) const {
138
   BOTAN_UNUSED(ws);
2,649✔
139

140
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
2,649✔
141
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
2,649✔
142
   return m_group->pcurve().mul_x_only(m_pt, k, rng);
2,649✔
143
}
144

145
size_t EC_AffinePoint_Data_PC::field_element_bytes() const {
149,531✔
146
   return m_group->pcurve().field_element_bytes();
149,531✔
147
}
148

149
bool EC_AffinePoint_Data_PC::is_identity() const {
78,392✔
150
   return m_xy.empty();
78,392✔
151
}
152

153
void EC_AffinePoint_Data_PC::serialize_x_to(std::span<uint8_t> bytes) const {
8,626✔
154
   BOTAN_STATE_CHECK(!this->is_identity());
8,626✔
155
   const size_t fe_bytes = this->field_element_bytes();
8,626✔
156
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
8,626✔
157
   copy_mem(bytes, std::span{m_xy}.subspan(1, fe_bytes));
8,626✔
158
}
8,626✔
159

160
void EC_AffinePoint_Data_PC::serialize_y_to(std::span<uint8_t> bytes) const {
20✔
161
   BOTAN_STATE_CHECK(!this->is_identity());
20✔
162
   const size_t fe_bytes = this->field_element_bytes();
20✔
163
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
20✔
164
   copy_mem(bytes, std::span{m_xy}.subspan(1 + fe_bytes, fe_bytes));
20✔
165
}
20✔
166

167
void EC_AffinePoint_Data_PC::serialize_xy_to(std::span<uint8_t> bytes) const {
150✔
168
   BOTAN_STATE_CHECK(!this->is_identity());
150✔
169
   const size_t fe_bytes = this->field_element_bytes();
150✔
170
   BOTAN_ARG_CHECK(bytes.size() == 2 * fe_bytes, "Invalid output size");
150✔
171
   copy_mem(bytes, std::span{m_xy}.last(2 * fe_bytes));
150✔
172
}
150✔
173

174
void EC_AffinePoint_Data_PC::serialize_compressed_to(std::span<uint8_t> bytes) const {
8,204✔
175
   BOTAN_STATE_CHECK(!this->is_identity());
8,204✔
176
   const size_t fe_bytes = this->field_element_bytes();
8,204✔
177
   BOTAN_ARG_CHECK(bytes.size() == 1 + fe_bytes, "Invalid output size");
8,204✔
178
   const bool y_is_odd = (m_xy.back() & 0x01) == 0x01;
8,204✔
179

180
   BufferStuffer stuffer(bytes);
8,204✔
181
   stuffer.append(y_is_odd ? 0x03 : 0x02);
12,419✔
182
   this->serialize_x_to(stuffer.next(fe_bytes));
8,204✔
183
}
8,204✔
184

185
void EC_AffinePoint_Data_PC::serialize_uncompressed_to(std::span<uint8_t> bytes) const {
15,287✔
186
   BOTAN_STATE_CHECK(!this->is_identity());
15,287✔
187
   const size_t fe_bytes = this->field_element_bytes();
15,287✔
188
   BOTAN_ARG_CHECK(bytes.size() == 1 + 2 * fe_bytes, "Invalid output size");
15,287✔
189
   copy_mem(bytes, m_xy);
15,287✔
190
}
15,287✔
191

192
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
193
EC_Point EC_AffinePoint_Data_PC::to_legacy_point() const {
21,860✔
194
   if(this->is_identity()) {
21,860✔
195
      return EC_Point(m_group->curve());
96✔
196
   } else {
197
      const size_t fe_bytes = this->field_element_bytes();
21,764✔
198
      return EC_Point(m_group->curve(),
21,764✔
199
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
43,528✔
200
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
65,292✔
201
   }
202
}
203
#endif
204

205
EC_Mul2Table_Data_PC::EC_Mul2Table_Data_PC(const EC_AffinePoint_Data& q) : m_group(q.group()) {
11,178✔
206
   BOTAN_ARG_CHECK(q.group() == m_group, "Curve mismatch");
11,178✔
207

208
   const auto& pt_q = EC_AffinePoint_Data_PC::checked_ref(q);
11,178✔
209

210
   m_tbl = m_group->pcurve().mul2_setup_g(pt_q.value());
11,178✔
211
}
11,178✔
212

213
std::unique_ptr<EC_AffinePoint_Data> EC_Mul2Table_Data_PC::mul2_vartime(const EC_Scalar_Data& xd,
1,099✔
214
                                                                        const EC_Scalar_Data& yd) const {
215
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
1,099✔
216

217
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
1,099✔
218
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
1,099✔
219

220
   if(auto pt = m_group->pcurve().mul2_vartime(*m_tbl, x.value(), y.value())) {
1,099✔
221
      return std::make_unique<EC_AffinePoint_Data_PC>(m_group, pt->to_affine());
1,099✔
222
   } else {
223
      return nullptr;
×
224
   }
1,099✔
225
}
226

227
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
17,626✔
228
                                                       const EC_Scalar_Data& xd,
229
                                                       const EC_Scalar_Data& yd) const {
230
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
17,626✔
231

232
   const auto& v = EC_Scalar_Data_PC::checked_ref(vd);
17,626✔
233
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
17,626✔
234
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
17,626✔
235

236
   return m_group->pcurve().mul2_vartime_x_mod_order_eq(*m_tbl, v.value(), x.value(), y.value());
17,626✔
237
}
238

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