-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_zero.rs
69 lines (60 loc) · 2.08 KB
/
is_zero.rs
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
use halo2_proofs::{
arithmetic::FieldExt,
circuit::*,
plonk::*,
poly::Rotation,
};
#[derive(Clone, Debug)]
pub struct IsZeroConfig<F> {
pub value_inv: Column<Advice>,
pub is_zero_expr: Expression<F>,
}
impl<F: FieldExt> IsZeroConfig<F> {
pub fn expr(&self) -> Expression<F> {
self.is_zero_expr.clone()
}
}
pub struct IsZeroChip<F: FieldExt> {
config: IsZeroConfig<F>,
}
impl<F: FieldExt> IsZeroChip<F> {
pub fn construct(config: IsZeroConfig<F>) -> Self {
IsZeroChip { config }
}
pub fn configure(
meta: &mut ConstraintSystem<F>,
q_enable: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression<F>,
value: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression<F>,
value_inv: Column<Advice>,
) -> IsZeroConfig<F> {
let mut is_zero_expr = Expression::Constant(F::zero());
meta.create_gate("is_zero", |meta| {
//
// valid | value | value_inv | 1 - value * value_inv | value * (1 - value * value_inv)
// yes | x | 1/x | 0 | 0
// no | x | 0 | 1 | x
// yes | 0 | 0 | 1 | 0
// yse | 0 | y | 1 | 0
//
let value = value(meta);
let q_enable = q_enable(meta);
let value_inv = meta.query_advice(value_inv, Rotation::cur());
is_zero_expr = Expression::Constant(F::one()) - value.clone() * value_inv;
vec![q_enable * value * is_zero_expr.clone()]
});
IsZeroConfig {
value_inv,
is_zero_expr,
}
}
pub fn assign(
&self,
region: &mut Region<'_, F>,
offset: usize,
value: Value<F>,
) -> Result<(), Error> {
let value_inv = value.map(|value| value.invert().unwrap_or(F::zero()));
region.assign_advice(|| "value inv", self.config.value_inv, offset, || value_inv)?;
Ok(())
}
}