Skip to content

Commit

Permalink
Minor tests updates #116
Browse files Browse the repository at this point in the history
  • Loading branch information
nemothenoone committed Mar 31, 2023
1 parent 7bf936a commit c025b88
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 182 deletions.
2 changes: 1 addition & 1 deletion cmake/modules
201 changes: 111 additions & 90 deletions test/non_native/plonk/bool_scalar_multiplication.cpp

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion test/non_native/plonk/doubling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_doubling) {
}
};

test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(params, public_input, result_check);
test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(params, public_input,
result_check);
}

BOOST_AUTO_TEST_SUITE_END()
10 changes: 5 additions & 5 deletions test/non_native/plonk/ec_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ BOOST_AUTO_TEST_CASE(blueprint_ec_point_to_work) {
constexpr std::size_t ConstantColumns = 1;
constexpr std::size_t SelectorColumns = 5;
using ArithmetizationParams =
zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
nil::crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = blueprint::assignment<ArithmetizationType>;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using var = zk::snark::plonk_variable<BlueprintFieldType>;
using var = nil::crypto3::zk::snark::plonk_variable<BlueprintFieldType>;

using component_type =
zk::components::ec_point<ArithmetizationType, curve_type, ed25519_type, 0, 1, 2, 3, 4, 5, 6, 7, 8>;
nil::crypto3::zk::components::ec_point<ArithmetizationType, curve_type, ed25519_type, 0, 1, 2, 3, 4, 5, 6, 7, 8>;

auto value = ed25519_type::template g1_type<algebra::curves::coordinates::affine>::value_type::one();
auto value = ed25519_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type::one();

typename ed25519_type::scalar_field_type::integral_type base = 1;
typename ed25519_type::scalar_field_type::integral_type mask = (base << 66) - 1;
Expand Down
22 changes: 11 additions & 11 deletions test/non_native/plonk/ed25519.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

BOOST_AUTO_TEST_CASE(blueprint_edwards) {

using curve_type = crypto3::algebra::curves::pallas;
using ed25519_type = crypto3::algebra::curves::ed25519;
using curve_type = nil::crypto3::algebra::curves::pallas;
using ed25519_type = nil::crypto3::algebra::curves::ed25519;
using BlueprintFieldType = typename curve_type::base_field_type;
constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
Expand All @@ -59,20 +59,20 @@ BOOST_AUTO_TEST_CASE(blueprint_edwards) {
using ArithmetizationParams =
zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = blueprint::assignment<ArithmetizationType>;
using AssignmentType = nil::blueprint::assignment<ArithmetizationType>;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using var = zk::snark::plonk_variable<BlueprintFieldType>;

using component_type = zk::components::eddsa25519<ArithmetizationType, curve_type, ed25519_type, 0, 1, 2, 3,
4, 5, 6, 7, 8>;
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type B =
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type::one();
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type R = 2*B;
ed25519_type::scalar_field_type::value_type b = crypto3::algebra::random_element<ed25519_type::scalar_field_type>();
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type T = b*R;
ed25519_type::scalar_field_type::value_type s = 2*b + 2;
using component_type =
zk::components::eddsa25519<ArithmetizationType, curve_type, ed25519_type, 0, 1, 2, 3, 4, 5, 6, 7, 8>;
ed25519_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type B =
ed25519_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type::one();
ed25519_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type R = 2 * B;
ed25519_type::scalar_field_type::value_type b = nil::crypto3::algebra::random_element<ed25519_type::scalar_field_type>();
ed25519_type::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type T = b * R;
ed25519_type::scalar_field_type::value_type s = 2 * b + 2;

ed25519_type::base_field_type::integral_type Tx = ed25519_type::base_field_type::integral_type(T.X.data);
ed25519_type::base_field_type::integral_type Ty = ed25519_type::base_field_type::integral_type(T.Y.data);
Expand Down
10 changes: 6 additions & 4 deletions test/non_native/plonk/fixed_base_mul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_fixed_base_mul) {

using var = zk::snark::plonk_variable<BlueprintFieldType>;

using component_type = zk::components::fixed_base_multiplication<ArithmetizationType, curve_type, ed25519_type, 0,
1, 2, 3, 4, 5, 6, 7, 8>;
using component_type =
nil::crypto3::zk::components::fixed_base_multiplication<ArithmetizationType, curve_type, ed25519_type, 0, 1, 2,
3, 4, 5, 6, 7, 8>;

var var_b = var(0, 0, false, var::column_type::public_input);

ed25519_type::scalar_field_type::value_type b = crypto3::algebra::random_element<ed25519_type::scalar_field_type>();

typename component_type::params_type params = {{var_b}};

ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type B = ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type::one();
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type P = b*B;
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type B =
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type::one();
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type P = b * B;

ed25519_type::base_field_type::integral_type Px = ed25519_type::base_field_type::integral_type(P.X.data);
ed25519_type::base_field_type::integral_type Py = ed25519_type::base_field_type::integral_type(P.Y.data);
Expand Down
4 changes: 3 additions & 1 deletion test/non_native/plonk/non_native_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_kimchi_demo_verifier_test) {

typename mul_component_type::params_type mul_params = {{input_var_Xa, input_var_Xb}, b_var};

ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type T = crypto3::algebra::random_element<ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>>();
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type T =
crypto3::algebra::random_element<
ed25519_type::template g1_type<crypto3::algebra::curves::coordinates::affine>>();
ed25519_type::scalar_field_type::value_type b = crypto3::algebra::random_element<ed25519_type::scalar_field_type>();

ed25519_type::base_field_type::integral_type integral_b = ed25519_type::base_field_type::integral_type(b.data);
Expand Down
82 changes: 40 additions & 42 deletions test/non_native/plonk/scalar_non_native_range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,83 +43,83 @@

using namespace nil;

template <typename BlueprintFieldType>
auto test_scalar_non_native_range_inner(std::vector<typename BlueprintFieldType::value_type> public_input){
template<typename BlueprintFieldType>
auto test_scalar_non_native_range_inner(std::vector<typename BlueprintFieldType::value_type> public_input) {

using ed25519_type = crypto3::algebra::curves::ed25519;
constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 0;
constexpr std::size_t SelectorColumns = 2;
using ArithmetizationParams =
crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationParams = crypto3::zk::snark::
plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = blueprint::assignment<ArithmetizationType>;
using hash_type = crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using var = crypto3::zk::snark::plonk_variable<BlueprintFieldType>;

using component_type = blueprint::components::scalar_non_native_range<ArithmetizationType,
ed25519_type, 9>;
using component_type = blueprint::components::scalar_non_native_range<ArithmetizationType, ed25519_type, 9>;

typename component_type::input_type instance_input = {var(0, 0, false, var::column_type::public_input)};

auto result_check = [public_input](AssignmentType &assignment,
typename component_type::result_type &real_res) {
#ifdef BLUEPRINT_PLONK_PROFILING_ENABLED
std::cout << std::hex << "________________________________________________________________________________________\ninput: " << public_input[0].data << std::endl;
#endif
auto result_check = [public_input](AssignmentType &assignment, typename component_type::result_type &real_res) {
#ifdef BLUEPRINT_PLONK_PROFILING_ENABLED
std::cout << std::hex
<< "________________________________________________________________________________________\ninput: "
<< public_input[0].data << std::endl;
#endif
};

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{});
component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8}, {}, {});

return std::make_tuple(component_instance, instance_input, result_check);
}

template <typename BlueprintFieldType>
void test_scalar_non_native_range(std::vector<typename BlueprintFieldType::value_type> public_input){
template<typename BlueprintFieldType>
void test_scalar_non_native_range(std::vector<typename BlueprintFieldType::value_type> public_input) {

using ed25519_type = crypto3::algebra::curves::ed25519;
constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 0;
constexpr std::size_t SelectorColumns = 2;
using ArithmetizationParams =
crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationParams = crypto3::zk::snark::
plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = blueprint::assignment<ArithmetizationType>;
using hash_type = crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using component_type = blueprint::components::scalar_non_native_range<ArithmetizationType,
ed25519_type, 9>;
using component_type = blueprint::components::scalar_non_native_range<ArithmetizationType, ed25519_type, 9>;

auto [component_instance, instance_input, result_check] = test_scalar_non_native_range_inner<BlueprintFieldType>(public_input);
auto [component_instance, instance_input, result_check] =
test_scalar_non_native_range_inner<BlueprintFieldType>(public_input);

crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input);
}

template <typename BlueprintFieldType>
void test_scalar_non_native_range_to_fail(std::vector<typename BlueprintFieldType::value_type> public_input){
template<typename BlueprintFieldType>
void test_scalar_non_native_range_to_fail(std::vector<typename BlueprintFieldType::value_type> public_input) {

using ed25519_type = crypto3::algebra::curves::ed25519;
constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 0;
constexpr std::size_t SelectorColumns = 2;
using ArithmetizationParams =
crypto3::zk::snark::plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationParams = crypto3::zk::snark::
plonk_arithmetization_params<WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns>;
using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>;
using AssignmentType = blueprint::assignment<ArithmetizationType>;
using hash_type = crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;

using component_type = blueprint::components::scalar_non_native_range<ArithmetizationType,
ed25519_type, 9>;
using component_type = blueprint::components::scalar_non_native_range<ArithmetizationType, ed25519_type, 9>;

auto [component_instance, instance_input, result_check] = test_scalar_non_native_range_inner<BlueprintFieldType>(public_input);
auto [component_instance, instance_input, result_check] =
test_scalar_non_native_range_inner<BlueprintFieldType>(public_input);

crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
component_instance, public_input, result_check, instance_input);
Expand All @@ -130,22 +130,19 @@ constexpr static const std::size_t random_tests_amount = 10;
BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test0) {
test_scalar_non_native_range<typename crypto3::algebra::curves::pallas::base_field_type>(
{45524});
test_scalar_non_native_range<typename crypto3::algebra::curves::pallas::base_field_type>({45524});
}

BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test1) {
using field_type = typename crypto3::algebra::curves::pallas::base_field_type;


typename field_type::integral_type ed25519_scalar_modulus = 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed_cppui255;
typename field_type::value_type ones = 0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui255;
typename field_type::integral_type ed25519_scalar_modulus =
0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed_cppui255;
typename field_type::value_type ones = 0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui255;

test_scalar_non_native_range<field_type>(
{typename field_type::value_type(ed25519_scalar_modulus-1)});
test_scalar_non_native_range<field_type>({typename field_type::value_type(ed25519_scalar_modulus - 1)});

test_scalar_non_native_range<field_type>(
{typename field_type::value_type(ones)});
test_scalar_non_native_range<field_type>({typename field_type::value_type(ones)});

test_scalar_non_native_range<field_type>({1});

Expand All @@ -155,7 +152,6 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test1) {
boost::random::mt19937 seed_seq;
rand.seed(seed_seq);


typename field_type::value_type r;
typename field_type::integral_type r_integral;

Expand All @@ -175,15 +171,17 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_scalar_range_test_must_fail) {
boost::random::mt19937 seed_seq;
rand.seed(seed_seq);

typename field_type::integral_type ed25519_scalar_modulus = 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed_cppui255;
typename field_type::integral_type ed25519_scalar_modulus =
0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed_cppui255;
typename field_type::integral_type zero = 0;
typename field_type::integral_type ed25519_scalar_overage = zero - ed25519_scalar_modulus - 1;

typename field_type::integral_type overage;

for (std::size_t i = 0; i < random_tests_amount; i++) {
overage = (typename field_type::integral_type(rand().data)) % ed25519_scalar_overage;
test_scalar_non_native_range_to_fail<field_type>({typename field_type::value_type(ed25519_scalar_modulus + overage)}); // false positive
test_scalar_non_native_range_to_fail<field_type>(
{typename field_type::value_type(ed25519_scalar_modulus + overage)}); // false positive
}
test_scalar_non_native_range_to_fail<field_type>({-1});
}
Expand Down
Loading

0 comments on commit c025b88

Please sign in to comment.