Skip to content

Commit

Permalink
ec addition
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineCyr committed Jan 25, 2025
1 parent 3eb2601 commit fb19751
Show file tree
Hide file tree
Showing 19 changed files with 1,506 additions and 730 deletions.

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Valeh Farzaliyev <[email protected]>
// Copyright (c) 2024 Antoine Cyr <antoine.cyr@nil.foundation>
// Copyright (c) 2024 Antoine Cyr <antoinecyr@nil.foundation>
//
// MIT License
//
Expand All @@ -22,7 +22,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//
// @file Declaration of interfaces for FRI verification array swapping component.
// @file Declaration of interfaces for addition function on mod p.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_BBF_COMPONENTS_ADDITION_MOD_P_HPP
Expand Down Expand Up @@ -52,8 +52,8 @@ namespace nil {
// operates on k-chunked x,y, p, p'
// Parameters: num_chunks = k, bit_size_chunk = b
// Input: x[0], ..., x[k-1], y[0], ..., y[k-1], p[0], ..., p[k-1], p'[0], ..., p'[k-1], 0
// Intemmediate values: q, t[0], ..., t[k-1], carry[k-1], t'[0], ..., t'[k-1], t"[0], ..., t"[k-1], carry"[k-1]
// Output: z[0] = x[0] + y[0] - qp[0], ..., z[k-1] = x[k-1] + y[k-1] -qp[k-1]
// Intermediate values: q, t[0], ..., t[k-1], carry[k-1], t'[0], ..., t'[k-1], t"[0], ..., t"[k-1], carry"[k-1]
// Output: r[0] = x[0] + y[0] - qp[0], ..., r[k-1] = x[k-1] + y[k-1] -qp[k-1]
//

template<typename FieldType>
Expand Down Expand Up @@ -111,7 +111,7 @@ namespace nil {
std::vector<TYPE> inp_y;
std::vector<TYPE> inp_p;
std::vector<TYPE> inp_pp;
std::vector<TYPE> res_z;
std::vector<TYPE> res_r;

static table_params get_minimal_requirements(
std::size_t num_chunks, std::size_t bit_size_chunk) {
Expand Down Expand Up @@ -234,24 +234,24 @@ namespace nil {

Carry_On_Addition ca_1 = Carry_On_Addition(
context_object, X, Y, num_chunks, bit_size_chunk);
Range_Check rc_1 = Range_Check(context_object, ca_1.res_z,
Range_Check rc_1 = Range_Check(context_object, ca_1.res_r,
num_chunks, bit_size_chunk);
//(qp = 0 or p)
Choice_Function cf =
Choice_Function(context_object, Q, ZERO, P, num_chunks);

Carry_On_Addition ca_2 =
Carry_On_Addition(context_object, cf.res_z, Z, num_chunks,
Carry_On_Addition(context_object, cf.res_r, Z, num_chunks,
bit_size_chunk); // qp + z

// carry_on_addition results should be equal to each other x + y = z + qp

for (std::size_t i = 0; i < num_chunks; i++) {
copy_constrain(ca_1.res_z[i], ca_2.res_z[i]);
copy_constrain(ca_1.res_r[i], ca_2.res_r[i]);
}
copy_constrain(ca_1.res_c, ca_2.res_c);

Range_Check rc_2 = Range_Check(context_object, ca_2.res_z,
Range_Check rc_2 = Range_Check(context_object, ca_2.res_r,
num_chunks, bit_size_chunk);

Range_Check rc_3 =
Expand All @@ -277,7 +277,7 @@ namespace nil {
inp_pp.push_back(input_pp[i]);
}
for (int i = 0; i < num_chunks; ++i) {
res_z.push_back(Z[i]);
res_r.push_back(Z[i]);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace nil {
using Range_Check = typename bbf::components::range_check_multi<FieldType,stage>;

Carry_On_Addition ca = Carry_On_Addition(context_object,input_x,input_pp,num_chunks,bit_size_chunk);
Range_Check rc = Range_Check(context_object, ca.res_z,num_chunks,bit_size_chunk);
Range_Check rc = Range_Check(context_object, ca.res_r,num_chunks,bit_size_chunk);

if(expect_output){
output = ca.res_c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace nil {
std::vector<TYPE> inp_y;
std::vector<TYPE> inp_p;
std::vector<TYPE> inp_pp;
std::vector<TYPE> res_z;
std::vector<TYPE> res_r;

static table_params get_minimal_requirements(std::size_t num_chunks,std::size_t bit_size_chunk) {
//The 6 variables chunks fit in 2 rows, and there is a 3rd additionnal row available for the constraint values
Expand Down Expand Up @@ -321,7 +321,7 @@ namespace nil {
inp_pp.push_back(input_pp[i]);
}
for (int i = 0; i < num_chunks; ++i) {
res_z.push_back(R[i]);
res_r.push_back(R[i]);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Georgios Fotiadis <[email protected]>
// Copyright (c) 2024 Alexey Yashunsky <[email protected]>
// Copyright (c) 2024 Antoine Cyr <antoine.cyr@nil.foundation>
// Copyright (c) 2024 Antoine Cyr <antoinecyr@nil.foundation>
//
// MIT License
//
Expand All @@ -23,7 +23,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//
// @file Declaration of interfaces for FRI verification array swapping component.
// @file Declaration of interfaces for negation function on mod p.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_BBF_COMPONENTS_NEGATION_MOD_P_HPP
Expand All @@ -50,9 +50,9 @@ namespace nil {
namespace bbf {
namespace components {
// Parameters: num_chunks = k, bit_size_chunk = b
// Finding the negative y of integer x, modulo p and checking that x + y = 0 mod p
// Finding the negative r of integer x, modulo p and checking that x + r = 0 mod p
// Input: x[0], ..., x[k-1], p[0], ..., p[k-1], pp[0], ..., pp[k-1], 0 (expects zero constant as input)
// Output: y[0], ..., y[k-1]
// Output: r[0], ..., r[k-1]

template<typename FieldType>
struct negation_mod_p_raw_input {
Expand Down Expand Up @@ -107,7 +107,7 @@ namespace nil {
std::vector<TYPE> inp_x;
std::vector<TYPE> inp_p;
std::vector<TYPE> inp_pp;
std::vector<TYPE> res_z;
std::vector<TYPE> res_r;

static table_params get_minimal_requirements(
std::size_t num_chunks, std::size_t bit_size_chunk) {
Expand Down Expand Up @@ -224,7 +224,7 @@ namespace nil {

// x + y = 0 or p
for (std::size_t i = 0; i < num_chunks; i++) {
copy_constrain(ca.res_z[i], cf.res_z[i]);
copy_constrain(ca.res_r[i], cf.res_r[i]);
}
copy_constrain(ca.res_c, ZERO[0]);

Expand All @@ -249,7 +249,7 @@ namespace nil {
inp_pp.push_back(input_pp[i]);
}
for (int i = 0; i < num_chunks; ++i) {
res_z.push_back(Y[i]);
res_r.push_back(Y[i]);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace nil {
public:
std::vector<TYPE> inp_x;
std::vector<TYPE> inp_y;
std::vector<TYPE> res_z;
std::vector<TYPE> res_r;
TYPE res_c;

static table_params get_minimal_requirements(std::size_t num_chunks,std::size_t bit_size_chunk) {
Expand Down Expand Up @@ -137,7 +137,7 @@ namespace nil {
for (int i = 0; i < num_chunks; ++i) {
inp_x.push_back(input_x[i]);
inp_y.push_back(input_y[i]);
res_z.push_back(Z[i]);
res_r.push_back(Z[i]);
}
res_c = C[num_chunks-1];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Alexey Yashunsky <[email protected]>
// Copyright (c) 2024 Antoine Cyr <antoine.cyr@nil.foundation>
// Copyright (c) 2024 Antoine Cyr <antoinecyr@nil.foundation>
//
// MIT License
//
Expand Down Expand Up @@ -65,7 +65,7 @@ namespace nil {

public:
TYPE inp_q;
std::vector<TYPE> inp_x, inp_y, res_z;
std::vector<TYPE> inp_x, inp_y, res_r;

static table_params get_minimal_requirements(std::size_t num_chunks) {
std::size_t witness = num_chunks + 1;
Expand Down Expand Up @@ -133,7 +133,7 @@ namespace nil {
for (std::size_t i = 0; i < num_chunks; i++) {
inp_x.push_back(X[i]);
inp_y.push_back(Y[i]);
res_z.push_back(Z[i]);
res_r.push_back(Z[i]);
}
};
};
Expand Down
2 changes: 2 additions & 0 deletions crypto3/libs/blueprint/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ set(COMMON_TEST_FILES
"bbf/algebra/fields/non_native/negation_mod_p"
"bbf/algebra/fields/non_native/flexible_multiplication"
"bbf/algebra/curves/weierstrass/ec_double"
"bbf/algebra/curves/weierstrass/ec_full_add"
"bbf/algebra/curves/weierstrass/ec_incomplete_add"
)

set(NON_NATIVE_TESTS_FILES
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Antoine Cyr <antoine.cyr@nil.foundation>
// Copyright (c) 2024 Antoine Cyr <antoinecyr@nil.foundation>
//
// MIT License
//
Expand All @@ -22,7 +22,7 @@
// SOFTWARE.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE bbf_check_mod_p_test
#define BOOST_TEST_MODULE bbf_ec_double_test

#include <boost/test/unit_test.hpp>
#include <nil/blueprint/bbf/components/algebra/curves/weierstrass/ec_double.hpp>
Expand Down
Loading

0 comments on commit fb19751

Please sign in to comment.