zkgroup/crypto/
profile_key_commitment.rs

1//
2// Copyright 2020 Signal Messenger, LLC.
3// SPDX-License-Identifier: AGPL-3.0-only
4//
5
6#![allow(non_snake_case)]
7
8use curve25519_dalek_signal::ristretto::RistrettoPoint;
9use curve25519_dalek_signal::scalar::Scalar;
10use lazy_static::lazy_static;
11use partial_default::PartialDefault;
12use serde::{Deserialize, Serialize};
13
14use crate::common::constants::*;
15use crate::common::sho::*;
16use crate::common::simple_types::*;
17use crate::crypto::profile_key_struct;
18
19lazy_static! {
20    static ref SYSTEM_PARAMS: SystemParams =
21        crate::deserialize::<SystemParams>(&SystemParams::SYSTEM_HARDCODED).unwrap();
22}
23
24#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, PartialDefault)]
25pub struct SystemParams {
26    pub(crate) G_j1: RistrettoPoint,
27    pub(crate) G_j2: RistrettoPoint,
28    pub(crate) G_j3: RistrettoPoint,
29}
30
31#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
32pub struct CommitmentWithSecretNonce {
33    pub(crate) J1: RistrettoPoint,
34    pub(crate) J2: RistrettoPoint,
35    pub(crate) J3: RistrettoPoint,
36    pub(crate) j3: Scalar,
37}
38
39#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, PartialDefault)]
40pub struct Commitment {
41    pub(crate) J1: RistrettoPoint,
42    pub(crate) J2: RistrettoPoint,
43    pub(crate) J3: RistrettoPoint,
44}
45
46impl SystemParams {
47    pub fn generate() -> Self {
48        let mut sho = Sho::new(
49            b"Signal_ZKGroup_20200424_Constant_ProfileKeyCommitment_SystemParams_Generate",
50            b"",
51        );
52        let G_j1 = sho.get_point();
53        let G_j2 = sho.get_point();
54        let G_j3 = sho.get_point();
55        SystemParams { G_j1, G_j2, G_j3 }
56    }
57
58    pub fn get_hardcoded() -> SystemParams {
59        *SYSTEM_PARAMS
60    }
61
62    const SYSTEM_HARDCODED: [u8; 96] = [
63        0xa8, 0xca, 0xb, 0xbd, 0x11, 0x48, 0xc4, 0x66, 0x72, 0x58, 0x60, 0x64, 0xa, 0xc5, 0x3d,
64        0x27, 0x72, 0xb1, 0x4e, 0xea, 0xe0, 0x17, 0xa, 0x38, 0xc6, 0x2c, 0x7b, 0x3d, 0xd2, 0x9c,
65        0x3e, 0x4a, 0x14, 0xb9, 0x46, 0x2d, 0x94, 0x8f, 0x5, 0x94, 0x50, 0x79, 0x9f, 0x4c, 0xc2,
66        0xa0, 0x6e, 0x55, 0xde, 0xc8, 0x7, 0x73, 0x56, 0x70, 0xb9, 0x4a, 0x5c, 0xe8, 0xf, 0x59,
67        0xf1, 0x95, 0x8, 0x61, 0xb0, 0xc0, 0xf7, 0xb9, 0x1f, 0x6e, 0xf9, 0xc7, 0x55, 0x60, 0x93,
68        0xd8, 0x93, 0xa, 0x86, 0xbd, 0x36, 0x18, 0x8c, 0xec, 0x74, 0x5, 0x54, 0x65, 0x7d, 0x92,
69        0xdc, 0xd8, 0x6a, 0xad, 0x25, 0x1c,
70    ];
71}
72
73impl CommitmentWithSecretNonce {
74    pub fn new(
75        profile_key: profile_key_struct::ProfileKeyStruct,
76        uid_bytes: UidBytes,
77    ) -> CommitmentWithSecretNonce {
78        let commitment_system = SystemParams::get_hardcoded();
79
80        let profile_key_struct::ProfileKeyStruct { M3, M4, .. } = profile_key;
81        let j3 = Self::calc_j3(profile_key.bytes, uid_bytes);
82        let J1 = (j3 * commitment_system.G_j1) + M3;
83        let J2 = (j3 * commitment_system.G_j2) + M4;
84        let J3 = j3 * commitment_system.G_j3;
85        CommitmentWithSecretNonce { J1, J2, J3, j3 }
86    }
87
88    pub fn get_profile_key_commitment(&self) -> Commitment {
89        Commitment {
90            J1: self.J1,
91            J2: self.J2,
92            J3: self.J3,
93        }
94    }
95
96    pub fn calc_j3(profile_key_bytes: ProfileKeyBytes, uid_bytes: UidBytes) -> Scalar {
97        let mut combined_array = [0u8; PROFILE_KEY_LEN + UUID_LEN];
98        combined_array[..PROFILE_KEY_LEN].copy_from_slice(&profile_key_bytes);
99        combined_array[PROFILE_KEY_LEN..].copy_from_slice(&uid_bytes);
100        Sho::new(
101            b"Signal_ZKGroup_20200424_ProfileKeyAndUid_ProfileKeyCommitment_Calcj3",
102            &combined_array,
103        )
104        .get_scalar()
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn test_system() {
114        let params = SystemParams::generate();
115        println!("PARAMS = {:#x?}", bincode::serialize(&params));
116        assert!(SystemParams::generate() == SystemParams::get_hardcoded());
117    }
118
119    #[test]
120    fn test_commitment() {
121        let profile_key = profile_key_struct::ProfileKeyStruct::new(TEST_ARRAY_32, TEST_ARRAY_16);
122        let c1 = CommitmentWithSecretNonce::new(profile_key, TEST_ARRAY_16);
123        let c2 = CommitmentWithSecretNonce::new(profile_key, TEST_ARRAY_16);
124        assert!(c1 == c2);
125    }
126}