Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

350 basic cleanup of zkevm opcode circuits #361

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,59 +48,11 @@ namespace nil {

constexpr static const std::size_t chunk_amount = 16;
constexpr static const std::size_t carry_amount = 16 / 3 + 1;
constexpr static const value_type two_16 = 65536;
constexpr static const value_type two_32 = 4294967296;
constexpr static const value_type two_48 = 281474976710656;
constexpr static const value_type two_64 = 0x10000000000000000_big_uint254;
constexpr static const value_type two_128 =
0x100000000000000000000000000000000_big_uint254;
constexpr static const value_type two_192 =
0x1000000000000000000000000000000000000000000000000_big_uint254;

public:
using typename generic_component<FieldType, stage>::TYPE;
using typename generic_component<FieldType, stage>::context_type;

template<typename T, typename V = T>
T chunk_sum_64(const std::vector<V> &chunks, const unsigned char chunk_idx) const {
BOOST_ASSERT(chunk_idx < 4);
return chunks[4 * chunk_idx] + chunks[4 * chunk_idx + 1] * two_16 +
chunks[4 * chunk_idx + 2] * two_32 + chunks[4 * chunk_idx + 3] * two_48;
}

template<typename T>
T first_carryless_construct(const std::vector<T> &a_64_chunks,
const std::vector<T> &b_64_chunks,
const std::vector<T> &r_64_chunks,
const std::vector<T> &q_64_chunks) const {
return r_64_chunks[0] * b_64_chunks[0] + q_64_chunks[0] +
two_64 * (r_64_chunks[0] * b_64_chunks[1] +
r_64_chunks[1] * b_64_chunks[0] + q_64_chunks[1]) -
a_64_chunks[0] - two_64 * a_64_chunks[1];
}

template<typename T>
T second_carryless_construct(const std::vector<T> &a_64_chunks,
const std::vector<T> &b_64_chunks,
const std::vector<T> &r_64_chunks,
const std::vector<T> &q_64_chunks) const {
return (r_64_chunks[0] * b_64_chunks[2] + r_64_chunks[1] * b_64_chunks[1] +
r_64_chunks[2] * b_64_chunks[0] + q_64_chunks[2] - a_64_chunks[2]) +
two_64 *
(r_64_chunks[0] * b_64_chunks[3] + r_64_chunks[1] * b_64_chunks[2] +
r_64_chunks[2] * b_64_chunks[1] + r_64_chunks[3] * b_64_chunks[0] +
q_64_chunks[3] - a_64_chunks[3]);
}

template<typename T>
T third_carryless_construct(const std::vector<T> &b_64_chunks,
const std::vector<T> &r_64_chunks) const {
return (r_64_chunks[1] * b_64_chunks[3] + r_64_chunks[2] * b_64_chunks[2] +
r_64_chunks[3] * b_64_chunks[1]) +
two_64 *
(r_64_chunks[2] * b_64_chunks[3] + r_64_chunks[3] * b_64_chunks[2]);
}

TYPE carry_on_addition_constraint(TYPE a_0, TYPE a_1, TYPE a_2, TYPE b_0, TYPE b_1,
TYPE b_2, TYPE r_0, TYPE r_1, TYPE r_2,
TYPE last_carry, TYPE result_carry,
Expand Down Expand Up @@ -204,8 +156,8 @@ namespace nil {
if constexpr (stage == GenerationStage::ASSIGNMENT) {
// caluclate first row carries
auto first_row_carries =first_carryless.data.base()>>128;
value_type c_1 =
static_cast<value_type>(first_row_carries & (two_64 - 1).data.base());
value_type c_1 =
static_cast<value_type>(first_row_carries & (two_64 - 1));
c_2 = static_cast<value_type>(first_row_carries >> 64);
c_1_chunks = chunk_64_to_16<FieldType>(c_1);
// no need for c_2 chunks as there is only a single chunk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,59 +48,11 @@ namespace nil {

constexpr static const std::size_t chunk_amount = 16;
constexpr static const std::size_t carry_amount = 16 / 3 + 1;
constexpr static const value_type two_16 = 65536;
constexpr static const value_type two_32 = 4294967296;
constexpr static const value_type two_48 = 281474976710656;
constexpr static const value_type two_64 = 0x10000000000000000_big_uint254;
constexpr static const value_type two_128 =
0x100000000000000000000000000000000_big_uint254;
constexpr static const value_type two_192 =
0x1000000000000000000000000000000000000000000000000_big_uint254;

public:
using typename generic_component<FieldType, stage>::TYPE;
using typename generic_component<FieldType, stage>::context_type;

template<typename T, typename V = T>
T chunk_sum_64(const std::vector<V> &chunks, const unsigned char chunk_idx) const {
BOOST_ASSERT(chunk_idx < 4);
return chunks[4 * chunk_idx] + chunks[4 * chunk_idx + 1] * two_16 +
chunks[4 * chunk_idx + 2] * two_32 + chunks[4 * chunk_idx + 3] * two_48;
}

template<typename T>
T first_carryless_construct(const std::vector<T> &a_64_chunks,
const std::vector<T> &b_64_chunks,
const std::vector<T> &r_64_chunks,
const std::vector<T> &q_64_chunks) const {
return r_64_chunks[0] * b_64_chunks[0] + q_64_chunks[0] +
two_64 * (r_64_chunks[0] * b_64_chunks[1] +
r_64_chunks[1] * b_64_chunks[0] + q_64_chunks[1]) -
a_64_chunks[0] - two_64 * a_64_chunks[1];
}

template<typename T>
T second_carryless_construct(const std::vector<T> &a_64_chunks,
const std::vector<T> &b_64_chunks,
const std::vector<T> &r_64_chunks,
const std::vector<T> &q_64_chunks) const {
return (r_64_chunks[0] * b_64_chunks[2] + r_64_chunks[1] * b_64_chunks[1] +
r_64_chunks[2] * b_64_chunks[0] + q_64_chunks[2] - a_64_chunks[2]) +
two_64 *
(r_64_chunks[0] * b_64_chunks[3] + r_64_chunks[1] * b_64_chunks[2] +
r_64_chunks[2] * b_64_chunks[1] + r_64_chunks[3] * b_64_chunks[0] +
q_64_chunks[3] - a_64_chunks[3]);
}

template<typename T>
T third_carryless_construct(const std::vector<T> &b_64_chunks,
const std::vector<T> &r_64_chunks) const {
return (r_64_chunks[1] * b_64_chunks[3] + r_64_chunks[2] * b_64_chunks[2] +
r_64_chunks[3] * b_64_chunks[1]) +
two_64 *
(r_64_chunks[2] * b_64_chunks[3] + r_64_chunks[3] * b_64_chunks[2]);
}

TYPE carry_on_addition_constraint(TYPE a_0, TYPE a_1, TYPE a_2, TYPE b_0, TYPE b_1,
TYPE b_2, TYPE r_0, TYPE r_1, TYPE r_2,
TYPE last_carry, TYPE result_carry,
Expand Down Expand Up @@ -194,8 +146,7 @@ namespace nil {
if constexpr (stage == GenerationStage::ASSIGNMENT) {
// caluclate first row carries
auto first_row_carries = first_carryless.data.base() >> 128;
value_type c_1 =
static_cast<value_type>(first_row_carries & (two_64 - 1).data.base());
value_type c_1 = static_cast<value_type>(first_row_carries & (two_64 - 1));
c_2 = static_cast<value_type>(first_row_carries >> 64);
c_1_chunks = chunk_64_to_16<FieldType>(c_1);
// no need for c_2 chunks as there is only a single chunk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ namespace nil {
public:
using typename generic_component<FieldType,stage>::TYPE;

constexpr static const typename FieldType::value_type two_64 =
0x10000000000000000_big_uint256;
constexpr static const typename FieldType::value_type two_128 = 0x100000000000000000000000000000000_big_uint254;
constexpr static const typename FieldType::value_type two_192 = 0x1000000000000000000000000000000000000000000000000_big_uint254;

TYPE chunk_sum_64(const std::vector<TYPE> &chunks, const unsigned char chunk_idx) const {
BOOST_ASSERT(chunk_idx < 4);
TYPE result;
Expand All @@ -70,7 +65,7 @@ namespace nil {
) const {
return
a_64_chunks[3] * b_64_chunks[3] +
two_64 * (a_64_chunks[3] * b_64_chunks[2] + a_64_chunks[2] * b_64_chunks[3]) - r_64_chunks[3] - two_64 * r_64_chunks[2];
TYPE(two_64) * (a_64_chunks[3] * b_64_chunks[2] + a_64_chunks[2] * b_64_chunks[3]) - r_64_chunks[3] - TYPE(two_64) * r_64_chunks[2];
}


Expand All @@ -82,7 +77,7 @@ namespace nil {
return
(a_64_chunks[3] * b_64_chunks[1] + a_64_chunks[2] * b_64_chunks[2] +
a_64_chunks[1] * b_64_chunks[3] - r_64_chunks[1]) +
two_64 * (a_64_chunks[3] * b_64_chunks[0] + a_64_chunks[1] * b_64_chunks[2] +
TYPE(two_64) * (a_64_chunks[3] * b_64_chunks[0] + a_64_chunks[1] * b_64_chunks[2] +
a_64_chunks[2] * b_64_chunks[1] + a_64_chunks[0] * b_64_chunks[3] - r_64_chunks[0]);
}

Expand Down Expand Up @@ -135,10 +130,12 @@ namespace nil {
R_64[3] = chunk_sum_64(R, 3);


if constexpr( stage == GenerationStage::ASSIGNMENT ){
TYPE lo_carries = lo_carryless_construct(A_64, B_64, R_64);
TYPE hi_carries = hi_carryless_construct(A_64, B_64, R_64);
// std::vector<TYPE> DUMMY_64(4);
TYPE lo_carries = lo_carryless_construct(A_64, B_64, R_64);
// TYPE lo_carries = first_carryless_construct(R_64, A_64, B_64, DUMMY_64);
TYPE hi_carries = hi_carryless_construct(A_64, B_64, R_64);

if constexpr( stage == GenerationStage::ASSIGNMENT ){
zkevm_word_type c_first_i = typename FieldType::integral_type(lo_carries.data) >> 128;
auto c_first = w_to_16(c_first_i);
zkevm_word_type c_second_i = (typename FieldType::integral_type(hi_carries.data) + c_first_i) >> 128;
Expand All @@ -149,8 +146,8 @@ namespace nil {
C0 = c_second[11];
}

TYPE lo_carries = lo_carryless_construct(A_64, B_64, R_64);
TYPE hi_carries = hi_carryless_construct(A_64, B_64, R_64);
// TYPE lo_carries = lo_carryless_construct(A_64, B_64, R_64);
// TYPE hi_carries = hi_carryless_construct(A_64, B_64, R_64);

allocate(C3[0], 17, 1);
allocate(C3[1], 18, 1);
Expand Down
Loading