Clearmatics Libsnark  0.1
C++ library for zkSNARK proofs
demo_ram_ppzksnark.cpp
Go to the documentation of this file.
1 
7 #include <algorithm>
8 #include <fstream>
9 #include <iostream>
10 #include <sstream>
11 #include <string>
12 #ifndef MINDEPS
13 #include <boost/program_options.hpp>
14 #endif
15 
16 #include <libff/common/profiling.hpp>
21 
22 #ifndef MINDEPS
23 namespace po = boost::program_options;
24 
26  const int argc,
27  const char **argv,
28  std::string &assembly_fn,
29  std::string &processed_assembly_fn,
30  std::string &architecture_params_fn,
31  std::string &computation_bounds_fn,
32  std::string &primary_input_fn,
33  std::string &auxiliary_input_fn)
34 {
35  try {
36  po::options_description desc("Usage");
37  desc.add_options()("help", "print this help message")(
38  "assembly", po::value<std::string>(&assembly_fn)->required())(
39  "processed_assembly",
40  po::value<std::string>(&processed_assembly_fn)->required())(
41  "architecture_params",
42  po::value<std::string>(&architecture_params_fn)->required())(
43  "computation_bounds",
44  po::value<std::string>(&computation_bounds_fn)->required())(
45  "primary_input",
46  po::value<std::string>(&primary_input_fn)->required())(
47  "auxiliary_input",
48  po::value<std::string>(&auxiliary_input_fn)->required());
49 
50  po::variables_map vm;
51  po::store(po::parse_command_line(argc, argv, desc), vm);
52 
53  if (vm.count("help")) {
54  std::cout << desc << "\n";
55  return false;
56  }
57 
58  po::notify(vm);
59  } catch (std::exception &e) {
60  std::cerr << "Error: " << e.what() << "\n";
61  return false;
62  }
63 
64  return true;
65 }
66 #endif
67 
68 using namespace libsnark;
69 
70 int main(int argc, const char *argv[])
71 {
73 #ifdef MINDEPS
74  std::string assembly_fn = "assembly.s";
75  std::string processed_assembly_fn = "processed.txt";
76  std::string architecture_params_fn = "architecture_params.txt";
77  std::string computation_bounds_fn = "computation_bounds.txt";
78  std::string primary_input_fn = "primary_input.txt";
79  std::string auxiliary_input_fn = "auxiliary_input.txt";
80 #else
81  std::string assembly_fn;
82  std::string processed_assembly_fn;
83  std::string architecture_params_fn;
84  std::string computation_bounds_fn;
85  std::string primary_input_fn;
86  std::string auxiliary_input_fn;
87 
89  argc,
90  argv,
91  assembly_fn,
92  processed_assembly_fn,
93  architecture_params_fn,
94  computation_bounds_fn,
95  primary_input_fn,
96  auxiliary_input_fn)) {
97  return 1;
98  }
99 #endif
100  libff::start_profiling();
101 
102  printf("==================================================================="
103  "=============\n");
104  printf("TinyRAM example loader\n");
105  printf("==================================================================="
106  "=============\n\n");
107 
108  /* load everything */
110  std::ifstream f_ap(architecture_params_fn);
111  f_ap >> ap;
112 
113  printf("Will run on %zu register machine (word size = %zu)\n", ap.k, ap.w);
114 
115  std::ifstream f_rp(computation_bounds_fn);
116  size_t tinyram_input_size_bound, tinyram_program_size_bound, time_bound;
117  f_rp >> tinyram_input_size_bound >> tinyram_program_size_bound >>
118  time_bound;
119 
120  std::ifstream processed(processed_assembly_fn);
121  std::ifstream raw(assembly_fn);
122  tinyram_program program = load_preprocessed_program(ap, processed);
123 
124  printf(
125  "Program:\n%s\n",
126  std::string(
127  (std::istreambuf_iterator<char>(raw)),
128  std::istreambuf_iterator<char>())
129  .c_str());
130 
131  std::ifstream f_primary_input(primary_input_fn);
132  std::ifstream f_auxiliary_input(auxiliary_input_fn);
133 
134  libff::enter_block("Loading primary input");
135  tinyram_input_tape primary_input = load_tape(f_primary_input);
136  libff::leave_block("Loading primary input");
137 
138  libff::enter_block("Loading auxiliary input");
139  tinyram_input_tape auxiliary_input = load_tape(f_auxiliary_input);
140  libff::leave_block("Loading auxiliary input");
141 
142  printf("\nPress enter to continue.\n");
143  std::cin.get();
144 
145  const size_t boot_trace_size_bound =
146  tinyram_program_size_bound + tinyram_input_size_bound;
149  ap, boot_trace_size_bound, program, primary_input);
150 
151  printf("==================================================================="
152  "=============\n");
153  printf("TinyRAM arithmetization test for T = %zu time steps\n", time_bound);
154  printf("==================================================================="
155  "=============\n\n");
156 
158  typedef ram_base_field<default_ram> FieldT;
159 
160  ram_to_r1cs<default_ram> r(ap, boot_trace_size_bound, time_bound);
161  r.instance_map();
162 
165  ap, boot_trace_size_bound, boot_trace);
167  r.auxiliary_input_map(boot_trace, auxiliary_input);
168  const r1cs_constraint_system<FieldT> constraint_system =
170 
172  assert(constraint_system.is_satisfied(
174 
175  printf("\nPress enter to continue.\n");
176  std::cin.get();
177 
178  printf("==================================================================="
179  "=============\n");
180  printf("TinyRAM ppzkSNARK Key Pair Generator\n");
181  printf("==================================================================="
182  "=============\n\n");
184  ram_ppzksnark_generator<default_tinyram_ppzksnark_pp>(
185  ap, boot_trace_size_bound, time_bound);
186 
187  printf("\nPress enter to continue.\n");
188  std::cin.get();
189 
190  printf("==================================================================="
191  "=============\n");
192  printf("TinyRAM ppzkSNARK Prover\n");
193  printf("==================================================================="
194  "=============\n\n");
196  ram_ppzksnark_prover<default_tinyram_ppzksnark_pp>(
197  keypair.pk, boot_trace, auxiliary_input);
198 
199  printf("\nPress enter to continue.\n");
200  std::cin.get();
201 
202  printf("==================================================================="
203  "=============\n");
204  printf("TinyRAM ppzkSNARK Verifier\n");
205  printf("==================================================================="
206  "=============\n\n");
207  bool bit = ram_ppzksnark_verifier<default_tinyram_ppzksnark_pp>(
208  keypair.vk, boot_trace, proof);
209 
210  printf("==================================================================="
211  "=============\n");
212  printf("The verification result is: %s\n", (bit ? "PASS" : "FAIL"));
213  printf("==================================================================="
214  "=============\n");
215  libff::print_mem();
216  printf("==================================================================="
217  "=============\n");
218 }
tinyram_params.hpp
libsnark::r1cs_constraint_system::is_satisfied
bool is_satisfied(const r1cs_primary_input< FieldT > &primary_input, const r1cs_auxiliary_input< FieldT > &auxiliary_input) const
tinyram_ppzksnark_pp.hpp
process_demo_command_line
bool process_demo_command_line(const int argc, const char **argv, std::string &assembly_fn, std::string &processed_assembly_fn, std::string &architecture_params_fn, std::string &computation_bounds_fn, std::string &primary_input_fn, std::string &auxiliary_input_fn)
Definition: demo_ram_ppzksnark.cpp:25
libsnark::ram_to_r1cs::print_execution_trace
void print_execution_trace() const
main
int main(int argc, const char *argv[])
Definition: demo_ram_ppzksnark.cpp:70
libsnark::ram_to_r1cs::instance_map
void instance_map()
libsnark
Definition: accumulation_vector.hpp:18
libsnark::ram_ppzksnark_keypair::pk
ram_ppzksnark_proving_key< ram_ppzksnark_ppT > pk
Definition: ram_ppzksnark.hpp:184
libsnark::default_tinyram_ppzksnark_pp::init_public_params
static void init_public_params()
Definition: tinyram_ppzksnark_pp.cpp:18
libsnark::ram_base_field
typename ramT::base_field_type ram_base_field
Definition: ram_params.hpp:40
libsnark::load_preprocessed_program
tinyram_program load_preprocessed_program(const tinyram_architecture_params &ap, std::istream &preprocessed)
Definition: tinyram_aux.cpp:312
libsnark::ram_to_r1cs::auxiliary_input_map
r1cs_auxiliary_input< FieldT > auxiliary_input_map(const ram_boot_trace< ramT > &boot_trace, const ram_input_tape< ramT > &auxiliary_input)
libsnark::tinyram_input_tape
std::vector< size_t > tinyram_input_tape
Definition: tinyram_aux.hpp:122
libsnark::r1cs_ppzksnark_proof
Definition: r1cs_ppzksnark.hpp:298
libsnark::ram_ppzksnark_keypair
Definition: ram_ppzksnark.hpp:182
libsnark::ram_to_r1cs
Definition: ram_to_r1cs.hpp:25
libsnark::ram_ppzksnark_machine_pp
typename ram_ppzksnark_ppT::machine_pp ram_ppzksnark_machine_pp
Definition: ram_ppzksnark_params.hpp:64
libsnark::memory_store_trace
Definition: memory_store_trace.hpp:29
libsnark::r1cs_auxiliary_input
std::vector< FieldT > r1cs_auxiliary_input
Definition: r1cs.hpp:84
libsnark::ram_ppzksnark_architecture_params
ram_architecture_params< ram_ppzksnark_machine_pp< ram_ppzksnark_ppT > > ram_ppzksnark_architecture_params
Definition: ram_ppzksnark_params.hpp:68
libsnark::load_tape
tinyram_input_tape load_tape(std::istream &tape)
Definition: tinyram_aux.cpp:369
libsnark::r1cs_constraint_system
Definition: protoboard.hpp:25
libsnark::ram_to_r1cs::get_constraint_system
r1cs_constraint_system< FieldT > get_constraint_system() const
libsnark::tinyram_boot_trace_from_program_and_input
memory_store_trace tinyram_boot_trace_from_program_and_input(const tinyram_architecture_params &ap, const size_t boot_trace_size_bound, const tinyram_program &program, const tinyram_input_tape &primary_input)
Definition: tinyram_aux.cpp:338
ram_ppzksnark.hpp
libsnark::tinyram_program
Definition: tinyram_aux.hpp:200
ram_to_r1cs.hpp
libsnark::ram_to_r1cs::primary_input_map
static r1cs_primary_input< ram_base_field< ramT > > primary_input_map(const ram_architecture_params< ramT > &ap, const size_t boot_trace_size_bound, const ram_boot_trace< ramT > &boot_trace)
libsnark::r1cs_primary_input
std::vector< FieldT > r1cs_primary_input
Definition: r1cs.hpp:82
libsnark::ram_ppzksnark_keypair::vk
ram_ppzksnark_verification_key< ram_ppzksnark_ppT > vk
Definition: ram_ppzksnark.hpp:185