diff --git a/include/nil/blueprint/components/algebra/fields/plonk/exponentiation.hpp b/include/nil/blueprint/components/algebra/fields/plonk/exponentiation.hpp index bb9f3b67e0..35a8f3f642 100644 --- a/include/nil/blueprint/components/algebra/fields/plonk/exponentiation.hpp +++ b/include/nil/blueprint/components/algebra/fields/plonk/exponentiation.hpp @@ -124,7 +124,7 @@ namespace nil { }; - template + template using plonk_exponentiation = exponentiation< crypto3::zk::snark::plonk_constraint_system, diff --git a/test/algebra/curves/plonk/endo_scalar.cpp b/test/algebra/curves/plonk/endo_scalar.cpp index a73c262286..f2080aba75 100644 --- a/test/algebra/curves/plonk/endo_scalar.cpp +++ b/test/algebra/curves/plonk/endo_scalar.cpp @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #include @@ -47,9 +47,94 @@ #include "test_plonk_component.hpp" +template +struct endo_scalar_params; + +template<> +struct endo_scalar_params { + using curve_type = nil::crypto3::algebra::curves::vesta; + using scalar_field_type = typename curve_type::scalar_field_type; + using base_field_type = typename curve_type::base_field_type; + constexpr static const typename scalar_field_type::value_type endo_r = + 0x12CCCA834ACDBA712CAAD5DC57AAB1B01D1F8BD237AD31491DAD5EBDFDFE4AB9_cppui255; + constexpr static const typename base_field_type::value_type endo_q = + 0x2D33357CB532458ED3552A23A8554E5005270D29D19FC7D27B7FD22F0201B547_cppui255; +}; + +template<> +struct endo_scalar_params { + using curve_type = nil::crypto3::algebra::curves::pallas; + using scalar_field_type = typename curve_type::scalar_field_type; + using base_field_type = typename curve_type::base_field_type; + constexpr static const typename scalar_field_type::value_type endo_r = + 0x397E65A7D7C1AD71AEE24B27E308F0A61259527EC1D4752E619D1840AF55F1B1_cppui255; + constexpr static const typename base_field_type::value_type endo_q = + 0x2D33357CB532458ED3552A23A8554E5005270D29D19FC7D27B7FD22F0201B547_cppui255; +}; + +template +typename CurveType::scalar_field_type::value_type calculate_endo_scalar(typename CurveType::scalar_field_type::value_type scalar) { + + using endo_params = endo_scalar_params; + using BlueprintFieldType = typename CurveType::scalar_field_type; + + typename BlueprintFieldType::value_type endo_r = endo_params::endo_r; + + const std::size_t crumbs_per_row = 8; + const std::size_t bits_per_crumb = 2; + const std::size_t bits_per_row = + bits_per_crumb * crumbs_per_row; // we suppose that ScalarSize % bits_per_row = 0 + + typename BlueprintFieldType::integral_type integral_scalar = + typename BlueprintFieldType::integral_type(scalar.data); + std::array bits_msb; + { + nil::marshalling::status_type status; + assert(ScalarSize <= 255); + std::array bits_msb_all = + nil::marshalling::pack(integral_scalar, status); + assert(status == nil::marshalling::status_type::success); + std::copy(bits_msb_all.end() - ScalarSize, bits_msb_all.end(), bits_msb.begin()); + + for(std::size_t i = 0; i < 255 - ScalarSize; ++i) { + assert(bits_msb_all[i] == false); + } + } + typename BlueprintFieldType::value_type a = 2; + typename BlueprintFieldType::value_type b = 2; + typename BlueprintFieldType::value_type n = 0; + + assert (ScalarSize % bits_per_row == 0); + for (std::size_t chunk_start = 0; chunk_start < bits_msb.size(); chunk_start += bits_per_row) { + for (std::size_t j = 0; j < crumbs_per_row; j++) { + std::size_t crumb = chunk_start + j * bits_per_crumb; + typename BlueprintFieldType::value_type b0 = static_cast(bits_msb[crumb + 1]); + typename BlueprintFieldType::value_type b1 = static_cast(bits_msb[crumb + 0]); + + typename BlueprintFieldType::value_type crumb_value = b0 + b1.doubled(); + + a = a.doubled(); + b = b.doubled(); + + typename BlueprintFieldType::value_type s = + (b0 == BlueprintFieldType::value_type::one()) ? 1 : -1; + if (b1 == BlueprintFieldType::value_type::zero()) { + b += s; + } else { + a += s; + } + + n = (n.doubled()).doubled(); + n += crumb_value; + } + } + auto res = a * endo_r + b; + return res; +} + template void test_endo_scalar(std::vector public_input, - typename CurveType::scalar_field_type::value_type expected_res){ + typename CurveType::scalar_field_type::value_type expected_res){ using BlueprintFieldType = typename CurveType::scalar_field_type; constexpr std::size_t WitnessColumns = 15; constexpr std::size_t PublicInputColumns = 1; @@ -70,8 +155,14 @@ void test_endo_scalar(std::vector (component_instance, public_input, result_check, instance_input); } +constexpr static const std::size_t random_tests_amount = 10; + BOOST_AUTO_TEST_SUITE(blueprint_plonk_endo_scalar_test_suite) -BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_addition) { +BOOST_AUTO_TEST_CASE(blueprint_plonk_endo_scalar_vesta) { using curve_type = nil::crypto3::algebra::curves::vesta; using BlueprintFieldType = typename curve_type::scalar_field_type; - + typename BlueprintFieldType::value_type challenge = 0x00000000000000000000000000000000FC93536CAE0C612C18FBE5F6D8E8EEF2_cppui255; typename BlueprintFieldType::value_type result = 0x004638173549A4C55A118327904B54E5F6F6314225C8C862F5AFA2506C77AC65_cppui255; - std::vector public_input = {challenge}; - - test_endo_scalar(public_input, result); + test_endo_scalar({challenge}, result); + test_endo_scalar({1}, calculate_endo_scalar(1)); + test_endo_scalar({0}, calculate_endo_scalar(0)); + test_endo_scalar({0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255}, + calculate_endo_scalar(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255)); + + nil::crypto3::random::algebraic_engine generate_random; + boost::random::mt19937 seed_seq; + generate_random.seed(seed_seq); + + for (std::size_t i = 0; i < random_tests_amount; i++){ + typename curve_type::scalar_field_type::value_type input = generate_random(); + typename curve_type::scalar_field_type::integral_type input_integral = typename curve_type::scalar_field_type::integral_type(input.data); + input_integral = input_integral & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255; + typename curve_type::scalar_field_type::value_type input_scalar = input_integral; + test_endo_scalar({input_scalar}, calculate_endo_scalar(input_scalar)); + } +} + +BOOST_AUTO_TEST_CASE(blueprint_plonk_endo_scalar_pallas) { + using curve_type = nil::crypto3::algebra::curves::pallas; + using BlueprintFieldType = typename curve_type::scalar_field_type; + + typename BlueprintFieldType::value_type challenge = 0x00000000000000000000000000000000FC93536CAE0C612C18FBE5F6D8E8EEF2_cppui255; + + test_endo_scalar({challenge}, calculate_endo_scalar(challenge)); + test_endo_scalar({1}, calculate_endo_scalar(1)); + test_endo_scalar({0}, calculate_endo_scalar(0)); + test_endo_scalar({0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255}, + calculate_endo_scalar(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255)); + + nil::crypto3::random::algebraic_engine generate_random; + boost::random::mt19937 seed_seq; + generate_random.seed(seed_seq); + + for (std::size_t i = 0; i < random_tests_amount; i++){ + typename curve_type::scalar_field_type::value_type input = generate_random(); + typename curve_type::scalar_field_type::integral_type input_integral = typename curve_type::scalar_field_type::integral_type(input.data); + input_integral = input_integral & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255; + typename curve_type::scalar_field_type::value_type input_scalar = input_integral; + test_endo_scalar({input_scalar}, calculate_endo_scalar(input_scalar)); + } } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/algebra/curves/plonk/unified_addition.cpp b/test/algebra/curves/plonk/unified_addition.cpp index acdcbbe66c..ac73153941 100644 --- a/test/algebra/curves/plonk/unified_addition.cpp +++ b/test/algebra/curves/plonk/unified_addition.cpp @@ -28,9 +28,11 @@ #include +#include +#include #include #include -#include +#include #include @@ -70,8 +72,15 @@ void test_unified_addition(std::vector +void test_unified_addition_with_zeroes() { + nil::crypto3::random::algebraic_engine> generate_random_point; + boost::random::mt19937 seed_seq; + generate_random_point.seed(seed_seq); -BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_double) { + typename CurveType::template g1_type::value_type zero_algebraic = {0, 1}; + typename CurveType::template g1_type::value_type zero_circuits = {0, 0}; + typename CurveType::template g1_type::value_type P = generate_random_point(); + typename CurveType::template g1_type::value_type Q = -P; - using curve_type = crypto3::algebra::curves::pallas; + std::vector public_input; + + public_input = {zero_circuits.X, zero_circuits.Y, zero_circuits.X, zero_circuits.Y}; + test_unified_addition(public_input, zero_circuits); + + public_input = {zero_circuits.X, zero_circuits.Y, P.X, P.Y}; + test_unified_addition(public_input, P); - auto P = crypto3::algebra::random_element>().to_affine(); - auto Q(P); + public_input = {P.X, P.Y, zero_circuits.X, zero_circuits.Y}; + test_unified_addition(public_input, P); - std::vector public_input = {P.X, P.Y, Q.X, Q.Y}; - typename curve_type::template g1_type::value_type expected_res = P + Q; + public_input = {P.X, P.Y, Q.X, Q.Y}; + test_unified_addition(public_input, zero_circuits); - test_unified_addition(public_input, expected_res); + public_input = {Q.X, Q.Y, P.X, P.Y}; + test_unified_addition(public_input, zero_circuits); } -BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_addition) { +template +void test_unified_addition_doubling() { + nil::crypto3::random::algebraic_engine> generate_random_point; + boost::random::mt19937 seed_seq; + generate_random_point.seed(seed_seq); - using curve_type = crypto3::algebra::curves::pallas; + typename CurveType::template g1_type::value_type P = generate_random_point(); + typename CurveType::template g1_type::value_type Q(P); + + std::vector public_input; + + public_input = {P.X, P.Y, Q.X, Q.Y}; + test_unified_addition(public_input, P+Q); + + public_input = {Q.X, Q.Y, P.X, P.Y}; + test_unified_addition(public_input, P+Q); +} - auto P = crypto3::algebra::random_element>().to_affine(); - auto Q = crypto3::algebra::random_element>().to_affine(); - typename curve_type::template g1_type::value_type zero = {0, 0}; - typename curve_type::template g1_type::value_type expected_res; - P.X = Q.X; - P.Y = -Q.Y; - if (Q.X == zero.X && Q.Y == zero.Y) { - expected_res = P; - } else { - if (P.X == zero.X && P.Y == zero.Y) { - expected_res = Q; +template +void test_unified_addition_random_data() { + nil::crypto3::random::algebraic_engine> generate_random_point; + boost::random::mt19937 seed_seq; + generate_random_point.seed(seed_seq); + + typename CurveType::template g1_type::value_type P = generate_random_point(); + typename CurveType::template g1_type::value_type Q = generate_random_point(); + typename CurveType::template g1_type::value_type zero = {0, 0}; + typename CurveType::template g1_type::value_type expected_res; + + std::vector public_input; + + for (std::size_t i = 0; i < RandomTestsAmount; i++){ + P = generate_random_point(); + Q = generate_random_point(); + + if (Q.X == zero.X && Q.Y == zero.Y) { + expected_res = P; } else { - if (P.X == Q.X && P.Y == -Q.Y) { - expected_res = {0, 0}; + if (P.X == zero.X && P.Y == zero.Y) { + expected_res = Q; } else { - expected_res = P + Q; + if (P.X == Q.X && P.Y == -Q.Y) { + expected_res = {0, 0}; + } else { + expected_res = P + Q; + } } } + + public_input = {P.X, P.Y, Q.X, Q.Y}; + test_unified_addition(public_input, expected_res); } +} + +constexpr static const std::size_t random_tests_amount = 10; - std::vector public_input = {P.X, P.Y, Q.X, Q.Y}; +BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) + +BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_pallas) { + using curve_type = crypto3::algebra::curves::pallas; + test_unified_addition_with_zeroes(); + test_unified_addition_doubling(); + test_unified_addition_random_data(); +} - test_unified_addition(public_input, expected_res); +BOOST_AUTO_TEST_CASE(blueprint_plonk_unified_addition_vesta) { + using curve_type = crypto3::algebra::curves::vesta; + test_unified_addition_with_zeroes(); + test_unified_addition_doubling(); + test_unified_addition_random_data(); } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/algebra/fields/plonk/exponentiation.cpp b/test/algebra/fields/plonk/exponentiation.cpp index 6b87a69139..e867d0f9c5 100644 --- a/test/algebra/fields/plonk/exponentiation.cpp +++ b/test/algebra/fields/plonk/exponentiation.cpp @@ -27,13 +27,16 @@ #include +#include +#include +#include #include #include #include #include #include -#include +#include #include #include @@ -42,16 +45,14 @@ #include #include "../../../test_plonk_component.hpp" -template -void test_exponentiation(std::vector public_input, - typename CurveType::base_field_type::value_type expected_res){ +template +void test_exponentiation(std::vector public_input){ constexpr std::size_t WitnessColumns = 15; constexpr std::size_t PublicInputColumns = 1; constexpr std::size_t ConstantColumns = 1; constexpr std::size_t SelectorColumns = 1; constexpr std::size_t exp_size = ExpSize; - using BlueprintFieldType = typename CurveType::base_field_type; - using BlueprintScalarType = typename CurveType::scalar_field_type; + using BlueprintFieldType = FieldType; using ArithmetizationParams = nil::crypto3::zk::snark::plonk_arithmetization_params; using ArithmetizationType = nil::crypto3::zk::snark::plonk_constraint_system; @@ -67,8 +68,19 @@ void test_exponentiation(std::vector (component_instance, public_input, result_check, instance_input); +} +template +void exponentiation_tests(){ + for (int i = -2; i < 3; i++){ + for (int j = -2; j < 3; j++){ + test_exponentiation({i, j}); + } + } + + nil::crypto3::random::algebraic_engine generate_random; + boost::random::mt19937 seed_seq; + generate_random.seed(seed_seq); + + for (std::size_t i = 0; i < RandomTestsAmount; i++) { + test_exponentiation({generate_random(), generate_random()}); + } +} +constexpr static const std::size_t random_tests_amount = 10; + +BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) +BOOST_AUTO_TEST_CASE(blueprint_plonk_exponentiation_one) { + test_exponentiation, 1>({1, 1}); + test_exponentiation({1, 1}); + test_exponentiation({1, 1}); } -BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) +BOOST_AUTO_TEST_CASE(blueprint_plonk_exponentiation_b1111) { + test_exponentiation, 4>({2379842, 0b1111}); + test_exponentiation({2379842, 0b1111}); + test_exponentiation({2379842, 0b1111}); +} + +BOOST_AUTO_TEST_CASE(blueprint_plonk_exponentiation_pallas) { + using field_type = nil::crypto3::algebra::curves::pallas::base_field_type; + exponentiation_tests(); +} -BOOST_AUTO_TEST_CASE(blueprint_plonk_exponentiation) { - 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 BlueprintFieldType::value_type base_value = nil::crypto3::algebra::random_element(); - typename BlueprintFieldType::value_type exponent_value = 654; - typename BlueprintFieldType::integral_type exponent_value_integral = typename BlueprintFieldType::integral_type(exponent_value.data); - std::vector public_input = {base_value, exponent_value}; - typename BlueprintFieldType::value_type expected_result = power(base_value, exponent_value_integral); - - test_exponentiation(public_input, expected_result); +BOOST_AUTO_TEST_CASE(blueprint_plonk_exponentiation_vesta) { + using field_type = nil::crypto3::algebra::curves::vesta::base_field_type; + exponentiation_tests(); } -BOOST_AUTO_TEST_CASE(blueprint_plonk_exponentiation_2) { - 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 BlueprintFieldType::value_type base_value = nil::crypto3::algebra::random_element(); - typename BlueprintFieldType::value_type exponent_value = (BlueprintFieldType::value_type::modulus - 1) / 2; - typename BlueprintFieldType::integral_type exponent_value_integral = typename BlueprintFieldType::integral_type(exponent_value.data); - std::vector public_input = {base_value, exponent_value}; - typename BlueprintFieldType::value_type expected_result = power(base_value, exponent_value_integral); - - test_exponentiation(public_input, expected_result); +BOOST_AUTO_TEST_CASE(blueprint_plonk_exponentiation_bls12) { + using field_type = nil::crypto3::algebra::fields::bls12_fr<381>; + exponentiation_tests(); } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/algebra/fields/plonk/field_operations.cpp b/test/algebra/fields/plonk/field_operations.cpp index 81e01fface..6ac929de45 100644 --- a/test/algebra/fields/plonk/field_operations.cpp +++ b/test/algebra/fields/plonk/field_operations.cpp @@ -26,10 +26,14 @@ #include +#include +#include +#include #include #include #include +#include #include #include @@ -44,12 +48,9 @@ using namespace nil; -BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) - -BOOST_AUTO_TEST_CASE(blueprint_plonk_multiplication) { - - using curve_type = crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::base_field_type; +template +void test_add(std::vector public_input){ + using BlueprintFieldType = FieldType; constexpr std::size_t WitnessColumns = 3; constexpr std::size_t PublicInputColumns = 1; constexpr std::size_t ConstantColumns = 0; @@ -59,35 +60,36 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_multiplication) { using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system; using hash_type = nil::crypto3::hashes::keccak_1600<256>; constexpr std::size_t Lambda = 40; + using AssignmentType = nil::blueprint::assignment; using var = crypto3::zk::snark::plonk_variable; - using component_type = blueprint::components::multiplication>; - - typename BlueprintFieldType::value_type x = 2; - typename BlueprintFieldType::value_type y = 12; - typename BlueprintFieldType::value_type expected_res = x * y; + using component_type = blueprint::components::addition>; typename component_type::input_type instance_input = { var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input)}; - std::vector public_input = {x, y}; - - auto result_check = [&expected_res](blueprint::assignment &assignment, - component_type::result_type &real_res) { - assert(expected_res == var_value(assignment, real_res.output)); + typename BlueprintFieldType::value_type expected_res = public_input[0] + public_input[1]; + + auto result_check = [&expected_res, public_input](AssignmentType &assignment, + typename component_type::result_type &real_res) { + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + std::cout << "add test: " << "\n"; + std::cout << "input : " << public_input[0].data << " " << public_input[1].data << "\n"; + std::cout << "expected: " << expected_res.data << "\n"; + std::cout << "real : " << var_value(assignment, real_res.output).data << "\n\n"; + #endif + assert(expected_res == var_value(assignment, real_res.output)); }; component_type component_instance({0, 1, 2},{},{}); - crypto3::test_component( - component_instance, public_input, result_check, instance_input); + nil::crypto3::test_component (component_instance, public_input, result_check, instance_input); } -BOOST_AUTO_TEST_CASE(blueprint_plonk_addition) { - - using curve_type = crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::base_field_type; +template +void test_sub(std::vector public_input){ + using BlueprintFieldType = FieldType; constexpr std::size_t WitnessColumns = 3; constexpr std::size_t PublicInputColumns = 1; constexpr std::size_t ConstantColumns = 0; @@ -97,36 +99,37 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_addition) { using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system; using hash_type = nil::crypto3::hashes::keccak_1600<256>; constexpr std::size_t Lambda = 40; + using AssignmentType = nil::blueprint::assignment; using var = crypto3::zk::snark::plonk_variable; - using component_type = blueprint::components::addition>; - - typename BlueprintFieldType::value_type x = 2; - typename BlueprintFieldType::value_type y = 22; - typename BlueprintFieldType::value_type expected_res = x + y; + using component_type = blueprint::components::subtraction>; typename component_type::input_type instance_input = { var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input)}; - std::vector public_input = {x, y}; - - auto result_check = [&expected_res](blueprint::assignment &assignment, - component_type::result_type &real_res) { - assert(expected_res == var_value(assignment, real_res.output)); + typename BlueprintFieldType::value_type expected_res = public_input[0] - public_input[1]; + + auto result_check = [&expected_res, public_input](AssignmentType &assignment, + typename component_type::result_type &real_res) { + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + std::cout << "sub test: " << "\n"; + std::cout << "input : " << public_input[0].data << " " << public_input[1].data << "\n"; + std::cout << "expected: " << expected_res.data << "\n"; + std::cout << "real : " << var_value(assignment, real_res.output).data << "\n\n"; + #endif + assert(expected_res == var_value(assignment, real_res.output)); }; component_type component_instance({0, 1, 2},{},{}); - crypto3::test_component( - component_instance, public_input, result_check, instance_input); + nil::crypto3::test_component (component_instance, public_input, result_check, instance_input); } -BOOST_AUTO_TEST_CASE(blueprint_plonk_division) { - - using curve_type = crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::base_field_type; - constexpr std::size_t WitnessColumns = 4; +template +void test_mul(std::vector public_input){ + using BlueprintFieldType = FieldType; + constexpr std::size_t WitnessColumns = 3; constexpr std::size_t PublicInputColumns = 1; constexpr std::size_t ConstantColumns = 0; constexpr std::size_t SelectorColumns = 1; @@ -135,110 +138,108 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_division) { using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system; using hash_type = nil::crypto3::hashes::keccak_1600<256>; constexpr std::size_t Lambda = 40; + using AssignmentType = nil::blueprint::assignment; using var = crypto3::zk::snark::plonk_variable; - using component_type = blueprint::components::division>; - - typename BlueprintFieldType::value_type x = 16; - typename BlueprintFieldType::value_type y = 2; - typename BlueprintFieldType::value_type expected_res = x / y; + using component_type = blueprint::components::multiplication>; typename component_type::input_type instance_input = { var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input)}; - std::vector public_input = {x, y}; - - auto result_check = [&expected_res](blueprint::assignment &assignment, - component_type::result_type &real_res) { - assert(expected_res == var_value(assignment, real_res.output)); + typename BlueprintFieldType::value_type expected_res = public_input[0] * public_input[1]; + + auto result_check = [&expected_res, public_input](AssignmentType &assignment, + typename component_type::result_type &real_res) { + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + std::cout << "mul test: " << "\n"; + std::cout << "input : " << public_input[0].data << " " << public_input[1].data << "\n"; + std::cout << "expected: " << expected_res.data << "\n"; + std::cout << "real : " << var_value(assignment, real_res.output).data << "\n\n"; + #endif + assert(expected_res == var_value(assignment, real_res.output)); }; - component_type component_instance({0, 1, 2, 3},{},{}); + component_type component_instance({0, 1, 2},{},{}); - crypto3::test_component( - component_instance, public_input, result_check, instance_input); + nil::crypto3::test_component (component_instance, public_input, result_check, instance_input); } -BOOST_AUTO_TEST_CASE(blueprint_plonk_subtraction) { - - using curve_type = crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::base_field_type; - constexpr std::size_t WitnessColumns = 3; +template +void test_mul_by_const(std::vector public_input, + typename FieldType::value_type y){ + using BlueprintFieldType = FieldType; + constexpr std::size_t WitnessColumns = 2; constexpr std::size_t PublicInputColumns = 1; - constexpr std::size_t ConstantColumns = 0; + constexpr std::size_t ConstantColumns = 1; constexpr std::size_t SelectorColumns = 1; using ArithmetizationParams = crypto3::zk::snark::plonk_arithmetization_params; using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system; using hash_type = nil::crypto3::hashes::keccak_1600<256>; constexpr std::size_t Lambda = 40; + using AssignmentType = nil::blueprint::assignment; using var = crypto3::zk::snark::plonk_variable; - using component_type = blueprint::components::subtraction>; - - typename BlueprintFieldType::value_type x = 0x56BC8334B5713726A_cppui256; - typename BlueprintFieldType::value_type y = 101; - typename BlueprintFieldType::value_type expected_res = x - y; + using component_type = blueprint::components::mul_by_constant; typename component_type::input_type instance_input = { - var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input)}; - - std::vector public_input = {x, y}; + var(0, 0, false, var::column_type::public_input), y}; - auto result_check = [&expected_res](blueprint::assignment &assignment, - component_type::result_type &real_res) { - assert(expected_res == var_value(assignment, real_res.output)); + typename BlueprintFieldType::value_type expected_res = public_input[0] * y; + + auto result_check = [&expected_res, public_input, y](AssignmentType &assignment, + typename component_type::result_type &real_res) { + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + std::cout << "mul_by_const test: " << "\n"; + std::cout << "input : " << public_input[0].data << " " << y.data << "\n"; + std::cout << "expected: " << expected_res.data << "\n"; + std::cout << "real : " << var_value(assignment, real_res.output).data << "\n\n"; + #endif + assert(expected_res == var_value(assignment, real_res.output)); }; - component_type component_instance({0, 1, 2},{},{}); + component_type component_instance({0, 1},{0},{}); - crypto3::test_component( - component_instance, public_input, result_check, instance_input); + nil::crypto3::test_component (component_instance, public_input, result_check, instance_input); } -BOOST_AUTO_TEST_CASE(blueprint_plonk_mul_by_constant) { - - using curve_type = crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::base_field_type; - constexpr std::size_t WitnessColumns = 2; +template +void test_div(std::vector public_input, + typename FieldType::value_type expected_res){ + using BlueprintFieldType = FieldType; + constexpr std::size_t WitnessColumns = 4; constexpr std::size_t PublicInputColumns = 1; - constexpr std::size_t ConstantColumns = 1; + constexpr std::size_t ConstantColumns = 0; constexpr std::size_t SelectorColumns = 1; using ArithmetizationParams = crypto3::zk::snark::plonk_arithmetization_params; using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system; using hash_type = nil::crypto3::hashes::keccak_1600<256>; constexpr std::size_t Lambda = 40; + using AssignmentType = nil::blueprint::assignment; using var = crypto3::zk::snark::plonk_variable; - using component_type = blueprint::components::mul_by_constant; - - typename BlueprintFieldType::value_type x = 2; - typename BlueprintFieldType::value_type y = 22; - typename BlueprintFieldType::value_type expected_res = x * y; + using component_type = blueprint::components::division>; typename component_type::input_type instance_input = { - var(0, 0, false, var::column_type::public_input), y}; - - std::vector public_input = {x}; + var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input)}; - auto result_check = [&expected_res](blueprint::assignment &assignment, - component_type::result_type &real_res) { - assert(expected_res == var_value(assignment, real_res.output)); + auto result_check = [&expected_res](AssignmentType &assignment, + typename component_type::result_type &real_res) { + assert(expected_res == var_value(assignment, real_res.output)); }; - component_type component_instance({0, 1},{0},{}); - crypto3::test_component( - component_instance, public_input, result_check, instance_input); -} + component_type component_instance({0, 1, 2, 3},{},{}); -BOOST_AUTO_TEST_CASE(blueprint_plonk_div_or_zero) { + nil::crypto3::test_component (component_instance, public_input, result_check, instance_input); +} - using curve_type = crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::base_field_type; +template +void test_div_or_zero(std::vector public_input){ + using BlueprintFieldType = FieldType; constexpr std::size_t WitnessColumns = 5; constexpr std::size_t PublicInputColumns = 1; constexpr std::size_t ConstantColumns = 0; @@ -248,28 +249,96 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_div_or_zero) { using ArithmetizationType = crypto3::zk::snark::plonk_constraint_system; using hash_type = nil::crypto3::hashes::keccak_1600<256>; constexpr std::size_t Lambda = 40; + using AssignmentType = nil::blueprint::assignment; using var = crypto3::zk::snark::plonk_variable; using component_type = blueprint::components::division_or_zero; - typename BlueprintFieldType::value_type x = 2; - typename BlueprintFieldType::value_type y = 0; - typename BlueprintFieldType::value_type expected_res = 0; - typename component_type::input_type instance_input = { var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input)}; - std::vector public_input = {x, y}; - - auto result_check = [&expected_res](blueprint::assignment &assignment, - component_type::result_type &real_res) { - assert(expected_res == var_value(assignment, real_res.output)); + typename FieldType::value_type expected_res; + if (public_input[1] != 0) { + expected_res = public_input[0] / public_input[1]; + } else { + expected_res = 0; + } + + auto result_check = [&expected_res, public_input](AssignmentType &assignment, + typename component_type::result_type &real_res) { + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + std::cout << "div_or_zero test: " << "\n"; + std::cout << "input : " << public_input[0].data << " " << public_input[1].data << "\n"; + std::cout << "expected: " << expected_res.data << "\n"; + std::cout << "real : " << var_value(assignment, real_res.output).data << "\n\n"; + #endif + assert(expected_res == var_value(assignment, real_res.output)); }; component_type component_instance({0, 1, 2, 3, 4},{},{}); - crypto3::test_component( - component_instance, public_input, result_check, instance_input); + + nil::crypto3::test_component (component_instance, public_input, result_check, instance_input); +} + +template +void test_5_components(int i, int j) { + typename FieldType::value_type x = i; + typename FieldType::value_type y = j; + + test_add({i, j}); + test_sub({i, j}); + test_mul({i, j}); + test_mul_by_const({i}, j); + test_div_or_zero({i, j}); +} + +template +void test_5_components_on_random_data() { + nil::crypto3::random::algebraic_engine generate_random; + boost::random::mt19937 seed_seq; + generate_random.seed(seed_seq); + + typename FieldType::value_type i = generate_random(); + typename FieldType::value_type j = generate_random(); + + test_add({i, j}); + test_sub({i, j}); + test_mul({i, j}); + test_mul_by_const({i}, j); + test_div_or_zero({i, j}); +} + +template +void field_operations_test() { + for (int i = -2; i < 3; i++){ + for (int j = -2; j < 3; j++){ + test_5_components(i, j); + } + } + + for (std::size_t i = 0; i < RandomTestsAmount; i++){ + test_5_components_on_random_data(); + } +} + +constexpr static const std::size_t random_tests_amount = 10; + +BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) + +BOOST_AUTO_TEST_CASE(blueprint_plonk_field_operations_test_vesta) { + using field_type = typename crypto3::algebra::curves::vesta::base_field_type; + field_operations_test(); +} + +BOOST_AUTO_TEST_CASE(blueprint_plonk_field_operations_test_pallas) { + using field_type = typename crypto3::algebra::curves::pallas::base_field_type; + field_operations_test(); +} + +BOOST_AUTO_TEST_CASE(blueprint_plonk_field_operations_test_bls12) { + using field_type = typename crypto3::algebra::fields::bls12_fr<381>; + field_operations_test(); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/algebra/fields/plonk/range_check.cpp b/test/algebra/fields/plonk/range_check.cpp index 26727bdaaa..c22040e3e4 100644 --- a/test/algebra/fields/plonk/range_check.cpp +++ b/test/algebra/fields/plonk/range_check.cpp @@ -26,13 +26,16 @@ #include +#include +#include +#include #include #include #include #include #include -#include +#include #include #include @@ -62,6 +65,10 @@ void test_range_check(std::vector publi typename component_type::input_type instance_input = {x}; + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + std::cout << "range_check_test_input: " << std::hex << public_input[0].data << "\n"; + #endif + auto result_check = [](AssignmentType &assignment, typename component_type::result_type &real_res) { }; @@ -69,34 +76,100 @@ void test_range_check(std::vector publi component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},{0},{0}); nil::crypto3::test_component (component_instance, public_input, result_check, instance_input); +} + +template +void test_range_check_specific_inputs(){ + test_range_check({0}); + test_range_check({1}); + test_range_check({35000}); + test_range_check({0xFFFFFFFFFFFFFFFF_cppui256}); +} +template +void test_range_check_random_inputs(){ + + nil::crypto3::random::algebraic_engine generate_random; + boost::random::mt19937 seed_seq; + generate_random.seed(seed_seq); + + for (std::size_t i = 0; i < RandomTestsAmount; i++){ + typename FieldType::value_type input = generate_random(); + typename FieldType::integral_type input_integral = typename FieldType::integral_type(input.data); + input_integral = input_integral & 0xFFFFFFFFFFFFFFFF_cppui255; + typename FieldType::value_type input_scalar = input_integral; + test_range_check({input_scalar}); + } } +constexpr static const std::size_t random_tests_amount = 10; + BOOST_AUTO_TEST_SUITE(blueprint_plonk_fields_range_check_test_suite) -BOOST_AUTO_TEST_CASE(blueprint_plonk_fields_range_check) { - using curve_type = nil::crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::base_field_type; +BOOST_AUTO_TEST_CASE(blueprint_plonk_fields_range_check_bls12) { + using field_type = nil::crypto3::algebra::fields::bls12_fr<381>; + test_range_check_specific_inputs(); + test_range_check_random_inputs(); +} + +BOOST_AUTO_TEST_CASE(blueprint_plonk_fields_range_check_pallas) { + using field_type = nil::crypto3::algebra::curves::pallas::scalar_field_type; + test_range_check_specific_inputs(); + test_range_check_random_inputs(); +} + +BOOST_AUTO_TEST_CASE(blueprint_plonk_fields_range_check_vesta) { + using field_type = nil::crypto3::algebra::curves::vesta::scalar_field_type; + test_range_check_specific_inputs(); + test_range_check_random_inputs(); +} - typename BlueprintFieldType::value_type x_value = 35000; +BOOST_AUTO_TEST_SUITE_END() - std::vector public_input = {x_value}; +template +void test_range_check_fail_specific_inputs(){ + test_range_check({-1}); + test_range_check({0x10000000000000000_cppui256}); + test_range_check({0x4000000000000000000000000000000000000000000000000000000000000000_cppui256}); +} - test_range_check(public_input); +template +void test_range_check_fail_random_inputs(){ + + nil::crypto3::random::algebraic_engine generate_random; + boost::random::mt19937 seed_seq; + generate_random.seed(seed_seq); + + for (std::size_t i = 0; i < RandomTestsAmount; i++){ + typename FieldType::value_type input = generate_random(); + if (input < 0x10000000000000000_cppui255) { + continue; + } + typename FieldType::integral_type input_integral = typename FieldType::integral_type(input.data); + typename FieldType::value_type input_scalar = input_integral; + test_range_check({input_scalar}); + } } -// TODO: we need to check that component fails on the wrong input. Now our tests only can check thet component works correctly with correct data -/* -BOOST_AUTO_TEST_CASE(blueprint_plonk_fields_range_check_false_result) { - using curve_type = nil::crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::base_field_type; +BOOST_AUTO_TEST_SUITE(blueprint_plonk_fields_range_check_fail_test_suite) +// TODO: we need to check that component fails on the wrong input. Don't have such feature yet - typename BlueprintFieldType::value_type x_value = 2; - x_value = x_value.pow(64); +BOOST_AUTO_TEST_CASE(blueprint_plonk_fields_range_check_fail_bls12) { + using field_type = nil::crypto3::algebra::fields::bls12_fr<381>; + test_range_check_fail_specific_inputs(); + test_range_check_fail_random_inputs(); +} - std::vector public_input = {x_value}; +BOOST_AUTO_TEST_CASE(blueprint_plonk_fields_range_check_fail_pallas) { + using field_type = nil::crypto3::algebra::curves::pallas::scalar_field_type; + test_range_check_fail_specific_inputs(); + test_range_check_fail_random_inputs(); +} - test_range_check(public_input); +BOOST_AUTO_TEST_CASE(blueprint_plonk_fields_range_check_fail_vesta) { + using field_type = nil::crypto3::algebra::curves::vesta::scalar_field_type; + test_range_check_fail_specific_inputs(); + test_range_check_fail_random_inputs(); } -*/ + BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/hashes/plonk/poseidon.cpp b/test/hashes/plonk/poseidon.cpp index 2f01af1fc3..2e7453e77b 100644 --- a/test/hashes/plonk/poseidon.cpp +++ b/test/hashes/plonk/poseidon.cpp @@ -28,6 +28,9 @@ #include +#include +#include +#include #include #include @@ -37,6 +40,11 @@ #include #include +#include +#include + +#include + #include "../../test_plonk_component.hpp" using namespace nil; @@ -66,11 +74,21 @@ void test_poseidon(std::vector public_i std::array input_state_var = {var(0, 0, false, var::column_type::public_input), var(0, 1, false, var::column_type::public_input), var(0, 2, false, var::column_type::public_input)}; typename component_type::input_type instance_input = {input_state_var}; + + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + for (std::uint32_t i = 0; i < component_type::state_size; i++){ + std::cout << "input[" << i << "] : " << public_input[i].data << "\n"; + } + #endif auto result_check = [&expected_res](AssignmentType &assignment, typename component_type::result_type &real_res) { for (std::uint32_t i = 0; i < component_type::state_size; i++){ + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + std::cout << "expected[" << i << "]: " << expected_res[i].data << "\n"; + std::cout << "real[" << i << "] : " << var_value(assignment, real_res.output_state[i]).data << "\n"; + #endif assert(expected_res[i] == var_value(assignment, real_res.output_state[i])); } }; @@ -81,14 +99,70 @@ void test_poseidon(std::vector public_i component_instance, public_input, result_check, instance_input); } +template +std::vector calculate_expected_poseidon(typename std::vector a) { + using poseidon_policy = crypto3::hashes::detail::base_poseidon_policy; + using poseidon_functions_t = crypto3::hashes::detail::poseidon_functions; + typename poseidon_functions_t::state_type poseidon_state({a[0], a[1], a[2]}); + poseidon_functions_t::permute(poseidon_state); + + std::vector expected = {poseidon_state[0], poseidon_state[1], poseidon_state[2]}; + return expected; +} + +template +void test_poseidon_specfic_data(){ + std::vector input = {0,1,1}; + test_poseidon(input, calculate_expected_poseidon(input)); + + input = {0,0,0}; + test_poseidon(input, calculate_expected_poseidon(input)); + + input = {1,2,3}; + test_poseidon(input, calculate_expected_poseidon(input)); + + input = {-1,-1,-1}; + test_poseidon(input, calculate_expected_poseidon(input)); + + typename FieldType::value_type threeFFF = 0x3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui256; + input = {threeFFF, threeFFF, threeFFF}; + test_poseidon(input, calculate_expected_poseidon(input)); +} + +template +void test_poseidon_random_data(){ + using generator_type = nil::crypto3::random::algebraic_engine; + generator_type g; + boost::random::mt19937 seed_seq; + g.seed(seed_seq); + std::vector input; + + for (std::size_t i = 0; i < RandomDataTestsAmount; i++) { + input = {g(), g(), g()}; + test_poseidon(input, calculate_expected_poseidon(input)); + } +} + +constexpr static const std::size_t random_data_tests_amount = 10; + BOOST_AUTO_TEST_SUITE(blueprint_plonk_poseidon_test_suite) -BOOST_AUTO_TEST_CASE(blueprint_plonk_poseidon_test_case0) { - test_poseidon( - {0, 1, 1}, - {0x294B71F8CF2C775369A3B0B8912E508790B0C64BDBE6A5C26F2C6B53767A47CB_cppui255, - 0x244E5FA0EE801AB3FCCAB47ED7F6EAB38126318F7BD2C414ADDBF62FCC30316A_cppui255, - 0x273C6EE50F9A2970162F5D4503596175C6D3FB4C0BF6C269BCD1DFEFB4F50D47_cppui255}); +BOOST_AUTO_TEST_CASE(blueprint_plonk_poseidon_test_vesta) { + using field_type = typename crypto3::algebra::curves::vesta::base_field_type; + test_poseidon_specfic_data(); + test_poseidon_random_data(); +} + +BOOST_AUTO_TEST_CASE(blueprint_plonk_poseidon_test_pallas) { + using field_type = typename crypto3::algebra::curves::pallas::base_field_type; + test_poseidon_specfic_data(); + test_poseidon_random_data(); } +// BOOST_AUTO_TEST_CASE(blueprint_plonk_poseidon_test_bls12) { +// using field_type = typename crypto3::algebra::fields::bls12_fr<381>; +// test_poseidon_specfic_data(); +// test_poseidon_random_data(); +// } + BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test/verifiers/kimchi/sponge/oracles.cpp b/test/verifiers/kimchi/sponge/oracles.cpp index 709fbc698f..4e3fd808b8 100644 --- a/test/verifiers/kimchi/sponge/oracles.cpp +++ b/test/verifiers/kimchi/sponge/oracles.cpp @@ -26,6 +26,7 @@ #include +#include #include #include #include @@ -66,8 +67,13 @@ void test_from_limbs(std::vector public typename component_type::input_type instance_input = {x, y}; - auto result_check = [&expected_res](AssignmentType &assignment, + auto result_check = [&expected_res, public_input](AssignmentType &assignment, typename component_type::result_type &real_res) { + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + std::cout << "from_limbs input: " << std::hex << public_input[0].data << " " << public_input[1].data << std::endl; + std::cout << "expected_res: " << std::hex << expected_res.data << std::endl; + std::cout << "real res: " << std::hex << var_value(assignment, real_res.result).data << "\n" << std::endl; + #endif assert(expected_res == var_value(assignment, real_res.result)); }; @@ -99,8 +105,14 @@ void test_to_limbs(std::vector public_i typename component_type::input_type instance_input = {x}; - auto result_check = [&expected_res](AssignmentType &assignment, + auto result_check = [&expected_res, public_input](AssignmentType &assignment, typename component_type::result_type &real_res) { + #ifdef BLUEPRINT_PLONK_PROFILING_ENABLED + std::cout << "to_limbs input: " << std::hex << public_input[0].data << std::endl; + std::cout << "expexted: " << std::hex << expected_res[3].data << " " << expected_res[2].data << " " << expected_res[1].data << " " << expected_res[0].data << std::endl; + std::cout << "real : " << std::hex << var_value(assignment, real_res.result[3]).data << " " << var_value(assignment, real_res.result[2]).data << " " << var_value(assignment, real_res.result[1]).data << " " << var_value(assignment, real_res.result[0]).data << "\n" < public_i BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite) -BOOST_AUTO_TEST_CASE(blueprint_plonk_from_limbs) { - using curve_type = nil::crypto3::algebra::curves::vesta; - using BlueprintFieldType = typename curve_type::scalar_field_type; +template +void test_from_limbs_specific_data(){ + test_from_limbs({0, 0}, 0); + test_from_limbs({5, 12}, 0xC0000000000000005_cppui255); + test_from_limbs({0, 0xFFFFFFFFFFFFFFFF_cppui255}, 0xFFFFFFFFFFFFFFFF0000000000000000_cppui255); + test_from_limbs({0xFFFFFFFFFFFFFFFF_cppui255, 0}, 0xFFFFFFFFFFFFFFFF_cppui255); + test_from_limbs({0xFFFFFFFFFFFFFFFF_cppui255, 0xFFFFFFFFFFFFFFFF_cppui255}, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255); +} - typename BlueprintFieldType::value_type x = 5; - typename BlueprintFieldType::value_type y = 12; - typename BlueprintFieldType::value_type expected_res = 0xC0000000000000005_cppui256; +BOOST_AUTO_TEST_CASE(blueprint_plonk_from_limbs_vesta) { + using field_type = nil::crypto3::algebra::curves::vesta::scalar_field_type; + test_from_limbs_specific_data(); +} - std::vector public_input = {x, y}; +BOOST_AUTO_TEST_CASE(blueprint_plonk_from_limbs_bls12) { + using field_type = nil::crypto3::algebra::fields::bls12_fr<381>; + test_from_limbs_specific_data(); +} - test_from_limbs(public_input, expected_res); +BOOST_AUTO_TEST_CASE(blueprint_plonk_from_limbs_pallas) { + using field_type = nil::crypto3::algebra::curves::pallas::scalar_field_type; + test_from_limbs_specific_data(); } -BOOST_AUTO_TEST_CASE(blueprint_plonk_to_limbs_1) { - using curve_type = nil::crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::scalar_field_type; +template +void test_to_limbs_specific_data(){ + test_to_limbs({0x1D42ED837696F2A777E7C1FF0436D46E96878B624ECDE039732E37AFCD409C88_cppui256}, + {0x732E37AFCD409C88_cppui256, 0x96878B624ECDE039_cppui256, 0x77E7C1FF0436D46E_cppui256, 0x1D42ED837696F2A7_cppui256}); + + test_to_limbs({0xE826DABA538B6DF0000000000000000FB812F513D0FCC04106CB4BD3F32FAD3_cppui256}, + {0x106CB4BD3F32FAD3_cppui256, 0xFB812F513D0FCC04_cppui256, 0x0_cppui256, 0xE826DABA538B6DF_cppui256}); - typename BlueprintFieldType::value_type input = 0x1D42ED837696F2A777E7C1FF0436D46E96878B624ECDE039732E37AFCD409C88_cppui256; - typename BlueprintFieldType::value_type x0 = 0x732E37AFCD409C88_cppui256; - typename BlueprintFieldType::value_type x1 = 0x96878B624ECDE039_cppui256; - typename BlueprintFieldType::value_type x2 = 0x77E7C1FF0436D46E_cppui256; - typename BlueprintFieldType::value_type x3 = 0x1D42ED837696F2A7_cppui256; - - std::vector expected_res = {x0, x1, x2, x3}; - std::vector public_input = {input}; + test_to_limbs({0x3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui256}, + {0xFFFFFFFFFFFFFFFF_cppui256, 0xFFFFFFFFFFFFFFFF_cppui256, 0xFFFFFFFFFFFFFFFF_cppui256, 0x3FFFFFFFFFFFFFFF_cppui256}); - test_to_limbs(public_input, expected_res); } -BOOST_AUTO_TEST_CASE(blueprint_plonk_to_limbs_2) { - using curve_type = nil::crypto3::algebra::curves::pallas; - using BlueprintFieldType = typename curve_type::scalar_field_type; +BOOST_AUTO_TEST_CASE(blueprint_plonk_to_limbs_vesta) { + using field_type = nil::crypto3::algebra::curves::vesta::scalar_field_type; + test_to_limbs_specific_data(); - typename BlueprintFieldType::value_type input = 0xE826DABA538B6DF0000000000000000FB812F513D0FCC04106CB4BD3F32FAD3_cppui256; - typename BlueprintFieldType::value_type x0 = 0x106CB4BD3F32FAD3_cppui256; - typename BlueprintFieldType::value_type x1 = 0xFB812F513D0FCC04_cppui256; - typename BlueprintFieldType::value_type x2 = 0x0_cppui256; - typename BlueprintFieldType::value_type x3 = 0xE826DABA538B6DF_cppui256; + test_to_limbs({0x40000000000000000000000000000000224698fc094cf91b992d30ed00000000_cppui255}, //-1 vesta + {0x992d30ed00000000_cppui256, 0x224698fc094cf91b_cppui256, 0x0000000000000000_cppui256, 0x4000000000000000_cppui256}); +} + +BOOST_AUTO_TEST_CASE(blueprint_plonk_to_limbs_pallas) { + using field_type = nil::crypto3::algebra::curves::pallas::scalar_field_type; + test_to_limbs_specific_data(); + + test_to_limbs({0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000000_cppui256}, //-1 pallas + {0x8c46eb2100000000_cppui256, 0x224698fc0994a8dd_cppui256, 0x0000000000000000_cppui256, 0x4000000000000000_cppui256}); +} - std::vector expected_res = {x0, x1, x2, x3}; - std::vector public_input = {input}; +BOOST_AUTO_TEST_CASE(blueprint_plonk_to_limbs_bls12) { + using field_type = nil::crypto3::algebra::fields::bls12_fr<381>; + test_to_limbs_specific_data(); - test_to_limbs(public_input, expected_res); + test_to_limbs({0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000_cppui256}, //-1 bls12<381> + {0xffffffff00000000_cppui256, 0x53bda402fffe5bfe_cppui256, 0x3339d80809a1d805_cppui256, 0x73eda753299d7d48_cppui256}); } BOOST_AUTO_TEST_SUITE_END()