97 lines
3.7 KiB
Rust
97 lines
3.7 KiB
Rust
//! Bucket padding (design: "Record envelope" and the profile padding rows).
|
|
//!
|
|
//! Padded form: `u32be(len) || data || zeros`, with the total rounded up to a
|
|
//! bucket so sizes leak less. Unpadding is strict: the fill must be all zeros.
|
|
|
|
use crate::CascadeError;
|
|
|
|
/// How sizes are rounded.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub struct Bucketing {
|
|
/// Bucket step in bytes below `linear_limit`.
|
|
pub step: usize,
|
|
/// Up to this size, buckets are multiples of `step`; above it, powers of two.
|
|
pub linear_limit: usize,
|
|
}
|
|
|
|
impl Bucketing {
|
|
/// Records at rest: 256-byte steps up to 4 KiB, then powers of two.
|
|
pub const RECORDS: Bucketing = Bucketing { step: 256, linear_limit: 4096 };
|
|
/// Standard and the floor: 256-byte buckets.
|
|
pub const WIRE_256: Bucketing = Bucketing { step: 256, linear_limit: 4096 };
|
|
/// High: 1 KiB buckets.
|
|
pub const WIRE_1K: Bucketing = Bucketing { step: 1024, linear_limit: 4096 };
|
|
/// Ultra: one 4 KiB bucket for every message (plus random extra steps).
|
|
pub const WIRE_4K: Bucketing = Bucketing { step: 4096, linear_limit: 4096 };
|
|
|
|
/// The bucket size for `n` bytes (including the 4-byte length prefix).
|
|
pub fn bucket(&self, n: usize) -> usize {
|
|
let step = self.step.max(1);
|
|
if n <= self.linear_limit {
|
|
n.div_ceil(step).max(1) * step
|
|
} else {
|
|
n.next_power_of_two()
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Pad `data`. `extra_steps` adds that many more `step`s (Ultra's random
|
|
/// extra; the caller draws it from its RNG port).
|
|
pub fn pad(data: &[u8], b: Bucketing, extra_steps: usize) -> Result<Vec<u8>, CascadeError> {
|
|
let len = u32::try_from(data.len()).map_err(|_| CascadeError::InvalidInput("too large to pad"))?;
|
|
let total = b.bucket(data.len() + 4) + extra_steps * b.step;
|
|
let mut out = Vec::with_capacity(total);
|
|
out.extend_from_slice(&len.to_be_bytes());
|
|
out.extend_from_slice(data);
|
|
out.resize(total, 0);
|
|
Ok(out)
|
|
}
|
|
|
|
/// Undo [`pad`]. Fails if the length prefix or the zero fill is wrong.
|
|
pub fn unpad(padded: &[u8]) -> Result<&[u8], CascadeError> {
|
|
let bad = CascadeError::InvalidInput("bad padding");
|
|
let prefix: [u8; 4] = padded.get(..4).and_then(|p| p.try_into().ok()).ok_or(bad.clone())?;
|
|
let len = u32::from_be_bytes(prefix) as usize;
|
|
let end = 4usize.checked_add(len).filter(|e| *e <= padded.len()).ok_or(bad.clone())?;
|
|
if padded[end..].iter().any(|b| *b != 0) {
|
|
return Err(bad);
|
|
}
|
|
Ok(&padded[4..end])
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn buckets() {
|
|
let r = Bucketing::RECORDS;
|
|
assert_eq!(r.bucket(1), 256);
|
|
assert_eq!(r.bucket(256), 256);
|
|
assert_eq!(r.bucket(257), 512);
|
|
assert_eq!(r.bucket(4096), 4096);
|
|
assert_eq!(r.bucket(4097), 8192);
|
|
assert_eq!(Bucketing::WIRE_4K.bucket(10), 4096);
|
|
assert_eq!(Bucketing::WIRE_1K.bucket(1500), 2048);
|
|
}
|
|
|
|
#[test]
|
|
fn round_trip_and_strict_unpad() {
|
|
for n in [0usize, 1, 251, 252, 253, 4092, 4093, 10_000] {
|
|
let data: Vec<u8> = (0..n).map(|i| i as u8).collect();
|
|
let p = pad(&data, Bucketing::RECORDS, 0).unwrap();
|
|
assert_eq!(p.len(), Bucketing::RECORDS.bucket(n + 4));
|
|
assert_eq!(unpad(&p).unwrap(), data.as_slice());
|
|
}
|
|
let p = pad(b"hi", Bucketing::WIRE_256, 2).unwrap();
|
|
assert_eq!(p.len(), 256 * 3);
|
|
let mut bad = p.clone();
|
|
*bad.last_mut().unwrap() = 1;
|
|
assert!(unpad(&bad).is_err(), "non-zero fill");
|
|
let mut bad = p.clone();
|
|
bad[..4].copy_from_slice(&10_000u32.to_be_bytes());
|
|
assert!(unpad(&bad).is_err(), "length past end");
|
|
assert!(unpad(&[0, 0]).is_err());
|
|
}
|
|
}
|