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

Commit 9933e9e

Browse files
committed
Use pack() for chopping values
Author: x-mass <[email protected]>
1 parent 1e9ceab commit 9933e9e

File tree

1 file changed

+21
-55
lines changed

1 file changed

+21
-55
lines changed

include/nil/blueprint/basic_non_native_policy.hpp

Lines changed: 21 additions & 55 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/marshalling/algorithms/pack.hpp>
3132

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

@@ -41,44 +42,27 @@ 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) {
7463
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-
64+
nil::marshalling::pack(input, result);
8065
return result;
81-
8266
}
8367
};
8468

@@ -102,41 +86,23 @@ namespace nil {
10286
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
10387
typename crypto3::algebra::curves::pallas::scalar_field_type> {
10488

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

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-
}
93+
constexpr static const std::uint32_t native_type_element_bit_length = 254;
94+
constexpr static const std::uint32_t native_type_elements_needed =
95+
(non_native_field_type::value_bits + (native_type_element_bit_length - 1))
96+
/ native_type_element_bit_length
97+
;
12998

99+
using non_native_var_type = std::array<var, native_type_elements_needed>;
100+
using chopped_value_type = std::array<native_field_type::value_type, native_type_elements_needed>;
130101

131102
static chopped_value_type chop_non_native(non_native_field_type::value_type input) {
132103
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-
104+
nil::marshalling::pack(input, result);
138105
return result;
139-
140106
}
141107
};
142108

0 commit comments

Comments
 (0)