|
Barretenberg
The ZK-SNARK library at the core of Aztec
|
#include <scalar_multiplication.hpp>
Classes | |
| struct | AffineAdditionData |
| Scratch space for batched affine point additions (one per thread) More... | |
| struct | BucketAccumulators |
| Affine bucket accumulators for the fast affine-trick Pippenger variant. More... | |
| struct | JacobianBucketAccumulators |
| Jacobian bucket accumulators for the safe Pippenger variant. More... | |
| struct | MSMData |
| Container for MSM input data passed between algorithm stages. More... | |
| struct | MSMWorkUnit |
| MSMWorkUnit describes an MSM that may be part of a larger MSM. More... | |
| struct | PointScheduleEntry |
| Packed point schedule entry: (point_index << 32) | bucket_index. More... | |
Public Types | |
| using | Element = typename Curve::Element |
| using | ScalarField = typename Curve::ScalarField |
| using | BaseField = typename Curve::BaseField |
| using | AffineElement = typename Curve::AffineElement |
| using | ThreadWorkUnits = std::vector< MSMWorkUnit > |
Static Public Member Functions | |
| static const AffineElement & | get_offset_generator () noexcept |
| static AffineElement | msm (std::span< const AffineElement > points, PolynomialSpan< const ScalarField > scalars, bool handle_edge_cases=false) noexcept |
| Main entry point for single MSM computation. | |
| static std::vector< AffineElement > | batch_multi_scalar_mul (std::span< std::span< const AffineElement > > points, std::span< std::span< ScalarField > > scalars, bool handle_edge_cases=true) noexcept |
| Compute multiple MSMs in parallel with work balancing. | |
| static uint32_t | get_num_rounds (size_t num_points) noexcept |
| static void | add_affine_points (AffineElement *points, const size_t num_points, typename Curve::BaseField *scratch_space) noexcept |
| Batch add n/2 independent point pairs using Montgomery's trick. | |
| static uint32_t | get_scalar_slice (const ScalarField &scalar, size_t round, size_t slice_size) noexcept |
| Extract c-bit slice from scalar for bucket index computation. | |
| static uint32_t | get_optimal_log_num_buckets (size_t num_points) noexcept |
| Compute optimal bits per slice by minimizing cost over c in [1, MAX_SLICE_BITS) | |
| static std::vector< ThreadWorkUnits > | partition_by_weight (std::span< const std::vector< uint16_t > > msm_scalar_weights, size_t num_threads) noexcept |
| Partition per-MSM scalar weights into num_threads work units of approximately equal cumulative weight. | |
| static void | batch_accumulate_points_into_buckets (std::span< const uint64_t > point_schedule, std::span< const AffineElement > points, AffineAdditionData &affine_data, BucketAccumulators &bucket_data) noexcept |
| Process sorted point schedule into bucket accumulators using batched affine additions. | |
| template<typename BucketType > | |
| static Element | accumulate_buckets (BucketType &bucket_accumulators) noexcept |
| Reduce buckets to single point using running (suffix) sum from high to low: R = sum(k * B_k) | |
Static Public Attributes | |
| static constexpr size_t | NUM_BITS_IN_FIELD = ScalarField::modulus.get_msb() + 1 |
| static constexpr size_t | PIPPENGER_THRESHOLD = 16 |
| static constexpr size_t | AFFINE_TRICK_THRESHOLD = 128 |
| static constexpr size_t | MAX_SLICE_BITS = 20 |
| static constexpr size_t | PREFETCH_LOOKAHEAD = 32 |
| static constexpr size_t | PREFETCH_INTERVAL = 16 |
| static constexpr size_t | PREFETCH_INTERVAL_MASK = PREFETCH_INTERVAL - 1 |
| static constexpr size_t | BUCKET_ACCUMULATION_COST = 5 |
| static constexpr size_t | AFFINE_TRICK_SAVINGS_PER_OP = 5 |
| static constexpr size_t | JACOBIAN_Z_NOT_ONE_PENALTY = 5 |
| static constexpr size_t | INVERSION_TABLE_COST = 14 |
Private Member Functions | |
| __attribute__ ((always_inline)) static void process_single_point(size_t bucket | |
| if (has_accumulator) | |
| bucket_data bucket_exists | set (bucket, true) |
| __attribute__ ((always_inline)) static void process_bucket_pair(size_t lhs_bucket | |
| bucket_data bucket_exists | set (lhs_bucket,(has_bucket_accumulator &&buckets_match)||!do_affine_add) |
Static Private Member Functions | |
| static void | transform_scalar_and_get_nonzero_scalar_indices (std::span< ScalarField > scalars, std::vector< uint32_t > &nonzero_scalar_indices) noexcept |
| Convert scalars from Montgomery form and collect indices of nonzero scalars. | |
| static void | compute_scalar_slice_weights (std::span< const ScalarField > scalars, std::span< const uint32_t > nonzero_indices, uint32_t bits_per_slice, std::vector< uint16_t > &weights) noexcept |
| Compute per-scalar slice-count weights ceil(bit_length / bits_per_slice). | |
| static std::vector< ThreadWorkUnits > | get_work_units (std::span< std::span< ScalarField > > scalars, std::vector< std::vector< uint32_t > > &msm_scalar_indices) noexcept |
| Distribute multiple MSMs across threads with balanced bucket-accumulation work. | |
| static bool | use_affine_trick (size_t num_points, size_t num_buckets) noexcept |
| Decide if batch inversion saves work vs Jacobian additions. | |
| static Element | jacobian_pippenger_with_transformed_scalars (MSMData &msm_data) noexcept |
| Pippenger using Jacobian buckets (handles edge cases: doubling, infinity) | |
| static Element | affine_pippenger_with_transformed_scalars (MSMData &msm_data) noexcept |
| Pippenger using affine buckets with batch inversion (faster, no edge case handling) | |
Definition at line 19 of file scalar_multiplication.hpp.
| using bb::scalar_multiplication::MSM< Curve >::AffineElement = typename Curve::AffineElement |
Definition at line 24 of file scalar_multiplication.hpp.
| using bb::scalar_multiplication::MSM< Curve >::BaseField = typename Curve::BaseField |
Definition at line 23 of file scalar_multiplication.hpp.
| using bb::scalar_multiplication::MSM< Curve >::Element = typename Curve::Element |
Definition at line 21 of file scalar_multiplication.hpp.
| using bb::scalar_multiplication::MSM< Curve >::ScalarField = typename Curve::ScalarField |
Definition at line 22 of file scalar_multiplication.hpp.
| using bb::scalar_multiplication::MSM< Curve >::ThreadWorkUnits = std::vector<MSMWorkUnit> |
Definition at line 99 of file scalar_multiplication.hpp.
|
private |
|
private |
|
inlinestaticnoexcept |
Reduce buckets to single point using running (suffix) sum from high to low: R = sum(k * B_k)
Definition at line 258 of file scalar_multiplication.hpp.
|
staticnoexcept |
Batch add n/2 independent point pairs using Montgomery's trick.
Definition at line 298 of file scalar_multiplication.cpp.
|
staticprivatenoexcept |
Pippenger using affine buckets with batch inversion (faster, no edge case handling)
Definition at line 352 of file scalar_multiplication.cpp.
|
staticnoexcept |
Process sorted point schedule into bucket accumulators using batched affine additions.
Definition at line 407 of file scalar_multiplication.cpp.
|
staticnoexcept |
Compute multiple MSMs in parallel with work balancing.
Definition at line 497 of file scalar_multiplication.cpp.
|
staticprivatenoexcept |
Compute per-scalar slice-count weights ceil(bit_length / bits_per_slice).
Parallel over nonzero_indices. Scalars must be in non-Montgomery form (as left by transform_scalar_and_get_nonzero_scalar_indices). Weights drive thread partitioning in get_work_units.
Definition at line 89 of file scalar_multiplication.cpp.
|
inlinestaticnoexcept |
Definition at line 226 of file scalar_multiplication.hpp.
|
inlinestaticnoexcept |
Definition at line 74 of file scalar_multiplication.hpp.
|
staticnoexcept |
Compute optimal bits per slice by minimizing cost over c in [1, MAX_SLICE_BITS)
Definition at line 253 of file scalar_multiplication.cpp.
|
staticnoexcept |
Extract c-bit slice from scalar for bucket index computation.
Extract a slice of bits from a scalar for Pippenger bucket assignment.
Extracts bits [lo_bit, hi_bit) from the scalar's raw limb representation. The scalar must already be converted out of Montgomery form.
IMPORTANT RESTRICTIONS (optimized for Pippenger's specific usage pattern):
| scalar | The scalar field element (must be in non-Montgomery form) |
| round | The current Pippenger round (0 = most significant bits) |
| slice_size | Number of bits per slice |
Definition at line 227 of file scalar_multiplication.cpp.
|
staticprivatenoexcept |
Distribute multiple MSMs across threads with balanced bucket-accumulation work.
Per-thread assignment is a contiguous range of each MSM's nonzero-scalar indices, sized by cumulative slice-count weight ceil(bit_length / c). This is the actual number of nonzero c-bit slices a scalar contributes — the quantity that drives bucket-accumulation cost.
Definition at line 172 of file scalar_multiplication.cpp.
|
inlineprivate |
Definition at line 336 of file scalar_multiplication.hpp.
|
staticprivatenoexcept |
Pippenger using Jacobian buckets (handles edge cases: doubling, infinity)
Definition at line 312 of file scalar_multiplication.cpp.
|
staticnoexcept |
Main entry point for single MSM computation.
| handle_edge_cases | false (default): fast affine variant; true: safe Jacobian variant |
Definition at line 576 of file scalar_multiplication.cpp.
|
staticnoexcept |
Partition per-MSM scalar weights into num_threads work units of approximately equal cumulative weight.
Curve-independent and side-effect-free. The walk closes a work unit every time the running weight crosses the per-thread target, except on the last thread which absorbs any remainder so rounding drift doesn't leave work stranded.
Definition at line 121 of file scalar_multiplication.cpp.
|
private |
|
private |
|
staticprivatenoexcept |
Convert scalars from Montgomery form and collect indices of nonzero scalars.
Definition at line 42 of file scalar_multiplication.cpp.
|
staticprivatenoexcept |
Decide if batch inversion saves work vs Jacobian additions.
Definition at line 274 of file scalar_multiplication.cpp.
|
private |
Definition at line 330 of file scalar_multiplication.hpp.
|
private |
Definition at line 355 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 63 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 38 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 60 of file scalar_multiplication.hpp.
|
private |
Definition at line 331 of file scalar_multiplication.hpp.
|
private |
Definition at line 356 of file scalar_multiplication.hpp.
|
private |
Definition at line 361 of file scalar_multiplication.hpp.
|
private |
Definition at line 371 of file scalar_multiplication.hpp.
|
private |
Definition at line 372 of file scalar_multiplication.hpp.
|
private |
Definition at line 362 of file scalar_multiplication.hpp.
|
private |
Definition at line 342 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 69 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 66 of file scalar_multiplication.hpp.
|
private |
Definition at line 366 of file scalar_multiplication.hpp.
|
private |
Definition at line 374 of file scalar_multiplication.hpp.
|
private |
Definition at line 353 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 41 of file scalar_multiplication.hpp.
|
private |
Definition at line 333 of file scalar_multiplication.hpp.
|
private |
Definition at line 358 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 26 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 34 of file scalar_multiplication.hpp.
|
private |
Definition at line 346 of file scalar_multiplication.hpp.
|
private |
Definition at line 329 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 50 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 51 of file scalar_multiplication.hpp.
|
staticconstexpr |
Definition at line 47 of file scalar_multiplication.hpp.
|
private |
Definition at line 352 of file scalar_multiplication.hpp.
|
private |
Definition at line 368 of file scalar_multiplication.hpp.
|
private |
Definition at line 375 of file scalar_multiplication.hpp.
|
private |
Definition at line 364 of file scalar_multiplication.hpp.
|
private |
Definition at line 354 of file scalar_multiplication.hpp.
|
private |
Definition at line 332 of file scalar_multiplication.hpp.
|
private |
Definition at line 357 of file scalar_multiplication.hpp.
|
private |
Definition at line 378 of file scalar_multiplication.hpp.