7#include <botan/bls12_381.h>
9#include <botan/mem_ops.h>
10#include <botan/internal/bls12_381_fields.h>
11#include <botan/internal/bls12_381_group.h>
12#include <botan/internal/bls12_381_point_mul.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/mp_core.h>
20FieldElement2 fe2_load(
const std::array<word, 2 * FieldElement::N>& w) {
21 std::array<word, FieldElement::N> c0{};
22 std::array<word, FieldElement::N> c1{};
23 copy_mem(c0.data(), w.data(), c0.size());
24 copy_mem(c1.data(), w.data() + c0.size(), c1.size());
28std::array<word, 2 * FieldElement::N> fe2_store(
const FieldElement2& v) {
29 std::array<word, 2 * FieldElement::N> w{};
38 "058191924350bcd76f67b7631863366b9894999d1a3caee9a1a893b53e2ae580b3f5fb2687b4961af5f28fa202940a10"),
40 "11922a097360edf3c2b6ed0ef21585471b1ab6cc8541b3673bb17e18e2867806aaa0c59dbccd60c3a5a9c0759e23f606"));
44 "0083fd8e7e80dae507d3a975f0ef25a2bbefb5e96e0d495fe7e6856caa0a635a597cfa1f5e369c5a4c730af860494c4a"),
46 "0b2bc2a163de1bf2e7175850a43ccaed79495c4ec93da33a86adac6a3be4eba018aa270a2b1461dcadc0fc92df64b05d"));
51 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
53 "14e56d3f1564853a14e4f04fe2db9068a20d1b8c7e88102450880866309b7e2c2af322533285a5d5890dc9e4867545c3"));
56 "0bd592fc7d825ec81d794e4fac7cf0b992ad2afd19103e18382844c88b6237324294213d86c181833e2f585da55c9ad1"),
58 "0e2b7eedbbfd87d22da2596696cebc1dd1ca2087da74d4a72f088dd86b4ebef1dc17dec12a927e7c7bcfa7a25aa30fda"));
63 "18f020655463874103f97d6e83d050d28eb60ebe01bacb9e587042afd3851b955dab22461fcda5d2cd03c9e48671f071"),
65 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
71 using Affine = G2Affine;
72 using Projective = G2Projective;
73 using FE = FieldElement2;
75 static FE load(
const std::array<word, 2 * FieldElement::N>& w) {
return fe2_load(w); }
77 static std::array<word, 2 * FieldElement::N> store(
const FE& v) {
return fe2_store(v); }
79 static FE mul_by_3b(
const FE& fe) {
81 const auto fe2 = fe + fe;
82 const auto fe4 = fe2 + fe2;
83 return (fe4 + fe4 + fe4).mul_by_nonresidue();
88 return FieldElement2(four, four);
97 m_x(fe2_store(x)), m_y(fe2_store(y)), m_infinity(infinity) {}
106 return G2Affine(G2_X, G2_Y, 0);
110 return fe2_load(m_x);
114 return fe2_load(m_y);
118 return m_infinity != 0;
123 auto pt = G2Ops::deserialize_unchecked(bytes);
124 if(!pt || pt->is_identity()) {
135 const auto psi_x = pt->_x().conjugate() * G2_PSI_X;
136 const auto psi_y = pt->_y().conjugate() * G2_PSI_Y;
139 const auto z_p_z = fe2_load(z_p.m_z);
140 const auto x_eq = (fe2_load(z_p.m_x) == psi_x * z_p_z);
141 const auto y_eq = (fe2_load(z_p.m_y) == psi_y * z_p_z);
142 if(!(x_eq && y_eq).as_bool()) {
150 return G2Ops::serialize(*
this);
154 m_x(fe2_store(x)), m_y(fe2_store(y)), m_z(fe2_store(z)) {}
160 return G2Ops::from_affine(affine);
174 return G2Ops::to_affine(*
this);
179 return G2Ops::to_affine_batch(points);
183 return G2Ops::negate(*
this);
186void G2Projective::conditional_negate(uint32_t negate) {
187 G2Ops::conditional_negate(*
this,
negate);
195 return G2Ops::dbl(*
this);
199 return G2Ops::add(*
this, other);
203 return G2Ops::add_mixed(*
this, other,
false);
207 return G2Ops::add_mixed(*
this, other, negate_other);
234 fe2_load(m_x).conjugate() * G2_PSI_X, fe2_load(m_y).conjugate() * G2_PSI_Y, fe2_load(m_z).conjugate());
237G2Projective G2Projective::psi2()
const {
238 return G2Projective(fe2_load(m_x) * G2_PSI2_X, fe2_load(m_y).
negate(), fe2_load(m_z));
241G2Projective G2Projective::clear_cofactor()
const {
246 const auto t2 = this->psi();
248 auto r = this->dbl().psi2();
250 r = r.
add(t1.negate());
251 r = r.
add(t2.negate());
static FieldElement2 one()
static FieldElement2 zero()
static constexpr FieldElement2 _unchecked_from_words(std::array< word, FieldElement::N > c0, std::array< word, FieldElement::N > c1)
static constexpr size_t N
static FieldElement from_u32(uint32_t v)
static G2Affine generator()
std::array< uint8_t, G2Affine::BYTES > serialize() const
static G2Affine identity()
static std::optional< G2Affine > deserialize(std::span< const uint8_t > bytes)
static G2Projective generator()
static G2Projective mul2_vartime(const G2Projective &p, const Scalar &a, const G2Projective &q, const Scalar &b)
static G2Projective _unchecked_from_affine_coords(const FieldElement2 &x, const FieldElement2 &y)
static G2Projective mul2(const G2Projective &p, const Scalar &a, const G2Projective &q, const Scalar &b)
static G2Projective msm_vartime(std::span< const G2Affine > points, std::span< const Scalar > scalars)
G2Projective add_mixed(const G2Affine &other) const
G2Projective mul(const Scalar &scalar) const
G2Affine to_affine() const
G2Projective add(const G2Projective &other) const
static G2Projective from_affine(const G2Affine &affine)
G2Projective negate() const
static std::vector< G2Affine > to_affine_batch(std::span< const G2Projective > points)
static Pt mul_by_z_abs(const Pt &pt)
static Pt mul2_vartime(const Pt &p, const Scalar &a, const Pt &q, const Scalar &b)
static Pt msm_vartime(std::span< const AffinePt > points, std::span< const Scalar > scalars)
static Pt mul2(const Pt &p, const Scalar &a, const Pt &q, const Scalar &b)
static Pt mul(const Pt &pt, const Scalar &scalar)
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr auto hex_to_words(const char(&s)[N])