hopr_crypto_sphinx/
ec_groups.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use curve25519_dalek::traits::IsIdentity;
use hopr_crypto_types::errors::Result;

#[cfg(feature = "secp256k1")]
use elliptic_curve::{ops::MulByGenerator, Group};

use crate::shared_keys::{Alpha, GroupElement, Scalar, SphinxSuite};

#[cfg(any(feature = "x25519", feature = "ed25519"))]
impl Scalar for curve25519_dalek::scalar::Scalar {
    fn random() -> Self {
        let bytes = hopr_crypto_random::random_bytes::<32>();
        Self::from_bytes(&bytes).unwrap()
    }

    fn from_bytes(bytes: &[u8]) -> Result<Self> {
        hopr_crypto_types::utils::x25519_scalar_from_bytes(bytes)
    }

    fn to_bytes(&self) -> Box<[u8]> {
        self.to_bytes().into()
    }
}

#[cfg(feature = "secp256k1")]
impl Scalar for k256::Scalar {
    fn random() -> Self {
        // Beware this is not constant time
        let mut bytes = k256::FieldBytes::default();
        loop {
            hopr_crypto_random::random_fill(&mut bytes);
            if let Ok(scalar) = Self::from_bytes(&bytes) {
                return scalar;
            }
        }
    }

    fn from_bytes(bytes: &[u8]) -> Result<Self> {
        hopr_crypto_types::utils::k256_scalar_from_bytes(bytes)
    }

    fn to_bytes(&self) -> Box<[u8]> {
        let ret = self.to_bytes();
        Box::<[u8]>::from(ret.as_slice())
    }
}

#[cfg(feature = "x25519")]
impl GroupElement<curve25519_dalek::scalar::Scalar> for curve25519_dalek::montgomery::MontgomeryPoint {
    type AlphaLen = typenum::U32;

    fn to_alpha(&self) -> Alpha<typenum::U32> {
        self.0.into()
    }

    fn from_alpha(alpha: Alpha<typenum::U32>) -> Result<Self> {
        Ok(curve25519_dalek::montgomery::MontgomeryPoint(alpha.into()))
    }

    fn generate(scalar: &curve25519_dalek::scalar::Scalar) -> Self {
        scalar * curve25519_dalek::constants::X25519_BASEPOINT
    }

    fn is_valid(&self) -> bool {
        !self.is_identity()
    }
}

#[cfg(feature = "ed25519")]
impl GroupElement<curve25519_dalek::scalar::Scalar> for curve25519_dalek::edwards::EdwardsPoint {
    type AlphaLen = typenum::U32;

    fn to_alpha(&self) -> Alpha<typenum::U32> {
        self.compress().0.into()
    }

    fn from_alpha(alpha: Alpha<typenum::U32>) -> Result<Self> {
        curve25519_dalek::edwards::CompressedEdwardsY(alpha.into())
            .decompress()
            .ok_or(hopr_crypto_types::errors::CryptoError::InvalidInputValue)
    }

    fn generate(scalar: &curve25519_dalek::scalar::Scalar) -> Self {
        scalar * curve25519_dalek::constants::ED25519_BASEPOINT_POINT
    }

    fn is_valid(&self) -> bool {
        self.is_torsion_free() && !self.is_identity()
    }
}

#[cfg(feature = "secp256k1")]
impl GroupElement<k256::Scalar> for k256::ProjectivePoint {
    type AlphaLen = typenum::U33;

    fn to_alpha(&self) -> Alpha<typenum::U33> {
        let mut ret = Alpha::<typenum::U33>::default();
        ret.copy_from_slice(
            hopr_crypto_types::types::CurvePoint::from(self.to_affine())
                .as_compressed()
                .as_ref(),
        );
        ret
    }

    fn from_alpha(alpha: Alpha<typenum::U33>) -> Result<Self> {
        let v: &[u8] = alpha.as_ref();
        hopr_crypto_types::types::CurvePoint::try_from(v)
            .map(|c| c.into_projective_point())
            .map_err(|_| hopr_crypto_types::errors::CryptoError::InvalidInputValue)
    }

    fn generate(scalar: &k256::Scalar) -> Self {
        k256::ProjectivePoint::mul_by_generator(scalar)
    }

    fn is_valid(&self) -> bool {
        self.is_identity().unwrap_u8() == 0
    }
}

/// Represents an instantiation of the Sphinx protocol using secp256k1 elliptic curve and `ChainKeypair`
#[cfg(feature = "secp256k1")]
pub struct Secp256k1Suite;

#[cfg(feature = "secp256k1")]
impl SphinxSuite for Secp256k1Suite {
    type P = hopr_crypto_types::keypairs::ChainKeypair;
    type E = k256::Scalar;
    type G = k256::ProjectivePoint;
}

/// Represents an instantiation of the Sphinx protocol using the ed25519 curve and `OffchainKeypair`
#[cfg(feature = "ed25519")]
pub struct Ed25519Suite;

#[cfg(feature = "ed25519")]
impl SphinxSuite for Ed25519Suite {
    type P = hopr_crypto_types::keypairs::OffchainKeypair;
    type E = curve25519_dalek::scalar::Scalar;
    type G = curve25519_dalek::edwards::EdwardsPoint;
}

/// Represents an instantiation of the Sphinx protocol using the Curve25519 curve and `OffchainKeypair`
#[cfg(feature = "x25519")]
pub struct X25519Suite;

#[cfg(feature = "x25519")]
impl SphinxSuite for X25519Suite {
    type P = hopr_crypto_types::keypairs::OffchainKeypair;
    type E = curve25519_dalek::scalar::Scalar;
    type G = curve25519_dalek::montgomery::MontgomeryPoint;
}

#[cfg(test)]
mod tests {
    use crate::shared_keys::tests::generic_sphinx_suite_test;
    use crate::shared_keys::GroupElement;
    use hex_literal::hex;
    use parameterized::parameterized;

    #[test]
    fn test_extract_key_from_group_element() {
        let salt = [0xde, 0xad, 0xbe, 0xef];
        let pt = k256::ProjectivePoint::GENERATOR;

        let key = pt.extract_key(&salt);

        let res = hex!("54bf34178075e153f481ce05b113c1530ecc45a2f1f13a3366d4389f65470de6");
        assert_eq!(res, key.as_ref());
    }

    #[test]
    fn test_expand_key_from_group_element() {
        let salt = [0xde, 0xad, 0xbe, 0xef];
        let pt = k256::ProjectivePoint::GENERATOR;

        let key = pt.expand_key(&salt);

        let res = hex!("d138d9367474911f7124b95be844d2f8a6d34e962694e37e8717bdbd3c15690b");
        assert_eq!(res, key.as_ref());
    }

    #[cfg(feature = "secp256k1")]
    #[parameterized(nodes = {4, 3, 2, 1})]
    fn test_secp256k1_suite(nodes: usize) {
        generic_sphinx_suite_test::<crate::ec_groups::Secp256k1Suite>(nodes)
    }

    #[cfg(feature = "ed25519")]
    #[parameterized(nodes = {4, 3, 2, 1})]
    fn test_ed25519_shared_keys(nodes: usize) {
        generic_sphinx_suite_test::<crate::ec_groups::Ed25519Suite>(nodes)
    }

    #[cfg(feature = "x25519")]
    #[parameterized(nodes = {4, 3, 2, 1})]
    fn test_montgomery_shared_keys(nodes: usize) {
        generic_sphinx_suite_test::<crate::ec_groups::X25519Suite>(nodes)
    }
}