barretenberg
Loading...
Searching...
No Matches
runtime_states.hpp
1#pragma once
2
3#include "barretenberg/ecc/curves/bn254/bn254.hpp"
4#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
5#include "barretenberg/ecc/groups/wnaf.hpp"
6
7namespace barretenberg::scalar_multiplication {
8// simple helper functions to retrieve pointers to pre-allocated memory for the scalar multiplication algorithm.
9// This is to eliminate page faults when allocating (and writing) to large tranches of memory.
10constexpr size_t get_optimal_bucket_width(const size_t num_points)
11{
12 if (num_points >= 14617149) {
13 return 21;
14 }
15 if (num_points >= 1139094) {
16 return 18;
17 }
18 // if (num_points >= 100000)
19 if (num_points >= 155975) {
20 return 15;
21 }
22 if (num_points >= 144834)
23 // if (num_points >= 100000)
24 {
25 return 14;
26 }
27 if (num_points >= 25067) {
28 return 12;
29 }
30 if (num_points >= 13926) {
31 return 11;
32 }
33 if (num_points >= 7659) {
34 return 10;
35 }
36 if (num_points >= 2436) {
37 return 9;
38 }
39 if (num_points >= 376) {
40 return 7;
41 }
42 if (num_points >= 231) {
43 return 6;
44 }
45 if (num_points >= 97) {
46 return 5;
47 }
48 if (num_points >= 35) {
49 return 4;
50 }
51 if (num_points >= 10) {
52 return 3;
53 }
54 if (num_points >= 2) {
55 return 2;
56 }
57 return 1;
58}
59
60constexpr size_t get_num_rounds(const size_t num_points)
61{
62 const size_t bits_per_bucket = get_optimal_bucket_width(num_points / 2);
63 return WNAF_SIZE(bits_per_bucket + 1);
64}
65
66template <typename Curve> struct affine_product_runtime_state {
67 typename Curve::AffineElement* points;
68 typename Curve::AffineElement* point_pairs_1;
69 typename Curve::AffineElement* point_pairs_2;
70 typename Curve::BaseField* scratch_space;
71 uint32_t* bucket_counts;
72 uint32_t* bit_offsets;
73 uint64_t* point_schedule;
74 uint32_t num_points;
75 uint32_t num_buckets;
76 bool* bucket_empty_status;
77};
78
79template <typename Curve> struct pippenger_runtime_state {
80 using Fq = typename Curve::BaseField;
81 using AffineElement = typename Curve::AffineElement;
82
83 static constexpr size_t MAX_NUM_ROUNDS = 256;
84 uint64_t num_points;
85 size_t num_buckets;
86 size_t num_rounds;
87 size_t num_threads;
88 size_t prefetch_overflow;
89 std::shared_ptr<void> point_schedule_ptr;
90 std::shared_ptr<void> point_pairs_1_ptr;
91 std::shared_ptr<void> point_pairs_2_ptr;
92 std::shared_ptr<void> scratch_space_ptr;
93 uint64_t* point_schedule;
94 typename Curve::AffineElement* point_pairs_1;
95 typename Curve::AffineElement* point_pairs_2;
96 typename Curve::BaseField* scratch_space;
97
98 bool* skew_table;
99 uint32_t* bucket_counts;
100 uint32_t* bit_counts;
101 bool* bucket_empty_status;
102 uint64_t* round_counts;
103
104 pippenger_runtime_state(size_t num_initial_points) noexcept;
106 pippenger_runtime_state& operator=(pippenger_runtime_state&& other) noexcept;
107 ~pippenger_runtime_state() noexcept;
108
109 // explicitly delete copy constructor and copy assignment operator.
110 // This is an expensive, large data structure. No copy! Bad!
111 pippenger_runtime_state& operator=(pippenger_runtime_state& other) = delete;
113
114 affine_product_runtime_state<Curve> get_affine_product_runtime_state(size_t num_threads, size_t thread_index);
115};
116
117extern template struct affine_product_runtime_state<curve::BN254>;
119extern template struct pippenger_runtime_state<curve::BN254>;
120extern template struct pippenger_runtime_state<curve::Grumpkin>;
121} // namespace barretenberg::scalar_multiplication