2#include "barretenberg/ecc/curves/bn254/fq2.hpp"
3#include "barretenberg/numeric/uint256/uint256.hpp"
4#include "barretenberg/serialize/msgpack.hpp"
8namespace barretenberg::group_elements {
11template <
typename Fq,
typename Fr,
typename Params>
class alignas(64)
affine_element {
13 using in_buf =
const uint8_t*;
14 using vec_in_buf =
const uint8_t*;
15 using out_buf = uint8_t*;
16 using vec_out_buf = uint8_t**;
27 static constexpr affine_element one()
noexcept {
return { Params::one_x, Params::one_y }; };
38 template <
typename BaseField =
Fq,
39 typename CompileTimeEnabled = std::enable_if_t<(BaseField::modulus >> 255) ==
uint256_t(0),
void>>
51 template <
typename BaseField =
Fq,
52 typename CompileTimeEnabled = std::enable_if_t<(BaseField::modulus >> 255) ==
uint256_t(1),
void>>
61 template <
typename BaseField =
Fq,
62 typename CompileTimeEnabled = std::enable_if_t<(BaseField::modulus >> 255) ==
uint256_t(0),
void>>
63 [[nodiscard]]
constexpr uint256_t compress() const noexcept;
67 constexpr
void self_set_infinity() noexcept;
69 [[nodiscard]] constexpr
bool is_point_at_infinity() const noexcept;
71 [[nodiscard]] constexpr
bool on_curve() const noexcept;
73 static constexpr std::optional<
affine_element> derive_from_x_coordinate(const
Fq& x,
bool sign_bit) noexcept;
84 constexpr
bool operator==(const
affine_element& other) const noexcept;
86 constexpr
affine_element operator-() const noexcept {
return { x, -y }; }
89 constexpr bool operator<(
const affine_element& other)
const noexcept {
return (other > *
this); }
102 if (value.is_point_at_infinity()) {
103 if constexpr (Fq::modulus.get_msb() == 255) {
105 write(buffer, Fq::modulus);
111 Fq::serialize_to_buffer(value.y, buffer);
112 Fq::serialize_to_buffer(value.x, buffer +
sizeof(
Fq));
133 auto raw_x = from_buffer<uint256_t>(buffer +
sizeof(
Fq));
135 if constexpr (Fq::modulus.get_msb() == 255) {
136 if (raw_x == Fq::modulus) {
137 result.y = Fq::zero();
138 result.x.data[0] = raw_x.data[0];
139 result.x.data[1] = raw_x.data[1];
140 result.x.data[2] = raw_x.data[2];
141 result.x.data[3] = raw_x.data[3];
143 result.y = Fq::serialize_from_buffer(buffer);
144 result.x =
Fq(raw_x);
147 if (raw_x.get_msb() == 255) {
148 result.y = Fq::zero();
149 result.x = Fq::zero();
150 result.self_set_infinity();
153 if constexpr (std::is_same<Fq, fq2>::value) {
154 result.y = Fq::serialize_from_buffer(buffer);
155 result.x = Fq::serialize_from_buffer(buffer +
sizeof(
Fq));
157 result.y = Fq::serialize_from_buffer(buffer);
158 result.x =
Fq(raw_x);
170 [[nodiscard]]
inline std::vector<uint8_t>
to_buffer()
const
177 friend std::ostream& operator<<(std::ostream& os,
const affine_element& a)
179 os <<
"{ " << a.x <<
", " << a.y <<
" }";
185 MSGPACK_FIELDS(x, y);
189#include "./affine_element_impl.hpp"
Definition: affine_element.hpp:11
static void serialize_to_buffer(const affine_element &value, uint8_t *buffer)
Serialize the point to the given buffer.
Definition: affine_element.hpp:100
static affine_element serialize_from_buffer(uint8_t *buffer)
Restore point from a buffer.
Definition: affine_element.hpp:128
static constexpr affine_element hash_to_curve(const std::vector< uint8_t > &seed, uint8_t attempt_count=0) noexcept
Hash a seed buffer into a point.
Definition: affine_element_impl.hpp:224
static constexpr affine_element from_compressed(const uint256_t &compressed) noexcept
Reconstruct a point in affine coordinates from compressed form.
constexpr bool operator>(const affine_element &other) const noexcept
Definition: affine_element_impl.hpp:153
static affine_element random_element(numeric::random::Engine *engine=nullptr) noexcept
Samples a random point on the curve.
Definition: affine_element_impl.hpp:266
std::vector< uint8_t > to_buffer() const
Serialize the point to a byte vector.
Definition: affine_element.hpp:170
static constexpr std::array< affine_element, 2 > from_compressed_unsafe(const uint256_t &compressed) noexcept
Reconstruct a point in affine coordinates from compressed form.
Definition: uint256.hpp:25
Definition: affine_element.hpp:10
Definition: field2_declarations.hpp:6