-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added barrett_reduction implementation into uintx (#6768)
This PR adds a `barrett_reduction` method into `unitx`, a fast division algorithm when the divisor is known ahead of time such that precomputed factors can be determined. `barrett_reduction` is used to speed up `divmod` for some important hardcoded moduli. Or particular relevance is the prime field associated with BN254 curve arithmetic, as expensive 1024-bit `divmod` operations are performed when computing witnesses within `stdlib::bitfield` - commonly used to perform non-native BN254 curve arithmetic. Speeds up biggroup batch_mul 4x --------- Co-authored-by: Rumata888 <isennovskiy@gmail.com>
- Loading branch information
1 parent
4ddea82
commit abced57
Showing
6 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
barretenberg/cpp/src/barretenberg/benchmark/circuit_construction_bench/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
barretenberg_module(circuit_construction_bench stdlib_primitives) |
46 changes: 46 additions & 0 deletions
46
.../cpp/src/barretenberg/benchmark/circuit_construction_bench/circuit_construction.bench.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
#include <benchmark/benchmark.h> | ||
|
||
#include "barretenberg/stdlib/primitives/biggroup/biggroup.hpp" | ||
#include "barretenberg/stdlib/primitives/curves/bn254.hpp" | ||
#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp" | ||
|
||
using namespace benchmark; | ||
using namespace bb; | ||
|
||
namespace { | ||
|
||
auto& engine = numeric::get_debug_randomness(); | ||
void biggroup_construction_bench(State& state) | ||
{ | ||
using Curve = stdlib::bn254<UltraCircuitBuilder>; | ||
using affine_element = Curve::AffineElementNative; | ||
using element_ct = Curve::Element; | ||
using scalar_ct = Curve::ScalarField; | ||
for (auto _ : state) { | ||
state.PauseTiming(); | ||
|
||
UltraCircuitBuilder builder; | ||
size_t num_points = static_cast<size_t>(state.range(0)); | ||
std::vector<affine_element> points; | ||
std::vector<fr> scalars; | ||
for (size_t i = 0; i < num_points; ++i) { | ||
points.push_back(affine_element(Curve::ElementNative::random_element())); | ||
scalars.push_back(fr::random_element()); | ||
} | ||
|
||
std::vector<element_ct> circuit_points; | ||
std::vector<scalar_ct> circuit_scalars; | ||
for (size_t i = 0; i < num_points; ++i) { | ||
circuit_points.push_back(element_ct::from_witness(&builder, points[i])); | ||
circuit_scalars.push_back(scalar_ct::from_witness(&builder, scalars[i])); | ||
} | ||
state.ResumeTiming(); | ||
element_ct::batch_mul(circuit_points, circuit_scalars); | ||
state.PauseTiming(); | ||
} | ||
} | ||
} // namespace | ||
BENCHMARK(biggroup_construction_bench)->Unit(kMicrosecond)->DenseRange(2, 20); | ||
|
||
BENCHMARK_MAIN(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters