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, CtGt, CtLt, 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> CtGt for ConstMontyForm<MOD, LIMBS>
38where
39    MOD: ConstMontyParams<LIMBS>,
40{
41    fn ct_gt(&self, other: &Self) -> Choice {
42        self.retrieve().ct_gt(&other.retrieve())
43    }
44}
45
46impl<MOD, const LIMBS: usize> CtLt for ConstMontyForm<MOD, LIMBS>
47where
48    MOD: ConstMontyParams<LIMBS>,
49{
50    fn ct_lt(&self, other: &Self) -> Choice {
51        self.retrieve().ct_lt(&other.retrieve())
52    }
53}
54
55impl<MOD, const LIMBS: usize> CtSelectUsingCtAssign for ConstMontyForm<MOD, LIMBS> where
56    MOD: ConstMontyParams<LIMBS>
57{
58}
59
60#[cfg(feature = "subtle")]
61impl<MOD, const LIMBS: usize> subtle::ConstantTimeEq for ConstMontyForm<MOD, LIMBS>
62where
63    MOD: ConstMontyParams<LIMBS>,
64{
65    fn ct_eq(&self, other: &Self) -> subtle::Choice {
66        CtEq::ct_eq(&self.montgomery_form, &other.montgomery_form).into()
67    }
68}
69
70#[cfg(feature = "subtle")]
71impl<MOD, const LIMBS: usize> subtle::ConditionallySelectable for ConstMontyForm<MOD, LIMBS>
72where
73    MOD: ConstMontyParams<LIMBS> + Copy,
74{
75    fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self {
76        CtSelect::ct_select(a, b, choice.into())
77    }
78}
79
80#[cfg(feature = "subtle")]
81impl<MOD, const LIMBS: usize> subtle::ConstantTimeGreater for ConstMontyForm<MOD, LIMBS>
82where
83    MOD: ConstMontyParams<LIMBS>,
84{
85    fn ct_gt(&self, other: &Self) -> subtle::Choice {
86        CtGt::ct_gt(self, other).into()
87    }
88}
89
90#[cfg(feature = "subtle")]
91impl<MOD, const LIMBS: usize> subtle::ConstantTimeLess for ConstMontyForm<MOD, LIMBS>
92where
93    MOD: ConstMontyParams<LIMBS>,
94{
95    fn ct_lt(&self, other: &Self) -> subtle::Choice {
96        CtLt::ct_lt(self, other).into()
97    }
98}