forked from jvhs0706/zkllm-ccs2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommitment.cu
148 lines (126 loc) · 5.98 KB
/
commitment.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
#include "commitment.cuh"
Commitment Commitment::random(uint size)
{
Commitment out(size, G1Jacobian_generator);
out *= FrTensor::random(size);
return out;
}
// KERNEL void com_sum_row_kernel(const G1Jacobian_t* arr, G1Jacobian_t* arr_out, uint m, uint n) {
// auto row = GET_GLOBAL_ID();
// if (row < m) {
// G1Jacobian_t rowSum = arr[row * n];
// for (uint i = 1; i < n; ++ i) {
// rowSum = blstrs__g1__G1Affine_add(rowSum, arr[row * n + i]);
// }
// arr_out[row] = rowSum;
// }
// }
G1TensorJacobian Commitment::commit(const FrTensor& t) const
{
if (t.size % size != 0) throw std::runtime_error("Commitment::commit - Incompatible dimensions");
uint m = t.size / size;
G1TensorJacobian temp = (*this) * t;
return temp.rowwise_sum(m, size);
}
DEVICE G1Jacobian_t commit_int_dev_func(G1Jacobian_t a, Fr_t s) {
const int x = scalar_to_int(s);
G1Jacobian_t out = blstrs__g1__G1Affine_ZERO;
#pragma unroll
for (uint i = 0; i < 31; ++ i) {
if ((x >> i) & 1) out = blstrs__g1__G1Affine_add(out, a);
a = blstrs__g1__G1Affine_double(a);
}
if (x < 0) out = blstrs__g1__G1Affine_add(out, G1Jacobian_minus(a));
return out;
}
KERNEL void commit_int_kernel(const G1Jacobian_t* generators, const Fr_t* scalars, G1Jacobian_t* out, uint n, uint m) {
const uint gid = GET_GLOBAL_ID();
if (gid >= m * n) return;
out[gid] = commit_int_dev_func(generators[gid % n], scalars[gid]);
}
G1TensorJacobian Commitment::commit_int (const FrTensor& t) const{
if (t.size % size != 0) throw std::runtime_error("Commitment::commit_int - Incompatible dimensions");
uint m = t.size / size;
G1TensorJacobian temp(t.size);
commit_int_kernel<<<(m*size+G1NumThread-1)/G1NumThread,G1NumThread>>>(gpu_data, t.gpu_data, temp.gpu_data, size, m);
cudaDeviceSynchronize();
return temp.rowwise_sum(m, size);
}
G1TensorJacobian Commitment::commit_int_multi(const vector<FrTensor>& ts) const{
uint num_row = 0;
for (auto& t : ts) {
if (t.size % size != 0) throw std::runtime_error("Commitment::commit_int_multi - Incompatible dimensions");
num_row += t.size / size;
}
G1TensorJacobian temp(num_row * size);
auto temp_start = temp.gpu_data;
for (auto& t: ts)
{
uint m = t.size / size;
commit_int_kernel<<<(m*size+G1NumThread-1)/G1NumThread,G1NumThread>>>(gpu_data, t.gpu_data, temp_start, size, m);
cudaDeviceSynchronize();
temp_start += m * size;
}
return temp.rowwise_sum(temp.size / size, size);
}
KERNEL void me_open_step(GLOBAL Fr_t* scalars, GLOBAL G1Jacobian_t* generators, Fr_t u, // always assume that scalars and u is in mont form
GLOBAL Fr_t* new_scalars, GLOBAL G1Jacobian_t* new_generators,
GLOBAL G1Jacobian_t* temp_out, GLOBAL G1Jacobian_t* temp_out0, GLOBAL G1Jacobian_t* temp_out1,
uint old_size, uint new_size)
{
const uint gid = GET_GLOBAL_ID();
if (gid >= new_size) return;
uint gid0 = 2 * gid;
uint gid1 = 2 * gid + 1;
if (gid1 >= old_size) {
new_scalars[gid] = blstrs__scalar__Scalar_sub(scalars[gid0],
blstrs__scalar__Scalar_mont(blstrs__scalar__Scalar_mul(u, scalars[gid0]))
);
new_generators[gid] = G1Jacobian_mul(generators[gid0], u);
temp_out[gid] = G1Jacobian_mul(generators[gid0], scalars[gid0]);
temp_out0[gid] = blstrs__g1__G1Affine_ZERO;
temp_out1[gid] = blstrs__g1__G1Affine_ZERO;
return;
}
new_scalars[gid] = blstrs__scalar__Scalar_add(scalars[gid0], blstrs__scalar__Scalar_mont(blstrs__scalar__Scalar_mul(u, blstrs__scalar__Scalar_sub(scalars[gid1], scalars[gid0]))));
new_generators[gid] = blstrs__g1__G1Affine_add(generators[gid1], G1Jacobian_mul(blstrs__g1__G1Affine_add(generators[gid0], G1Jacobian_minus(generators[gid1])), u));
temp_out[gid] = blstrs__g1__G1Affine_add(G1Jacobian_mul(generators[gid0], scalars[gid0]), G1Jacobian_mul(generators[gid1], scalars[gid1]));
temp_out0[gid] = G1Jacobian_mul(generators[gid1], scalars[gid0]);
temp_out1[gid] = G1Jacobian_mul(generators[gid0], scalars[gid1]);
}
Fr_t Commitment::me_open(const FrTensor& t, const Commitment& generators, vector<Fr_t>::const_iterator begin, vector<Fr_t>::const_iterator end, vector<G1Jacobian_t>& proof)
{
if (t.size != generators.size) throw std::runtime_error("Commitment::me_open - Incompatible dimensions "+ std::to_string(t.size) + " " + std::to_string(generators.size));
if (begin >= end)
{
proof.push_back(generators(0));
return t(0);
}
uint new_size = (t.size + 1) / 2;
FrTensor new_scalars(new_size);
Commitment new_generators(new_size);
G1TensorJacobian temp(new_size), temp0(new_size), temp1(new_size);
me_open_step<<<(new_size+G1NumThread-1)/G1NumThread,G1NumThread>>>(t.gpu_data, generators.gpu_data, *begin,
new_scalars.gpu_data, new_generators.gpu_data, temp.gpu_data, temp0.gpu_data, temp1.gpu_data,
t.size, new_size);
cudaDeviceSynchronize();
proof.push_back(temp.sum());
proof.push_back(temp0.sum());
proof.push_back(temp1.sum());
return me_open(new_scalars, new_generators, begin + 1, end, proof);
}
Fr_t Commitment::open(const FrTensor& t, const G1TensorJacobian& com, const vector<Fr_t>& u) const
{
const vector<Fr_t> u_out(u.end() - ceilLog2(com.size), u.end());
const vector<Fr_t> u_in(u.begin(), u.end() - ceilLog2(com.size));
auto g_temp = (com.size == 1)? com(0) : com(u_out);
// if (size != (1 << u_in.size())) throw std::runtime_error("Incompatible dimensions");
vector<G1Jacobian_t> proof;
return me_open(t.partial_me(u_out, t.size / com.size), *this, u_in.begin(), u_in.end(), proof);
}
Weight create_weight(string generator_filename, string weight_filename, string com_filename, uint in_dim, uint out_dim) {
Commitment generator(generator_filename);
FrTensor weight = FrTensor::from_int_bin(weight_filename);
G1TensorJacobian com(com_filename);
return {generator, weight, com, in_dim, out_dim};
}