Skip to content

Commit

Permalink
scrypt: Increase log_n until it is measurable
Browse files Browse the repository at this point in the history
Closes #418.
  • Loading branch information
str4d committed Dec 17, 2023
1 parent ed12d5f commit 4a8f448
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions age/src/scrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ const ENCRYPTED_FILE_KEY_BYTES: usize = FILE_KEY_BYTES + 16;
///
/// Guaranteed to return a valid work factor (less than 64).
fn target_scrypt_work_factor() -> u8 {
// Time a work factor that should always be fast.
let mut log_n = 10;

let duration: Option<Duration> = {
let measure_duration = |log_n| {
// Platforms that have a functional SystemTime::now():
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
{
Expand Down Expand Up @@ -61,6 +58,16 @@ fn target_scrypt_work_factor() -> u8 {
}
};

// Time a work factor that should always be fast.
let mut log_n = 10;
let mut duration: Option<Duration> = measure_duration(log_n);
while duration.map(|d| d.is_zero()).unwrap_or(false) {
// On some newer platforms, the work factor may be so fast that it is cannot be
// measured. Increase the work factor until we can measure something.
log_n += 1;
duration = measure_duration(log_n);
}

duration
.map(|mut d| {
// Use duration as a proxy for CPU usage, which scales linearly with N.
Expand Down

0 comments on commit 4a8f448

Please sign in to comment.