Skip to content

Commit

Permalink
refacto: rename Revision Map get_current_element to get_latest
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Rosenkranz-Costa committed Dec 21, 2023
1 parent 7755caf commit 6bde142
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
35 changes: 15 additions & 20 deletions src/core/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,9 @@ pub fn keygen(
// Use the last key for each partitions in the decryption set
let mut subkeys = RevisionVec::with_capacity(decryption_set.len());
decryption_set.iter().try_for_each(|partition| {
let subkey = msk
.subkeys
.get_current_revision(partition)
.ok_or(Error::KeyError(
"Master secret key and Policy are not in sync.".to_string(),
))?;
let subkey = msk.subkeys.get_latest(partition).ok_or(Error::KeyError(
"Master secret key and Policy are not in sync.".to_string(),
))?;
subkeys.create_chain_with_single_value(partition.clone(), subkey.clone());
Ok::<_, Error>(())
})?;
Expand Down Expand Up @@ -375,7 +372,7 @@ pub fn update(
let h = R25519PublicKey::from(&msk.s);
for (partition, &(is_hybridized, write_status)) in partitions_set {
// check if secret key exist for this partition
if let Some(secret_subkey) = msk.subkeys.get_mut(partition) {
if let Some(secret_subkey) = msk.subkeys.get_latest_mut(partition) {
// update the master secret and public subkey if needed
match (write_status, mpk.subkeys.get_mut(partition)) {
(EncryptDecrypt, None) => unreachable!(),
Expand Down Expand Up @@ -419,7 +416,7 @@ pub fn rekey(
for coordinate in coordinates {
let is_hybridized = EncryptionHint::new(
msk.subkeys
.get_current_revision(&coordinate)
.get_latest(&coordinate)
.and_then(|(sk_i, _)| sk_i.as_ref())
.is_some(),
);
Expand Down Expand Up @@ -544,12 +541,12 @@ mod tests {
let (mut msk, mut mpk) = setup(&mut rng, &partitions_set);

// The admin partition matches a hybridized sub-key.
let admin_secret_subkeys = msk.subkeys.get_current_revision(&admin_partition);
let admin_secret_subkeys = msk.subkeys.get_latest(&admin_partition);
assert!(admin_secret_subkeys.is_some());
assert!(admin_secret_subkeys.unwrap().0.is_some());

// The developer partition matches a classic sub-key.
let dev_secret_subkeys = msk.subkeys.get_current_revision(&dev_partition);
let dev_secret_subkeys = msk.subkeys.get_latest(&dev_partition);
assert!(dev_secret_subkeys.is_some());
assert!(dev_secret_subkeys.unwrap().0.is_none());

Expand Down Expand Up @@ -594,12 +591,12 @@ mod tests {
refresh(&msk, &mut dev_usk, true)?;

// The dev partition matches a hybridized sub-key.
let dev_secret_subkeys = msk.subkeys.get_current_revision(&dev_partition);
let dev_secret_subkeys = msk.subkeys.get_latest(&dev_partition);
assert!(dev_secret_subkeys.is_some());
assert!(dev_secret_subkeys.unwrap().0.is_some());

// The client partition matches a classic sub-key.
let client_secret_subkeys = msk.subkeys.get_current_revision(&client_partition);
let client_secret_subkeys = msk.subkeys.get_latest(&client_partition);
assert!(client_secret_subkeys.is_some());
assert!(client_secret_subkeys.unwrap().0.is_none());

Expand Down Expand Up @@ -740,20 +737,18 @@ mod tests {
assert!(!usk.subkeys.flat_iter().any(|x| {
x == (
&partition_1,
old_msk.subkeys.get_current_revision(&partition_1).unwrap(),
)
}));
assert!(usk.subkeys.flat_iter().any(|x| {
x == (
&partition_2,
msk.subkeys.get_current_revision(&partition_2).unwrap(),
old_msk.subkeys.get_latest(&partition_1).unwrap(),
)
}));
assert!(usk
.subkeys
.flat_iter()
.any(|x| { x == (&partition_2, msk.subkeys.get_latest(&partition_2).unwrap(),) }));
// user key kept the old hybrid key for partition 3
assert!(!usk.subkeys.flat_iter().any(|x| {
x == (
&partition_3,
old_msk.subkeys.get_current_revision(&partition_3).unwrap(),
old_msk.subkeys.get_latest(&partition_3).unwrap(),
)
}));

Expand Down
10 changes: 5 additions & 5 deletions src/data_struct/revision_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
}

/// Returns the last revised value for a given key.
pub fn get_current_revision<Q>(&self, key: &Q) -> Option<&V>
pub fn get_latest<Q>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Expand All @@ -101,7 +101,7 @@ where
}

/// Returns a mutable reference to the last revised value for a given key.
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_latest_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Expand Down Expand Up @@ -184,9 +184,9 @@ mod tests {
assert_eq!(map.count_elements(), 6);

// Get
assert_eq!(map.get_current_revision("Part1").unwrap(), "Part1V2");
assert_eq!(map.get_current_revision("Part2").unwrap(), "Part2V3");
assert!(map.get_current_revision("Missing").is_none());
assert_eq!(map.get_latest("Part1").unwrap(), "Part1V2");
assert_eq!(map.get_latest("Part2").unwrap(), "Part2V3");
assert!(map.get_latest("Missing").is_none());

// Iterators
let vec: Vec<_> = map.get("Part1").unwrap().iter().collect();
Expand Down
2 changes: 1 addition & 1 deletion src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod tests {
let part = Partition::from(vec![1, 6]);
usk.subkeys.create_chain_with_single_value(
part.clone(),
msk.subkeys.get_current_revision(&part).unwrap().clone(),
msk.subkeys.get_latest(&part).unwrap().clone(),
);
assert!(cover_crypt
.refresh_user_secret_key(&mut usk, &msk, false)
Expand Down

0 comments on commit 6bde142

Please sign in to comment.