Botan 3.6.1
Crypto and TLS for C&
pcurves_secp256k1.cpp
Go to the documentation of this file.
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/pcurves_instance.h>
8
9#include <botan/internal/pcurves_wrap.h>
10
11namespace Botan::PCurve {
12
13namespace {
14
15namespace secp256k1 {
16
17template <typename Params>
18class Secp256k1Rep final {
19 public:
20 static constexpr auto P = Params::P;
21 static constexpr size_t N = Params::N;
22 typedef typename Params::W W;
23
24 static_assert(WordInfo<W>::bits >= 33);
25
26 static constexpr W C = 0x1000003d1;
27
28 constexpr static std::array<W, N> one() { return std::array<W, N>{1}; }
29
30 constexpr static std::array<W, N> redc(const std::array<W, 2 * N>& z) {
31 return redc_crandall<W, N, C>(std::span{z});
32 }
33
34 constexpr static std::array<W, N> to_rep(const std::array<W, N>& x) { return x; }
35
36 constexpr static std::array<W, N> wide_to_rep(const std::array<W, 2 * N>& x) { return redc(x); }
37
38 constexpr static std::array<W, N> from_rep(const std::array<W, N>& z) { return z; }
39};
40
41// clang-format off
42class Params final : public EllipticCurveParameters<
43 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F",
44 "0",
45 "7",
46 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
47 "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
48 "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"> {
49};
50
51// clang-format on
52#if BOTAN_MP_WORD_BITS == 64
53typedef EllipticCurve<Params, Secp256k1Rep> Secp256k1Base;
54#else
55typedef EllipticCurve<Params> Secp256k1Base;
56#endif
57
58class Curve final : public Secp256k1Base {
59 public:
60 // Return the square of the inverse of x
61 static FieldElement fe_invert2(const FieldElement& x) {
62 auto z = x.square();
63 z *= x;
64 auto t0 = z;
65 t0.square_n(2);
66 t0 *= z;
67 auto t1 = t0.square();
68 auto t2 = t1 * x;
69 t1 = t2;
70 t1.square_n(2);
71 t1 *= z;
72 auto t3 = t1;
73 t3.square_n(4);
74 t0 *= t3;
75 t3 = t0;
76 t3.square_n(11);
77 t0 *= t3;
78 t3 = t0;
79 t3.square_n(5);
80 t2 *= t3;
81 t3 = t2;
82 t3.square_n(27);
83 t2 *= t3;
84 t3 = t2;
85 t3.square_n(54);
86 t2 *= t3;
87 t3 = t2;
88 t3.square_n(108);
89 t2 *= t3;
90 t2.square_n(7);
91 t1 *= t2;
92 t1.square_n(23);
93 t0 *= t1;
94 t0.square_n(5);
95 t0 *= x;
96 t0.square_n(3);
97 z *= t0;
98 z.square_n(2);
99 return z;
100 }
101
102 static Scalar scalar_invert(const Scalar& x) {
103 auto z = x.square();
104 auto t2 = x * z;
105 auto t6 = t2 * z;
106 auto t5 = t6 * z;
107 auto t0 = t5 * z;
108 auto t3 = t0 * z;
109 auto t1 = t3 * z;
110 z = t1;
111 z.square_n(2);
112 z *= t3;
113 auto t4 = z.square();
114 auto t7 = t4 * x;
115 t4 = t7.square();
116 t4 *= x;
117 auto t9 = t4;
118 t9.square_n(3);
119 auto t10 = t9;
120 t10.square_n(2);
121 auto t11 = t10.square();
122 auto t8 = t11.square();
123 auto t12 = t8;
124 t12.square_n(7);
125 t11 *= t12;
126 t11.square_n(9);
127 t8 *= t11;
128 t11 = t8;
129 t11.square_n(6);
130 t10 *= t11;
131 t10.square_n(26);
132 t8 *= t10;
133 t10 = t8;
134 t10.square_n(4);
135 t9 *= t10;
136 t9.square_n(60);
137 t8 *= t9;
138 t7 *= t8;
139 t7.square_n(5);
140 t7 *= t3;
141 t7.square_n(3);
142 t7 *= t6;
143 t7.square_n(4);
144 t7 *= t6;
145 t7.square_n(4);
146 t7 *= t5;
147 t7.square_n(5);
148 t7 *= t1;
149 t7.square_n(2);
150 t7 *= t2;
151 t7.square_n(5);
152 t7 *= t5;
153 t7.square_n(6);
154 t7 *= t1;
155 t7.square_n(5);
156 t7 *= t3;
157 t7.square_n(4);
158 t7 *= t1;
159 t7.square_n(3);
160 t7 *= x;
161 t7.square_n(6);
162 t6 *= t7;
163 t6.square_n(10);
164 t6 *= t5;
165 t6.square_n(4);
166 t5 *= t6;
167 t5.square_n(9);
168 t4 *= t5;
169 t4.square_n(5);
170 t4 *= t0;
171 t4.square_n(6);
172 t3 *= t4;
173 t3.square_n(4);
174 t3 *= t1;
175 t3.square_n(5);
176 t2 *= t3;
177 t2.square_n(6);
178 t2 *= t1;
179 t2.square_n(10);
180 t1 *= t2;
181 t1.square_n(4);
182 t0 *= t1;
183 t0.square_n(6);
184 t0 *= x;
185 t0.square_n(8);
186 z *= t0;
187 return z;
188 }
189};
190
191} // namespace secp256k1
192
193} // namespace
194
195std::shared_ptr<const PrimeOrderCurve> PCurveInstance::secp256k1() {
197}
198
199} // namespace Botan::PCurve
static std::shared_ptr< const PrimeOrderCurve > secp256k1()
Definition pcurves.cpp:54
static std::shared_ptr< const PrimeOrderCurve > instance()
int(* final)(unsigned char *, CTX *)
constexpr std::array< W, N > redc_crandall(std::span< const W, 2 *N > z)
Definition mp_core.h:1122