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
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::{r1cs_to_qap::R1CSToQAP, Groth16, ProvingKey, Vec, VerifyingKey};
use ark_ec::{pairing::Pairing, scalar_mul::fixed_base::FixedBase, CurveGroup, Group};
use ark_ff::{Field, PrimeField, UniformRand, Zero};
use ark_poly::{EvaluationDomain, GeneralEvaluationDomain};
use ark_relations::r1cs::{
ConstraintSynthesizer, ConstraintSystem, OptimizationGoal, Result as R1CSResult,
SynthesisError, SynthesisMode,
};
use ark_std::rand::Rng;
use ark_std::{cfg_into_iter, cfg_iter};
#[cfg(feature = "parallel")]
use rayon::prelude::*;
impl<E: Pairing, QAP: R1CSToQAP> Groth16<E, QAP> {
#[inline]
pub fn generate_random_parameters_with_reduction<C>(
circuit: C,
rng: &mut impl Rng,
) -> R1CSResult<ProvingKey<E>>
where
C: ConstraintSynthesizer<E::ScalarField>,
{
let alpha = E::ScalarField::rand(rng);
let beta = E::ScalarField::rand(rng);
let gamma = E::ScalarField::rand(rng);
let delta = E::ScalarField::rand(rng);
let g1_generator = E::G1::rand(rng);
let g2_generator = E::G2::rand(rng);
Self::generate_parameters_with_qap(
circuit,
alpha,
beta,
gamma,
delta,
g1_generator,
g2_generator,
rng,
)
}
pub fn generate_parameters_with_qap<C>(
circuit: C,
alpha: E::ScalarField,
beta: E::ScalarField,
gamma: E::ScalarField,
delta: E::ScalarField,
g1_generator: E::G1,
g2_generator: E::G2,
rng: &mut impl Rng,
) -> R1CSResult<ProvingKey<E>>
where
C: ConstraintSynthesizer<E::ScalarField>,
{
type D<F> = GeneralEvaluationDomain<F>;
let setup_time = start_timer!(|| "Groth16::Generator");
let cs = ConstraintSystem::new_ref();
cs.set_optimization_goal(OptimizationGoal::Constraints);
cs.set_mode(SynthesisMode::Setup);
let synthesis_time = start_timer!(|| "Constraint synthesis");
circuit.generate_constraints(cs.clone())?;
end_timer!(synthesis_time);
let lc_time = start_timer!(|| "Inlining LCs");
cs.finalize();
end_timer!(lc_time);
let domain_time = start_timer!(|| "Constructing evaluation domain");
let domain_size = cs.num_constraints() + cs.num_instance_variables();
let domain = D::new(domain_size).ok_or(SynthesisError::PolynomialDegreeTooLarge)?;
let t = domain.sample_element_outside_domain(rng);
end_timer!(domain_time);
let reduction_time = start_timer!(|| "R1CS to QAP Instance Map with Evaluation");
let num_instance_variables = cs.num_instance_variables();
let (a, b, c, zt, qap_num_variables, m_raw) =
QAP::instance_map_with_evaluation::<E::ScalarField, D<E::ScalarField>>(cs, &t)?;
end_timer!(reduction_time);
let non_zero_a: usize = cfg_into_iter!(0..qap_num_variables)
.map(|i| usize::from(!a[i].is_zero()))
.sum();
let non_zero_b: usize = cfg_into_iter!(0..qap_num_variables)
.map(|i| usize::from(!b[i].is_zero()))
.sum();
let scalar_bits = E::ScalarField::MODULUS_BIT_SIZE as usize;
let gamma_inverse = gamma.inverse().ok_or(SynthesisError::UnexpectedIdentity)?;
let delta_inverse = delta.inverse().ok_or(SynthesisError::UnexpectedIdentity)?;
let gamma_abc = cfg_iter!(a[..num_instance_variables])
.zip(&b[..num_instance_variables])
.zip(&c[..num_instance_variables])
.map(|((a, b), c)| (beta * a + &(alpha * b) + c) * &gamma_inverse)
.collect::<Vec<_>>();
let l = cfg_iter!(a[num_instance_variables..])
.zip(&b[num_instance_variables..])
.zip(&c[num_instance_variables..])
.map(|((a, b), c)| (beta * a + &(alpha * b) + c) * &delta_inverse)
.collect::<Vec<_>>();
drop(c);
let g2_time = start_timer!(|| "Compute G2 table");
let g2_window = FixedBase::get_mul_window_size(non_zero_b);
let g2_table = FixedBase::get_window_table::<E::G2>(scalar_bits, g2_window, g2_generator);
end_timer!(g2_time);
let b_g2_time = start_timer!(|| "Calculate B G2");
let b_g2_query = FixedBase::msm::<E::G2>(scalar_bits, g2_window, &g2_table, &b);
drop(g2_table);
end_timer!(b_g2_time);
let g1_window_time = start_timer!(|| "Compute G1 window table");
let g1_window =
FixedBase::get_mul_window_size(non_zero_a + non_zero_b + qap_num_variables + m_raw + 1);
let g1_table = FixedBase::get_window_table::<E::G1>(scalar_bits, g1_window, g1_generator);
end_timer!(g1_window_time);
let proving_key_time = start_timer!(|| "Generate the R1CS proving key");
let alpha_g1 = g1_generator.mul_bigint(&alpha.into_bigint());
let beta_g1 = g1_generator.mul_bigint(&beta.into_bigint());
let beta_g2 = g2_generator.mul_bigint(&beta.into_bigint());
let delta_g1 = g1_generator.mul_bigint(&delta.into_bigint());
let delta_g2 = g2_generator.mul_bigint(&delta.into_bigint());
let a_time = start_timer!(|| "Calculate A");
let a_query = FixedBase::msm::<E::G1>(scalar_bits, g1_window, &g1_table, &a);
drop(a);
end_timer!(a_time);
let b_g1_time = start_timer!(|| "Calculate B G1");
let b_g1_query = FixedBase::msm::<E::G1>(scalar_bits, g1_window, &g1_table, &b);
drop(b);
end_timer!(b_g1_time);
let h_time = start_timer!(|| "Calculate H");
let h_query = FixedBase::msm::<E::G1>(
scalar_bits,
g1_window,
&g1_table,
&QAP::h_query_scalars::<_, D<E::ScalarField>>(m_raw - 1, t, zt, delta_inverse)?,
);
end_timer!(h_time);
let l_time = start_timer!(|| "Calculate L");
let l_query = FixedBase::msm::<E::G1>(scalar_bits, g1_window, &g1_table, &l);
drop(l);
end_timer!(l_time);
end_timer!(proving_key_time);
let verifying_key_time = start_timer!(|| "Generate the R1CS verification key");
let gamma_g2 = g2_generator.mul_bigint(&gamma.into_bigint());
let gamma_abc_g1 = FixedBase::msm::<E::G1>(scalar_bits, g1_window, &g1_table, &gamma_abc);
drop(g1_table);
end_timer!(verifying_key_time);
let vk = VerifyingKey::<E> {
alpha_g1: alpha_g1.into_affine(),
beta_g2: beta_g2.into_affine(),
gamma_g2: gamma_g2.into_affine(),
delta_g2: delta_g2.into_affine(),
gamma_abc_g1: E::G1::normalize_batch(&gamma_abc_g1),
};
let batch_normalization_time = start_timer!(|| "Convert proving key elements to affine");
let a_query = E::G1::normalize_batch(&a_query);
let b_g1_query = E::G1::normalize_batch(&b_g1_query);
let b_g2_query = E::G2::normalize_batch(&b_g2_query);
let h_query = E::G1::normalize_batch(&h_query);
let l_query = E::G1::normalize_batch(&l_query);
end_timer!(batch_normalization_time);
end_timer!(setup_time);
Ok(ProvingKey {
vk,
beta_g1: beta_g1.into_affine(),
delta_g1: delta_g1.into_affine(),
a_query,
b_g1_query,
b_g2_query,
h_query,
l_query,
})
}
}