Skip to content

Commit

Permalink
add_sub_mod_p
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineCyr committed Feb 13, 2025
1 parent dafda78 commit 2c62d6a
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
#ifndef CRYPTO3_BBF_COMPONENTS_EC_DOUBLE_ECDSA_HPP
#define CRYPTO3_BBF_COMPONENTS_EC_DOUBLE_ECDSA_HPP

#include <nil/blueprint/bbf/components/algebra/fields/non_native/addition_mod_p.hpp>
#include <nil/blueprint/bbf/components/algebra/fields/non_native/add_sub_mod_p.hpp>
#include <nil/blueprint/bbf/components/algebra/fields/non_native/check_mod_p.hpp>
#include <nil/blueprint/bbf/components/algebra/fields/non_native/flexible_multiplication.hpp>
#include <nil/blueprint/bbf/components/algebra/fields/non_native/negation_mod_p.hpp>
#include <nil/blueprint/bbf/components/detail/range_check_multi.hpp>
#include <nil/blueprint/bbf/generic.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>
Expand Down Expand Up @@ -92,8 +91,7 @@ namespace nil {
}

static std::tuple<std::vector<TYPE>, std::vector<TYPE>,
std::vector<TYPE>, std::vector<TYPE>,
TYPE>
std::vector<TYPE>, std::vector<TYPE>, TYPE>
form_input(context_type& context_object, raw_input_type raw_input,
std::size_t num_chunks, std::size_t bit_size_chunk) {
std::vector<TYPE> input_xQ(num_chunks);
Expand Down Expand Up @@ -122,7 +120,7 @@ namespace nil {
column_type::public_input);
}
context_object.allocate(input_zero, 0, 4 * num_chunks,
column_type::public_input);
column_type::public_input);
return std::make_tuple(input_xQ, input_yQ, input_p, input_pp,
input_zero);
}
Expand All @@ -142,12 +140,11 @@ namespace nil {
typename bbf::components::range_check_multi<FieldType, stage>;
using Check_Mod_P =
typename bbf::components::check_mod_p<FieldType, stage>;
using Addition_Mod_P =
typename bbf::components::addition_mod_p<FieldType, stage,
NonNativeFieldType>;
using Negation_Mod_P =
typename bbf::components::negation_mod_p<FieldType, stage,
NonNativeFieldType>;
using Addition_Mod_P = typename bbf::components::add_sub_mod_p<
FieldType, stage, NonNativeFieldType, true>;
using Substraction_Mod_P =
typename bbf::components::add_sub_mod_p<
FieldType, stage, NonNativeFieldType, false>;
using Multiplication_Mod_P =
typename bbf::components::flexible_multiplication<
FieldType, stage, NonNativeFieldType>;
Expand Down Expand Up @@ -246,31 +243,30 @@ namespace nil {
input_zero, num_chunks, bit_size_chunk);
return t.r;
};
auto NegModP = [&context_object, input_p, input_pp, input_zero,
num_chunks, bit_size_chunk](std::vector<TYPE> x) {
Negation_Mod_P t =
Negation_Mod_P(context_object, x, input_p, input_pp,
input_zero, num_chunks, bit_size_chunk);
auto SubModP = [&context_object, input_p, input_pp, input_zero,
num_chunks, bit_size_chunk](std::vector<TYPE> x,
std::vector<TYPE> y) {
Substraction_Mod_P t = Substraction_Mod_P(
context_object, x, y, input_p, input_pp, input_zero,
num_chunks, bit_size_chunk);
return t.r;
};

auto t1 = MultModP(input_yQ, LAMBDA); // t1 = yQ * lambda
auto t2 = AddModP(t1, t1); // t2 = t1 + t1 = 2yQ * lambda
auto t1 = MultModP(input_yQ, LAMBDA); // t1 = yQ * lambda
auto t2 = AddModP(t1, t1); // t2 = t1 + t1 = 2yQ * lambda
auto t3 = AddModP(input_xQ, input_xQ); // t3 = xQ + xQ = 2xQ
auto t4 = AddModP(input_xQ, t3); // t4 = xQ + t3 = 3xQ
auto t5 = MultModP(t4, input_xQ); // t5 = t4 * xQ = 3xQ^2
CopyConstrain(t2, t5); // 2yQ lambda = 3xQ^2
CopyConstrain(t2, t5); // 2yQ lambda = 3xQ^2
auto t6 = AddModP(XR, t3); // t6 = xR + t3 = xR + 2xQ
auto t7 = MultModP(LAMBDA, LAMBDA); // t7 = lambda * lambda
CopyConstrain(t6, t7); // xR + 2xQ = lambda^2
CopyConstrain(t6, t7); // xR + 2xQ = lambda^2
auto t8 = AddModP(YR, input_yQ); // t8 = yR + yQ
auto t9 = NegModP(XR); // t9 = -xR
auto t10 = AddModP(input_xQ, t9); // t10 = xQ + t9 = xQ - xR
auto t11 =
MultModP(LAMBDA, t10); // t11 = lambda * t10 =lambda(xQ-xR)
CopyConstrain(t8, t11); // yR + yQ = lambda(xQ - xR)
auto t12 = MultModP(Z, t1); // t12 = z * t1 = z * yQ * lambda
CopyConstrain(LAMBDA, t12); // lambda = z yQ lambda
auto t9 = SubModP(input_xQ, XR); // t9 = xQ - xR
auto t10 = MultModP(LAMBDA, t9); // t11 = lambda * t9 =lambda(xQ-xR)
CopyConstrain(t8, t10); // yR + yQ = lambda(xQ - xR)
auto t11 = MultModP(Z, t1); // t12 = z * t1 = z * yQ * lambda
CopyConstrain(LAMBDA, t11); // lambda = z yQ lambda

for (int i = 0; i < num_chunks; ++i) {
xR.push_back(XR[i]);
Expand Down
Loading

0 comments on commit 2c62d6a

Please sign in to comment.