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

randombit / botan / 13033027833

29 Jan 2025 02:04PM UTC coverage: 91.23% (-0.004%) from 91.234%
13033027833

push

github

web-flow
Merge pull request #4611 from randombit/jack/mul2-setup-g

Add PrimeOrderCurve::mul2_setup_g

94111 of 103158 relevant lines covered (91.23%)

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

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

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

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

44
bool EC_Scalar_Data_PC::is_zero() const {
39,704✔
45
   return this->value().is_zero();
39,704✔
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,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 {
24,356✔
66
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_v.invert());
24,356✔
67
}
68

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

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

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

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

86
EC_AffinePoint_Data_PC::EC_AffinePoint_Data_PC(std::shared_ptr<const EC_Group_Data> group,
71,615✔
87
                                               PCurve::PrimeOrderCurve::AffinePoint pt) :
71,615✔
88
      m_group(std::move(group)), m_pt(std::move(pt)) {
71,615✔
89
   if(!m_pt.is_identity()) {
71,615✔
90
      m_xy = m_pt.serialize<secure_vector<uint8_t>>();
71,399✔
91
      BOTAN_ASSERT_NOMSG(m_xy.size() == 1 + 2 * field_element_bytes());
71,399✔
92
   }
93
}
71,615✔
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) {
32,781✔
105
   const auto* p = dynamic_cast<const EC_AffinePoint_Data_PC*>(&data);
32,781✔
106
   if(!p) {
32,781✔
107
      throw Invalid_State("Failed conversion to EC_AffinePoint_Data_PC");
×
108
   }
109
   return *p;
32,781✔
110
}
111

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

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

120
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::mul(const EC_Scalar_Data& scalar,
10,020✔
121
                                                                 RandomNumberGenerator& rng,
122
                                                                 std::vector<BigInt>& ws) const {
123
   BOTAN_UNUSED(ws);
10,020✔
124

125
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
10,020✔
126
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
10,020✔
127
   auto pt = m_group->pcurve().mul(m_pt, k, rng).to_affine();
20,040✔
128
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, std::move(pt));
10,020✔
129
}
10,020✔
130

131
secure_vector<uint8_t> EC_AffinePoint_Data_PC::mul_x_only(const EC_Scalar_Data& scalar,
2,649✔
132
                                                          RandomNumberGenerator& rng,
133
                                                          std::vector<BigInt>& ws) const {
134
   BOTAN_UNUSED(ws);
2,649✔
135

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

141
size_t EC_AffinePoint_Data_PC::field_element_bytes() const {
149,432✔
142
   return m_group->pcurve().field_element_bytes();
149,432✔
143
}
144

145
bool EC_AffinePoint_Data_PC::is_identity() const {
78,305✔
146
   return m_xy.empty();
78,305✔
147
}
148

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

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

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

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

176
   BufferStuffer stuffer(bytes);
8,194✔
177
   stuffer.append(y_is_odd ? 0x03 : 0x02);
12,377✔
178
   this->serialize_x_to(stuffer.next(fe_bytes));
8,194✔
179
}
8,194✔
180

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

188
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
189
EC_Point EC_AffinePoint_Data_PC::to_legacy_point() const {
21,841✔
190
   if(this->is_identity()) {
21,841✔
191
      return EC_Point(m_group->curve());
96✔
192
   } else {
193
      const size_t fe_bytes = this->field_element_bytes();
21,745✔
194
      return EC_Point(m_group->curve(),
21,745✔
195
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
43,490✔
196
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
65,235✔
197
   }
198
}
199
#endif
200

201
EC_Mul2Table_Data_PC::EC_Mul2Table_Data_PC(const EC_AffinePoint_Data& q) : m_group(q.group()) {
11,171✔
202
   BOTAN_ARG_CHECK(q.group() == m_group, "Curve mismatch");
11,171✔
203

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

206
   m_tbl = m_group->pcurve().mul2_setup_g(pt_q.value());
11,171✔
207
}
11,171✔
208

209
std::unique_ptr<EC_AffinePoint_Data> EC_Mul2Table_Data_PC::mul2_vartime(const EC_Scalar_Data& xd,
1,101✔
210
                                                                        const EC_Scalar_Data& yd) const {
211
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
1,101✔
212

213
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
1,101✔
214
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
1,101✔
215

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

223
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
17,633✔
224
                                                       const EC_Scalar_Data& xd,
225
                                                       const EC_Scalar_Data& yd) const {
226
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
17,633✔
227

228
   const auto& v = EC_Scalar_Data_PC::checked_ref(vd);
17,633✔
229
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
17,633✔
230
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
17,633✔
231

232
   return m_group->pcurve().mul2_vartime_x_mod_order_eq(*m_tbl, v.value(), x.value(), y.value());
17,633✔
233
}
234

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