Skip to content

Commit

Permalink
wip #355
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Mar 26, 2024
1 parent 59e4a59 commit e480efe
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
namespace nil {
namespace blueprint {
namespace components {

using mnt4_g2_params = crypto3::algebra::curves::detail::
mnt4_g2_params<298,crypto3::algebra::curves::forms::short_weierstrass>;

// E'(F_p^2) : y^2 = x^3 + a*x + b point addition gate.
// Expects point at infinity encoded by (0,0) in input and output
// Input: (xP, yP) = P[4], (xQ, yQ) = Q[4]
Expand All @@ -70,7 +74,6 @@ namespace nil {

public:
using component_type = plonk_component<BlueprintFieldType>;

using var = typename component_type::var;
using manifest_type = plonk_component_manifest;

Expand Down Expand Up @@ -187,7 +190,7 @@ namespace nil {
zQ = yQ.inversed(),
zPQ = (xP - xQ).inversed(),
wPQ = (yP + yQ).inversed(),
lambda = (xP == xQ)? ((3*xP.pow(2) + mnt4_298::g2_type::params_type::a ) / (2*yP)) : ((yP-yQ)/(xP-xQ)),
lambda = (xP == xQ)? ((3*xP.pow(2) + mnt4_g2_params::a ) / (2*yP)) : ((yP-yQ)/(xP-xQ)),
nu = yP - lambda*xP,
xR, yR;
if (yP == fp2zero) {
Expand Down Expand Up @@ -243,12 +246,20 @@ namespace nil {
using constraint_type = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;

// Fp2 field over constraints:
using fp2_constraint = detail::abstract_fp2_element<constraint_type>;
using fp2_constraint = detail::abstract_fp2_element<
constraint_type,
typename mnt4_g2_params::field_type::value_type >;

constraint_type cnstr_zero = constraint_type(),
cnstr_one = cnstr_zero + 1;

constraint_type
constr_a0 = constraint_type()+mnt4_g2_params::a.data[0],
constr_a1 = constraint_type()+mnt4_g2_params::a.data[1];


fp2_constraint one = {cnstr_one,cnstr_zero},
a = {constr_a0, constr_a1},
xP = {var(component.W(0), 0, true),var(component.W(1), 0, true)},
yP = {var(component.W(2), 0, true),var(component.W(3), 0, true)},
xQ = {var(component.W(4), 0, true),var(component.W(5), 0, true)},
Expand Down Expand Up @@ -323,7 +334,7 @@ namespace nil {
// yQ ( 2yP zPQ ( (xP - xQ)la - (yP - yQ) ) + (1 - (xP - xQ)zPQ) wPQ (2yP la - 3xP^2)) = 0 (15)
C = yQ * (
2*yP * zPQ * ((xP - xQ)*la - (yP - yQ)) +
(one - (xP - xQ)*zPQ) * wPQ *(2*yP*la - 3*xP*xP - mnt4_298::g2_type::params_type::a)
(one - (xP - xQ)*zPQ) * wPQ *(2*yP*la - 3*xP*xP - a)
);
Cs.push_back(C[0]); Cs.push_back(C[1]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ namespace nil {
namespace detail {
template<typename T, typename UnderlyingFieldType>
class abstract_fp2_element {
using non_residue = UnderlyingFieldType::non_residue;
public:
std::array<T,2> data;

Expand All @@ -48,7 +47,7 @@ namespace nil {


constexpr abstract_fp2_element operator*(const abstract_fp2_element& other) {
return { data[0] * other.data[0] + non_residue * data[1] * other.data[1],
return { data[0] * other.data[0] + UnderlyingFieldType::non_residue * data[1] * other.data[1],
data[0] * other.data[1] + data[1] * other.data[0]};
}
constexpr abstract_fp2_element operator*(const int x) {
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ set(PLONK_TESTS_FILES
"algebra/curves/plonk/unified_addition"
"algebra/curves/plonk/variable_base_endo_scalar_mul"
"algebra/curves/plonk/endo_scalar"
"algebra/curves/plonk/mnt4_g2"
"hashes/plonk/poseidon"
"hashes/plonk/sha256"
"hashes/plonk/sha512"
Expand Down
4 changes: 2 additions & 2 deletions test/algebra/curves/plonk/mnt4_g2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void test_mnt4_g2_adding(std::vector<typename CurveType::base_field_type::value_
typename CurveType::template g2_type<>::value_type expected_res){

using curve_type = CurveType;
using BlueprintFieldType = typename curve_type::g2_type<>::field_type::base_field_type;
using BlueprintFieldType = typename curve_type::template g2_type<>::field_type::base_field_type;

constexpr std::size_t WitnessColumns = 12;
constexpr std::size_t PublicInputColumns = 1;
Expand All @@ -129,7 +129,7 @@ void test_mnt4_g2_adding(std::vector<typename CurveType::base_field_type::value_

auto result_check = [&expected_res, public_input](AssignmentType &assignment,
typename component_type::result_type &real_res) {
typename curve_type::g2_type<>::field_type::value_type expected_x = expected_res.X / expected_res.Z.pow(2),
typename curve_type::template g2_type<>::field_type::value_type expected_x = expected_res.X / expected_res.Z.pow(2),
expected_y = expected_res.Y / expected_res.Z.pow(3);
#ifdef BLUEPRINT_PLONK_PROFILING_ENABLED
std::cout << "G2 addition test: " << "\n";
Expand Down
6 changes: 3 additions & 3 deletions test/test_plonk_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
#include <nil/blueprint/blueprint/plonk/circuit_proxy.hpp>
#include <nil/blueprint/blueprint/plonk/assignment.hpp>
#include <nil/blueprint/blueprint/plonk/assignment_proxy.hpp>
//#include <nil/blueprint/utils/table_profiling.hpp>
#include <nil/blueprint/utils/table_profiling.hpp>
#include <nil/blueprint/utils/satisfiability_check.hpp>
#include <nil/blueprint/component_stretcher.hpp>
#include <nil/blueprint/utils/connectedness_check.hpp>

#include <nil/crypto3/math/algorithms/calculate_domain_set.hpp>

// #include "profiling_plonk_circuit.hpp"
#include "profiling_plonk_circuit.hpp"

#include <nil/marshalling/status_type.hpp>
#include <nil/marshalling/field_type.hpp>
Expand Down Expand Up @@ -605,4 +605,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_TEST_PLONK_COMPONENT_HPP
#endif // CRYPTO3_TEST_PLONK_COMPONENT_HPP

0 comments on commit e480efe

Please sign in to comment.