Skip to main content

crypto_bigint/modular/fixed_monty_form/
ct.rs

1//! Constant-time support: impls of `Ct*` traits and constant-time `const fn` operations.
2
3use crate::{Choice, CtAssign, CtEq, modular::FixedMontyForm};
4use ctutils::{CtAssignSlice, CtEqSlice, CtSelectUsingCtAssign};
5
6#[cfg(feature = "subtle")]
7use crate::CtSelect;
8
9impl<const LIMBS: usize> CtAssign for FixedMontyForm<LIMBS> {
10    fn ct_assign(&mut self, other: &Self, choice: Choice) {
11        self.montgomery_form
12            .ct_assign(&other.montgomery_form, choice);
13        self.params.ct_assign(&other.params, choice);
14    }
15}
16impl<const LIMBS: usize> CtAssignSlice for FixedMontyForm<LIMBS> {}
17impl<const LIMBS: usize> CtSelectUsingCtAssign for FixedMontyForm<LIMBS> {}
18
19impl<const LIMBS: usize> CtEq for FixedMontyForm<LIMBS> {
20    fn ct_eq(&self, other: &Self) -> Choice {
21        self.montgomery_form.ct_eq(&other.montgomery_form) & self.params.ct_eq(&other.params)
22    }
23}
24impl<const LIMBS: usize> CtEqSlice for FixedMontyForm<LIMBS> {}
25
26#[cfg(feature = "subtle")]
27impl<const LIMBS: usize> subtle::ConstantTimeEq for FixedMontyForm<LIMBS> {
28    fn ct_eq(&self, other: &Self) -> subtle::Choice {
29        CtEq::ct_eq(self, other).into()
30    }
31}
32
33#[cfg(feature = "subtle")]
34impl<const LIMBS: usize> subtle::ConditionallySelectable for FixedMontyForm<LIMBS> {
35    fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self {
36        a.ct_select(b, choice.into())
37    }
38}