Botan 3.14.1
Crypto and TLS for C&
Botan::BLS12_381::Fp12 Class Referencefinal

#include <bls12_381_tower.h>

Public Member Functions

Fp12 add (const Fp12 &x) const
const Fp6 & c0 () const
const Fp6 & c1 () const
void conditional_assign (CT::Choice cnd, const Fp12 &other)
Fp12 conjugate () const
Fp12 cyclotomic_square () const
constexpr Fp12 ()=default
constexpr Fp12 (const Fp6 &c0, const Fp6 &c1)
Fp12 frobenius_map () const
Fp12 invert () const
CT::Choice is_zero () const
Fp12 mul (const Fp12 &x) const
Fp12 mul_by_014 (const FieldElement2 &b0, const FieldElement2 &b1, const FieldElement2 &b4) const
Fp12 negate () const
CT::Choice operator== (const Fp12 &other) const
std::array< uint8_t, Fp12::BYTES > serialize () const
void serialize_to (std::span< uint8_t, Fp12::BYTES > bytes) const
Fp12 square () const
Fp12 sub (const Fp12 &x) const

Static Public Member Functions

static Fp12 one ()
static Fp12 zero ()

Static Public Attributes

static constexpr size_t BYTES = 12 * FieldElement::BYTES

Detailed Description

An element of Fp12 = Fp6[w]/(w^2 - v)

Represented as c0 + c1*w

Definition at line 101 of file bls12_381_tower.h.

Constructor & Destructor Documentation

◆ Fp12() [1/2]

Botan::BLS12_381::Fp12::Fp12 ( )
constexprdefault

◆ Fp12() [2/2]

Botan::BLS12_381::Fp12::Fp12 ( const Fp6 & c0,
const Fp6 & c1 )
inlineconstexpr

Definition at line 108 of file bls12_381_tower.h.

108: m_c0(c0), m_c1(c1) {}
const Fp6 & c1() const
const Fp6 & c0() const

References c0(), and c1().

Member Function Documentation

◆ add()

Fp12 Botan::BLS12_381::Fp12::add ( const Fp12 & x) const

Definition at line 40 of file bls12_381_fp12.cpp.

40 {
41 return Fp12(m_c0 + x.m_c0, m_c1 + x.m_c1);
42}
constexpr Fp12()=default

References Fp12().

Referenced by Botan::BLS12_381::operator+().

◆ c0()

const Fp6 & Botan::BLS12_381::Fp12::c0 ( ) const
inline

Definition at line 114 of file bls12_381_tower.h.

114{ return m_c0; }

Referenced by cyclotomic_square(), Fp12(), frobenius_map(), and square().

◆ c1()

const Fp6 & Botan::BLS12_381::Fp12::c1 ( ) const
inline

Definition at line 116 of file bls12_381_tower.h.

116{ return m_c1; }

Referenced by cyclotomic_square(), Fp12(), frobenius_map(), mul(), mul_by_014(), and square().

◆ conditional_assign()

void Botan::BLS12_381::Fp12::conditional_assign ( CT::Choice cnd,
const Fp12 & other )

Definition at line 150 of file bls12_381_fp12.cpp.

150 {
151 m_c0.conditional_assign(cnd, other.m_c0);
152 m_c1.conditional_assign(cnd, other.m_c1);
153}

References Fp12().

◆ conjugate()

Fp12 Botan::BLS12_381::Fp12::conjugate ( ) const

Conjugation c0 - c1*w; for elements of the cyclotomic subgroup (such as pairing values) this is the inverse

Definition at line 75 of file bls12_381_fp12.cpp.

75 {
76 return Fp12(m_c0, m_c1.negate());
77}

References Fp12().

◆ cyclotomic_square()

Fp12 Botan::BLS12_381::Fp12::cyclotomic_square ( ) const

Squaring specialized for elements of the cyclotomic subgroup

Definition at line 102 of file bls12_381_fp12.cpp.

102 {
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}

References Botan::BLS12_381::FieldElement2::c0(), c0(), c1(), Botan::BLS12_381::Fp6::c1(), Fp12(), Botan::BLS12_381::FieldElement2::square(), and square().

◆ frobenius_map()

Fp12 Botan::BLS12_381::Fp12::frobenius_map ( ) const

The Frobenius endomorphism x -> x^p

Definition at line 87 of file bls12_381_fp12.cpp.

87 {
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}
static constexpr FieldElement2 _unchecked_from_words(std::array< word, FieldElement::N > c0, std::array< word, FieldElement::N > c1)
constexpr auto hex_to_words(const char(&s)[N])
Definition mp_core.h:921

References Botan::BLS12_381::FieldElement2::_unchecked_from_words(), c0(), c1(), Fp12(), and Botan::hex_to_words().

◆ invert()

Fp12 Botan::BLS12_381::Fp12::invert ( ) const

Return the inverse of this element, or zero for zero

Definition at line 70 of file bls12_381_fp12.cpp.

70 {
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}

References Fp12(), invert(), and negate().

Referenced by invert().

◆ is_zero()

CT::Choice Botan::BLS12_381::Fp12::is_zero ( ) const

Definition at line 13 of file bls12_381_fp12.cpp.

13 {
14 return m_c0.is_zero() && m_c1.is_zero();
15}

◆ mul()

Fp12 Botan::BLS12_381::Fp12::mul ( const Fp12 & x) const

Definition at line 52 of file bls12_381_fp12.cpp.

52 {
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}

References c1(), and Fp12().

Referenced by Botan::BLS12_381::operator*().

◆ mul_by_014()

Fp12 Botan::BLS12_381::Fp12::mul_by_014 ( const FieldElement2 & b0,
const FieldElement2 & b1,
const FieldElement2 & b4 ) const

Sparse multiplication by b = (b0 + b1*v)*1 + (b4*v)*w, the shape produced by pairing line evaluations

Definition at line 79 of file bls12_381_fp12.cpp.

79 {
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}

References c1(), and Fp12().

◆ negate()

Fp12 Botan::BLS12_381::Fp12::negate ( ) const

Definition at line 48 of file bls12_381_fp12.cpp.

48 {
49 return Fp12(m_c0.negate(), m_c1.negate());
50}

References Fp12().

Referenced by invert().

◆ one()

Fp12 Botan::BLS12_381::Fp12::one ( )
inlinestatic

◆ operator==()

CT::Choice Botan::BLS12_381::Fp12::operator== ( const Fp12 & other) const

Definition at line 146 of file bls12_381_fp12.cpp.

146 {
147 return (m_c0 == other.m_c0) && (m_c1 == other.m_c1);
148}

References Fp12().

◆ serialize()

std::array< uint8_t, Fp12::BYTES > Botan::BLS12_381::Fp12::serialize ( ) const
inline

Definition at line 127 of file bls12_381_tower.h.

127 {
128 std::array<uint8_t, Fp12::BYTES> buf{};
129 this->serialize_to(buf);
130 return buf;
131 }
void serialize_to(std::span< uint8_t, Fp12::BYTES > bytes) const

References serialize_to().

◆ serialize_to()

void Botan::BLS12_381::Fp12::serialize_to ( std::span< uint8_t, Fp12::BYTES > bytes) const

Serialization for tests and Gt; the coefficients are encoded in the order c0.c0.c0, c0.c0.c1, c0.c1.c0, ..., c1.c2.c1 with each Fp coefficient as 48 big-endian bytes

Definition at line 17 of file bls12_381_fp12.cpp.

17 {
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}
static constexpr size_t BYTES

References Botan::BLS12_381::FieldElement::BYTES.

Referenced by serialize().

◆ square()

Fp12 Botan::BLS12_381::Fp12::square ( ) const

Definition at line 61 of file bls12_381_fp12.cpp.

61 {
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}

References c0(), c1(), and Fp12().

Referenced by cyclotomic_square().

◆ sub()

Fp12 Botan::BLS12_381::Fp12::sub ( const Fp12 & x) const

Definition at line 44 of file bls12_381_fp12.cpp.

44 {
45 return Fp12(m_c0 - x.m_c0, m_c1 - x.m_c1);
46}

References Fp12().

Referenced by Botan::BLS12_381::operator-().

◆ zero()

Fp12 Botan::BLS12_381::Fp12::zero ( )
inlinestatic

Definition at line 110 of file bls12_381_tower.h.

110{ return Fp12(); }

References Fp12().

Member Data Documentation

◆ BYTES

size_t Botan::BLS12_381::Fp12::BYTES = 12 * FieldElement::BYTES
staticconstexpr

Definition at line 103 of file bls12_381_tower.h.


The documentation for this class was generated from the following files: