Skip to content

Commit 9b90963

Browse files
committed
Add DefaultHint
1 parent 21b5dfd commit 9b90963

File tree

4 files changed

+32
-35
lines changed

4 files changed

+32
-35
lines changed

circuit/src/builder/compiler/expression_lowerer.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::Op;
1010
use crate::builder::CircuitBuilderError;
1111
use crate::builder::compiler::get_witness_id;
1212
use crate::expr::{Expr, ExpressionGraph};
13-
use crate::op::WitnessHintFiller;
13+
use crate::op::{DefaultHint, WitnessHintFiller};
1414
use crate::types::{ExprId, WitnessAllocator, WitnessId};
1515

1616
/// Sparse disjoint-set "find" with path compression over a HashMap (iterative).
@@ -189,29 +189,18 @@ where
189189

190190
// Pass C: emit arithmetic ops in creation order; tie outputs to class slot if connected
191191
let mut hints_sequence = vec![];
192-
let mut hints_wit_fillers_iter = self.hints_with_fillers.iter();
192+
let mut fillers_iter = self.hints_with_fillers.iter().cloned();
193193
for (expr_idx, expr) in self.graph.nodes().iter().enumerate() {
194194
let expr_id = ExprId(expr_idx as u32);
195195
match expr {
196196
Expr::Const(_) | Expr::Public(_) => { /* handled above */ }
197-
Expr::Witness {
198-
has_filler: false, ..
199-
} => {
200-
let expr_id = ExprId(expr_idx as u32);
201-
let out_widx = alloc_witness_id_for_expr(expr_idx);
202-
expr_to_widx.insert(expr_id, out_widx);
203-
hints_sequence.push(out_widx);
204-
}
205-
Expr::Witness {
206-
last_hint,
207-
has_filler: true,
208-
} => {
197+
Expr::Witness { last_hint } => {
209198
let expr_id = ExprId(expr_idx as u32);
210199
let out_widx = alloc_witness_id_for_expr(expr_idx);
211200
expr_to_widx.insert(expr_id, out_widx);
212201
hints_sequence.push(out_widx);
213202
if *last_hint {
214-
let filler = hints_wit_fillers_iter.next().expect("Hints with fillers can be only created with `alloc_witness_hints`, which in turn must have add a filler");
203+
let filler = fillers_iter.next().unwrap_or(DefaultHint::boxed_default());
215204
let inputs = filler
216205
.inputs()
217206
.iter()
@@ -228,7 +217,7 @@ where
228217
primitive_ops.push(Op::Unconstrained {
229218
inputs,
230219
outputs: hints_sequence,
231-
filler: filler.clone(),
220+
filler,
232221
});
233222
hints_sequence = vec![];
234223
}

circuit/src/builder/expression_builder.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ where
111111
#[allow(unused_variables)]
112112
#[must_use]
113113
pub fn add_witness_hint(&mut self, label: &'static str) -> ExprId {
114-
let expr_id = self.graph.add_expr(Expr::Witness {
115-
last_hint: false,
116-
has_filler: false,
117-
});
114+
let expr_id = self.graph.add_expr(Expr::Witness { last_hint: true });
118115

119116
#[cfg(debug_assertions)]
120117
self.allocation_log.push(AllocationEntry {
@@ -143,7 +140,6 @@ where
143140
.map(|i| {
144141
self.graph.add_expr(Expr::Witness {
145142
last_hint: i == n_outputs - 1,
146-
has_filler: true,
147143
})
148144
})
149145
.collect_vec();
@@ -601,16 +597,7 @@ mod tests {
601597
assert_eq!(builder.graph().nodes().len(), 4);
602598

603599
match (&builder.graph().nodes()[2], &builder.graph().nodes()[3]) {
604-
(
605-
Expr::Witness {
606-
last_hint: false,
607-
has_filler: true,
608-
},
609-
Expr::Witness {
610-
last_hint: true,
611-
has_filler: true,
612-
},
613-
) => (),
600+
(Expr::Witness { last_hint: false }, Expr::Witness { last_hint: true }) => (),
614601
_ => panic!("Expected Witness operation"),
615602
}
616603
}

circuit/src/expr.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ pub enum Expr<F> {
1313
/// a non-deterministic hint. The boolean indicates
1414
/// wether this is the last witness in a sequence of hint.
1515
/// A sequence of hints is one generated from a single filler.
16-
/// TODO: The extra bool indicates whether this witness
17-
/// has an associted filler. We should get rid of this
18-
/// when all witness have an associated filler.
19-
Witness { last_hint: bool, has_filler: bool },
16+
Witness { last_hint: bool },
2017
/// Addition of two expressions
2118
Add { lhs: ExprId, rhs: ExprId },
2219
/// Subtraction of two expressions

circuit/src/op.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use alloc::boxed::Box;
2+
use alloc::vec;
23
use alloc::vec::Vec;
34
use core::fmt::Debug;
45
use core::hash::Hash;
@@ -417,6 +418,29 @@ impl<F> Clone for Box<dyn WitnessHintFiller<F>> {
417418
}
418419
}
419420

421+
#[derive(Debug, Clone, Default)]
422+
pub struct DefaultHint {}
423+
424+
impl DefaultHint {
425+
pub fn boxed_default<F: Default>() -> Box<dyn WitnessHintFiller<F>> {
426+
Box::new(Self::default())
427+
}
428+
}
429+
430+
impl<F: Default> WitnessHintFiller<F> for DefaultHint {
431+
fn inputs(&self) -> &[ExprId] {
432+
&[]
433+
}
434+
435+
fn n_outputs(&self) -> usize {
436+
1
437+
}
438+
439+
fn compute_outputs(&self, _inputs_val: Vec<F>) -> Result<Vec<F>, CircuitError> {
440+
Ok(vec![F::default()])
441+
}
442+
}
443+
420444
// Object-safe "clone into Box" helper
421445
pub trait WitnessFillerClone<F> {
422446
fn clone_box(&self) -> Box<dyn WitnessHintFiller<F>>;

0 commit comments

Comments
 (0)