use crate::{
collections::Set,
error::CgkaError,
id::{MemberId, TreeId},
keys::{NodeKey, ShareKeyMap},
pcs_key::PcsKey,
secret_store::SecretStore,
treemath,
};
use alloc::{collections::BTreeMap, vec, vec::Vec};
use keyhive_crypto::share_key::{ShareKey, ShareSecretKey};
use serde::{Deserialize, Serialize};
use tracing::instrument;
use treemath::{InnerNodeIndex, LeafNodeIndex, TreeNodeIndex, TreeSize};
pub type InnerNode = SecretStore;
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
pub struct PathChange {
pub leaf_id: MemberId,
pub leaf_idx: u32,
pub leaf_pk: NodeKey,
pub path: Vec<(u32, InnerNode)>,
pub removed_keys: Vec<ShareKey>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Hash)]
pub struct BeeKem {
doc_id: TreeId,
next_leaf_idx: LeafNodeIndex,
leaves: Vec<Option<LeafNode>>,
inner_nodes: Vec<Option<InnerNode>>,
tree_size: TreeSize,
id_to_leaf_idx: BTreeMap<MemberId, LeafNodeIndex>,
current_secret_encrypter_leaf_idx: Option<LeafNodeIndex>,
}
impl BeeKem {
pub fn new(
doc_id: TreeId,
initial_member_id: MemberId,
initial_member_pk: ShareKey,
) -> Result<Self, CgkaError> {
let mut tree = Self {
doc_id,
next_leaf_idx: LeafNodeIndex::new(0),
leaves: Vec::new(),
inner_nodes: Vec::new(),
tree_size: TreeSize::from_leaf_count(1),
id_to_leaf_idx: BTreeMap::new(),
current_secret_encrypter_leaf_idx: None,
};
tree.grow_tree_to_size();
tree.push_leaf(initial_member_id, initial_member_pk.into());
Ok(tree)
}
pub fn contains_id(&self, id: &MemberId) -> bool {
self.id_to_leaf_idx.contains_key(id)
}
pub fn node_key_for_id(&self, id: MemberId) -> Result<NodeKey, CgkaError> {
let idx = self.leaf_index_for_id(id)?;
self.node_key_for_index((*idx).into())
}
pub fn sort_leaves_and_blank_paths_for_concurrent_membership_changes(
&mut self,
mut added_ids: Set<MemberId>,
removed_ids: Set<(MemberId, u32)>,
) {
let mut leaves_to_sort = Vec::new();
for (id, idx) in removed_ids {
added_ids.remove(&id);
let leaf_idx = LeafNodeIndex::new(idx);
debug_assert!(self.leaf(leaf_idx).is_none());
self.blank_leaf_and_path(leaf_idx);
}
while !added_ids.is_empty() && self.next_leaf_idx.u32() > 0 {
let leaf_idx = self.next_leaf_idx - 1;
if let Some(next_leaf) = self.leaf(leaf_idx).clone() {
added_ids.remove(&next_leaf.id);
leaves_to_sort.push(next_leaf);
}
self.blank_leaf_and_path(leaf_idx);
self.next_leaf_idx = leaf_idx;
}
leaves_to_sort.sort_by_key(|a| a.id);
for leaf in leaves_to_sort {
self.push_leaf(leaf.id, leaf.pk.clone());
}
}
pub fn blank_leaf_and_path(&mut self, idx: LeafNodeIndex) {
self.leaves[idx.usize()] = None;
self.blank_path(treemath::parent(idx.into()));
}
pub fn push_leaf(&mut self, id: MemberId, pk: NodeKey) -> u32 {
self.maybe_grow_tree(self.next_leaf_idx.u32());
let l_idx = self.next_leaf_idx;
self.next_leaf_idx += 1;
self.insert_leaf_at(l_idx, id, pk);
self.id_to_leaf_idx.insert(id, l_idx);
self.blank_path(treemath::parent(l_idx.into()));
l_idx.u32()
}
pub fn remove_id(&mut self, id: MemberId) -> Result<(u32, Vec<ShareKey>), CgkaError> {
if self.member_count() == 1 {
return Err(CgkaError::RemoveLastMember);
}
let l_idx = *self.leaf_index_for_id(id)?;
let mut removed_keys = Vec::new();
for idx in treemath::direct_path((l_idx).into(), self.tree_size) {
if let Some(store) = self.inner_node(idx) {
removed_keys.append(&mut store.node_key().keys());
}
}
self.blank_leaf_and_path(l_idx);
self.id_to_leaf_idx.remove(&id);
while self.leaf(self.next_leaf_idx - 1).is_none() {
self.blank_path(treemath::parent((self.next_leaf_idx - 1).into()));
self.next_leaf_idx -= 1;
}
Ok((l_idx.u32(), removed_keys))
}
pub fn member_count(&self) -> u32 {
self.id_to_leaf_idx.len() as u32
}
#[instrument(skip_all, fields(doc_id, epochs))]
pub fn decrypt_tree_secret(
&self,
owner_id: MemberId,
owner_sks: &mut ShareKeyMap,
) -> Result<ShareSecretKey, CgkaError> {
let leaf_idx = match self.leaf_index_for_id(owner_id) {
Ok(idx) => *idx,
Err(CgkaError::IdentifierNotFound) => *self.leaf_index_for_id(MemberId::public())?,
Err(e) => return Err(e),
};
if !self.has_root_key() {
return Err(CgkaError::NoRootKey);
}
let leaf = self
.leaf(leaf_idx)
.as_ref()
.expect("Leaf should not be blank");
if Some(leaf_idx) == self.current_secret_encrypter_leaf_idx {
let NodeKey::ShareKey(pk) = leaf.pk else {
return Err(CgkaError::ShareKeyNotFound);
};
let secret = owner_sks.get(&pk).ok_or(CgkaError::ShareKeyNotFound)?;
return Ok(secret
.ratchet_n_forward(treemath::direct_path(leaf_idx.into(), self.tree_size).len()));
}
let lca_with_encrypter = treemath::lowest_common_ancestor(
leaf_idx,
self.current_secret_encrypter_leaf_idx
.expect("A tree with a root key should have a current encrypter"),
);
let mut child_idx: TreeNodeIndex = leaf_idx.into();
let mut seen_idxs = vec![child_idx];
let mut maybe_last_secret_decrypted = None;
let mut child_node_key = leaf.pk.clone();
let mut parent_idx: TreeNodeIndex = treemath::parent(child_idx).into();
while !self.is_root(child_idx) {
while self.should_skip_for_resolution(parent_idx) {
child_idx = parent_idx;
parent_idx = treemath::parent(child_idx).into();
}
debug_assert!(!self.is_root(child_idx));
maybe_last_secret_decrypted =
self.maybe_decrypt_parent_key(child_idx, &child_node_key, &seen_idxs, owner_sks)?;
let Some(ref secret) = maybe_last_secret_decrypted else {
panic!("Non-blank, non-conflict parent should have a secret we can decrypt");
};
if parent_idx == TreeNodeIndex::Inner(lca_with_encrypter) {
return Ok(secret
.ratchet_n_forward(treemath::direct_path(parent_idx, self.tree_size).len()));
}
seen_idxs.push(parent_idx);
child_idx = parent_idx;
child_node_key = self.node_key_for_index(child_idx)?;
parent_idx = treemath::parent(child_idx).into();
}
maybe_last_secret_decrypted.ok_or(CgkaError::NoRootKey)
}
pub fn encrypt_path<R: rand::CryptoRng + rand::RngCore>(
&mut self,
id: MemberId,
pk: ShareKey,
sks: &mut ShareKeyMap,
csprng: &mut R,
) -> Result<Option<(PcsKey, PathChange)>, CgkaError> {
let leaf_idx = *self.leaf_index_for_id(id)?;
debug_assert!(self.id_for_leaf(leaf_idx).unwrap() == id);
let mut new_path = PathChange {
leaf_id: id,
leaf_idx: leaf_idx.u32(),
leaf_pk: NodeKey::ShareKey(pk),
path: Vec::new(),
removed_keys: self.node_key_for_id(id)?.keys(),
};
self.insert_leaf_at(leaf_idx, id, NodeKey::ShareKey(pk));
let mut child_idx: TreeNodeIndex = leaf_idx.into();
let mut child_pk = pk;
let mut child_sk = *sks.get(&pk).ok_or(CgkaError::SecretKeyNotFound)?;
let mut parent_idx = treemath::parent(child_idx);
while !self.is_root(child_idx) {
if let Some(store) = self.inner_node(parent_idx) {
new_path.removed_keys.append(&mut store.node_key().keys());
}
let new_parent_sk = child_sk.ratchet_forward();
let new_parent_pk = new_parent_sk.share_key();
self.encrypt_key_for_parent(
child_idx,
child_pk,
&child_sk,
new_parent_pk,
&new_parent_sk,
csprng,
)?;
new_path.path.push((
parent_idx.u32(),
self.inner_node(parent_idx)
.as_ref()
.expect("Parent node should not be None after encryption")
.clone(),
));
child_idx = parent_idx.into();
child_pk = new_parent_pk;
child_sk = new_parent_sk;
parent_idx = treemath::parent(child_idx);
}
self.current_secret_encrypter_leaf_idx = Some(leaf_idx);
Ok(Some((child_sk.into(), new_path)))
}
pub fn apply_path(&mut self, new_path: &PathChange) {
if !self.id_to_leaf_idx.contains_key(&new_path.leaf_id) {
return;
}
let leaf_idx = *self
.leaf_index_for_id(new_path.leaf_id)
.expect("Id should be present");
if !self.is_valid_path(new_path) {
let Some(leaf) = self.leaf(leaf_idx) else {
panic!("Leaf for present ID should not be None");
};
let new_node_key = leaf.pk.merge(&new_path.leaf_pk, &new_path.removed_keys);
self.insert_leaf_at(leaf_idx, new_path.leaf_id, new_node_key);
self.blank_path(treemath::parent(leaf_idx.into()));
return;
}
let old_leaf = self.leaf(leaf_idx).as_ref().unwrap();
let new_leaf_pk = new_path.leaf_pk.clone();
self.insert_leaf_at(
leaf_idx,
new_path.leaf_id,
old_leaf.pk.merge(&new_leaf_pk, &new_path.removed_keys),
);
let removed_keys_set: Set<ShareKey> = Set::from_iter(new_path.removed_keys.iter().copied());
for (idx, node) in &new_path.path {
let current_idx = InnerNodeIndex::new(*idx);
if let Some(current_node) = self.inner_node_mut(current_idx) {
current_node.merge(node, &removed_keys_set);
} else {
self.insert_inner_node_at(current_idx, node.clone());
}
}
if self.has_root_key() {
self.current_secret_encrypter_leaf_idx = Some(leaf_idx);
} else {
self.current_secret_encrypter_leaf_idx = None;
}
}
pub fn has_root_key(&self) -> bool {
let root_idx: TreeNodeIndex = treemath::root(self.tree_size);
let TreeNodeIndex::Inner(p_idx) = root_idx else {
panic!("BeeKEM should always have a root at an inner node.")
};
if let Some(r) = self.inner_node(p_idx) {
!r.has_conflict()
} else {
false
}
}
fn maybe_decrypt_parent_key(
&self,
child_idx: TreeNodeIndex,
child_node_key: &NodeKey,
seen_idxs: &[TreeNodeIndex],
child_sks: &mut ShareKeyMap,
) -> Result<Option<ShareSecretKey>, CgkaError> {
debug_assert!(!self.is_root(child_idx));
let parent_idx = treemath::parent(child_idx);
let Some(parent) = self.inner_node(parent_idx) else {
return Ok(None);
};
let maybe_secret = match parent.node_key() {
NodeKey::ConflictKeys(_) => None,
NodeKey::ShareKey(parent_pk) => {
if child_sks.contains_key(&parent_pk) {
return Ok(child_sks.get(&parent_pk).cloned());
}
let secret = parent.decrypt_secret(child_node_key, child_sks, seen_idxs)?;
child_sks.insert(parent_pk, secret);
Some(secret)
}
};
Ok(maybe_secret)
}
fn encrypt_key_for_parent<R: rand::CryptoRng + rand::RngCore>(
&mut self,
child_idx: TreeNodeIndex,
child_pk: ShareKey,
child_sk: &ShareSecretKey,
new_parent_pk: ShareKey,
new_parent_sk: &ShareSecretKey,
csprng: &mut R,
) -> Result<(), CgkaError> {
debug_assert!(!self.is_root(child_idx));
let parent_idx = treemath::parent(child_idx);
let secret_store = self.encrypt_new_secret_store_for_parent(
child_idx,
child_pk,
child_sk,
new_parent_pk,
new_parent_sk,
csprng,
)?;
self.insert_inner_node_at(parent_idx, secret_store);
Ok(())
}
#[allow(clippy::type_complexity)]
fn encrypt_new_secret_store_for_parent<R: rand::CryptoRng + rand::RngCore>(
&self,
child_idx: TreeNodeIndex,
child_pk: ShareKey,
child_sk: &ShareSecretKey,
new_parent_pk: ShareKey,
new_parent_sk: &ShareSecretKey,
csprng: &mut R,
) -> Result<SecretStore, CgkaError> {
debug_assert!(!self.is_root(child_idx));
let sibling_idx = treemath::sibling(child_idx);
let mut secret_map = BTreeMap::new();
let mut sibling_resolution = Vec::new();
self.append_resolution(sibling_idx, &mut sibling_resolution);
if sibling_resolution.is_empty() {
let paired_sk = ShareSecretKey::generate(csprng);
let paired_pk = paired_sk.share_key();
let encrypted_sk = crate::encrypted::encrypt_secret(
self.doc_id.as_bytes(),
*new_parent_sk,
child_sk,
&paired_pk,
)?;
secret_map.insert(child_idx, encrypted_sk);
} else {
let mut used_paired_sibling = false;
for idx in sibling_resolution {
let next_pk = match self.node_key_for_index(idx)? {
NodeKey::ShareKey(share_key) => share_key,
_ => panic!("Sibling resolution nodes should have exactly one ShareKey"),
};
let encrypted_sk = crate::encrypted::encrypt_secret(
self.doc_id.as_bytes(),
*new_parent_sk,
child_sk,
&next_pk,
)?;
if !used_paired_sibling {
secret_map.insert(child_idx, encrypted_sk.clone());
used_paired_sibling = true;
}
secret_map.insert(idx, encrypted_sk);
}
};
Ok(SecretStore::new(new_parent_pk, child_pk, secret_map))
}
fn node_key_for_index(&self, idx: TreeNodeIndex) -> Result<NodeKey, CgkaError> {
Ok(match idx {
TreeNodeIndex::Leaf(l_idx) => self
.leaf(l_idx)
.as_ref()
.ok_or(CgkaError::ShareKeyNotFound)?
.pk
.clone(),
TreeNodeIndex::Inner(i_idx) => self
.inner_node(i_idx)
.as_ref()
.ok_or(CgkaError::ShareKeyNotFound)?
.node_key(),
})
}
fn leaf(&self, idx: LeafNodeIndex) -> &Option<LeafNode> {
self.leaves
.get(idx.usize())
.expect("Leaf index should be in bounds")
}
pub fn leaf_index_for_id(&self, id: MemberId) -> Result<&LeafNodeIndex, CgkaError> {
self.id_to_leaf_idx
.get(&id)
.ok_or(CgkaError::IdentifierNotFound)
}
fn id_for_leaf(&self, idx: LeafNodeIndex) -> Result<MemberId, CgkaError> {
Ok(self
.leaf(idx)
.as_ref()
.ok_or(CgkaError::IdentifierNotFound)?
.id)
}
fn inner_node(&self, idx: InnerNodeIndex) -> &Option<InnerNode> {
self.inner_nodes
.get(idx.usize())
.expect("Inner node index should be in bounds")
}
fn inner_node_mut(&mut self, idx: InnerNodeIndex) -> &mut Option<InnerNode> {
self.inner_nodes
.get_mut(idx.usize())
.expect("Node should not be blank")
}
fn insert_leaf_at(&mut self, idx: LeafNodeIndex, id: MemberId, pk: NodeKey) {
let leaf = LeafNode { id, pk };
self.leaves[idx.usize()] = Some(leaf);
}
fn insert_inner_node_at(&mut self, idx: InnerNodeIndex, secret_store: SecretStore) {
self.inner_nodes[idx.usize()] = Some(secret_store);
}
fn is_blank(&self, idx: TreeNodeIndex) -> bool {
match idx {
TreeNodeIndex::Leaf(l_idx) => self.leaf(l_idx).is_none(),
TreeNodeIndex::Inner(p_idx) => self.inner_node(p_idx).is_none(),
}
}
fn should_skip_for_resolution(&self, idx: TreeNodeIndex) -> bool {
match idx {
TreeNodeIndex::Leaf(_) => self.is_blank(idx),
TreeNodeIndex::Inner(i_idx) => self
.inner_node(i_idx)
.as_ref()
.is_none_or(|n| n.has_conflict()),
}
}
fn blank_path(&mut self, mut idx: InnerNodeIndex) {
while !self.is_root(idx.into()) {
self.blank_inner_node(idx);
idx = treemath::parent(idx.into());
}
self.blank_inner_node(idx);
self.current_secret_encrypter_leaf_idx = None;
}
fn blank_inner_node(&mut self, idx: InnerNodeIndex) {
self.inner_nodes[idx.usize()] = None;
}
fn is_valid_path(&self, new_path: &PathChange) -> bool {
debug_assert!(self.id_to_leaf_idx.contains_key(&new_path.leaf_id));
let leaf_idx = self
.leaf_index_for_id(new_path.leaf_id)
.expect("Id should be present");
new_path.path.len() == self.path_length_for(LeafNodeIndex::new(new_path.leaf_idx))
&& leaf_idx.u32() == new_path.leaf_idx
}
fn maybe_grow_tree(&mut self, new_count: u32) {
if self.tree_size >= TreeSize::from_leaf_count(new_count) {
return;
}
self.tree_size.inc();
self.grow_tree_to_size();
}
fn grow_tree_to_size(&mut self) {
self.leaves
.resize(self.tree_size.leaf_count() as usize, None);
self.inner_nodes
.resize(self.tree_size.inner_node_count() as usize, None);
}
fn is_root(&self, idx: TreeNodeIndex) -> bool {
idx == treemath::root(self.tree_size)
}
fn path_length_for(&self, idx: LeafNodeIndex) -> usize {
treemath::direct_path(idx.into(), self.tree_size).len()
}
fn append_resolution(&self, idx: TreeNodeIndex, acc: &mut Vec<TreeNodeIndex>) {
if self.should_skip_for_resolution(idx) {
if let TreeNodeIndex::Inner(i_idx) = idx {
let left_idx = treemath::left(i_idx);
self.append_resolution(left_idx, acc);
let right_idx = treemath::right(i_idx);
self.append_resolution(right_idx, acc);
}
} else {
acc.push(idx);
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Hash)]
pub struct LeafNode {
pub id: MemberId,
pub pk: NodeKey,
}