Skip to main content

fancy_garbling/circuits/arithmetic/
constant.rs

1use crate::{Circuit, CrtBundle, Fancy, util::factor};
2use swanky_channel::Channel;
3use swanky_error::Result;
4
5/// Arithmetic constant.
6///
7/// For `(value, modulus)`, return a [`CrtBundle`] containing `value` in its CRT
8/// representation.
9pub struct Constant {
10    xs: Vec<u16>,
11    moduli: Vec<u16>,
12}
13
14impl Constant {
15    /// Create a new [`Constant`] circuit for `value % modulus`.
16    pub fn new(value: u128, modulus: u128) -> Self {
17        let moduli = factor(modulus);
18        let xs = moduli
19            .iter()
20            .map(|&p| (value % p as u128) as u16)
21            .collect::<Vec<_>>();
22        Self { xs, moduli }
23    }
24}
25
26impl<F: Fancy> Circuit<F> for Constant {
27    type Input = ();
28    type Output = CrtBundle<F::Item>;
29
30    fn execute(
31        &self,
32        backend: &mut F,
33        _: Self::Input,
34        channel: &mut Channel,
35    ) -> Result<Self::Output> {
36        let constants = self
37            .xs
38            .iter()
39            .zip(self.moduli.iter())
40            .map(|(&x, &p)| backend.constant(x, p, channel))
41            .collect::<Result<_>>()?;
42        Ok(CrtBundle::new(constants))
43    }
44}