Skip to main content

kimchi/circuits/witness/
variable_bits_cell.rs

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