Skip to content

Commit

Permalink
Merge pull request #16 from QED-it/libsnark-e2e
Browse files Browse the repository at this point in the history
libsnark-e2e: End-to-end Rust/C++ gadget and proving flow
  • Loading branch information
naure committed Jul 26, 2020
2 parents e35f58b + cf8ba64 commit 83643dd
Show file tree
Hide file tree
Showing 37 changed files with 1,236 additions and 422 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
target/
build/
dist/
libsnark
*.a
local/
Expand Down
26 changes: 26 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
# Version v1.1.3, 2020-07, Rust/C++/libSNARK end-to-end integration

Rust:
- *(breaking)* Expanded `*Owned` structures (`KeyValueOwned`, rename `write_into`, serialization of `ConstraintSystemOwned`).
- *(breaking)* Generic code improvements (Rust 2018, common `Result`, naming, modules re-exports).
- Consolidated all tools into one CLI called `zkif`.
- Available commands:
- JSON converters (`json` and `pretty`).
- Human-readable decoder called `explain`.
- Example circuit generator.
- Statistics for cost estimation (`stats`).
- Support for byte streams to pipe with other CLIs (`cat` command and special `-` filename meaning stdin/stdout).
- Established a convention for easy file-based usage. The tools accept a workspace directory where `.zkif` files are organized.
- Structures to construct ZK statements. See `statement.rs / StatementBuilder`.
- A trait `GadgetCallbacks` and basic implementations to interact with gadgets (e.g. libSNARK below).
- Fixed a bug on empty variables array.

libsnark-rust:
- Support zero-copy mode using the generic `GadgetCallbacks` trait.
- Tests with a TinyRAM gadget and `StatementBuilder`.

C++:
- Support for multiple messages of each type.
- Simplify the `zkif_snark` CLI and work well with the workspace and pipe modes of the `zkif` CLI.


# Version v1.1.2, 2020-06, libsnark and gadgetlib for Rust

Rust:
Expand Down
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,10 @@ In the `rust` directory, run unit tests:
The following commands will generate and print a file containing the messages Circuit, R1CSConstraints, and Witness for a toy circuit in `rust/src/examples.rs`:

```
cargo run --bin example > example.zkif
cargo run --bin print < example.zkif
cargo run example > example.zkif
cargo run explain < example.zkif
```

This command will generate and compile Rust and C++ code, and run a test of both sides communicating through the standard interface:

`cargo test --features cpp`

<!-- TODO: Clarify what this tests -->

### Generated code

Generated C++ and Rust code is included.
Expand Down
30 changes: 30 additions & 0 deletions cpp/e2e_demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh
set -ex

# Build.
cmake -DCMAKE_INSTALL_PREFIX=dist . && make install

# Find the tools.
alias zkif="cargo run --manifest-path ../rust/Cargo.toml"
alias zkif_snark="dist/bin/zkif_snark"

# Generate a test statement.
pushd libsnark-rust
ZKINTERFACE_LIBSNARK_PATH=../dist cargo test
popd

# Look at the files.
DIR=libsnark-rust/local/test_statement
MAIN=$DIR/main.zkif
CONS=$DIR/constraints.zkif
WITN=$DIR/witness.zkif
NAME=$DIR/snark

zkif explain $MAIN $CONS $WITN

# Prove and verify.
cat $MAIN $CONS $WITN | zkif_snark validate $NAME
cat $MAIN $CONS | zkif_snark setup $NAME
cat $MAIN $WITN | zkif_snark prove $NAME
cat $MAIN | zkif_snark verify $NAME

14 changes: 8 additions & 6 deletions cpp/gadget_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ void run(string action, string zkif_out_prefix) {
vector<char> command_msg;
make_command(command_msg, action);

string constraints_name = zkif_out_prefix + "_constraints.zkif";
string witness_name = zkif_out_prefix + "_witness.zkif";
string return_name = zkif_out_prefix + "_return.zkif";
string constraints_name = zkif_out_prefix + "constraints.zkif";
string witness_name = zkif_out_prefix + "witness.zkif";
string response_name = zkif_out_prefix + "response.zkif";

gadgetlib_call_gadget(circuit_msg.data(), command_msg.data(),
callback_write_to_file, &constraints_name,
callback_write_to_file, &witness_name,
callback_write_to_file, &return_name);
callback_write_to_file, &response_name);
}

static const char USAGE[] =
Expand All @@ -88,13 +88,15 @@ static const char USAGE[] =

int main(int argc, const char **argv) {

if (argc < 3) {
if (argc < 2) {
cerr << USAGE << endl;
return 1;
}

string out_prefix = (argc > 2) ? string(argv[2]) : "";

try {
run(string(argv[1]), string(argv[2]));
run(string(argv[1]), out_prefix);
return 0;
} catch (const char *msg) {
cerr << msg << endl;
Expand Down
16 changes: 10 additions & 6 deletions cpp/gadgetlib_alu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,27 @@ namespace gadgetlib_alu {

// Gadget output.
{
FlatBufferBuilder builder;
VarIdConverter converter(circuit);

vector<uint64_t> output_ids({
converter.get_variable_id(destval),
converter.get_variable_id(flag),
});
auto output_values = elements_into_le({
pb.val(destval),
pb.val(flag),
});

FlatBufferBuilder builder;
Offset<Vector<unsigned char>> output_values;
if (command->witness_generation()) {
output_values = builder.CreateVector(
elements_into_le({
pb.val(destval),
pb.val(flag),
}));
}

auto connections = CreateVariables(
builder,
builder.CreateVector(output_ids),
builder.CreateVector(output_values));
output_values);

auto response = CreateCircuit(
builder,
Expand Down
6 changes: 3 additions & 3 deletions cpp/libsnark-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cpp/libsnark-rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zkinterface-libsnark"
version = "1.1.2"
version = "1.1.3"
description = "Wrapper for libsnark / gadgetlib through zkInterface"
homepage = "https://github.com/QED-it/zkinterface"
repository = "https://github.com/QED-it/zkinterface/tree/master/cpp/libsnark-rust"
Expand All @@ -11,7 +11,7 @@ build = "build.rs"
links = "zkif_gadgetlib"

[dependencies]
zkinterface = "1.1.2"
zkinterface = "1.1.3"
#zkinterface = { path = "../../rust" }

[build-dependencies]
Expand Down
Empty file added cpp/libsnark-rust/local/.keep
Empty file.
Loading

0 comments on commit 83643dd

Please sign in to comment.