Skip to main content

crypto_bigint/modular/const_monty_form/
ct.rs

1//! Constant-time support: impls of `Ct*` traits and constant-time `const fn` operations.
2
3use super::{ConstMontyForm, ConstMontyParams};
4use crate::{Choice, CtAssign, CtEq};
5use ctutils::{CtAssignSlice, CtEqSlice, CtSelectUsingCtAssign};
6
7#[cfg(feature = "subtle")]
8use crate::CtSelect;
9
10impl<MOD, const LIMBS: usize> CtAssign for ConstMontyForm<MOD, LIMBS>
11where
12    MOD: ConstMontyParams<LIMBS>,
13{
14    fn ct_assign(&mut self, other: &Self, choice: Choice) {
15        self.montgomery_form
16            .ct_assign(&other.montgomery_form, choice);
17    }
18}
19impl<MOD, const LIMBS: usize> CtAssignSlice for ConstMontyForm<MOD, LIMBS> where
20    MOD: ConstMontyParams<LIMBS>
21{
22}
23
24impl<MOD, const LIMBS: usize> CtEq for ConstMontyForm<MOD, LIMBS>
25where
26    MOD: ConstMontyParams<LIMBS>,
27{
28    fn ct_eq(&self, other: &Self) -> Choice {
29        CtEq::ct_eq(&self.montgomery_form, &other.montgomery_form)
30    }
31}
32impl<MOD, const LIMBS: usize> CtEqSlice for ConstMontyForm<MOD, LIMBS> where
33    MOD: ConstMontyParams<LIMBS>
34{
35}
36
37impl<MOD, const LIMBS: usize> CtSelectUsingCtAssign for ConstMontyForm<MOD, LIMBS> where
38    MOD: ConstMontyParams<LIMBS>
39{
40}
41
42#[cfg(feature = "subtle")]
43impl<MOD, const LIMBS: usize> subtle::ConstantTimeEq for ConstMontyForm<MOD, LIMBS>
44where
45    MOD: ConstMontyParams<LIMBS>,
46{
47    fn ct_eq(&self, other: &Self) -> subtle::Choice {
48        CtEq::ct_eq(&self.montgomery_form, &other.montgomery_form).into()
49    }
50}
51
52#[cfg(feature = "subtle")]
53impl<MOD, const LIMBS: usize> subtle::ConditionallySelectable for ConstMontyForm<MOD, LIMBS>
54where
55    MOD: ConstMontyParams<LIMBS> + Copy,
56{
57    fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self {
58        CtSelect::ct_select(a, b, choice.into())
59    }
60}