Skip to main content

bitwarden_crypto_primitives/rustcrypto/
ristretto255.rs

1use curve25519_dalek::{
2    ristretto::{CompressedRistretto, RistrettoPoint as Point},
3    scalar::Scalar,
4    traits::Identity,
5};
6use zeroize::Zeroizing;
7
8use crate::{
9    Error,
10    ristretto255::{POINT_LEN, WIDE_LEN},
11};
12
13#[derive(Clone, Copy, PartialEq, Eq)]
14pub(crate) struct RistrettoPoint(Point);
15
16impl RistrettoPoint {
17    pub(crate) fn from_uniform_bytes(bytes: &[u8; WIDE_LEN]) -> Self {
18        Self(Point::from_uniform_bytes(bytes))
19    }
20
21    pub(crate) fn decompress(bytes: &[u8]) -> Result<Self, Error> {
22        CompressedRistretto::from_slice(bytes)
23            .ok()
24            .and_then(|c| c.decompress())
25            .map(Self)
26            .ok_or(Error::InvalidKey)
27    }
28
29    pub(crate) fn compress(&self) -> [u8; POINT_LEN] {
30        self.0.compress().to_bytes()
31    }
32
33    pub(crate) fn is_identity(&self) -> bool {
34        self.0 == Point::identity()
35    }
36
37    pub(crate) fn mul_wide(&self, wide_scalar: &[u8; WIDE_LEN]) -> Self {
38        let scalar = Zeroizing::new(Scalar::from_bytes_mod_order_wide(wide_scalar));
39        Self(self.0 * *scalar)
40    }
41}