Skip to content

Commit 1fc176d

Browse files
committed
First commit. Doesn't yet work :)
1 parent 44588ae commit 1fc176d

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test
2+
*.o
3+
*.d
4+
depinst
5+
depsrc
6+

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
OPTFLAGS = -march=native -mtune=native -O2
2+
CXXFLAGS += -g -Wall -Wextra -Wno-unused-parameter -std=c++11 -fPIC -Wno-unused-variable
3+
CXXFLAGS += -I $(DEPINST)/include -I $(DEPINST)/include/libsnark -DUSE_ASM -DCURVE_ALT_BN128
4+
LDFLAGS += -flto
5+
6+
DEPSRC=depsrc
7+
DEPINST=depinst
8+
9+
LDLIBS += -L $(DEPINST)/lib -Wl,-rpath $(DEPINST)/lib -L . -lsnark -lgmpxx -lgmp
10+
LDLIBS += -lboost_system
11+
12+
all:
13+
$(CXX) -o test.o src/test.cpp -c $(CXXFLAGS)
14+
$(CXX) -o test test.o $(CXXFLAGS) $(LDFLAGS) $(LDLIBS)
15+
16+
clean:
17+
$(RM) test.o test

get-libsnark

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
# To pass options options to libsnark Makefile, put them in env var LIBSNARK_FLAGS.
3+
# To clone libsnark from an alternate location, set env var LIBSNARK_SRC. For example:
4+
# LIBSNARK_SRC="$HOME/libsnark.git --branch master" ./get-libsnark
5+
# To use curve ALT_BN128 instead of BN128 (which is x64-only), use:
6+
# CURVE=ALT_BN128 ./get-libsnark
7+
8+
set -e
9+
10+
LIBSNARK_SRC=${LIBSNARK_SRC:-https://github.com/scipr-lab/libsnark}
11+
12+
CURVE=${CURVE:-BN128}
13+
14+
LIBSNARK_FLAGS="$LIBSNARK_FLAGS NO_SUPERCOP=1 NO_GTEST=1 NO_DOCS=1 CURVE=$CURVE"
15+
if [[ `uname -s` == "Darwin" ]]; then
16+
LIBSNARK_FLAGS="$LIBSNARK_FLAGS NO_PROCPS=1"
17+
fi
18+
19+
set -x
20+
21+
DEPSRC=./depsrc
22+
DEPINST=./depinst
23+
24+
mkdir -p $DEPINST
25+
DEPINST=`pwd -P`/$DEPINST # remember absolute path
26+
27+
mkdir -p $DEPSRC
28+
cd $DEPSRC
29+
30+
# We use a specific snapshot of libsnark for now
31+
# later we should update to a CMake build
32+
[ ! -d libsnark ] && git clone $LIBSNARK_SRC libsnark
33+
cd libsnark
34+
git checkout 99210024784497c6fc7e1d3c485e45c8bba46b2c
35+
if [ "$CURVE" == "BN128" ]; then
36+
# TODO: submit -fPIC patch to ate-pairing
37+
INC_DIR=-fPIC ./prepare-depends.sh
38+
fi
39+
make clean
40+
make lib $LIBSNARK_FLAGS
41+
make install PREFIX=$DEPINST $LIBSNARK_FLAGS

src/test.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <stdlib.h>
2+
#include <iostream>
3+
#include <boost/optional/optional_io.hpp>
4+
5+
#include "algebra/fields/field_utils.hpp"
6+
#include "libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp"
7+
#include "libsnark/common/default_types/r1cs_ppzksnark_pp.hpp"
8+
#include "libsnark/common/utils.hpp"
9+
#include "libsnark/gadgetlib1/pb_variable.hpp"
10+
11+
12+
using namespace libsnark;
13+
using namespace std;
14+
15+
int main()
16+
{
17+
18+
typedef Fr<default_r1cs_ppzksnark_pp> FieldT;
19+
20+
// Create protoboard
21+
22+
protoboard<FieldT> pb;
23+
24+
// Define variables
25+
26+
pb_variable<FieldT> x;
27+
pb_variable<FieldT> out;
28+
pb_variable<FieldT> sym_1;
29+
pb_variable<FieldT> y;
30+
pb_variable<FieldT> sym_2;
31+
32+
// Allocate variables to protoboard
33+
// The strings (like "x") are only for debugging purposes
34+
35+
x.allocate(pb, "x");
36+
out.allocate(pb, "out");
37+
sym_1.allocate(pb, "sym_1");
38+
y.allocate(pb, "y");
39+
sym_2.allocate(pb, "sym_2");
40+
41+
// Add R1CS constraints to protoboard
42+
43+
// x*x = sym_1
44+
pb.add_r1cs_constraint(r1cs_constraint<FieldT>(x, x, sym_1));
45+
46+
// sym_1 * x = y
47+
pb.add_r1cs_constraint(r1cs_constraint<FieldT>(sym_1, x, y));
48+
49+
// y + x = sym_2
50+
pb.add_r1cs_constraint(r1cs_constraint<FieldT>(y + x, 1, sym_2));
51+
52+
// sym_2 + 5 = ~out
53+
pb.add_r1cs_constraint(r1cs_constraint<FieldT>(sym_2 + 5, 1, out));
54+
55+
// Add witness values
56+
57+
pb.val(x) = 3;
58+
pb.val(out) = 35;
59+
pb.val(sym_1) = 9;
60+
pb.val(y) = 27;
61+
pb.val(sym_2) = 30;
62+
63+
64+
if (pb.is_satisfied()) {
65+
cout << "Constraint system is satisfied." << endl;
66+
}
67+
else {
68+
cout << "Constraint system is not satisfied." << endl;
69+
}
70+
71+
// This is not working for some reason
72+
cout << "Value of x: " << pb.val(x) << endl;
73+
74+
const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
75+
76+
cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
77+
cout << "Number of variables: " << pb.num_variables() << endl;
78+
79+
// Initialize the curve parameters.
80+
default_r1cs_ppzksnark_pp::init_public_params();
81+
82+
r1cs_ppzksnark_keypair<default_r1cs_ppzksnark_pp> keypair = r1cs_ppzksnark_generator<default_r1cs_ppzksnark_pp>(constraint_system);
83+
84+
r1cs_ppzksnark_proof<default_r1cs_ppzksnark_pp> proof = r1cs_ppzksnark_prover<default_r1cs_ppzksnark_pp>(keypair.pk, pb.primary_input(), pb.auxiliary_input());
85+
86+
vector<FieldT> empty;
87+
88+
bool verified = r1cs_ppzksnark_verifier_strong_IC<default_r1cs_ppzksnark_pp>(keypair.vk, empty, proof);
89+
90+
cout << "Verification status: " << verified << endl;
91+
92+
cout << "made it here" << endl;
93+
94+
return 0;
95+
}

0 commit comments

Comments
 (0)