Skip to content

Commit

Permalink
non native components test rewrite, new test cases added #107
Browse files Browse the repository at this point in the history
  • Loading branch information
CblPOK-git committed Jan 28, 2023
1 parent c63077b commit 5657d44
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 18 deletions.
66 changes: 60 additions & 6 deletions test/algebra/fields/plonk/non_native/bit_decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include <nil/crypto3/algebra/curves/ed25519.hpp>
#include <nil/crypto3/algebra/fields/arithmetic_params/ed25519.hpp>
#include <nil/crypto3/algebra/random_element.hpp>
#include <nil/crypto3/random/algebraic_engine.hpp>

#include <nil/crypto3/hash/keccak.hpp>

Expand All @@ -44,7 +44,8 @@
using namespace nil;

template <typename BlueprintFieldType>
void test_field_add(std::vector<typename BlueprintFieldType::value_type> public_input){
void test_bit_decomposition(std::vector<typename BlueprintFieldType::value_type> public_input,
std::vector<typename BlueprintFieldType::value_type> expected_res){

constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
Expand All @@ -64,8 +65,24 @@ void test_field_add(std::vector<typename BlueprintFieldType::value_type> public_

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

auto result_check = [](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 << "input: " << std::hex << public_input[0].data << "\n";
for (std::size_t i = 0; i < expected_res.size(); i++){
std::cout << expected_res[i].data;
}
std::cout << std::endl;

for (std::size_t i = 0; i < real_res.output.size(); i++){
std::cout << var_value(assignment, real_res.output[i]).data;
}
std::cout << std::endl;
#endif

for(std::size_t i = 0; i < real_res.output.size(); i++) {
assert(expected_res[i] == var_value(assignment, real_res.output[i]));
}
};

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{});
Expand All @@ -76,9 +93,46 @@ void test_field_add(std::vector<typename BlueprintFieldType::value_type> public_

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

BOOST_AUTO_TEST_CASE(blueprint_non_native_bit_decomposition_test0) {
test_field_add<typename crypto3::algebra::curves::pallas::base_field_type>(
{45524});
constexpr static const std::size_t random_tests_amount = 10;

template<typename FieldType>
void calculate_expected_and_test_bit_decomposition(typename FieldType::value_type input) {

typename FieldType::integral_type input_integral = 1;
typename FieldType::integral_type max253 = (input_integral << 253) - 1;

input_integral = typename FieldType::integral_type(input.data);
input_integral = input_integral % max253;

std::vector <typename FieldType::value_type> expected_res = std::vector <typename FieldType::value_type>(253);
for (std::size_t i = 0; i < 253; i++) {
if (((input_integral >> i) & 0b1) == 1) {
expected_res[252 - i] = 1;
}
else {
expected_res[252 - i] = 0;
}
}
input = typename FieldType::value_type(input_integral);
test_bit_decomposition<FieldType>({input}, expected_res);
}

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

calculate_expected_and_test_bit_decomposition<field_type>(1);
calculate_expected_and_test_bit_decomposition<field_type>(0);
calculate_expected_and_test_bit_decomposition<field_type>(0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui255);
calculate_expected_and_test_bit_decomposition<field_type>(45524);

using generator_type = nil::crypto3::random::algebraic_engine<field_type>;
generator_type rand;
boost::random::mt19937 seed_seq;
rand.seed(seed_seq);

for (std::size_t j = 0; j < random_tests_amount; j++) {
calculate_expected_and_test_bit_decomposition<field_type>(rand());
}
}

BOOST_AUTO_TEST_SUITE_END()
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ std::vector<typename FieldType::value_type> create_public_input(std::array<typen
public_input.push_back(b[i]);
}
return public_input;
}

template <typename FieldType, typename NonNativeFieldType>
std::vector<typename FieldType::value_type> create_public_input_1_value(std::array<typename FieldType::value_type, 4> b) {
std::vector<typename FieldType::value_type> public_input;
for (std::size_t i = 0; i < b.size(); i++){
public_input.push_back(b[i]);
}
return public_input;
}
53 changes: 52 additions & 1 deletion test/algebra/fields/plonk/non_native/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
#include <nil/blueprint/components/algebra/fields/plonk/non_native/range.hpp>

#include <nil/crypto3/random/algebraic_engine.hpp>
#include <../test/algebra/fields/plonk/non_native/chop_and_glue_non_native.hpp>

#include "../../../../test_plonk_component.hpp"

using namespace nil;
Expand Down Expand Up @@ -67,8 +70,15 @@ void test_field_range(std::vector<typename BlueprintFieldType::value_type> publi

typename component_type::input_type instance_input = {input_var};

auto result_check = [](AssignmentType &assignment,
auto result_check = [public_input](AssignmentType &assignment,
typename component_type::result_type &real_res) {
#ifdef BLUEPRINT_PLONK_PROFILING_ENABLED
std::cout << "________________________________________________________________________\ninput: " << std::hex << std::endl;
for (int i = 0; i < 4; i++){
std::cout << public_input[3-i].data << " ";
}
std::cout << std::endl;
#endif
};

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{});
Expand All @@ -80,8 +90,49 @@ void test_field_range(std::vector<typename BlueprintFieldType::value_type> publi
BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test0) {
using non_native_field_type = typename crypto3::algebra::fields::curve25519_base_field;
using field_type = crypto3::algebra::curves::pallas::base_field_type;

test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
{455245345345345, 523553453454343, 68753453534534689, 54355345344544});

test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
create_public_input_1_value<field_type, non_native_field_type>(
chop_non_native<field_type, non_native_field_type>(1)
));
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
create_public_input_1_value<field_type, non_native_field_type>(
chop_non_native<field_type, non_native_field_type>(0)
));
test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
create_public_input_1_value<field_type, non_native_field_type>(
chop_non_native<field_type, non_native_field_type>(-1)
));

nil::crypto3::random::algebraic_engine<non_native_field_type> rand;
boost::random::mt19937 seed_seq;
rand.seed(seed_seq);

for (std::size_t i = 0; i < 10; i++) {
test_field_range<field_type>(
create_public_input_1_value<field_type, non_native_field_type>(
chop_non_native<field_type, non_native_field_type>(rand())
));
}
}

BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test_must_fail) {
using non_native_field_type = typename crypto3::algebra::fields::curve25519_base_field;
using field_type = crypto3::algebra::curves::pallas::base_field_type;

test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>( //ed25519 modulus
{0x3ffffffffffffffed_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255}
);

test_field_range<typename crypto3::algebra::curves::pallas::base_field_type>(
{0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x3ffffffffffffffff_cppui255, 0x1ffffffffffffff_cppui255}
);

}

BOOST_AUTO_TEST_SUITE_END()
52 changes: 45 additions & 7 deletions test/algebra/fields/plonk/non_native/reduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
using namespace nil;

template <typename BlueprintFieldType>
void test_field_reduction(std::vector<typename BlueprintFieldType::value_type> public_input){
void test_reduction(std::vector<typename BlueprintFieldType::value_type> public_input,
typename BlueprintFieldType::value_type expected_res){

// using curve_type = nil::crypto3::algebra::curves::pallas;
constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 0;
Expand All @@ -70,8 +70,17 @@ void test_field_reduction(std::vector<typename BlueprintFieldType::value_type> p

typename component_type::input_type instance_input = {input_state_var};

auto result_check = [](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 << std::hex << "___________________________________________________________________________________________________\ninput: ";
for (std::size_t i = 0; i < 8; i++) {
std::cout << public_input[7-i].data << " ";
}
std::cout << "\nexpected: " << expected_res.data << "\n";
std::cout << "real : " << var_value(assignment, real_res.output).data << std::endl;
#endif
assert(expected_res == var_value(assignment, real_res.output));
};

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{});
Expand All @@ -80,16 +89,45 @@ void test_field_reduction(std::vector<typename BlueprintFieldType::value_type> p
component_instance, public_input, result_check, instance_input);
}

constexpr static const crypto3::algebra::curves::ed25519::scalar_field_type::extended_integral_type ed25519_scalar_modulus = 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed_cppui512;
constexpr static const crypto3::algebra::curves::ed25519::scalar_field_type::extended_integral_type one = 1;
constexpr static const crypto3::algebra::curves::ed25519::scalar_field_type::extended_integral_type max512 = (one<<512)-1;

template<typename FieldType>
std::vector<typename FieldType::value_type> vector_from_extended_integral(typename crypto3::algebra::curves::ed25519::scalar_field_type::extended_integral_type input) {
std::vector<typename FieldType::value_type> pub_inp;
for (std::size_t i = 0; i < 8; i++) {
typename crypto3::algebra::curves::ed25519::scalar_field_type::extended_integral_type mask = 0xffffffffffffffff_cppui512;
typename FieldType::value_type current = typename FieldType::value_type((input >> (64*i)) & mask);
pub_inp.push_back(current);
}
return pub_inp;
}

template<typename FieldType>
void test_reduction_input_expended_integral_calculate_expected(typename crypto3::algebra::curves::ed25519::scalar_field_type::extended_integral_type input) {
assert(input <= max512);
test_reduction<FieldType>(vector_from_extended_integral<FieldType>(input), typename FieldType::value_type(input % ed25519_scalar_modulus));
}

BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

BOOST_AUTO_TEST_CASE(blueprint_variable_base_decomposition_edward25519) {

using curve_type = nil::crypto3::algebra::curves::pallas;
using BlueprintFieldType = typename curve_type::scalar_field_type;

std::vector<typename BlueprintFieldType::value_type> public_input = {0, 0, 0, 0, 0, 0, 0, 1};

test_field_reduction<BlueprintFieldType>(public_input);
using extended_integral = typename crypto3::algebra::curves::ed25519::scalar_field_type::extended_integral_type;

test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(max512);
test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(0);
test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(1);
test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui512);
test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(ed25519_scalar_modulus);
test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(ed25519_scalar_modulus * 2);
test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(ed25519_scalar_modulus + 1);
test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(ed25519_scalar_modulus - 1);
test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(ed25519_scalar_modulus << 256);
test_reduction_input_expended_integral_calculate_expected<BlueprintFieldType>(max512 - 0x399411b7c309a3dceec73d217f5be65d00e1ba768859347a40611e3449c0f00_cppui512);
}

BOOST_AUTO_TEST_SUITE_END()
67 changes: 63 additions & 4 deletions test/non_native/plonk/scalar_non_native_range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include <nil/crypto3/algebra/curves/ed25519.hpp>
#include <nil/crypto3/algebra/fields/arithmetic_params/ed25519.hpp>
#include <nil/crypto3/algebra/random_element.hpp>
#include <nil/crypto3/random/algebraic_engine.hpp>

#include <nil/crypto3/hash/keccak.hpp>

Expand All @@ -44,7 +44,7 @@
using namespace nil;

template <typename BlueprintFieldType>
void test_scalar_non_native_range_add(std::vector<typename BlueprintFieldType::value_type> public_input){
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;
Expand All @@ -65,8 +65,11 @@ void test_scalar_non_native_range_add(std::vector<typename BlueprintFieldType::v

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

auto result_check = [](AssignmentType &assignment,
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},{},{});
Expand All @@ -75,11 +78,67 @@ void test_scalar_non_native_range_add(std::vector<typename BlueprintFieldType::v
component_instance, public_input, result_check, instance_input);
}

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_add<typename crypto3::algebra::curves::pallas::base_field_type>(
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;

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>({1});

test_scalar_non_native_range<field_type>({0});

nil::crypto3::random::algebraic_engine<field_type> rand;
boost::random::mt19937 seed_seq;
rand.seed(seed_seq);


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

for (std::size_t i = 0; i < random_tests_amount; i++) {
r = rand();
r_integral = typename field_type::integral_type(r.data);
r_integral = r_integral % ed25519_scalar_modulus;
r = typename field_type::value_type(r_integral);
test_scalar_non_native_range<field_type>({r});
}
}

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

nil::crypto3::random::algebraic_engine<field_type> rand;
boost::random::mt19937 seed_seq;
rand.seed(seed_seq);

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<field_type>({typename field_type::value_type(ed25519_scalar_modulus + overage)});
}
test_scalar_non_native_range<field_type>({-1});
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 5657d44

Please sign in to comment.