Skip to main content

FeistelNetwork

Struct FeistelNetwork 

Source
pub struct FeistelNetwork {
    pub half_width: u64,
    pub right_mask: u64,
    pub left_mask: u64,
    /* private fields */
}
Expand description

Implements a Feistel network, which can take a non-invertible pseudo-random function (PRF) and turn it into an invertible pseudo-random permutation (PRP).

If you use this struct directly note that its intended purpose is to be a PRP and map from an n-bit input to an n-bit output, where n is an even positive integer. For example, if constructed with a max of 10, internally it creates a 4-bit Feistel network, and for all integers in the 4-bit domain [0, 16) (0 inclusive to 16 exclusive) it will map an input to one and only one output, and vice-versa (a given output maps to one and only one input). Even though you specified a max value of 10, the output range may be larger than expected. Clients like RandomPermutor handle this by excluding output values outside of the desired range.

This is useful in fields like cryptography, where a block cipher is a PRP.

Another great use of a Feistel network is when you want some input to always map to one and only one output (and vice versa). For example, given a 32-bit IP address, we could use some secret key and map each IP address to some other 32-bit IP address. We could log this new 32-bit IP address and people who do not know what the secret key is would find it difficult to determine what the input IP address was. This is Format Preserving Encryption (FPE).

Fields§

§half_width: u64

TODO visible just for testing, fix

§right_mask: u64

Mask used to keep within the width for the right. TODO visible just for testing, fix

§left_mask: u64

Mask used to keep within the width for the left. TODO visible just for testing, fix

Implementations§

Source§

impl FeistelNetwork

Source

pub fn new(max: u64) -> FeistelNetwork

Create a new FeistelNetwork instance that can give you a random permutation of integers.

Note that the value of max is rounded up to the nearest even power of 2. If clients are trying to get a permutation of [0, max) they need to iterate over the input range and discard values from FeistelNetwork >= max.

The key used for the permutation is made up of securely gathered 32 bytes.

Source

pub fn new_with_slice_key(max_value: u64, key: [u8; 32]) -> FeistelNetwork

Create a new FeistelNetwork instance that can give you a random permutation of integers.

Note that the value of max is rounded up to the nearest even power of 2. If clients are trying to get a permutation of [0, max) they need to iterate over the input range and discard values from FeistelNetwork >= max.

Source

pub fn permute(&self, input: u64) -> u64

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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V