Botan 3.14.1
Crypto and TLS for C&
bls12_381_fp6.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() && m_c2.is_zero();
15}
16
17Fp6 Fp6::add(const Fp6& x) const {
18 return Fp6(m_c0 + x.m_c0, m_c1 + x.m_c1, m_c2 + x.m_c2);
19}
20
21Fp6 Fp6::sub(const Fp6& x) const {
22 return Fp6(m_c0 - x.m_c0, m_c1 - x.m_c1, m_c2 - x.m_c2);
23}
24
26 return Fp6(m_c0.negate(), m_c1.negate(), m_c2.negate());
27}
28
29Fp6 Fp6::mul(const Fp6& x) const {
30 // Karatsuba multiplication using 6 Fp2 multiplications, with xi = u+1:
31 // c0 = t0 + xi*((a1+a2)(b1+b2) - t1 - t2)
32 // c1 = (a0+a1)(b0+b1) - t0 - t1 + xi*t2
33 // c2 = (a0+a2)(b0+b2) - t0 - t2 + t1
34 const auto t0 = m_c0 * x.m_c0;
35 const auto t1 = m_c1 * x.m_c1;
36 const auto t2 = m_c2 * x.m_c2;
37
38 const auto s12 = (m_c1 + m_c2) * (x.m_c1 + x.m_c2);
39 const auto s01 = (m_c0 + m_c1) * (x.m_c0 + x.m_c1);
40 const auto s02 = (m_c0 + m_c2) * (x.m_c0 + x.m_c2);
41
42 return Fp6((s12 - t1 - t2).mul_by_nonresidue() + t0, s01 - t0 - t1 + t2.mul_by_nonresidue(), s02 - t0 - t2 + t1);
43}
44
46 const auto s0 = m_c0.square();
47 const auto ab = m_c0 * m_c1;
48 const auto s1 = ab + ab;
49 const auto s2 = (m_c0 - m_c1 + m_c2).square();
50 const auto bc = m_c1 * m_c2;
51 const auto s3 = bc + bc;
52 const auto s4 = m_c2.square();
53
54 return Fp6(s3.mul_by_nonresidue() + s0, s4.mul_by_nonresidue() + s1, s1 + s2 + s3 - s0 - s4);
55}
56
58 const auto c0 = m_c0.square() - (m_c1 * m_c2).mul_by_nonresidue();
59 const auto c1 = m_c2.square().mul_by_nonresidue() - (m_c0 * m_c1);
60 const auto c2 = m_c1.square() - (m_c0 * m_c2);
61
62 const auto t = (((m_c1 * c2) + (m_c2 * c1)).mul_by_nonresidue() + (m_c0 * c0)).invert();
63
64 return Fp6(t * c0, t * c1, t * c2);
65}
66
68 // (c0 + c1*v + c2*v^2)*v = c2*(u+1) + c0*v + c1*v^2 using v^3 = u+1
69 return Fp6(m_c2.mul_by_nonresidue(), m_c0, m_c1);
70}
71
73 return Fp6((m_c2 * b1).mul_by_nonresidue(), m_c0 * b1, m_c1 * b1);
74}
75
76Fp6 Fp6::mul_by_01(const FieldElement2& b0, const FieldElement2& b1) const {
77 const auto a_a = m_c0 * b0;
78 const auto b_b = m_c1 * b1;
79
80 const auto t1 = (m_c2 * b1).mul_by_nonresidue() + a_a;
81 const auto t2 = (b0 + b1) * (m_c0 + m_c1) - a_a - b_b;
82 const auto t3 = m_c2 * b0 + b_b;
83
84 return Fp6(t1, t2, t3);
85}
86
88 // (u+1)^((p-1)/3)
89 constexpr auto FP6_FROBENIUS_C1 = FieldElement2::_unchecked_from_words(
91 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
93 "18f020655463874103f97d6e83d050d28eb60ebe01bacb9e587042afd3851b955dab22461fcda5d2cd03c9e48671f071"));
94
95 // (u+1)^((2p-2)/3)
96 constexpr auto FP6_FROBENIUS_C2 = FieldElement2::_unchecked_from_words(
98 "14e56d3f1564853a14e4f04fe2db9068a20d1b8c7e88102450880866309b7e2c2af322533285a5d5890dc9e4867545c3"),
100 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
101
102 return Fp6(m_c0.conjugate(), m_c1.conjugate() * FP6_FROBENIUS_C1, m_c2.conjugate() * FP6_FROBENIUS_C2);
103}
104
105CT::Choice Fp6::operator==(const Fp6& other) const {
106 return (m_c0 == other.m_c0) && (m_c1 == other.m_c1) && (m_c2 == other.m_c2);
107}
108
109void Fp6::conditional_assign(CT::Choice cnd, const Fp6& other) {
110 m_c0.conditional_assign(cnd, other.m_c0);
111 m_c1.conditional_assign(cnd, other.m_c1);
112 m_c2.conditional_assign(cnd, other.m_c2);
113}
114
115} // namespace Botan::BLS12_381
static constexpr FieldElement2 _unchecked_from_words(std::array< word, FieldElement::N > c0, std::array< word, FieldElement::N > c1)
void conditional_assign(CT::Choice cnd, const Fp6 &other)
Fp6 mul_by_01(const FieldElement2 &b0, const FieldElement2 &b1) const
CT::Choice operator==(const Fp6 &other) const
const FieldElement2 & c1() const
Fp6 mul(const Fp6 &x) const
CT::Choice is_zero() const
Fp6 sub(const Fp6 &x) const
Fp6 mul_by_1(const FieldElement2 &b1) const
Fp6 mul_by_nonresidue() const
const FieldElement2 & c2() const
const FieldElement2 & c0() const
Fp6 add(const Fp6 &x) const
constexpr Fp6()=default
constexpr auto hex_to_words(const char(&s)[N])
Definition mp_core.h:921