Skip to content

Commit

Permalink
zkevm shifts operation bbf
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Cyr committed Nov 21, 2024
1 parent 8d1cc0c commit 6e69f7d
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 318 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ namespace nil {
public:
zkevm_mulmod_bbf(context_type &context_object,
const opcode_input_type<FieldType, stage> &current_state)
: generic_component<FieldType, stage>(context_object,false), res(chunk_amount) {
: generic_component<FieldType, stage>(context_object, false),
res(chunk_amount) {
using integral_type = boost::multiprecision::number<
boost::multiprecision::backends::cpp_int_modular_backend<257>>;
using extended_integral_type = boost::multiprecision::number<
Expand Down Expand Up @@ -187,7 +188,6 @@ namespace nil {
TYPE Nrpp_add;

TYPE c_zero;
TYPE c_one;

std::vector<TYPE> a_64_chunks(8);
std::vector<TYPE> b_64_chunks(4);
Expand Down Expand Up @@ -321,33 +321,22 @@ namespace nil {
// caluclate first row carries
first_carryless =
first_carryless_construct<TYPE>(Nr_64_chunks, N_64_chunks, r_64_chunks);
auto first_row_carries =
first_carryless_construct(Nr_64_chunks, N_64_chunks, r_64_chunks)
.data >>
128;
auto first_row_carries = first_carryless.data >> 128;
value_type c_1 =
static_cast<value_type>(first_row_carries & (two_64 - 1).data);
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
second_carryless = second_carryless_construct<TYPE>(
Nr_64_chunks, N_64_chunks, r_64_chunks);
auto second_row_carries =
(second_carryless_construct(Nr_64_chunks, N_64_chunks, r_64_chunks) +
c_1 + c_2 * two_64)
.data >>
128;
auto second_row_carries = second_carryless.data >> 128;
value_type c_3 =
static_cast<value_type>(second_row_carries & (two_64 - 1).data);
c_4 = static_cast<value_type>(second_row_carries >> 64);
c_3_chunks = chunk_64_to_16<FieldType>(c_3);
third_carryless =
third_carryless_construct<TYPE>(Nr_64_chunks, N_64_chunks, r_64_chunks);
auto third_row_carries =
(third_carryless_construct(Nr_64_chunks, N_64_chunks, r_64_chunks) +
c_3 + c_4 * two_64)
.data >>
128;
auto third_row_carries = third_carryless.data >> 128;
value_type c_5 =
static_cast<value_type>(third_row_carries & (two_64 - 1).data);
c_6 = static_cast<value_type>(third_carryless.data >> 64);
Expand All @@ -370,10 +359,10 @@ namespace nil {
c_1_64 = chunk_sum_64<TYPE>(c_1_chunks, 0);
c_3_64 = chunk_sum_64<TYPE>(c_3_chunks, 0);
c_5_64 = chunk_sum_64<TYPE>(c_5_chunks, 0);
}
c_one = c_zero + 1;
N_nonzero = N_sum * N_sum_inverse;
N_nonzero = N_sum * N_sum_inverse;
N_nonzero_2 = N_nonzero;
}


allocate(N_nonzero, 32, 5);
for (std::size_t i = 0; i < chunk_amount; i++) {
Expand Down Expand Up @@ -554,16 +543,14 @@ namespace nil {
auto Res_128 = chunks16_to_chunks128_reversed<TYPE>(res);

TYPE A0, A1, B0, B1, N0, N1, Res0, Res1;
if constexpr (stage == GenerationStage::ASSIGNMENT) {
A0 = A_128.first;
A1 = A_128.second;
B0 = B_128.first;
B1 = B_128.second;
N0 = N_128.first;
N1 = N_128.second;
Res0 = Res_128.first;
Res1 = Res_128.second;
}
A0 = A_128.first;
A1 = A_128.second;
B0 = B_128.first;
B1 = B_128.second;
N0 = N_128.first;
N1 = N_128.second;
Res0 = Res_128.first;
Res1 = Res_128.second;
allocate(A0, 33, 4);
allocate(A1, 33, 5);
allocate(B0, 34, 5);
Expand All @@ -572,15 +559,6 @@ namespace nil {
allocate(N1, 42, 1);
allocate(Res0, 34, 2);
allocate(Res1, 34, 3);

constrain(A0 - A_128.first);
constrain(A1 - A_128.second);
constrain(B0 - B_128.first);
constrain(B1 - B_128.second);
constrain(N0 - N_128.first);
constrain(N1 - N_128.second);
constrain(Res0 - Res_128.first);
constrain(Res1 - Res_128.second);
if constexpr (stage == GenerationStage::CONSTRAINTS) {
constrain(current_state.pc_next() - current_state.pc(6) -
1); // PC transition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ namespace nil {
sum_part_b * I2); // z is zero if input_b >= 256, otherwise it is 1
two_powers =
(static_cast<unsigned int>(1) << int(integral_type(input_b) % 16));
tp = z * two_powers;
b_sum_inverse = b_sum.is_zero() ? 0 : b_sum.inversed();
b_zero = 1 - b_sum_inverse * b_sum;

Expand Down Expand Up @@ -353,14 +352,13 @@ namespace nil {
constrain(b_zero * r_chunks[i]);
}

tp = z * two_powers;
allocate(tp, 35, 3);
allocate(z, 36, 3);
allocate(I1, 37, 3);
allocate(I2, 38, 3);
allocate(two_powers, 39, 3);

constrain(tp - z * two_powers);

allocate(b0p, 32, 3);
allocate(b0pp, 33, 3);
allocate(b0ppp, 34, 3);
Expand Down Expand Up @@ -484,28 +482,19 @@ namespace nil {

TYPE A0, A1, B0, B1, Res0, Res1;

if constexpr (stage == GenerationStage::ASSIGNMENT) {
A0 = A_128.first;
A1 = A_128.second;
B0 = B_128.first;
B1 = B_128.second;
Res0 = Res_128.first;
Res1 = Res_128.second;
}
allocate(A0, 39, 0);
allocate(A1, 39, 2);
allocate(B0, 40, 0);
allocate(A1, 40, 0);
allocate(B0, 39, 2);
allocate(B1, 40, 2);
allocate(Res0, 41, 0);
allocate(Res1, 41, 2);

constrain(A0 - A_128.first);
constrain(A1 - A_128.second);
constrain(B0 - B_128.first);
constrain(B1 - B_128.second);
constrain(Res0 - Res_128.first);
constrain(Res1 - Res_128.second);

if constexpr (stage == GenerationStage::CONSTRAINTS) {
constrain(current_state.pc_next() - current_state.pc(4) -
1); // PC transition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ namespace nil {
(1 - sum_b * I2); // z is zero if input_b >= 256, otherwise it is 1
two_powers =
(static_cast<unsigned int>(1) << int(integral_type(input_b) % 16));
tp = z * two_powers;

// note that we don't assign 64-chunks for a/b, as we can build them from
// 16-chunks with constraints under the same logic we only assign the 16 -
Expand Down Expand Up @@ -224,14 +223,14 @@ namespace nil {
res[i] = r_chunks[i];
}

tp = z * two_powers;

allocate(tp, 32, 0);
allocate(z, 33, 0);
allocate(I1, 34, 0);
allocate(I2, 35, 0);
allocate(two_powers, 36, 0);

constrain(tp - z * two_powers);

allocate(b0p, 32, 1);
allocate(b0pp, 33, 1);
allocate(b0ppp, 34, 1);
Expand Down Expand Up @@ -262,28 +261,19 @@ namespace nil {
auto Res_128 = chunks16_to_chunks128_reversed<TYPE>(res);

TYPE A0, A1, B0, B1, Res0, Res1;
if constexpr (stage == GenerationStage::ASSIGNMENT) {
A0 = A_128.first;
A1 = A_128.second;
B0 = B_128.first;
B1 = B_128.second;
Res0 = Res_128.first;
Res1 = Res_128.second;
}
A0 = A_128.first;
A1 = A_128.second;
B0 = B_128.first;
B1 = B_128.second;
Res0 = Res_128.first;
Res1 = Res_128.second;
allocate(A0, 37, 0);
allocate(A1, 37, 1);
allocate(B0, 38, 0);
allocate(B1, 38, 1);
allocate(Res0, 39, 0);
allocate(Res1, 39, 1);

constrain(A0 - A_128.first);
constrain(A1 - A_128.second);
constrain(B0 - B_128.first);
constrain(B1 - B_128.second);
constrain(Res0 - Res_128.first);
constrain(Res1 - Res_128.second);

if constexpr (stage == GenerationStage::CONSTRAINTS) {
constrain(current_state.pc_next() - current_state.pc(2) -
1); // PC transition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ namespace nil {
sum_part_b * I2); // z is zero if input_b >= 256, otherwise it is 1
two_powers =
(static_cast<unsigned int>(1) << int(integral_type(input_b) % 16));
tp = z * two_powers;
b_sum_inverse = b_sum.is_zero() ? 0 : b_sum.inversed();
b_zero = 1 - b_sum_inverse * b_sum;

Expand Down Expand Up @@ -288,14 +287,13 @@ namespace nil {
constrain(b_zero * r_chunks[i]);
}

tp = z * two_powers;
allocate(tp, 32, 1);
allocate(z, 33, 1);
allocate(I1, 34, 1);
allocate(I2, 35, 1);
allocate(two_powers, 36, 1);

constrain(tp - z * two_powers);

allocate(b0p, 32, 0);
allocate(b0pp, 33, 0);
allocate(b0ppp, 34, 0);
Expand Down Expand Up @@ -349,28 +347,19 @@ namespace nil {
auto Res_128 = chunks16_to_chunks128_reversed<TYPE>(res);

TYPE A0, A1, B0, B1, Res0, Res1;
if constexpr (stage == GenerationStage::ASSIGNMENT) {
A0 = A_128.first;
A1 = A_128.second;
B0 = B_128.first;
B1 = B_128.second;
Res0 = Res_128.first;
Res1 = Res_128.second;
}
allocate(A0, 38, 0);
allocate(A1, 38, 1);
allocate(B0, 39, 0);
allocate(B1, 39, 1);
allocate(Res0, 40, 0);
allocate(Res1, 40, 1);

constrain(A0 - A_128.first);
constrain(A1 - A_128.second);
constrain(B0 - B_128.first);
constrain(B1 - B_128.second);
constrain(Res0 - Res_128.first);
constrain(Res1 - Res_128.second);

if constexpr (stage == GenerationStage::CONSTRAINTS) {
constrain(current_state.pc_next() - current_state.pc(3) -
1); // PC transition
Expand Down
1 change: 0 additions & 1 deletion crypto3/libs/blueprint/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ set(ZKEVM_BBF_TESTS_FILES
"zkevm_bbf/opcodes/byte_ops"
"zkevm_bbf/opcodes/cmp"
"zkevm_bbf/opcodes/add_sub"
"zkevm_bbf/opcodes/byte_ops"
)


Expand Down
Loading

0 comments on commit 6e69f7d

Please sign in to comment.