2 *****************************************************************************
4 Implementation of interfaces for a RAM example, as well as functions to sample
5 RAM examples with prescribed parameters (according to some distribution).
9 *****************************************************************************
10 * @author This file is part of libsnark, developed by SCIPR Lab
11 * and contributors (see AUTHORS).
12 * @copyright MIT license (see LICENSE file)
13 *****************************************************************************/
15 #ifndef RAM_EXAMPLES_TCC_
16 #define RAM_EXAMPLES_TCC_
18 #include <libsnark/relations/ram_computations/rams/tinyram/tinyram_aux.hpp>
23 template<typename ramT>
24 ram_example<ramT> gen_ram_example_simple(
25 const ram_architecture_params<ramT> &ap,
26 const size_t boot_trace_size_bound,
27 const size_t time_bound,
28 const bool satisfiable)
30 libff::enter_block("Call to gen_ram_example_simple");
32 const size_t program_size = boot_trace_size_bound / 2;
33 const size_t input_size = boot_trace_size_bound - program_size;
35 ram_example<ramT> result;
38 result.boot_trace_size_bound = boot_trace_size_bound;
39 result.time_bound = time_bound;
41 tinyram_program prelude;
42 prelude.instructions = generate_tinyram_prelude(ap);
45 for (size_t i = 0; i < prelude.instructions.size(); ++i) {
46 result.boot_trace.set_trace_entry(
48 std::make_pair(i, prelude.instructions[i].as_dword(ap)));
51 result.boot_trace[boot_pos] = std::make_pair(
54 tinyram_opcode_ANSWER, true, 0, 0, satisfiable ? 0 : 1)
55 .as_dword(ap)); /* answer 0/1 depending on satisfiability */
57 while (boot_pos < program_size) {
58 result.boot_trace.set_trace_entry(
59 boot_pos++, random_tinyram_instruction(ap).as_dword(ap));
62 for (size_t i = 0; i < input_size; ++i) {
63 result.boot_trace.set_trace_entry(
66 (1ul << (ap.dwaddr_len() - 1)) + i,
67 std::rand() % (1ul << (2 * ap.w))));
70 assert(boot_pos == boot_trace_size_bound);
72 libff::leave_block("Call to gen_ram_example_simple");
76 template<typename ramT>
77 ram_example<ramT> gen_ram_example_complex(
78 const ram_architecture_params<ramT> &ap,
79 const size_t boot_trace_size_bound,
80 const size_t time_bound,
81 const bool satisfiable)
83 libff::enter_block("Call to gen_ram_example_complex");
85 const size_t program_size = boot_trace_size_bound / 2;
86 const size_t input_size = boot_trace_size_bound - program_size;
88 assert(2 * ap.w / 8 * program_size < 1ul << (ap.w - 1));
89 assert(ap.w / 8 * input_size < 1ul << (ap.w - 1));
91 ram_example<ramT> result;
94 result.boot_trace_size_bound = boot_trace_size_bound;
95 result.time_bound = time_bound;
97 tinyram_program prelude;
98 prelude.instructions = generate_tinyram_prelude(ap);
101 for (size_t i = 0; i < prelude.instructions.size(); ++i) {
102 result.boot_trace.set_trace_entry(
104 std::make_pair(i, prelude.instructions[i].as_dword(ap)));
107 const size_t prelude_len = prelude.instructions.size();
108 const size_t instr_addr = (prelude_len + 4) * (2 * ap.w / 8);
109 const size_t input_addr =
110 (1ul << (ap.w - 1)) +
111 (ap.w / 8); // byte address of the first input word
113 result.boot_trace.set_trace_entry(
117 tinyram_instruction(tinyram_opcode_LOADB, true, 1, 0, instr_addr)
120 result.boot_trace.set_trace_entry(
124 tinyram_instruction(tinyram_opcode_LOADW, true, 2, 0, input_addr)
127 result.boot_trace.set_trace_entry(
131 tinyram_instruction(tinyram_opcode_SUB, false, 1, 1, 2)
134 result.boot_trace.set_trace_entry(
138 tinyram_instruction(tinyram_opcode_STOREB, true, 1, 0, instr_addr)
141 result.boot_trace.set_trace_entry(
145 tinyram_instruction(tinyram_opcode_ANSWER, true, 0, 0, 1)
149 while (boot_pos < program_size) {
150 result.boot_trace.set_trace_entry(
153 boot_pos, random_tinyram_instruction(ap).as_dword(ap)));
157 result.boot_trace.set_trace_entry(
160 1ul << (ap.dwaddr_len() - 1), satisfiable ? 1ul << ap.w : 0));
162 for (size_t i = 1; i < input_size; ++i) {
163 result.boot_trace.set_trace_entry(
166 (1ul << (ap.dwaddr_len() - 1)) + i + 1,
167 std::rand() % (1ul << (2 * ap.w))));
170 libff::leave_block("Call to gen_ram_example_complex");
174 } // namespace libsnark
176 #endif // RAM_EXAMPLES_TCC_