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

Commit 1621171

Browse files
committed
Use algebra split_field_element() for chopping
1 parent 1e9ceab commit 1621171

File tree

1 file changed

+25
-65
lines changed

1 file changed

+25
-65
lines changed

include/nil/blueprint/basic_non_native_policy.hpp

Lines changed: 25 additions & 65 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,44 +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-
}
79-
80-
return result;
81-
63+
return marshalling::bincode::field<non_native_field_type>
64+
::split_field_element<native_field_type, native_type_element_bit_length>(input);
8265
}
8366
};
8467

@@ -89,10 +72,7 @@ namespace nil {
8972
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
9073
typename crypto3::algebra::curves::ed25519::scalar_field_type> {
9174

92-
constexpr static const std::uint32_t ratio = 1;
93-
94-
typedef crypto3::zk::snark::plonk_variable<typename crypto3::algebra::curves::pallas::base_field_type::value_type>
95-
non_native_var_type;
75+
using non_native_var_type = crypto3::zk::snark::plonk_variable<typename crypto3::algebra::curves::pallas::base_field_type::value_type>;
9676
};
9777

9878
/*
@@ -102,41 +82,22 @@ namespace nil {
10282
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
10383
typename crypto3::algebra::curves::pallas::scalar_field_type> {
10484

105-
constexpr static const std::uint32_t ratio = 2; // 254, 1 bits
10685
using non_native_field_type = typename crypto3::algebra::curves::pallas::scalar_field_type;
10786
using native_field_type = typename crypto3::algebra::curves::pallas::base_field_type;
10887
using var = crypto3::zk::snark::plonk_variable<native_field_type>;
10988

110-
typedef std::array<var, ratio> non_native_var_type;
111-
typedef std::array<native_field_type::value_type, ratio> chopped_value_type;
112-
113-
constexpr static const std::array<std::size_t, ratio> chunk_sizes = {254, 1};
114-
115-
116-
static native_field_type::value_type get_i_th_chunk(non_native_field_type::value_type input,
117-
std::size_t i_th) {
118-
assert(i_th < ratio && "non-native type does not have that much chunks!");
119-
native_field_type::extended_integral_type result = native_field_type::extended_integral_type(input.data);
120-
native_field_type::integral_type base = 1;
121-
native_field_type::integral_type mask = (base << chunk_sizes[i_th]) - 1;
122-
std::size_t shift = 0;
123-
for (std::size_t i = 1; i <= i_th; i++) {
124-
shift += chunk_sizes[i - 1];
125-
}
126-
127-
return (result >> shift) & mask;
128-
}
89+
constexpr static const std::uint32_t native_type_element_bit_length = 254;
90+
constexpr static const std::uint32_t native_type_elements_needed =
91+
(non_native_field_type::value_bits + (native_type_element_bit_length - 1))
92+
/ native_type_element_bit_length
93+
;
12994

95+
using non_native_var_type = std::array<var, native_type_elements_needed>;
96+
using chopped_value_type = std::array<native_field_type::value_type, native_type_elements_needed>;
13097

13198
static chopped_value_type chop_non_native(non_native_field_type::value_type input) {
132-
chopped_value_type result;
133-
for (std::size_t i = 0; i < ratio; i++) {
134-
result[i] = get_i_th_chunk(input, i);
135-
136-
}
137-
138-
return result;
139-
99+
return marshalling::bincode::field<non_native_field_type>
100+
::split_field_element<native_field_type, native_type_element_bit_length>(input);
140101
}
141102
};
142103

@@ -146,9 +107,8 @@ namespace nil {
146107
template<typename BlueprintFieldType>
147108
struct basic_non_native_policy_field_type<BlueprintFieldType, BlueprintFieldType> {
148109

149-
constexpr static const std::uint32_t ratio = 1;
110+
using value_type = crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>;
150111

151-
typedef crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type> value_type;
152112
};
153113
} // namespace detail
154114

0 commit comments

Comments
 (0)