forked from jancarlsson/snarkfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_merkle.cpp
173 lines (138 loc) · 4.33 KB
/
test_merkle.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
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
166
167
168
169
170
171
172
173
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <vector>
#include "snarkfront.hpp"
using namespace snarkfront;
using namespace std;
void printUsage(const char* exeName) {
cout << "usage: " << exeName
<< " -p BN128|Edwards"
" -b 256|512"
" -d tree_depth"
" -i leaf_number"
<< endl;
exit(EXIT_FAILURE);
}
template <typename PAIRING, typename BUNDLE, typename ZK_PATH>
void runTest(const size_t treeDepth,
const size_t leafNumber)
{
BUNDLE bundle(treeDepth);
while (! bundle.isFull()) {
const typename BUNDLE::DigType leaf{bundle.treeSize()};
bundle.addLeaf(
leaf,
leafNumber == bundle.treeSize());
}
if (leafNumber >= bundle.treeSize()) {
cout << "leaf number " << leafNumber
<< " is larger than " << bundle.treeSize()
<< endl;
exit(EXIT_FAILURE);
}
const auto& leaf = bundle.authLeaf().front();
const auto& authPath = bundle.authPath().front();
cout << "leaf " << leafNumber << " child bits ";
for (int i = authPath.childBits().size() - 1; i >= 0; --i) {
cout << authPath.childBits()[i];
}
cout << endl;
cout << "root path" << endl;
for (int i = authPath.rootPath().size() - 1; i >= 0; --i) {
cout << "[" << i << "] "
<< asciiHex(authPath.rootPath()[i], true) << endl;
}
cout << "siblings" << endl;
for (int i = authPath.siblings().size() - 1; i >= 0; --i) {
cout << "[" << i << "] "
<< asciiHex(authPath.siblings()[i], true) << endl;
}
typename ZK_PATH::DigType rt;
bless(rt, authPath.rootHash());
end_input<PAIRING>();
typename ZK_PATH::DigType zkLeaf;
bless(zkLeaf, leaf);
ZK_PATH zkAuthPath(authPath);
zkAuthPath.updatePath(zkLeaf);
assert_true(rt == zkAuthPath.rootHash());
cout << "variable count " << variable_count<PAIRING>() << endl;
}
template <typename PAIRING>
bool runTest(const string& shaBits,
const size_t treeDepth,
const size_t leafNumber)
{
typedef typename PAIRING::Fr FR;
if ("256" == shaBits) {
runTest<PAIRING,
MerkleBundle_SHA256<uint32_t>, // count could be size_t
zk::MerkleAuthPath_SHA256<FR>>(
treeDepth,
leafNumber);
} else if ("512" == shaBits) {
runTest<PAIRING,
MerkleBundle_SHA512<uint64_t>, // count could be size_t
zk::MerkleAuthPath_SHA512<FR>>(
treeDepth,
leafNumber);
}
GenericProgressBar progress1(cerr), progress2(cerr, 100);
cerr << "generate key pair";
const auto key = keypair<PAIRING>(progress2);
cerr << endl;
const auto in = input<PAIRING>();
cerr << "generate proof";
const auto p = proof(key, progress2);
cerr << endl;
cerr << "verify proof ";
const bool proofOK = verify(key, in, p, progress1);
cerr << endl;
return proofOK;
}
int main(int argc, char *argv[])
{
// command line switches
string pairing, shaBits;
size_t treeDepth = -1, leafNumber = -1;
int opt;
while (-1 != (opt = getopt(argc, argv, "p:b:d:i:"))) {
switch (opt) {
case ('p') :
pairing = optarg;
break;
case ('b') :
shaBits = optarg;
break;
case('d') : {
stringstream ss(optarg);
ss >> treeDepth;
if (!ss) printUsage(argv[0]);
}
break;
case('i') : {
stringstream ss(optarg);
ss >> leafNumber;
if (!ss) printUsage(argv[0]);
}
break;
}
}
if (!validPairingName(pairing) || shaBits.empty() || -1 == treeDepth || -1 == leafNumber)
printUsage(argv[0]);
bool result;
if (pairingBN128(pairing)) {
// Barreto-Naehrig 128 bits
init_BN128();
result = runTest<BN128_PAIRING>(shaBits, treeDepth, leafNumber);
} else if (pairingEdwards(pairing)) {
// Edwards 80 bits
init_Edwards();
result = runTest<EDWARDS_PAIRING>(shaBits, treeDepth, leafNumber);
}
cout << "proof verification " << (result ? "OK" : "FAIL") << endl;
exit(EXIT_SUCCESS);
}