Botan 3.14.1
Crypto and TLS for C&
bls12_381_tower.h
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#ifndef BOTAN_BLS12_381_TOWER_H_
8#define BOTAN_BLS12_381_TOWER_H_
9
10#include <botan/internal/bls12_381_fields.h>
11
12namespace Botan::BLS12_381 {
13
14/**
15* An element of Fp6 = Fp2[v]/(v^3 - (u + 1))
16*
17* Represented as c0 + c1*v + c2*v^2
18*/
19class BOTAN_TEST_API Fp6 final {
20 public:
21 /** Default constructed as zero */
22 constexpr Fp6() = default;
23
24 constexpr Fp6(const FieldElement2& c0, const FieldElement2& c1, const FieldElement2& c2) :
25 m_c0(c0), m_c1(c1), m_c2(c2) {}
26
27 static Fp6 zero() { return Fp6(); }
28
30
31 const FieldElement2& c0() const { return m_c0; }
32
33 const FieldElement2& c1() const { return m_c1; }
34
35 const FieldElement2& c2() const { return m_c2; }
36
37 CT::Choice is_zero() const;
38
39 Fp6 add(const Fp6& x) const;
40
41 Fp6 sub(const Fp6& x) const;
42
43 Fp6 mul(const Fp6& x) const;
44
45 Fp6 square() const;
46
47 Fp6 negate() const;
48
49 /**
50 * Return the inverse of this element, or zero for zero
51 */
52 Fp6 invert() const;
53
54 /**
55 * Multiplication by v
56 */
57 Fp6 mul_by_nonresidue() const;
58
59 /**
60 * Sparse multiplication by b1*v
61 */
62 Fp6 mul_by_1(const FieldElement2& b1) const;
63
64 /**
65 * Sparse multiplication by b0 + b1*v
66 */
67 Fp6 mul_by_01(const FieldElement2& b0, const FieldElement2& b1) const;
68
69 /**
70 * The Frobenius endomorphism x -> x^p
71 */
72 Fp6 frobenius_map() const;
73
74 CT::Choice operator==(const Fp6& other) const;
75
76 void conditional_assign(CT::Choice cnd, const Fp6& other);
77
78 private:
79 FieldElement2 m_c0;
80 FieldElement2 m_c1;
81 FieldElement2 m_c2;
82};
83
84inline Fp6 operator+(const Fp6& a, const Fp6& b) {
85 return a.add(b);
86}
87
88inline Fp6 operator-(const Fp6& a, const Fp6& b) {
89 return a.sub(b);
90}
91
92inline Fp6 operator*(const Fp6& a, const Fp6& b) {
93 return a.mul(b);
94}
95
96/**
97* An element of Fp12 = Fp6[w]/(w^2 - v)
98*
99* Represented as c0 + c1*w
100*/
101class BOTAN_TEST_API Fp12 final {
102 public:
103 static constexpr size_t BYTES = 12 * FieldElement::BYTES;
104
105 /** Default constructed as zero */
106 constexpr Fp12() = default;
107
108 constexpr Fp12(const Fp6& c0, const Fp6& c1) : m_c0(c0), m_c1(c1) {}
109
110 static Fp12 zero() { return Fp12(); }
111
112 static Fp12 one() { return Fp12(Fp6::one(), Fp6::zero()); }
113
114 const Fp6& c0() const { return m_c0; }
115
116 const Fp6& c1() const { return m_c1; }
117
118 CT::Choice is_zero() const;
119
120 /**
121 * Serialization for tests and Gt; the coefficients are encoded in
122 * the order c0.c0.c0, c0.c0.c1, c0.c1.c0, ..., c1.c2.c1 with each
123 * Fp coefficient as 48 big-endian bytes
124 */
125 void serialize_to(std::span<uint8_t, Fp12::BYTES> bytes) const;
126
127 std::array<uint8_t, Fp12::BYTES> serialize() const {
128 std::array<uint8_t, Fp12::BYTES> buf{};
129 this->serialize_to(buf);
130 return buf;
131 }
132
133 Fp12 add(const Fp12& x) const;
134
135 Fp12 sub(const Fp12& x) const;
136
137 Fp12 mul(const Fp12& x) const;
138
139 Fp12 square() const;
140
141 Fp12 negate() const;
142
143 /**
144 * Return the inverse of this element, or zero for zero
145 */
146 Fp12 invert() const;
147
148 /**
149 * Conjugation c0 - c1*w; for elements of the cyclotomic subgroup
150 * (such as pairing values) this is the inverse
151 */
152 Fp12 conjugate() const;
153
154 /**
155 * Sparse multiplication by b = (b0 + b1*v)*1 + (b4*v)*w, the shape
156 * produced by pairing line evaluations
157 */
158 Fp12 mul_by_014(const FieldElement2& b0, const FieldElement2& b1, const FieldElement2& b4) const;
159
160 /**
161 * The Frobenius endomorphism x -> x^p
162 */
163 Fp12 frobenius_map() const;
164
165 /**
166 * Squaring specialized for elements of the cyclotomic subgroup
167 */
168 Fp12 cyclotomic_square() const;
169
170 CT::Choice operator==(const Fp12& other) const;
171
172 void conditional_assign(CT::Choice cnd, const Fp12& other);
173
174 private:
175 Fp6 m_c0;
176 Fp6 m_c1;
177};
178
179inline Fp12 operator+(const Fp12& a, const Fp12& b) {
180 return a.add(b);
181}
182
183inline Fp12 operator-(const Fp12& a, const Fp12& b) {
184 return a.sub(b);
185}
186
187inline Fp12 operator*(const Fp12& a, const Fp12& b) {
188 return a.mul(b);
189}
190
191} // namespace Botan::BLS12_381
192
193#endif
#define BOTAN_TEST_API
Definition api.h:41
static constexpr size_t BYTES
Fp12 mul(const Fp12 &x) const
void serialize_to(std::span< uint8_t, Fp12::BYTES > bytes) const
constexpr Fp12(const Fp6 &c0, const Fp6 &c1)
const Fp6 & c1() const
Fp12 sub(const Fp12 &x) const
static constexpr size_t BYTES
Fp12 add(const Fp12 &x) const
const Fp6 & c0() const
constexpr Fp12()=default
std::array< uint8_t, Fp12::BYTES > serialize() const
const FieldElement2 & c1() const
Fp6 mul(const Fp6 &x) const
Fp6 sub(const Fp6 &x) const
constexpr Fp6(const FieldElement2 &c0, const FieldElement2 &c1, const FieldElement2 &c2)
const FieldElement2 & c2() const
const FieldElement2 & c0() const
Fp6 add(const Fp6 &x) const
constexpr Fp6()=default
Scalar operator-(const Scalar &a, const Scalar &b)
Definition bls12_381.h:177
Scalar operator+(const Scalar &a, const Scalar &b)
Definition bls12_381.h:172
Scalar operator*(const Scalar &a, const Scalar &b)
Definition bls12_381.h:182
BigInt square(const BigInt &x)
Definition numthry.cpp:184
bool operator==(const AlgorithmIdentifier &x, const AlgorithmIdentifier &y)
Definition alg_id.cpp:54