Skip to content

Commit

Permalink
Merge pull request #201 from clearmatics/api-refactor
Browse files Browse the repository at this point in the history
[WIP] Api refactor
  • Loading branch information
AntoineRondelet committed Apr 22, 2020
2 parents 5d11215 + 885896a commit edf32bd
Show file tree
Hide file tree
Showing 29 changed files with 577 additions and 267 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
# Set the target of the Protobuf and gRPC generated files.
set(
PROTO_SRC_DIR
${PROJECT_BINARY_DIR}/proto_src
${PROJECT_BINARY_DIR}
)
file(MAKE_DIRECTORY ${PROTO_SRC_DIR})
#file(MAKE_DIRECTORY ${PROTO_SRC_DIR})

# Get the proto files
file(
Expand All @@ -216,8 +216,8 @@ file(
set(PROTOBUF_IMPORT_DIRS ${PROJECT_SOURCE_DIR})
set(PROTOBUF_PROTO_PATH ${PROJECT_SOURCE_DIR})
set(PROTOBUF_APPEND_DEST_PATH "/api")
set(PROTOBUF_GENERATE_CPP_APPEND_PATH FALSE)
set(GRPC_GENERATE_CPP_APPEND_PATH FALSE)
set(PROTOBUF_GENERATE_CPP_APPEND_PATH OFF)
set(GRPC_GENERATE_CPP_APPEND_PATH OFF)

# Add sub-directories for the build
add_subdirectory(depends)
Expand Down
21 changes: 1 addition & 20 deletions api/util.proto → api/ec_group_messages.proto
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
syntax = "proto3";

package prover_proto;

message ZethNote {
string apk = 1;
// Hex string representing a int64 value
string value = 2;
string rho = 3;
string trap_r = 4;
}

message JoinsplitInput {
// Merkle authentication path to the commitment
// of the note in the Merkle tree. Each node of
// the merkle tree is treated as a string
repeated string merkle_path = 1;
int64 address = 2;
ZethNote note = 3;
string spending_ask = 4;
string nullifier = 5;
}
package zeth_proto;

// Every point coordinate (ie: base field element)
// is treated as an hexadecimal string.
Expand Down
4 changes: 2 additions & 2 deletions api/groth16_messages.proto
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
syntax = "proto3";

package prover_proto;
package zeth_proto;

import public "api/util.proto";
import "api/ec_group_messages.proto";

message VerificationKeyGROTH16 {
HexPointBaseGroup1Affine alpha_g1 = 1;
Expand Down
4 changes: 2 additions & 2 deletions api/pghr13_messages.proto
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
syntax = "proto3";

package prover_proto;
package zeth_proto;

import public "api/util.proto";
import "api/ec_group_messages.proto";

message VerificationKeyPGHR13 {
HexPointBaseGroup2Affine a = 1;
Expand Down
40 changes: 6 additions & 34 deletions api/prover.proto
Original file line number Diff line number Diff line change
@@ -1,44 +1,16 @@
syntax = "proto3";

package prover_proto;
package zeth_proto;

import "google/protobuf/empty.proto";
import "api/pghr13_messages.proto";
import "api/groth16_messages.proto";

import "api/zeth_messages.proto";
import "api/snark_messages.proto";

service Prover {
// Fetch the verification key from the proving service
// Fetch the verification key from the prover server
rpc GetVerificationKey(google.protobuf.Empty) returns (VerificationKey) {}

// Request a proof generation on the given input
// Request a proof generation on the given inputs
rpc Prove(ProofInputs) returns (ExtendedProof) {}
}

// Inputs of the Prove function of the Prover service
message ProofInputs {
string mk_root = 1;
// List of inputs to the Joinsplit
repeated JoinsplitInput js_inputs = 2;
// List of output to the Joinsplit
repeated ZethNote js_outputs = 3;
// Hexadecimal string representing a uint64 value
string pub_in_value = 4;
// Hexadecimal string representing a uint64 value
string pub_out_value = 5;
string h_sig = 6;
string phi = 7;
}

message VerificationKey {
oneof VK {
VerificationKeyPGHR13 pghr13_verification_key = 1;
VerificationKeyGROTH16 groth16_verification_key = 2;
}
}

message ExtendedProof {
oneof EP {
ExtendedProofPGHR13 pghr13_extended_proof = 1;
ExtendedProofGROTH16 groth16_extended_proof = 2;
}
}
20 changes: 20 additions & 0 deletions api/snark_messages.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";

package zeth_proto;

import "api/pghr13_messages.proto";
import "api/groth16_messages.proto";

message VerificationKey {
oneof VK {
VerificationKeyPGHR13 pghr13_verification_key = 1;
VerificationKeyGROTH16 groth16_verification_key = 2;
}
}

message ExtendedProof {
oneof EP {
ExtendedProofPGHR13 pghr13_extended_proof = 1;
ExtendedProofGROTH16 groth16_extended_proof = 2;
}
}
36 changes: 36 additions & 0 deletions api/zeth_messages.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
syntax = "proto3";

package zeth_proto;

message ZethNote {
string apk = 1;
// Hex string representing a int64 value
string value = 2;
string rho = 3;
string trap_r = 4;
}

message JoinsplitInput {
// Merkle authentication path to the commitment
// of the note in the Merkle tree. Each node of
// the merkle tree is treated as a string
repeated string merkle_path = 1;
int64 address = 2;
ZethNote note = 3;
string spending_ask = 4;
string nullifier = 5;
}

message ProofInputs {
string mk_root = 1;
// List of inputs to the Joinsplit
repeated JoinsplitInput js_inputs = 2;
// List of output to the Joinsplit
repeated ZethNote js_outputs = 3;
// Hexadecimal string representing a uint64 value
string pub_in_value = 4;
// Hexadecimal string representing a uint64 value
string pub_out_value = 5;
string h_sig = 6;
string phi = 7;
}
8 changes: 7 additions & 1 deletion libzeth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include_directories(${PROTO_SRC_DIR})
# Generate the protobuf files and set the result of the generation
# in the given env var (PROTO_SRCS, PROTO_HDRS).
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_SRC_DIR} ${PROTO_FILES})
grpc_generate_cpp(GRPC_SRCS GRPC_HDRS ${PROTO_SRC_DIR} ${PROTO_FILES})

set_property(SOURCE ${PROTO_SRCS} PROPERTY
COMPILE_FLAGS "-Wno-unused-variable -Wno-unused-parameter"
Expand Down Expand Up @@ -40,7 +41,10 @@ file(
circuit_wrapper.???
commitments/**.?pp commitments/**.tcc
libsnark_helpers/**.?pp libsnark_helpers/**.tcc
snarks/**.hpp snarks/**.tcc snarks/${ZKSNARK_NAME}/**.cpp
snarks/${ZKSNARK_NAME}/core/**.???
snarks/${ZKSNARK_NAME}/api/**.???
# We only implement the MPC for Groth16 for now
snarks/groth16/mpc/**.???
snarks_alias.hpp
include_libsnark.hpp
util.?pp util.tcc
Expand All @@ -52,6 +56,8 @@ add_library(

${ZETH_SOURCE}
${PROTO_SRCS}
${PROTO_HDRS}
${GRPC_SRCS}
)
target_include_directories(
zeth
Expand Down
6 changes: 3 additions & 3 deletions libzeth/snarks/groth16/api/response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef __ZETH_RESPONSE_HPP__
#define __ZETH_RESPONSE_HPP__

#include "api/prover.grpc.pb.h"
#include "api/snark_messages.grpc.pb.h"
#include "libzeth/libsnark_helpers/extended_proof.hpp"
#include "libzeth/util_api.hpp"

Expand All @@ -14,11 +14,11 @@ namespace libzeth

template<typename ppT>
void prepare_proof_response(
extended_proof<ppT> &ext_proof, prover_proto::ExtendedProof *message);
extended_proof<ppT> &ext_proof, zeth_proto::ExtendedProof *message);
template<typename ppT>
void prepare_verification_key_response(
libsnark::r1cs_gg_ppzksnark_verification_key<ppT> &vk,
prover_proto::VerificationKey *message);
zeth_proto::VerificationKey *message);

} // namespace libzeth
#include "libzeth/snarks/groth16/api/response.tcc"
Expand Down
32 changes: 16 additions & 16 deletions libzeth/snarks/groth16/api/response.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ namespace libzeth

template<typename ppT>
void prepare_proof_response(
extended_proof<ppT> &ext_proof, prover_proto::ExtendedProof *message)
extended_proof<ppT> &ext_proof, zeth_proto::ExtendedProof *message)
{
libsnark::r1cs_gg_ppzksnark_proof<ppT> proof_obj = ext_proof.get_proof();

prover_proto::HexPointBaseGroup1Affine *a =
new prover_proto::HexPointBaseGroup1Affine();
prover_proto::HexPointBaseGroup2Affine *b =
new prover_proto::HexPointBaseGroup2Affine(); // in G2
prover_proto::HexPointBaseGroup1Affine *c =
new prover_proto::HexPointBaseGroup1Affine();
zeth_proto::HexPointBaseGroup1Affine *a =
new zeth_proto::HexPointBaseGroup1Affine();
zeth_proto::HexPointBaseGroup2Affine *b =
new zeth_proto::HexPointBaseGroup2Affine(); // in G2
zeth_proto::HexPointBaseGroup1Affine *c =
new zeth_proto::HexPointBaseGroup1Affine();

a->CopyFrom(format_hexPointBaseGroup1Affine<ppT>(proof_obj.g_A));
b->CopyFrom(format_hexPointBaseGroup2Affine<ppT>(proof_obj.g_B)); // in G2
Expand All @@ -34,7 +34,7 @@ void prepare_proof_response(
// Note on memory safety: set_allocated deleted the allocated objects
// See:
// https://stackoverflow.com/questions/33960999/protobuf-will-set-allocated-delete-the-allocated-object
prover_proto::ExtendedProofGROTH16 *grpc_extended_groth16_proof_obj =
zeth_proto::ExtendedProofGROTH16 *grpc_extended_groth16_proof_obj =
message->mutable_groth16_extended_proof();

grpc_extended_groth16_proof_obj->set_allocated_a(a);
Expand All @@ -46,14 +46,14 @@ void prepare_proof_response(
template<typename ppT>
void prepare_verification_key_response(
libsnark::r1cs_gg_ppzksnark_verification_key<ppT> &vk,
prover_proto::VerificationKey *message)
zeth_proto::VerificationKey *message)
{
prover_proto::HexPointBaseGroup1Affine *a =
new prover_proto::HexPointBaseGroup1Affine(); // in G1
prover_proto::HexPointBaseGroup2Affine *b =
new prover_proto::HexPointBaseGroup2Affine(); // in G2
prover_proto::HexPointBaseGroup2Affine *d =
new prover_proto::HexPointBaseGroup2Affine(); // in G2
zeth_proto::HexPointBaseGroup1Affine *a =
new zeth_proto::HexPointBaseGroup1Affine(); // in G1
zeth_proto::HexPointBaseGroup2Affine *b =
new zeth_proto::HexPointBaseGroup2Affine(); // in G2
zeth_proto::HexPointBaseGroup2Affine *d =
new zeth_proto::HexPointBaseGroup2Affine(); // in G2

a->CopyFrom(format_hexPointBaseGroup1Affine<ppT>(vk.alpha_g1)); // in G1
b->CopyFrom(format_hexPointBaseGroup2Affine<ppT>(vk.beta_g2)); // in G2
Expand All @@ -73,7 +73,7 @@ void prepare_verification_key_response(
// Note on memory safety: set_allocated deleted the allocated objects
// See:
// https://stackoverflow.com/questions/33960999/protobuf-will-set-allocated-delete-the-allocated-object
prover_proto::VerificationKeyGROTH16 *grpc_verification_key_groth16 =
zeth_proto::VerificationKeyGROTH16 *grpc_verification_key_groth16 =
message->mutable_groth16_verification_key();

grpc_verification_key_groth16->set_allocated_alpha_g1(a);
Expand Down
6 changes: 3 additions & 3 deletions libzeth/snarks/pghr13/api/response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef __ZETH_RESPONSE_HPP__
#define __ZETH_RESPONSE_HPP__

#include "api/prover.grpc.pb.h"
#include "api/snark_messages.grpc.pb.h"
#include "libzeth/libsnark_helpers/extended_proof.hpp"
#include "libzeth/util_api.hpp"

Expand All @@ -14,11 +14,11 @@ namespace libzeth

template<typename ppT>
void prepare_proof_response(
extended_proof<ppT> &ext_proof, prover_proto::ExtendedProof *message);
extended_proof<ppT> &ext_proof, zeth_proto::ExtendedProof *message);
template<typename ppT>
void prepare_verification_key_response(
libsnark::r1cs_ppzksnark_verification_key<ppT> &vk,
prover_proto::VerificationKey *message);
zeth_proto::VerificationKey *message);

} // namespace libzeth
#include "libzeth/snarks/pghr13/api/response.tcc"
Expand Down
Loading

0 comments on commit edf32bd

Please sign in to comment.