-
Notifications
You must be signed in to change notification settings - Fork 12
/
DSL_ppzk.hpp
212 lines (180 loc) · 5.53 KB
/
DSL_ppzk.hpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifndef _SNARKFRONT_DSL_PPZK_HPP_
#define _SNARKFRONT_DSL_PPZK_HPP_
#include <cstdint>
#include <memory>
#include <string>
#include <snarklib/PPZK_keypair.hpp>
#include <snarklib/PPZK_proof.hpp>
#include <snarklib/ProgressCallback.hpp>
#include <snarkfront/Alg.hpp>
#include <snarkfront/Alg_bool.hpp>
#include <snarkfront/DSL_base.hpp>
#include <snarkfront/R1C.hpp>
#include <snarkfront/TLsingleton.hpp>
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// zero knowledge proof management
//
template <typename PAIRING> using Keypair = snarklib::PPZK_Keypair<PAIRING>;
template <typename PAIRING> using ProvingKey = snarklib::PPZK_ProvingKey<PAIRING>;
template <typename PAIRING> using VerificationKey = snarklib::PPZK_VerificationKey<PAIRING>;
template <typename PAIRING> using Input = R1Cowitness<typename PAIRING::Fr>;
template <typename PAIRING> using Proof = snarklib::PPZK_Proof<PAIRING>;
typedef snarklib::ProgressCallback ProgressCallback;
template <typename PAIRING>
void write_files(const std::string& filePrefix, const std::size_t maxSize)
{
TL<R1C<typename PAIRING::Fr>>::singleton()
->writeFiles(filePrefix, maxSize);
}
template <typename PAIRING>
void finalize_files()
{
TL<R1C<typename PAIRING::Fr>>::singleton()
->finalizeFiles();
}
template <typename PAIRING>
void reset()
{
TL<R1C<typename PAIRING::Fr>>::singleton()
->reset();
}
template <typename PAIRING>
void end_input()
{
TL<R1C<typename PAIRING::Fr>>::singleton()
->checkpointInput();
}
template <typename PAIRING>
std::size_t variable_count()
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->counterID();
}
template <typename PAIRING>
snarklib::PPZK_Keypair<PAIRING> keypair()
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->keypair<PAIRING>();
}
template <typename PAIRING>
snarklib::PPZK_Keypair<PAIRING> keypair(
snarklib::ProgressCallback& callback)
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->keypair<PAIRING>(std::addressof(callback));
}
template <typename PAIRING>
const R1Cowitness<typename PAIRING::Fr>& input()
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->input();
}
template <typename PAIRING>
const snarklib::R1Witness<typename PAIRING::Fr>& witness()
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->witness();
}
template <typename PAIRING>
snarklib::PPZK_Proof<PAIRING> proof(
const snarklib::PPZK_ProvingKey<PAIRING>& key)
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->proof(key, 0);
}
template <typename PAIRING>
snarklib::PPZK_Proof<PAIRING> proof(
const snarklib::PPZK_ProvingKey<PAIRING>& key,
snarklib::ProgressCallback& callback)
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->proof(key, 0, std::addressof(callback));
}
template <typename PAIRING>
snarklib::PPZK_Proof<PAIRING> proof(
const snarklib::PPZK_ProvingKey<PAIRING>& key,
const std::size_t reserveTune,
snarklib::ProgressCallback& callback)
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->proof(key, reserveTune, std::addressof(callback));
}
template <typename PAIRING>
snarklib::PPZK_Proof<PAIRING> proof(
const snarklib::PPZK_Keypair<PAIRING>& keypair)
{
return proof(keypair.pk());
}
template <typename PAIRING>
snarklib::PPZK_Proof<PAIRING> proof(
const snarklib::PPZK_Keypair<PAIRING>& keypair,
snarklib::ProgressCallback& callback)
{
return proof(keypair.pk(), callback);
}
template <typename PAIRING>
snarklib::PPZK_Proof<PAIRING> proof(
const snarklib::PPZK_Keypair<PAIRING>& keypair,
const std::size_t reserveTune,
snarklib::ProgressCallback& callback)
{
return proof(keypair.pk(), reserveTune, callback);
}
template <typename PAIRING>
bool verify(
const snarklib::PPZK_VerificationKey<PAIRING>& key,
const R1Cowitness<typename PAIRING::Fr>& input,
const snarklib::PPZK_Proof<PAIRING>& proof)
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->verify(key, input, proof);
}
template <typename PAIRING>
bool verify(
const snarklib::PPZK_VerificationKey<PAIRING>& key,
const R1Cowitness<typename PAIRING::Fr>& input,
const snarklib::PPZK_Proof<PAIRING>& proof,
snarklib::ProgressCallback& callback)
{
return TL<R1C<typename PAIRING::Fr>>::singleton()
->verify(key, input, proof, std::addressof(callback));
}
template <typename PAIRING>
bool verify(
const snarklib::PPZK_Keypair<PAIRING>& keypair,
const R1Cowitness<typename PAIRING::Fr>& input,
const snarklib::PPZK_Proof<PAIRING>& proof)
{
return verify(keypair.vk(), input, proof);
}
template <typename PAIRING>
bool verify(
const snarklib::PPZK_Keypair<PAIRING>& keypair,
const R1Cowitness<typename PAIRING::Fr>& input,
const snarklib::PPZK_Proof<PAIRING>& proof,
snarklib::ProgressCallback& callback)
{
return verify(keypair.vk(), input, proof, callback);
}
////////////////////////////////////////////////////////////////////////////////
// terminate circuits, constrain final proof output
//
template <typename FR>
void assert_true(const AST_Var<Alg_bool<FR>>& x) {
TL<R1C<FR>>::singleton()->setTrue(x->r1Terms()[0]);
}
template <typename FR>
void assert_false(const AST_Var<Alg_bool<FR>>& x) {
TL<R1C<FR>>::singleton()->setFalse(x->r1Terms()[0]);
}
template <typename FR>
void assert_true(const AST_X<Alg_bool<FR>>& a) {
assert_true(bool_x<FR>(a));
}
template <typename FR>
void assert_false(const AST_X<Alg_bool<FR>>& a) {
assert_false(bool_x<FR>(a));
}
} // namespace snarkfront
#endif