use crate::{Result, format::Header};
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Params {
log_n: u8,
r: u32,
p: u32,
}
impl Params {
#[inline]
pub fn new(ciphertext: impl AsRef<[u8]>) -> Result<Self> {
let inner = |ciphertext: &[u8]| -> Result<Self> {
let params = Header::parse(ciphertext).map(|h| h.params())?;
Ok(params)
};
inner(ciphertext.as_ref())
}
#[must_use]
#[inline]
pub const fn log_n(&self) -> u8 {
self.log_n
}
#[must_use]
#[inline]
pub const fn n(&self) -> u64 {
1 << self.log_n
}
#[must_use]
#[inline]
pub const fn r(&self) -> u32 {
self.r
}
#[must_use]
#[inline]
pub const fn p(&self) -> u32 {
self.p
}
}
impl From<Params> for scrypt::Params {
#[inline]
fn from(params: Params) -> Self {
Self::new(
params.log_n(),
params.r(),
params.p(),
Self::RECOMMENDED_LEN,
)
.expect("`Params` should be valid as `scrypt::Params`")
}
}
impl From<scrypt::Params> for Params {
#[inline]
fn from(params: scrypt::Params) -> Self {
let (log_n, r, p) = (params.log_n(), params.r(), params.p());
Self { log_n, r, p }
}
}