Skip to content

Commit

Permalink
docs: update comments and params name
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Rosenkranz-Costa committed Dec 6, 2023
1 parent a5cad94 commit b6e953a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 62 deletions.
1 change: 1 addition & 0 deletions src/abe_policy/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl BitOr for EncryptionHint {
}

impl EncryptionHint {
#[must_use]
pub fn new(is_hybridized: bool) -> Self {
if is_hybridized {
Self::Hybridized
Expand Down
27 changes: 12 additions & 15 deletions src/abe_policy/dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,26 @@ impl AttributeParameters {
}
}

#[must_use]
pub fn get_id(&self) -> u32 {
self.id
}

#[must_use]
pub fn get_encryption_hint(&self) -> EncryptionHint {
self.encryption_hint
}

#[must_use]
pub fn get_status(&self) -> AttributeStatus {
self.write_status
}

/// Returns a tuple containing the attribute id, the associated encryption
/*// Returns a tuple containing the attribute id, the associated encryption
/// hint, and the `read_only` flag.
pub fn get_attribute_properties(&self) -> (u32, EncryptionHint, AttributeStatus) {
(self.id, self.encryption_hint, self.write_status)
}
}*/
}

type AttributeName = String;
Expand Down Expand Up @@ -138,13 +141,15 @@ impl Dimension {
}
}

#[must_use]
pub fn nb_attributes(&self) -> usize {
match self {
Self::Unordered(attributes) => attributes.len(),
Self::Ordered(attributes) => attributes.len(),
}
}

#[must_use]
pub fn is_ordered(&self) -> bool {
match self {
Self::Unordered(_) => false,
Expand All @@ -155,13 +160,15 @@ impl Dimension {
/// Returns an iterator over the attributes name.
/// If the dimension is ordered, the names are returned in this order,
/// otherwise they are returned in arbitrary order.
#[must_use]
pub fn get_attributes_name(&self) -> Box<dyn '_ + Iterator<Item = &AttributeName>> {
match self {
Self::Unordered(attributes) => Box::new(attributes.keys()),
Self::Ordered(attributes) => Box::new(attributes.keys()),
}
}

#[must_use]
pub fn get_attribute(&self, attr_name: &AttributeName) -> Option<&AttributeParameters> {
match self {
Self::Unordered(attributes) => attributes.get(attr_name),
Expand Down Expand Up @@ -286,23 +293,13 @@ impl Dimension {
}
}

/// Returns an iterator over the AttributesParameters and parameters.
/// Returns an iterator over the `AttributesParameters` and parameters.
/// If the dimension is ordered, the attributes are returned in order.
pub fn attributes_properties(&self) -> Box<dyn '_ + Iterator<Item = &AttributeParameters>> {
#[must_use]
pub fn iter_attributes(&self) -> Box<dyn '_ + Iterator<Item = &AttributeParameters>> {
match self {
Self::Unordered(attributes) => Box::new(attributes.values()),
Self::Ordered(attributes) => Box::new(attributes.values()),
}
}

/// Returns an iterator over the Attributes names and parameters.
/// If the dimension is ordered, the attributes are returned in order.
pub fn iter_attributes(
&self,
) -> Box<dyn '_ + Iterator<Item = (&AttributeName, &AttributeParameters)>> {
match self {
Self::Unordered(attributes) => Box::new(attributes.iter()),
Self::Ordered(attributes) => Box::new(attributes.iter()),
}
}
}
65 changes: 25 additions & 40 deletions src/abe_policy/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Policy {
/// Adds the given attribute to the policy.
/// /!\ No old key will be able to use this attribute. In particular, keys
/// which associated access policy was implicitly deriving rights for this
/// dimension (e.g. "Security::High" implicitly derives rights for all
/// dimension (e.g. "`Security::High`" implicitly derives rights for all
/// attributes from any other dimensions) need to be regenerated. A refresh
/// will *not* implicitly derive rights for this attribute. Fails if the
/// dim of the attribute does not exist in the policy.
Expand Down Expand Up @@ -186,7 +186,7 @@ impl Policy {
fn combine_attributes(
current_dim: usize,
dimensions: &[String],
attr_params_per_dim: &HashMap<String, Vec<(u32, EncryptionHint, AttributeStatus)>>,
attr_params_per_dim: &HashMap<String, Vec<&AttributeParameters>>,
) -> Result<Vec<(Vec<u32>, EncryptionHint, AttributeStatus)>, Error> {
let current_dim_name = match dimensions.get(current_dim) {
None => {
Expand All @@ -208,15 +208,15 @@ impl Policy {
Self::combine_attributes(current_dim + 1, dimensions, attr_params_per_dim)?;

let mut combinations = Vec::with_capacity(current_dim_values.len() * other_values.len());
for (current_values, is_hybridized, is_readonly) in current_dim_values {
for attr in current_dim_values {
for (other_values, is_other_hybridized, is_other_readonly) in &other_values {
let mut combined = Vec::with_capacity(1 + other_values.len());
combined.push(*current_values);
combined.push(attr.get_id());
combined.extend_from_slice(other_values);
combinations.push((
combined,
*is_hybridized | *is_other_hybridized,
*is_readonly | *is_other_readonly,
attr.get_encryption_hint() | *is_other_hybridized,
attr.get_status() | *is_other_readonly,
));
}
}
Expand All @@ -231,12 +231,7 @@ impl Policy {
) -> Result<HashMap<Partition, (EncryptionHint, AttributeStatus)>, Error> {
let mut attr_params_per_dim = HashMap::with_capacity(self.dimensions.len());
for (dim_name, dim) in &self.dimensions {
attr_params_per_dim.insert(
dim_name.clone(),
dim.attributes_properties()
.map(|attr| attr.get_attribute_properties())
.collect(),
);
attr_params_per_dim.insert(dim_name.clone(), dim.iter_attributes().collect());
}

// Combine axes values into partitions.
Expand All @@ -252,24 +247,20 @@ impl Policy {
Ok(res)
}

/// Generates an `AccessPolicy` into the list of corresponding current
/// partitions.
/// Converts an `AccessPolicy` into a list of corresponding coordinates.
///
/// - `access_policy` : access policy to convert
/// - `follow_hierarchical_dim` : set to `true` to combine lower dim
/// attributes
/// - `access_policy` : access policy to convert
/// - `cascade_rights` : include lower rights from hierarchical dimensions
pub fn access_policy_to_partitions(
&self,
access_policy: &AccessPolicy,
follow_hierarchical_dim: bool,
cascade_rights: bool,
) -> Result<HashSet<Partition>, Error> {
let attr_combinations =
access_policy.to_attribute_combinations(self, follow_hierarchical_dim)?;
let attr_combinations = access_policy.to_attribute_combinations(self, cascade_rights)?;
let mut res = HashSet::with_capacity(attr_combinations.len());
for attr_combination in &attr_combinations {
for partition in generate_attribute_partitions(attr_combination, self)? {
let is_unique = res.insert(partition);
if !is_unique {
if !res.insert(partition) {
return Err(Error::ExistingCombination(format!("{attr_combination:?}")));
}
}
Expand Down Expand Up @@ -304,38 +295,32 @@ fn generate_attribute_partitions(
policy: &Policy,
) -> Result<HashSet<Partition>, Error> {
let mut attr_params_per_dim =
HashMap::<String, Vec<(u32, EncryptionHint, AttributeStatus)>>::with_capacity(
policy.dimensions.len(),
);
for attribute in attributes.iter() {
HashMap::<String, Vec<&AttributeParameters>>::with_capacity(policy.dimensions.len());
for attribute in attributes {
let entry = attr_params_per_dim
.entry(attribute.dimension.clone())
.or_default();
let attr_properties = policy.get_attribute(attribute)?;
entry.push(attr_properties.get_attribute_properties());
entry.push(policy.get_attribute(attribute)?);
}

// When a dimension is not mentioned in the attribute list, all the attribute
// from this dimension are used.
for (dim, dim_properties) in &policy.dimensions {
if !attr_params_per_dim.contains_key(dim) {
// gather all the latest value for that dim
let values = dim_properties
.attributes_properties()
.map(AttributeParameters::get_attribute_properties)
.collect();
let values = dim_properties.iter_attributes().collect();
attr_params_per_dim.insert(dim.clone(), values);
}
}

// Combine axes values into partitions.
let axes = attr_params_per_dim.keys().cloned().collect::<Vec<_>>();
let combinations = Policy::combine_attributes(0, axes.as_slice(), &attr_params_per_dim)?;
let mut res = HashSet::with_capacity(combinations.len());
for (combination, _, _) in combinations {
res.insert(Partition::from_attribute_ids(combination)?);
}
Ok(res)
// Combine dimensions attributes into partitions.
let dimensions = attr_params_per_dim.keys().cloned().collect::<Vec<_>>();
let combinations = Policy::combine_attributes(0, dimensions.as_slice(), &attr_params_per_dim)?;

combinations
.into_iter()
.map(|(coordinate, _, _)| Partition::from_attribute_ids(coordinate))
.collect::<Result<HashSet<_>, _>>()
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions src/core/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,12 @@ pub fn update(
match (write_status, mpk.subkeys.get_mut(partition)) {
(EncryptDecrypt, None) => unreachable!(),
(EncryptDecrypt, Some(public_subkey)) => {
update_subkey_pair(rng, &h, public_subkey, secret_subkey, is_hybridized)?
update_subkey_pair(rng, &h, public_subkey, secret_subkey, is_hybridized)?;
}
(DecryptOnly, None) => update_master_subkey(rng, &h, secret_subkey, is_hybridized),
(DecryptOnly, Some(_)) => {
mpk.subkeys.remove(partition);
update_master_subkey(rng, &h, secret_subkey, is_hybridized)
update_master_subkey(rng, &h, secret_subkey, is_hybridized);
}
}
} else {
Expand Down
8 changes: 6 additions & 2 deletions src/data_struct/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serde::{
use super::error::Error;

type Index = usize;
/// HashMap keeping insertion order inspired by Python dictionary.
/// `HashMap` keeping insertion order inspired by Python dictionary.
#[derive(Default, Clone, Eq, PartialEq, Debug)]
pub struct Dict<K, V>
where
Expand All @@ -30,24 +30,28 @@ impl<K, V> Dict<K, V>
where
K: Hash + PartialEq + Eq + Clone + Debug,
{
#[must_use]
pub fn new() -> Self {
Self {
indices: HashMap::new(),
entries: Vec::new(),
}
}

#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self {
indices: HashMap::with_capacity(capacity),
entries: Vec::with_capacity(capacity),
}
}

#[must_use]
pub fn len(&self) -> usize {
self.indices.len()
}

#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
Expand All @@ -74,7 +78,7 @@ where
/// Removes the entry corresponding to the given key.
/// To maintain order, all inserted entries after the removed one will be
/// shifted by one and the indices map will be updated accordingly.
/// Compared to a regular HashMap, this operation is O(n).
/// Compared to a regular `HashMap`, this operation is O(n).
pub fn remove(&mut self, key: &K) -> Option<V> {
let entry_index = self.indices.remove(key)?;

Expand Down
6 changes: 3 additions & 3 deletions src/data_struct/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ impl Error {
where
T: Debug,
{
Self::EntryNotFound(format!("{:?}", key))
Self::EntryNotFound(format!("{key:?}"))
}

pub fn existing_entry<T>(key: &T) -> Self
where
T: Debug,
{
Self::ExistingEntry(format!("{:?}", key))
Self::ExistingEntry(format!("{key:?}"))
}

pub fn already_has_child<T>(key: &T) -> Self
where
T: Debug,
{
Self::AlreadyHasChild(format!("{:?}", key))
Self::AlreadyHasChild(format!("{key:?}"))
}
}
4 changes: 4 additions & 0 deletions src/data_struct/revision_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ where
K: Hash + PartialEq + Eq + Clone + Debug,
V: Clone + Debug,
{
#[must_use]
pub fn new() -> Self {
Self {
map: HashMap::new(),
}
}

#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self {
map: HashMap::with_capacity(capacity),
}
}

/// Returns the number of chains stored.
#[must_use]
pub fn len(&self) -> usize {
self.map.len()
}
Expand All @@ -53,6 +56,7 @@ where
self.map.values().map(LinkedList::len).sum()
}

#[must_use]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
Expand Down
Loading

0 comments on commit b6e953a

Please sign in to comment.