forked from iden3/rapidsnark-old
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprover.cpp
144 lines (113 loc) · 4.17 KB
/
prover.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
#include <gmp.h>
#include <memory>
#include <string>
#include <cstring>
#include <stdexcept>
#include <alt_bn128.hpp>
#include <nlohmann/json.hpp>
#include "prover.h"
#include "zkey_utils.hpp"
#include "wtns_utils.hpp"
#include "groth16.hpp"
#include "binfile_utils.hpp"
using json = nlohmann::json;
static size_t ProofBufferMinSize()
{
return 726;
}
static size_t PublicBufferMinSize(size_t count)
{
return count * 81 + 3;
}
static void VerifyPrimes(mpz_srcptr zkey_prime, mpz_srcptr wtns_prime)
{
mpz_t altBbn128r;
mpz_init(altBbn128r);
mpz_set_str(altBbn128r, "21888242871839275222246405745257275088548364400416034343698204186575808495617", 10);
if (mpz_cmp(zkey_prime, altBbn128r) != 0) {
throw std::invalid_argument( "zkey curve not supported" );
}
if (mpz_cmp(wtns_prime, altBbn128r) != 0) {
throw std::invalid_argument( "different wtns curve" );
}
mpz_clear(altBbn128r);
}
std::string BuildPublicString(AltBn128::FrElement *wtnsData, size_t nPublic)
{
json jsonPublic;
AltBn128::FrElement aux;
for (u_int32_t i=1; i<= nPublic; i++) {
AltBn128::Fr.toMontgomery(aux, wtnsData[i]);
jsonPublic.push_back(AltBn128::Fr.toString(aux));
}
return jsonPublic.dump();
}
int
groth16_prover(const void *zkey_buffer, unsigned long zkey_size,
const void *wtns_buffer, unsigned long wtns_size,
char *proof_buffer, unsigned long *proof_size,
char *public_buffer, unsigned long *public_size,
char *error_msg, unsigned long error_msg_maxsize)
{
try {
BinFileUtils::BinFile zkey(zkey_buffer, zkey_size, "zkey", 1);
auto zkeyHeader = ZKeyUtils::loadHeader(&zkey);
BinFileUtils::BinFile wtns(wtns_buffer, wtns_size, "wtns", 2);
auto wtnsHeader = WtnsUtils::loadHeader(&wtns);
size_t proofMinSize = ProofBufferMinSize();
size_t publicMinSize = PublicBufferMinSize(zkeyHeader->nPublic);
if (*proof_size < proofMinSize || *public_size < publicMinSize) {
*proof_size = proofMinSize;
*public_size = publicMinSize;
return PPROVER_ERROR_SHORT_BUFFER;
}
VerifyPrimes(zkeyHeader->rPrime, wtnsHeader->prime);
auto prover = Groth16::makeProver<AltBn128::Engine>(
zkeyHeader->nVars,
zkeyHeader->nPublic,
zkeyHeader->domainSize,
zkeyHeader->nCoefs,
zkeyHeader->vk_alpha1,
zkeyHeader->vk_beta1,
zkeyHeader->vk_beta2,
zkeyHeader->vk_delta1,
zkeyHeader->vk_delta2,
zkey.getSectionData(4), // Coefs
zkey.getSectionData(5), // pointsA
zkey.getSectionData(6), // pointsB1
zkey.getSectionData(7), // pointsB2
zkey.getSectionData(8), // pointsC
zkey.getSectionData(9) // pointsH1
);
AltBn128::FrElement *wtnsData = (AltBn128::FrElement *)wtns.getSectionData(2);
auto proof = prover->prove(wtnsData);
std::string stringProof = proof->toJson().dump();
std::string stringPublic = BuildPublicString(wtnsData, zkeyHeader->nPublic);
size_t stringProofSize = stringProof.length();
size_t stringPublicSize = stringPublic.length();
if (*proof_size < stringProofSize || *public_size < stringPublicSize) {
*proof_size = stringProofSize;
*public_size = stringPublicSize;
return PPROVER_ERROR_SHORT_BUFFER;
}
std::strncpy(proof_buffer, stringProof.data(), *proof_size);
std::strncpy(public_buffer, stringPublic.data(), *public_size);
} catch (std::exception& e) {
if (error_msg) {
strncpy(error_msg, e.what(), error_msg_maxsize);
}
return PPROVER_ERROR;
} catch (std::exception *e) {
if (error_msg) {
strncpy(error_msg, e->what(), error_msg_maxsize);
}
delete e;
return PPROVER_ERROR;
} catch (...) {
if (error_msg) {
strncpy(error_msg, "unknown error", error_msg_maxsize);
}
return PPROVER_ERROR;
}
return PRPOVER_OK;
}