Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
Use policy's chop_non_native for tests instead of hardcoded
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Sep 20, 2023
1 parent 1621171 commit 8e3ae3c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 48 deletions.
18 changes: 12 additions & 6 deletions test/algebra/fields/plonk/non_native/addition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
#include <nil/blueprint/components/algebra/fields/plonk/non_native/addition.hpp>

#include <../test/algebra/fields/plonk/non_native/chop_and_glue_non_native.hpp>
#include <../test/algebra/fields/plonk/non_native/glue_non_native.hpp>

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

Expand Down Expand Up @@ -95,10 +95,16 @@ void test_field_add(std::vector<typename BlueprintFieldType::value_type> public_

template <typename FieldType, typename NonNativeFieldType>
void test_field_add_useable(typename NonNativeFieldType::value_type a, typename NonNativeFieldType::value_type b){
using chunked_non_native_type = std::array<typename FieldType::value_type, 4>;
chunked_non_native_type first = chop_non_native<FieldType, NonNativeFieldType>(a);
chunked_non_native_type second = chop_non_native<FieldType, NonNativeFieldType>(b);
chunked_non_native_type expected_result = chop_non_native<FieldType, NonNativeFieldType>(a + b);
using non_native_policy_type =
blueprint::detail::basic_non_native_policy_field_type<
FieldType,
NonNativeFieldType
>;
using chunked_non_native_type = typename non_native_policy_type::chopped_value_type;

chunked_non_native_type first = non_native_policy_type::chop_non_native(a);
chunked_non_native_type second = non_native_policy_type::chop_non_native(b);
chunked_non_native_type expected_result = non_native_policy_type::chop_non_native(a + b);
std::vector<typename FieldType::value_type> public_input = create_public_input<FieldType, NonNativeFieldType>(first, second);
test_field_add<FieldType, NonNativeFieldType>(public_input, expected_result);
}
Expand Down Expand Up @@ -146,4 +152,4 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_addition_pallas) {
test_field_add_all_cases<field_type, non_native_field_type>();
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
template<typename FieldType, typename NonNativeFieldType>
std::array<typename FieldType::value_type, 4> chop_non_native(typename NonNativeFieldType::value_type input) {
typename NonNativeFieldType::integral_type input_integral = typename NonNativeFieldType::integral_type(input.data);

std::array<typename FieldType::value_type, 4> output;

typename NonNativeFieldType::integral_type base = 1;
typename NonNativeFieldType::integral_type mask = (base << 66) - 1;

output[0] = input_integral & mask;
output[1] = (input_integral >> 66) & mask;
output[2] = (input_integral >> 132) & mask;
output[3] = (input_integral >> 198) & mask;

return output;
}

template<typename FieldType, typename NonNativeFieldType>
typename NonNativeFieldType::value_type glue_non_native(std::array<typename FieldType::value_type, 4> input) {
typename NonNativeFieldType::integral_type base = 1;
Expand Down Expand Up @@ -56,4 +39,4 @@ std::vector<typename FieldType::value_type>
public_input.push_back(b[i]);
}
return public_input;
}
}
17 changes: 11 additions & 6 deletions test/algebra/fields/plonk/non_native/multiplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <nil/blueprint/components/algebra/fields/plonk/non_native/multiplication.hpp>

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

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

Expand Down Expand Up @@ -137,10 +137,15 @@ BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

template<typename FieldType, typename NonNativeFieldType>
void test_field_mul_useable(typename NonNativeFieldType::value_type a, typename NonNativeFieldType::value_type b) {
using chunked_non_native_type = std::array<typename FieldType::value_type, 4>;
chunked_non_native_type first = chop_non_native<FieldType, NonNativeFieldType>(a);
chunked_non_native_type second = chop_non_native<FieldType, NonNativeFieldType>(b);
chunked_non_native_type expected_result = chop_non_native<FieldType, NonNativeFieldType>(a * b);
using non_native_policy_type =
blueprint::detail::basic_non_native_policy_field_type<
FieldType,
NonNativeFieldType
>;
using chunked_non_native_type = typename non_native_policy_type::chopped_value_type;
chunked_non_native_type first = non_native_policy_type::chop_non_native(a);
chunked_non_native_type second = non_native_policy_type::chop_non_native(b);
chunked_non_native_type expected_result = non_native_policy_type::chop_non_native(a * b);
std::vector<typename FieldType::value_type> public_input =
create_public_input<FieldType, NonNativeFieldType>(first, second);
test_field_mul<FieldType, NonNativeFieldType>(public_input, expected_result);
Expand Down Expand Up @@ -203,4 +208,4 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_multiplication_pallas) {
test_field_mul_all_cases<field_type, non_native_field_type>();
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
18 changes: 12 additions & 6 deletions test/algebra/fields/plonk/non_native/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#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/algebra/fields/plonk/non_native/glue_non_native.hpp>

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

Expand Down Expand Up @@ -98,21 +98,27 @@ 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;
using non_native_policy_type =
blueprint::detail::basic_non_native_policy_field_type<
field_type,
non_native_field_type
>;
using chunked_non_native_type = typename non_native_policy_type::chopped_value_type;

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

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)
non_native_policy_type::chop_non_native(1)
), true);
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)
non_native_policy_type::chop_non_native(0)
), true);
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)
non_native_policy_type::chop_non_native(-1)
), true);

nil::crypto3::random::algebraic_engine<non_native_field_type> rand;
Expand All @@ -122,7 +128,7 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test0) {
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())
non_native_policy_type::chop_non_native(rand())
), true);
}
}
Expand All @@ -141,4 +147,4 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_range_test_must_fail) {

}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
17 changes: 11 additions & 6 deletions test/algebra/fields/plonk/non_native/subtraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
#include <nil/blueprint/components/algebra/fields/plonk/non_native/subtraction.hpp>

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

#include "../../../../test_plonk_component.hpp"
Expand Down Expand Up @@ -125,10 +125,15 @@ BOOST_AUTO_TEST_SUITE(blueprint_plonk_test_suite)

template <typename FieldType, typename NonNativeFieldType>
void test_field_sub_useable(typename NonNativeFieldType::value_type a, typename NonNativeFieldType::value_type b){
using chunked_non_native_type = std::array<typename FieldType::value_type, 4>;
chunked_non_native_type first = chop_non_native<FieldType, NonNativeFieldType>(a);
chunked_non_native_type second = chop_non_native<FieldType, NonNativeFieldType>(b);
chunked_non_native_type expected_result = chop_non_native<FieldType, NonNativeFieldType>(a - b);
using non_native_policy_type =
blueprint::detail::basic_non_native_policy_field_type<
FieldType,
NonNativeFieldType
>;
using chunked_non_native_type = typename non_native_policy_type::chopped_value_type;
chunked_non_native_type first = non_native_policy_type::chop_non_native(a);
chunked_non_native_type second = non_native_policy_type::chop_non_native(b);
chunked_non_native_type expected_result = non_native_policy_type::chop_non_native(a - b);
std::vector<typename FieldType::value_type> public_input = create_public_input<FieldType, NonNativeFieldType>(first, second);
test_field_sub<FieldType, NonNativeFieldType>(public_input, expected_result);
}
Expand Down Expand Up @@ -182,4 +187,4 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_subtraction_pallas) {
test_field_sub_all_cases<field_type, non_native_field_type>();
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
22 changes: 16 additions & 6 deletions test/non_native/plonk/bool_scalar_multiplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <nil/crypto3/algebra/curves/ed25519.hpp>
#include <nil/crypto3/algebra/fields/arithmetic_params/ed25519.hpp>
#include <nil/crypto3/random/algebraic_engine.hpp>
#include <../test/algebra/fields/plonk/non_native/chop_and_glue_non_native.hpp>
#include <../test/algebra/fields/plonk/non_native/glue_non_native.hpp>

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

Expand Down Expand Up @@ -157,14 +157,24 @@ void test_bool_scalar_multiplication(const std::vector<typename BlueprintFieldTy

template<typename FieldType, typename NonNativeCurveType>
void test_bool_scalar_multiplication_usable(
typename NonNativeCurveType::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type point,
typename NonNativeCurveType::template g1_type<
crypto3::algebra::curves::coordinates::affine
>::value_type point,
typename FieldType::value_type scalar_bool,
const bool expected_to_pass) {
const bool expected_to_pass
) {

using non_native_policy_type =
blueprint::detail::basic_non_native_policy_field_type<
FieldType,
typename NonNativeCurveType::base_field_type
>;

std::vector<typename FieldType::value_type> public_input =
create_public_input<FieldType, typename NonNativeCurveType::base_field_type>(
chop_non_native<FieldType, typename NonNativeCurveType::base_field_type>(point.X),
chop_non_native<FieldType, typename NonNativeCurveType::base_field_type>(point.Y));
non_native_policy_type::chop_non_native(point.X),
non_native_policy_type::chop_non_native(point.Y)
);

std::vector<typename FieldType::value_type> expected_res;
if (scalar_bool == 1) {
Expand Down Expand Up @@ -220,4 +230,4 @@ BOOST_AUTO_TEST_CASE(blueprint_non_native_bool_scalar_mul_must_fail) {
test_bool_scalar_multiplication_usable<field_type, non_native_curve_type>(rand(), -1, false);
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 8e3ae3c

Please sign in to comment.