Botan 3.14.1
Crypto and TLS for C&
bls12_381_fp12.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/internal/bls12_381_tower.h>
8
9#include <botan/internal/mp_core.h>
10
11namespace Botan::BLS12_381 {
12
14 return m_c0.is_zero() && m_c1.is_zero();
15}
16
17void Fp12::serialize_to(std::span<uint8_t, Fp12::BYTES> bytes) const {
18 constexpr size_t fe_bytes = FieldElement::BYTES;
19
20 const std::array<const FieldElement*, 12> coeffs = {
21 &m_c0.c0().c0(),
22 &m_c0.c0().c1(),
23 &m_c0.c1().c0(),
24 &m_c0.c1().c1(),
25 &m_c0.c2().c0(),
26 &m_c0.c2().c1(),
27 &m_c1.c0().c0(),
28 &m_c1.c0().c1(),
29 &m_c1.c1().c0(),
30 &m_c1.c1().c1(),
31 &m_c1.c2().c0(),
32 &m_c1.c2().c1(),
33 };
34
35 for(size_t i = 0; i != coeffs.size(); ++i) {
36 coeffs[i]->serialize_to(bytes.subspan(i * fe_bytes).first<fe_bytes>());
37 }
38}
39
40Fp12 Fp12::add(const Fp12& x) const {
41 return Fp12(m_c0 + x.m_c0, m_c1 + x.m_c1);
42}
43
44Fp12 Fp12::sub(const Fp12& x) const {
45 return Fp12(m_c0 - x.m_c0, m_c1 - x.m_c1);
46}
47
49 return Fp12(m_c0.negate(), m_c1.negate());
50}
51
52Fp12 Fp12::mul(const Fp12& x) const {
53 // Karatsuba multiplication using 3 Fp6 multiplications, with w^2 = v
54 const auto aa = m_c0 * x.m_c0;
55 const auto bb = m_c1 * x.m_c1;
56 const auto c1 = (m_c1 + m_c0) * (x.m_c0 + x.m_c1) - aa - bb;
57
58 return Fp12(bb.mul_by_nonresidue() + aa, c1);
59}
60
62 const auto ab = m_c0 * m_c1;
63 const auto c0c1 = m_c0 + m_c1;
64 const auto c0 = (m_c1.mul_by_nonresidue() + m_c0) * c0c1 - ab - ab.mul_by_nonresidue();
65 const auto c1 = ab + ab;
66
67 return Fp12(c0, c1);
68}
69
71 const auto t = (m_c0.square() - m_c1.square().mul_by_nonresidue()).invert();
72 return Fp12(m_c0 * t, (m_c1 * t).negate());
73}
74
76 return Fp12(m_c0, m_c1.negate());
77}
78
79Fp12 Fp12::mul_by_014(const FieldElement2& b0, const FieldElement2& b1, const FieldElement2& b4) const {
80 const auto aa = m_c0.mul_by_01(b0, b1);
81 const auto bb = m_c1.mul_by_1(b4);
82 const auto c1 = (m_c1 + m_c0).mul_by_01(b0, b1 + b4) - aa - bb;
83
84 return Fp12(bb.mul_by_nonresidue() + aa, c1);
85}
86
88 // (u+1)^((p-1)/6)
89 constexpr auto FP12_FROBENIUS_C1 = FieldElement2::_unchecked_from_words(
91 "08f2220fb0fb66eb1ce393ea5daace4da35baecab2dc29ee97e83cccd117228fc6695f92b50a831307089552b319d465"),
93 "110eefda88847faf2e3813cbe5a0de89c11b9cba40a8e8d0cf4895d42599d3945842a06bfc497cecb2f66aad4ce5d646"));
94
95 const auto c0 = m_c0.frobenius_map();
96 const auto c1 = m_c1.frobenius_map();
97
98 // c1 *= (u+1)^((p-1)/6), scaling each Fp2 coefficient directly
99 return Fp12(c0, Fp6(c1.c0() * FP12_FROBENIUS_C1, c1.c1() * FP12_FROBENIUS_C1, c1.c2() * FP12_FROBENIUS_C1));
100}
101
103 // See "Guide to Pairing-Based Cryptography", Algorithm 5.5.4
104
105 // Squaring in Fp4 = Fp2[w]/(w^2 - v), on coefficients directly
106 const auto fp4_square = [](const FieldElement2& a,
107 const FieldElement2& b) -> std::pair<FieldElement2, FieldElement2> {
108 const auto t0 = a.square();
109 const auto t1 = b.square();
110 const auto c0 = t1.mul_by_nonresidue() + t0;
111 const auto c1 = (a + b).square() - t0 - t1;
112 return {c0, c1};
113 };
114
115 auto z0 = m_c0.c0();
116 auto z4 = m_c0.c1();
117 auto z3 = m_c0.c2();
118 auto z2 = m_c1.c0();
119 auto z1 = m_c1.c1();
120 auto z5 = m_c1.c2();
121
122 const auto [t0a, t1a] = fp4_square(z0, z1);
123
124 z0 = t0a - z0;
125 z0 = z0 + z0 + t0a;
126 z1 = t1a + z1;
127 z1 = z1 + z1 + t1a;
128
129 const auto [t0b, t1b] = fp4_square(z2, z3);
130 const auto [t2, t3] = fp4_square(z4, z5);
131
132 z4 = t0b - z4;
133 z4 = z4 + z4 + t0b;
134 z5 = t1b + z5;
135 z5 = z5 + z5 + t1b;
136
137 const auto t = t3.mul_by_nonresidue();
138 z2 = t + z2;
139 z2 = z2 + z2 + t;
140 z3 = t2 - z3;
141 z3 = z3 + z3 + t2;
142
143 return Fp12(Fp6(z0, z4, z3), Fp6(z2, z1, z5));
144}
145
146CT::Choice Fp12::operator==(const Fp12& other) const {
147 return (m_c0 == other.m_c0) && (m_c1 == other.m_c1);
148}
149
150void Fp12::conditional_assign(CT::Choice cnd, const Fp12& other) {
151 m_c0.conditional_assign(cnd, other.m_c0);
152 m_c1.conditional_assign(cnd, other.m_c1);
153}
154
155} // namespace Botan::BLS12_381
static constexpr FieldElement2 _unchecked_from_words(std::array< word, FieldElement::N > c0, std::array< word, FieldElement::N > c1)
const FieldElement & c0() const
static constexpr size_t BYTES
Fp12 mul(const Fp12 &x) const
void serialize_to(std::span< uint8_t, Fp12::BYTES > bytes) const
CT::Choice is_zero() const
Fp12 mul_by_014(const FieldElement2 &b0, const FieldElement2 &b1, const FieldElement2 &b4) const
const Fp6 & c1() const
Fp12 sub(const Fp12 &x) const
Fp12 add(const Fp12 &x) const
const Fp6 & c0() const
void conditional_assign(CT::Choice cnd, const Fp12 &other)
CT::Choice operator==(const Fp12 &other) const
constexpr Fp12()=default
const FieldElement2 & c1() const
constexpr auto hex_to_words(const char(&s)[N])
Definition mp_core.h:921