|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + adapter::Adapter, |
| 7 | + blocks::BasicBlock, |
| 8 | + expression::{AlgebraicExpression, AlgebraicReference}, |
| 9 | + SymbolicConstraint, |
| 10 | +}; |
| 11 | + |
| 12 | +/// "Constraints" that were inferred from execution statistics. |
| 13 | +#[derive(Serialize, Deserialize, Clone, Default)] |
| 14 | +pub struct EmpiricalConstraints { |
| 15 | + /// For each program counter, the range constraints for each column. |
| 16 | + /// The range might not hold in 100% of cases. |
| 17 | + pub column_ranges_by_pc: BTreeMap<u32, Vec<(u32, u32)>>, |
| 18 | + /// For each basic block (identified by its starting PC), the equivalence classes of columns. |
| 19 | + /// Each equivalence class is a list of (instruction index in block, column index). |
| 20 | + pub equivalence_classes_by_block: BTreeMap<u64, Vec<Vec<(usize, usize)>>>, |
| 21 | +} |
| 22 | + |
| 23 | +/// Debug information mapping AIR ids to program counters and column names. |
| 24 | +#[derive(Serialize, Deserialize)] |
| 25 | +pub struct DebugInfo { |
| 26 | + /// Mapping from program counter to AIR id. |
| 27 | + pub air_id_by_pc: BTreeMap<u32, usize>, |
| 28 | + /// Mapping from AIR id to column names. |
| 29 | + pub column_names_by_air_id: BTreeMap<usize, Vec<String>>, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Serialize, Deserialize)] |
| 33 | +pub struct EmpiricalConstraintsJson { |
| 34 | + pub empirical_constraints: EmpiricalConstraints, |
| 35 | + pub debug_info: DebugInfo, |
| 36 | +} |
| 37 | + |
| 38 | +pub fn add_empirical_constraints<A: Adapter>( |
| 39 | + empirical_constraints: &EmpiricalConstraints, |
| 40 | + subs: &[Vec<u64>], |
| 41 | + block: &BasicBlock<A::Instruction>, |
| 42 | + columns: impl Iterator<Item = AlgebraicReference>, |
| 43 | +) -> ( |
| 44 | + Vec<SymbolicConstraint<A::PowdrField>>, |
| 45 | + Vec<SymbolicConstraint<A::PowdrField>>, |
| 46 | +) { |
| 47 | + let range_constraints = &empirical_constraints.column_ranges_by_pc; |
| 48 | + let equivalence_classes_by_block = &empirical_constraints.equivalence_classes_by_block; |
| 49 | + |
| 50 | + let mut range_analyzer_constraints = Vec::new(); |
| 51 | + let mut equivalence_analyzer_constraints = Vec::new(); |
| 52 | + |
| 53 | + // Mapping (instruction index, column index) -> AlgebraicReference |
| 54 | + let reverse_subs = subs |
| 55 | + .iter() |
| 56 | + .enumerate() |
| 57 | + .flat_map(|(instr_index, subs)| { |
| 58 | + subs.iter() |
| 59 | + .enumerate() |
| 60 | + .map(move |(col_index, &poly_id)| (poly_id, (instr_index, col_index))) |
| 61 | + }) |
| 62 | + .collect::<BTreeMap<_, _>>(); |
| 63 | + let algebraic_references = columns |
| 64 | + .map(|r| (reverse_subs.get(&r.id).unwrap().clone(), r.clone())) |
| 65 | + .collect::<BTreeMap<_, _>>(); |
| 66 | + |
| 67 | + for i in 0..block.statements.len() { |
| 68 | + let pc = (block.start_pc + (i * 4) as u64) as u32; |
| 69 | + let Some(range_constraints) = range_constraints.get(&pc) else { |
| 70 | + continue; |
| 71 | + }; |
| 72 | + for (col_index, range) in range_constraints.iter().enumerate() { |
| 73 | + if range.0 == range.1 { |
| 74 | + let value = A::PowdrField::from(range.0 as u64); |
| 75 | + let Some(reference) = algebraic_references.get(&(i, col_index)).cloned() else { |
| 76 | + panic!( |
| 77 | + "Missing reference for (i: {}, col_index: {}, block_id: {})", |
| 78 | + i, col_index, block.start_pc |
| 79 | + ); |
| 80 | + }; |
| 81 | + let constraint = |
| 82 | + AlgebraicExpression::Reference(reference) - AlgebraicExpression::Number(value); |
| 83 | + |
| 84 | + range_analyzer_constraints.push(SymbolicConstraint { expr: constraint }); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if let Some(equivalence_classes) = equivalence_classes_by_block.get(&block.start_pc) { |
| 90 | + for equivalence_class in equivalence_classes { |
| 91 | + let first = equivalence_class.first().unwrap(); |
| 92 | + let Some(first_ref) = algebraic_references.get(first).cloned() else { |
| 93 | + // TODO: This fails in some blocks. For now, just return no extra constraints. |
| 94 | + println!( |
| 95 | + "Missing reference for (i: {}, col_index: {}, block_id: {})", |
| 96 | + first.0, first.1, block.start_pc |
| 97 | + ); |
| 98 | + return (range_analyzer_constraints, vec![]); |
| 99 | + }; |
| 100 | + for other in equivalence_class.iter().skip(1) { |
| 101 | + let Some(other_ref) = algebraic_references.get(other).cloned() else { |
| 102 | + // TODO: This fails in some blocks. For now, just return no extra constraints. |
| 103 | + println!( |
| 104 | + "Missing reference for (i: {}, col_index: {}, block_id: {})", |
| 105 | + other.0, other.1, block.start_pc |
| 106 | + ); |
| 107 | + return (range_analyzer_constraints, vec![]); |
| 108 | + }; |
| 109 | + let constraint = AlgebraicExpression::Reference(first_ref.clone()) |
| 110 | + - AlgebraicExpression::Reference(other_ref.clone()); |
| 111 | + equivalence_analyzer_constraints.push(SymbolicConstraint { expr: constraint }); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + (range_analyzer_constraints, equivalence_analyzer_constraints) |
| 117 | +} |
0 commit comments