-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsha256.cpp
More file actions
165 lines (118 loc) · 7.03 KB
/
sha256.cpp
File metadata and controls
165 lines (118 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdlib.h>
#include <iostream>
#include <chrono>
#include "libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp"
#include "libsnark/common/default_types/r1cs_ppzksnark_pp.hpp"
#include "libsnark/gadgetlib1/pb_variable.hpp"
#include "libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_gadget.hpp"
using namespace libsnark;
using namespace std;
typedef libff::Fr<default_r1cs_ppzksnark_pp> FieldT;
template<typename ppT, typename FieldT>
bool verify_proof(r1cs_ppzksnark_verification_key<ppT> verification_key, r1cs_primary_input<FieldT> primary_input,
r1cs_ppzksnark_proof<default_r1cs_ppzksnark_pp> proof) {
return r1cs_ppzksnark_verifier_strong_IC<default_r1cs_ppzksnark_pp>(verification_key, primary_input, proof);
}
r1cs_ppzksnark_keypair<default_r1cs_ppzksnark_pp> setup_gadget(protoboard<FieldT> &pb, block_variable<FieldT> *&inp, digest_variable<FieldT> *&out, sha256_two_to_one_hash_gadget<FieldT> *&g) {
inp = new block_variable<FieldT>(pb, SHA256_block_size, "input");
out = new digest_variable<FieldT>(pb, SHA256_block_size, "output");
g = new sha256_two_to_one_hash_gadget<FieldT>(pb, SHA256_block_size, *inp, *out, "f");
g->generate_r1cs_constraints();
printf("Number of constraints for sha256_two_to_one_hash_gadget: %zu\n", pb.num_constraints());
// Trusted setup
const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
const r1cs_ppzksnark_keypair<default_r1cs_ppzksnark_pp> keypair = r1cs_ppzksnark_generator<default_r1cs_ppzksnark_pp>(
constraint_system);
return keypair;
}
void two_inputs_hash_gadget() {
typedef libff::Fr<default_r1cs_ppzksnark_pp> FieldT;
protoboard<FieldT> pb;
digest_variable<FieldT> left(pb, SHA256_digest_size, "left");
digest_variable<FieldT> right(pb, SHA256_digest_size, "right");
digest_variable<FieldT> output(pb, SHA256_digest_size, "output");
sha256_two_to_one_hash_gadget<FieldT> f(pb, left, right, output, "f");
f.generate_r1cs_constraints();
printf("Number of constraints for sha256_two_to_one_hash_gadget: %zu\n", pb.num_constraints());
// Trusted setup
const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
const r1cs_ppzksnark_keypair<default_r1cs_ppzksnark_pp> keypair = r1cs_ppzksnark_generator<default_r1cs_ppzksnark_pp>(
constraint_system);
// Add witness values
// Empty string (all 0s)
const libff::bit_vector left_bv = libff::int_list_to_bits({0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 32);
const libff::bit_vector right_bv = libff::int_list_to_bits({0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 32);
const libff::bit_vector hash_bv = libff::int_list_to_bits({0xda5698be, 0x17b9b469, 0x62335799, 0x779fbeca, 0x8ce5d491, 0xc0d26243, 0xbafef9ea, 0x1837a9d8}, 32);
// concatenation of bytes:
// [197, 215, 20, 132, 248, 207, 155, 244, 183, 111, 71, 144, 71, 48, 128, 75, 158, 50, 37, 169, 241, 51, 181, 222, 161, 104, 244, 226, 133, 31, 7, 47]
// and
// [204, 0, 252, 170, 124, 166, 32, 97, 113, 122, 72, 229, 46, 41, 163, 250, 55, 154, 149, 63, 170, 104, 147, 227, 46, 197, 162, 123, 148, 94, 96, 95]
/*const libff::bit_vector left_bv = libff::int_list_to_bits({0x426bc2d8, 0x4dc86782, 0x81e8957a, 0x409ec148, 0xe6cffbe8, 0xafe6ba4f, 0x9c6f1978, 0xdd7af7e9}, 32);
const libff::bit_vector right_bv = libff::int_list_to_bits({0x038cce42, 0xabd366b8, 0x3ede7e00, 0x9130de53, 0x72cdf73d, 0xee825114, 0x8cb48d1b, 0x9af68ad0}, 32);
const libff::bit_vector hash_bv = libff::int_list_to_bits({0xeffd0b7f, 0x1ccba116, 0x2ee816f7, 0x31c62b48, 0x59305141, 0x990e5c0a, 0xce40d33d, 0x0b1167d1}, 32);*/
left.generate_r1cs_witness(left_bv);
right.generate_r1cs_witness(right_bv);
f.generate_r1cs_witness();
output.generate_r1cs_witness(hash_bv);
cout << "two_inputs_hash_gadget => Satisfied status: " << pb.is_satisfied() << endl;
// Create proof
const r1cs_ppzksnark_proof<default_r1cs_ppzksnark_pp> proof1 = r1cs_ppzksnark_prover<default_r1cs_ppzksnark_pp>(
keypair.pk, pb.primary_input(), pb.auxiliary_input());
// Verify proof
bool verified1 = verify_proof(keypair.vk, pb.primary_input(), proof1);
cout << "two_inputs_hash_gadget => Verfied: " << verified1 << endl;
}
int one_input_hash_gadget(int num_iterations) {
using namespace std::chrono;
protoboard<FieldT> pb;
block_variable<FieldT>* input;
digest_variable<FieldT>* output;
sha256_two_to_one_hash_gadget<FieldT>* f;
r1cs_ppzksnark_keypair<default_r1cs_ppzksnark_pp> keypair = setup_gadget(pb, input, output, f);
int i = 0;
duration<double> tc(0);
duration<double> tp(0);
duration<double> tv(0);
while (i < num_iterations) {
// Hash of string "hello world"
const libff::bit_vector hash_bv = libff::int_list_to_bits({0xc082e440, 0x671cd799, 0x8baf04c0, 0x22c07e03, 0x4b125ee7, 0xd28e0a59, 0x49e4b924, 0x5f5cf897}, 32);
output->generate_r1cs_witness(hash_bv);
steady_clock::time_point begin = steady_clock::now();
// Add witness values
// For string "hello world"
const libff::bit_vector input_bv = libff::int_list_to_bits({0x6c6c6568, 0x6f77206f, 0x00646c72, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000}, 32);
input->generate_r1cs_witness(input_bv);
f->generate_r1cs_witness();
steady_clock::time_point mid = steady_clock::now();
tc += duration_cast<duration<double>>(mid - begin);
cout << "one_input_hash_gadget => Satisfied status: " << pb.is_satisfied() << endl;
// Create proof
const r1cs_ppzksnark_proof<default_r1cs_ppzksnark_pp> proof1 = r1cs_ppzksnark_prover<default_r1cs_ppzksnark_pp>(
keypair.pk, pb.primary_input(), pb.auxiliary_input());
steady_clock::time_point end = steady_clock::now();
tp += duration_cast<duration<double>>(end - begin);
steady_clock::time_point begin1 = steady_clock::now();
// Verify proof
bool verified1 = verify_proof(keypair.vk, pb.primary_input(), proof1);
steady_clock::time_point end1 = steady_clock::now();
if (verified1 == 0) return -1;
tv += duration_cast<duration<double>>(end1 - begin1);
cout << "one_input_hash_gadget => Verfied: " << verified1 << endl;
/*cout << "primary_input: " << pb.primary_input() << endl;
cout << "auxiliary_input: " << pb.auxiliary_input() << endl;*/
i++;
}
cout << "Total iterations : " << num_iterations << endl;
cout << "Total constraint generation time (seconds): " << tc.count() << endl;
cout << "Total proving time (seconds): " << tp.count() << endl;
cout << "Total verification time (seconds): " << tv.count() << endl;
return 1;
}
int main() {
// Initialize the curve parameters
default_r1cs_ppzksnark_pp::init_public_params();
int num_iterations = 1;
// two_inputs_hash_gadget();
if (one_input_hash_gadget(num_iterations) != 1) return -1;
return 0;
}