Skip to main content

StaticCodex

Struct StaticCodex 

Source
pub struct StaticCodex { /* private fields */ }
Expand description

Involution-based byte-swap codex backed by a 256-byte lookup table.

The table is held in a crate-internal LockedBytes buffer (mlock’d, zeroed on drop) since knowledge of the table is equivalent to knowledge of the transformation.

Implementations§

Source§

impl StaticCodex

Source

pub fn from_swaps(swaps: &[(u8, u8)]) -> Result<Self>

Construct a codex from a list of swap pairs.

Each (a, b) in swaps means “byte a encodes to b and b encodes to a.” Bytes not mentioned in any pair are fixed points (encode to themselves). Self-swaps ((x, x)) are accepted as no-ops.

§Errors

Returns Error::Codex if a byte appears in more than one swap pair — that would force the table to disagree with itself and break the involution property.

§Examples
use key_vault::{Codex, StaticCodex};

// Swap the ASCII digit '0' with '#' and 'A' with '@'.
let codex = StaticCodex::from_swaps(&[(b'0', b'#'), (b'A', b'@')]).unwrap();
assert_eq!(codex.encode(b'0'), b'#');
assert_eq!(codex.encode(b'#'), b'0');
assert_eq!(codex.encode(b'B'), b'B'); // not in any swap, fixed point
// Involution holds:
for byte in 0u8..=255 {
    assert_eq!(codex.decode(codex.encode(byte)), byte);
}
Source

pub fn random_involution() -> Result<Self>

Generate a random involution with no fixed points.

Pairs up all 256 bytes uniformly at random and writes the resulting permutation into the lookup table. Every byte transforms to a different byte.

§Errors

Returns Error::Internal if the OS CSPRNG fails — same failure mode as everywhere else in the crate.

§Examples
use key_vault::{Codex, StaticCodex};

let codex = StaticCodex::random_involution().unwrap();
// Involution: applying it twice returns the original byte.
for byte in 0u8..=255 {
    assert_eq!(codex.decode(codex.encode(byte)), byte);
}
// No fixed points: every byte transforms to a different byte.
for byte in 0u8..=255 {
    assert_ne!(codex.encode(byte), byte);
}

Trait Implementations§

Source§

impl Codex for StaticCodex

Source§

fn encode(&self, byte: u8) -> u8

Transform a byte on the way into storage.
Source§

fn decode(&self, byte: u8) -> u8

Transform a byte on the way out of storage. Read more
Source§

impl Debug for StaticCodex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more