|
| 1 | +//---------------------------------------------------------------------------// |
| 2 | +// Copyright (c) 2021 Mikhail Komarov <[email protected]> |
| 3 | +// Copyright (c) 2021 Nikita Kaskov <[email protected]> |
| 4 | +// Copyright (c) 2022 Alisa Cherniaeva <[email protected]> |
| 5 | +// Copyright (c) 2022 Ekaterina Chukavina <[email protected]> |
| 6 | +// |
| 7 | +// MIT License |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in all |
| 17 | +// copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +// SOFTWARE. |
| 26 | +//---------------------------------------------------------------------------// |
| 27 | +// @file Declaration of interfaces for auxiliary components for the SHA512_PROCESS component. |
| 28 | +//---------------------------------------------------------------------------// |
| 29 | + |
| 30 | +#ifndef CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_SHA2_SPLIT_FUNCTIONS_HPP |
| 31 | +#define CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_SHA2_SPLIT_FUNCTIONS_HPP |
| 32 | + |
| 33 | +namespace nil { |
| 34 | + namespace blueprint { |
| 35 | + namespace components { |
| 36 | + namespace detail { |
| 37 | + |
| 38 | + template <typename BlueprintFieldType> |
| 39 | + std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> split_and_sparse( |
| 40 | + std::vector<bool> bits, const std::vector<size_t> &sizes, std::size_t base) { |
| 41 | + |
| 42 | + std::size_t size = sizes.size() - 1; |
| 43 | + std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> res = {std::vector<typename BlueprintFieldType::integral_type>(size + 1), |
| 44 | + std::vector<typename BlueprintFieldType::integral_type>(size + 1)}; |
| 45 | + std::size_t k = 0; |
| 46 | + for (int i = size; i > -1; i--) { |
| 47 | + res[0][i] = int(bits[k]); |
| 48 | + res[1][i] = int(bits[k]); |
| 49 | + for (std::size_t j = 1; j < sizes[i]; j++) { |
| 50 | + res[0][i] = res[0][i] * 2 + int(bits[k + j]); |
| 51 | + res[1][i] = res[1][i] * base + int(bits[k + j]); |
| 52 | + } |
| 53 | + k = k + sizes[i]; |
| 54 | + } |
| 55 | + return res; |
| 56 | + } |
| 57 | + |
| 58 | + template <typename BlueprintFieldType> |
| 59 | + std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> |
| 60 | + reversed_sparse_and_split(typename BlueprintFieldType::integral_type sparse_value, |
| 61 | + const std::vector<size_t> &sizes, std::size_t base) { |
| 62 | + std::size_t size = sizes.size(); |
| 63 | + std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> res = { |
| 64 | + std::vector<typename BlueprintFieldType::integral_type>(size), |
| 65 | + std::vector<typename BlueprintFieldType::integral_type>(size)}; |
| 66 | + typename BlueprintFieldType::integral_type sparse_base = base; |
| 67 | + typename BlueprintFieldType::value_type value_base = base; |
| 68 | + std::size_t k = -1; |
| 69 | + for (int i = sizes.size() - 1; i > -1; i--) { |
| 70 | + k = k + sizes[i]; |
| 71 | + } |
| 72 | + typename BlueprintFieldType::integral_type tmp = sparse_value; |
| 73 | + for (int i = sizes.size() - 1; i > -1; i--) { |
| 74 | + res[0][i] = 0; |
| 75 | + res[1][i] = 0; |
| 76 | + for (int j = sizes[i] - 1; j > -1; j--) { |
| 77 | + if (tmp > typename BlueprintFieldType::integral_type(value_base.pow(k).data) - 1) { |
| 78 | + typename BlueprintFieldType::integral_type r = (tmp - (tmp % typename BlueprintFieldType::integral_type(value_base.pow(k).data))) / |
| 79 | + typename BlueprintFieldType::integral_type(value_base.pow(k).data); |
| 80 | + res[0][i] = res[0][i] * 2 + (r&1); |
| 81 | + res[1][i] = res[1][i] * sparse_base + r; |
| 82 | + } |
| 83 | + else { |
| 84 | + res[0][i] = res[0][i] * 2; |
| 85 | + res[1][i] = res[1][i] * sparse_base; |
| 86 | + } |
| 87 | + tmp = tmp % typename BlueprintFieldType::integral_type(value_base.pow(k).data); |
| 88 | + k--; |
| 89 | + } |
| 90 | + } |
| 91 | + return res; |
| 92 | + } |
| 93 | + |
| 94 | + template <typename BlueprintFieldType> |
| 95 | + std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> |
| 96 | + reversed_sparse_and_split_maj(typename BlueprintFieldType::integral_type sparse_value, |
| 97 | + const std::vector<size_t> &sizes, std::size_t base) { |
| 98 | + std::size_t size = sizes.size(); |
| 99 | + std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> res = { |
| 100 | + std::vector<typename BlueprintFieldType::integral_type>(size), |
| 101 | + std::vector<typename BlueprintFieldType::integral_type>(size)}; |
| 102 | + typename BlueprintFieldType::integral_type sparse_base = base; |
| 103 | + typename BlueprintFieldType::value_type value_base = base; |
| 104 | + std::size_t k = -1; |
| 105 | + for (int i = sizes.size() - 1; i > -1; i--) { |
| 106 | + k = k + sizes[i]; |
| 107 | + } |
| 108 | + std::array<std::size_t, 4> r_values = {0,0,1,1}; |
| 109 | + typename BlueprintFieldType::integral_type tmp = sparse_value; |
| 110 | + for (int i = sizes.size() - 1; i > -1; i--) { |
| 111 | + res[0][i] = 0; |
| 112 | + res[1][i] = 0; |
| 113 | + for (int j = sizes[i] - 1; j > -1; j--) { |
| 114 | + if (tmp > typename BlueprintFieldType::integral_type(value_base.pow(k).data) - 1) { |
| 115 | + typename BlueprintFieldType::integral_type r = (tmp - (tmp % typename BlueprintFieldType::integral_type(value_base.pow(k).data))) / |
| 116 | + typename BlueprintFieldType::integral_type(value_base.pow(k).data); |
| 117 | + res[0][i] = res[0][i] * 2 + r_values[std::size_t(r)]; |
| 118 | + res[1][i] = res[1][i] * sparse_base + r; |
| 119 | + } |
| 120 | + else { |
| 121 | + res[0][i] = res[0][i] * 2; |
| 122 | + res[1][i] = res[1][i] * sparse_base; |
| 123 | + } |
| 124 | + tmp = tmp % typename BlueprintFieldType::integral_type(value_base.pow(k).data); |
| 125 | + k--; |
| 126 | + } |
| 127 | + } |
| 128 | + return res; |
| 129 | + } |
| 130 | + |
| 131 | + template <typename BlueprintFieldType> |
| 132 | + std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> |
| 133 | + reversed_sparse_and_split_ch(typename BlueprintFieldType::integral_type sparse_value, |
| 134 | + const std::vector<size_t> &sizes, std::size_t base) { |
| 135 | + std::size_t size = sizes.size(); |
| 136 | + std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> res = { |
| 137 | + std::vector<typename BlueprintFieldType::integral_type>(size), |
| 138 | + std::vector<typename BlueprintFieldType::integral_type>(size)}; |
| 139 | + typename BlueprintFieldType::integral_type sparse_base = base; |
| 140 | + typename BlueprintFieldType::value_type value_base = base; |
| 141 | + std::size_t k = -1; |
| 142 | + for (int i = sizes.size() - 1; i > -1; i--) { |
| 143 | + k = k + sizes[i]; |
| 144 | + } |
| 145 | + std::array<std::size_t, 6> r_values = {0,0,1,0,1,1}; |
| 146 | + typename BlueprintFieldType::integral_type tmp = sparse_value; |
| 147 | + for (int i = sizes.size() - 1; i > -1; i--) { |
| 148 | + res[0][i] = 0; |
| 149 | + res[1][i] = 0; |
| 150 | + for (int j = sizes[i] - 1; j > -1; j--) { |
| 151 | + if (tmp > typename BlueprintFieldType::integral_type(value_base.pow(k).data) - 1) { |
| 152 | + typename BlueprintFieldType::integral_type r = (tmp - (tmp % typename BlueprintFieldType::integral_type(value_base.pow(k).data))) / |
| 153 | + typename BlueprintFieldType::integral_type(value_base.pow(k).data); |
| 154 | + res[0][i] = res[0][i] * 2 + r_values[std::size_t(r) - 1]; |
| 155 | + res[1][i] = res[1][i] * sparse_base + r; |
| 156 | + } |
| 157 | + else { |
| 158 | + res[0][i] = res[0][i] * 2; |
| 159 | + res[1][i] = res[1][i] * sparse_base; |
| 160 | + } |
| 161 | + tmp = tmp % typename BlueprintFieldType::integral_type(value_base.pow(k).data); |
| 162 | + k--; |
| 163 | + } |
| 164 | + } |
| 165 | + return res; |
| 166 | + } |
| 167 | + } // namespace detail |
| 168 | + } // namespace components |
| 169 | + } // namespace blueprint |
| 170 | +} // namespace nil |
| 171 | + |
| 172 | +#endif // CRYPTO3_BLUEPRINT_COMPONENTS_PLONK_SHA2_SPLIT_FUNCTIONS_HPP |
0 commit comments