Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
to_radix.cpp
Go to the documentation of this file.
2
6
7namespace bb::avm2 {
8
9namespace {
10
11// The number of limbs that the field modulus, p, decomposes into given a radix.
12const std::array<size_t, 257> p_limbs_per_radix_sizes = {
13 0, 0, 254, 161, 127, 110, 99, 91, 85, 81, 77, 74, 71, 69, 67, 65, 64, 63, 61, 60, 59, 58, 57, 57, 56, 55,
14 54, 54, 53, 53, 52, 52, 51, 51, 50, 50, 50, 49, 49, 48, 48, 48, 48, 47, 47, 47, 46, 46, 46, 46, 45, 45,
15 45, 45, 45, 44, 44, 44, 44, 44, 43, 43, 43, 43, 43, 43, 42, 42, 42, 42, 42, 42, 42, 41, 41, 41, 41, 41,
16 41, 41, 41, 41, 40, 40, 40, 40, 40, 40, 40, 40, 40, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 38,
17 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
18 37, 37, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 35, 35, 35, 35,
19 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 34, 34, 34, 34, 34, 34,
20 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 33, 33,
21 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
22 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
23};
24
25// The little endian decompositions of Fr modulus into limbs for each radix.
26// Radix goes up to 256 so we need 257 decompositions.
27std::array<std::vector<uint8_t>, 257> create_p_limbs_per_radix()
28{
29 std::array<std::vector<uint8_t>, 257> limbs_per_radix;
30
31 for (size_t radix = 2; radix < 257; ++radix) {
32 std::vector<uint8_t> p_limbs;
33 p_limbs.reserve(p_limbs_per_radix_sizes[radix]);
35
36 while (p != 0) {
37 const auto [quotient, remainder] = p.divmod(static_cast<uint64_t>(radix));
38 p_limbs.push_back(static_cast<uint8_t>(remainder));
39 p = quotient;
40 }
41
42 limbs_per_radix[radix] = p_limbs;
43 }
44
45 return limbs_per_radix;
46}
47
48} // namespace
49
57{
58 static const std::array<std::vector<uint8_t>, 257> limbs_per_radix = create_p_limbs_per_radix();
59 return limbs_per_radix;
60}
61
69size_t get_p_limbs_per_radix_size(size_t radix)
70{
71 BB_ASSERT_LTE(radix, static_cast<decltype(radix)>(256), "Radix out of bounds");
72 return p_limbs_per_radix_sizes[radix];
73}
74
75} // namespace bb::avm2
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:158
constexpr std::pair< uint256_t, uint256_t > divmod(const uint256_t &b) const
const std::array< std::vector< uint8_t >, 257 > & get_p_limbs_per_radix()
Gets the p limbs per radix array. Each element is a vector containing the little endian decomposition...
Definition to_radix.cpp:56
size_t get_p_limbs_per_radix_size(size_t radix)
Gets the number of limbs that the modulus, p, decomposes into for a given radix.
Definition to_radix.cpp:69
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr uint256_t modulus