zkgroup/crypto/
receipt_struct.rs

1//
2// Copyright 2021 Signal Messenger, LLC.
3// SPDX-License-Identifier: AGPL-3.0-only
4//
5
6use curve25519_dalek_signal::scalar::Scalar;
7use serde::{Deserialize, Serialize};
8
9use crate::common::sho::Sho;
10use crate::common::simple_types::{ReceiptLevel, ReceiptSerialBytes, Timestamp};
11
12/// The full set of information known by the client after receiving the credential response from
13/// the issuing server. It will all be shared with the credential presentation. Initially the
14/// client only knows the receipt_serial_bytes which is randomly generated. receipt_serial_bytes
15/// should never be shared with the issuing service in unencrypted form.
16///
17/// Clients must do validation on the returned receipt_expiration_time and receipt_level to ensure
18/// no tagging has occurred.
19#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
20pub struct ReceiptStruct {
21    pub(crate) receipt_serial_bytes: ReceiptSerialBytes,
22    pub(crate) receipt_expiration_time: Timestamp,
23    pub(crate) receipt_level: ReceiptLevel,
24}
25
26impl ReceiptStruct {
27    pub fn new(
28        receipt_serial_bytes: ReceiptSerialBytes,
29        receipt_expiration_time: Timestamp,
30        receipt_level: ReceiptLevel,
31    ) -> Self {
32        Self {
33            receipt_serial_bytes,
34            receipt_expiration_time,
35            receipt_level,
36        }
37    }
38
39    pub fn calc_m1(&self) -> Scalar {
40        Self::calc_m1_from(self.receipt_expiration_time, self.receipt_level)
41    }
42
43    pub fn calc_m1_from(receipt_expiration_time: Timestamp, receipt_level: ReceiptLevel) -> Scalar {
44        let mut bytes =
45            [0u8; std::mem::size_of::<Timestamp>() + std::mem::size_of::<ReceiptLevel>()];
46        bytes[..std::mem::size_of::<Timestamp>()]
47            .copy_from_slice(&receipt_expiration_time.to_be_bytes());
48        bytes[std::mem::size_of::<Timestamp>()..].copy_from_slice(&receipt_level.to_be_bytes());
49        let mut sho = Sho::new(b"Signal_ZKGroup_20210919_Receipt_CalcM1", &bytes);
50        sho.get_scalar()
51    }
52}