Skip to content

Commit

Permalink
use_squares
Browse files Browse the repository at this point in the history
  • Loading branch information
Rumata888 committed Mar 28, 2024
1 parent d4e7070 commit 10ee871
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
20 changes: 20 additions & 0 deletions barretenberg/cpp/src/barretenberg/polynomials/univariate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ template <class Fr, size_t domain_end, size_t domain_start = 0> class Univariate
}
return *this;
}
Univariate& self_sqr()
{
for (size_t i = 0; i < LENGTH; ++i) {
evaluations[i].self_sqr();
}
return *this;
}
Univariate operator+(const Univariate& other) const
{
Univariate res(*this);
Expand Down Expand Up @@ -148,6 +155,13 @@ template <class Fr, size_t domain_end, size_t domain_start = 0> class Univariate
return res;
}

Univariate sqr() const
{
Univariate res(*this);
res.self_sqr();
return res;
}

// Operations between Univariate and scalar
Univariate& operator+=(const Fr& scalar)
{
Expand Down Expand Up @@ -404,6 +418,12 @@ template <class Fr, size_t domain_end, size_t domain_start = 0> class Univariate
res *= other;
return res;
}
Univariate<Fr, domain_end, domain_start> sqr() const
{
Univariate<Fr, domain_end, domain_start> res(*this);
res = res.sqr();
return res;
}

Univariate<Fr, domain_end, domain_start> operator*(const Univariate<Fr, domain_end, domain_start>& other) const
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ template <typename FF_> class DeltaRangeConstraintRelationImpl {
auto w_1_shift = View(in.w_l_shift);
auto q_delta_range = View(in.q_delta_range);

static const FF one = FF::one();
static const FF minus_one = FF(-1);
static const FF minus_two = FF(-2);
static const FF minus_three = FF(-3);
Expand All @@ -55,37 +56,29 @@ template <typename FF_> class DeltaRangeConstraintRelationImpl {
auto delta_4 = w_1_shift - w_4;

// Contribution (1)
auto tmp_1 = delta_1;
tmp_1 *= (delta_1 + minus_one);
tmp_1 *= (delta_1 + minus_two);
tmp_1 *= (delta_1 + minus_three);
auto tmp_1 = (delta_1 + minus_one).sqr() - one;
tmp_1 *= (delta_1 + minus_two).sqr() - one;
tmp_1 *= q_delta_range;
tmp_1 *= scaling_factor;
std::get<0>(accumulators) += tmp_1;

// Contribution (2)
auto tmp_2 = delta_2;
tmp_2 *= (delta_2 + minus_one);
tmp_2 *= (delta_2 + minus_two);
tmp_2 *= (delta_2 + minus_three);
auto tmp_2 = (delta_2 + minus_one).sqr() - one;
tmp_2 *= (delta_2 + minus_two).sqr() - one;
tmp_2 *= q_delta_range;
tmp_2 *= scaling_factor;
std::get<1>(accumulators) += tmp_2;

// Contribution (3)
auto tmp_3 = delta_3;
tmp_3 *= (delta_3 + minus_one);
tmp_3 *= (delta_3 + minus_two);
tmp_3 *= (delta_3 + minus_three);
auto tmp_3 = (delta_3 + minus_one).sqr() - one;
tmp_3 *= (delta_3 + minus_two).sqr() - one;
tmp_3 *= q_delta_range;
tmp_3 *= scaling_factor;
std::get<2>(accumulators) += tmp_3;

// Contribution (4)
auto tmp_4 = delta_4;
tmp_4 *= (delta_4 + minus_one);
tmp_4 *= (delta_4 + minus_two);
tmp_4 *= (delta_4 + minus_three);
auto tmp_4 = (delta_4 + minus_one).sqr() - one;
tmp_4 *= (delta_4 + minus_two).sqr() - one;
tmp_4 *= q_delta_range;
tmp_4 *= scaling_factor;
std::get<3>(accumulators) += tmp_4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ template <typename FF_> class EllipticRelationImpl {
// Contribution (1) point addition, x-coordinate check
// q_elliptic * (x3 + x2 + x1)(x2 - x1)(x2 - x1) - y2^2 - y1^2 + 2(y2y1)*q_sign = 0
auto x_diff = (x_2 - x_1);
auto y2_sqr = (y_2 * y_2);
auto y1_sqr = (y_1 * y_1);
auto y2_sqr = y_2.sqr();
auto y1_sqr = y_1.sqr();
auto y1y2 = y_1 * y_2 * q_sign;
auto x_add_identity = (x_3 + x_2 + x_1) * x_diff * x_diff - y2_sqr - y1_sqr + y1y2 + y1y2;
std::get<0>(accumulators) += x_add_identity * scaling_factor * q_elliptic * (-q_is_double + 1);
Expand All @@ -89,7 +89,8 @@ template <typename FF_> class EllipticRelationImpl {

// Contribution (4) point doubling, y-coordinate check
// (y1 + y1) (2y1) - (3 * x1 * x1)(x1 - x3) = 0
auto x1_sqr_mul_3 = (x_1 + x_1 + x_1) * x_1;
auto x1_sqr = x_1 * x_1;
auto x1_sqr_mul_3 = x1_sqr + x1_sqr + x1_sqr;
auto y_double_identity = x1_sqr_mul_3 * (x_1 - x_3) - (y_1 + y_1) * (y_1 + y_3);
std::get<1>(accumulators) += y_double_identity * scaling_factor * q_elliptic * q_is_double;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ template <typename FF_> class Poseidon2ExternalRelationImpl {
auto s4 = w_4 + q_4;

// apply s-box round
auto u1 = s1 * s1;
u1 *= u1;
auto u1 = s1.sqr();
u1 = u1.sqr();
u1 *= s1;
auto u2 = s2 * s2;
u2 *= u2;
auto u2 = s2.sqr();
u2 = u2.sqr();
u2 *= s2;
auto u3 = s3 * s3;
u3 *= u3;
auto u3 = s3.sqr();
u3 = u3.sqr();
u3 *= s3;
auto u4 = s4 * s4;
u4 *= u4;
auto u4 = s4.sqr();
u4 = u4.sqr();
u4 *= s4;

// matrix mul v = M_E * u with 14 additions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ template <typename FF_> class Poseidon2InternalRelationImpl {
auto s1 = w_l + q_l;

// apply s-box round
auto u1 = s1 * s1;
u1 *= u1;
auto u1 = s1.sqr();
u1 = u1.sqr();
u1 *= s1;
auto u2 = w_r;
auto u3 = w_o;
Expand Down

0 comments on commit 10ee871

Please sign in to comment.