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
//! Group API for MLS
//!
//! This module contains the API to interact with groups.

mod group_context;

use std::fmt::Display;

#[cfg(test)]
use crate::ciphersuite::*;
use crate::extensions::*;
#[cfg(test)]
use crate::utils::*;

use serde::{Deserialize, Serialize};
use tls_codec::*;

// Crate
pub(crate) mod core_group;
pub(crate) mod public_group;
pub(crate) use core_group::*;
pub(crate) mod errors;
pub(crate) mod mls_group;

// Public
pub use core_group::proposals::*;
pub use core_group::staged_commit::StagedCommit;
pub use errors::*;
pub use group_context::GroupContext;
pub use mls_group::config::*;
pub use mls_group::membership::*;
pub use mls_group::*;
pub use public_group::*;

// Tests
#[cfg(test)]
pub(crate) use core_group::create_commit_params::*;
#[cfg(any(feature = "test-utils", test))]
pub(crate) mod tests;
use openmls_traits::random::OpenMlsRand;

/// A group ID. The group ID is chosen by the creator of the group and should be globally unique.
#[derive(
    Clone,
    Debug,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Deserialize,
    Serialize,
    TlsDeserialize,
    TlsDeserializeBytes,
    TlsSerialize,
    TlsSize,
)]
pub struct GroupId {
    value: VLBytes,
}

impl GroupId {
    /// Create a new (random) group ID.
    ///
    /// Group IDs should be random and not be misused as, e.g., a group name.
    pub fn random(rng: &impl OpenMlsRand) -> Self {
        Self {
            value: rng.random_vec(16).expect("Not enough randomness.").into(),
        }
    }

    /// Create a group ID from a byte slice.
    ///
    /// This should be used only if the group ID is chosen by an entity that ensures uniqueness.
    pub fn from_slice(bytes: &[u8]) -> Self {
        GroupId {
            value: bytes.into(),
        }
    }

    /// Returns the group ID as a byte slice.
    pub fn as_slice(&self) -> &[u8] {
        self.value.as_slice()
    }

    /// Returns the group ID as a byte vector.
    pub fn to_vec(&self) -> Vec<u8> {
        self.value.clone().into()
    }
}

/// Group epoch. Internally this is stored as a `u64`.
/// The group epoch is incremented with every valid Commit that is merged into the group state.
#[derive(
    Clone,
    Copy,
    Debug,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Deserialize,
    Serialize,
    TlsDeserialize,
    TlsDeserializeBytes,
    TlsSerialize,
    TlsSize,
)]
pub struct GroupEpoch(u64);

impl GroupEpoch {
    /// Increment the group epoch by 1.
    pub(crate) fn increment(&mut self) {
        self.0 += 1;
    }

    /// Returns the group epoch as a `u64`.
    pub fn as_u64(&self) -> u64 {
        self.0
    }
}

impl From<u64> for GroupEpoch {
    fn from(val: u64) -> Self {
        Self(val)
    }
}

impl Display for GroupEpoch {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("{}", self.0))
    }
}