Skip to main content

bitwarden_crypto_primitives/
ristretto255.rs

1//! ristretto255 group operations (RFC 9496), as used by CPace.
2
3use crate::{Error, backend};
4
5/// Compressed point length in bytes.
6pub const POINT_LEN: usize = 32;
7/// Length in bytes of the uniform input to `from_uniform_bytes` and of wide scalars.
8pub const WIDE_LEN: usize = 64;
9
10/// A ristretto255 group element.
11#[derive(Clone, Copy, PartialEq, Eq)]
12pub struct RistrettoPoint(backend::ristretto255::RistrettoPoint);
13
14impl RistrettoPoint {
15    /// Maps 64 uniform bytes to a point (RFC 9496 §4.3.4).
16    pub fn from_uniform_bytes(bytes: &[u8; WIDE_LEN]) -> Self {
17        Self(backend::ristretto255::RistrettoPoint::from_uniform_bytes(
18            bytes,
19        ))
20    }
21
22    /// Decodes a canonical 32-byte encoding.
23    pub fn decompress(bytes: &[u8]) -> Result<Self, Error> {
24        backend::ristretto255::RistrettoPoint::decompress(bytes).map(Self)
25    }
26
27    /// Returns the canonical 32-byte encoding.
28    pub fn compress(&self) -> [u8; POINT_LEN] {
29        self.0.compress()
30    }
31
32    /// Returns whether this is the identity element.
33    pub fn is_identity(&self) -> bool {
34        self.0.is_identity()
35    }
36
37    /// Multiplies by the scalar `wide_scalar mod ℓ` (64 bytes, little-endian).
38    pub fn mul_wide(&self, wide_scalar: &[u8; WIDE_LEN]) -> Self {
39        Self(self.0.mul_wide(wide_scalar))
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use crate::test_vectors::{array, bytes};
47
48    const UNIFORM_01: &str = "0101010101010101010101010101010101010101010101010101010101010101\
49    0101010101010101010101010101010101010101010101010101010101010101";
50
51    #[test]
52    fn scalar_multiplication_commutes() {
53        let g = RistrettoPoint::from_uniform_bytes(&array(UNIFORM_01));
54        let (a, b) = (
55            array(
56                "0202020202020202020202020202020202020202020202020202020202020202\
57                      0202020202020202020202020202020202020202020202020202020202020202",
58            ),
59            array(
60                "0303030303030303030303030303030303030303030303030303030303030303\
61                      0303030303030303030303030303030303030303030303030303030303030303",
62            ),
63        );
64        assert!(g.mul_wide(&a).mul_wide(&b) == g.mul_wide(&b).mul_wide(&a));
65    }
66
67    #[test]
68    fn encoding_roundtrip_and_identity() {
69        let g = RistrettoPoint::from_uniform_bytes(&array(UNIFORM_01));
70        assert!(RistrettoPoint::decompress(&g.compress()).expect("canonical") == g);
71        assert!(!g.is_identity());
72
73        let identity = RistrettoPoint::decompress(&bytes(
74            "0000000000000000000000000000000000000000000000000000000000000000",
75        ))
76        .expect("identity encodes as zeros");
77        assert!(identity.is_identity());
78
79        assert_eq!(
80            RistrettoPoint::decompress(&bytes(
81                "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
82            ))
83            .err(),
84            Some(Error::InvalidKey)
85        );
86        assert_eq!(
87            RistrettoPoint::decompress(&bytes(
88                "00000000000000000000000000000000000000000000000000000000000000"
89            ))
90            .err(),
91            Some(Error::InvalidKey)
92        );
93    }
94
95    /// RFC 9496 Appendix A.1: the encoding of the basepoint `B`.
96    const BASEPOINT: &str = "e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76";
97
98    #[test]
99    fn known_answers() {
100        const SCALAR_24: &str = "2424242424242424242424242424242424242424242424242424242424242424\
101        2424242424242424242424242424242424242424242424242424242424242424";
102        /// Wide scalar 1 (little-endian).
103        const ONE: &str = "0100000000000000000000000000000000000000000000000000000000000000\
104        0000000000000000000000000000000000000000000000000000000000000000";
105
106        let point = RistrettoPoint::from_uniform_bytes(&array(
107            "4242424242424242424242424242424242424242424242424242424242424242\
108            4242424242424242424242424242424242424242424242424242424242424242",
109        ));
110        assert_eq!(
111            hex::encode(point.compress()),
112            "441bdd7b3fb1a07d8a8add8ca6ede38cbce78eef2a34323aa4c2cce37db81561"
113        );
114        assert_eq!(
115            hex::encode(point.mul_wide(&array(SCALAR_24)).compress()),
116            "ca2d29b8b09bf24387d77c47cb5495332b44ac465c1a35e5bca5302a77f48239"
117        );
118
119        let basepoint = RistrettoPoint::decompress(&bytes(BASEPOINT)).expect("canonical");
120        assert_eq!(hex::encode(basepoint.compress()), BASEPOINT);
121
122        // Multiplying by scalar 1 is the identity map.
123        assert!(basepoint.mul_wide(&array(ONE)) == basepoint);
124    }
125}