hopr_crypto_packet/
por.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use hopr_crypto_sphinx::{
    derivation::{derive_ack_key_share, derive_own_key_share},
    shared_keys::SharedSecret,
};
use hopr_crypto_types::types::{Challenge, CurvePoint, HalfKey, HalfKeyChallenge, PublicKey, Response};
use hopr_primitive_types::prelude::*;
use tracing::error;

use crate::errors::{PacketError, Result};

/// Proof of Relay secret length is twice the size of secp256k1 public key
pub const POR_SECRET_LENGTH: usize = 2 * PublicKey::SIZE_COMPRESSED;

/// Type that contains the challenge for the first ticket sent to the first relayer.
#[derive(Clone)]
pub struct ProofOfRelayValues {
    pub ack_challenge: HalfKeyChallenge,
    pub ticket_challenge: Challenge,
    pub own_key: HalfKey,
}

impl ProofOfRelayValues {
    /// Takes the secrets which the first and the second relayer are able to derive from the packet header
    /// and computes the challenge for the first ticket.
    pub fn new(secret_b: &SharedSecret, secret_c: Option<&SharedSecret>) -> Self {
        let s0 = derive_own_key_share(secret_b);
        let s1 = derive_ack_key_share(secret_c.unwrap_or(&SharedSecret::random()));

        Self {
            // ack_challenge = s0_ack * G
            ack_challenge: derive_ack_key_share(secret_b).to_challenge(),
            // ticket challenge (s0_own + s1_ack) * G
            ticket_challenge: Response::from_half_keys(&s0, &s1)
                .expect("failed to derive response")
                .to_challenge(),
            own_key: s0,
        }
    }
}

/// Contains the Proof of Relay challenge for the next downstream node as well as the hint that is used to
/// verify the challenge that is given to the relayer.
#[derive(Clone)]
pub struct ProofOfRelayString {
    pub next_ticket_challenge: Challenge,
    pub hint: HalfKeyChallenge,
}

impl ProofOfRelayString {
    /// Creates an instance from the shared secrets with node+2 and node+3
    pub fn new(secret_c: &SharedSecret, secret_d: Option<&SharedSecret>) -> Self {
        let s0 = derive_ack_key_share(secret_c); // s0_ack
        let s1 = derive_own_key_share(secret_c); // s1_own
        let s2 = derive_ack_key_share(secret_d.unwrap_or(&SharedSecret::random())); // s2_ack

        Self {
            next_ticket_challenge: Response::from_half_keys(&s1, &s2) // (s1_own + s2_ack) * G
                .expect("failed to derive response")
                .to_challenge(),
            hint: s0.to_challenge(), // s0_ack * G
        }
    }

    /// Generates Proof of Relay challenges from the shared secrets of the
    /// outgoing packet.
    pub fn from_shared_secrets(secrets: &[SharedSecret]) -> Vec<[u8; ProofOfRelayString::SIZE]> {
        (1..secrets.len())
            .map(|i| ProofOfRelayString::new(&secrets[i], secrets.get(i + 1)).into())
            .collect::<Vec<_>>()
    }
}

impl TryFrom<&[u8]> for ProofOfRelayString {
    type Error = GeneralError;

    fn try_from(value: &[u8]) -> std::result::Result<Self, Self::Error> {
        if value.len() == POR_SECRET_LENGTH {
            let (next_ticket_challenge, hint) = value.split_at(POR_SECRET_LENGTH / 2);
            Ok(Self {
                next_ticket_challenge: CurvePoint::try_from(next_ticket_challenge)?.into(),
                hint: HalfKeyChallenge::new(hint),
            })
        } else {
            Err(GeneralError::ParseError("ProofOfRelayString".into()))
        }
    }
}

impl From<ProofOfRelayString> for [u8; PROOF_OF_RELAY_STRING_LEN] {
    fn from(value: ProofOfRelayString) -> Self {
        let mut ret = [0u8; PROOF_OF_RELAY_STRING_LEN];
        ret[0..Challenge::SIZE].copy_from_slice(value.next_ticket_challenge.as_ref());
        ret[Challenge::SIZE..].copy_from_slice(value.hint.as_ref());
        ret
    }
}

const PROOF_OF_RELAY_STRING_LEN: usize = Challenge::SIZE + HalfKeyChallenge::SIZE;
impl BytesEncodable<PROOF_OF_RELAY_STRING_LEN> for ProofOfRelayString {}

/// Derivable challenge which contains the key share of the relayer as well as the secret that was used
/// to create it and the challenge for the next relayer.
#[derive(Clone)]
pub struct ProofOfRelayOutput {
    pub own_key: HalfKey,
    pub own_share: HalfKeyChallenge,
    pub next_ticket_challenge: Challenge,
    pub ack_challenge: HalfKeyChallenge,
}

/// Verifies that an incoming packet contains all values that are necessary to reconstruct the response to redeem the
/// incentive for relaying the packet.
/// # Arguments
/// * `secret` shared secret with the creator of the packet
/// * `por_bytes` serialized `ProofOfRelayString` as included within the packet
/// * `challenge` ticket challenge of the incoming ticket
pub fn pre_verify(
    secret: &SharedSecret,
    por_bytes: &[u8],
    challenge: &EthereumChallenge,
) -> Result<ProofOfRelayOutput> {
    assert_eq!(POR_SECRET_LENGTH, por_bytes.len(), "invalid por bytes length");

    let pors = ProofOfRelayString::try_from(por_bytes)?;

    let own_key = derive_own_key_share(secret);
    let own_share = own_key.to_challenge();

    if Challenge::from_hint_and_share(&own_share, &pors.hint)?
        .to_ethereum_challenge()
        .eq(challenge)
    {
        Ok(ProofOfRelayOutput {
            next_ticket_challenge: pors.next_ticket_challenge,
            ack_challenge: pors.hint,
            own_key,
            own_share,
        })
    } else {
        Err(PacketError::PoRVerificationError)
    }
}

/// Checks if the given acknowledgement solves the given challenge.
pub fn validate_por_half_keys(ethereum_challenge: &EthereumChallenge, own_key: &HalfKey, ack: &HalfKey) -> bool {
    Response::from_half_keys(own_key, ack)
        .map(|response| validate_por_response(ethereum_challenge, &response))
        .unwrap_or_else(|e| {
            error!(error = %e, "failed to validate por half keys");
            false
        })
}

/// Checks if the given response solves the given challenge.
pub fn validate_por_response(ethereum_challenge: &EthereumChallenge, response: &Response) -> bool {
    response.to_challenge().to_ethereum_challenge().eq(ethereum_challenge)
}

/// Checks if the given acknowledgement solves the given challenge.
pub fn validate_por_hint(ethereum_challenge: &EthereumChallenge, own_share: &HalfKeyChallenge, ack: &HalfKey) -> bool {
    Challenge::from_own_share_and_half_key(own_share, ack)
        .map(|c| c.to_ethereum_challenge().eq(ethereum_challenge))
        .unwrap_or_else(|e| {
            error!(error = %e,"failed to validate por hint");
            false
        })
}

#[cfg(test)]
mod tests {
    use crate::por::{
        pre_verify, validate_por_half_keys, validate_por_hint, validate_por_response, ProofOfRelayString,
        ProofOfRelayValues,
    };
    use hopr_crypto_sphinx::derivation::derive_ack_key_share;
    use hopr_crypto_sphinx::shared_keys::SharedSecret;
    use hopr_crypto_types::types::Response;
    use hopr_primitive_types::traits::BytesEncodable;

    #[test]
    fn test_por_preverify_validate() -> anyhow::Result<()> {
        const AMOUNT: usize = 4;

        let secrets = (0..AMOUNT).map(|_| SharedSecret::random()).collect::<Vec<_>>();

        // Generated challenge
        let first_challenge = ProofOfRelayValues::new(&secrets[0], Some(&secrets[1]));

        // For the first relayer
        let first_por_string = ProofOfRelayString::new(&secrets[1], Some(&secrets[2])).into_encoded();

        // For the second relayer
        let second_por_string = ProofOfRelayString::new(&secrets[2], Some(&secrets[3]));

        // Computation result of the first relayer before receiving an acknowledgement from the second relayer
        let first_challenge_eth = first_challenge.ticket_challenge.to_ethereum_challenge();
        let first_result = pre_verify(&secrets[0], &first_por_string, &first_challenge_eth)
            .expect("First challenge must be plausible");

        let expected_hkc = derive_ack_key_share(&secrets[1]).to_challenge();
        assert_eq!(expected_hkc, first_result.ack_challenge);

        // Simulates the transformation done by the first relayer
        let expected_pors = ProofOfRelayString::try_from(first_por_string.as_ref())?;
        assert_eq!(
            expected_pors.next_ticket_challenge, first_result.next_ticket_challenge,
            "Forward logic must extract correct challenge for the next downstream node"
        );

        // Computes the cryptographic material that is part of the acknowledgement
        let first_ack = derive_ack_key_share(&secrets[1]);
        assert!(
            validate_por_half_keys(
                &first_challenge.ticket_challenge.to_ethereum_challenge(),
                &first_result.own_key,
                &first_ack
            ),
            "Acknowledgement must solve the challenge"
        );

        // Simulates the transformation as done by the second relayer
        let first_result_challenge_eth = first_result.next_ticket_challenge.to_ethereum_challenge();
        let second_result = pre_verify(
            &secrets[1],
            &second_por_string.into_encoded(),
            &first_result_challenge_eth,
        )
        .expect("Second challenge must be plausible");

        let second_ack = derive_ack_key_share(&secrets[2]);
        assert!(
            validate_por_half_keys(
                &first_result.next_ticket_challenge.to_ethereum_challenge(),
                &second_result.own_key,
                &second_ack
            ),
            "Second acknowledgement must solve the challenge"
        );

        assert!(
            validate_por_hint(
                &first_result.next_ticket_challenge.to_ethereum_challenge(),
                &second_result.own_share,
                &second_ack
            ),
            "Second acknowledgement must solve the challenge"
        );

        Ok(())
    }

    #[test]
    fn test_challenge_and_response_solving() -> anyhow::Result<()> {
        const AMOUNT: usize = 2;
        let secrets = (0..AMOUNT).map(|_| SharedSecret::random()).collect::<Vec<_>>();

        let first_challenge = ProofOfRelayValues::new(&secrets[0], Some(&secrets[1]));
        let ack = derive_ack_key_share(&secrets[1]);

        assert!(
            validate_por_half_keys(
                &first_challenge.ticket_challenge.to_ethereum_challenge(),
                &first_challenge.own_key,
                &ack
            ),
            "Challenge must be solved"
        );

        assert!(
            validate_por_response(
                &first_challenge.ticket_challenge.to_ethereum_challenge(),
                &Response::from_half_keys(&first_challenge.own_key, &ack)?
            ),
            "Returned response must solve the challenge"
        );

        Ok(())
    }
}