Skip to content

Commit

Permalink
Rename shares to retrieved_shares in RecipientPickCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Jan 15, 2025
1 parent 969f3b3 commit 5eeac66
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
26 changes: 16 additions & 10 deletions libparsec/crates/client/src/invite/claimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ pub async fn claimer_retrieve_info(
shamir_recovery_created_on,
recipients,
threshold,
shares: HashMap::new(),
retrieved_shares: HashMap::new(),
time_provider,
},
)),
Expand Down Expand Up @@ -335,7 +335,7 @@ pub struct ShamirRecoveryClaimPickRecipientCtx {
shamir_recovery_created_on: DateTime,
recipients: Vec<ShamirRecoveryRecipient>,
threshold: NonZeroU8,
shares: HashMap<UserID, Vec<ShamirShare>>,
retrieved_shares: HashMap<UserID, Vec<ShamirShare>>,
time_provider: TimeProvider,
}

Expand Down Expand Up @@ -369,8 +369,8 @@ impl ShamirRecoveryClaimPickRecipientCtx {
>= self.threshold.get()
}

pub fn shares(&self) -> HashMap<UserID, NonZeroU8> {
self.shares
pub fn retrieved_shares(&self) -> HashMap<UserID, NonZeroU8> {
self.retrieved_shares
.iter()
.filter_map(|(k, v)| {
u8::try_from(v.len())
Expand Down Expand Up @@ -398,7 +398,7 @@ impl ShamirRecoveryClaimPickRecipientCtx {
let greeter_user_id = recipient.user_id;
let greeter_human_handle = recipient.human_handle.clone();

if self.shares.contains_key(&greeter_user_id) {
if self.retrieved_shares.contains_key(&greeter_user_id) {
return Err(ShamirRecoveryClaimPickRecipientError::RecipientAlreadyPicked);
}

Expand All @@ -415,7 +415,7 @@ impl ShamirRecoveryClaimPickRecipientCtx {
self,
share_ctx: ShamirRecoveryClaimShare,
) -> Result<ShamirRecoveryClaimMaybeRecoverDeviceCtx, ShamirRecoveryClaimAddShareError> {
let mut shares = self.shares;
let mut retrieved_shares = self.retrieved_shares;

self.recipients
.iter()
Expand All @@ -425,13 +425,16 @@ impl ShamirRecoveryClaimPickRecipientCtx {
// Note that we do not check if the share is already present
// This is to avoid handling an extra error that has no real value,
// since adding shares can be seen as an idempotent operation.
shares.insert(share_ctx.recipient, share_ctx.weighted_share);
if shares.values().map(|shares| shares.len()).sum::<usize>()
retrieved_shares.insert(share_ctx.recipient, share_ctx.weighted_share);
if retrieved_shares
.values()
.map(|shares| shares.len())
.sum::<usize>()
>= self.threshold.get() as usize
{
let secret = ShamirRecoverySecret::decrypt_and_load_from_shares(
self.threshold,
shares.values().flatten(),
retrieved_shares.values().flatten(),
)
.map_err(ShamirRecoveryClaimAddShareError::CorruptedSecret)?;
Ok(ShamirRecoveryClaimMaybeRecoverDeviceCtx::RecoverDevice(
Expand All @@ -446,7 +449,10 @@ impl ShamirRecoveryClaimPickRecipientCtx {
))
} else {
Ok(ShamirRecoveryClaimMaybeRecoverDeviceCtx::PickRecipient(
Self { shares, ..self },
Self {
retrieved_shares,
..self
},
))
}
}
Expand Down
12 changes: 6 additions & 6 deletions libparsec/crates/client/tests/unit/invite/shamir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async fn shamir_full_greeting(tmp_path: TmpPath, env: &TestbedEnv) {
]
);
p_assert_eq!(alice_ctx.threshold(), 2.try_into().unwrap());
p_assert_eq!(alice_ctx.shares(), HashMap::new());
p_assert_eq!(alice_ctx.retrieved_shares(), HashMap::new());
assert!(alice_ctx.is_recoverable());
alice_ctx
}
Expand Down Expand Up @@ -154,7 +154,7 @@ async fn shamir_full_greeting(tmp_path: TmpPath, env: &TestbedEnv) {
ShamirRecoveryClaimMaybeRecoverDeviceCtx::PickRecipient(ctx) => ctx,
_ => panic!("Expected PickRecipient context"),
};
p_assert_eq!(alice_recipient_pick_ctx.shares().len(), 1);
p_assert_eq!(alice_recipient_pick_ctx.retrieved_shares().len(), 1);

// Continue with Bob

Expand Down Expand Up @@ -425,7 +425,7 @@ async fn unrecoverable_recovery(env: &TestbedEnv) {
]
);
p_assert_eq!(alice_ctx.threshold(), 2.try_into().unwrap());
p_assert_eq!(alice_ctx.shares(), HashMap::new());
p_assert_eq!(alice_ctx.retrieved_shares(), HashMap::new());
assert!(!alice_ctx.is_recoverable());
alice_ctx
}
Expand Down Expand Up @@ -546,7 +546,7 @@ async fn already_picked_recipient(env: &TestbedEnv) {
ShamirRecoveryClaimMaybeRecoverDeviceCtx::PickRecipient(ctx) => ctx,
_ => panic!("Expected PickRecipient context"),
};
p_assert_eq!(alice_recipient_pick_ctx.shares().len(), 1);
p_assert_eq!(alice_recipient_pick_ctx.retrieved_shares().len(), 1);

// Try to pick Mike again

Expand Down Expand Up @@ -724,7 +724,7 @@ async fn add_share_is_idempotent(env: &TestbedEnv) {
ShamirRecoveryClaimMaybeRecoverDeviceCtx::PickRecipient(ctx) => ctx,
_ => panic!("Expected PickRecipient context"),
};
p_assert_eq!(alice_recipient_pick_ctx.shares().len(), 1);
p_assert_eq!(alice_recipient_pick_ctx.retrieved_shares().len(), 1);

// Add the same share once again

Expand All @@ -735,5 +735,5 @@ async fn add_share_is_idempotent(env: &TestbedEnv) {
ShamirRecoveryClaimMaybeRecoverDeviceCtx::PickRecipient(ctx) => ctx,
_ => panic!("Expected PickRecipient context"),
};
p_assert_eq!(alice_recipient_pick_ctx.shares().len(), 1);
p_assert_eq!(alice_recipient_pick_ctx.retrieved_shares().len(), 1);
}
2 changes: 1 addition & 1 deletion libparsec/src/invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ pub fn claimer_shamir_recovery_add_share(
let shamir_recovery_created_on = ctx.shamir_recovery_created_on().to_owned();
let recipients = ctx.recipients().to_owned();
let threshold = ctx.threshold().to_owned();
let recovered_shares = ctx.shares().to_owned();
let recovered_shares = ctx.retrieved_shares().to_owned();
let is_recoverable = ctx.is_recoverable();
let new_handle = register_handle(HandleItem::ShamirRecoveryClaimPickRecipient(ctx));
Ok(ShamirRecoveryClaimMaybeRecoverDeviceInfo::PickRecipient {
Expand Down

0 comments on commit 5eeac66

Please sign in to comment.