Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#include <vector>

namespace bb::group_elements {

// MSB of the top 64-bit limb in a uint256_t (bit 255). Used in point compression to encode the
// y-coordinate parity bit, and cleared when recovering the x-coordinate.
static constexpr uint64_t UINT256_TOP_LIMB_MSB = 0x8000000000000000ULL;

template <typename T>
concept SupportsHashToCurve = T::can_hash_to_curve;
template <typename Fq_, typename Fr_, typename Params_> class alignas(64) affine_element {
Expand Down Expand Up @@ -80,10 +85,6 @@ template <typename Fq_, typename Fr_, typename Params_> class alignas(64) affine

constexpr affine_element operator*(const Fr& exponent) const noexcept;

template <typename BaseField = Fq,
typename CompileTimeEnabled = std::enable_if_t<(BaseField::modulus >> 255) == uint256_t(0), void>>
[[nodiscard]] constexpr uint256_t compress() const noexcept;

static constexpr affine_element infinity();
constexpr affine_element set_infinity() const noexcept;
constexpr void self_set_infinity() noexcept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ template <typename G1> class TestAffineElement : public testing::Test {
{
for (size_t i = 0; i < 10; i++) {
affine_element P = affine_element(element::random_element());
uint256_t compressed = P.compress();
uint256_t compressed = uint256_t(P.x);
if (uint256_t(P.y).get_bit(0)) {
compressed.data[3] |= group_elements::UINT256_TOP_LIMB_MSB;
}
affine_element Q = affine_element::from_compressed(compressed);
EXPECT_EQ(P, Q);
}
Expand Down Expand Up @@ -168,8 +171,6 @@ template <typename G1> class TestAffineElement : public testing::Test {
affine_element R(0, P.y);
ASSERT_FALSE(P == R);
}
// Regression test to ensure that the point at infinity is not equal to its coordinate-wise reduction, which may lie
// on the curve, depending on the y-coordinate.
static void test_infinity_ordering_regression()
{
affine_element P(0, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ template <typename BaseField, typename CompileTimeEnabled>
constexpr affine_element<Fq, Fr, T> affine_element<Fq, Fr, T>::from_compressed(const uint256_t& compressed) noexcept
{
uint256_t x_coordinate = compressed;
x_coordinate.data[3] = x_coordinate.data[3] & (~0x8000000000000000ULL);
x_coordinate.data[3] = x_coordinate.data[3] & (~UINT256_TOP_LIMB_MSB);
bool y_bit = compressed.get_bit(255);

Fq x = Fq(x_coordinate);
Expand Down Expand Up @@ -80,18 +80,6 @@ constexpr affine_element<Fq, Fr, T> affine_element<Fq, Fr, T>::operator*(const F
return bb::group_elements::element(*this) * exponent;
}

template <class Fq, class Fr, class T>
template <typename BaseField, typename CompileTimeEnabled>

constexpr uint256_t affine_element<Fq, Fr, T>::compress() const noexcept
{
uint256_t out(x);
if (uint256_t(y).get_bit(0)) {
out.data[3] = out.data[3] | 0x8000000000000000ULL;
}
return out;
}

template <class Fq, class Fr, class T> constexpr affine_element<Fq, Fr, T> affine_element<Fq, Fr, T>::infinity()
{
affine_element e{};
Expand Down Expand Up @@ -157,15 +145,9 @@ constexpr bool affine_element<Fq, Fr, T>::operator==(const affine_element& other
return !only_one_is_infinity && (both_infinity || ((x == other.x) && (y == other.y)));
}

/**
* Comparison operators (for std::sort)
*
* @details CAUTION!! Don't use this operator. It has no meaning other than for use by std::sort.
**/
template <class Fq, class Fr, class T>
constexpr bool affine_element<Fq, Fr, T>::operator>(const affine_element& other) const noexcept
{
// We are setting point at infinity to always be the lowest element
if (is_point_at_infinity()) {
return false;
}
Expand Down
26 changes: 0 additions & 26 deletions barretenberg/cpp/src/barretenberg/ecc/groups/element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ template <class Fq, class Fr, class Params> class alignas(32) element {

constexpr element dbl() const noexcept;
constexpr void self_dbl() noexcept;
constexpr void self_mixed_add_or_sub(const affine_element<Fq, Fr, Params>& other, uint64_t predicate) noexcept;

constexpr element operator+(const element& other) const noexcept;
constexpr element operator+(const affine_element<Fq, Fr, Params>& other) const noexcept;
Expand Down Expand Up @@ -128,27 +127,6 @@ template <class Fq, class Fr, class Params> class alignas(32) element {

template <typename = typename std::enable_if<Params::can_hash_to_curve>>
static element random_coordinates_on_curve(numeric::RNG* engine = nullptr) noexcept;
// {
// bool found_one = false;
// Fq yy;
// Fq x;
// Fq y;
// Fq t0;
// while (!found_one) {
// x = Fq::random_element(engine);
// yy = x.sqr() * x + Params::b;
// if constexpr (Params::has_a) {
// yy += (x * Params::a);
// }
// y = yy.sqrt();
// t0 = y.sqr();
// found_one = (yy == t0);
// }
// return { x, y, Fq::one() };
// }
static void conditional_negate_affine(const affine_element<Fq, Fr, Params>& in,
affine_element<Fq, Fr, Params>& out,
uint64_t predicate) noexcept;

friend std::ostream& operator<<(std::ostream& os, const element& a)
{
Expand All @@ -162,10 +140,6 @@ template <class Fq, class Fr, class Params> std::ostream& operator<<(std::ostrea
return os << "x:" << e.x << " y:" << e.y << " z:" << e.z;
}

// constexpr element<Fq, Fr, Params>::one = element<Fq, Fr, Params>{ Params::one_x, Params::one_y, Fq::one() };
// constexpr element<Fq, Fr, Params>::point_at_infinity = one.set_infinity();
// constexpr element<Fq, Fr, Params>::curve_b = Params::b;

} // namespace bb::group_elements

#include "./element_impl.hpp"
101 changes: 0 additions & 101 deletions barretenberg/cpp/src/barretenberg/ecc/groups/element_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,99 +155,6 @@ template <class Fq, class Fr, class T> constexpr element<Fq, Fr, T> element<Fq,
return result;
}

template <class Fq, class Fr, class T>
constexpr void element<Fq, Fr, T>::self_mixed_add_or_sub(const affine_element<Fq, Fr, T>& other,
const uint64_t predicate) noexcept
{
if constexpr (Fq::modulus.data[3] >= MODULUS_TOP_LIMB_LARGE_THRESHOLD) {
if (is_point_at_infinity()) {
conditional_negate_affine(other, *(affine_element<Fq, Fr, T>*)this, predicate); // NOLINT
z = Fq::one();
return;
}
} else {
const bool edge_case_trigger = x.is_msb_set() || other.x.is_msb_set();
if (edge_case_trigger) {
if (x.is_msb_set()) {
conditional_negate_affine(other, *(affine_element<Fq, Fr, T>*)this, predicate); // NOLINT
z = Fq::one();
}
return;
}
}

// T0 = z1.z1
Fq T0 = z.sqr();

// T1 = x2.t0 - x1 = x2.z1.z1 - x1
Fq T1 = other.x * T0;
T1 -= x;

// T2 = T0.z1 = z1.z1.z1
// T2 = T2.y2 - y1 = y2.z1.z1.z1 - y1
Fq T2 = z * T0;
T2 *= other.y;
T2.self_conditional_negate(predicate);
T2 -= y;

if (__builtin_expect(T1.is_zero(), 0)) {
if (T2.is_zero()) {
// y2 equals y1, x2 equals x1, double x1
self_dbl();
return;
}
self_set_infinity();
return;
}

// T2 = 2T2 = 2(y2.z1.z1.z1 - y1) = R
// z3 = z1 + H
T2 += T2;
z += T1;

// T3 = T1*T1 = HH
Fq T3 = T1.sqr();

// z3 = z3 - z1z1 - HH
T0 += T3;

// z3 = (z1 + H)*(z1 + H)
z.self_sqr();
z -= T0;

// T3 = 4HH
T3 += T3;
T3 += T3;

// T1 = T1*T3 = 4HHH
T1 *= T3;

// T3 = T3 * x1 = 4HH*x1
T3 *= x;

// T0 = 2T3
T0 = T3 + T3;

// T0 = T0 + T1 = 2(4HH*x1) + 4HHH
T0 += T1;
x = T2.sqr();

// x3 = x3 - T0 = R*R - 8HH*x1 -4HHH
x -= T0;

// T3 = T3 - x3 = 4HH*x1 - x3
T3 -= x;

T1 *= y;
T1 += T1;

// T3 = T2 * T3 = R*(4HH*x1 - x3)
T3 *= T2;

// y3 = T3 - T1
y = T3 - T1;
}

template <class Fq, class Fr, class T>
constexpr element<Fq, Fr, T> element<Fq, Fr, T>::operator+=(const affine_element<Fq, Fr, T>& other) noexcept
{
Expand Down Expand Up @@ -1057,14 +964,6 @@ std::vector<affine_element<Fq, Fr, T>> element<Fq, Fr, T>::batch_mul_with_endomo
return work_elements;
}

template <typename Fq, typename Fr, typename T>
void element<Fq, Fr, T>::conditional_negate_affine(const affine_element<Fq, Fr, T>& in,
affine_element<Fq, Fr, T>& out,
const uint64_t predicate) noexcept
{
out = { in.x, predicate ? -in.y : in.y };
}

template <typename Fq, typename Fr, typename T>
void element<Fq, Fr, T>::batch_normalize(element* elements, const size_t num_elements) noexcept
{
Expand Down
10 changes: 0 additions & 10 deletions barretenberg/cpp/src/barretenberg/ecc/groups/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,6 @@ template <typename Fq_, typename Fr_, typename Params> class group {
}
return derive_generators(domain_bytes, num_generators, starting_index);
}

BB_INLINE static void conditional_negate_affine(const affine_element* src,
affine_element* dest,
uint64_t predicate);
};

} // namespace bb

#ifdef DISABLE_ASM
#include "group_impl_int128.tcc"
#else
#include "group_impl_asm.tcc"
#endif
Loading
Loading