-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_proof.cpp
129 lines (95 loc) · 3.56 KB
/
test_proof.cpp
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
#include <array>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "snarkfront.hpp"
using namespace snarkfront;
using namespace cryptl;
using namespace std;
void printUsage(const char* exeName) {
cout << "usage: " << exeName
<< " -m keygen|input|proof|verify"
<< endl;
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
Getopt cmdLine(argc, argv, "m", "", "");
if (!cmdLine || cmdLine.empty()) printUsage(argv[0]);
const auto mode = cmdLine.getString('m');
// Barreto-Naehrig 128 bits
init_BN128();
typedef BN128_FR FR;
typedef BN128_PAIRING PAIRING;
// output hash digest is publicly known
vector<uint8_t> preImage;
for (const auto& c : "secret message") preImage.push_back(c);
const auto pubHash = digest(cryptl::SHA256(), preImage);
if ("keygen" == mode) {
////////////////////////////////////////////////////////////
// trusted key generation
// input variables (values don't matter here)
array<uint32_x<FR>, 8> pubVars;
bless(pubVars);
// marks end of public input variables
end_input<PAIRING>();
// constraint system from circuit
assert_true(pubVars == digest(snarkfront::SHA256<FR>(), vector<uint8_t>()));
// generate proving/verification key pair
GenericProgressBar progress(cerr, 50);
cerr << "generate key pair";
cout << keypair<PAIRING>(progress); // expensive!
cerr << endl;
} else if ("input" == mode) {
////////////////////////////////////////////////////////////
// public inputs
// input variables (need values)
array<uint32_x<FR>, 8> pubVars;
bless(pubVars, pubHash);
// marks end of public input variables
end_input<PAIRING>();
// publicly known input variables
cout << input<PAIRING>();
} else if ("proof" == mode) {
////////////////////////////////////////////////////////////
// generate a proof
Keypair<PAIRING> keypair; // proving/verification key pair
Input<PAIRING> input; // public inputs to circuit
cin >> keypair >> input;
// check for marshalling errors
assert(!keypair.empty() && !input.empty());
// input variables (need values)
array<uint32_x<FR>, 8> pubVars;
bless(pubVars, input);
// marks end of public input variables
end_input<PAIRING>();
// perform calculation
assert_true(pubVars == digest(snarkfront::SHA256<FR>(), preImage));
// generate proof
GenericProgressBar progress(cerr, 50);
cerr << "generate proof";
cout << proof(keypair, progress);
cerr << endl;
} else if ("verify" == mode) {
////////////////////////////////////////////////////////////
// verify a proof
Keypair<PAIRING> keypair; // proving/verification key pair
Input<PAIRING> input; // public inputs to circuit
Proof<PAIRING> proof; // zero knowledge proof
cin >> keypair >> input >> proof;
// check for marshalling errors
assert(!keypair.empty() && !input.empty() && !proof.empty());
// verify proof
GenericProgressBar progress(cerr);
cerr << "verify proof ";
const bool valid = verify(keypair, input, proof, progress);
cerr << endl;
cout << "proof is " << (valid ? "verified" : "rejected") << endl;
} else {
// no mode specified
printUsage(argv[0]);
}
return EXIT_SUCCESS;
}