Skip to content

Commit 842507e

Browse files
committed
add gadget files
1 parent 8305cb5 commit 842507e

File tree

4 files changed

+147
-8
lines changed

4 files changed

+147
-8
lines changed

.gitignore

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
test
2-
*.o
3-
*.d
4-
depinst
5-
depsrc
61
*~
72
build

src/CMakeLists.txt

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
include_directories(.)
22

33
add_executable(
4-
main
4+
test
55

66
test.cpp
77
)
88
target_link_libraries(
9-
main
9+
test
1010

1111
snark
1212
)
1313
target_include_directories(
14-
main
14+
test
15+
16+
PUBLIC
17+
${DEPENDS_DIR}/libsnark
18+
${DEPENDS_DIR}/libsnark/depends/libfqfft
19+
${DEPENDS_DIR}/libsnark/depends/libff
20+
)
21+
22+
add_executable(
23+
test-gadget
24+
25+
test-gadget.cpp
26+
)
27+
target_link_libraries(
28+
test-gadget
29+
30+
snark
31+
)
32+
target_include_directories(
33+
test-gadget
1534

1635
PUBLIC
1736
${DEPENDS_DIR}/libsnark

src/gadget.hpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "libsnark/gadgetlib1/gadget.hpp"
2+
3+
using namespace libsnark;
4+
5+
template<typename FieldT>
6+
class test_gadget : public gadget<FieldT> {
7+
private:
8+
pb_variable<FieldT> sym_1;
9+
pb_variable<FieldT> y;
10+
pb_variable<FieldT> sym_2;
11+
public:
12+
const pb_variable<FieldT> out;
13+
const pb_variable<FieldT> x;
14+
15+
test_gadget(protoboard<FieldT> &pb,
16+
const pb_variable<FieldT> &out,
17+
const pb_variable<FieldT> &x) :
18+
gadget<FieldT>(pb, "poly_gadget"), out(out), x(x)
19+
{
20+
// Allocate variables to protoboard
21+
// The strings (like "x") are only for debugging purposes
22+
23+
sym_1.allocate(this->pb, "sym_1");
24+
y.allocate(this->pb, "y");
25+
sym_2.allocate(this->pb, "sym_2");
26+
}
27+
28+
void generate_r1cs_constraints()
29+
{
30+
// x*x = sym_1
31+
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(x, x, sym_1));
32+
33+
// sym_1 * x = y
34+
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(sym_1, x, y));
35+
36+
// y + x = sym_2
37+
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(y + x, 1, sym_2));
38+
39+
// sym_2 + 5 = ~out
40+
this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(sym_2 + 5, 1, out));
41+
}
42+
43+
void generate_r1cs_witness()
44+
{
45+
this->pb.val(sym_1) = this->pb.val(x) * this->pb.val(x);
46+
this->pb.val(y) = this->pb.val(sym_1) * this->pb.val(x);
47+
this->pb.val(sym_2) = this->pb.val(y) + this->pb.val(x);
48+
}
49+
};

src/test-gadget.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <stdlib.h>
2+
#include <iostream>
3+
4+
#include "libff/algebra/fields/field_utils.hpp"
5+
#include "libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp"
6+
#include "libsnark/common/default_types/r1cs_ppzksnark_pp.hpp"
7+
#include "libsnark/gadgetlib1/pb_variable.hpp"
8+
9+
#include "gadget.hpp"
10+
11+
using namespace libsnark;
12+
using namespace std;
13+
14+
int main()
15+
{
16+
// Initialize the curve parameters.
17+
default_r1cs_ppzksnark_pp::init_public_params();
18+
19+
typedef libff::Fr<default_r1cs_ppzksnark_pp> FieldT;
20+
21+
// Create protoboard
22+
23+
protoboard<FieldT> pb;
24+
pb_variable<FieldT> out;
25+
pb_variable<FieldT> x;
26+
27+
// Allocate variables
28+
29+
out.allocate(pb, "out");
30+
x.allocate(pb, "x");
31+
32+
// This sets up the protoboard variables
33+
// so that the first one (out) represents the public
34+
// input and the rest is private input
35+
36+
pb.set_input_sizes(1);
37+
38+
test_gadget<FieldT> g(pb, out, x);
39+
g.generate_r1cs_constraints();
40+
41+
cout << "Number of variables: " << pb.num_variables() << endl;
42+
43+
// Add witness values
44+
45+
pb.val(out) = 35;
46+
pb.val(x) = 3;
47+
48+
g.generate_r1cs_witness();
49+
50+
if (pb.is_satisfied()) {
51+
cout << "Constraint system is satisfied." << endl;
52+
}
53+
else {
54+
cout << "Constraint system is not satisfied." << endl;
55+
}
56+
57+
cout << "primary (public) input: " << pb.primary_input() << endl;
58+
cout << "auxiliary (private) input: " << pb.auxiliary_input() << endl;
59+
60+
const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
61+
62+
cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
63+
64+
r1cs_ppzksnark_keypair<default_r1cs_ppzksnark_pp> keypair = r1cs_ppzksnark_generator<default_r1cs_ppzksnark_pp>(constraint_system);
65+
66+
r1cs_ppzksnark_proof<default_r1cs_ppzksnark_pp> proof = r1cs_ppzksnark_prover<default_r1cs_ppzksnark_pp>(keypair.pk, pb.primary_input(), pb.auxiliary_input());
67+
68+
bool verified = r1cs_ppzksnark_verifier_strong_IC<default_r1cs_ppzksnark_pp>(keypair.vk, pb.primary_input(), proof);
69+
70+
cout << "Primary (public) input: " << pb.primary_input() << endl;
71+
cout << "Auxiliary (private) input: " << pb.auxiliary_input() << endl;
72+
73+
cout << "Verification status: " << verified << endl;
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)