From 5ad1ad899bb2a9738a7cce98ec88e3086f9c0ae8 Mon Sep 17 00:00:00 2001 From: Antoine Cyr Date: Fri, 24 Jan 2025 20:34:11 -0500 Subject: [PATCH] ec arithmetic cleanup --- .../curves/weierstrass/ec_full_add.hpp | 28 ++++++++---------- .../fields/non_native/addition_mod_p.hpp | 29 ++----------------- .../fields/non_native/negation_mod_p.hpp | 29 ++----------------- .../include/nil/blueprint/bbf/tester.hpp | 10 +++---- 4 files changed, 21 insertions(+), 75 deletions(-) diff --git a/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/curves/weierstrass/ec_full_add.hpp b/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/curves/weierstrass/ec_full_add.hpp index 170907547..443ad7ff7 100644 --- a/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/curves/weierstrass/ec_full_add.hpp +++ b/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/curves/weierstrass/ec_full_add.hpp @@ -191,7 +191,7 @@ namespace nil { std::vector YQ(num_chunks); std::vector P(num_chunks); std::vector PP(num_chunks); - TYPE ZERO; + std::vector ZERO(num_chunks); std::vector LAMBDA(num_chunks); std::vector XR(num_chunks); @@ -200,7 +200,7 @@ namespace nil { std::vector ZQ(num_chunks); std::vector ZPQ(num_chunks); std::vector WPQ(num_chunks); - std::vector ZEROv(num_chunks); + if constexpr (stage == GenerationStage::ASSIGNMENT) { for (std::size_t i = 0; i < num_chunks; ++i) { @@ -210,8 +210,8 @@ namespace nil { YQ[i] = input_yQ[i]; P[i] = input_p[i]; PP[i] = input_pp[i]; + ZERO[i] = input_zero; } - ZERO = input_zero; non_native_integral_type pow = 1; NON_NATIVE_TYPE xP = 0, yP = 0, xQ = 0, yQ = 0; @@ -300,6 +300,7 @@ namespace nil { allocate(YQ[i]); allocate(P[i]); allocate(PP[i]); + allocate(ZERO[i]); allocate(LAMBDA[i]); allocate(XR[i]); @@ -308,17 +309,13 @@ namespace nil { allocate(ZQ[i]); allocate(ZPQ[i]); allocate(WPQ[i]); - - ZEROv[i] = ZERO; - allocate(ZEROv[i]); } - allocate(ZERO); auto check_chunked = [&context_object, num_chunks, bit_size_chunk, PP, ZERO](std::vector x) { Range_Check rc = Range_Check(context_object, x, num_chunks, bit_size_chunk); - Check_Mod_P cm = Check_Mod_P(context_object, x, PP, ZERO, + Check_Mod_P cm = Check_Mod_P(context_object, x, PP, ZERO[0], num_chunks, bit_size_chunk); }; @@ -332,7 +329,6 @@ namespace nil { // perform range checks and mod p checks on all stored variables check_chunked(LAMBDA); - check_chunked(Z); check_chunked(XR); check_chunked(YR); check_chunked(ZP); @@ -344,7 +340,7 @@ namespace nil { bit_size_chunk](std::vector x, std::vector y) { Multiplication_Mod_P t = - Multiplication_Mod_P(context_object, x, y, P, PP, ZERO, + Multiplication_Mod_P(context_object, x, y, P, PP, ZERO[0], num_chunks, bit_size_chunk); return t.res_r; }; @@ -352,14 +348,14 @@ namespace nil { bit_size_chunk](std::vector x, std::vector y) { Addition_Mod_P t = - Addition_Mod_P(context_object, x, y, P, PP, ZERO, + Addition_Mod_P(context_object, x, y, P, PP, ZERO[0], num_chunks, bit_size_chunk); return t.res_r; }; auto NegModP = [&context_object, P, PP, ZERO, num_chunks, bit_size_chunk](std::vector x) { Negation_Mod_P t = - Negation_Mod_P(context_object, x, P, PP, ZERO, num_chunks, + Negation_Mod_P(context_object, x, P, PP, ZERO[0], num_chunks, bit_size_chunk); return t.res_r; }; @@ -413,16 +409,16 @@ namespace nil { auto t35 = MultModP(t7, LAMBDA); // t35 = (xR - xP) lambda auto t36 = AddModP(t34, t35); // t36 = yR + yP + (xR - xP)lambda auto t37 = MultModP(t28, t33); // t37 = yP yQ (xP - xQ + (yP + yQ)(1 - ZPQ))(xR - lambda^2 + xP + xQ) - CopyConstrain(t37, ZEROv); // t37 = 0 + CopyConstrain(t37, ZERO); // t37 = 0 auto t38 = MultModP(t28, t36); // t38 = yP yQ (xP - xQ + (yP + yQ)(1 -ZPQ))(yR + yP + (xR - xP)lambda) - CopyConstrain(t38, ZEROv); // t38 = 0 + CopyConstrain(t38, ZERO); // t38 = 0 // part 4 auto t39 = MultModP(t9, LAMBDA); // t39 = (xP - xQ) lambda auto t40 = AddModP(t39, t4); // t40 = (xP - xQ) lambda - yP auto t41 = AddModP(t40, YQ); // t41 = (xP - xQ) lambda - yP + yQ auto t42 = MultModP(t9, t41); // t42 = (xP - xQ)((xP - xQ) lambda - yP + yQ) - CopyConstrain(t42, ZEROv); // t42 = 0 + CopyConstrain(t42, ZERO); // t42 = 0 auto t43 = MultModP(XP, t3); // t43 = -xP^2 auto t44 = AddModP(t43, t43); // t44 = -2xP^2 auto t45 = AddModP(t43, t44); // t45 = -3xP^2 @@ -440,8 +436,8 @@ namespace nil { copy_constrain(YQ[i], input_yQ[i]); copy_constrain(P[i], input_p[i]); copy_constrain(PP[i], input_pp[i]); + copy_constrain(ZERO[i], input_zero); } - copy_constrain(ZERO, input_zero); } for (int i = 0; i < num_chunks; ++i) { diff --git a/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/fields/non_native/addition_mod_p.hpp b/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/fields/non_native/addition_mod_p.hpp index 8fea6ac38..365c135ae 100644 --- a/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/fields/non_native/addition_mod_p.hpp +++ b/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/fields/non_native/addition_mod_p.hpp @@ -42,7 +42,6 @@ #include #include #include -#include namespace nil { namespace blueprint { @@ -83,28 +82,7 @@ namespace nil { typename std::conditional, std::tuple<>>::type; - using NonNativeIntegralExtendedVariant = - std::variant, - nil::crypto3::multiprecision::big_uint< - 2 * crypto3::algebra::curves::vesta:: - base_field_type::modulus_bits>>; - - template - struct NonNativeFieldTypeIndex; - - template<> - struct NonNativeFieldTypeIndex< - crypto3::algebra::curves::pallas::base_field_type> { - static constexpr std::size_t value = 0; - }; - - template<> - struct NonNativeFieldTypeIndex< - crypto3::algebra::curves::vesta::base_field_type> { - static constexpr std::size_t value = 1; - }; + public: std::vector inp_x; @@ -165,10 +143,7 @@ namespace nil { std::size_t bit_size_chunk, bool make_links = true) : generic_component(context_object) { using integral_type = typename FieldType::integral_type; - using extended_integral_type = - typename std::variant_alternative_t< - NonNativeFieldTypeIndex::value, - NonNativeIntegralExtendedVariant>; + using extended_integral_type = nil::crypto3::multiprecision::big_uint<2* NonNativeFieldType::modulus_bits>; using Carry_On_Addition = typename bbf::components::carry_on_addition; diff --git a/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/fields/non_native/negation_mod_p.hpp b/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/fields/non_native/negation_mod_p.hpp index da476ba85..017e86cc6 100644 --- a/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/fields/non_native/negation_mod_p.hpp +++ b/crypto3/libs/blueprint/include/nil/blueprint/bbf/components/algebra/fields/non_native/negation_mod_p.hpp @@ -43,7 +43,6 @@ #include #include #include -#include namespace nil { namespace blueprint { @@ -80,28 +79,7 @@ namespace nil { typename std::conditional, std::tuple<>>::type; - using NonNativeIntegralExtendedVariant = - std::variant, - nil::crypto3::multiprecision::big_uint< - 2 * crypto3::algebra::curves::vesta:: - base_field_type::modulus_bits>>; - - template - struct NonNativeFieldTypeIndex; - - template<> - struct NonNativeFieldTypeIndex< - crypto3::algebra::curves::pallas::base_field_type> { - static constexpr std::size_t value = 0; - }; - - template<> - struct NonNativeFieldTypeIndex< - crypto3::algebra::curves::vesta::base_field_type> { - static constexpr std::size_t value = 1; - }; + public: std::vector inp_x; @@ -157,10 +135,7 @@ namespace nil { bool make_links = true) : generic_component(context_object) { using integral_type = typename FieldType::integral_type; - using extended_integral_type = - typename std::variant_alternative_t< - NonNativeFieldTypeIndex::value, - NonNativeIntegralExtendedVariant>; + using extended_integral_type = nil::crypto3::multiprecision::big_uint<2* NonNativeFieldType::modulus_bits>; using Carry_On_Addition = typename bbf::components::carry_on_addition; diff --git a/crypto3/libs/blueprint/include/nil/blueprint/bbf/tester.hpp b/crypto3/libs/blueprint/include/nil/blueprint/bbf/tester.hpp index e9a7aa7c7..55e9117f5 100644 --- a/crypto3/libs/blueprint/include/nil/blueprint/bbf/tester.hpp +++ b/crypto3/libs/blueprint/include/nil/blueprint/bbf/tester.hpp @@ -38,7 +38,7 @@ #include #include -#include +// #include // #include #include @@ -104,7 +104,7 @@ namespace nil { generic_component(context_object) { using Is_Zero = is_zero; - using Choice_Function = choice_function; + // using Choice_Function = choice_function; // using Carry_On_Addition = carry_on_addition; using Useless = useless; @@ -113,9 +113,9 @@ namespace nil { Is_Zero(context_object, X, make_links); // make_links delegated to subcomponent - std::vector ct2_area = {2,3,4,5}; - context_type ct2 = context_object.subcontext(ct2_area,0,4); - auto c2 = Choice_Function(ct2,Q,CX,CY, make_links); // make_links delegated to subcomponent + // std::vector ct2_area = {2,3,4,5}; + // context_type ct2 = context_object.subcontext(ct2_area,0,4); + // auto c2 = Choice_Function(ct2,Q,CX,CY, make_links); // make_links delegated to subcomponent // std::vector ct3_area = {7,8,9,10,11}; // context_type ct3 = context_object.subcontext(ct3_area,0,4);