-
Notifications
You must be signed in to change notification settings - Fork 1
/
qapver.cpp
140 lines (118 loc) · 5.05 KB
/
qapver.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
/**
* Copyright (c) 2016-2018 Koninklijke Philips N.V. All rights reserved. A
* copyright license for redistribution and use in source and binary forms,
* with or without modification, is hereby granted for non-commercial,
* experimental and research purposes, provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution. If
* you wish to use this software commercially, kindly contact
* [email protected] to obtain a commercial license.
*
* This license extends only to copyright and does not include or grant any
* patent license or other license whatsoever.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <fstream>
#include <map>
#include <set>
#include <time.h>
#include "base.h"
#include "key.h"
#include "prove.h" // TODO: eliminate this include
#include "proof.h"
#include "verify.h"
using namespace std;
// TODO: move to common lib
template<typename X> X readfromfile(string fname, bool skiptok = false) {
X ret;
ifstream fl(fname);
if (!fl.is_open()) {
cerr << "*** Could not open file " << fname << endl;
exit(1);
}
if (skiptok) {
string tok;
fl >> tok;
}
fl >> ret;
fl.close();
return ret;
}
//bool qapblockver(const masterkey& mk, const datablock& db, const qapvk& qvk, const blockvk& bvk, const blockproof& block);
//bool qapver(const qapvk& qvk, const qapproof& proof);
int main (int argc, char **argv) {
libqap_init();
if (argc!=5) {
cerr << "Usage: " << argv[0] << "<masterkey> <schedule> <proof> <iowires>" << endl;
return 2;
}
masterkey mkey = readfromfile<masterkey>(argv[1]);
ifstream sched(argv[2]);
ifstream prooff(argv[3]);
wirevalt wirevals = readfromfile<wirevalt>(argv[4]);
map<string,string> qap2type;
map<string,qapvk> qapvks;
map<string,qapproof> proofs;
while (!sched.eof()) {
string tok;
sched >> tok;
if (tok=="") continue;
if (tok == "[function]") {
//string type; sched >> type;
string name; sched >> name;
string eqfile; sched >> eqfile;
string ekfile; sched >> ekfile;
string vkfile; sched >> vkfile;
qap2type[name] = vkfile;
if (qapvks.find(vkfile) == qapvks.end()) {
cerr << "Reading QAP vk: " << vkfile << endl;
qapvks[vkfile] = readfromfile<qapvk>(vkfile, true);
}
cerr << "Verifying " << name << " (" << vkfile << ")" << endl;
prooff >> proofs[name];
if (!qapver(qapvks[vkfile], proofs[name], wirevals, name)) return 10;
} else if (tok=="[external]") {
string fun; sched >> fun;
string type = qap2type[fun];
string blk; sched >> blk;
string blkwires; sched >> blkwires;
string blkfile; sched >> blkfile;
cerr << "Reading input block file " << blkfile << endl;
datablock din = readfromfile<datablock>(blkfile);
cerr << "Verifying input block " << fun << " " << blk << endl;
if (!qapblockvalid(mkey, din)) return 11;
if (!qapblockver(mkey, din, qapvks[type].blocks[blk], proofs[fun].blocks[blk])) return 12;
} else if (tok=="[glue]") {
string fun1; sched >> fun1;
string type1 = qap2type[fun1];
string blk1; sched >> blk1;
string fun2; sched >> fun2;
string type2 = qap2type[fun2];
string blk2; sched >> blk2;
datablock blk; prooff >> blk;
cerr << "Verifying glue " << fun1 << "." << blk1 << "<->" << fun2 << "." << blk2 << endl;
if (!qapblockvalid(mkey, blk)) return 2;
if (!qapblockver(mkey, blk, qapvks[type1].blocks[blk1], proofs[fun1].blocks[blk1])) return 13;
if (!qapblockver(mkey, blk, qapvks[type2].blocks[blk2], proofs[fun2].blocks[blk2])) return 14;
} else {
cerr << "*** Unrecognized token: " << tok << endl;
return 15;
}
}
return 0;
}