Skip to content

Commit

Permalink
cpp: start zkinterface_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
naure committed Jun 14, 2020
1 parent 0a01eea commit 80ee5d1
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 65 deletions.
4 changes: 2 additions & 2 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enable_testing()


# Frontend gadgetlib.
add_executable(gadgetlib gadgetlib.cpp libsnark_integration.cpp gadgetlib_example.cpp)
add_executable(gadgetlib gadgetlib.cpp zkinterface_utils.cpp libsnark_integration.cpp gadgetlib_example.cpp)
# Dependencies.
target_link_libraries(gadgetlib ff gmp)
target_include_directories(gadgetlib PRIVATE . libsnark libsnark/depends/libff libsnark/depends/libfqfft /usr/local/include)
Expand All @@ -24,7 +24,7 @@ target_compile_definitions(gadgetlib PRIVATE -DBINARY_OUTPUT -DCURVE_ALT_BN128 -


# Backend.
add_executable(backend backend.cpp libsnark_integration.cpp)
add_executable(backend backend.cpp zkinterface_utils.cpp libsnark_integration.cpp)
add_test(backend backend)
# Dependencies.
target_link_libraries(backend ff gmp)
Expand Down
25 changes: 14 additions & 11 deletions cpp/gadgetlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bool write_to_file(void *context, unsigned char *message) {
return true;
}

FlatBufferBuilder make_input_circuit() {
void make_input_circuit(vector<uint8_t> &output) {
FlatBufferBuilder builder;

auto connections = CreateVariables(
Expand All @@ -32,34 +32,37 @@ FlatBufferBuilder make_input_circuit() {

auto root = CreateRoot(builder, Message_Circuit, circuit.Union());
builder.FinishSizePrefixed(root);
return builder;

// Append to the output buffer.
uint8_t *begin = builder.GetBufferPointer();
output.insert(output.end(), begin, begin + builder.GetSize());
}

FlatBufferBuilder make_command(string action) {
void make_command(vector<uint8_t> &output, string action) {
bool constraints_generation = (action == "constraints");
bool witness_generation = (action == "witness");

FlatBufferBuilder builder;
auto command = CreateCommand(builder, constraints_generation, witness_generation);
auto root = CreateRoot(builder, Message_Command, command.Union());
builder.FinishSizePrefixed(root);
return builder;

// Append to the output buffer.
uint8_t *begin = builder.GetBufferPointer();
output.insert(output.end(), begin, begin + builder.GetSize());
}

void run(string action, string zkif_out_path) {

auto circuit_builder = make_input_circuit();
auto circuit_msg = circuit_builder.GetBufferPointer();

auto command_build = make_command(action);
auto command_msg = command_build.GetBufferPointer();
vector<uint8_t> buf;
make_input_circuit(buf);
make_command(buf, action);

string constraints_name = "out_constraints.zkif";
string witness_name = "out_witness.zkif";
string return_name = "out_return.zkif";

gadgetlib_example::call_gadget_example(
circuit_msg,
buf.data(),
write_to_file, &constraints_name,
write_to_file, &witness_name,
write_to_file, &return_name);
Expand Down
31 changes: 0 additions & 31 deletions cpp/libsnark_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,6 @@

namespace zkinterface_libsnark {

// ==== Reading helpers ====

uoffset_t read_size_prefix(void *buffer) {
uoffset_t message_length = *reinterpret_cast<uoffset_t *>(buffer);
return sizeof(uoffset_t) + message_length;
}

const Root *find_message(vector<char> &buffer, Message type) {
auto offset = 0;

while (offset + sizeof(uoffset_t) * 2 <= buffer.size()) {
auto current = buffer.data() + offset;

auto size = read_size_prefix(current);
if (offset + size > buffer.size()) {
throw "invalid offset";
}

auto root = GetSizePrefixedRoot(current);

if (root->message_type() == type) {
return root; // Found.
}

offset += size;
}

throw MessageNotFoundException();
}


// ==== Element conversion helpers ====

// Bytes to Bigint. Little-Endian.
Expand Down
22 changes: 5 additions & 17 deletions cpp/libsnark_integration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#ifndef ZKIF_LIBSNARK_INTEGRATION_H_
#define ZKIF_LIBSNARK_INTEGRATION_H_

#include "zkinterface.h"
#include "zkinterface_generated.h"

#include "libsnark/gadgetlib1/gadget.hpp"
#include "libff/common/default_types/ec_pp.hpp"

#include "zkinterface.h"
#include "zkinterface_generated.h"
#include "zkinterface_utils.hpp"

using namespace zkinterface;
using flatbuffers::FlatBufferBuilder;
using flatbuffers::uoffset_t;
Expand All @@ -24,6 +25,7 @@ using libff::bigint;
using libff::bit_vector;

namespace zkinterface_libsnark {
using namespace zkinterface_utils;

typedef libff::default_ec_pp CurveT;
typedef libff::Fr<CurveT> FieldT;
Expand All @@ -46,20 +48,6 @@ namespace zkinterface_libsnark {
};


// ==== Reading helpers ====

uoffset_t read_size_prefix(void *buffer);

class MessageNotFoundException : public std::exception {
public:
inline const char *what() const throw() {
return "message of the required type not found";
}
};

const Root *find_message(vector<char> &buffer, Message type);


// ==== Element conversion helpers ====

// Bytes to Bigint. Little-Endian.
Expand Down
8 changes: 4 additions & 4 deletions cpp/libsnark_zkif_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace zkinterface_libsnark {
import_zkif(protoboard<FieldT> &pb, const std::string &annotation_prefix) :
gadget<FieldT>(pb, annotation_prefix) {}

protoboard<FieldT>* get_pb() { return &pb; }
protoboard<FieldT> *get_pb() { return &pb; }

void load(vector<char> &buf) {
buffer = buf;
Expand All @@ -33,9 +33,9 @@ namespace zkinterface_libsnark {
return root->message_as_Circuit();
}

const R1CSConstraints *get_constraints() {
auto root = find_message(buffer, Message_R1CSConstraints);
return root->message_as_R1CSConstraints();
const ConstraintSystem *get_constraints() {
auto root = find_message(buffer, Message_ConstraintSystem);
return root->message_as_ConstraintSystem();
}

const Witness *get_witness() {
Expand Down
37 changes: 37 additions & 0 deletions cpp/zkinterface_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// zkInterface - integration helpers.
//
// @author Aurélien Nicolas <info@nau.re> for QED-it.com
// @date 2020

#include "zkinterface_utils.hpp"

namespace zkinterface_utils {

uoffset_t read_size_prefix(void *buffer) {
uoffset_t message_length = *reinterpret_cast<uoffset_t *>(buffer);
return sizeof(uoffset_t) + message_length;
}

const Root *find_message(vector<char> &buffer, Message type) {
auto offset = 0;

while (offset + sizeof(uoffset_t) * 2 <= buffer.size()) {
auto current = buffer.data() + offset;

auto size = read_size_prefix(current);
if (offset + size > buffer.size()) {
throw "invalid offset";
}

auto root = GetSizePrefixedRoot(current);

if (root->message_type() == type) {
return root; // Found.
}

offset += size;
}

throw MessageNotFoundException();
}
} // namespace zkinterface_utils
31 changes: 31 additions & 0 deletions cpp/zkinterface_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// zkInterface - integration helpers.
//
// @author Aurélien Nicolas <info@nau.re> for QED-it.com
// @date 2020

#ifndef ZKINTERFACE_ZKINTERFACE_UTILS_HPP
#define ZKINTERFACE_ZKINTERFACE_UTILS_HPP

#include "zkinterface_generated.h"

namespace zkinterface_utils {
using namespace std;
using namespace flatbuffers;
using namespace zkinterface;


// ==== Reading helpers ====

uoffset_t read_size_prefix(void *buffer);

const Root *find_message(vector<char> &buffer, Message type);

class MessageNotFoundException : public std::exception {
public:
inline const char *what() const throw() {
return "message of the required type not found";
}
};

}
#endif //ZKINTERFACE_ZKINTERFACE_UTILS_HPP

0 comments on commit 80ee5d1

Please sign in to comment.