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

Commit ca439d8

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

File tree

1 file changed

+22
-55
lines changed

1 file changed

+22
-55
lines changed

include/nil/blueprint/basic_non_native_policy.hpp

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include <nil/crypto3/algebra/curves/pallas.hpp>
3030
#include <nil/crypto3/algebra/curves/ed25519.hpp>
31+
#include <nil/marshalling/algorithms/pack.hpp>
32+
#include <nil/marshalling/options.hpp>
3133

3234
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>
3335

@@ -41,44 +43,27 @@ namespace nil {
4143
* Specialization for non-native Ed25519 base field element on Pallas base field
4244
*/
4345
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
46+
struct basic_non_native_policy_field_type<
47+
typename crypto3::algebra::curves::pallas::base_field_type,
48+
typename crypto3::algebra::curves::ed25519::base_field_type
49+
> {
4850
using non_native_field_type = typename crypto3::algebra::curves::ed25519::base_field_type;
4951
using native_field_type = typename crypto3::algebra::curves::pallas::base_field_type;
5052
using var = crypto3::zk::snark::plonk_variable<typename native_field_type::value_type>;
5153

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-
}
54+
constexpr static const std::uint32_t native_type_element_bit_length = 66;
55+
constexpr static const std::uint32_t native_type_elements_needed =
56+
(non_native_field_type::value_bits + (native_type_element_bit_length - 1))
57+
/ native_type_element_bit_length
58+
;
7159

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

7363
static chopped_value_type chop_non_native(non_native_field_type::value_type input) {
7464
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-
65+
nil::marshalling::pack(input, result);
8066
return result;
81-
8267
}
8368
};
8469

@@ -102,41 +87,23 @@ namespace nil {
10287
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
10388
typename crypto3::algebra::curves::pallas::scalar_field_type> {
10489

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

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

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

131103
static chopped_value_type chop_non_native(non_native_field_type::value_type input) {
132104
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-
105+
nil::marshalling::pack(input, result);
138106
return result;
139-
140107
}
141108
};
142109

0 commit comments

Comments
 (0)