Botan 3.14.1
Crypto and TLS for C&
bls12_381_g1.cpp
Go to the documentation of this file.
1/*
2* (C) 2024,2025,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/internal/bls12_381_fields.h>
10#include <botan/internal/bls12_381_group.h>
11#include <botan/internal/bls12_381_point_mul.h>
12#include <botan/internal/ct_utils.h>
13#include <botan/internal/mp_core.h>
14
15namespace Botan::BLS12_381 {
16
17namespace {
18
19// Standard generator coordinates, pre-converted into Montgomery form
21 "120177419e0bfb75edce6ecc21dbf440f0ae6acdf3d0e747154f95c7143ba1c17817fc679976fff55cb38790fd530c16"));
22
24 "0bbc3efc5008a26a0e1c8c3fad0059c051ac582950405194dd595f13570725ce8c22631a7918fd8ebaac93d50ce72271"));
25
26// A nontrivial cube root of unity, acting on G1 as multiplication by -z^2
28 "051ba4ab241b61603636b76660701c6ec26a2ff874fd029b16a8ca3ac61577f7f3b8ddab7ece5a2a30f1361b798a64e8"));
29
30/**
31* The G1 traits for GroupOps
32*/
33struct G1Group final {
34 using Affine = G1Affine;
35 using Projective = G1Projective;
36 using FE = FieldElement;
37
38 static FE load(const std::array<word, FieldElement::N>& w) { return FieldElement::_unchecked_from_words(w); }
39
40 static std::array<word, FieldElement::N> store(const FE& v) { return v._words(); }
41
42 static FE mul_by_3b(const FE& fe) {
43 // b == 4 so 3*b == 12
44 const auto fe2 = fe + fe;
45 const auto fe4 = fe2 + fe2;
46 return fe4 + fe4 + fe4;
47 }
48
49 static FE curve_b() { return FieldElement::from_u32(4); }
50};
51
52using G1Ops = GroupOps<G1Group>;
53
54} // namespace
55
56G1Affine::G1Affine(const FieldElement& x, const FieldElement& y, uint32_t infinity) :
57 m_x(x._words()), m_y(y._words()), m_infinity(infinity) {}
58
59//static
61 return G1Affine(FieldElement::zero(), FieldElement::one(), 1);
62}
63
64//static
66 return G1Affine(G1_X, G1_Y, 0);
67}
68
70 return G1Group::load(m_x);
71}
72
74 return G1Group::load(m_y);
75}
76
78 return m_infinity != 0;
79}
80
81//static
82std::optional<G1Affine> G1Affine::deserialize(std::span<const uint8_t> bytes) {
83 auto pt = G1Ops::deserialize_unchecked(bytes);
84 if(!pt || pt->is_identity()) {
85 return pt;
86 }
87
88 // Fast subgroup check (https://eprint.iacr.org/2021/1130 section 6):
89 // P is in the prime order subgroup iff phi(P) == -[z^2]P, where
90 // phi(x, y) = (beta*x, y); per the paper, "if the endomorphism is
91 // true, then P must be of order r"
92 using Mul = PointMul<G1Projective>;
93 const auto zz_p = Mul::mul_by_z_abs(Mul::mul_by_z_abs(G1Projective::from_affine(*pt))).negate();
94
95 // Compare the projective -[z^2]P with the affine phi(P)
96 const auto zz_p_z = G1Group::load(zz_p.m_z);
97 const auto x_eq = (G1Group::load(zz_p.m_x) == (pt->_x() * G1_BETA) * zz_p_z);
98 const auto y_eq = (G1Group::load(zz_p.m_y) == pt->_y() * zz_p_z);
99 if(!(x_eq && y_eq).as_bool()) {
100 return {};
101 }
102
103 return pt;
104}
105
106std::array<uint8_t, G1Affine::BYTES> G1Affine::serialize() const {
107 return G1Ops::serialize(*this);
108}
109
111 m_x(x._words()), m_y(y._words()), m_z(z._words()) {}
112
113G1Projective::G1Projective() : m_x({}), m_y(FieldElement::one()._words()), m_z({}) {}
114
115//static
117 return G1Ops::from_affine(affine);
118}
119
120//static
124
125//static
129
131 return G1Ops::to_affine(*this);
132}
133
134//static
135std::vector<G1Affine> G1Projective::to_affine_batch(std::span<const G1Projective> points) {
136 return G1Ops::to_affine_batch(points);
137}
138
140 return G1Ops::negate(*this);
141}
142
143void G1Projective::conditional_negate(uint32_t negate) {
144 G1Ops::conditional_negate(*this, negate);
145}
146
148 return CT::all_zeros(m_z.data(), m_z.size()).as_bool();
149}
150
151G1Projective G1Projective::dbl() const {
152 return G1Ops::dbl(*this);
153}
154
156 return G1Ops::add(*this, other);
157}
158
160 return G1Ops::add_mixed(*this, other, false);
161}
162
163G1Projective G1Projective::add_mixed(const G1Affine& other, bool negate_other) const {
164 return G1Ops::add_mixed(*this, other, negate_other);
165}
166
168 return PointMul<G1Projective>::mul(*this, scalar);
169}
170
171//static
172G1Projective G1Projective::mul2(const G1Projective& p, const Scalar& a, const G1Projective& q, const Scalar& b) {
173 return PointMul<G1Projective>::mul2(p, a, q, b);
174}
175
176//static
178 const Scalar& a,
179 const G1Projective& q,
180 const Scalar& b) {
181 return PointMul<G1Projective>::mul2_vartime(p, a, q, b);
182}
183
184//static
185G1Projective G1Projective::msm_vartime(std::span<const G1Affine> points, std::span<const Scalar> scalars) {
186 return PointMul<G1Projective>::msm_vartime(points, scalars);
187}
188
189} // namespace Botan::BLS12_381
static constexpr FieldElement _unchecked_from_words(std::array< word, FieldElement::N > v)
static FieldElement one()
static FieldElement from_u32(uint32_t v)
static G1Affine identity()
static std::optional< G1Affine > deserialize(std::span< const uint8_t > bytes)
FieldElement _y() const
FieldElement _x() const
std::array< uint8_t, G1Affine::BYTES > serialize() const
static G1Affine generator()
G1Projective mul(const Scalar &scalar) const
static G1Projective mul2_vartime(const G1Projective &p, const Scalar &a, const G1Projective &q, const Scalar &b)
static std::vector< G1Affine > to_affine_batch(std::span< const G1Projective > points)
static G1Projective mul2(const G1Projective &p, const Scalar &a, const G1Projective &q, const Scalar &b)
static G1Projective generator()
G1Projective add_mixed(const G1Affine &other) const
G1Projective add(const G1Projective &other) const
static G1Projective from_affine(const G1Affine &affine)
static G1Projective msm_vartime(std::span< const G1Affine > points, std::span< const Scalar > scalars)
static G1Projective _unchecked_from_affine_coords(const FieldElement &x, const FieldElement &y)
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 auto hex_to_words(const char(&s)[N])
Definition mp_core.h:921