Skip to main content

crypto_bigint/limb/
sqrt.rs

1//! Square root implementation for [`Limb`].
2
3use super::Limb;
4use crate::{CheckedSquareRoot, CtOption, FloorSquareRoot, U64};
5
6impl CheckedSquareRoot for Limb {
7    type Output = Self;
8
9    fn checked_sqrt(&self) -> CtOption<Self::Output> {
10        U64::from_word(self.0).checked_sqrt().map(|rt| rt.limbs[0])
11    }
12
13    fn checked_sqrt_vartime(&self) -> Option<Self::Output> {
14        U64::from_word(self.0)
15            .checked_sqrt_vartime()
16            .map(|rt| rt.limbs[0])
17    }
18}
19
20impl FloorSquareRoot for Limb {
21    fn floor_sqrt(&self) -> Self::Output {
22        U64::from_word(self.0).floor_sqrt().limbs[0]
23    }
24
25    fn floor_sqrt_vartime(&self) -> Self::Output {
26        U64::from_word(self.0).floor_sqrt_vartime().limbs[0]
27    }
28}