Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 134c770

Browse files
authored
Merge pull request #142 from NilFoundation/141-update-sha2
SHA2 components updated
2 parents 5407a4f + 031455a commit 134c770

File tree

20 files changed

+220
-284
lines changed

20 files changed

+220
-284
lines changed

include/nil/blueprint/basic_non_native_policy.hpp

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ namespace nil {
3737
template<typename BlueprintFieldType, typename OperatingFieldType>
3838
struct basic_non_native_policy_field_type;
3939

40+
/*
41+
* Specialization for non-native Ed25519 base field element on Pallas base field
42+
*/
4043
template<>
4144
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
4245
typename crypto3::algebra::curves::ed25519::base_field_type> {
@@ -49,6 +52,19 @@ namespace nil {
4952
value_type;
5053
};
5154

55+
/*
56+
* Specialization for non-native Ed25519 scalar field element on Pallas base field
57+
*/
58+
template<>
59+
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
60+
typename crypto3::algebra::curves::ed25519::scalar_field_type> {
61+
62+
constexpr static const std::uint32_t ratio = 1;
63+
64+
typedef crypto3::zk::snark::plonk_variable<typename crypto3::algebra::curves::pallas::base_field_type>
65+
value_type;
66+
};
67+
5268
/*
5369
* Native element type.
5470
*/

include/nil/blueprint/components/algebra/fields/plonk/non_native/reduction.hpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,28 @@
3737
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
3838
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
3939
#include <nil/blueprint/component.hpp>
40+
#include <nil/blueprint/basic_non_native_policy.hpp>
41+
4042
namespace nil {
4143
namespace blueprint {
4244
namespace components {
4345

44-
template<typename ArithmetizationType, typename FieldType, std::uint32_t WitnessesAmount>
46+
template<typename ArithmetizationType, typename FieldType, std::uint32_t WitnessesAmount,
47+
typename NonNativePolicyType>
4548
class reduction;
4649

4750
template<typename BlueprintFieldType, typename ArithmetizationParams>
4851
class reduction<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
49-
BlueprintFieldType, 9>
52+
BlueprintFieldType, 9,
53+
basic_non_native_policy<BlueprintFieldType>>
5054
: public plonk_component<BlueprintFieldType, ArithmetizationParams, 9, 0, 0> {
5155

5256
constexpr static const std::uint32_t WitnessesAmount = 9;
5357

5458
using component_type =
5559
plonk_component<BlueprintFieldType, ArithmetizationParams, WitnessesAmount, 0, 0>;
60+
using operating_field_type = crypto3::algebra::fields::curve25519_scalar_field;
61+
using non_native_policy_type = basic_non_native_policy<BlueprintFieldType>;
5662

5763
public:
5864
using var = typename component_type::var;
@@ -66,7 +72,7 @@ namespace nil {
6672
};
6773

6874
struct result_type {
69-
var output;
75+
typename non_native_policy_type::template field<operating_field_type>::value_type output;
7076

7177
result_type(const reduction &component, std::uint32_t start_row_index) {
7278
output = var(component.W(4), start_row_index + rows_amount - 3, false);
@@ -93,7 +99,8 @@ namespace nil {
9399
template<typename BlueprintFieldType, typename ArithmetizationParams, std::int32_t WitnessesAmount>
94100
using plonk_reduction =
95101
reduction<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>,
96-
BlueprintFieldType, WitnessesAmount>;
102+
BlueprintFieldType, WitnessesAmount,
103+
basic_non_native_policy<BlueprintFieldType>>;
97104

98105
template<typename BlueprintFieldType, typename ArithmetizationParams>
99106
typename plonk_reduction<BlueprintFieldType, ArithmetizationParams, 9>::result_type generate_assignments(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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

include/nil/blueprint/components/hashes/sha256/plonk/sha256.hpp include/nil/blueprint/components/hashes/sha2/plonk/sha256.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
3333
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
3434
#include <nil/blueprint/component.hpp>
35-
#include <nil/blueprint/components/hashes/sha256/plonk/sha256_process.hpp>
36-
#include <nil/blueprint/components/hashes/sha256/plonk/decomposition.hpp>
35+
#include <nil/blueprint/components/hashes/sha2/plonk/sha256_process.hpp>
36+
#include <nil/blueprint/components/hashes/sha2/plonk/decomposition.hpp>
3737

3838
namespace nil {
3939
namespace blueprint {

0 commit comments

Comments
 (0)