pil2-std-lib 1.1.0-alpha

PIL2 standard library: range checks, lookups, and sum/product arguments for proofman
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use std::sync::RwLock;

use rayon::prelude::*;

use rustc_hash::FxHashMap;
use proofman_fields::PrimeField64;

use proofman_common::{DebugInfo, ProofCtx, ProofmanError, ProofmanResult, SetupCtx, store_rows_info_air};
use proofman_hints::{
    get_hint_field, get_hint_field_a, get_hint_field_gc, get_hint_field_gc_a, get_hint_ids_by_name, HintFieldOptions,
    HintFieldValue, HintFieldValuesVec,
};

use crate::{
    check_invalid_opids, get_global_hint_field, get_global_hint_field_constant_as, get_hint_field_constant_as,
    get_hint_field_constant_as_string, get_hint_field_constant_a_as_string, get_hint_field_constant_as_field,
    get_row_field_value, max_positive_multiplicity, print_debug_info, update_debug_data, update_debug_data_fast,
    DebugData, DebugDataFast, DebugDataFastGlobal, DebugDataInfo, HintMetadata, hash_vals, normalize_vals,
    PIOP_TYPE_ASSUMES, PIOP_TYPE_FREE, PIOP_TYPE_PROVES,
};

#[allow(clippy::too_many_arguments)]
pub fn extract_global_hint_fields<F: PrimeField64>(
    pctx: &ProofCtx<F>,
    sctx: &SetupCtx<F>,
    debug_data: &mut DebugData,
    debug_data_info: &mut DebugDataInfo,
    debug_data_fast: &mut DebugDataFast,
    debug_data_fast_global: &mut DebugDataFastGlobal,
    fast_mode: bool,
    is_prod: bool,
    debug_hashes: &[u64],
) -> ProofmanResult<()> {
    let debug_data_name = if is_prod { "gprod_debug_data_global" } else { "gsum_debug_data_global" };
    let debug_data_hints = get_hint_ids_by_name(sctx.get_global_bin(), debug_data_name);
    if !debug_data_hints.is_empty() {
        let num_global_hints =
            get_global_hint_field_constant_as::<usize, F>(sctx, debug_data_hints[0], "num_global_hints")?;
        for i in 0..num_global_hints {
            let airgroup_id =
                get_global_hint_field_constant_as::<usize, F>(sctx, debug_data_hints[1 + i], "airgroup_id")?;
            let type_piop = get_global_hint_field_constant_as::<u64, F>(sctx, debug_data_hints[1 + i], "type_piop")?;
            if ![PIOP_TYPE_ASSUMES, PIOP_TYPE_PROVES, PIOP_TYPE_FREE].contains(&type_piop) {
                return Err(ProofmanError::StdError(format!("Invalid type_piop: {type_piop}")));
            }

            let opid = get_global_hint_field(sctx, debug_data_hints[1 + i], "busid")?;
            let opid = opid.as_canonical_u64();

            // If opids are specified, then only update the bus if the opid is in the list
            if !pctx.debug_info.read().unwrap().std_mode.opids.is_empty()
                && !pctx.debug_info.read().unwrap().std_mode.opids.contains(&opid)
            {
                continue;
            }

            let num_reps = get_hint_field_gc(pctx, sctx, debug_data_hints[1 + i], "num_reps", false)?;

            // If the number of repetitions is zero, continue
            let mut num_reps = get_row_field_value(&num_reps, 0, "num_reps")?;
            if num_reps.is_zero() {
                continue;
            }

            // Determine whether this is a prove or an assume
            let is_proves = match type_piop {
                PIOP_TYPE_ASSUMES => false,
                PIOP_TYPE_PROVES => true,
                PIOP_TYPE_FREE => {
                    if num_reps.as_canonical_u64() > max_positive_multiplicity::<F>() {
                        num_reps = -num_reps;
                        false
                    } else {
                        true
                    }
                }
                _ => unreachable!(),
            };

            let expressions = get_hint_field_gc_a(pctx, sctx, debug_data_hints[1 + i], "expressions", false)?;
            let expr = expressions.get(0);
            let norm_vals = normalize_vals(&expr);
            let hash = hash_vals(norm_vals);
            if fast_mode {
                update_debug_data_fast(
                    debug_data_fast,
                    debug_data_fast_global,
                    opid,
                    hash,
                    is_proves,
                    num_reps.as_canonical_u64(),
                    true,
                )?;
            } else {
                update_debug_data(
                    debug_data,
                    debug_data_info,
                    i,
                    opid,
                    norm_vals,
                    hash,
                    airgroup_id,
                    None,
                    None,
                    0,
                    is_proves,
                    num_reps.as_canonical_u64(),
                    true,
                    is_prod,
                    true,
                    debug_hashes,
                )?;
            }
        }
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub fn extract_hint_fields<F: PrimeField64>(
    pctx: &ProofCtx<F>,
    sctx: &SetupCtx<F>,
    instance_id: usize,
    debug_data: &mut DebugData,
    debug_data_info: &mut DebugDataInfo,
    debug_data_fast: &RwLock<DebugDataFast>,
    debug_data_fast_global: &RwLock<DebugDataFastGlobal>,
    fast_mode: bool,
    is_prod: bool,
    debug_hashes: &[u64],
) -> ProofmanResult<()> {
    let (airgroup_id, air_id) = pctx.dctx_get_instance_info(instance_id)?;
    let air_instance_id = pctx.dctx_find_air_instance_id(instance_id)?;

    let setup = sctx.get_setup(airgroup_id, air_id)?;
    let p_expressions_bin = setup.p_setup.p_expressions_bin;

    let debug_data_name = if is_prod { "gprod_debug_data" } else { "gsum_debug_data" };

    let debug_data_hints = get_hint_ids_by_name(p_expressions_bin, debug_data_name);

    let num_rows = pctx.global_info.airs[airgroup_id][air_id].num_rows;

    let hint_metadatas: Result<Vec<_>, ProofmanError> = debug_data_hints
        .iter()
        .enumerate()
        .map(|(i, &hint)| {
            let busid = get_hint_field(pctx, setup, instance_id, hint as usize, "busid", HintFieldOptions::default())?;

            let type_piop = get_hint_field_constant_as::<u64, F>(
                pctx,
                setup,
                airgroup_id,
                air_id,
                hint as usize,
                "type_piop",
                HintFieldOptions::default(),
            )?;
            if ![PIOP_TYPE_ASSUMES, PIOP_TYPE_PROVES, PIOP_TYPE_FREE].contains(&type_piop) {
                return Err(ProofmanError::StdError(format!("Invalid type_piop: {type_piop}")));
            }

            let num_reps =
                get_hint_field(pctx, setup, instance_id, hint as usize, "num_reps", HintFieldOptions::default())?;

            let deg_expr = get_hint_field_constant_as_field(
                pctx,
                setup,
                airgroup_id,
                air_id,
                hint as usize,
                "deg_expr",
                HintFieldOptions::default(),
            )?;

            let deg_mul = get_hint_field_constant_as_field(
                pctx,
                setup,
                airgroup_id,
                air_id,
                hint as usize,
                "deg_sel",
                HintFieldOptions::default(),
            )?;

            let name_piop = get_hint_field_constant_as_string(
                pctx,
                setup,
                airgroup_id,
                air_id,
                hint as usize,
                "name_piop",
                HintFieldOptions::default(),
            )?;

            let name_exprs = get_hint_field_constant_a_as_string(
                pctx,
                setup,
                airgroup_id,
                air_id,
                hint as usize,
                "name_exprs",
                HintFieldOptions::default(),
            )?;

            let expressions =
                get_hint_field_a(pctx, setup, instance_id, hint as usize, "expressions", HintFieldOptions::default())?;

            Ok(HintMetadata {
                hint,
                hint_id: i,
                busid,
                type_piop,
                num_reps,
                expressions,
                deg_expr,
                deg_mul,
                name_piop,
                name_exprs,
            })
        })
        .collect();

    let hint_metadatas = hint_metadatas?;

    let opids = &pctx.debug_info.read().unwrap().std_mode.opids;

    if fast_mode {
        // Process hints in chunks of to reuse pre-allocated HashMaps
        hint_metadatas.par_iter().try_for_each(|hint_metadata| -> ProofmanResult<()> {
            // Directly acquire write lock and work with it
            let mut local_debug_data = DebugDataFast::new();
            let mut local_debug_data_global = DebugDataFastGlobal::new();

            // If both the expression and the mul are of degree zero, then simply update the bus once
            if hint_metadata.deg_expr.is_zero() && hint_metadata.deg_mul.is_zero() {
                update_bus_fast(
                    opids,
                    &hint_metadata.busid,
                    hint_metadata.type_piop,
                    &hint_metadata.num_reps,
                    &hint_metadata.expressions,
                    0,
                    &mut local_debug_data,
                    &mut local_debug_data_global,
                    false,
                )?;
            }
            // Otherwise, update the bus for each row
            else {
                for j in 0..num_rows {
                    update_bus_fast(
                        opids,
                        &hint_metadata.busid,
                        hint_metadata.type_piop,
                        &hint_metadata.num_reps,
                        &hint_metadata.expressions,
                        j,
                        &mut local_debug_data,
                        &mut local_debug_data_global,
                        false,
                    )?;
                }
            }

            let mut shared = debug_data_fast.write().unwrap();
            local_debug_data.merge_into(&mut shared);
            let mut shared_global = debug_data_fast_global.write().unwrap();
            for (opid, hashes) in local_debug_data_global.into_iter() {
                shared_global.entry(opid).or_default().extend(hashes);
            }
            Ok(())
        })?;
    } else {
        let store_row_info = store_rows_info_air(pctx, airgroup_id, air_id, instance_id);
        for hint_metadata in hint_metadatas.iter() {
            // If both the expresion and the mul are of degree zero, then simply update the bus once
            if hint_metadata.deg_expr.is_zero() && hint_metadata.deg_mul.is_zero() {
                update_bus(
                    opids,
                    hint_metadata.hint_id,
                    airgroup_id,
                    air_id,
                    air_instance_id,
                    &hint_metadata.busid,
                    hint_metadata.type_piop,
                    &hint_metadata.num_reps,
                    &hint_metadata.expressions,
                    0,
                    debug_data,
                    debug_data_info,
                    false,
                    is_prod,
                    store_row_info,
                    debug_hashes,
                )?;
            }
            // Otherwise, update the bus for each row
            else {
                for j in 0..num_rows {
                    update_bus(
                        opids,
                        hint_metadata.hint_id,
                        airgroup_id,
                        air_id,
                        air_instance_id,
                        &hint_metadata.busid,
                        hint_metadata.type_piop,
                        &hint_metadata.num_reps,
                        &hint_metadata.expressions,
                        j,
                        debug_data,
                        debug_data_info,
                        false,
                        is_prod,
                        store_row_info,
                        debug_hashes,
                    )?;
                }
            }
        }
    }

    std::thread::spawn(move || {
        drop(hint_metadatas);
    });
    Ok(())
}

#[allow(clippy::too_many_arguments)]
#[inline]
pub fn update_bus<F: PrimeField64>(
    op_ids: &[u64],
    hint_id: usize,
    airgroup_id: usize,
    air_id: usize,
    instance_id: usize,
    busid: &HintFieldValue<F>,
    type_piop: u64,
    num_reps: &HintFieldValue<F>,
    expressions: &HintFieldValuesVec<F>,
    row: usize,
    debug_data: &mut DebugData,
    debug_data_info: &mut DebugDataInfo,
    is_global: bool,
    is_prod: bool,
    store_row_info: bool,
    debug_hashes: &[u64],
) -> ProofmanResult<()> {
    let opid = get_row_field_value(busid, row, "busid")?;
    if !op_ids.is_empty() && !op_ids.contains(&opid.as_canonical_u64()) {
        return Ok(());
    }

    let mut num_reps = get_row_field_value(num_reps, row, "num_reps")?;
    if num_reps.is_zero() {
        return Ok(());
    }

    // Determine whether this is a prove or an assume
    let is_proves = match type_piop {
        PIOP_TYPE_ASSUMES => false,
        PIOP_TYPE_PROVES => true,
        PIOP_TYPE_FREE => {
            if num_reps.as_canonical_u64() > max_positive_multiplicity::<F>() {
                num_reps = -num_reps;
                false
            } else {
                true
            }
        }
        _ => unreachable!(),
    };

    let expr = expressions.get(row);
    let norm_vals = normalize_vals(&expr);
    let hash = hash_vals(norm_vals);

    update_debug_data(
        debug_data,
        debug_data_info,
        hint_id,
        opid.as_canonical_u64(),
        norm_vals,
        hash,
        airgroup_id,
        Some(air_id),
        Some(instance_id),
        row,
        is_proves,
        num_reps.as_canonical_u64(),
        is_global,
        is_prod,
        store_row_info,
        debug_hashes,
    )
}

#[allow(clippy::too_many_arguments)]
fn update_bus_fast<F: PrimeField64>(
    op_ids: &[u64],
    busid: &HintFieldValue<F>,
    type_piop: u64,
    num_reps: &HintFieldValue<F>,
    expressions: &HintFieldValuesVec<F>,
    row: usize,
    debug_data_fast: &mut DebugDataFast,
    debug_data_fast_global: &mut DebugDataFastGlobal,
    is_global: bool,
) -> ProofmanResult<()> {
    let opid = get_row_field_value(busid, row, "busid")?;
    if !op_ids.is_empty() && !op_ids.contains(&opid.as_canonical_u64()) {
        return Ok(());
    }

    let mut num_reps = get_row_field_value(num_reps, row, "num_reps")?;
    if num_reps.is_zero() {
        return Ok(());
    }

    // Determine whether this is a prove or an assume
    let is_proves = match type_piop {
        PIOP_TYPE_ASSUMES => false,
        PIOP_TYPE_PROVES => true,
        PIOP_TYPE_FREE => {
            if num_reps.as_canonical_u64() > max_positive_multiplicity::<F>() {
                num_reps = -num_reps;
                false
            } else {
                true
            }
        }
        _ => unreachable!(),
    };

    let expr = expressions.get(row);
    let norm_vals = normalize_vals(&expr);
    let hash = hash_vals(norm_vals);

    update_debug_data_fast(
        debug_data_fast,
        debug_data_fast_global,
        opid.as_canonical_u64(),
        hash,
        is_proves,
        num_reps.as_canonical_u64(),
        is_global,
    )
}

#[allow(clippy::too_many_arguments)]
pub fn print_std_debug_info<F: PrimeField64>(
    pctx: &ProofCtx<F>,
    sctx: &SetupCtx<F>,
    debug_data: &RwLock<DebugData>,
    debug_data_info: &RwLock<DebugDataInfo>,
    debug_data_fast: &RwLock<DebugDataFast>,
    debug_data_fast_global: &RwLock<DebugDataFastGlobal>,
    debug_info: &DebugInfo,
    is_prod: bool,
    debug_hashes: &[u64],
) -> ProofmanResult<()> {
    let fast_mode = debug_info.std_mode.fast_mode;

    if fast_mode {
        let mut debug_data_fast = debug_data_fast.write().unwrap();
        let mut debug_data_fast_global = debug_data_fast_global.write().unwrap();

        // Perform the global hint update
        extract_global_hint_fields(
            pctx,
            sctx,
            &mut FxHashMap::default(),
            &mut FxHashMap::default(),
            &mut debug_data_fast,
            &mut debug_data_fast_global,
            fast_mode,
            is_prod,
            debug_hashes,
        )?;

        check_invalid_opids(pctx, std::mem::take(&mut *debug_data_fast));
    } else {
        let mut debug_data_t = debug_data.write().unwrap();
        let mut debug_data_info_t = debug_data_info.write().unwrap();
        extract_global_hint_fields(
            pctx,
            sctx,
            &mut debug_data_t,
            &mut debug_data_info_t,
            &mut DebugDataFast::new(),
            &mut DebugDataFastGlobal::new(),
            fast_mode,
            is_prod,
            debug_hashes,
        )?;

        let max_values_to_print = debug_info.std_mode.n_vals;
        let print_to_file = debug_info.std_mode.print_to_file;
        print_debug_info(pctx, sctx, max_values_to_print, print_to_file, &mut debug_data_t, &mut debug_data_info_t)?;
    }

    Ok(())
}