fhe_math/rq/
switcher.rs

1#![warn(missing_docs, unused_imports)]
2
3//! Polynomial modulus switcher.
4
5use super::{scaler::Scaler, Context, Poly};
6use crate::{rns::ScalingFactor, Result};
7use std::sync::Arc;
8
9/// Context switcher.
10#[derive(Default, Debug, Clone, PartialEq, Eq)]
11pub struct Switcher {
12    pub(crate) scaler: Scaler,
13}
14
15impl Switcher {
16    /// Create a switcher from a context `from` to a context `to`.
17    pub fn new(from: &Arc<Context>, to: &Arc<Context>) -> Result<Self> {
18        Ok(Self {
19            scaler: Scaler::new(from, to, ScalingFactor::new(to.modulus(), from.modulus()))?,
20        })
21    }
22
23    /// Switch a polynomial.
24    pub(crate) fn switch(&self, p: &Poly) -> Result<Poly> {
25        self.scaler.scale(p)
26    }
27}