Skip to content

Commit

Permalink
remove unneeded else in batch mul
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Aug 25, 2023
1 parent f6ba9a2 commit 6d654db
Showing 1 changed file with 38 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -604,48 +604,47 @@ element<C, Fq, Fr, G> element<C, Fq, Fr, G>::batch_mul(const std::vector<element
{
if constexpr (use_goblin) {
return goblin_batch_mul(points, scalars);
} else {
const size_t num_points = points.size();
ASSERT(scalars.size() == num_points);
batch_lookup_table point_table(points);
const size_t num_rounds = (max_num_bits == 0) ? Fr::modulus.get_msb() + 1 : max_num_bits;

std::vector<std::vector<bool_t<C>>> naf_entries;
for (size_t i = 0; i < num_points; ++i) {
naf_entries.emplace_back(compute_naf(scalars[i], max_num_bits));
}
const auto offset_generators = compute_offset_generators(num_rounds);
element accumulator =
element::chain_add_end(element::chain_add(offset_generators.first, point_table.get_chain_initial_entry()));

constexpr size_t num_rounds_per_iteration = 4;
size_t num_iterations = num_rounds / num_rounds_per_iteration;
num_iterations += ((num_iterations * num_rounds_per_iteration) == num_rounds) ? 0 : 1;
const size_t num_rounds_per_final_iteration = (num_rounds - 1) - ((num_iterations - 1) * num_rounds_per_iteration);
for (size_t i = 0; i < num_iterations; ++i) {

std::vector<bool_t<C>> nafs(num_points);
std::vector<element::chain_add_accumulator> to_add;
const size_t inner_num_rounds =
(i != num_iterations - 1) ? num_rounds_per_iteration : num_rounds_per_final_iteration;
for (size_t j = 0; j < inner_num_rounds; ++j) {
for (size_t k = 0; k < num_points; ++k) {
nafs[k] = (naf_entries[k][i * num_rounds_per_iteration + j + 1]);
}
to_add.emplace_back(point_table.get_chain_add_accumulator(nafs));
}
const size_t num_points = points.size();
ASSERT(scalars.size() == num_points);
batch_lookup_table point_table(points);
const size_t num_rounds = (max_num_bits == 0) ? Fr::modulus.get_msb() + 1 : max_num_bits;

std::vector<std::vector<bool_t<C>>> naf_entries;
for (size_t i = 0; i < num_points; ++i) {
naf_entries.emplace_back(compute_naf(scalars[i], max_num_bits));
}
const auto offset_generators = compute_offset_generators(num_rounds);
element accumulator =
element::chain_add_end(element::chain_add(offset_generators.first, point_table.get_chain_initial_entry()));

constexpr size_t num_rounds_per_iteration = 4;
size_t num_iterations = num_rounds / num_rounds_per_iteration;
num_iterations += ((num_iterations * num_rounds_per_iteration) == num_rounds) ? 0 : 1;
const size_t num_rounds_per_final_iteration = (num_rounds - 1) - ((num_iterations - 1) * num_rounds_per_iteration);
for (size_t i = 0; i < num_iterations; ++i) {

std::vector<bool_t<C>> nafs(num_points);
std::vector<element::chain_add_accumulator> to_add;
const size_t inner_num_rounds =
(i != num_iterations - 1) ? num_rounds_per_iteration : num_rounds_per_final_iteration;
for (size_t j = 0; j < inner_num_rounds; ++j) {
for (size_t k = 0; k < num_points; ++k) {
nafs[k] = (naf_entries[k][i * num_rounds_per_iteration + j + 1]);
}
accumulator = accumulator.multiple_montgomery_ladder(to_add);
to_add.emplace_back(point_table.get_chain_add_accumulator(nafs));
}
for (size_t i = 0; i < num_points; ++i) {
element skew = accumulator - points[i];
Fq out_x = accumulator.x.conditional_select(skew.x, naf_entries[i][num_rounds]);
Fq out_y = accumulator.y.conditional_select(skew.y, naf_entries[i][num_rounds]);
accumulator = element(out_x, out_y);
}
accumulator = accumulator - offset_generators.second;

return accumulator;
accumulator = accumulator.multiple_montgomery_ladder(to_add);
}
for (size_t i = 0; i < num_points; ++i) {
element skew = accumulator - points[i];
Fq out_x = accumulator.x.conditional_select(skew.x, naf_entries[i][num_rounds]);
Fq out_y = accumulator.y.conditional_select(skew.y, naf_entries[i][num_rounds]);
accumulator = element(out_x, out_y);
}
accumulator = accumulator - offset_generators.second;

return accumulator;
}

/**
Expand Down

0 comments on commit 6d654db

Please sign in to comment.