use crate::primitives::{PrpError, Symbol};
use crate::OreError;
pub(crate) const fn num_blocks_6bit(n: usize) -> usize {
(8 * n).div_ceil(6)
}
pub(crate) fn decompose_6bit(bytes: &[u8], out: &mut [u8]) {
for (i, slot) in out.iter_mut().enumerate() {
let bit = 6 * i;
let byte = bit / 8;
let offset = bit % 8;
let hi = *bytes.get(byte).unwrap_or(&0) as u16;
let lo = *bytes.get(byte + 1).unwrap_or(&0) as u16;
let window = (hi << 8) | lo;
*slot = ((window >> (10 - offset)) & 0x3f) as u8;
}
}
pub(crate) struct Blocks6<B>(B);
impl<B: AsRef<[u8]>> Blocks6<B> {
#[inline(never)]
pub(crate) fn check(blocks: B) -> Result<Self, OreError> {
let high = blocks.as_ref().iter().fold(0u8, |acc, &b| acc | (b >> 6));
if high != 0 {
return Err(OreError::PrpError(PrpError));
}
Ok(Self(blocks))
}
#[inline]
pub(crate) fn as_bytes(&self) -> &[u8] {
self.0.as_ref()
}
#[inline]
pub(crate) fn symbol(&self, i: usize) -> Symbol<64> {
Symbol::from_low_bits(self.0.as_ref()[i])
}
#[inline]
pub(crate) fn len(&self) -> usize {
self.0.as_ref().len()
}
#[inline]
pub(crate) fn inner(&self) -> &B {
&self.0
}
}
impl<B: AsRef<[u8]> + AsMut<[u8]>> Blocks6<B> {
pub(crate) fn decompose(bytes: &[u8], mut out: B) -> Self {
decompose_6bit(bytes, out.as_mut());
Self(out)
}
pub(crate) fn decompose_padded(bytes: &[u8], mut out: B) -> Self {
decompose_6bit(bytes, out.as_mut());
Self(out)
}
}
#[cfg(test)]
mod tests {
use super::*;
quickcheck! {
fn order_preserved_u64(x: u64, y: u64) -> bool {
let xb = x.to_be_bytes();
let yb = y.to_be_bytes();
let mut xq = [0u8; num_blocks_6bit(8)];
let mut yq = [0u8; num_blocks_6bit(8)];
decompose_6bit(&xb, &mut xq);
decompose_6bit(&yb, &mut yq);
xb.cmp(&yb) == xq.cmp(&yq)
}
fn order_preserved_any_lengths(a: Vec<u8>, b: Vec<u8>) -> bool {
let mut aq = vec![0u8; num_blocks_6bit(a.len())];
let mut bq = vec![0u8; num_blocks_6bit(b.len())];
decompose_6bit(&a, &mut aq);
decompose_6bit(&b, &mut bq);
a.cmp(&b) == aq.cmp(&bq)
}
fn order_preserved_proper_prefix(a: Vec<u8>, tail: Vec<u8>) -> bool {
let b: Vec<u8> = a.iter().chain(&tail).copied().collect();
let mut aq = vec![0u8; num_blocks_6bit(a.len())];
let mut bq = vec![0u8; num_blocks_6bit(b.len())];
decompose_6bit(&a, &mut aq);
decompose_6bit(&b, &mut bq);
a.cmp(&b) == aq.cmp(&bq)
}
fn blocks_in_domain(bytes: Vec<u8>) -> bool {
let mut out = vec![0u8; num_blocks_6bit(bytes.len())];
decompose_6bit(&bytes, &mut out);
out.iter().all(|&b| b < 64)
}
fn decompose_padded_matches_nul_padding(bytes: Vec<u8>, pad: u8) -> bool {
let mut padded = bytes.clone();
padded.resize(bytes.len() + usize::from(pad % 16), 0);
let n = num_blocks_6bit(padded.len());
let a = Blocks6::decompose_padded(&bytes, vec![0xffu8; n]);
let b = Blocks6::decompose(&padded, vec![0u8; n]);
a.as_bytes() == b.as_bytes()
}
fn roundtrip(bytes: Vec<u8>) -> bool {
let mut out = vec![0u8; num_blocks_6bit(bytes.len())];
decompose_6bit(&bytes, &mut out);
let mut rebuilt = vec![0u8; bytes.len()];
for (i, &v) in out.iter().enumerate() {
let bit = 6 * i;
let byte = bit / 8;
let offset = bit % 8;
let window = (v as u16) << (10 - offset);
rebuilt[byte] |= (window >> 8) as u8;
if byte + 1 < rebuilt.len() {
rebuilt[byte + 1] |= (window & 0xff) as u8;
}
}
rebuilt == bytes
}
}
#[test]
fn block_counts() {
assert_eq!(num_blocks_6bit(4), 6); assert_eq!(num_blocks_6bit(8), 11); assert_eq!(num_blocks_6bit(14), 19); assert_eq!(num_blocks_6bit(16), 22); assert_eq!(num_blocks_6bit(0), 0);
}
#[test]
fn blocks6_check_accepts_exactly_the_domain() {
assert!(Blocks6::check([0u8, 63, 17]).is_ok());
assert!(matches!(
Blocks6::check([0u8, 64, 17]),
Err(OreError::PrpError(_))
));
assert!(Blocks6::check([255u8]).is_err());
assert!(Blocks6::check(Vec::<u8>::new()).is_ok());
}
#[test]
fn known_packing() {
let mut out = [0u8; 3];
decompose_6bit(&[0b1111_1100, 0b0000_1111], &mut out);
assert_eq!(out, [0b111111, 0b000000, 0b111100]);
}
}
#[cfg(kani)]
mod kani_proofs {
use super::*;
const MAX_LEN: usize = 16;
const MAX_BLOCKS: usize = 22;
fn input_bit(bytes: &[u8], k: usize) -> u8 {
if k / 8 < bytes.len() {
(bytes[k / 8] >> (7 - k % 8)) & 1
} else {
0
}
}
#[kani::proof]
fn num_blocks_6bit_is_ceil_8n_over_6() {
let n: usize = kani::any();
kani::assume(n <= usize::MAX / 8);
let b = num_blocks_6bit(n);
assert!(6 * (b as u128) >= 8 * (n as u128));
if b > 0 {
assert!(6 * ((b - 1) as u128) < 8 * (n as u128));
}
}
#[kani::proof]
#[kani::unwind(23)]
fn decompose_6bit_matches_bit_spec() {
let buf: [u8; MAX_LEN] = kani::any();
let len: usize = kani::any();
kani::assume(len <= MAX_LEN);
let bytes = &buf[..len];
let nb = num_blocks_6bit(len);
let mut out_buf = [0u8; MAX_BLOCKS];
let out = &mut out_buf[..nb];
decompose_6bit(bytes, out);
let i: usize = kani::any();
kani::assume(i < nb);
assert!(out[i] < 64);
let t: usize = kani::any();
kani::assume(t < 6);
assert_eq!((out[i] >> (5 - t)) & 1, input_bit(bytes, 6 * i + t));
}
#[kani::proof]
#[kani::unwind(23)]
fn decompose_6bit_injective() {
let a_buf: [u8; MAX_LEN] = kani::any();
let b_buf: [u8; MAX_LEN] = kani::any();
let a_len: usize = kani::any();
let b_len: usize = kani::any();
kani::assume(a_len <= MAX_LEN && b_len <= MAX_LEN);
let a = &a_buf[..a_len];
let b = &b_buf[..b_len];
let mut a_out = [0u8; MAX_BLOCKS];
let mut b_out = [0u8; MAX_BLOCKS];
let a_nb = num_blocks_6bit(a_len);
let b_nb = num_blocks_6bit(b_len);
decompose_6bit(a, &mut a_out[..a_nb]);
decompose_6bit(b, &mut b_out[..b_nb]);
if a_out[..a_nb] == b_out[..b_nb] {
assert!(a == b);
}
}
#[kani::proof]
#[kani::unwind(23)]
fn decompose_6bit_preserves_order() {
let a_buf: [u8; MAX_LEN] = kani::any();
let b_buf: [u8; MAX_LEN] = kani::any();
let a_len: usize = kani::any();
let b_len: usize = kani::any();
kani::assume(a_len <= MAX_LEN && b_len <= MAX_LEN);
let a = &a_buf[..a_len];
let b = &b_buf[..b_len];
let a_nb = num_blocks_6bit(a_len);
let b_nb = num_blocks_6bit(b_len);
let mut a_out = [0u8; MAX_BLOCKS];
let mut b_out = [0u8; MAX_BLOCKS];
decompose_6bit(a, &mut a_out[..a_nb]);
decompose_6bit(b, &mut b_out[..b_nb]);
assert!(a.cmp(b) == a_out[..a_nb].cmp(&b_out[..b_nb]));
}
}