Skip to content

Commit 07b954f

Browse files
committed
fix
1 parent 8263578 commit 07b954f

File tree

5 files changed

+88
-89
lines changed

5 files changed

+88
-89
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33
Cargo.lock
44
.vscode
55
**/*.html
6-
output/
7-
run-cost-model.sh

halo2_proofs/examples/simple-example-cost-model.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use halo2_proofs::{
22
arithmetic::FieldExt,
33
circuit::{Cell, Layouter, SimpleFloorPlanner},
4+
cost_model_main,
45
plonk::*,
5-
poly::Rotation, cost_model_main,
6+
poly::Rotation,
67
};
78
use pairing::bn256::{Bn256, Fr as Fp};
89

@@ -236,4 +237,7 @@ impl<F: FieldExt> Circuit<F> for MyCircuit<F> {
236237
}
237238
}
238239

239-
cost_model_main!(MyCircuit::<Fp>{a: Some(Fp::from(5)), k: 8});
240+
cost_model_main!(MyCircuit::<Fp> {
241+
a: Some(Fp::from(5)),
242+
k: 8
243+
});

halo2_proofs/src/dev/cost_model.rs

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
//! Circuit cost model.
2-
use std::{
3-
time::Instant, io, fs, collections::BTreeMap, mem,
4-
};
2+
use std::{collections::BTreeMap, fs, io, mem, time::Instant};
53

64
use crate::{
7-
arithmetic::{Field, CurveAffine, Engine, eval_polynomial},
5+
arithmetic::{eval_polynomial, CurveAffine, Engine, Field},
86
circuit::{Cell, Layouter, SimpleFloorPlanner},
7+
multicore,
98
plonk::*,
109
poly::{commitment::Params, commitment::ParamsVerifier, EvaluationDomain, Rotation},
11-
transcript::{Blake2bRead, Blake2bWrite, Challenge255}, multicore,
10+
transcript::{Blake2bRead, Blake2bWrite, Challenge255},
1211
};
1312
use group::{prime::PrimeCurveAffine, GroupEncoding};
1413
use pairing::bn256::{Bn256, Fr as Fp, G1Affine};
@@ -41,12 +40,10 @@ impl EstimateResult {
4140
}
4241

4342
impl Calculation {
44-
fn fake_evaluate<F: Field>(
45-
&self,
46-
) -> usize {
43+
fn fake_evaluate<F: Field>(&self) -> usize {
4744
match self {
4845
Calculation::Add(_, _) => 0,
49-
Calculation::Sub(_, _) => 0,
46+
Calculation::Sub(_, _) => 0,
5047
Calculation::Mul(_, _) => 1,
5148
Calculation::Negate(_) => 0,
5249
Calculation::LcBeta(_, _) => 1,
@@ -58,11 +55,7 @@ impl Calculation {
5855
}
5956

6057
impl<C: CurveAffine> Evaluator<C> {
61-
fn fake_evaluate_h(
62-
&self,
63-
pk: &ProvingKey<C>,
64-
l: usize,
65-
) -> usize {
58+
fn fake_evaluate_h(&self, pk: &ProvingKey<C>, l: usize) -> usize {
6659
let cs = pk.get_vk().get_cs();
6760
let mut num_mul = 0;
6861
// All calculations, with cached intermediate results
@@ -96,7 +89,7 @@ impl<C: CurveAffine> Evaluator<C> {
9689
if num_perm_slices > 0 {
9790
num_mul += 2 * (num_perm_slices - 1);
9891
}
99-
92+
10093
// delta_start * beta_start
10194
num_mul += 1;
10295
// And for all the sets we enforce:
@@ -156,15 +149,12 @@ impl<C: CurveAffine> Evaluator<C> {
156149
}
157150

158151
/// estimate is to estimate the prover time, peek memory usage and aggregate circuit size.
159-
pub fn estimate<
160-
E: Engine,
161-
ConcreteCircuit: Circuit<E::Scalar>,
162-
>(
152+
pub fn estimate<E: Engine, ConcreteCircuit: Circuit<E::Scalar>>(
163153
circuit: ConcreteCircuit,
164154
k: usize,
165155
) -> EstimateResult {
166156
// Generate small vk & pk
167-
let params: Params<E::G1Affine> = Params::<E::G1Affine>::unsafe_setup::<E>(15 as u32);
157+
let params: Params<E::G1Affine> = Params::<E::G1Affine>::unsafe_setup::<E>(k as u32);
168158
let vk = keygen_vk(&params, &circuit).expect("keygen_vk should not fail");
169159
let pk = keygen_pk(&params, vk, &circuit).expect("keygen_pk should not fail");
170160

@@ -183,20 +173,20 @@ pub fn estimate<
183173
Params {
184174
k: k as u32,
185175
n: n as u64,
186-
g: (0..n).map(|_| rand_c1.clone()).collect(),
187-
g_lagrange: (0..n).map(|_| rand_c1.clone()).collect(),
176+
g: (0..n).map(|_| rand_c1).collect(),
177+
g_lagrange: (0..n).map(|_| rand_c1).collect(),
188178
additional_data: Vec::from(rand_c2.to_bytes().as_ref()),
189179
}
190180
};
191181

192182
let params = generate_fake_params(k);
193-
183+
194184
// Initialize the domain
195185
let domain = EvaluationDomain::fake_new(cs.degree() as u32, params.k, E::Scalar::random(OsRng));
196-
186+
197187
let n = 1 << k as usize;
198188
let rand_ele = E::Scalar::random(&mut OsRng);
199-
let rand_vec: Vec::<E::Scalar> = (0..n).map(|_| rand_ele.clone()).collect();
189+
let rand_vec: Vec<E::Scalar> = (0..n).map(|_| rand_ele).collect();
200190
let rand_vec2 = rand_vec.clone();
201191
let rand_values = domain.lagrange_from_vec(rand_vec);
202192

@@ -209,12 +199,14 @@ pub fn estimate<
209199
let (time_extended_fft, _) = measure_elapsed_time(|| domain.coeff_to_extended(rand_poly));
210200
// BTree time cost in lookup argument
211201
let (time_btree, _) = measure_elapsed_time(|| {
212-
let mut leftover_table_map: BTreeMap<E::Scalar, u32> = rand_vec2
213-
.iter().take(n)
214-
.fold(BTreeMap::new(), |mut acc, coeff| {
215-
*acc.entry(*coeff).or_insert(0) += 1;
216-
acc
217-
});
202+
let mut leftover_table_map: BTreeMap<E::Scalar, u32> =
203+
rand_vec2
204+
.iter()
205+
.take(n)
206+
.fold(BTreeMap::new(), |mut acc, coeff| {
207+
*acc.entry(*coeff).or_insert(0) += 1;
208+
acc
209+
});
218210
for item in &rand_vec2 {
219211
if let Some(count) = leftover_table_map.get_mut(item) {
220212
*count -= 1;
@@ -225,17 +217,19 @@ pub fn estimate<
225217
let num_threads = multicore::current_num_threads();
226218

227219
// NOTE(sphere): estimate op count
228-
let FuncCount { num_fft, num_extended_fft, num_msm, num_btree, num_mul, mem_usage} = dummy_proof(
229-
&params,
230-
&pk,
231-
&domain,
232-
l,
233-
);
220+
let FuncCount {
221+
num_fft,
222+
num_extended_fft,
223+
num_msm,
224+
num_btree,
225+
num_mul,
226+
mem_usage,
227+
} = dummy_proof(&params, &pk, &domain, l);
234228

235229
let estimate_add_mul_field_op_time = || {
236230
let m = (domain.extended_len() + num_threads - 1) / num_threads;
237-
let a = rand_ele.clone();
238-
let mut b = rand_ele.clone();
231+
let a = rand_ele;
232+
let mut b = rand_ele;
239233
// m mul field ops
240234
let (time_mul, _) = measure_elapsed_time(|| {
241235
for _ in 0..m {
@@ -248,15 +242,17 @@ pub fn estimate<
248242
};
249243

250244
println!("num_fft = {}, time_fft = {}", num_fft, time_fft);
251-
println!("num_extended_fft = {}, time_extended_fft = {}", num_extended_fft, time_extended_fft);
245+
println!(
246+
"num_extended_fft = {}, time_extended_fft = {}",
247+
num_extended_fft, time_extended_fft
248+
);
252249
println!("num_msm = {}, time_msm = {}", num_msm, time_msm);
253250
println!("num_btree = {}, time_btree = {}", num_btree, time_btree);
254-
255251

256-
let pt_non_linear = (num_fft as f64) * time_fft +
257-
(num_extended_fft as f64) * time_extended_fft +
258-
(num_msm as f64) * time_msm +
259-
(num_btree as f64) * time_btree;
252+
let pt_non_linear = (num_fft as f64) * time_fft
253+
+ (num_extended_fft as f64) * time_extended_fft
254+
+ (num_msm as f64) * time_msm
255+
+ (num_btree as f64) * time_btree;
260256
println!("pt_non_linear = {}", pt_non_linear);
261257

262258
let pt_linear = estimate_add_mul_field_op_time();
@@ -270,7 +266,7 @@ pub fn estimate<
270266
random_poly
271267
});
272268
println!("pt_random = {}", pt_random);
273-
println!("");
269+
println!();
274270

275271
let prover_time = pt_non_linear + pt_linear + pt_random;
276272

@@ -286,18 +282,18 @@ pub fn estimate<
286282
// println!("mem_usage by linear regression = {}", mem_usage2);
287283

288284
// NOTE(sphere): calculate aggregate_circuit_size
289-
285+
290286
EstimateResult {
291287
prover_time,
292288
mem_usage: (mem_usage as f64) / 1024.0, // to KB
293289
}
294290
}
295291

296292
/// simulate_circuit is to run a circuit proving process.
297-
pub fn simulate_circuit<
298-
E: Engine,
299-
ConcreteCircuit: Circuit<E::Scalar>,
300-
>(circuit: ConcreteCircuit, k: usize) {
293+
pub fn simulate_circuit<E: Engine, ConcreteCircuit: Circuit<E::Scalar>>(
294+
circuit: ConcreteCircuit,
295+
k: usize,
296+
) {
301297
// let public_inputs_size = 0;
302298

303299
// Initialize the polynomial commitment parameters
@@ -310,17 +306,17 @@ pub fn simulate_circuit<
310306
// Create a proof
311307
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
312308

313-
let (prover_time, _) = measure_elapsed_time(||
309+
let (prover_time, _) = measure_elapsed_time(|| {
314310
create_proof(&params, &pk, &[circuit], &[&[]], OsRng, &mut transcript)
315-
.expect("proof generation should not fail")
316-
);
311+
.expect("proof generation should not fail")
312+
});
317313

318314
// NOTE(liutainyi): output prover_time
319315
println!("k = {}, prover_time = {}", k, prover_time);
320316
}
321317

322318
struct FuncCount {
323-
num_fft: usize,
319+
num_fft: usize,
324320
num_extended_fft: usize,
325321
num_msm: usize,
326322
num_btree: usize,
@@ -334,10 +330,10 @@ fn dummy_proof<C: CurveAffine>(
334330
domain: &EvaluationDomain<C::Scalar>,
335331
l: usize, // The number of input.
336332
) -> FuncCount {
337-
let mut num_fft = 0 as usize;
338-
let mut num_extended_fft = 0 as usize;
339-
let mut num_msm = 0 as usize;
340-
let mut num_btree = 0 as usize;
333+
let mut num_fft = 0_usize;
334+
let mut num_extended_fft = 0_usize;
335+
let mut num_msm = 0_usize;
336+
let mut num_btree = 0_usize;
341337

342338
let cs = pk.get_vk().get_cs();
343339

@@ -364,13 +360,14 @@ fn dummy_proof<C: CurveAffine>(
364360
// Commit to permutations.
365361
// NOTE(sphere): l * perm_commit_t
366362
// commit_lagrange: z
367-
let num_perm_slices = (cs.permutation.get_columns().len() + (cs.degree() - 3)) / (cs.degree() - 2);
363+
let num_perm_slices =
364+
(cs.permutation.get_columns().len() + (cs.degree() - 3)) / (cs.degree() - 2);
368365
num_msm += num_perm_slices;
369366
// lagrange_to_coeff: z
370367
num_fft += num_perm_slices;
371368
// coeff_to_extended: z
372369
num_extended_fft += num_perm_slices;
373-
370+
374371
// NOTE(sphere): pt += lookup_commit_product
375372
// commit_lagrange: z, a', s'
376373
num_msm += 3 * num_lookups;
@@ -389,21 +386,21 @@ fn dummy_proof<C: CurveAffine>(
389386
// Construct the vanishing argument's h(X) commitments
390387
// NOTE(sphere): pt += vanishing_construct
391388
// extended_to_coeff: h_poly
392-
num_extended_fft += 1;
389+
num_extended_fft += 1;
393390
// commit: h_poly_i
394391
let num_h_pieces = ((domain.extended_len() as u64 + params.n - 1) / params.n) as usize;
395392
num_msm += num_h_pieces;
396393

397394
// NOTE(sphere): evaluate h.
398-
let num_mul = pk.get_ev().fake_evaluate_h(&pk, l);
395+
let num_mul = pk.get_ev().fake_evaluate_h(pk, l);
399396

400-
// NOTE(sphere): multiopen(shplonk).
397+
// TODO(sphere): multiopen(shplonk). There should be a more detailed evaluation.
401398
// commit: h_x, h_x
402399
// The evaluations in multiopen is too small.
403400
num_msm += 2;
404401

405-
// TODO(sphere): Memory
406-
let mut mem_usage = 0 as usize;
402+
// NOTE(sphere): Memory
403+
let mut mem_usage = 0_usize;
407404
// instance / advice / fixed as value poly, and coset:
408405
let n = 1 << params.k as usize;
409406
let ext_n = domain.extended_len();
@@ -437,7 +434,7 @@ fn dummy_proof<C: CurveAffine>(
437434
mem_usage *= mem::size_of::<C::Scalar>();
438435

439436
FuncCount {
440-
num_fft,
437+
num_fft,
441438
num_extended_fft,
442439
num_msm,
443440
num_btree,
@@ -446,20 +443,20 @@ fn dummy_proof<C: CurveAffine>(
446443
}
447444
}
448445

449-
450446
/// cost_model_main is to generate a main function to run the cost model for a circuit.
451447
#[macro_export]
452448
macro_rules! cost_model_main {
453449
($cir:expr) => {
454-
use halo2_proofs::dev::{
455-
simulate_circuit,
456-
estimate,
457-
};
450+
use halo2_proofs::dev::{estimate, simulate_circuit};
458451

459452
fn main() {
460453
// NOTE(sphere): get k from args
461454
let mode = std::env::args().nth(1).expect("no running-mode given");
462-
let k = std::env::args().nth(2).expect("no circuit size given").parse().unwrap();
455+
let k = std::env::args()
456+
.nth(2)
457+
.expect("no circuit size given")
458+
.parse()
459+
.unwrap();
463460
// NOTE(sphere): estimate linear cost (cfg == simulate)
464461
let circuit = $cir;
465462
if mode.eq(&String::from("simulate")) {
@@ -471,5 +468,5 @@ macro_rules! cost_model_main {
471468
panic!("unrecognized format");
472469
}
473470
}
474-
}
471+
};
475472
}

halo2_proofs/src/plonk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ mod verifier;
3030
pub use assigned::*;
3131
pub use circuit::*;
3232
pub use error::*;
33+
pub use evaluation::*;
3334
pub use keygen::*;
3435
pub use prover::*;
3536
pub use verifier::*;
36-
pub use evaluation::*;
3737

3838
use std::io;
3939

@@ -148,7 +148,7 @@ impl<C: CurveAffine> ProvingKey<C> {
148148
pub fn get_vk(&self) -> &VerifyingKey<C> {
149149
&self.vk
150150
}
151-
151+
152152
/// Get the underlying [`Evaluator`].
153153
pub fn get_ev(&self) -> &Evaluator<C> {
154154
&self.ev

0 commit comments

Comments
 (0)