use crate::error::{Error, Result};
const fn mask(bits: u32) -> u64 {
if bits >= 64 {
u64::MAX
} else {
(1u64 << bits) - 1
}
}
const fn shl(value: u64, bits: u32) -> u64 {
if bits >= 64 { 0 } else { value << bits }
}
const fn shr(value: u64, bits: u32) -> u64 {
if bits >= 64 { 0 } else { value >> bits }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SenderKeyIdLayout {
ratchet_bits: u32,
}
impl SenderKeyIdLayout {
pub fn new(ratchet_bits: u32) -> Result<Self> {
if ratchet_bits > 64 {
return Err(Error::KeyIdLayout);
}
Ok(Self { ratchet_bits })
}
#[must_use]
pub const fn ratchet_bits(&self) -> u32 {
self.ratchet_bits
}
pub fn key_id(&self, key_generation: u64, ratchet_step: u64) -> Result<u64> {
if key_generation > mask(64 - self.ratchet_bits) {
return Err(Error::KeyIdLayout);
}
Ok(self.compose(key_generation, ratchet_step))
}
pub(crate) const fn compose(&self, key_generation: u64, ratchet_step: u64) -> u64 {
shl(key_generation, self.ratchet_bits) | (ratchet_step & mask(self.ratchet_bits))
}
#[must_use]
pub const fn split(&self, kid: u64) -> SenderKeyId {
SenderKeyId {
key_generation: shr(kid, self.ratchet_bits),
ratchet_step: kid & mask(self.ratchet_bits),
}
}
#[must_use]
pub const fn steps_between(&self, from_step: u64, ratchet_step: u64) -> u64 {
ratchet_step.wrapping_sub(from_step) & mask(self.ratchet_bits)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SenderKeyId {
pub key_generation: u64,
pub ratchet_step: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MlsKeyIdLayout {
epoch_bits: u32,
sender_bits: u32,
}
impl MlsKeyIdLayout {
pub fn new(epoch_bits: u32, sender_bits: u32) -> Result<Self> {
if epoch_bits > 64 || sender_bits > 64 - epoch_bits {
return Err(Error::KeyIdLayout);
}
Ok(Self {
epoch_bits,
sender_bits,
})
}
pub fn for_group_size(epoch_bits: u32, group_size: u64) -> Result<Self> {
let sender_bits = match group_size {
0 | 1 => 0,
size => 64 - (size - 1).leading_zeros(),
};
Self::new(epoch_bits, sender_bits)
}
#[must_use]
pub const fn epoch_bits(&self) -> u32 {
self.epoch_bits
}
#[must_use]
pub const fn sender_bits(&self) -> u32 {
self.sender_bits
}
#[must_use]
pub const fn context_bits(&self) -> u32 {
64 - self.sender_bits - self.epoch_bits
}
#[must_use]
pub const fn epoch_slot(&self, epoch: u64) -> u64 {
epoch & mask(self.epoch_bits)
}
pub fn key_id(&self, epoch: u64, sender_index: u64, context: u64) -> Result<u64> {
if sender_index > mask(self.sender_bits) || context > mask(self.context_bits()) {
return Err(Error::KeyIdLayout);
}
Ok(shl(context, self.sender_bits + self.epoch_bits)
| shl(sender_index, self.epoch_bits)
| self.epoch_slot(epoch))
}
#[must_use]
pub const fn split(&self, kid: u64) -> MlsKeyId {
MlsKeyId {
context: shr(kid, self.sender_bits + self.epoch_bits),
sender_index: shr(kid, self.epoch_bits) & mask(self.sender_bits),
epoch: kid & mask(self.epoch_bits),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MlsKeyId {
pub context: u64,
pub sender_index: u64,
pub epoch: u64,
}