Skip to content

Commit

Permalink
Merge branch '95-rw-in-bbf' into 136-zkevm-operations-in-bbf
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Cyr committed Nov 26, 2024
2 parents bf8ad39 + 724ace0 commit 189119a
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ namespace nil {
constraint_type c_1_64 = chunk_sum_64<constraint_type, var>(c_1_chunks, 0);
constraint_type c_3_64 = chunk_sum_64<constraint_type, var>(c_3_chunks, 0);
constraint_type first_carryless = first_carryless_consrtruct<constraint_type>(
a_64_chunks, b_64_chunks, r_64_chunks
);
a_64_chunks, b_64_chunks, r_64_chunks);
constraints.push_back({position, (first_carryless - c_1_64 * two128 - c_2 * two192)});
constraint_type second_carryless = second_carryless_construct<constraint_type>(
a_64_chunks, b_64_chunks, r_64_chunks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ namespace nil {
const std::map<std::size_t, std::uint8_t> memory;
const std::map<zkevm_word_type, zkevm_word_type> storage;

for( std::size_t ind = 0; ind < tester.get_opcodes().size(); ind++ ){
auto opcode = tester.get_opcodes()[ind].first;
auto additional_input = tester.get_opcodes()[ind].second;
while(true){
auto [opcode,additional_input] = tester.get_opcode_by_pc(pc);

zkevm_state state; // TODO:optimize
state.tx_hash = 0; // * change it
Expand All @@ -82,7 +81,7 @@ namespace nil {
state.rw_counter = rw_counter;
state.bytecode_hash = _bytecodes.get_data()[current_buffer_id].second;
state.additional_input = additional_input;
state.tx_finish = (ind == tester.get_opcodes().size() - 1);
//state.tx_finish = (ind == tester.get_opcodes().size() - 1);
state.stack_size = stack.size();
state.memory_size = memory.size();
state.stack_slice = stack;
Expand Down Expand Up @@ -386,8 +385,18 @@ namespace nil {
stack.push_back(result);
pc++;
gas -= 3;
} else if(opcode == zkevm_opcode::NOT) {
// 0x18
zkevm_word_type a = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, a));
zkevm_word_type result = zkevm_word_type(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_cppui_modular257) - a;;
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, true, result));
stack.push_back(result);
pc++;
gas -= 3;
}else if(opcode == zkevm_opcode::SHL) {
// // 0x1b
// 0x1b
zkevm_word_type b = stack.back();
stack.pop_back();
_rw_operations.push_back(stack_rw_operation(call_id, stack.size(), rw_counter++, false, b));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ namespace nil {
auto S_128 = chunks16_to_chunks128<TYPE>(S);
carry0 = (A_128.second + B_128.second >= two_128);
carry1 = (A_128.first + B_128.first + carry0 >= two_128);
std::cout << std::hex;
std::cout << "\tA = " << A_128.first << " " << A_128.second << std::endl;
std::cout << "\tB = " << B_128.first << " " << B_128.second << std::endl;
std::cout << "\tS = " << S_128.first << " " << S_128.second << std::endl;
std::cout << "\tcarry0 = " << carry0 << std::endl;
std::cout << "\tcarry1 = " << carry1 << std::endl;
std::cout << std::dec;
}
for( std::size_t i = 0; i < 16; i++){
allocate(A[i], i, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ namespace nil {
const std::vector<TYPE> &r_64_chunks
) {
return
(a_64_chunks[3] * b_64_chunks[1] + a_64_chunks[2] * b_64_chunks[2] +
(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] +
a_64_chunks[2] * b_64_chunks[1] + a_64_chunks[0] * b_64_chunks[3] - r_64_chunks[0]);
a_64_chunks[2] * b_64_chunks[1] + a_64_chunks[0] * b_64_chunks[3] - r_64_chunks[0]);
}

zkevm_mul_bbf(context_type &context_object, const opcode_input_type<FieldType, stage> &current_state):
Expand Down Expand Up @@ -139,18 +139,14 @@ namespace nil {
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::cout << "\tlo_carries = " << std::hex << lo_carries.data << std::dec << std::endl;
std::cout << "\thi_carries = " << std::hex << lo_carries.data << std::dec << std::endl;
integral_type c_first_i = typename FieldType::integral_type(lo_carries.data) >> 128;
auto c_first = w_to_16(zwordc(c_first_i));
integral_type c_second_i = typename FieldType::integral_type(hi_carries.data) >> 128;
integral_type c_second_i = (typename FieldType::integral_type(hi_carries.data) + c_first_i) >> 128;
auto c_second = w_to_16(zwordc(c_second_i));
C3[3] = c_first[15]; C3[2] = c_first[14]; C3[1] = c_first[13]; C3[0] = c_first[12];
C2 = c_first[11];
C1[3] = c_second[15]; C1[2] = c_second[14]; C1[1] = c_second[13]; C1[0] = c_second[12];
C0 = c_second[11];
std::cout << "\tC0 = " << C0 << std::endl;
std::cout << "\tC2 = " << C2<< std::endl;
}

TYPE lo_carries = lo_carryless_construct(A_64, B_64, R_64);
Expand All @@ -174,14 +170,14 @@ namespace nil {
constrain(C0 * (C0 - 1) * (C0 - 2) * (C0 - 3));

constrain(lo_carries - C3_64 * two_128 - C2 * two_192);
//constrain(hi_carries + C3_64 + C2 * two_64 + C1_64 * two_128 + C0_64 * two_192);
constrain(hi_carries + C3_64 + C2 * two_64 - C1_64 * two_128 - C0 * two_192);

auto A_128 = chunks16_to_chunks128<TYPE>(A);
auto B_128 = chunks16_to_chunks128<TYPE>(B);
auto R_128 = chunks16_to_chunks128<TYPE>(R);
if constexpr( stage == GenerationStage::CONSTRAINTS ){
constrain(current_state.pc_next() - current_state.pc(2) - 1); // PC transition
constrain(current_state.gas(1) - current_state.gas_next() - 3); // GAS transition
constrain(current_state.gas(1) - current_state.gas_next() - 5); // GAS transition
constrain(current_state.stack_size(1) - current_state.stack_size_next() - 1); // stack_size transition
constrain(current_state.memory_size(1) - current_state.memory_size_next()); // memory_size transition
constrain(current_state.rw_counter_next() - current_state.rw_counter(1) - 3); // rw_counter transition
Expand Down Expand Up @@ -225,6 +221,8 @@ namespace nil {
R_128.second
};
lookup(tmp, "zkevm_rw");
} else {
std::cout << "\tAssignment implemented" << std::endl;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,62 @@ namespace nil {
generic_component<FieldType,stage>(context_object, false)
{
// NOT checked by current test
// if constexpr( stage == GenerationStage::CONSTRAINTS ){
// constrain(current_state.pc_next() - current_state.pc(0) - 1); // PC transition
// constrain(current_state.gas(0) - current_state.gas_next() - 3); // GAS transition
// constrain(current_state.stack_size(0) - current_state.stack_size_next()); // stack_size transition
// constrain(current_state.memory_size(0) - current_state.memory_size_next()); // memory_size transition
// constrain(current_state.rw_counter_next() - current_state.rw_counter(0) - 2); // rw_counter transition
// }
std::vector<TYPE> A(16);
std::vector<TYPE> R(16);

if constexpr( stage == GenerationStage::ASSIGNMENT ){
auto a = w_to_16(current_state.stack_top());
for( std::size_t i = 0; i < 16; i++ ){
A[i] = a[i];
R[i] = 0xFFFF - a[i];
}
}
for( std::size_t i = 0; i < 16; i++ ){
allocate(A[i], i, 0);
allocate(R[i], i + 16, 0);
}
for( std::size_t i = 0; i < 16; i++ ){
constrain(R[i] + A[i] - 0xFFFF);
}

auto A_128 = chunks16_to_chunks128<TYPE>(A);
auto R_128 = chunks16_to_chunks128<TYPE>(R);
if constexpr( stage == GenerationStage::CONSTRAINTS ){
constrain(current_state.pc_next() - current_state.pc(0) - 1); // PC transition
constrain(current_state.gas(0) - current_state.gas_next() - 3); // GAS transition
constrain(current_state.stack_size(0) - current_state.stack_size_next()); // stack_size transition
constrain(current_state.memory_size(0) - current_state.memory_size_next()); // memory_size transition
constrain(current_state.rw_counter_next() - current_state.rw_counter(0) - 2); // rw_counter transition
std::vector<TYPE> tmp;
tmp = {
TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(1),
current_state.stack_size(1) - 1,
TYPE(0),// storage_key_hi
TYPE(0),// storage_key_lo
TYPE(0),// field
current_state.rw_counter(1),
TYPE(0),// is_write
A_128.first,
A_128.second
};
lookup(tmp, "zkevm_rw");
tmp = {
TYPE(rw_op_to_num(rw_operation_type::stack)),
current_state.call_id(1),
current_state.stack_size(1) - 1,
TYPE(0),// storage_key_hi
TYPE(0),// storage_key_lo
TYPE(0),// field
current_state.rw_counter(1) + 1,
TYPE(1),// is_write
R_128.first,
R_128.second
};
lookup(tmp, "zkevm_rw");
} else {
std::cout << "Assignment implemented" << std::endl;
}
}
};

Expand Down
3 changes: 3 additions & 0 deletions crypto3/libs/blueprint/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ set(ZKEVM_BBF_TESTS_FILES
"zkevm_bbf/opcodes/add_sub"
"zkevm_bbf/opcodes/div"
"zkevm_bbf/opcodes/mul"
"zkevm_bbf/opcodes/div"
"zkevm_bbf/opcodes/jumps"
"zkevm_bbf/opcodes/not"
)


Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"bytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063caf118f114610030575b600080fd5b61004361003e36600461007d565b610055565b60405190815260200160405180910390f35b600082600a816080375050608051600080549080610072836100ef565b919050555092915050565b6000806020838503121561009057600080fd5b823567ffffffffffffffff808211156100a857600080fd5b818501915085601f8301126100bc57600080fd5b8135818111156100cb57600080fd5b8660208285010111156100dd57600080fd5b60209290920196919550909350505050565b60006001820161010f57634e487b7160e01b600052601160045260246000fd5b506001019056fea264697066735822122023e3a5a966dc31aca0a0b4516074390a8fb3b9d48615d032488f404f1e6eb04764736f6c63430008190033"}
{"bytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063caf118f114610030575b600080fd5b61004361003e36600461007d565b610055565b60405190815260200160405180910390f35b600082600a816080375050608051600080549080610072836100ef565b919050555092915050565b6000806020838503121561009057600080fd5b823567ffffffffffffffff808211156100a857600080fd5b818501915085601f8301126100bc57600080fd5b8135818111156100cb57600080fd5b8660208285010111156100dd57600080fd5b60209290920196919550909350505050565b60006001820161010f57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"bytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c80631a737e4b14610030575b600080fd5b61004361003e36600461018a565b610055565b60405190815260200160405180910390f35b600061005f610154565b60005b60058160ff1610156100e757838160ff1660058110610083576100836101c0565b60200201602081019061009691906101d6565b82868360ff16600581106100ac576100ac6101c0565b6020020160208101906100bf91906101d6565b60ff16601e81106100d2576100d26101c0565b60ff9092166020929092020152600101610062565b5060005b601e8161ffff16101561013857818161ffff16601e811061010e5761010e6101c0565b60200201516101249060ff1661ffff8316610216565b61012e908461022d565b92506001016100eb565b5060008054908061014883610240565b91905055505092915050565b604051806103c00160405280601e906020820280368337509192915050565b8060a0810183101561018457600080fd5b92915050565b600080610140838503121561019e57600080fd5b6101a88484610173565b91506101b78460a08501610173565b90509250929050565b634e487b7160e01b600052603260045260246000fd5b6000602082840312156101e857600080fd5b813560ff811681146101f957600080fd5b9392505050565b634e487b7160e01b600052601160045260246000fd5b808202811582820484141761018457610184610200565b8082018082111561018457610184610200565b60006001820161025257610252610200565b506001019056fea2646970667358221220a48d2dc7e92e3f0586935a425ee013849eac2b26afb9943227b0ac3590fb050864736f6c63430008190033"}
{"bytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c80631a737e4b14610030575b600080fd5b61004361003e3660046101a1565b610055565b60405190815260200160405180910390f35b600061005f61016b565b60005b60058160ff1610156100f157838160ff1660058110610083576100836101d7565b60200201602081019061009691906101ed565b82868360ff16600581106100ac576100ac6101d7565b6020020160208101906100bf91906101ed565b60ff16601e81106100d2576100d26101d7565b60ff9092166020929092020152806100e98161022d565b915050610062565b5060005b601e8161ffff16101561014c57818161ffff16601e8110610118576101186101d7565b602002015161012e9060ff1661ffff831661024c565b6101389084610263565b92508061014481610276565b9150506100f5565b508160008082825461015e9190610263565b9091555091949350505050565b604051806103c00160405280601e906020820280368337509192915050565b8060a0810183101561019b57600080fd5b92915050565b60008061014083850312156101b557600080fd5b6101bf848461018a565b91506101ce8460a0850161018a565b90509250929050565b634e487b7160e01b600052603260045260246000fd5b6000602082840312156101ff57600080fd5b813560ff8116811461021057600080fd5b9392505050565b634e487b7160e01b600052601160045260246000fd5b600060ff821660ff810361024357610243610217565b60010192915050565b808202811582820484141761019b5761019b610217565b8082018082111561019b5761019b610217565b600061ffff80831681810361028d5761028d610217565b600101939250505056"}

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions crypto3/libs/blueprint/test/zkevm_bbf/l1_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_mstore8_test) {

BOOST_AUTO_TEST_CASE(blueprint_plonk_meminit_test) {
using field_type = typename algebra::curves::pallas::base_field_type;
auto [bytecodes, pts] = load_hardhat_input("../crypto3/libs/blueprint/test/zkevm/data/meminit/");
auto [bytecodes, pts] = load_hardhat_input("../crypto3/libs/blueprint/test/zkevm/data/mem_init/");
l1_size_restrictions max_sizes;

max_sizes.max_keccak_blocks = 50;
max_sizes.max_bytecode = 3000;
max_sizes.max_mpt = 0;
max_sizes.max_rw = 5000;
max_sizes.max_rw = 10000;
max_sizes.max_copy = 3000;
max_sizes.max_zkevm_rows = 4500;
max_sizes.max_zkevm_rows = 10000;

complex_test<field_type>(bytecodes, pts, max_sizes);
}
Expand Down
Loading

0 comments on commit 189119a

Please sign in to comment.