Skip to content

Commit

Permalink
variable_base_scalar_mul test update #107
Browse files Browse the repository at this point in the history
  • Loading branch information
CblPOK-git committed Jan 30, 2023
1 parent 5657d44 commit ee0ed71
Showing 1 changed file with 64 additions and 7 deletions.
71 changes: 64 additions & 7 deletions test/algebra/curves/plonk/variable_base_scalar_mul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,42 +73,99 @@ typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates:

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},{0},{});

auto result_check = [&expected](AssignmentType &assignment,
auto result_check = [&expected, public_input](AssignmentType &assignment,
typename component_type::result_type &real_res) {
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type R;
R.X = var_value(assignment, real_res.X);
R.Y = var_value(assignment, real_res.Y);
assert(expected.X == var_value(assignment, real_res.X));
assert(expected.Y == var_value(assignment, real_res.Y));
std::cout << "var base scal mul: (" << public_input[0].data << " " << public_input[1].data << ") * " << public_input[2].data << "\n";
std::cout << "expected:" << expected.X.data << " " << expected.Y.data << "\n";
std::cout << "real :" << R.X.data << " " << R.Y.data << "\n\n";
auto ydata_expected = expected.Y.data;
auto ydata_real = R.Y.data;

assert(expected.X == R.X);
std::cout << "assert(" << ydata_expected << " == " << ydata_real << ");" << std::endl;
assert(ydata_expected == ydata_real);
};
nil::crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda> (component_instance, public_input, result_check, instance_input);
}


template <typename CurveType>
struct shift_params;

template<>
struct shift_params<nil::crypto3::algebra::curves::pallas> {
constexpr static const typename nil::crypto3::algebra::curves::pallas::scalar_field_type::value_type shift_base = 2;
constexpr static const typename nil::crypto3::algebra::curves::pallas::scalar_field_type::value_type shift_for_1_0_neg1 = shift_base.pow(255) + 1;
constexpr static const typename nil::crypto3::algebra::curves::pallas::scalar_field_type::value_type denominator_for_1_0_neg1 = 2;
};

template<>
struct shift_params<nil::crypto3::algebra::curves::vesta> {
constexpr static const typename nil::crypto3::algebra::curves::vesta::scalar_field_type::value_type shift_base = 2;
constexpr static const typename nil::crypto3::algebra::curves::vesta::scalar_field_type::value_type shift_for_1_0_neg1 = shift_base.pow(255);
constexpr static const typename nil::crypto3::algebra::curves::vesta::scalar_field_type::value_type denominator_for_1_0_neg1 = 1;
};

template<typename CurveType>
typename CurveType::base_field_type::value_type shift_scalar(typename CurveType::scalar_field_type::value_type unshifted) {
typename CurveType::scalar_field_type::value_type shift_base = 2;
typename CurveType::scalar_field_type::value_type shift = shift_base.pow(255) + 1;
typename CurveType::scalar_field_type::value_type denominator = 2;

typename CurveType::scalar_field_type::value_type shift_for_1_0_neg1 = shift_params<CurveType>::shift_for_1_0_neg1;
typename CurveType::scalar_field_type::value_type denominator_for_1_0_neg1 = shift_params<CurveType>::denominator_for_1_0_neg1;

typename CurveType::scalar_field_type::value_type shifted;

if ((unshifted == 1) || (unshifted == 0) || (unshifted == 1)){
shifted = (unshifted - shift_for_1_0_neg1) / denominator_for_1_0_neg1;
}
else {
shifted = (unshifted - shift) / denominator;
}

typename CurveType::scalar_field_type::integral_type shifted_integral_type = typename CurveType::scalar_field_type::integral_type(shifted.data);
typename CurveType::base_field_type::value_type shifted_base_value_type = shifted_integral_type;
return shifted_base_value_type;
}

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

BOOST_AUTO_TEST_CASE(blueprint_plonk_variable_base_scalar_mul_random_scalar_pallas) {
using curve_type = nil::crypto3::algebra::curves::pallas;
using BlueprintFieldType = typename curve_type::base_field_type;
using BlueprintScalarType = typename curve_type::scalar_field_type;

typename BlueprintScalarType::value_type b_scalar = nil::crypto3::algebra::random_element<BlueprintScalarType>();

typename BlueprintScalarType::value_type b_scalar = 0x20000000000000000000000000000000224698fc094cf91b992d30ed00000000_cppui256;//nil::crypto3::algebra::random_element<BlueprintScalarType>();
typename curve_type::scalar_field_type::value_type shift_base = 2;
auto shift = shift_base.pow(255) + 1;
typename BlueprintScalarType::value_type x = (b_scalar - shift)/2;
typename BlueprintScalarType::integral_type integral_x = typename BlueprintScalarType::integral_type(x.data);
typename BlueprintFieldType::value_type x_scalar = integral_x;
shift_scalar<curve_type>(b_scalar);

curve_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type T = nil::crypto3::algebra::random_element<curve_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>>();
curve_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type zero_point = {0, 0};
std::vector<typename BlueprintFieldType::value_type> public_input = {T.X, T.Y, x_scalar};
curve_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type expected;
curve_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type expected;
if (b_scalar != 0) {
expected = b_scalar * T;
} else {
expected = {0, 0};
}

test_variable_base_scalar_mul<curve_type>(public_input, expected);
test_variable_base_scalar_mul<curve_type>({T.X, T.Y, shift_scalar<curve_type>(b_scalar)}, expected);
test_variable_base_scalar_mul<curve_type>({T.X, T.Y, shift_scalar<curve_type>(-1)}, {T.X, -T.Y});
test_variable_base_scalar_mul<curve_type>({T.X, T.Y, shift_scalar<curve_type>(0)}, zero_point);
test_variable_base_scalar_mul<curve_type>({T.X, T.Y, shift_scalar<curve_type>(1)}, T);

test_variable_base_scalar_mul<curve_type>({0, 0, shift_scalar<curve_type>(b_scalar)}, zero_point);
test_variable_base_scalar_mul<curve_type>({0, 0, shift_scalar<curve_type>(-1)}, zero_point);
test_variable_base_scalar_mul<curve_type>({0, 0, shift_scalar<curve_type>(0)}, zero_point);
test_variable_base_scalar_mul<curve_type>({0, 0, shift_scalar<curve_type>(1)}, zero_point);
}

BOOST_AUTO_TEST_CASE(blueprint_plonk_variable_base_scalar_mul_scalar_one_pallas) {
Expand Down

0 comments on commit ee0ed71

Please sign in to comment.