7#include <botan/bls12_381.h>
9#include <botan/exceptn.h>
10#include <botan/mem_ops.h>
11#include <botan/internal/bls12_381_point_mul.h>
12#include <botan/internal/bls12_381_tower.h>
13#include <botan/internal/ct_utils.h>
43LineEval doubling_step(MillerG2& r) {
45 const auto tmp0 = r.x.square();
46 const auto tmp1 = r.y.square();
47 const auto tmp2 = tmp1.square();
48 auto tmp3 = (tmp1 + r.x).
square() - tmp0 - tmp2;
50 const auto tmp4 = tmp0 + tmp0 + tmp0;
51 auto tmp6 = r.x + tmp4;
52 const auto tmp5 = tmp4.square();
53 const auto zsquared = r.z.square();
54 r.x = tmp5 - tmp3 - tmp3;
55 r.z = (r.z + r.y).
square() - tmp1 - zsquared;
56 r.y = (tmp3 - r.x) * tmp4;
57 auto tmp2_8 = tmp2 + tmp2;
58 tmp2_8 = tmp2_8 + tmp2_8;
59 tmp2_8 = tmp2_8 + tmp2_8;
61 auto ltmp3 = tmp4 * zsquared;
62 ltmp3 = ltmp3 + ltmp3;
63 ltmp3 = ltmp3.negate();
64 tmp6 = tmp6.square() - tmp0 - tmp5;
65 auto tmp1_4 = tmp1 + tmp1;
66 tmp1_4 = tmp1_4 + tmp1_4;
68 auto ltmp0 = r.z * zsquared;
69 ltmp0 = ltmp0 + ltmp0;
71 return LineEval{ltmp0, ltmp3, tmp6};
76 const auto zsquared = r.z.square();
77 const auto ysquared = qy.square();
78 const auto t0 = zsquared * qx;
79 const auto t1 = ((qy + r.z).square() - ysquared - zsquared) * zsquared;
80 const auto t2 = t0 - r.x;
81 const auto t3 = t2.square();
84 const auto t5 = t4 * t2;
85 const auto t6 = t1 - r.y - r.y;
87 const auto t7 = t4 * r.x;
88 r.x = t6.square() - t5 - t7 - t7;
89 r.z = (r.z + t2).
square() - zsquared - t3;
91 const auto t8 = (t7 - r.x) * t6;
95 t10 = t10.square() - ysquared;
96 const auto ztsquared = r.z.square();
97 t10 = t10 - ztsquared;
99 const auto lt10 = r.z + r.z;
100 const auto t6neg = t6.negate();
101 const auto lt1 = t6neg + t6neg;
103 return LineEval{lt10, lt1, t9};
107 const auto c0 =
FieldElement2(line.c0.c0() * py, line.c0.c1() * py);
108 const auto c1 =
FieldElement2(line.c1.c0() * px, line.c1.c1() * px);
110 return f.mul_by_014(line.c2, c1, c0);
113Fp12 multi_miller_loop(std::span<PairingTerm> terms) {
116 bool found_one =
false;
117 for(
size_t b = 64; b > 0; --b) {
118 const bool i = (((
BLS_Z_ABS >> 1) >> (b - 1)) & 1) == 1;
124 for(
auto& term : terms) {
125 f = ell(f, doubling_step(term.cur), term.px, term.py);
129 for(
auto& term : terms) {
130 f = ell(f, addition_step(term.cur, term.qx, term.qy), term.px, term.py);
137 for(
auto& term : terms) {
138 f = ell(f, doubling_step(term.cur), term.px, term.py);
142 return f.conjugate();
149 bool found_one =
false;
150 for(
size_t b = 64; b > 0; --b) {
151 const bool i = ((
BLS_Z_ABS >> (b - 1)) & 1) == 1;
153 tmp = tmp.cyclotomic_square();
163 return tmp.conjugate();
166Fp12 final_exponentiation(
const Fp12& ml) {
178 auto t3 = cyclotomic_exp(t2);
179 auto t4 = t3.cyclotomic_square();
181 t1 = cyclotomic_exp(t5);
182 t0 = cyclotomic_exp(t1);
183 auto t6 = cyclotomic_exp(t0);
185 t4 = cyclotomic_exp(t6);
190 t1 = t1.frobenius_map().frobenius_map().frobenius_map();
192 t6 = t6.frobenius_map();
194 t3 = t3.frobenius_map().frobenius_map();
203 const auto qx = q._x();
204 const auto qy = q._y();
210Gt::Gt(
const Fp12& v) : m_coeffs{} {
211 constexpr size_t N = FieldElement::N;
213 const std::array<const FieldElement2*, 6> c = {
214 &v.c0().c0(), &v.c0().c1(), &v.c0().c2(), &v.c1().c0(), &v.c1().c1(), &v.c1().c2()};
216 for(
size_t i = 0; i != c.size(); ++i) {
217 copy_mem(m_coeffs.data() + (2 * i) * N, c[i]->c0()._words().data(), N);
218 copy_mem(m_coeffs.data() + (2 * i + 1) * N, c[i]->c1()._words().data(), N);
222Fp12 Gt::_to_fp12()
const {
223 constexpr size_t N = FieldElement::N;
225 std::array<FieldElement2, 6> c;
226 for(
size_t i = 0; i != c.size(); ++i) {
227 std::array<word, N> c0{};
228 std::array<word, N> c1{};
229 copy_mem(c0.data(), m_coeffs.data() + (2 * i) * N, N);
230 copy_mem(c1.data(), m_coeffs.data() + (2 * i + 1) * N, N);
231 c[i] = FieldElement2::_unchecked_from_words(c0, c1);
234 return Fp12(Fp6(c[0], c[1], c[2]), Fp6(c[3], c[4], c[5]));
248 return CT::is_equal(m_coeffs.data(), other.m_coeffs.data(), m_coeffs.size()).as_bool();
252 return this->_to_fp12().serialize();
260 std::array<PairingTerm, 1> terms = {make_term(p, q)};
262 return Gt(final_exponentiation(multi_miller_loop(terms)));
266 if(p.size() != q.size()) {
267 throw Invalid_Argument(
"BLS12_381::multi_pairing spans must have equal length");
270 std::vector<PairingTerm> terms;
271 terms.reserve(p.size());
273 for(
size_t i = 0; i != p.size(); ++i) {
278 terms.push_back(make_term(p[i], q[i]));
285 return Gt(final_exponentiation(multi_miller_loop(terms)));
static FieldElement2 one()
Fp12 cyclotomic_square() const
Fp12 frobenius_map() const
std::array< uint8_t, BYTES > serialize() const
static Gt pairing(const G1Affine &p, const G2Affine &q)
static Gt multi_pairing(std::span< const G1Affine > p, std::span< const G2Affine > q)
bool operator==(const Gt &other) const
constexpr uint64_t BLS_Z_ABS
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
BigInt square(const BigInt &x)
constexpr void copy_mem(T *out, const T *in, size_t n)