pil2-stark-setup 1.1.0-alpha

Setup and proving/verifying-key generation for the pil2-stark prover
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
use anyhow::Result;

use crate::io::bin_file_writer::BinFileWriter;
use crate::io::parser_args::{get_parser_args, ExpsInfo};
use crate::types::stark_info::{ExpressionsInfo, Hint, OpType, StarkInfo, VerifierInfo};

const CHELPERS_NSECTIONS: u32 = 3;
const CHELPERS_EXPRESSIONS_SECTION: u32 = 1;
const CHELPERS_CONSTRAINTS_DEBUG_SECTION: u32 = 2;
const CHELPERS_HINTS_SECTION: u32 = 3;

/// Prepared expression binary data ready for writing.
struct PreparedExpressionsBin {
    exps_info: Vec<ExpsInfo>,
    constraints_info: Vec<ExpsInfo>,
    hints_info: Vec<Hint>,
    numbers_exps: Vec<String>,
    numbers_constraints: Vec<String>,
    max_tmp1: u64,
    max_tmp3: u64,
    max_args: u64,
    max_ops: u64,
}

/// Prepared verifier binary data.
struct PreparedVerifierBin {
    q_code: ExpsInfo,
    query_code: ExpsInfo,
    numbers_exps: Vec<String>,
    max_tmp1: u64,
    max_tmp3: u64,
    max_args: u64,
    max_ops: u64,
}

fn prepare_expressions_bin(
    stark_info: &StarkInfo,
    expressions_info: &ExpressionsInfo,
) -> Result<PreparedExpressionsBin> {
    let mut exps_info: Vec<ExpsInfo> = Vec::new();
    let mut constraints_info: Vec<ExpsInfo> = Vec::new();
    let mut numbers_exps: Vec<String> = Vec::new();
    let mut numbers_constraints: Vec<String> = Vec::new();

    let n = 1u64 << stark_info.stark_struct.n_bits;

    let mut max_tmp1: u64 = 0;
    let mut max_tmp3: u64 = 0;
    let mut max_args: u64 = 0;
    let mut max_ops: u64 = 0;

    // Process constraints
    for constraint_code in &expressions_info.constraints {
        let (first_row, last_row) = match constraint_code.boundary.as_str() {
            "everyRow" => (0, n),
            "firstRow" | "finalProof" => (0, 1),
            "lastRow" => (n - 1, n),
            "everyFrame" => (constraint_code.offset_min, n - constraint_code.offset_max),
            other => anyhow::bail!("Invalid boundary: {}", other),
        };

        let result =
            get_parser_args(stark_info, &constraint_code.code, &mut numbers_constraints, false, false, None, "")?;

        let mut info = result.exps_info;
        info.stage = constraint_code.stage;
        info.first_row = first_row;
        info.last_row = last_row;
        info.line = constraint_code.line.clone();
        info.im_pol = constraint_code.im_pol;

        if info.n_temp1 > max_tmp1 {
            max_tmp1 = info.n_temp1;
        }
        if info.n_temp3 > max_tmp3 {
            max_tmp3 = info.n_temp3;
        }
        if info.args.len() as u64 > max_args {
            max_args = info.args.len() as u64;
        }
        if info.ops.len() as u64 > max_ops {
            max_ops = info.ops.len() as u64;
        }

        constraints_info.push(info);
    }

    // Process expressions
    for exp_code_orig in &expressions_info.expressions_code {
        let mut exp_code = exp_code_orig.clone();

        // Check if dest should be redirected to tmp
        let is_special = exp_code.exp_id == stark_info.c_exp_id
            || exp_code.exp_id == stark_info.fri_exp_id
            || stark_info.cm_pols_map.iter().any(|c| c.exp_id == exp_code.exp_id);

        if is_special {
            if let Some(last) = exp_code.code.last_mut() {
                last.dest.op_type = OpType::Tmp;
                last.dest.id = exp_code.tmp_used;
                exp_code.tmp_used += 1;
            }
        }

        let result = get_parser_args(stark_info, &exp_code.code, &mut numbers_exps, false, false, None, "")?;

        let mut info = result.exps_info;
        info.exp_id = exp_code.exp_id;
        info.stage = exp_code.stage;
        info.line = exp_code.line.clone();

        if info.n_temp1 > max_tmp1 {
            max_tmp1 = info.n_temp1;
        }
        if info.n_temp3 > max_tmp3 {
            max_tmp3 = info.n_temp3;
        }
        if info.args.len() as u64 > max_args {
            max_args = info.args.len() as u64;
        }
        if info.ops.len() as u64 > max_ops {
            max_ops = info.ops.len() as u64;
        }

        exps_info.push(info);
    }

    Ok(PreparedExpressionsBin {
        exps_info,
        constraints_info,
        hints_info: expressions_info.hints_info.clone(),
        numbers_exps,
        numbers_constraints,
        max_tmp1,
        max_tmp3,
        max_args,
        max_ops,
    })
}

fn prepare_verifier_expressions_bin(
    stark_info: &StarkInfo,
    verifier_info: &VerifierInfo,
) -> Result<PreparedVerifierBin> {
    let mut max_tmp1: u64 = 0;
    let mut max_tmp3: u64 = 0;
    let mut max_args: u64 = 0;
    let mut max_ops: u64 = 0;
    let mut numbers_exps: Vec<String> = Vec::new();

    let q_result =
        get_parser_args(stark_info, &verifier_info.q_verifier.code, &mut numbers_exps, false, true, None, "")?;
    let mut q_code = q_result.exps_info;
    q_code.exp_id = stark_info.c_exp_id;
    q_code.stage = stark_info.n_stages + 1;
    q_code.line = String::new();

    if q_code.n_temp1 > max_tmp1 {
        max_tmp1 = q_code.n_temp1;
    }
    if q_code.n_temp3 > max_tmp3 {
        max_tmp3 = q_code.n_temp3;
    }
    if q_code.args.len() as u64 > max_args {
        max_args = q_code.args.len() as u64;
    }
    if q_code.ops.len() as u64 > max_ops {
        max_ops = q_code.ops.len() as u64;
    }

    let query_result =
        get_parser_args(stark_info, &verifier_info.query_verifier.code, &mut numbers_exps, false, true, None, "")?;
    let mut query_code = query_result.exps_info;
    query_code.exp_id = stark_info.fri_exp_id;
    query_code.stage = stark_info.n_stages + 2;
    query_code.line = String::new();

    if query_code.n_temp1 > max_tmp1 {
        max_tmp1 = query_code.n_temp1;
    }
    if query_code.n_temp3 > max_tmp3 {
        max_tmp3 = query_code.n_temp3;
    }
    if query_code.args.len() as u64 > max_args {
        max_args = query_code.args.len() as u64;
    }
    if query_code.ops.len() as u64 > max_ops {
        max_ops = query_code.ops.len() as u64;
    }

    Ok(PreparedVerifierBin { q_code, query_code, numbers_exps, max_tmp1, max_tmp3, max_args, max_ops })
}

#[allow(clippy::too_many_arguments)]
fn write_expressions_section(
    writer: &mut BinFileWriter,
    expressions_info: &[ExpsInfo],
    numbers_exps: &[String],
    max_tmp1: u64,
    max_tmp3: u64,
    max_args: u64,
    max_ops: u64,
    section: u32,
) -> Result<()> {
    writer.start_write_section(section)?;

    let mut ops_all: Vec<u8> = Vec::new();
    let mut args_all: Vec<u16> = Vec::new();
    let mut ops_offsets: Vec<u32> = Vec::new();
    let mut args_offsets: Vec<u32> = Vec::new();

    for (i, exp) in expressions_info.iter().enumerate() {
        if i == 0 {
            ops_offsets.push(0);
            args_offsets.push(0);
        } else {
            ops_offsets.push(ops_offsets[i - 1] + expressions_info[i - 1].ops.len() as u32);
            args_offsets.push(args_offsets[i - 1] + expressions_info[i - 1].args.len() as u32);
        }
        for op in &exp.ops {
            ops_all.push(*op as u8);
        }
        for arg in &exp.args {
            args_all.push(*arg as u16);
        }
    }

    writer.write_u32(max_tmp1 as u32)?;
    writer.write_u32(max_tmp3 as u32)?;
    writer.write_u32(max_args as u32)?;
    writer.write_u32(max_ops as u32)?;
    writer.write_u32(ops_all.len() as u32)?;
    writer.write_u32(args_all.len() as u32)?;
    writer.write_u32(numbers_exps.len() as u32)?;

    let n_expressions = expressions_info.len() as u32;
    writer.write_u32(n_expressions)?;

    for (i, exp) in expressions_info.iter().enumerate() {
        writer.write_u32(exp.exp_id as u32)?;
        writer.write_u32(exp.dest_dim as u32)?;
        writer.write_u32(exp.dest_id as u32)?;
        writer.write_u32(exp.stage as u32)?;
        writer.write_u32(exp.n_temp1 as u32)?;
        writer.write_u32(exp.n_temp3 as u32)?;

        writer.write_u32(exp.ops.len() as u32)?;
        writer.write_u32(ops_offsets[i])?;

        writer.write_u32(exp.args.len() as u32)?;
        writer.write_u32(args_offsets[i])?;

        writer.write_string(&exp.line)?;
    }

    // Write ops as u8
    for op in &ops_all {
        writer.write_u8(*op)?;
    }

    // Write args as u16 LE
    for arg in &args_all {
        writer.write_u16(*arg)?;
    }

    // Write numbers as u64 LE
    for num_str in numbers_exps {
        let num: u64 = num_str.parse().unwrap_or(0);
        writer.write_u64(num)?;
    }

    writer.end_write_section()?;
    Ok(())
}

fn write_constraints_section(
    writer: &mut BinFileWriter,
    constraints_info: &[ExpsInfo],
    numbers_constraints: &[String],
    section: u32,
) -> Result<()> {
    writer.start_write_section(section)?;

    let mut ops_all: Vec<u8> = Vec::new();
    let mut args_all: Vec<u16> = Vec::new();
    let mut ops_offsets: Vec<u32> = Vec::new();
    let mut args_offsets: Vec<u32> = Vec::new();

    for (i, constraint) in constraints_info.iter().enumerate() {
        if i == 0 {
            ops_offsets.push(0);
            args_offsets.push(0);
        } else {
            ops_offsets.push(ops_offsets[i - 1] + constraints_info[i - 1].ops.len() as u32);
            args_offsets.push(args_offsets[i - 1] + constraints_info[i - 1].args.len() as u32);
        }
        for op in &constraint.ops {
            ops_all.push(*op as u8);
        }
        for arg in &constraint.args {
            args_all.push(*arg as u16);
        }
    }

    writer.write_u32(ops_all.len() as u32)?;
    writer.write_u32(args_all.len() as u32)?;
    writer.write_u32(numbers_constraints.len() as u32)?;

    let n_constraints = constraints_info.len() as u32;
    writer.write_u32(n_constraints)?;

    for (i, constraint) in constraints_info.iter().enumerate() {
        writer.write_u32(constraint.stage as u32)?;
        writer.write_u32(constraint.dest_dim as u32)?;
        writer.write_u32(constraint.dest_id as u32)?;
        writer.write_u32(constraint.first_row as u32)?;
        writer.write_u32(constraint.last_row as u32)?;
        writer.write_u32(constraint.n_temp1 as u32)?;
        writer.write_u32(constraint.n_temp3 as u32)?;

        writer.write_u32(constraint.ops.len() as u32)?;
        writer.write_u32(ops_offsets[i])?;

        writer.write_u32(constraint.args.len() as u32)?;
        writer.write_u32(args_offsets[i])?;

        writer.write_u32(constraint.im_pol as u32)?;
        writer.write_string(&constraint.line)?;
    }

    for op in &ops_all {
        writer.write_u8(*op)?;
    }
    for arg in &args_all {
        writer.write_u16(*arg)?;
    }
    for num_str in numbers_constraints {
        let num: u64 = num_str.parse().unwrap_or(0);
        writer.write_u64(num)?;
    }

    writer.end_write_section()?;
    Ok(())
}

fn write_hints_section(writer: &mut BinFileWriter, hints_info: &[Hint], section: u32) -> Result<()> {
    writer.start_write_section(section)?;

    let n_hints = hints_info.len() as u32;
    writer.write_u32(n_hints)?;

    for hint in hints_info {
        writer.write_string(&hint.name)?;
        let n_fields = hint.fields.len() as u32;
        writer.write_u32(n_fields)?;

        for field in &hint.fields {
            writer.write_string(&field.name)?;
            let n_values = field.values.len() as u32;
            writer.write_u32(n_values)?;

            for value in &field.values {
                writer.write_string(&value.op)?;

                if value.op == "number" {
                    writer.write_u64(value.value)?;
                } else if value.op == "string" {
                    writer.write_string(&value.string_value)?;
                } else {
                    writer.write_u32(value.id as u32)?;
                }

                if value.op == "custom" || value.op == "const" || value.op == "cm" {
                    writer.write_u32(value.row_offset_index as u32)?;
                }
                if value.op == "tmp" {
                    writer.write_u32(value.dim as u32)?;
                }
                if value.op == "custom" {
                    writer.write_u32(value.commit_id as u32)?;
                }

                writer.write_u32(value.pos.len() as u32)?;
                for p in &value.pos {
                    writer.write_u32(*p as u32)?;
                }
            }
        }
    }

    writer.end_write_section()?;
    Ok(())
}

/// Writes the prover expressions binary file (3 sections: expressions, constraints, hints).
pub fn write_expressions_bin_file(
    path: &str,
    stark_info: &StarkInfo,
    expressions_info: &ExpressionsInfo,
) -> Result<()> {
    println!("> Writing the chelpers file");

    let bin = prepare_expressions_bin(stark_info, expressions_info)?;

    let mut writer = BinFileWriter::new(path, "chps", 1, CHELPERS_NSECTIONS)?;

    write_expressions_section(
        &mut writer,
        &bin.exps_info,
        &bin.numbers_exps,
        bin.max_tmp1,
        bin.max_tmp3,
        bin.max_args,
        bin.max_ops,
        CHELPERS_EXPRESSIONS_SECTION,
    )?;

    write_constraints_section(
        &mut writer,
        &bin.constraints_info,
        &bin.numbers_constraints,
        CHELPERS_CONSTRAINTS_DEBUG_SECTION,
    )?;

    write_hints_section(&mut writer, &bin.hints_info, CHELPERS_HINTS_SECTION)?;

    println!("> Writing the chelpers file finished");
    println!("---------------------------------------------");

    writer.close()?;
    Ok(())
}

/// Writes the verifier expressions binary file (1 section: expressions for q and query).
pub fn write_verifier_expressions_bin_file(
    path: &str,
    stark_info: &StarkInfo,
    verifier_info: &VerifierInfo,
) -> Result<()> {
    println!("> Writing chelpers verifier file");

    let bin = prepare_verifier_expressions_bin(stark_info, verifier_info)?;

    let ver_exps = vec![bin.q_code, bin.query_code];

    let mut writer = BinFileWriter::new(path, "chps", 1, 1)?;

    write_expressions_section(
        &mut writer,
        &ver_exps,
        &bin.numbers_exps,
        bin.max_tmp1,
        bin.max_tmp3,
        bin.max_args,
        bin.max_ops,
        CHELPERS_EXPRESSIONS_SECTION,
    )?;

    println!("> Writing the chelpers file finished");
    println!("---------------------------------------------");

    writer.close()?;
    Ok(())
}