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

Commit 04043e1

Browse files
x-massnkaskov
authored andcommitted
Use algebra split_field_element() for chopping
1 parent 41cb11e commit 04043e1

File tree

1 file changed

+25
-59
lines changed

1 file changed

+25
-59
lines changed

include/nil/blueprint/basic_non_native_policy.hpp

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <nil/crypto3/algebra/curves/pallas.hpp>
3030
#include <nil/crypto3/algebra/curves/ed25519.hpp>
31+
#include <nil/crypto3/algebra/marshalling.hpp>
3132

3233
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>
3334

@@ -41,41 +42,26 @@ namespace nil {
4142
* Specialization for non-native Ed25519 base field element on Pallas base field
4243
*/
4344
template<>
44-
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
45-
typename crypto3::algebra::curves::ed25519::base_field_type> {
46-
47-
constexpr static const std::uint32_t ratio = 4; // 66,66,66,66 bits
45+
struct basic_non_native_policy_field_type<
46+
typename crypto3::algebra::curves::pallas::base_field_type,
47+
typename crypto3::algebra::curves::ed25519::base_field_type
48+
> {
4849
using non_native_field_type = typename crypto3::algebra::curves::ed25519::base_field_type;
4950
using native_field_type = typename crypto3::algebra::curves::pallas::base_field_type;
5051
using var = crypto3::zk::snark::plonk_variable<typename native_field_type::value_type>;
5152

52-
typedef std::array<var, ratio> non_native_var_type;
53-
typedef std::array<native_field_type::value_type, ratio> chopped_value_type;
54-
55-
constexpr static const std::array<std::size_t, ratio> chunk_sizes = {66, 66, 66, 66};
56-
57-
58-
static native_field_type::value_type get_i_th_chunk(non_native_field_type::value_type input,
59-
std::size_t i_th) {
60-
assert(i_th < ratio && "non-native type does not have that much chunks!");
61-
native_field_type::extended_integral_type result = native_field_type::extended_integral_type(input.data);
62-
native_field_type::integral_type base = 1;
63-
native_field_type::integral_type mask = (base << chunk_sizes[i_th]) - 1;
64-
std::size_t shift = 0;
65-
for (std::size_t i = 1; i <= i_th; i++) {
66-
shift += chunk_sizes[i - 1];
67-
}
68-
69-
return (result >> shift) & mask;
70-
}
53+
constexpr static const std::uint32_t native_type_element_bit_length = 66;
54+
constexpr static const std::uint32_t native_type_elements_needed =
55+
(non_native_field_type::value_bits + (native_type_element_bit_length - 1))
56+
/ native_type_element_bit_length
57+
;
7158

59+
using non_native_var_type = std::array<var, native_type_elements_needed>;
60+
using chopped_value_type = std::array<native_field_type::value_type, native_type_elements_needed>;
7261

7362
static chopped_value_type chop_non_native(non_native_field_type::value_type input) {
74-
chopped_value_type result;
75-
for (std::size_t i = 0; i < ratio; i++) {
76-
result[i] = get_i_th_chunk(input, i);
77-
}
78-
return result;
63+
return marshalling::bincode::field<non_native_field_type>
64+
::split_field_element<native_field_type, native_type_element_bit_length>(input);
7965
}
8066

8167
static non_native_field_type::value_type glue_non_native(chopped_value_type input) {
@@ -101,10 +87,7 @@ namespace nil {
10187
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
10288
typename crypto3::algebra::curves::ed25519::scalar_field_type> {
10389

104-
constexpr static const std::uint32_t ratio = 1;
105-
106-
typedef crypto3::zk::snark::plonk_variable<typename crypto3::algebra::curves::pallas::base_field_type::value_type>
107-
non_native_var_type;
90+
using non_native_var_type = crypto3::zk::snark::plonk_variable<typename crypto3::algebra::curves::pallas::base_field_type::value_type>;
10891
};
10992

11093
/*
@@ -114,38 +97,22 @@ namespace nil {
11497
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
11598
typename crypto3::algebra::curves::pallas::scalar_field_type> {
11699

117-
constexpr static const std::uint32_t ratio = 2; // 254, 1 bits
118100
using non_native_field_type = typename crypto3::algebra::curves::pallas::scalar_field_type;
119101
using native_field_type = typename crypto3::algebra::curves::pallas::base_field_type;
120102
using var = crypto3::zk::snark::plonk_variable<native_field_type>;
121103

122-
typedef std::array<var, ratio> non_native_var_type;
123-
typedef std::array<native_field_type::value_type, ratio> chopped_value_type;
124-
125-
constexpr static const std::array<std::size_t, ratio> chunk_sizes = {254, 1};
126-
127-
128-
static native_field_type::value_type get_i_th_chunk(non_native_field_type::value_type input,
129-
std::size_t i_th) {
130-
assert(i_th < ratio && "non-native type does not have that much chunks!");
131-
native_field_type::extended_integral_type result = native_field_type::extended_integral_type(input.data);
132-
native_field_type::integral_type base = 1;
133-
native_field_type::integral_type mask = (base << chunk_sizes[i_th]) - 1;
134-
std::size_t shift = 0;
135-
for (std::size_t i = 1; i <= i_th; i++) {
136-
shift += chunk_sizes[i - 1];
137-
}
138-
139-
return (result >> shift) & mask;
140-
}
104+
constexpr static const std::uint32_t native_type_element_bit_length = 254;
105+
constexpr static const std::uint32_t native_type_elements_needed =
106+
(non_native_field_type::value_bits + (native_type_element_bit_length - 1))
107+
/ native_type_element_bit_length
108+
;
141109

110+
using non_native_var_type = std::array<var, native_type_elements_needed>;
111+
using chopped_value_type = std::array<native_field_type::value_type, native_type_elements_needed>;
142112

143113
static chopped_value_type chop_non_native(non_native_field_type::value_type input) {
144-
chopped_value_type result;
145-
for (std::size_t i = 0; i < ratio; i++) {
146-
result[i] = get_i_th_chunk(input, i);
147-
}
148-
return result;
114+
return marshalling::bincode::field<non_native_field_type>
115+
::split_field_element<native_field_type, native_type_element_bit_length>(input);
149116
}
150117

151118
static non_native_field_type::value_type glue_non_native(chopped_value_type input) {
@@ -170,9 +137,8 @@ namespace nil {
170137
template<typename BlueprintFieldType>
171138
struct basic_non_native_policy_field_type<BlueprintFieldType, BlueprintFieldType> {
172139

173-
constexpr static const std::uint32_t ratio = 1;
140+
using value_type = crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>;
174141

175-
typedef crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type> value_type;
176142
};
177143
} // namespace detail
178144

0 commit comments

Comments
 (0)