Skip to content

Commit

Permalink
New memory tests and copy circuit #378
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Jul 10, 2024
1 parent a3b6a52 commit 7264521
Show file tree
Hide file tree
Showing 11 changed files with 1,406 additions and 56 deletions.
388 changes: 388 additions & 0 deletions include/nil/blueprint/zkevm/copy.hpp

Large diffs are not rendered by default.

739 changes: 739 additions & 0 deletions include/nil/blueprint/zkevm/copy_event.hpp

Large diffs are not rendered by default.

68 changes: 16 additions & 52 deletions include/nil/blueprint/zkevm/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include <nil/blueprint/zkevm/util/ptree.hpp>

namespace nil {
namespace blueprint {
constexpr std::uint8_t START_OP = 0;
Expand Down Expand Up @@ -137,59 +139,12 @@ namespace nil {
std::vector<rw_operation> rw_ops;
std::size_t call_id;

std::uint8_t char_to_hex(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return 0;
}

zkevm_word_type zkevm_word_from_string(std::string val){
zkevm_word_type result;
for(std::size_t i = 0; i < val.size(); i++ ){
result *= 16;
result += char_to_hex(val[i]);
}
return result;
}

std::vector<zkevm_word_type> zkevm_word_vector_from_ptree(const boost::property_tree::ptree &ptree){
std::vector<zkevm_word_type> result;
for(auto it = ptree.begin(); it != ptree.end(); it++){
result.push_back(zkevm_word_from_string(it->second.data()));
}
return result;
}

std::map<zkevm_word_type, zkevm_word_type> key_value_storage_from_ptree(const boost::property_tree::ptree &ptree){
std::map<zkevm_word_type, zkevm_word_type> result;
// std::cout << "Storage:" << std::endl;
for(auto it = ptree.begin(); it != ptree.end(); it++){
result[zkevm_word_from_string(it->first.data())] = zkevm_word_from_string(it->second.data());
// std::cout << "\t" << it->first.data() << "=>" << it->second.data() << std::endl;
}
return result;
}

std::vector<std::uint8_t> byte_vector_from_ptree(const boost::property_tree::ptree &ptree){
std::vector<std::uint8_t> result;
// std::cout << "MEMORY words " << ptree.size() << ":";
for(auto it = ptree.begin(); it != ptree.end(); it++){
for(std::size_t i = 0; i < it->second.data().size(); i+=2){
std::uint8_t byte = char_to_hex(it->second.data()[i]) * 16 + char_to_hex(it->second.data()[i+1]);
// std::cout << std::hex << std::setw(2) << std::setfill('0') << std::size_t(byte) << " ";
result.push_back(byte);
}
}
// std::cout << std::endl;
return result;
}

void append_opcode(
std::string opcode,
const std::vector<zkevm_word_type> &stack, // Stack state before operation
const std::vector<zkevm_word_type> &stack_next, // stack state after operation. We need it for correct PUSH and correct SLOAD
const std::vector<uint8_t> &memory , // Memory state before operation in bytes format
const std::vector<uint8_t> &memory_next , // Memory state before operation in bytes format
const std::map<zkevm_word_type, zkevm_word_type> &storage,// Storage state before operation
const std::map<zkevm_word_type, zkevm_word_type> &storage_next// Storage state before operation
){
Expand Down Expand Up @@ -455,13 +410,20 @@ namespace nil {
} else if(opcode == "CALLDATACOPY") {
// 0x37
std::cout << "Test me, please!" << std::endl;
//exit(2);
rw_ops.push_back(stack_operation(call_id, stack.size()-3, rw_ops.size(), false, stack[stack.size()-3]));
std::cout << "\t" << rw_ops[rw_ops.size()-1] << std::endl;
rw_ops.push_back(stack_operation(call_id, stack.size()-2, rw_ops.size(), false, stack[stack.size()-2]));
std::cout << "\t" << rw_ops[rw_ops.size()-1] << std::endl;
rw_ops.push_back(stack_operation(call_id, stack.size()-1, rw_ops.size(), false, stack[stack.size()-1]));
std::cout << "\t" << rw_ops[rw_ops.size()-1] << std::endl;
std::size_t length = std::size_t(stack[stack.size()-3]);
auto dest = stack[stack.size()-1];
std::cout << "Length = " << length << std::endl;
std::cout << "Memory_size " << memory.size() << "=>" << memory_next.size() << std::endl;
for( std::size_t i = 0; i < length; i++){
rw_ops.push_back(memory_operation(call_id, dest+i, rw_ops.size(), true, memory_next[std::size_t(dest+i)]));
std::cout << "\t" << rw_ops[rw_ops.size() - 1] << std::endl;
}
// TODO: add length read operations to calldata
// TODO: add length write operations to memory
} else if(opcode == "CODESIZE") {
Expand Down Expand Up @@ -1302,22 +1264,24 @@ namespace nil {

std::vector<zkevm_word_type> stack = zkevm_word_vector_from_ptree(ptrace.begin()->second.get_child("stack"));
std::vector<std::uint8_t> memory = byte_vector_from_ptree(ptrace.begin()->second.get_child("memory"));
std::vector<std::uint8_t> memory_next;
std::vector<zkevm_word_type> stack_next;
std::map<zkevm_word_type, zkevm_word_type> storage = key_value_storage_from_ptree(ptrace.begin()->second.get_child("storage"));
std::map<zkevm_word_type, zkevm_word_type> storage_next;

rw_ops.push_back(start_operation());
for( auto it = ptrace.begin(); it!=ptrace.end(); it++ ){
if(std::distance(it, ptrace.end()) == 1)
append_opcode(it->second.get_child("op").data(), stack, {}, memory, storage, storage);
append_opcode(it->second.get_child("op").data(), stack, {}, memory, {}, storage, storage);
else{
stack_next = zkevm_word_vector_from_ptree(std::next(it)->second.get_child("stack"));
memory_next = byte_vector_from_ptree(std::next(it)->second.get_child("memory"));
storage_next = key_value_storage_from_ptree(it->second.get_child("storage"));
append_opcode(it->second.get_child("op").data(), stack, stack_next, memory, storage, storage_next);
memory = byte_vector_from_ptree(std::next(it)->second.get_child("memory"));
append_opcode(it->second.get_child("op").data(), stack, stack_next, memory, memory_next, storage, storage_next);
}
storage = storage_next;
stack = stack_next;
memory = memory_next;
}
std::sort(rw_ops.begin(), rw_ops.end(), [](rw_operation a, rw_operation b){
return a < b;
Expand Down
5 changes: 3 additions & 2 deletions include/nil/blueprint/zkevm/rw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace nil {
static constexpr std::size_t VALUE_HI = 8;
static constexpr std::size_t VALUE_LO = 9;

// Advice columns
static constexpr std::size_t OP_SELECTORS_AMOUNT = 4; // index \in {0..31}
static constexpr std::array<std::size_t, OP_SELECTORS_AMOUNT> OP_SELECTORS = {10, 11, 12, 13};

Expand Down Expand Up @@ -109,7 +110,7 @@ namespace nil {
}
};

static gate_manifest get_gate_manifest(std::size_t witness_amount, std::size_t max_rw_size = 5000) {
static gate_manifest get_gate_manifest(std::size_t witness_amount, std::size_t max_rw_size= 10000) {
gate_manifest manifest = gate_manifest(gate_manifest_type());
return manifest;
}
Expand All @@ -122,7 +123,7 @@ namespace nil {
return manifest;
}

constexpr static std::size_t get_rows_amount(std::size_t witness_amount, std::size_t max_rw_size = 5000) {
constexpr static std::size_t get_rows_amount(std::size_t witness_amount, std::size_t max_rw_size= 10000) {
return max_rw_size;
}

Expand Down
66 changes: 66 additions & 0 deletions include/nil/blueprint/zkevm/util/ptree.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Elena Tatuzova <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#pragma once

#include <nil/blueprint/zkevm/zkevm_word.hpp>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

namespace nil {
namespace blueprint {
std::vector<zkevm_word_type> zkevm_word_vector_from_ptree(const boost::property_tree::ptree &ptree){
std::vector<zkevm_word_type> result;
for(auto it = ptree.begin(); it != ptree.end(); it++){
result.push_back(zkevm_word_from_string(it->second.data()));
}
return result;
}

std::map<zkevm_word_type, zkevm_word_type> key_value_storage_from_ptree(const boost::property_tree::ptree &ptree){
std::map<zkevm_word_type, zkevm_word_type> result;
// std::cout << "Storage:" << std::endl;
for(auto it = ptree.begin(); it != ptree.end(); it++){
result[zkevm_word_from_string(it->first.data())] = zkevm_word_from_string(it->second.data());
// std::cout << "\t" << it->first.data() << "=>" << it->second.data() << std::endl;
}
return result;
}

std::vector<std::uint8_t> byte_vector_from_ptree(const boost::property_tree::ptree &ptree){
std::vector<std::uint8_t> result;
// std::cout << "MEMORY words " << ptree.size() << ":";
for(auto it = ptree.begin(); it != ptree.end(); it++){
for(std::size_t i = 0; i < it->second.data().size(); i+=2){
std::uint8_t byte = char_to_hex(it->second.data()[i]) * 16 + char_to_hex(it->second.data()[i+1]);
// std::cout << std::hex << std::setw(2) << std::setfill('0') << std::size_t(byte) << " ";
result.push_back(byte);
}
}
// std::cout << std::endl;
return result;
}
}
}
16 changes: 16 additions & 0 deletions include/nil/blueprint/zkevm/zkevm_word.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,21 @@ namespace nil {
}
return chunks;
}

std::uint8_t char_to_hex(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return 0;
}

zkevm_word_type zkevm_word_from_string(std::string val){
zkevm_word_type result;
for(std::size_t i = 0; i < val.size(); i++ ){
result *= 16;
result += char_to_hex(val[i]);
}
return result;
}
} // namespace blueprint
} // namespace nil
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ set(PLONK_TESTS_FILES
"verifiers/placeholder/verifier"
"zkevm/bytecode"
"zkevm/rw"
"zkevm/copy"
)

set(FIELDS_TESTS_FILES
Expand Down
Loading

0 comments on commit 7264521

Please sign in to comment.