Skip to content

Commit

Permalink
comments, multithreading sort, and inlining add method
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Jul 11, 2024
1 parent 6a2c1d7 commit 1341c62
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ MsmSorter<Curve>::AdditionSequences MsmSorter<Curve>::construct_addition_sequenc
// Create the array containing the indices of the scalars and points sorted by scalar value
const size_t num_points = points.size();
std::iota(index.begin(), index.end(), 0);
#ifdef NO_TBB
std::sort(index.begin(), index.end(), [&](size_t idx_1, size_t idx_2) { return scalars[idx_1] < scalars[idx_2]; });
#else
std::sort(std::execution::par_unseq, index.begin(), index.end(), [&](size_t idx_1, size_t idx_2) {
return scalars[idx_1] < scalars[idx_2];
});
#endif

// Store the unique scalar values, the input points sorted by scalar value, and the number of occurences of each
// unique scalar (i.e. the size of each addition sequence)
Expand Down Expand Up @@ -144,34 +150,6 @@ void MsmSorter<Curve>::batch_compute_point_addition_slope_inverses(AdditionSeque
}
}

/**
* @brief Add two affine elements with the inverse in the slope term \lambda provided as input
* @details The sum of two points (x1, y1), (x2, y2) is given by x3 = \lambda^2 - x1 - x2, y3 = \lambda*(x1 - x3) - y1,
* where \lambda = (y2 - y1)/(x2 - x1). When performing many additions at once, it is more efficient to batch compute
* the inverse component of \lambda for each pair of points. This gives rise to the need for a method like this one.
*
* @tparam Curve
* @param point_1 (x1, y1)
* @param point_2 (x2, y2)
* @param denominator 1/(x2 - x1)
* @return Curve::AffineElement
*/
template <typename Curve>
typename Curve::AffineElement MsmSorter<Curve>::affine_add_with_denominator(const G1& point_1,
const G1& point_2,
const Fq& denominator)
{
const auto& x1 = point_1.x;
const auto& y1 = point_1.y;
const auto& x2 = point_2.x;
const auto& y2 = point_2.y;

const Fq lambda = denominator * (y2 - y1);
Fq x3 = lambda.sqr() - x2 - x1;
Fq y3 = lambda * (x1 - x3) - y1;
return { x3, y3 };
}

/**
* @brief In-place summation to reduce a set of addition sequences to a single point for each sequence
* @details At each round, the set of points in each addition sequence is roughly halved by performing pairwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,37 @@ template <typename Curve> class MsmSorter {

ReducedMsmInputs reduce_msm_inputs(std::span<Fr> scalars, std::span<G1> points);

G1 affine_add_with_denominator(const G1&, const G1&, const Fq& denominator);

void batch_compute_point_addition_slope_inverses(AdditionSequences& add_sequences);

void batched_affine_add_in_place(AdditionSequences addition_sequences);

AdditionSequences construct_addition_sequences(std::span<Fr> scalars, std::span<G1> points);

/**
* @brief Add two affine elements with the inverse in the slope term \lambda provided as input
* @details The sum of two points (x1, y1), (x2, y2) is given by x3 = \lambda^2 - x1 - x2, y3 = \lambda*(x1 - x3) -
* y1, where \lambda = (y2 - y1)/(x2 - x1). When performing many additions at once, it is more efficient to batch
* compute the inverse component of \lambda for each pair of points. This gives rise to the need for a method like
* this one.
*
* @tparam Curve
* @param point_1 (x1, y1)
* @param point_2 (x2, y2)
* @param denominator 1/(x2 - x1)
* @return Curve::AffineElement
*/
inline G1 affine_add_with_denominator(const G1& point_1, const G1& point_2, const Fq& denominator)
{
const auto& x1 = point_1.x;
const auto& y1 = point_1.y;
const auto& x2 = point_2.x;
const auto& y2 = point_2.y;

const Fq lambda = denominator * (y2 - y1);
Fq x3 = lambda.sqr() - x2 - x1;
Fq y3 = lambda * (x1 - x3) - y1;
return { x3, y3 };
}
};

} // namespace bb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ TYPED_TEST(SortedMsmTests, GenerateAdditionSequences)
EXPECT_EQ(msm_result, expected_msm_result);
}

// Test that the method reduce_msm_inputs can reduce a set of {points, scalars} with duplicate scalars to a reduced set
// of inputs {points', scalars'} such that all scalars in scalars' are unique and that perfoming the MSM on the reduced
// inputs yields the same result as with the original inputs
TYPED_TEST(SortedMsmTests, ReduceMsmInputsSimple)
{
using Curve = TypeParam;
Expand All @@ -225,6 +228,9 @@ TYPED_TEST(SortedMsmTests, ReduceMsmInputsSimple)
EXPECT_EQ(msm_result, expected_msm_result);
}

// Test that the method reduce_msm_inputs can reduce a set of {points, scalars} with duplicate scalars to a reduced set
// of inputs {points', scalars'} such that all scalars in scalars' are unique and that perfoming the MSM on the reduced
// inputs yields the same result as with the original inputs
TYPED_TEST(SortedMsmTests, ReduceMsmInputs)
{
using Curve = TypeParam;
Expand Down

0 comments on commit 1341c62

Please sign in to comment.