2#include "./element.hpp"
3#include "barretenberg/crypto/blake3s/blake3s.hpp"
4#include "barretenberg/crypto/keccak/keccak.hpp"
6namespace barretenberg::group_elements {
7template <
class Fq,
class Fr,
class T>
13template <
class Fq,
class Fr,
class T>
14template <
typename BaseField,
typename CompileTimeEnabled>
18 x_coordinate.data[3] = x_coordinate.data[3] & (~0x8000000000000000ULL);
19 bool y_bit = compressed.get_bit(255);
21 Fq x =
Fq(x_coordinate);
22 Fq y2 = (x.
sqr() * x + T::b);
23 if constexpr (T::has_a) {
26 auto [is_quadratic_remainder, y] = y2.
sqrt();
27 if (!is_quadratic_remainder) {
37template <
class Fq,
class Fr,
class T>
38template <
typename BaseField,
typename CompileTimeEnabled>
42 auto get_y_coordinate = [](
const uint256_t& x_coordinate) {
43 Fq x =
Fq(x_coordinate);
44 Fq y2 = (x.
sqr() * x + T::b);
45 if constexpr (T::has_a) {
53 auto [is_quadratic_remainder_1, y_1] = get_y_coordinate(x_1);
54 auto [is_quadratic_remainder_2, y_2] = get_y_coordinate(x_2);
61 return { output_1, output_2 };
64template <
class Fq,
class Fr,
class T>
68 return affine_element(element<Fq, Fr, T>(*
this) + element<Fq, Fr, T>(other));
71template <
class Fq,
class Fr,
class T>
72template <
typename BaseField,
typename CompileTimeEnabled>
78 out.data[3] = out.data[3] | 0x8000000000000000ULL;
86 e.self_set_infinity();
90template <
class Fq,
class Fr,
class T>
94 result.self_set_infinity();
100 if constexpr (Fq::modulus.data[3] >= 0x4000000000000000ULL) {
102 x.data[0] = Fq::modulus.data[0];
103 x.data[1] = Fq::modulus.data[1];
104 x.data[2] = Fq::modulus.data[2];
105 x.data[3] = Fq::modulus.data[3];
114 if constexpr (Fq::modulus.data[3] >= 0x4000000000000000ULL) {
116 return ((x.data[0] ^ Fq::modulus.data[0]) | (x.data[1] ^ Fq::modulus.data[1]) |
117 (x.data[2] ^ Fq::modulus.data[2]) | (x.data[3] ^ Fq::modulus.data[3])) == 0;
120 return (x.is_msb_set());
126 if (is_point_at_infinity()) {
129 Fq xxx = x.
sqr() * x + T::b;
131 if constexpr (T::has_a) {
137template <
class Fq,
class Fr,
class T>
140 bool this_is_infinity = is_point_at_infinity();
141 bool other_is_infinity = other.is_point_at_infinity();
142 bool both_infinity = this_is_infinity && other_is_infinity;
143 bool only_one_is_infinity = this_is_infinity != other_is_infinity;
144 return !only_one_is_infinity && (both_infinity || ((x == other.x) && (y == other.y)));
152template <
class Fq,
class Fr,
class T>
156 if (is_point_at_infinity()) {
159 if (other.is_point_at_infinity()) {
166 if (x == other.x && y > other.y) {
172template <
class Fq,
class Fr,
class T>
174 const Fq& x,
bool sign_bit)
noexcept
176 auto yy = x.
sqr() * x + T::b;
177 if constexpr (T::has_a) {
180 auto [found_root, y] = yy.
sqrt();
183 if (
uint256_t(y).get_bit(0) != sign_bit) {
223template <
class Fq,
class Fr,
class T>
225 uint8_t attempt_count)
noexcept
229 std::vector<uint8_t> target_seed(seed);
231 const size_t seed_size = seed.size();
232 for (
size_t i = 0; i < 2; ++i) {
233 target_seed.push_back(0);
235 target_seed[seed_size] = attempt_count;
236 target_seed[seed_size + 1] = 0;
237 const auto hash_hi = blake3::blake3s_constexpr(&target_seed[0], target_seed.size());
238 target_seed[seed_size + 1] = 1;
239 const auto hash_lo = blake3::blake3s_constexpr(&target_seed[0], target_seed.size());
241 const auto read_uint256 = [](
const uint8_t* in) {
242 const auto read_limb = [](
const uint8_t* in, uint64_t& out) {
243 for (
size_t i = 0; i < 8; ++i) {
244 out +=
static_cast<uint64_t
>(in[i]) << ((7 - i) * 8);
248 read_limb(&in[0], out.data[3]);
249 read_limb(&in[8], out.data[2]);
250 read_limb(&in[16], out.data[1]);
251 read_limb(&in[24], out.data[0]);
256 Fq x(uint512_t(read_uint256(&hash_lo[0]), read_uint256(&hash_hi[0])));
257 bool sign_bit = hash_hi[0] > 127;
258 std::optional<affine_element> result = derive_from_x_coordinate(x, sign_bit);
259 if (result.has_value()) {
260 return result.value();
262 return hash_to_curve(seed, attempt_count + 1);
265template <
typename Fq,
typename Fr,
typename T>
268 if (engine ==
nullptr) {
269 engine = &numeric::random::get_engine();
276 x = Fq::random_element(engine);
278 bool sign_bit = (engine->get_random_uint8() & 1) != 0;
280 std::optional<affine_element> result = derive_from_x_coordinate(x, sign_bit);
282 if (result.has_value()) {
283 return result.value();
286 throw_or_abort(
"affine_element::random_element error");
Definition: affine_element.hpp:11
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
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: engine.hpp:10
Definition: uint256.hpp:25
Definition: affine_element.hpp:10
BBERG_INLINE constexpr field sqr() const noexcept
Definition: field_impl.hpp:61
constexpr std::pair< bool, field > sqrt() const noexcept
Compute square root of the field element.
Definition: field_impl.hpp:507