Skip to content

Commit

Permalink
ec arithmetic cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineCyr committed Jan 25, 2025
1 parent fb19751 commit 5ad1ad8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace nil {
std::vector<TYPE> YQ(num_chunks);
std::vector<TYPE> P(num_chunks);
std::vector<TYPE> PP(num_chunks);
TYPE ZERO;
std::vector<TYPE> ZERO(num_chunks);

std::vector<TYPE> LAMBDA(num_chunks);
std::vector<TYPE> XR(num_chunks);
Expand All @@ -200,7 +200,7 @@ namespace nil {
std::vector<TYPE> ZQ(num_chunks);
std::vector<TYPE> ZPQ(num_chunks);
std::vector<TYPE> WPQ(num_chunks);
std::vector<TYPE> ZEROv(num_chunks);


if constexpr (stage == GenerationStage::ASSIGNMENT) {
for (std::size_t i = 0; i < num_chunks; ++i) {
Expand All @@ -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;
Expand Down Expand Up @@ -300,6 +300,7 @@ namespace nil {
allocate(YQ[i]);
allocate(P[i]);
allocate(PP[i]);
allocate(ZERO[i]);

allocate(LAMBDA[i]);
allocate(XR[i]);
Expand All @@ -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<TYPE> 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);
};

Expand All @@ -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);
Expand All @@ -344,22 +340,22 @@ namespace nil {
bit_size_chunk](std::vector<TYPE> x,
std::vector<TYPE> 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;
};
auto AddModP = [&context_object, P, PP, ZERO, num_chunks,
bit_size_chunk](std::vector<TYPE> x,
std::vector<TYPE> 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<TYPE> 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;
};
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include <nil/blueprint/bbf/components/detail/choice_function.hpp>
#include <nil/blueprint/bbf/components/detail/range_check_multi.hpp>
#include <stdexcept>
#include <variant>

namespace nil {
namespace blueprint {
Expand Down Expand Up @@ -83,28 +82,7 @@ namespace nil {
typename std::conditional<stage == GenerationStage::ASSIGNMENT,
addition_mod_p_raw_input<FieldType>,
std::tuple<>>::type;
using NonNativeIntegralExtendedVariant =
std::variant<nil::crypto3::multiprecision::big_uint<
2 * crypto3::algebra::curves::pallas::
base_field_type::modulus_bits>,
nil::crypto3::multiprecision::big_uint<
2 * crypto3::algebra::curves::vesta::
base_field_type::modulus_bits>>;

template<typename T>
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<TYPE> inp_x;
Expand Down Expand Up @@ -165,10 +143,7 @@ namespace nil {
std::size_t bit_size_chunk, bool make_links = true)
: generic_component<FieldType, stage>(context_object) {
using integral_type = typename FieldType::integral_type;
using extended_integral_type =
typename std::variant_alternative_t<
NonNativeFieldTypeIndex<NonNativeFieldType>::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<FieldType, stage>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <nil/blueprint/bbf/components/detail/choice_function.hpp>
#include <nil/blueprint/bbf/components/detail/range_check_multi.hpp>
#include <stdexcept>
#include <variant>

namespace nil {
namespace blueprint {
Expand Down Expand Up @@ -80,28 +79,7 @@ namespace nil {
typename std::conditional<stage == GenerationStage::ASSIGNMENT,
negation_mod_p_raw_input<FieldType>,
std::tuple<>>::type;
using NonNativeIntegralExtendedVariant =
std::variant<nil::crypto3::multiprecision::big_uint<
2 * crypto3::algebra::curves::pallas::
base_field_type::modulus_bits>,
nil::crypto3::multiprecision::big_uint<
2 * crypto3::algebra::curves::vesta::
base_field_type::modulus_bits>>;

template<typename T>
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<TYPE> inp_x;
Expand Down Expand Up @@ -157,10 +135,7 @@ namespace nil {
bool make_links = true)
: generic_component<FieldType, stage>(context_object) {
using integral_type = typename FieldType::integral_type;
using extended_integral_type =
typename std::variant_alternative_t<
NonNativeFieldTypeIndex<NonNativeFieldType>::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<FieldType, stage>;
Expand Down
10 changes: 5 additions & 5 deletions crypto3/libs/blueprint/include/nil/blueprint/bbf/tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include <nil/blueprint/bbf/generic.hpp>

#include <nil/blueprint/bbf/is_zero.hpp>
#include <nil/blueprint/bbf/choice_function.hpp>
// #include <nil/blueprint/bbf/choice_function.hpp>
// #include <nil/blueprint/bbf/components/detail/carry_on_addition.hpp>
#include <nil/blueprint/bbf/useless.hpp>

Expand Down Expand Up @@ -104,7 +104,7 @@ namespace nil {
generic_component<FieldType,stage>(context_object) {

using Is_Zero = is_zero<FieldType, stage>;
using Choice_Function = choice_function<FieldType, stage, 3>;
// using Choice_Function = choice_function<FieldType, stage, 3>;
// using Carry_On_Addition = carry_on_addition<FieldType, stage, 3, 16>;
using Useless = useless<FieldType, stage>;

Expand All @@ -113,9 +113,9 @@ namespace nil {

Is_Zero(context_object, X, make_links); // make_links delegated to subcomponent

std::vector<std::size_t> 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<std::size_t> 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<std::size_t> ct3_area = {7,8,9,10,11};
// context_type ct3 = context_object.subcontext(ct3_area,0,4);
Expand Down

0 comments on commit 5ad1ad8

Please sign in to comment.