bitwarden_crypto_primitives/
ristretto255.rs1use crate::{Error, backend};
4
5pub const POINT_LEN: usize = 32;
7pub const WIDE_LEN: usize = 64;
9
10#[derive(Clone, Copy, PartialEq, Eq)]
12pub struct RistrettoPoint(backend::ristretto255::RistrettoPoint);
13
14impl RistrettoPoint {
15 pub fn from_uniform_bytes(bytes: &[u8; WIDE_LEN]) -> Self {
17 Self(backend::ristretto255::RistrettoPoint::from_uniform_bytes(
18 bytes,
19 ))
20 }
21
22 pub fn decompress(bytes: &[u8]) -> Result<Self, Error> {
24 backend::ristretto255::RistrettoPoint::decompress(bytes).map(Self)
25 }
26
27 pub fn compress(&self) -> [u8; POINT_LEN] {
29 self.0.compress()
30 }
31
32 pub fn is_identity(&self) -> bool {
34 self.0.is_identity()
35 }
36
37 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 const BASEPOINT: &str = "e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76";
97
98 #[test]
99 fn known_answers() {
100 const SCALAR_24: &str = "2424242424242424242424242424242424242424242424242424242424242424\
101 2424242424242424242424242424242424242424242424242424242424242424";
102 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 assert!(basepoint.mul_wide(&array(ONE)) == basepoint);
124 }
125}