-
Notifications
You must be signed in to change notification settings - Fork 1
/
proof.cu
200 lines (170 loc) · 8.35 KB
/
proof.cu
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
#include "proof.cuh"
vector<Fr_t> random_vec(uint len)
{
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<unsigned int> dist(0, UINT_MAX);
vector<Fr_t> out(len);
for (uint i = 0; i < len; ++ i) out[i] = {dist(mt), dist(mt), dist(mt), dist(mt), dist(mt), dist(mt), dist(mt), dist(mt) % 1944954707};
return out;
}
uint ceilLog2(uint num) {
if (num == 0) return 0;
// Decrease num to handle the case where num is already a power of 2
num--;
uint result = 0;
// Keep shifting the number to the right until it becomes zero.
// Each shift means the number is halved, which corresponds to
// a division by 2 in logarithmic terms.
while (num > 0) {
num >>= 1;
result++;
}
return result;
}
template<typename T>
std::vector<T> concatenate(const std::vector<std::vector<T>>& vecs) {
// First, compute the total size for the result vector.
size_t totalSize = 0;
for (const auto& v : vecs) {
totalSize += v.size();
}
// Allocate space for the result vector.
std::vector<T> result;
result.reserve(totalSize);
// Append each vector's contents to the result vector.
for (const auto& v : vecs) {
result.insert(result.end(), v.begin(), v.end());
}
return result;
}
// specify to the compiler that this function needs to be compiled for Fr_t otherwise it cannot be linked
template std::vector<Fr_t> concatenate(const std::vector<std::vector<Fr_t>>& vecs);
KERNEL void Fr_ip_sc_step(GLOBAL Fr_t *a, GLOBAL Fr_t *b, GLOBAL Fr_t *out0, GLOBAL Fr_t *out1, GLOBAL Fr_t *out2, uint in_size, uint out_size)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= out_size) return;
uint gid0 = 2 * gid;
uint gid1 = 2 * gid + 1;
Fr_t a0 = (gid0 < in_size) ? a[gid0] : blstrs__scalar__Scalar_ZERO;
Fr_t b0 = (gid0 < in_size) ? b[gid0] : blstrs__scalar__Scalar_ZERO;
Fr_t a1 = (gid1 < in_size) ? a[gid1] : blstrs__scalar__Scalar_ZERO;
Fr_t b1 = (gid1 < in_size) ? b[gid1] : blstrs__scalar__Scalar_ZERO;
out0[gid] = blstrs__scalar__Scalar_mul(a0, b0);
out1[gid] = blstrs__scalar__Scalar_add(blstrs__scalar__Scalar_mul(a0, blstrs__scalar__Scalar_sub(b1, b0)),
blstrs__scalar__Scalar_mul(b0, blstrs__scalar__Scalar_sub(a1, a0)));
out2[gid] = blstrs__scalar__Scalar_mul(blstrs__scalar__Scalar_sub(a1, a0), blstrs__scalar__Scalar_sub(b1, b0));
}
void Fr_ip_sc(const FrTensor& a, const FrTensor& b, vector<Fr_t>::const_iterator begin, vector<Fr_t>::const_iterator end, vector<Fr_t>& proof)
{
if (a.size != b.size) throw std::runtime_error("Incompatible dimensions");
if (begin >= end) {
proof.push_back(a(0));
proof.push_back(b(0));
return;
}
auto in_size = a.size;
auto out_size = (in_size + 1) / 2;
FrTensor out0(out_size), out1(out_size), out2(out_size);
Fr_ip_sc_step<<<(out_size+FrNumThread-1)/FrNumThread,FrNumThread>>>(a.gpu_data, b.gpu_data, out0.gpu_data, out1.gpu_data, out2.gpu_data, in_size, out_size);
cudaDeviceSynchronize();
proof.push_back(out0.sum());
proof.push_back(out1.sum());
proof.push_back(out2.sum());
FrTensor a_new(out_size), b_new(out_size);
Fr_me_step<<<(out_size+FrNumThread-1)/FrNumThread,FrNumThread>>>(a.gpu_data, a_new.gpu_data, *begin, in_size, out_size);
cudaDeviceSynchronize();
Fr_me_step<<<(out_size+FrNumThread-1)/FrNumThread,FrNumThread>>>(b.gpu_data, b_new.gpu_data, *begin, in_size, out_size);
cudaDeviceSynchronize();
Fr_ip_sc(a_new, b_new, begin + 1, end, proof);
}
vector<Fr_t> inner_product_sumcheck(const FrTensor& a, const FrTensor& b, vector<Fr_t> u)
{
vector<Fr_t> proof;
uint log_size = u.size();
if (a.size != b.size) throw std::runtime_error("Incompatible dimensions");
if (a.size <= (1 << (log_size))/2) throw std::runtime_error("Incompatible dimensions");
if (a.size > (1 << log_size)) throw std::runtime_error("Incompatible dimensions");
Fr_ip_sc(a, b, u.begin(), u.end(), proof);
return proof;
}
void Fr_hp_sc(const FrTensor& a, const FrTensor& b, vector<Fr_t>::const_iterator u_begin, vector<Fr_t>::const_iterator u_end, vector<Fr_t>::const_iterator v_begin, vector<Fr_t>::const_iterator v_end, vector<Fr_t>& proof)
{
if (a.size != b.size) throw std::runtime_error("Incompatible dimensions 5");
if (v_end - v_begin != u_end - u_begin) throw std::runtime_error("Incompatible dimensions 6");
if (v_begin >= v_end) {
proof.push_back(a(0));
proof.push_back(b(0));
return;
}
auto in_size = a.size;
auto out_size = (in_size + 1) / 2;
FrTensor out0(out_size), out1(out_size), out2(out_size);
Fr_ip_sc_step<<<(out_size+FrNumThread-1)/FrNumThread,FrNumThread>>>(a.gpu_data, b.gpu_data, out0.gpu_data, out1.gpu_data, out2.gpu_data, in_size, out_size);
cudaDeviceSynchronize();
vector<Fr_t> u_(u_begin + 1, u_end);
//std::cout << u_.size() << "\t" << out0.size << "\t" << out1.size << "\t" << out2.size << std::endl;
proof.push_back(out0(u_));
proof.push_back(out1(u_));
proof.push_back(out2(u_));
FrTensor a_new(out_size), b_new(out_size);
Fr_me_step<<<(out_size+FrNumThread-1)/FrNumThread,FrNumThread>>>(a.gpu_data, a_new.gpu_data, *v_begin, in_size, out_size);
cudaDeviceSynchronize();
Fr_me_step<<<(out_size+FrNumThread-1)/FrNumThread,FrNumThread>>>(b.gpu_data, b_new.gpu_data, *v_begin, in_size, out_size);
cudaDeviceSynchronize();
Fr_hp_sc(a_new, b_new, u_begin + 1, u_end, v_begin + 1, v_end, proof);
}
vector<Fr_t> hadamard_product_sumcheck(const FrTensor& a, const FrTensor& b, vector<Fr_t> u, vector<Fr_t> v)
{
vector<Fr_t> proof;
if (u.size() != v.size()) throw std::runtime_error("Incompatible dimensions 1");
uint log_size = u.size();
if (a.size != b.size) throw std::runtime_error("Incompatible dimensions 2");
if (a.size <= (1 << (log_size - 1))) throw std::runtime_error("Incompatible dimensions 3");
if (a.size > (1 << log_size)) throw std::runtime_error("Incompatible dimensions 4");
Fr_hp_sc(a, b, u.begin(), u.end(), v.begin(), v.end(), proof);
return proof;
}
KERNEL void Fr_bin_sc_step(GLOBAL Fr_t *a, GLOBAL Fr_t *out0, GLOBAL Fr_t *out1, GLOBAL Fr_t *out2, uint in_size, uint out_size)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= out_size) return;
Fr_t a0 = (2 * gid < in_size) ? a[2 * gid] : blstrs__scalar__Scalar_ZERO;
Fr_t a1 = (2 * gid + 1 < in_size) ? a[2 * gid + 1] : blstrs__scalar__Scalar_ZERO;
out0[gid] = blstrs__scalar__Scalar_sub(blstrs__scalar__Scalar_mul(a0, a0), a0);
Fr_t diff = blstrs__scalar__Scalar_sub(a1, a0);
out1[gid] = blstrs__scalar__Scalar_sub(blstrs__scalar__Scalar_mul(blstrs__scalar__Scalar_double(a0), diff), diff);
out2[gid] = blstrs__scalar__Scalar_sqr(diff);
}
void Fr_bin_sc(const FrTensor& a, vector<Fr_t>::const_iterator u_begin, vector<Fr_t>::const_iterator u_end, vector<Fr_t>::const_iterator v_begin, vector<Fr_t>::const_iterator v_end, vector<Fr_t>& proof)
{
if (v_end - v_begin != u_end - u_begin) throw std::runtime_error("Incompatible dimensions 6");
if (v_begin >= v_end) {
proof.push_back(a(0));
return;
}
auto in_size = a.size;
auto out_size = (in_size + 1) / 2;
FrTensor out0(out_size), out1(out_size), out2(out_size);
Fr_bin_sc_step<<<(out_size+FrNumThread-1)/FrNumThread,FrNumThread>>>(a.gpu_data, out0.gpu_data, out1.gpu_data, out2.gpu_data, in_size, out_size);
cudaDeviceSynchronize();
vector<Fr_t> u_(u_begin + 1, u_end);
//std::cout << u_.size() << "\t" << out0.size << "\t" << out1.size << "\t" << out2.size << std::endl;
proof.push_back(out0(u_));
proof.push_back(out1(u_));
proof.push_back(out2(u_));
FrTensor a_new(out_size);
Fr_me_step<<<(out_size+FrNumThread-1)/FrNumThread,FrNumThread>>>(a.gpu_data, a_new.gpu_data, *v_begin, in_size, out_size);
cudaDeviceSynchronize();
Fr_bin_sc(a_new, u_begin + 1, u_end, v_begin + 1, v_end, proof);
}
vector<Fr_t> binary_sumcheck(const FrTensor& a, vector<Fr_t> u, vector<Fr_t> v)
{
vector<Fr_t> proof;
if (u.size() != v.size()) throw std::runtime_error("Incompatible dimensions");
uint log_size = u.size();
if (a.size <= (1 << (log_size))/2) throw std::runtime_error("Incompatible dimensions");
if (a.size > (1 << log_size)) throw std::runtime_error("Incompatible dimensions");
Fr_bin_sc(a, u.begin(), u.end(), v.begin(), v.end(), proof);
return proof;
}