Skip to content

Commit

Permalink
Use pack() for chopping values
Browse files Browse the repository at this point in the history
Author:    x-mass <[email protected]>
  • Loading branch information
x-mass committed Sep 18, 2023
1 parent 1e9ceab commit 9933e9e
Showing 1 changed file with 21 additions and 55 deletions.
76 changes: 21 additions & 55 deletions include/nil/blueprint/basic_non_native_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/crypto3/algebra/curves/ed25519.hpp>
#include <nil/marshalling/algorithms/pack.hpp>

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

Expand All @@ -41,44 +42,27 @@ namespace nil {
* Specialization for non-native Ed25519 base field element on Pallas base field
*/
template<>
struct basic_non_native_policy_field_type<typename crypto3::algebra::curves::pallas::base_field_type,
typename crypto3::algebra::curves::ed25519::base_field_type> {

constexpr static const std::uint32_t ratio = 4; // 66,66,66,66 bits
struct basic_non_native_policy_field_type<
typename crypto3::algebra::curves::pallas::base_field_type,
typename crypto3::algebra::curves::ed25519::base_field_type
> {
using non_native_field_type = typename crypto3::algebra::curves::ed25519::base_field_type;
using native_field_type = typename crypto3::algebra::curves::pallas::base_field_type;
using var = crypto3::zk::snark::plonk_variable<typename native_field_type::value_type>;

typedef std::array<var, ratio> non_native_var_type;
typedef std::array<native_field_type::value_type, ratio> chopped_value_type;

constexpr static const std::array<std::size_t, ratio> chunk_sizes = {66, 66, 66, 66};


static native_field_type::value_type get_i_th_chunk(non_native_field_type::value_type input,
std::size_t i_th) {
assert(i_th < ratio && "non-native type does not have that much chunks!");
native_field_type::extended_integral_type result = native_field_type::extended_integral_type(input.data);
native_field_type::integral_type base = 1;
native_field_type::integral_type mask = (base << chunk_sizes[i_th]) - 1;
std::size_t shift = 0;
for (std::size_t i = 1; i <= i_th; i++) {
shift += chunk_sizes[i - 1];
}

return (result >> shift) & mask;
}
constexpr static const std::uint32_t native_type_element_bit_length = 66;
constexpr static const std::uint32_t native_type_elements_needed =
(non_native_field_type::value_bits + (native_type_element_bit_length - 1))
/ native_type_element_bit_length
;

using non_native_var_type = std::array<var, native_type_elements_needed>;
using chopped_value_type = std::array<native_field_type::value_type, native_type_elements_needed>;

static chopped_value_type chop_non_native(non_native_field_type::value_type input) {
chopped_value_type result;
for (std::size_t i = 0; i < ratio; i++) {
result[i] = get_i_th_chunk(input, i);

}

nil::marshalling::pack(input, result);
return result;

}
};

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

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

typedef std::array<var, ratio> non_native_var_type;
typedef std::array<native_field_type::value_type, ratio> chopped_value_type;

constexpr static const std::array<std::size_t, ratio> chunk_sizes = {254, 1};


static native_field_type::value_type get_i_th_chunk(non_native_field_type::value_type input,
std::size_t i_th) {
assert(i_th < ratio && "non-native type does not have that much chunks!");
native_field_type::extended_integral_type result = native_field_type::extended_integral_type(input.data);
native_field_type::integral_type base = 1;
native_field_type::integral_type mask = (base << chunk_sizes[i_th]) - 1;
std::size_t shift = 0;
for (std::size_t i = 1; i <= i_th; i++) {
shift += chunk_sizes[i - 1];
}

return (result >> shift) & mask;
}
constexpr static const std::uint32_t native_type_element_bit_length = 254;
constexpr static const std::uint32_t native_type_elements_needed =
(non_native_field_type::value_bits + (native_type_element_bit_length - 1))
/ native_type_element_bit_length
;

using non_native_var_type = std::array<var, native_type_elements_needed>;
using chopped_value_type = std::array<native_field_type::value_type, native_type_elements_needed>;

static chopped_value_type chop_non_native(non_native_field_type::value_type input) {
chopped_value_type result;
for (std::size_t i = 0; i < ratio; i++) {
result[i] = get_i_th_chunk(input, i);

}

nil::marshalling::pack(input, result);
return result;

}
};

Expand Down

0 comments on commit 9933e9e

Please sign in to comment.