Botan 3.14.1
Crypto and TLS for C&
bls12_381_g2.cpp
Go to the documentation of this file.
1/*
2* (C) 2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/bls12_381.h>
8
9#include <botan/mem_ops.h>
10#include <botan/internal/bls12_381_fields.h>
11#include <botan/internal/bls12_381_group.h>
12#include <botan/internal/bls12_381_point_mul.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/mp_core.h>
15
16namespace Botan::BLS12_381 {
17
18namespace {
19
20FieldElement2 fe2_load(const std::array<word, 2 * FieldElement::N>& w) {
21 std::array<word, FieldElement::N> c0{};
22 std::array<word, FieldElement::N> c1{};
23 copy_mem(c0.data(), w.data(), c0.size());
24 copy_mem(c1.data(), w.data() + c0.size(), c1.size());
26}
27
28std::array<word, 2 * FieldElement::N> fe2_store(const FieldElement2& v) {
29 std::array<word, 2 * FieldElement::N> w{};
30 copy_mem(w.data(), v.c0()._words().data(), FieldElement::N);
31 copy_mem(w.data() + FieldElement::N, v.c1()._words().data(), FieldElement::N);
32 return w;
33}
34
35// Standard generator coordinates, pre-converted into Montgomery form
36constexpr auto G2_X = FieldElement2::_unchecked_from_words(
38 "058191924350bcd76f67b7631863366b9894999d1a3caee9a1a893b53e2ae580b3f5fb2687b4961af5f28fa202940a10"),
40 "11922a097360edf3c2b6ed0ef21585471b1ab6cc8541b3673bb17e18e2867806aaa0c59dbccd60c3a5a9c0759e23f606"));
41
42constexpr auto G2_Y = FieldElement2::_unchecked_from_words(
44 "0083fd8e7e80dae507d3a975f0ef25a2bbefb5e96e0d495fe7e6856caa0a635a597cfa1f5e369c5a4c730af860494c4a"),
46 "0b2bc2a163de1bf2e7175850a43ccaed79495c4ec93da33a86adac6a3be4eba018aa270a2b1461dcadc0fc92df64b05d"));
47
48// 1/(u+1)^((p-1)/3) and 1/(u+1)^((p-1)/2), for the psi endomorphism
49constexpr auto G2_PSI_X = FieldElement2::_unchecked_from_words(
51 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
53 "14e56d3f1564853a14e4f04fe2db9068a20d1b8c7e88102450880866309b7e2c2af322533285a5d5890dc9e4867545c3"));
54constexpr auto G2_PSI_Y = FieldElement2::_unchecked_from_words(
56 "0bd592fc7d825ec81d794e4fac7cf0b992ad2afd19103e18382844c88b6237324294213d86c181833e2f585da55c9ad1"),
58 "0e2b7eedbbfd87d22da2596696cebc1dd1ca2087da74d4a72f088dd86b4ebef1dc17dec12a927e7c7bcfa7a25aa30fda"));
59
60// The x coefficient of psi^2; the y coefficient is -1
61constexpr auto G2_PSI2_X = FieldElement2::_unchecked_from_words(
63 "18f020655463874103f97d6e83d050d28eb60ebe01bacb9e587042afd3851b955dab22461fcda5d2cd03c9e48671f071"),
65 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
66
67/**
68* The G2 traits for GroupOps
69*/
70struct G2Group final {
71 using Affine = G2Affine;
72 using Projective = G2Projective;
73 using FE = FieldElement2;
74
75 static FE load(const std::array<word, 2 * FieldElement::N>& w) { return fe2_load(w); }
76
77 static std::array<word, 2 * FieldElement::N> store(const FE& v) { return fe2_store(v); }
78
79 static FE mul_by_3b(const FE& fe) {
80 // b == 4*(u+1) so 3*b == 12*(u+1)
81 const auto fe2 = fe + fe;
82 const auto fe4 = fe2 + fe2;
83 return (fe4 + fe4 + fe4).mul_by_nonresidue();
84 }
85
86 static FE curve_b() {
87 const auto four = FieldElement::from_u32(4);
88 return FieldElement2(four, four);
89 }
90};
91
92using G2Ops = GroupOps<G2Group>;
93
94} // namespace
95
96G2Affine::G2Affine(const FieldElement2& x, const FieldElement2& y, uint32_t infinity) :
97 m_x(fe2_store(x)), m_y(fe2_store(y)), m_infinity(infinity) {}
98
99//static
101 return G2Affine(FieldElement2::zero(), FieldElement2::one(), 1);
102}
103
104//static
106 return G2Affine(G2_X, G2_Y, 0);
107}
108
110 return fe2_load(m_x);
111}
112
114 return fe2_load(m_y);
115}
116
118 return m_infinity != 0;
119}
120
121//static
122std::optional<G2Affine> G2Affine::deserialize(std::span<const uint8_t> bytes) {
123 auto pt = G2Ops::deserialize_unchecked(bytes);
124 if(!pt || pt->is_identity()) {
125 return pt;
126 }
127
128 // Fast subgroup check (https://eprint.iacr.org/2021/1130 section 4):
129 // P is in the prime order subgroup iff psi(P) == [z]P, where psi is
130 // the untwist-Frobenius-twist endomorphism; the test is conclusive
131 // since "Q is of order r under the condition gcd(h1, h2) = 1" and
132 // for BLS12 curves "h2 mod h1 = 1 so that the condition is true"
134
135 const auto psi_x = pt->_x().conjugate() * G2_PSI_X;
136 const auto psi_y = pt->_y().conjugate() * G2_PSI_Y;
137
138 // Compare the projective [z]P with the affine psi(P)
139 const auto z_p_z = fe2_load(z_p.m_z);
140 const auto x_eq = (fe2_load(z_p.m_x) == psi_x * z_p_z);
141 const auto y_eq = (fe2_load(z_p.m_y) == psi_y * z_p_z);
142 if(!(x_eq && y_eq).as_bool()) {
143 return {};
144 }
145
146 return pt;
147}
148
149std::array<uint8_t, G2Affine::BYTES> G2Affine::serialize() const {
150 return G2Ops::serialize(*this);
151}
152
154 m_x(fe2_store(x)), m_y(fe2_store(y)), m_z(fe2_store(z)) {}
155
156G2Projective::G2Projective() : m_x({}), m_y(fe2_store(FieldElement2::one())), m_z({}) {}
157
158//static
160 return G2Ops::from_affine(affine);
161}
162
163//static
167
168//static
172
174 return G2Ops::to_affine(*this);
175}
176
177//static
178std::vector<G2Affine> G2Projective::to_affine_batch(std::span<const G2Projective> points) {
179 return G2Ops::to_affine_batch(points);
180}
181
183 return G2Ops::negate(*this);
184}
185
186void G2Projective::conditional_negate(uint32_t negate) {
187 G2Ops::conditional_negate(*this, negate);
188}
189
191 return CT::all_zeros(m_z.data(), m_z.size()).as_bool();
192}
193
194G2Projective G2Projective::dbl() const {
195 return G2Ops::dbl(*this);
196}
197
199 return G2Ops::add(*this, other);
200}
201
203 return G2Ops::add_mixed(*this, other, false);
204}
205
206G2Projective G2Projective::add_mixed(const G2Affine& other, bool negate_other) const {
207 return G2Ops::add_mixed(*this, other, negate_other);
208}
209
211 return PointMul<G2Projective>::mul(*this, scalar);
212}
213
214//static
215G2Projective G2Projective::mul2(const G2Projective& p, const Scalar& a, const G2Projective& q, const Scalar& b) {
216 return PointMul<G2Projective>::mul2(p, a, q, b);
217}
218
219//static
221 const Scalar& a,
222 const G2Projective& q,
223 const Scalar& b) {
224 return PointMul<G2Projective>::mul2_vartime(p, a, q, b);
225}
226
227//static
228G2Projective G2Projective::msm_vartime(std::span<const G2Affine> points, std::span<const Scalar> scalars) {
229 return PointMul<G2Projective>::msm_vartime(points, scalars);
230}
231
232G2Projective G2Projective::psi() const {
233 return G2Projective(
234 fe2_load(m_x).conjugate() * G2_PSI_X, fe2_load(m_y).conjugate() * G2_PSI_Y, fe2_load(m_z).conjugate());
235}
236
237G2Projective G2Projective::psi2() const {
238 return G2Projective(fe2_load(m_x) * G2_PSI2_X, fe2_load(m_y).negate(), fe2_load(m_z));
239}
240
241G2Projective G2Projective::clear_cofactor() const {
242 // Budroni-Pintore cofactor clearing (https://eprint.iacr.org/2017/419 section 4.1)
243
244 // [z]P; the parameter z is negative
245 const auto t1 = PointMul<G2Projective>::mul_by_z_abs(*this).negate();
246 const auto t2 = this->psi();
247
248 auto r = this->dbl().psi2();
249 r = r.add(PointMul<G2Projective>::mul_by_z_abs(t1.add(t2)).negate());
250 r = r.add(t1.negate());
251 r = r.add(t2.negate());
252 r = r.add(this->negate());
253
254 return r;
255}
256
257} // namespace Botan::BLS12_381
static constexpr FieldElement2 _unchecked_from_words(std::array< word, FieldElement::N > c0, std::array< word, FieldElement::N > c1)
static constexpr size_t N
static FieldElement from_u32(uint32_t v)
static G2Affine generator()
FieldElement2 _x() const
std::array< uint8_t, G2Affine::BYTES > serialize() const
static std::optional< G2Affine > deserialize(std::span< const uint8_t > bytes)
FieldElement2 _y() const
static G2Projective generator()
static G2Projective mul2_vartime(const G2Projective &p, const Scalar &a, const G2Projective &q, const Scalar &b)
static G2Projective _unchecked_from_affine_coords(const FieldElement2 &x, const FieldElement2 &y)
static G2Projective mul2(const G2Projective &p, const Scalar &a, const G2Projective &q, const Scalar &b)
static G2Projective msm_vartime(std::span< const G2Affine > points, std::span< const Scalar > scalars)
G2Projective add_mixed(const G2Affine &other) const
G2Projective mul(const Scalar &scalar) const
G2Projective add(const G2Projective &other) const
static G2Projective from_affine(const G2Affine &affine)
static std::vector< G2Affine > to_affine_batch(std::span< const G2Projective > points)
static Pt mul_by_z_abs(const Pt &pt)
static Pt mul2_vartime(const Pt &p, const Scalar &a, const Pt &q, const Scalar &b)
static Pt msm_vartime(std::span< const AffinePt > points, std::span< const Scalar > scalars)
static Pt mul2(const Pt &p, const Scalar &a, const Pt &q, const Scalar &b)
static Pt mul(const Pt &pt, const Scalar &scalar)
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:790
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
constexpr auto hex_to_words(const char(&s)[N])
Definition mp_core.h:921