Skip to content

Commit

Permalink
reduce size of ecdsa table
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineCyr committed Feb 5, 2025
1 parent d06a946 commit e815fb7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ namespace nil {

static table_params get_minimal_requirements(
std::size_t num_chunks, std::size_t bit_size_chunk) {
const std::size_t L = bit_size_chunk*num_chunks + (bit_size_chunk*num_chunks % 2), // if odd, then +1. Thus L is always even
Q = L/2;
std::size_t witness = num_chunks * Q;
std::size_t witness = 7 * num_chunks;
constexpr std::size_t public_inputs = 1;
constexpr std::size_t constants = 0;
constexpr std::size_t rows = 65536 - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,8 @@ namespace nil {
std::vector<TYPE> yQA;

static table_params get_minimal_requirements(
// TODO
// Get the right minimal_requirements for this component
// and all non_native and ec subcomponents
std::size_t num_chunks, std::size_t bit_size_chunk) {
const std::size_t
L = bit_size_chunk * num_chunks +
(bit_size_chunk * num_chunks %
2),
Q = L / 2;
std::size_t witness = 10 * num_chunks * Q;
std::size_t witness = 7 * num_chunks;
constexpr std::size_t public_inputs = 1;
constexpr std::size_t constants = 1;
constexpr std::size_t rows = 131072 - 1;
Expand Down Expand Up @@ -195,6 +187,100 @@ namespace nil {
typename bbf::components::ec_scalar_mult<FieldType, stage,
BaseField>;

// Definition of constants
base_integral_type bB = base_integral_type(1) << bit_size_chunk,
p = BaseField::modulus,
b_ext_pow = base_integral_type(1)
<< num_chunks * bit_size_chunk,
pp = b_ext_pow - p;

scalar_integral_type sB = scalar_integral_type(1)
<< bit_size_chunk,
n = ScalarField::modulus,
s_ext_pow = scalar_integral_type(1)
<< num_chunks * bit_size_chunk,
np = s_ext_pow - n, m = (n - 1) / 2 + 1,
mp = s_ext_pow - m;

ec_point_value_type G = ec_point_value_type::one();
base_integral_type x = base_integral_type(
base_basic_integral_type(G.X.data)),
y = base_integral_type(
base_basic_integral_type(G.Y.data));
BASE_TYPE a = CurveType::template g1_type<
nil::crypto3::algebra::curves::coordinates::affine>::
params_type::b;

size_t row = 0;

// Helper functions for allocating constants
auto PushBaseChunks = [&context_object, &row, bB, num_chunks](base_integral_type x) {
std::vector<TYPE> X(num_chunks);
for (std::size_t i = 0; i < num_chunks; i++) {
X[i] = TYPE(x % bB);
context_object.allocate(X[i], 0, row,
column_type::constant);
x /= bB;
row++;
}
return X;
};

auto PushScalarChunks = [&context_object, &row, sB, num_chunks](scalar_integral_type x) {
std::vector<TYPE> X(num_chunks);
for (std::size_t i = 0; i < num_chunks; i++) {
X[i] = TYPE(x % sB);
context_object.allocate(X[i], 0, row,
column_type::constant);
x /= sB;
row++;
}
return X;
};

// Allocating constants
std::vector<TYPE> P = PushBaseChunks(p);
std::vector<TYPE> PP = PushBaseChunks(pp);

std::vector<TYPE> N = PushScalarChunks(n);
std::vector<TYPE> NP = PushScalarChunks(np);
std::vector<TYPE> M = PushScalarChunks(m);
std::vector<TYPE> MP = PushScalarChunks(mp);
std::vector<TYPE> X = PushBaseChunks(x);
std::vector<TYPE> Y = PushBaseChunks(y);
base_basic_integral_type aBB = base_basic_integral_type(a.data);
base_integral_type aB = base_integral_type(aBB);
std::vector<TYPE> A = PushBaseChunks(aB);

TYPE zero = 0;
TYPE one = 1;
allocate(zero, 0, row, column_type::constant);
row++;
allocate(one, 0, row, column_type::constant);
row++;

std::vector<TYPE> CHUNKED_ZERO(num_chunks);
std::vector<TYPE> CHUNKED_ONE(num_chunks);
std::vector<TYPE> CHUNKED_BIT(num_chunks);

for (std::size_t i = 0; i < num_chunks; i++) {
(i != 0) ? CHUNKED_ONE[i] = 0 : CHUNKED_ONE[i] = 1;
CHUNKED_ZERO[i] = 0;
CHUNKED_BIT[i] = 0;
context_object.allocate(CHUNKED_ZERO[i], 0, row,
column_type::constant);
row++;
context_object.allocate(CHUNKED_ONE[i], 0, row,
column_type::constant);
row++;
if (i != 0) {
context_object.allocate(CHUNKED_BIT[i], 0, row,
column_type::constant);
row++;
}
}
allocate(CHUNKED_BIT[0]);

// Helper functions for subcomponents
auto RangeCheck = [&context_object, num_chunks,
bit_size_chunk](std::vector<TYPE> x) {
Expand Down Expand Up @@ -324,100 +410,6 @@ namespace nil {
}
};

// Definition of constants
base_integral_type bB = base_integral_type(1) << bit_size_chunk,
p = BaseField::modulus,
b_ext_pow = base_integral_type(1)
<< num_chunks * bit_size_chunk,
pp = b_ext_pow - p;

scalar_integral_type sB = scalar_integral_type(1)
<< bit_size_chunk,
n = ScalarField::modulus,
s_ext_pow = scalar_integral_type(1)
<< num_chunks * bit_size_chunk,
np = s_ext_pow - n, m = (n - 1) / 2 + 1,
mp = s_ext_pow - m;

ec_point_value_type G = ec_point_value_type::one();
base_integral_type x = base_integral_type(
base_basic_integral_type(G.X.data)),
y = base_integral_type(
base_basic_integral_type(G.Y.data));
BASE_TYPE a = CurveType::template g1_type<
nil::crypto3::algebra::curves::coordinates::affine>::
params_type::b;

size_t row = 0;

// Helper functions for allocating constants
auto PushBaseChunks = [&context_object, &row, bB, num_chunks](base_integral_type x) {
std::vector<TYPE> X(num_chunks);
for (std::size_t i = 0; i < num_chunks; i++) {
X[i] = TYPE(x % bB);
context_object.allocate(X[i], 0, row,
column_type::constant);
x /= bB;
row++;
}
return X;
};

auto PushScalarChunks = [&context_object, &row, sB, num_chunks](scalar_integral_type x) {
std::vector<TYPE> X(num_chunks);
for (std::size_t i = 0; i < num_chunks; i++) {
X[i] = TYPE(x % sB);
context_object.allocate(X[i], 0, row,
column_type::constant);
x /= sB;
row++;
}
return X;
};

// Allocating constants
std::vector<TYPE> P = PushBaseChunks(p);
std::vector<TYPE> PP = PushBaseChunks(pp);

std::vector<TYPE> N = PushScalarChunks(n);
std::vector<TYPE> NP = PushScalarChunks(np);
std::vector<TYPE> M = PushScalarChunks(m);
std::vector<TYPE> MP = PushScalarChunks(mp);
std::vector<TYPE> X = PushBaseChunks(x);
std::vector<TYPE> Y = PushBaseChunks(y);
base_basic_integral_type aBB = base_basic_integral_type(a.data);
base_integral_type aB = base_integral_type(aBB);
std::vector<TYPE> A = PushBaseChunks(aB);

TYPE zero = 0;
TYPE one = 1;
allocate(zero, 0, row, column_type::constant);
row++;
allocate(one, 0, row, column_type::constant);
row++;

std::vector<TYPE> CHUNKED_ZERO(num_chunks);
std::vector<TYPE> CHUNKED_ONE(num_chunks);
std::vector<TYPE> CHUNKED_BIT(num_chunks);

for (std::size_t i = 0; i < num_chunks; i++) {
(i != 0) ? CHUNKED_ONE[i] = 0 : CHUNKED_ONE[i] = 1;
CHUNKED_ZERO[i] = 0;
CHUNKED_BIT[i] = 0;
context_object.allocate(CHUNKED_ZERO[i], 0, row,
column_type::constant);
row++;
context_object.allocate(CHUNKED_ONE[i], 0, row,
column_type::constant);
row++;
if (i != 0) {
context_object.allocate(CHUNKED_BIT[i], 0, row,
column_type::constant);
row++;
}
}
allocate(CHUNKED_BIT[0]);



// Declaring intermediate values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,8 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_bbf_ec_scalar_mult_test) {
// The curve is passed in as an argument to access additionnal properties
using pallas = typename crypto3::algebra::curves::pallas;
using vesta = typename crypto3::algebra::curves::vesta;

ec_scalar_mult_tests<pallas::base_field_type, vesta, 3, 96, random_tests_amount>();

ec_scalar_mult_tests<pallas::base_field_type, vesta, 3, 96, random_tests_amount>();

ec_scalar_mult_tests<vesta::base_field_type, pallas, 4, 65, random_tests_amount>();

ec_scalar_mult_tests<vesta::base_field_type, pallas, 8, 32, random_tests_amount>();
ec_scalar_mult_tests<vesta::base_field_type, pallas, 3, 96, random_tests_amount>();
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit e815fb7

Please sign in to comment.