Skip to content

Commit

Permalink
zero representation in unified addition component changed from (0,0) …
Browse files Browse the repository at this point in the history
…to (0,1)
  • Loading branch information
CblPOK-git committed Sep 13, 2023
1 parent 5b549b6 commit 1c5f11c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace nil {
static_assert(std::is_same<typename CurveType::base_field_type, BlueprintFieldType>::value);

constexpr static const std::uint32_t WitnessAmount = 11;

using component_type = plonk_component<BlueprintFieldType, ArithmetizationParams, WitnessAmount, 0, 0>;

public:
Expand Down Expand Up @@ -138,7 +138,7 @@ namespace nil {
assignment.witness(component.W(2), j) = Q.X;
assignment.witness(component.W(3), j) = Q.Y;
typename CurveType::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type zero = {
0, 0};
0, 1};
if (P.X == zero.X && P.Y == zero.Y) {
assignment.witness(component.W(4), j) = Q.X;
assignment.witness(component.W(5), j) = Q.Y;
Expand All @@ -149,7 +149,7 @@ namespace nil {
} else {
if (Q.X == P.X && Q.Y == -P.Y) {
assignment.witness(component.W(4), j) = 0;
assignment.witness(component.W(5), j) = 0;
assignment.witness(component.W(5), j) = 1;
} else {
assignment.witness(component.W(4), j) = (P + Q).X;
assignment.witness(component.W(5), j) = (P + Q).Y;
Expand Down Expand Up @@ -192,7 +192,7 @@ namespace nil {
return typename plonk_native_unified_addition<BlueprintFieldType, ArithmetizationParams, CurveType, 11>::result_type(
component, start_row_index);
}

template<typename BlueprintFieldType, typename ArithmetizationParams, typename CurveType>
void generate_gates(
const plonk_native_unified_addition<BlueprintFieldType, ArithmetizationParams, CurveType, 11> &component,
Expand Down Expand Up @@ -250,7 +250,7 @@ namespace nil {
auto constraint_12 = bp.add_constraint(
(1 - (var(component.W(2), 0) - var(component.W(0), 0)) * var(component.W(8), 0) -
(var(component.W(3), 0) + var(component.W(1), 0)) * var(component.W(9), 0)) *
var(component.W(5), 0));
(1 - var(component.W(5), 0)));

bp.add_gate(first_selector_index,
{constraint_1, constraint_2, constraint_3, constraint_4, constraint_5, constraint_6,
Expand Down
38 changes: 12 additions & 26 deletions test/algebra/curves/plonk/unified_addition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ using namespace nil;
template <typename CurveType>
void test_unified_addition(std::vector<typename CurveType::base_field_type::value_type> public_input,
typename CurveType::template g1_type<crypto3::algebra::curves::coordinates::affine>::value_type expected_res){

using curve_type = CurveType;
using BlueprintFieldType = typename curve_type::base_field_type;

Expand All @@ -72,15 +72,16 @@ void test_unified_addition(std::vector<typename CurveType::base_field_type::valu
{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), var(0, 3, false, var::column_type::public_input)}};

auto result_check = [&expected_res, public_input](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 << "unified_addition test: " << "\n";
std::cout << "input : " << public_input[0].data << " " << public_input[1].data << "\n";
std::cout << "input : " << public_input[2].data << " " << public_input[3].data << "\n";
std::cout << "expected: " << expected_res.X.data << " " << expected_res.Y.data << "\n";
std::cout << "real : " << var_value(assignment, real_res.X).data << " " << var_value(assignment, real_res.Y).data << "\n\n";
#endif

if((expected_res.X != var_value(assignment, real_res.X)) || (expected_res.Y != var_value(assignment, real_res.Y))) {
std::cout << "unified_addition failed, expected result differs form circuit output: " << "\n";
std::cout << "input : " << public_input[0].data << " " << public_input[1].data << "\n";
std::cout << "input : " << public_input[2].data << " " << public_input[3].data << "\n";
std::cout << "expected: " << expected_res.X.data << " " << expected_res.Y.data << "\n";
std::cout << "real : " << var_value(assignment, real_res.X).data << " " << var_value(assignment, real_res.Y).data << "\n\n";
}
assert(expected_res.X == var_value(assignment, real_res.X));
assert(expected_res.Y == var_value(assignment, real_res.Y));
};
Expand All @@ -97,8 +98,7 @@ void test_unified_addition_with_zeroes() {
boost::random::mt19937 seed_seq;
generate_random_point.seed(seed_seq);

typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type zero_algebraic = {0, 1};
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type zero_circuits = {0, 0};
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type zero_circuits = {0, 1};
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type P = generate_random_point();
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type Q = -P;

Expand Down Expand Up @@ -146,28 +146,14 @@ void test_unified_addition_random_data() {

typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type P = generate_random_point();
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type Q = generate_random_point();
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type zero = {0, 0};
typename CurveType::template g1_type<nil::crypto3::algebra::curves::coordinates::affine>::value_type expected_res;

std::vector<typename CurveType::base_field_type::value_type> 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 == zero.X && P.Y == zero.Y) {
expected_res = Q;
} else {
if (P.X == Q.X && P.Y == -Q.Y) {
expected_res = {0, 0};
} else {
expected_res = P + Q;
}
}
}
expected_res = P + Q;

public_input = {P.X, P.Y, Q.X, Q.Y};
test_unified_addition<CurveType>(public_input, expected_res);
Expand Down

0 comments on commit 1c5f11c

Please sign in to comment.