binius_frontend/compiler/
circuit.rs1use std::{error, fmt};
3
4use binius_core::{
5 constraint_system::{ConstraintSystem, ValueIndex, ValueVec},
6 word::Word,
7};
8use cranelift_entity::SecondaryMap;
9
10use crate::compiler::{
11 eval_form::EvalForm,
12 gate_graph::{GateGraph, Wire},
13};
14
15#[derive(Debug)]
17pub struct PopulateError {
18 pub messages: Vec<String>,
20 pub total_count: usize,
22}
23
24impl fmt::Display for PopulateError {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 writeln!(f, "assertions failed:")?;
27 for message in &self.messages {
28 writeln!(f, "{message}")?;
29 }
30 if self.total_count > self.messages.len() {
31 writeln!(f, "(Some assertions are omitted. Total: {})", self.total_count)?;
32 }
33 Ok(())
34 }
35}
36
37impl error::Error for PopulateError {}
38
39pub struct WitnessFiller<'a> {
41 pub(crate) circuit: &'a Circuit,
42 pub(crate) value_vec: ValueVec,
43}
44
45impl<'a> WitnessFiller<'a> {
46 pub fn into_value_vec(self) -> ValueVec {
48 self.value_vec
49 }
50}
51
52impl<'a> std::ops::Index<Wire> for WitnessFiller<'a> {
53 type Output = Word;
54
55 fn index(&self, wire: Wire) -> &Self::Output {
56 &self.value_vec[self.circuit.witness_index(wire)]
57 }
58}
59
60impl<'a> std::ops::IndexMut<Wire> for WitnessFiller<'a> {
61 fn index_mut(&mut self, wire: Wire) -> &mut Self::Output {
62 &mut self.value_vec[self.circuit.witness_index(wire)]
63 }
64}
65
66pub struct Circuit {
71 gate_graph: GateGraph,
72 constraint_system: ConstraintSystem,
73 wire_mapping: SecondaryMap<Wire, ValueIndex>,
74 eval_form: EvalForm,
75}
76
77impl Circuit {
78 pub(super) fn new(
81 gate_graph: GateGraph,
82 constraint_system: ConstraintSystem,
83 wire_mapping: SecondaryMap<Wire, ValueIndex>,
84 eval_form: EvalForm,
85 ) -> Self {
86 assert!(constraint_system.value_vec_layout.validate().is_ok());
87 Self {
88 gate_graph,
89 constraint_system,
90 wire_mapping,
91 eval_form,
92 }
93 }
94
95 #[inline(always)]
97 pub fn witness_index(&self, wire: Wire) -> ValueIndex {
98 self.wire_mapping[wire]
99 }
100
101 pub fn new_witness_filler(&self) -> WitnessFiller<'_> {
103 WitnessFiller {
104 circuit: self,
105 value_vec: ValueVec::new(self.constraint_system.value_vec_layout.clone()),
106 }
107 }
108
109 pub fn populate_wire_witness(&self, w: &mut WitnessFiller) -> Result<(), PopulateError> {
132 for (index, constant) in self.constraint_system.constants.iter().enumerate() {
134 w.value_vec.set(index, *constant);
135 }
136
137 self.eval_form
140 .evaluate(&mut w.value_vec, Some(&self.gate_graph.path_spec_tree))?;
141
142 Ok(())
143 }
144
145 pub fn constraint_system(&self) -> &ConstraintSystem {
147 &self.constraint_system
148 }
149
150 pub fn n_gates(&self) -> usize {
155 self.gate_graph.gates.len()
156 }
157
158 pub fn n_eval_insn(&self) -> usize {
160 self.eval_form.n_eval_insn()
161 }
162
163 pub fn simple_json_dump(&self) -> String {
165 crate::compiler::dump::dump_composition(&self.gate_graph)
166 }
167}