kimchi/circuits/witness/
variable_bits_cell.rs

1use super::{variables::Variables, WitnessCell};
2use ark_ff::Field;
3use o1_utils::FieldHelpers;
4
5/// Witness cell assigned from bits of a variable
6/// See [Variables] for more details
7pub struct VariableBitsCell<'a> {
8    name: &'a str,
9    start: usize,       // inclusive
10    end: Option<usize>, // exclusive
11}
12
13impl<'a> VariableBitsCell<'a> {
14    /// Create witness cell assigned from the bits [start, end) of named variable.
15    /// If end is None, then the final bit corresponds to the position of the highest bit of the variable.
16    pub fn create(name: &'a str, start: usize, end: Option<usize>) -> Box<VariableBitsCell<'a>> {
17        Box::new(VariableBitsCell { name, start, end })
18    }
19}
20
21impl<'a, F: Field, const W: usize> WitnessCell<F, F, W> for VariableBitsCell<'a> {
22    fn value(&self, _witness: &mut [Vec<F>; W], variables: &Variables<F>, _index: usize) -> F {
23        let bits = if let Some(end) = self.end {
24            F::from_bits(&variables[self.name].to_bits()[self.start..end])
25        } else {
26            F::from_bits(&variables[self.name].to_bits()[self.start..])
27        };
28        bits.expect("failed to deserialize field bits for variable bits cell")
29    }
30}