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

randombit / botan / 13076249566

31 Jan 2025 01:32PM UTC coverage: 91.231% (+0.007%) from 91.224%
13076249566

push

github

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

Add EC_Scalar::invert_vartime

94170 of 103222 relevant lines covered (91.23%)

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

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

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

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

44
bool EC_Scalar_Data_PC::is_zero() const {
39,747✔
45
   return this->value().is_zero();
39,747✔
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,186✔
57
   // TODO square in place
58
   m_v = m_v.square();
3,186✔
59
}
3,186✔
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,809✔
66
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v.invert());
6,809✔
67
}
68

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

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

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

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

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

90
EC_AffinePoint_Data_PC::EC_AffinePoint_Data_PC(std::shared_ptr<const EC_Group_Data> group,
71,626✔
91
                                               PCurve::PrimeOrderCurve::AffinePoint pt) :
71,626✔
92
      m_group(std::move(group)), m_pt(std::move(pt)) {
71,626✔
93
   if(!m_pt.is_identity()) {
71,626✔
94
      m_xy = m_pt.serialize<secure_vector<uint8_t>>();
71,410✔
95
      BOTAN_ASSERT_NOMSG(m_xy.size() == 1 + 2 * field_element_bytes());
71,410✔
96
   }
97
}
71,626✔
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,781✔
109
   const auto* p = dynamic_cast<const EC_AffinePoint_Data_PC*>(&data);
32,781✔
110
   if(!p) {
32,781✔
111
      throw Invalid_State("Failed conversion to EC_AffinePoint_Data_PC");
×
112
   }
113
   return *p;
32,781✔
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,578✔
121
   return m_group;
49,578✔
122
}
123

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

129
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
10,020✔
130
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
10,020✔
131
   auto pt = m_group->pcurve().mul(m_pt, k, rng).to_affine();
20,040✔
132
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, std::move(pt));
10,020✔
133
}
10,020✔
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,530✔
146
   return m_group->pcurve().field_element_bytes();
149,530✔
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,627✔
154
   BOTAN_STATE_CHECK(!this->is_identity());
8,627✔
155
   const size_t fe_bytes = this->field_element_bytes();
8,627✔
156
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
8,627✔
157
   copy_mem(bytes, std::span{m_xy}.subspan(1, fe_bytes));
8,627✔
158
}
8,627✔
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,205✔
175
   BOTAN_STATE_CHECK(!this->is_identity());
8,205✔
176
   const size_t fe_bytes = this->field_element_bytes();
8,205✔
177
   BOTAN_ARG_CHECK(bytes.size() == 1 + fe_bytes, "Invalid output size");
8,205✔
178
   const bool y_is_odd = (m_xy.back() & 0x01) == 0x01;
8,205✔
179

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

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

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

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

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

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

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

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

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

227
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
17,655✔
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,655✔
231

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

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