Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change unified addition zero #209

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions include/nil/blueprint/basic_non_native_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,24 @@ namespace nil {
chopped_value_type result;
for (std::size_t i = 0; i < ratio; i++) {
result[i] = get_i_th_chunk(input, i);

}

return result;
}

static non_native_field_type::value_type glue_non_native(chopped_value_type input) {
non_native_field_type::value_type result;
native_field_type::integral_type integral_input;
result = non_native_field_type::value_type(native_field_type::integral_type(input[0].data));
for (std::size_t i = 1; i < ratio; i++) {
std::size_t shift = 0;
for (std::size_t j = 0; j < i; j++) {
shift += chunk_sizes[j];
}
result += non_native_field_type::value_type(native_field_type::integral_type(input[i].data) << shift);
}
return result;
}

};

/*
Expand Down Expand Up @@ -132,12 +144,24 @@ namespace nil {
chopped_value_type result;
for (std::size_t i = 0; i < ratio; i++) {
result[i] = get_i_th_chunk(input, i);

}

return result;
}

static non_native_field_type::value_type glue_non_native(chopped_value_type input) {
non_native_field_type::value_type result;
native_field_type::integral_type integral_input;
result = non_native_field_type::value_type(native_field_type::integral_type(input[0].data));
for (std::size_t i = 1; i < ratio; i++) {
std::size_t shift = 0;
for (std::size_t j = 0; j < i; j++) {
shift += chunk_sizes[j];
}
result += non_native_field_type::value_type(native_field_type::integral_type(input[i].data) << shift);
}
return result;
}

};

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,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 @@ -174,7 +174,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 @@ -275,7 +275,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
23 changes: 13 additions & 10 deletions test/algebra/curves/plonk/unified_addition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ void test_unified_addition(std::vector<typename CurveType::base_field_type::valu

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,14 +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();
<<<<<<< HEAD

if (Q.X == zero.X && Q.Y == zero.Y) {
expected_res = P;
Expand All @@ -168,6 +168,9 @@ void test_unified_addition_random_data() {
}
}
}
=======
expected_res = P + Q;
>>>>>>> 1c5f11c9 (zero representation in unified addition component changed from (0,0) to (0,1))

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